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 39412851..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::kGet); + 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 60361d9d..67536d3e 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; + METHOD_ADD(Attachment::get, "/", 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..63a76748 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; - 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"); - 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}", 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: + 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 8ce63936..b95e7212 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,13 @@ 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 &filtersAndMethods = 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,11 +59,11 @@ class HttpApiController : public DrObject if (pattern[0] == '/') HttpAppFramework::registerHttpApiMethod(path + pattern, std::forward(function), - filters); + filtersAndMethods); else HttpAppFramework::registerHttpApiMethod(path + "/" + pattern, std::forward(function), - filters); + filtersAndMethods); } private: diff --git a/lib/inc/drogon/HttpAppFramework.h b/lib/inc/drogon/HttpAppFramework.h index 51467137..7e2b8167 100755 --- a/lib/inc/drogon/HttpAppFramework.h +++ b/lib/inc/drogon/HttpAppFramework.h @@ -74,11 +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 &filters = std::vector()) + const std::vector &filtersAndMethods = std::vector()) { LOG_TRACE << "pathPattern:" << pathPattern; HttpApiBinderBasePtr binder; @@ -86,7 +86,31 @@ class HttpAppFramework : public trantor::NonCopyable binder = std::make_shared< HttpApiBinder>(std::forward(function)); - instance().registerHttpApiController(pathPattern, binder, filters); + 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; virtual void disableSession() = 0; @@ -115,6 +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 &filters = std::vector()) = 0; }; } // namespace drogon diff --git a/lib/inc/drogon/HttpRequest.h b/lib/inc/drogon/HttpRequest.h index c4be37b2..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 - { - kInvalid, - kGet, - kPost, - kHead, - kPut, - kDelete - }; + 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 a166f0a0..7f871c8f 100755 --- a/lib/inc/drogon/HttpSimpleController.h +++ b/lib/inc/drogon/HttpSimpleController.h @@ -20,16 +20,13 @@ #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...) __registerSelf(path, {filters}); #define PATH_LIST_END \ - return vet; \ } namespace drogon { @@ -48,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 @@ -55,13 +57,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: diff --git a/lib/src/DeleteFilter.cc b/lib/src/DeleteFilter.cc index 78186ea0..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::kDelete) + if (req->method() == 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..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::kPost) + 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 5582e9bb..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::kGet) + if (req->method() == 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..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,11 +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 &filters) { //path will be like /api/v1/service/method/{1}/{2}/xxx... @@ -207,11 +239,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._validMethodsFlags.resize(Invalid, 0); + for (auto method : validMethods) + { + _binder._validMethodsFlags[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 +262,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) { @@ -856,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; @@ -945,6 +1001,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._validMethodsFlags.size() > 0) + { + assert(binder._validMethodsFlags.size() > req->method()); + if (binder._validMethodsFlags[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..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,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; @@ -129,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; @@ -152,6 +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 _validMethodsFlags; std::regex _regex; }; //std::unordered_map_apiCtrlMap; diff --git a/lib/src/HttpRequestImpl.cc b/lib/src/HttpRequestImpl.cc index 15e16354..2065a79d 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::Get); req->setVersion(drogon::HttpRequest::kHttp11); return req; } diff --git a/lib/src/HttpRequestImpl.h b/lib/src/HttpRequestImpl.h index 0f7fe17e..b4b58599 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,10 +100,10 @@ class HttpRequestImpl : public HttpRequest _parameters.clear(); _headers.clear(); } - return _method != kInvalid; + 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; } @@ -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; + 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 7dabc253..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::kHead); + bool _isHeadMethod = (req->method() == Head); if (_isHeadMethod) { - req->setMethod(HttpRequest::kGet); + 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 705169d2..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::kPost) + if (req->method() == 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..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::kPut) + if (req->method() == 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 +}