From 1011d5211deb07e5c91558201605461a05a4a1a4 Mon Sep 17 00:00:00 2001 From: antao Date: Fri, 31 Aug 2018 15:17:09 +0800 Subject: [PATCH] Add LocalHostFilter --- examples/simple_example/JsonTestController.h | 2 +- examples/simple_example/main.cc | 12 ++++---- lib/inc/drogon/HttpAppFramework.h | 1 + lib/inc/drogon/LocalHostFilter.h | 29 ++++++++++++++++++++ lib/src/LocalHostFilter.cc | 29 ++++++++++++++++++++ 5 files changed, 66 insertions(+), 7 deletions(-) create mode 100644 lib/inc/drogon/LocalHostFilter.h create mode 100644 lib/src/LocalHostFilter.cc diff --git a/examples/simple_example/JsonTestController.h b/examples/simple_example/JsonTestController.h index e181a8d6..e7fb66ba 100755 --- a/examples/simple_example/JsonTestController.h +++ b/examples/simple_example/JsonTestController.h @@ -10,6 +10,6 @@ public: virtual void asyncHandleHttpRequest(const HttpRequestPtr& req,const std::function&callback)override; PATH_LIST_BEGIN - PATH_ADD("/json"); + PATH_ADD("/json","drogon::GetFilter","drogon::LocalHostFilter"); PATH_LIST_END }; \ No newline at end of file diff --git a/examples/simple_example/main.cc b/examples/simple_example/main.cc index be46ed64..a33ff633 100755 --- a/examples/simple_example/main.cc +++ b/examples/simple_example/main.cc @@ -85,12 +85,12 @@ int main() drogon::HttpAppFramework::instance().addListener("0.0.0.0",12345); drogon::HttpAppFramework::instance().addListener("0.0.0.0",8080); -#ifdef USE_OPENSSL - //https - drogon::HttpAppFramework::instance().setSSLFiles("server.pem","server.pem"); - drogon::HttpAppFramework::instance().addListener("0.0.0.0",4430,true); - drogon::HttpAppFramework::instance().addListener("0.0.0.0",4431,true); -#endif +//#ifdef USE_OPENSSL +// //https +// drogon::HttpAppFramework::instance().setSSLFiles("server.pem","server.pem"); +// drogon::HttpAppFramework::instance().addListener("0.0.0.0",4430,true); +// drogon::HttpAppFramework::instance().addListener("0.0.0.0",4431,true); +//#endif drogon::HttpAppFramework::instance().setThreadNum(4); trantor::Logger::setLogLevel(trantor::Logger::TRACE); //class function diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index e867227e..256f3847 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include diff --git a/lib/inc/drogon/LocalHostFilter.h b/lib/inc/drogon/LocalHostFilter.h new file mode 100644 index 00000000..5c2cfe40 --- /dev/null +++ b/lib/inc/drogon/LocalHostFilter.h @@ -0,0 +1,29 @@ +/** + * + * @file + * @author An Tao + * @section LICENSE + * + * Copyright 2018, An Tao. All rights reserved. + * Use of this source code is governed by a MIT license + * that can be found in the License file. + * + * @section DESCRIPTION + * + */ + +#pragma once + +#include + +namespace drogon +{ + class LocalHostFilter:public HttpFilter + { + public: + LocalHostFilter(){} + virtual void doFilter(const HttpRequestPtr& req, + const FilterCallback &fcb, + const FilterChainCallback &fccb) override ; + }; +} \ No newline at end of file diff --git a/lib/src/LocalHostFilter.cc b/lib/src/LocalHostFilter.cc new file mode 100644 index 00000000..043e1ae9 --- /dev/null +++ b/lib/src/LocalHostFilter.cc @@ -0,0 +1,29 @@ +/** + * + * @file + * @author An Tao + * @section LICENSE + * + * Copyright 2018, An Tao. All rights reserved. + * Use of this source code is governed by a MIT license + * that can be found in the License file. + * + * @section DESCRIPTION + * + */ + +#include +#include "HttpResponseImpl.h" +using namespace drogon; +void LocalHostFilter::doFilter(const HttpRequestPtr& req, + const FilterCallback &fcb, + const FilterChainCallback &fccb) +{ + if(req->peerAddr().toIp()=="127.0.0.1") + { + fccb(); + return; + } + auto res=drogon::HttpResponse::newNotFoundResponse(); + fcb(res); +} \ No newline at end of file