From acc26c077609591721c09db1334745934d8ed615 Mon Sep 17 00:00:00 2001 From: antao Date: Sat, 20 Oct 2018 11:04:01 +0800 Subject: [PATCH 1/4] Add Http method constraints --- examples/client_example/main.cc | 2 +- examples/simple_example/api_Attachment.h | 23 ++++++++-------- examples/simple_example/api_v1_ApiTest.h | 6 ++--- lib/inc/drogon/HttpApiController.h | 21 ++++++++------- lib/inc/drogon/HttpAppFramework.h | 4 ++- lib/inc/drogon/HttpRequest.h | 12 ++++----- lib/src/DeleteFilter.cc | 4 +-- lib/src/FileUpload.cc | 2 +- lib/src/GetFilter.cc | 4 +-- lib/src/HttpAppFrameworkImpl.cc | 30 ++++++++++++++++++--- lib/src/HttpAppFrameworkImpl.h | 3 +++ lib/src/HttpRequestImpl.cc | 22 +++++++-------- lib/src/HttpRequestImpl.h | 34 ++++++++++++------------ lib/src/HttpServer.cc | 4 +-- lib/src/PostFilter.cc | 4 +-- lib/src/PutFilter.cc | 4 +-- 16 files changed, 104 insertions(+), 75 deletions(-) diff --git a/examples/client_example/main.cc b/examples/client_example/main.cc index 39412851..517574af 100644 --- a/examples/client_example/main.cc +++ b/examples/client_example/main.cc @@ -7,7 +7,7 @@ int main() auto client = HttpClient::newHttpClient("http://www.baidu.com"); auto req = HttpRequest::newHttpRequest(); - req->setMethod(drogon::HttpRequest::kGet); + req->setMethod(drogon::HttpRequest::Get); req->setPath("/s"); req->setParameter("wd", "weixin"); int count = 0; diff --git a/examples/simple_example/api_Attachment.h b/examples/simple_example/api_Attachment.h index 60361d9d..d37f4881 100644 --- a/examples/simple_example/api_Attachment.h +++ b/examples/simple_example/api_Attachment.h @@ -5,17 +5,16 @@ namespace api { class Attachment : public drogon::HttpApiController { - public: - METHOD_LIST_BEGIN - //use METHOD_ADD to add your custom processing function here; - METHOD_ADD(Attachment::get, "/", "drogon::GetFilter"); - METHOD_ADD(Attachment::upload, "/upload", "drogon::PostFilter"); - - METHOD_LIST_END - //your declaration of processing function maybe like this: - void get(const HttpRequestPtr &req, - const std::function &callback); - void upload(const HttpRequestPtr &req, - const std::function &callback); +public: + METHOD_LIST_BEGIN + //use METHOD_ADD to add your custom processing function here; + registerMethod(&Attachment::get, "/", {HttpRequest::Post}); + + METHOD_LIST_END + //your declaration of processing function maybe like this: + void get(const HttpRequestPtr &req, + const std::function &callback); + void upload(const HttpRequestPtr &req, + const std::function &callback); }; } // namespace api diff --git a/examples/simple_example/api_v1_ApiTest.h b/examples/simple_example/api_v1_ApiTest.h index 35972fc7..89a7f6a7 100755 --- a/examples/simple_example/api_v1_ApiTest.h +++ b/examples/simple_example/api_v1_ApiTest.h @@ -10,9 +10,9 @@ class ApiTest : public drogon::HttpApiController public: METHOD_LIST_BEGIN //use METHOD_ADD to add your custom processing function here; - METHOD_ADD(ApiTest::get, "/get/{2}/{1}", "drogon::GetFilter"); //path will be /api/v1/apitest/get/{arg2}/{arg1} - METHOD_ADD(ApiTest::your_method_name, "/{1}/List?P2={2}", "drogon::GetFilter"); //path will be /api/v1/apitest/{arg1}/list - METHOD_ADD(ApiTest::staticApi, "/static"); + registerMethod(&ApiTest::get, "/get/{2}/{1}", {},{"drogon::GetFilter"}); //path will be /api/v1/apitest/get/{arg2}/{arg1} + registerMethod(&ApiTest::your_method_name, "/{1}/List?P2={2}", {HttpRequest::Get}); //path will be /api/v1/apitest/{arg1}/list + registerMethod(&ApiTest::staticApi, "/static",{HttpRequest::Get,HttpRequest::Post}); METHOD_ADD(ApiTest::get2, "/get/{1}", "drogon::GetFilter"); METHOD_LIST_END //your declaration of processing function maybe like this: diff --git a/lib/inc/drogon/HttpApiController.h b/lib/inc/drogon/HttpApiController.h index 8ce63936..5e02e64f 100755 --- a/lib/inc/drogon/HttpApiController.h +++ b/lib/inc/drogon/HttpApiController.h @@ -24,10 +24,10 @@ #define METHOD_LIST_BEGIN \ static void initMethods() \ { -#define METHOD_ADD(method, pattern, filters...) \ - { \ - std::string methodName = ""; \ - registerMethod(methodName, pattern, &method, {filters}); \ + +#define METHOD_ADD(method, pattern, filters...) \ + { \ + registerMethod(&method, pattern, {}, {filters}); \ } #define METHOD_LIST_END \ @@ -43,15 +43,14 @@ class HttpApiController : public DrObject { protected: template - static void registerMethod(const std::string &methodName, const std::string &pattern, FUNCTION &&function, const std::vector &filters) + static void registerMethod(FUNCTION &&function, + const std::string &pattern, + const std::vector &validMethods = std::vector(), + const std::vector &filters = std::vector()) { std::string path = std::string("/") + HttpApiController::classTypeName(); LOG_TRACE << "classname:" << HttpApiController::classTypeName(); - if (!methodName.empty()) - { - path.append("/"); - path.append(methodName); - } + //transform(path.begin(), path.end(), path.begin(), tolower); std::string::size_type pos; while ((pos = path.find("::")) != std::string::npos) @@ -61,10 +60,12 @@ class HttpApiController : public DrObject if (pattern[0] == '/') HttpAppFramework::registerHttpApiMethod(path + pattern, std::forward(function), + validMethods, filters); else HttpAppFramework::registerHttpApiMethod(path + "/" + pattern, std::forward(function), + validMethods, filters); } diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 51467137..84d582b9 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -78,6 +78,7 @@ class HttpAppFramework : public trantor::NonCopyable template static void registerHttpApiMethod(const std::string &pathPattern, FUNCTION &&function, + const std::vector &validMethods = std::vector(), const std::vector &filters = std::vector()) { LOG_TRACE << "pathPattern:" << pathPattern; @@ -86,7 +87,7 @@ class HttpAppFramework : public trantor::NonCopyable binder = std::make_shared< HttpApiBinder>(std::forward(function)); - instance().registerHttpApiController(pathPattern, binder, filters); + instance().registerHttpApiController(pathPattern, binder, validMethods, filters); } virtual void enableSession(const size_t timeout = 0) = 0; virtual void disableSession() = 0; @@ -115,6 +116,7 @@ class HttpAppFramework : public trantor::NonCopyable private: virtual void registerHttpApiController(const std::string &pathPattern, const HttpApiBinderBasePtr &binder, + const std::vector &validMethods = std::vector(), const std::vector &filters = std::vector()) = 0; }; } // namespace drogon diff --git a/lib/inc/drogon/HttpRequest.h b/lib/inc/drogon/HttpRequest.h index c4be37b2..4e7c34d1 100755 --- a/lib/inc/drogon/HttpRequest.h +++ b/lib/inc/drogon/HttpRequest.h @@ -39,12 +39,12 @@ class HttpRequest }; enum Method { - kInvalid, - kGet, - kPost, - kHead, - kPut, - kDelete + Get = 0, + Post, + Head, + Put, + Delete, + Invalid }; virtual const char *methodString() const = 0; virtual Method method() const = 0; diff --git a/lib/src/DeleteFilter.cc b/lib/src/DeleteFilter.cc index 78186ea0..2208de2a 100755 --- a/lib/src/DeleteFilter.cc +++ b/lib/src/DeleteFilter.cc @@ -19,7 +19,7 @@ void DeleteFilter::doFilter(const HttpRequestPtr &req, const FilterCallback &fcb, const FilterChainCallback &fccb) { - if (req->method() == HttpRequest::kDelete) + if (req->method() == HttpRequest::Delete) { fccb(); return; @@ -27,4 +27,4 @@ void DeleteFilter::doFilter(const HttpRequestPtr &req, auto res = drogon::HttpResponse::newHttpResponse(); res->setStatusCode(HttpResponse::k405MethodNotAllowed); fcb(res); -} \ No newline at end of file +} diff --git a/lib/src/FileUpload.cc b/lib/src/FileUpload.cc index 71d10bab..3e696ff5 100755 --- a/lib/src/FileUpload.cc +++ b/lib/src/FileUpload.cc @@ -21,7 +21,7 @@ const std::map &FileUpload::getPremeter() const }; int FileUpload::parse(const HttpRequestPtr &req) { - if (req->method() != HttpRequest::kPost) + if (req->method() != HttpRequest::Post) return -1; std::string contentType = req->getHeader("Content-Type"); if (contentType.empty()) diff --git a/lib/src/GetFilter.cc b/lib/src/GetFilter.cc index 5582e9bb..2e61e440 100755 --- a/lib/src/GetFilter.cc +++ b/lib/src/GetFilter.cc @@ -19,7 +19,7 @@ void GetFilter::doFilter(const HttpRequestPtr &req, const FilterCallback &fcb, const FilterChainCallback &fccb) { - if (req->method() == HttpRequest::kGet) + if (req->method() == HttpRequest::Get) { fccb(); return; @@ -27,4 +27,4 @@ void GetFilter::doFilter(const HttpRequestPtr &req, auto res = drogon::HttpResponse::newHttpResponse(); res->setStatusCode(HttpResponse::k405MethodNotAllowed); fcb(res); -} \ No newline at end of file +} diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index b9eb5599..3cf4d851 100755 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -151,6 +151,7 @@ void HttpAppFrameworkImpl::registerHttpSimpleController(const std::string &pathN } void HttpAppFrameworkImpl::addApiPath(const std::string &path, const HttpApiBinderBasePtr &binder, + const std::vector &validMethods, const std::vector &filters) { //path will be like /api/v1/service/method/{1}/{2}/xxx... @@ -207,11 +208,22 @@ void HttpAppFrameworkImpl::addApiPath(const std::string &path, _binder.binderPtr = binder; _binder.filtersName = filters; _binder.pathParameterPattern = std::regex_replace(originPath, regex, "([^/]*)"); - std::lock_guard guard(_apiCtrlMutex); - _apiCtrlVector.push_back(std::move(_binder)); + if (validMethods.size() > 0) + { + _binder._validMethodsFlag.resize(HttpRequest::Invalid, 0); + for (auto method : validMethods) + { + _binder._validMethodsFlag[method] = 1; + } + } + { + std::lock_guard guard(_apiCtrlMutex); + _apiCtrlVector.push_back(std::move(_binder)); + } } void HttpAppFrameworkImpl::registerHttpApiController(const std::string &pathPattern, const HttpApiBinderBasePtr &binder, + const std::vector &validMethods, const std::vector &filters) { assert(!pathPattern.empty()); @@ -219,7 +231,7 @@ void HttpAppFrameworkImpl::registerHttpApiController(const std::string &pathPatt std::string path(pathPattern); //std::transform(path.begin(), path.end(), path.begin(), tolower); - addApiPath(path, binder, filters); + addApiPath(path, binder, validMethods, filters); } void HttpAppFrameworkImpl::setThreadNum(size_t threadNum) { @@ -945,6 +957,18 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr &req, const std:: size_t ctlIndex = i - 1; auto &binder = _apiCtrlVector[ctlIndex]; LOG_TRACE << "got api access,regex=" << binder.pathParameterPattern; + if (binder._validMethodsFlag.size() > 0) + { + assert(binder._validMethodsFlag.size() > req->method()); + if (binder._validMethodsFlag[req->method()] == 0) + { + //Invalid Http Method + auto res = drogon::HttpResponse::newHttpResponse(); + res->setStatusCode(HttpResponse::k405MethodNotAllowed); + callback(res); + return; + } + } auto &filters = binder.filtersName; doFilters(filters, req, callback, needSetJsessionid, session_id, [=]() { auto &binder = _apiCtrlVector[ctlIndex]; diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index 1d0a627c..c37301b0 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -88,6 +88,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework private: virtual void registerHttpApiController(const std::string &pathPattern, const HttpApiBinderBasePtr &binder, + const std::vector &validMethods = std::vector(), const std::vector &filters = std::vector()) override; std::vector> _listeners; @@ -101,6 +102,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework void readSendFile(const std::string &filePath, const HttpRequestPtr &req, const HttpResponsePtr &resp); void addApiPath(const std::string &path, const HttpApiBinderBasePtr &binder, + const std::vector &validMethods, const std::vector &filters); void initRegex(); //if uuid package found,we can use a uuid string as session id; @@ -152,6 +154,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework std::vector filtersName; std::unique_ptr binderMtx = std::unique_ptr(new std::mutex); std::weak_ptr responsePtr; + std::vector _validMethodsFlag; std::regex _regex; }; //std::unordered_map_apiCtrlMap; diff --git a/lib/src/HttpRequestImpl.cc b/lib/src/HttpRequestImpl.cc index 15e16354..563e0b1f 100755 --- a/lib/src/HttpRequestImpl.cc +++ b/lib/src/HttpRequestImpl.cc @@ -34,7 +34,7 @@ void HttpRequestImpl::parsePremeter() std::transform(type.begin(), type.end(), type.begin(), tolower); const std::string &input = query(); - if (_method == kGet || (_method == kPost && (type == "" || type.find("application/x-www-form-urlencoded") != std::string::npos))) + if (_method == Get || (_method == Post && (type == "" || type.find("application/x-www-form-urlencoded") != std::string::npos))) { std::string::size_type pos = 0; @@ -104,21 +104,21 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const { switch (_method) { - case kDelete: - output->append("DELETE "); - break; - case kGet: + case Get: output->append("GET "); break; - case kHead: - output->append("HEAD "); - break; - case kPost: + case Post: output->append("POST "); break; - case kPut: + case Head: + output->append("HEAD "); + break; + case Put: output->append("PUT "); break; + case Delete: + output->append("DELETE "); + break; default: return; } @@ -192,7 +192,7 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const HttpRequestPtr HttpRequest::newHttpRequest() { auto req = std::make_shared(); - req->setMethod(drogon::HttpRequest::kGet); + req->setMethod(drogon::HttpRequest::Get); req->setVersion(drogon::HttpRequest::kHttp11); return req; } diff --git a/lib/src/HttpRequestImpl.h b/lib/src/HttpRequestImpl.h index 0f7fe17e..b21bbae8 100755 --- a/lib/src/HttpRequestImpl.h +++ b/lib/src/HttpRequestImpl.h @@ -47,7 +47,7 @@ class HttpRequestImpl : public HttpRequest friend class HttpContext; HttpRequestImpl() - : _method(kInvalid), + : _method(Invalid), _version(kUnknown), contentLen(0) { @@ -66,33 +66,33 @@ class HttpRequestImpl : public HttpRequest bool setMethod(const char *start, const char *end) { - assert(_method == kInvalid); + assert(_method == Invalid); std::string m(start, end); if (m == "GET") { - _method = kGet; + _method = Get; } else if (m == "POST") { - _method = kPost; + _method = Post; } else if (m == "HEAD") { - _method = kHead; + _method = Head; } else if (m == "PUT") { - _method = kPut; + _method = Put; } else if (m == "DELETE") { - _method = kDelete; + _method = Delete; } else { - _method = kInvalid; + _method = Invalid; } - if (_method != kInvalid) + if (_method != Invalid) { content_ = ""; _query = ""; @@ -100,7 +100,7 @@ class HttpRequestImpl : public HttpRequest _parameters.clear(); _headers.clear(); } - return _method != kInvalid; + return _method != Invalid; } virtual void setMethod(const Method method) override @@ -124,19 +124,19 @@ class HttpRequestImpl : public HttpRequest const char *result = "UNKNOWN"; switch (_method) { - case kGet: + case Get: result = "GET"; break; - case kPost: + case Post: result = "POST"; break; - case kHead: + case Head: result = "HEAD"; break; - case kPut: + case Put: result = "PUT"; break; - case kDelete: + case Delete: result = "DELETE"; break; default: @@ -179,7 +179,7 @@ class HttpRequestImpl : public HttpRequest { if (_query != "") return _query; - if (_method == kPost) + if (_method == Post) return content_; return _query; } @@ -360,7 +360,7 @@ class HttpRequestImpl : public HttpRequest } private: - Method _method = kGet; + Method _method = Get; Version _version = kHttp11; std::string _path; std::string _query; diff --git a/lib/src/HttpServer.cc b/lib/src/HttpServer.cc index 7dabc253..6450ca06 100755 --- a/lib/src/HttpServer.cc +++ b/lib/src/HttpServer.cc @@ -162,10 +162,10 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestPtr &r bool _close = connection == "close" || (req->getVersion() == HttpRequestImpl::kHttp10 && connection != "Keep-Alive"); - bool _isHeadMethod = (req->method() == HttpRequest::kHead); + bool _isHeadMethod = (req->method() == HttpRequest::Head); if (_isHeadMethod) { - req->setMethod(HttpRequest::kGet); + req->setMethod(HttpRequest::Get); } HttpContext *context = any_cast(conn->getMutableContext()); //request will be received in same thread,so we don't need mutex; diff --git a/lib/src/PostFilter.cc b/lib/src/PostFilter.cc index 705169d2..62abbb72 100755 --- a/lib/src/PostFilter.cc +++ b/lib/src/PostFilter.cc @@ -19,7 +19,7 @@ void PostFilter::doFilter(const HttpRequestPtr &req, const FilterCallback &fcb, const FilterChainCallback &fccb) { - if (req->method() == HttpRequest::kPost) + if (req->method() == HttpRequest::Post) { fccb(); return; @@ -27,4 +27,4 @@ void PostFilter::doFilter(const HttpRequestPtr &req, auto res = drogon::HttpResponse::newHttpResponse(); res->setStatusCode(HttpResponse::k405MethodNotAllowed); fcb(res); -} \ No newline at end of file +} diff --git a/lib/src/PutFilter.cc b/lib/src/PutFilter.cc index f4a567fb..be829bde 100755 --- a/lib/src/PutFilter.cc +++ b/lib/src/PutFilter.cc @@ -19,7 +19,7 @@ void PutFilter::doFilter(const HttpRequestPtr &req, const FilterCallback &fcb, const FilterChainCallback &fccb) { - if (req->method() == HttpRequest::kPut) + if (req->method() == HttpRequest::Put) { fccb(); return; @@ -27,4 +27,4 @@ void PutFilter::doFilter(const HttpRequestPtr &req, auto res = drogon::HttpResponse::newHttpResponse(); res->setStatusCode(HttpResponse::k405MethodNotAllowed); fcb(res); -} \ No newline at end of file +} From 6745d703db0d07195a3ac040e051ed28e0d8595d Mon Sep 17 00:00:00 2001 From: antao Date: Sat, 20 Oct 2018 11:50:56 +0800 Subject: [PATCH 2/4] Modify HttpSimpleController.h --- lib/inc/drogon/HttpSimpleController.h | 23 +++++++++-------------- 1 file changed, 9 insertions(+), 14 deletions(-) diff --git a/lib/inc/drogon/HttpSimpleController.h b/lib/inc/drogon/HttpSimpleController.h index a166f0a0..8db0a84b 100755 --- a/lib/inc/drogon/HttpSimpleController.h +++ b/lib/inc/drogon/HttpSimpleController.h @@ -20,16 +20,17 @@ #include #include #include -#define PATH_LIST_BEGIN \ - static std::vector>> paths() \ - { \ - std::vector>> vet; +#define PATH_LIST_BEGIN \ + static void ___paths___() \ + { -#define PATH_ADD(path, filters...) \ - vet.push_back({path, {filters}}) +#define PATH_ADD(path, filters...) \ + { \ + LOG_TRACE << "register simple controller(" << classTypeName() << ") on path:" << path; \ + HttpAppFramework::instance().registerHttpSimpleController(path, classTypeName(), {filters}); \ + } #define PATH_LIST_END \ - return vet; \ } namespace drogon { @@ -55,13 +56,7 @@ class HttpSimpleController : public DrObject, public HttpSimpleControllerBase public: pathRegister() { - auto vPaths = T::paths(); - - for (auto path : vPaths) - { - LOG_TRACE << "register simple controller(" << HttpSimpleController::classTypeName() << ") on path:" << path.first; - HttpAppFramework::instance().registerHttpSimpleController(path.first, HttpSimpleController::classTypeName(), path.second); - } + T::___paths___(); } protected: From eef0ee8729b359c754db29211fbdc5a489d4cea6 Mon Sep 17 00:00:00 2001 From: antao Date: Sat, 20 Oct 2018 12:40:30 +0800 Subject: [PATCH 3/4] Use 'any' to support two types of constraints --- examples/simple_example/api_Attachment.h | 2 +- examples/simple_example/api_v1_ApiTest.h | 26 +++++++++---------- lib/inc/drogon/HttpApiController.h | 33 ++++++++++++++++++++---- 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/examples/simple_example/api_Attachment.h b/examples/simple_example/api_Attachment.h index d37f4881..cc433884 100644 --- a/examples/simple_example/api_Attachment.h +++ b/examples/simple_example/api_Attachment.h @@ -8,7 +8,7 @@ class Attachment : public drogon::HttpApiController public: METHOD_LIST_BEGIN //use METHOD_ADD to add your custom processing function here; - registerMethod(&Attachment::get, "/", {HttpRequest::Post}); + METHOD_ADD(Attachment::get, "/", HttpRequest::Post); METHOD_LIST_END //your declaration of processing function maybe like this: diff --git a/examples/simple_example/api_v1_ApiTest.h b/examples/simple_example/api_v1_ApiTest.h index 89a7f6a7..f355ef88 100755 --- a/examples/simple_example/api_v1_ApiTest.h +++ b/examples/simple_example/api_v1_ApiTest.h @@ -7,19 +7,19 @@ namespace v1 { class ApiTest : public drogon::HttpApiController { - public: - METHOD_LIST_BEGIN - //use METHOD_ADD to add your custom processing function here; - registerMethod(&ApiTest::get, "/get/{2}/{1}", {},{"drogon::GetFilter"}); //path will be /api/v1/apitest/get/{arg2}/{arg1} - registerMethod(&ApiTest::your_method_name, "/{1}/List?P2={2}", {HttpRequest::Get}); //path will be /api/v1/apitest/{arg1}/list - registerMethod(&ApiTest::staticApi, "/static",{HttpRequest::Get,HttpRequest::Post}); - METHOD_ADD(ApiTest::get2, "/get/{1}", "drogon::GetFilter"); - METHOD_LIST_END - //your declaration of processing function maybe like this: - void get(const HttpRequestPtr &req, const std::function &callback, int p1, std::string &&p2); - void your_method_name(const HttpRequestPtr &req, const std::function &callback, double p1, int p2) const; - void staticApi(const HttpRequestPtr &req, const std::function &callback); - void get2(const HttpRequestPtr &req, const std::function &callback, std::string &&p1); +public: + METHOD_LIST_BEGIN + //use METHOD_ADD to add your custom processing function here; + METHOD_ADD(ApiTest::get, "/get/{2}/{1}", "drogon::GetFilter"); //path will be /api/v1/apitest/get/{arg2}/{arg1} + METHOD_ADD(ApiTest::your_method_name, "/{1}/List?P2={2}", HttpRequest::Get); //path will be /api/v1/apitest/{arg1}/list + METHOD_ADD(ApiTest::staticApi, "/static", HttpRequest::Get, HttpRequest::Post); + METHOD_ADD(ApiTest::get2, "/get/{1}", "drogon::GetFilter"); + METHOD_LIST_END + //your declaration of processing function maybe like this: + void get(const HttpRequestPtr &req, const std::function &callback, int p1, std::string &&p2); + void your_method_name(const HttpRequestPtr &req, const std::function &callback, double p1, int p2) const; + void staticApi(const HttpRequestPtr &req, const std::function &callback); + void get2(const HttpRequestPtr &req, const std::function &callback, std::string &&p1); }; } // namespace v1 } // namespace api diff --git a/lib/inc/drogon/HttpApiController.h b/lib/inc/drogon/HttpApiController.h index 5e02e64f..483045ac 100755 --- a/lib/inc/drogon/HttpApiController.h +++ b/lib/inc/drogon/HttpApiController.h @@ -25,9 +25,9 @@ static void initMethods() \ { -#define METHOD_ADD(method, pattern, filters...) \ - { \ - registerMethod(&method, pattern, {}, {filters}); \ +#define METHOD_ADD(method, pattern, filters...) \ + { \ + registerMethod(&method, pattern, {filters}); \ } #define METHOD_LIST_END \ @@ -45,14 +45,37 @@ class HttpApiController : public DrObject template static void registerMethod(FUNCTION &&function, const std::string &pattern, - const std::vector &validMethods = std::vector(), - const std::vector &filters = std::vector()) + const std::vector &filtersAndMethods = std::vector()) { std::string path = std::string("/") + HttpApiController::classTypeName(); LOG_TRACE << "classname:" << HttpApiController::classTypeName(); //transform(path.begin(), path.end(), path.begin(), tolower); std::string::size_type pos; + + std::vector validMethods; + std::vector filters; + for (auto &filterOrMethod : filtersAndMethods) + { + if (filterOrMethod.type() == typeid(std::string)) + { + filters.push_back(*any_cast(&filterOrMethod)); + } + else if (filterOrMethod.type() == typeid(const char *)) + { + filters.push_back(*any_cast(&filterOrMethod)); + } + else if (filterOrMethod.type() == typeid(HttpRequest::Method)) + { + validMethods.push_back(*any_cast(&filterOrMethod)); + } + else + { + std::cerr << "Invalid controller constraint type:" << filterOrMethod.type().name() << std::endl; + LOG_ERROR << "Invalid controller constraint type"; + exit(1); + } + } while ((pos = path.find("::")) != std::string::npos) { path.replace(pos, 2, "/"); From fcb2ab7880d14fd5713f83b4c6ea41a5cd06764c Mon Sep 17 00:00:00 2001 From: antao Date: Sat, 20 Oct 2018 13:45:17 +0800 Subject: [PATCH 4/4] HttpMethod --- drogon_ctl/create_controller.cc | 4 +- examples/client_example/main.cc | 2 +- examples/simple_example/ListParaCtl.h | 2 +- examples/simple_example/TestController.h | 20 ++++---- examples/simple_example/api_Attachment.h | 2 +- examples/simple_example/api_v1_ApiTest.h | 4 +- lib/inc/drogon/HttpApiController.h | 30 +----------- lib/inc/drogon/HttpAppFramework.h | 31 ++++++++++-- lib/inc/drogon/HttpRequest.h | 23 ++++----- lib/inc/drogon/HttpSimpleController.h | 13 ++--- lib/src/DeleteFilter.cc | 2 +- lib/src/FileUpload.cc | 2 +- lib/src/GetFilter.cc | 2 +- lib/src/HttpAppFrameworkImpl.cc | 62 ++++++++++++++++++++---- lib/src/HttpAppFrameworkImpl.h | 11 +++-- lib/src/HttpRequestImpl.cc | 2 +- lib/src/HttpRequestImpl.h | 6 +-- lib/src/HttpServer.cc | 4 +- lib/src/PostFilter.cc | 2 +- lib/src/PutFilter.cc | 2 +- 20 files changed, 135 insertions(+), 91 deletions(-) diff --git a/drogon_ctl/create_controller.cc b/drogon_ctl/create_controller.cc index 838c7b0c..0dc146a0 100755 --- a/drogon_ctl/create_controller.cc +++ b/drogon_ctl/create_controller.cc @@ -218,7 +218,7 @@ void create_controller::newSimpleControllerHeaderFile(std::ofstream &file, const file << indent << " PATH_LIST_BEGIN\n"; file << indent << " //list path definitions here;\n"; - file << indent << " //PATH_ADD(\"/path\",\"filter1\",\"filter2\",...);\n"; + file << indent << " //PATH_ADD(\"/path\",\"filter1\",\"filter2\",HttpMethod1,HttpMethod2...);\n"; file << indent << " PATH_LIST_END\n"; file << indent << "};\n"; if (indent == "") @@ -367,7 +367,7 @@ void create_controller::newApiControllerHeaderFile(std::ofstream &file, const st indent.append(" "); file << indent << "METHOD_LIST_BEGIN\n"; file << indent << "//use METHOD_ADD to add your custom processing function here;\n"; - file << indent << "//METHOD_ADD(" << class_name << "::get,\"/get/{2}/{1}\",\"drogon::GetFilter\");" + file << indent << "//METHOD_ADD(" << class_name << "::get,\"/get/{2}/{1}\",Get);" "//path will be " << namepace_path << class_name << "/get/{arg2}/{arg1}\n"; file << indent << "//METHOD_ADD(" << class_name << "::your_method_name,\"/{1}/{2}/list\",\"drogon::GetFilter\");" diff --git a/examples/client_example/main.cc b/examples/client_example/main.cc index 517574af..45346652 100644 --- a/examples/client_example/main.cc +++ b/examples/client_example/main.cc @@ -7,7 +7,7 @@ int main() auto client = HttpClient::newHttpClient("http://www.baidu.com"); auto req = HttpRequest::newHttpRequest(); - req->setMethod(drogon::HttpRequest::Get); + req->setMethod(drogon::Get); req->setPath("/s"); req->setParameter("wd", "weixin"); int count = 0; diff --git a/examples/simple_example/ListParaCtl.h b/examples/simple_example/ListParaCtl.h index 9759287c..af0ffbb1 100755 --- a/examples/simple_example/ListParaCtl.h +++ b/examples/simple_example/ListParaCtl.h @@ -8,6 +8,6 @@ class ListParaCtl : public drogon::HttpSimpleController PATH_LIST_BEGIN //list path definations here; //PATH_ADD("/path","filter1","filter2",...); - PATH_ADD("/listpara"); + PATH_ADD("/listpara",Get); PATH_LIST_END }; diff --git a/examples/simple_example/TestController.h b/examples/simple_example/TestController.h index f6c52b73..4bae623a 100755 --- a/examples/simple_example/TestController.h +++ b/examples/simple_example/TestController.h @@ -5,15 +5,15 @@ namespace example { class TestController : public drogon::HttpSimpleController { - public: - virtual void asyncHandleHttpRequest(const HttpRequestPtr &req, const std::function &callback) override; - PATH_LIST_BEGIN - //list path definations here; - //PATH_ADD("/path","filter1","filter2",...); - PATH_ADD("/"); - PATH_ADD("/Test", "nonFilter"); - PATH_ADD("/tpost", "drogon::PostFilter"); - PATH_ADD("/slow", "TimeFilter"); - PATH_LIST_END +public: + virtual void asyncHandleHttpRequest(const HttpRequestPtr &req, const std::function &callback) override; + PATH_LIST_BEGIN + //list path definations here; + //PATH_ADD("/path","filter1","filter2",...); + PATH_ADD("/"); + PATH_ADD("/Test", "nonFilter"); + PATH_ADD("/tpost", "drogon::PostFilter"); + PATH_ADD("/slow", "TimeFilter", Get); + PATH_LIST_END }; } // namespace example diff --git a/examples/simple_example/api_Attachment.h b/examples/simple_example/api_Attachment.h index cc433884..67536d3e 100644 --- a/examples/simple_example/api_Attachment.h +++ b/examples/simple_example/api_Attachment.h @@ -8,7 +8,7 @@ class Attachment : public drogon::HttpApiController public: METHOD_LIST_BEGIN //use METHOD_ADD to add your custom processing function here; - METHOD_ADD(Attachment::get, "/", HttpRequest::Post); + METHOD_ADD(Attachment::get, "/", Post); METHOD_LIST_END //your declaration of processing function maybe like this: diff --git a/examples/simple_example/api_v1_ApiTest.h b/examples/simple_example/api_v1_ApiTest.h index f355ef88..63a76748 100755 --- a/examples/simple_example/api_v1_ApiTest.h +++ b/examples/simple_example/api_v1_ApiTest.h @@ -11,8 +11,8 @@ public: METHOD_LIST_BEGIN //use METHOD_ADD to add your custom processing function here; METHOD_ADD(ApiTest::get, "/get/{2}/{1}", "drogon::GetFilter"); //path will be /api/v1/apitest/get/{arg2}/{arg1} - METHOD_ADD(ApiTest::your_method_name, "/{1}/List?P2={2}", HttpRequest::Get); //path will be /api/v1/apitest/{arg1}/list - METHOD_ADD(ApiTest::staticApi, "/static", HttpRequest::Get, HttpRequest::Post); + METHOD_ADD(ApiTest::your_method_name, "/{1}/List?P2={2}", Get); //path will be /api/v1/apitest/{arg1}/list + METHOD_ADD(ApiTest::staticApi, "/static", Get, Post); METHOD_ADD(ApiTest::get2, "/get/{1}", "drogon::GetFilter"); METHOD_LIST_END //your declaration of processing function maybe like this: diff --git a/lib/inc/drogon/HttpApiController.h b/lib/inc/drogon/HttpApiController.h index 483045ac..b95e7212 100755 --- a/lib/inc/drogon/HttpApiController.h +++ b/lib/inc/drogon/HttpApiController.h @@ -52,30 +52,6 @@ class HttpApiController : public DrObject //transform(path.begin(), path.end(), path.begin(), tolower); std::string::size_type pos; - - std::vector validMethods; - std::vector filters; - for (auto &filterOrMethod : filtersAndMethods) - { - if (filterOrMethod.type() == typeid(std::string)) - { - filters.push_back(*any_cast(&filterOrMethod)); - } - else if (filterOrMethod.type() == typeid(const char *)) - { - filters.push_back(*any_cast(&filterOrMethod)); - } - else if (filterOrMethod.type() == typeid(HttpRequest::Method)) - { - validMethods.push_back(*any_cast(&filterOrMethod)); - } - else - { - std::cerr << "Invalid controller constraint type:" << filterOrMethod.type().name() << std::endl; - LOG_ERROR << "Invalid controller constraint type"; - exit(1); - } - } while ((pos = path.find("::")) != std::string::npos) { path.replace(pos, 2, "/"); @@ -83,13 +59,11 @@ class HttpApiController : public DrObject if (pattern[0] == '/') HttpAppFramework::registerHttpApiMethod(path + pattern, std::forward(function), - validMethods, - filters); + filtersAndMethods); else HttpAppFramework::registerHttpApiMethod(path + "/" + pattern, std::forward(function), - validMethods, - filters); + filtersAndMethods); } private: diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 84d582b9..7e2b8167 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -74,12 +74,11 @@ class HttpAppFramework : public trantor::NonCopyable std::vector()) = 0; virtual void registerHttpSimpleController(const std::string &pathName, const std::string &crtlName, - const std::vector &filters = std::vector()) = 0; + const std::vector &filtersAndMethods = std::vector()) = 0; template static void registerHttpApiMethod(const std::string &pathPattern, FUNCTION &&function, - const std::vector &validMethods = std::vector(), - const std::vector &filters = std::vector()) + const std::vector &filtersAndMethods = std::vector()) { LOG_TRACE << "pathPattern:" << pathPattern; HttpApiBinderBasePtr binder; @@ -87,6 +86,30 @@ class HttpAppFramework : public trantor::NonCopyable binder = std::make_shared< HttpApiBinder>(std::forward(function)); + std::vector validMethods; + std::vector filters; + for (auto &filterOrMethod : filtersAndMethods) + { + if (filterOrMethod.type() == typeid(std::string)) + { + filters.push_back(*any_cast(&filterOrMethod)); + } + else if (filterOrMethod.type() == typeid(const char *)) + { + filters.push_back(*any_cast(&filterOrMethod)); + } + else if (filterOrMethod.type() == typeid(HttpMethod)) + { + validMethods.push_back(*any_cast(&filterOrMethod)); + } + else + { + std::cerr << "Invalid controller constraint type:" << filterOrMethod.type().name() << std::endl; + LOG_ERROR << "Invalid controller constraint type"; + exit(1); + } + } + instance().registerHttpApiController(pathPattern, binder, validMethods, filters); } virtual void enableSession(const size_t timeout = 0) = 0; @@ -116,7 +139,7 @@ class HttpAppFramework : public trantor::NonCopyable private: virtual void registerHttpApiController(const std::string &pathPattern, const HttpApiBinderBasePtr &binder, - const std::vector &validMethods = std::vector(), + const std::vector &validMethods = std::vector(), const std::vector &filters = std::vector()) = 0; }; } // namespace drogon diff --git a/lib/inc/drogon/HttpRequest.h b/lib/inc/drogon/HttpRequest.h index 4e7c34d1..2bca049c 100755 --- a/lib/inc/drogon/HttpRequest.h +++ b/lib/inc/drogon/HttpRequest.h @@ -25,6 +25,15 @@ namespace drogon { class HttpRequest; typedef std::shared_ptr HttpRequestPtr; +enum HttpMethod +{ + Get = 0, + Post, + Head, + Put, + Delete, + Invalid +}; /* * abstract class for webapp developer to get Http client request; * */ @@ -37,17 +46,9 @@ class HttpRequest kHttp10 = 1, kHttp11 = 2 }; - enum Method - { - Get = 0, - Post, - Head, - Put, - Delete, - Invalid - }; + virtual const char *methodString() const = 0; - virtual Method method() const = 0; + virtual HttpMethod method() const = 0; virtual std::string getHeader(const std::string &field) const = 0; virtual std::string getCookie(const std::string &field) const = 0; virtual const std::map &headers() const = 0; @@ -63,7 +64,7 @@ class HttpRequest virtual ~HttpRequest() {} virtual const std::shared_ptr getJsonObject() const = 0; - virtual void setMethod(const Method method) = 0; + virtual void setMethod(const HttpMethod method) = 0; virtual void setPath(const std::string &path) = 0; virtual void setParameter(const std::string &key, const std::string &value) = 0; static HttpRequestPtr newHttpRequest(); diff --git a/lib/inc/drogon/HttpSimpleController.h b/lib/inc/drogon/HttpSimpleController.h index 8db0a84b..7f871c8f 100755 --- a/lib/inc/drogon/HttpSimpleController.h +++ b/lib/inc/drogon/HttpSimpleController.h @@ -20,15 +20,11 @@ #include #include #include -#define PATH_LIST_BEGIN \ +#define PATH_LIST_BEGIN \ static void ___paths___() \ { -#define PATH_ADD(path, filters...) \ - { \ - LOG_TRACE << "register simple controller(" << classTypeName() << ") on path:" << path; \ - HttpAppFramework::instance().registerHttpSimpleController(path, classTypeName(), {filters}); \ - } +#define PATH_ADD(path, filters...) __registerSelf(path, {filters}); #define PATH_LIST_END \ } @@ -49,6 +45,11 @@ class HttpSimpleController : public DrObject, public HttpSimpleControllerBase protected: HttpSimpleController() {} + static void __registerSelf(const std::string &path, const std::vector &filtersAndMethods) + { + LOG_TRACE << "register simple controller(" << HttpSimpleController::classTypeName() << ") on path:" << path; + HttpAppFramework::instance().registerHttpSimpleController(path, HttpSimpleController::classTypeName(), filtersAndMethods); + } private: class pathRegister diff --git a/lib/src/DeleteFilter.cc b/lib/src/DeleteFilter.cc index 2208de2a..5f2cfdf1 100755 --- a/lib/src/DeleteFilter.cc +++ b/lib/src/DeleteFilter.cc @@ -19,7 +19,7 @@ void DeleteFilter::doFilter(const HttpRequestPtr &req, const FilterCallback &fcb, const FilterChainCallback &fccb) { - if (req->method() == HttpRequest::Delete) + if (req->method() == Delete) { fccb(); return; diff --git a/lib/src/FileUpload.cc b/lib/src/FileUpload.cc index 3e696ff5..abf07d15 100755 --- a/lib/src/FileUpload.cc +++ b/lib/src/FileUpload.cc @@ -21,7 +21,7 @@ const std::map &FileUpload::getPremeter() const }; int FileUpload::parse(const HttpRequestPtr &req) { - if (req->method() != HttpRequest::Post) + if (req->method() != Post) return -1; std::string contentType = req->getHeader("Content-Type"); if (contentType.empty()) diff --git a/lib/src/GetFilter.cc b/lib/src/GetFilter.cc index 2e61e440..419af90e 100755 --- a/lib/src/GetFilter.cc +++ b/lib/src/GetFilter.cc @@ -19,7 +19,7 @@ void GetFilter::doFilter(const HttpRequestPtr &req, const FilterCallback &fcb, const FilterChainCallback &fccb) { - if (req->method() == HttpRequest::Get) + if (req->method() == Get) { fccb(); return; diff --git a/lib/src/HttpAppFrameworkImpl.cc b/lib/src/HttpAppFrameworkImpl.cc index 3cf4d851..d25e09fc 100755 --- a/lib/src/HttpAppFrameworkImpl.cc +++ b/lib/src/HttpAppFrameworkImpl.cc @@ -138,7 +138,7 @@ void HttpAppFrameworkImpl::registerWebSocketController(const std::string &pathNa } void HttpAppFrameworkImpl::registerHttpSimpleController(const std::string &pathName, const std::string &ctrlName, - const std::vector &filters) + const std::vector &filtersAndMethods) { assert(!pathName.empty()); assert(!ctrlName.empty()); @@ -146,12 +146,43 @@ void HttpAppFrameworkImpl::registerHttpSimpleController(const std::string &pathN std::string path(pathName); std::transform(pathName.begin(), pathName.end(), path.begin(), tolower); std::lock_guard guard(_simpCtrlMutex); + std::vector validMethods; + std::vector filters; + for (auto &filterOrMethod : filtersAndMethods) + { + if (filterOrMethod.type() == typeid(std::string)) + { + filters.push_back(*any_cast(&filterOrMethod)); + } + else if (filterOrMethod.type() == typeid(const char *)) + { + filters.push_back(*any_cast(&filterOrMethod)); + } + else if (filterOrMethod.type() == typeid(HttpMethod)) + { + validMethods.push_back(*any_cast(&filterOrMethod)); + } + else + { + std::cerr << "Invalid controller constraint type:" << filterOrMethod.type().name() << std::endl; + LOG_ERROR << "Invalid controller constraint type"; + exit(1); + } + } _simpCtrlMap[path].controllerName = ctrlName; _simpCtrlMap[path].filtersName = filters; + if (validMethods.size() > 0) + { + _simpCtrlMap[path]._validMethodsFlags.resize(Invalid, 0); + for (auto method : validMethods) + { + _simpCtrlMap[path]._validMethodsFlags[method] = 1; + } + } } void HttpAppFrameworkImpl::addApiPath(const std::string &path, const HttpApiBinderBasePtr &binder, - const std::vector &validMethods, + const std::vector &validMethods, const std::vector &filters) { //path will be like /api/v1/service/method/{1}/{2}/xxx... @@ -210,10 +241,10 @@ void HttpAppFrameworkImpl::addApiPath(const std::string &path, _binder.pathParameterPattern = std::regex_replace(originPath, regex, "([^/]*)"); if (validMethods.size() > 0) { - _binder._validMethodsFlag.resize(HttpRequest::Invalid, 0); + _binder._validMethodsFlags.resize(Invalid, 0); for (auto method : validMethods) { - _binder._validMethodsFlag[method] = 1; + _binder._validMethodsFlags[method] = 1; } } { @@ -223,7 +254,7 @@ void HttpAppFrameworkImpl::addApiPath(const std::string &path, } void HttpAppFrameworkImpl::registerHttpApiController(const std::string &pathPattern, const HttpApiBinderBasePtr &binder, - const std::vector &validMethods, + const std::vector &validMethods, const std::vector &filters) { assert(!pathPattern.empty()); @@ -868,7 +899,20 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr &req, const std:: if (_simpCtrlMap.find(pathLower) != _simpCtrlMap.end()) { - auto &filters = _simpCtrlMap[pathLower].filtersName; + auto &ctrlInfo = _simpCtrlMap[pathLower]; + if (ctrlInfo._validMethodsFlags.size() > 0) + { + assert(ctrlInfo._validMethodsFlags.size() > req->method()); + if (ctrlInfo._validMethodsFlags[req->method()] == 0) + { + //Invalid Http Method + auto res = drogon::HttpResponse::newHttpResponse(); + res->setStatusCode(HttpResponse::k405MethodNotAllowed); + callback(res); + return; + } + } + auto &filters = ctrlInfo.filtersName; doFilters(filters, req, callback, needSetJsessionid, session_id, [=]() { auto &ctrlItem = _simpCtrlMap[pathLower]; const std::string &ctrlName = ctrlItem.controllerName; @@ -957,10 +1001,10 @@ void HttpAppFrameworkImpl::onAsyncRequest(const HttpRequestPtr &req, const std:: size_t ctlIndex = i - 1; auto &binder = _apiCtrlVector[ctlIndex]; LOG_TRACE << "got api access,regex=" << binder.pathParameterPattern; - if (binder._validMethodsFlag.size() > 0) + if (binder._validMethodsFlags.size() > 0) { - assert(binder._validMethodsFlag.size() > req->method()); - if (binder._validMethodsFlag[req->method()] == 0) + assert(binder._validMethodsFlags.size() > req->method()); + if (binder._validMethodsFlags[req->method()] == 0) { //Invalid Http Method auto res = drogon::HttpResponse::newHttpResponse(); diff --git a/lib/src/HttpAppFrameworkImpl.h b/lib/src/HttpAppFrameworkImpl.h index c37301b0..44be39e7 100644 --- a/lib/src/HttpAppFrameworkImpl.h +++ b/lib/src/HttpAppFrameworkImpl.h @@ -51,8 +51,8 @@ class HttpAppFrameworkImpl : public HttpAppFramework std::vector()) override; virtual void registerHttpSimpleController(const std::string &pathName, const std::string &crtlName, - const std::vector &filters = - std::vector()) override; + const std::vector &filtersAndMethods = + std::vector()) override; virtual void enableSession(const size_t timeout = 0) override { _useSession = true; @@ -88,7 +88,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework private: virtual void registerHttpApiController(const std::string &pathPattern, const HttpApiBinderBasePtr &binder, - const std::vector &validMethods = std::vector(), + const std::vector &validMethods = std::vector(), const std::vector &filters = std::vector()) override; std::vector> _listeners; @@ -102,7 +102,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework void readSendFile(const std::string &filePath, const HttpRequestPtr &req, const HttpResponsePtr &resp); void addApiPath(const std::string &path, const HttpApiBinderBasePtr &binder, - const std::vector &validMethods, + const std::vector &validMethods, const std::vector &filters); void initRegex(); //if uuid package found,we can use a uuid string as session id; @@ -131,6 +131,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework { std::string controllerName; std::vector filtersName; + std::vector _validMethodsFlags; std::shared_ptr controller; std::weak_ptr responsePtr; std::mutex _mutex; @@ -154,7 +155,7 @@ class HttpAppFrameworkImpl : public HttpAppFramework std::vector filtersName; std::unique_ptr binderMtx = std::unique_ptr(new std::mutex); std::weak_ptr responsePtr; - std::vector _validMethodsFlag; + std::vector _validMethodsFlags; std::regex _regex; }; //std::unordered_map_apiCtrlMap; diff --git a/lib/src/HttpRequestImpl.cc b/lib/src/HttpRequestImpl.cc index 563e0b1f..2065a79d 100755 --- a/lib/src/HttpRequestImpl.cc +++ b/lib/src/HttpRequestImpl.cc @@ -192,7 +192,7 @@ void HttpRequestImpl::appendToBuffer(MsgBuffer *output) const HttpRequestPtr HttpRequest::newHttpRequest() { auto req = std::make_shared(); - req->setMethod(drogon::HttpRequest::Get); + req->setMethod(drogon::Get); req->setVersion(drogon::HttpRequest::kHttp11); return req; } diff --git a/lib/src/HttpRequestImpl.h b/lib/src/HttpRequestImpl.h index b21bbae8..b4b58599 100755 --- a/lib/src/HttpRequestImpl.h +++ b/lib/src/HttpRequestImpl.h @@ -103,7 +103,7 @@ class HttpRequestImpl : public HttpRequest return _method != Invalid; } - virtual void setMethod(const Method method) override + virtual void setMethod(const HttpMethod method) override { _method = method; content_ = ""; @@ -114,7 +114,7 @@ class HttpRequestImpl : public HttpRequest return; } - Method method() const override + HttpMethod method() const override { return _method; } @@ -360,7 +360,7 @@ class HttpRequestImpl : public HttpRequest } private: - Method _method = Get; + HttpMethod _method = Get; Version _version = kHttp11; std::string _path; std::string _query; diff --git a/lib/src/HttpServer.cc b/lib/src/HttpServer.cc index 6450ca06..cc8db995 100755 --- a/lib/src/HttpServer.cc +++ b/lib/src/HttpServer.cc @@ -162,10 +162,10 @@ void HttpServer::onRequest(const TcpConnectionPtr &conn, const HttpRequestPtr &r bool _close = connection == "close" || (req->getVersion() == HttpRequestImpl::kHttp10 && connection != "Keep-Alive"); - bool _isHeadMethod = (req->method() == HttpRequest::Head); + bool _isHeadMethod = (req->method() == Head); if (_isHeadMethod) { - req->setMethod(HttpRequest::Get); + req->setMethod(Get); } HttpContext *context = any_cast(conn->getMutableContext()); //request will be received in same thread,so we don't need mutex; diff --git a/lib/src/PostFilter.cc b/lib/src/PostFilter.cc index 62abbb72..cbdec201 100755 --- a/lib/src/PostFilter.cc +++ b/lib/src/PostFilter.cc @@ -19,7 +19,7 @@ void PostFilter::doFilter(const HttpRequestPtr &req, const FilterCallback &fcb, const FilterChainCallback &fccb) { - if (req->method() == HttpRequest::Post) + if (req->method() == Post) { fccb(); return; diff --git a/lib/src/PutFilter.cc b/lib/src/PutFilter.cc index be829bde..ab3a591c 100755 --- a/lib/src/PutFilter.cc +++ b/lib/src/PutFilter.cc @@ -19,7 +19,7 @@ void PutFilter::doFilter(const HttpRequestPtr &req, const FilterCallback &fcb, const FilterChainCallback &fccb) { - if (req->method() == HttpRequest::Put) + if (req->method() == Put) { fccb(); return;