Add LocalHostFilter

This commit is contained in:
antao 2018-08-31 15:17:09 +08:00
parent 6c444ec6ca
commit 1011d5211d
5 changed files with 66 additions and 7 deletions

View File

@ -10,6 +10,6 @@ public:
virtual void asyncHandleHttpRequest(const HttpRequestPtr& req,const std::function<void (const HttpResponsePtr &)>&callback)override;
PATH_LIST_BEGIN
PATH_ADD("/json");
PATH_ADD("/json","drogon::GetFilter","drogon::LocalHostFilter");
PATH_LIST_END
};

View File

@ -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

View File

@ -24,6 +24,7 @@
#include <drogon/GetFilter.h>
#include <drogon/PutFilter.h>
#include <drogon/DeleteFilter.h>
#include <drogon/LocalHostFilter.h>
#include <drogon/version.h>
#include <drogon/NotFound.h>
#include <drogon/HttpClient.h>

View File

@ -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 <drogon/HttpFilter.h>
namespace drogon
{
class LocalHostFilter:public HttpFilter<LocalHostFilter>
{
public:
LocalHostFilter(){}
virtual void doFilter(const HttpRequestPtr& req,
const FilterCallback &fcb,
const FilterChainCallback &fccb) override ;
};
}

View File

@ -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 <drogon/LocalHostFilter.h>
#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);
}