diff --git a/webcc/globals.h b/webcc/globals.h index 061fd33..2b66029 100644 --- a/webcc/globals.h +++ b/webcc/globals.h @@ -195,14 +195,18 @@ public: explicit Exception(Error error, const std::string& details = "", bool timeout = false); - Error error() const { return error_; } + Error error() const { + return error_; + } // Note that `noexcept` is required by GCC. const char* what() const WEBCC_NOEXCEPT override{ return msg_.c_str(); } - bool timeout() const { return timeout_; } + bool timeout() const { + return timeout_; + } private: Error error_; diff --git a/webcc/rest_request_handler.cc b/webcc/rest_request_handler.cc index 3fe7f07..a1581f5 100644 --- a/webcc/rest_request_handler.cc +++ b/webcc/rest_request_handler.cc @@ -20,11 +20,7 @@ void RestRequestHandler::HandleConnection(HttpConnectionPtr connection) { const Url& url = http_request->url(); - // TODO - RestRequest rest_request{ - http_request, - http_request->method(), http_request->content(), url.query() - }; + RestRequest rest_request{ http_request }; // Get service by URL path. std::string path = "/" + url.path(); diff --git a/webcc/rest_service.cc b/webcc/rest_service.cc index 99159b2..4ec0086 100644 --- a/webcc/rest_service.cc +++ b/webcc/rest_service.cc @@ -8,13 +8,16 @@ namespace webcc { void RestListService::Handle(const RestRequest& request, RestResponse* response) { - if (request.method == http::methods::kGet) { - Get(UrlQuery(request.url_query_str), response); - } else if (request.method == http::methods::kPost) { - Post(request.content, response); + const std::string& method = request.http->method(); + + if (method == http::methods::kGet) { + Get(UrlQuery(request.http->url().query()), response); + + } else if (method == http::methods::kPost) { + Post(request.http->content(), response); + } else { - LOG_ERRO("RestListService doesn't support '%s' method.", - request.method.c_str()); + LOG_ERRO("RestListService doesn't support '%s' method.", method.c_str()); } } @@ -22,17 +25,22 @@ void RestListService::Handle(const RestRequest& request, void RestDetailService::Handle(const RestRequest& request, RestResponse* response) { - if (request.method == http::methods::kGet) { - Get(request.url_matches, UrlQuery(request.url_query_str), response); - } else if (request.method == http::methods::kPut) { - Put(request.url_matches, request.content, response); - } else if (request.method == http::methods::kPatch) { - Patch(request.url_matches, request.content, response); - } else if (request.method == http::methods::kDelete) { + const std::string& method = request.http->method(); + + if (method == http::methods::kGet) { + Get(request.url_matches, UrlQuery(request.http->url().query()), response); + + } else if (method == http::methods::kPut) { + Put(request.url_matches, request.http->content(), response); + + } else if (method == http::methods::kPatch) { + Patch(request.url_matches, request.http->content(), response); + + } else if (method == http::methods::kDelete) { Delete(request.url_matches, response); + } else { - LOG_ERRO("RestDetailService doesn't support '%s' method.", - request.method.c_str()); + LOG_ERRO("RestDetailService doesn't support '%s' method.", method.c_str()); } } diff --git a/webcc/rest_service.h b/webcc/rest_service.h index 9e8c38f..bf04233 100644 --- a/webcc/rest_service.h +++ b/webcc/rest_service.h @@ -28,15 +28,6 @@ struct RestRequest { // Original HTTP request. HttpRequestPtr http; - // HTTP method (GET, POST, etc.). - const std::string& method; - - // Request content (JSON string). - const std::string& content; - - // Query string of the URL (only for GET). - const std::string& url_query_str; - // Regex sub-matches of the URL (usually resource ID's). UrlMatches url_matches; }; @@ -67,7 +58,7 @@ typedef std::shared_ptr RestServicePtr; class RestListService : public RestService { public: - void Handle(const RestRequest& request, RestResponse* response) final; + void Handle(const RestRequest& request, RestResponse* response) override; protected: virtual void Get(const UrlQuery& query, RestResponse* response) { @@ -82,7 +73,7 @@ protected: class RestDetailService : public RestService { public: - void Handle(const RestRequest& request, RestResponse* response) final; + void Handle(const RestRequest& request, RestResponse* response) override; protected: virtual void Get(const UrlMatches& url_matches,