From 8105c65fa650eabdba52a9d7eb2e24891eaca20a Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Tue, 16 Oct 2018 23:55:25 +0200 Subject: [PATCH] Add all HTTP methods --- src/App.h | 39 +++++++++++++++++++++++++++++++++++++-- src/HttpContext.h | 38 +++++++++++++++++++------------------- src/HttpContextData.h | 7 +++---- 3 files changed, 59 insertions(+), 25 deletions(-) diff --git a/src/App.h b/src/App.h index d6e9307..e79e35d 100644 --- a/src/App.h +++ b/src/App.h @@ -28,12 +28,47 @@ public: } TemplatedApp &get(std::string pattern, std::function *, HttpRequest *)> handler) { - httpContext->onGet(pattern, handler); + httpContext->onHttp("get", pattern, handler); return *this; } TemplatedApp &post(std::string pattern, std::function *, HttpRequest *)> handler) { - httpContext->onPost(pattern, handler); + httpContext->onHttp("post", pattern, handler); + return *this; + } + + TemplatedApp &options(std::string pattern, std::function *, HttpRequest *)> handler) { + httpContext->onHttp("options", pattern, handler); + return *this; + } + + TemplatedApp &del(std::string pattern, std::function *, HttpRequest *)> handler) { + httpContext->onHttp("delete", pattern, handler); + return *this; + } + + TemplatedApp &patch(std::string pattern, std::function *, HttpRequest *)> handler) { + httpContext->onHttp("patch", pattern, handler); + return *this; + } + + TemplatedApp &put(std::string pattern, std::function *, HttpRequest *)> handler) { + httpContext->onHttp("put", pattern, handler); + return *this; + } + + TemplatedApp &head(std::string pattern, std::function *, HttpRequest *)> handler) { + httpContext->onHttp("head", pattern, handler); + return *this; + } + + TemplatedApp &connect(std::string pattern, std::function *, HttpRequest *)> handler) { + httpContext->onHttp("connect", pattern, handler); + return *this; + } + + TemplatedApp &trace(std::string pattern, std::function *, HttpRequest *)> handler) { + httpContext->onHttp("trace", pattern, handler); return *this; } diff --git a/src/HttpContext.h b/src/HttpContext.h index cc17ab6..c8c6aba 100644 --- a/src/HttpContext.h +++ b/src/HttpContext.h @@ -12,6 +12,9 @@ #include #include +/* This is a hack for now on, update uSockets */ +extern "C" int us_internal_socket_is_closed(struct us_socket *s); + namespace uWS { template struct HttpResponse; @@ -114,11 +117,10 @@ private: httpResponseData->offset = 0; httpResponseData->state = 0; - // route it! - typename uWS::HttpContextData::UserData userData = { - (HttpResponse *) s, httpRequest - }; - httpContextData->router.route(httpRequest->getMethod(), httpRequest->getUrl(), &userData); + /* Route the method and URL */ + httpContextData->router.route(httpRequest->getMethod(), httpRequest->getUrl(), { + (HttpResponse *) s, httpRequest + }); // here we can be closed and in shutdown? @@ -129,8 +131,14 @@ private: }, [](void *user) { // close any socket on HTTP errors //static_dispatch(us_ssl_socket_close, us_socket_close)((SOCKET_TYPE *) user); + }); + if (us_internal_socket_is_closed((struct us_socket *) s)) { + // do you really return s? I guess so? + return s; + } + // uncork only if not closed ((AsyncSocket *) s)->uncork(); @@ -219,31 +227,23 @@ public: } /* Register an HTTP GET route handler acording to URL pattern */ - void onGet(std::string pattern, std::function *, uWS::HttpRequest *)> handler) { + void onHttp(std::string method, std::string pattern, std::function *, uWS::HttpRequest *)> handler) { HttpContextData *httpContextData = getSocketContextData(); - httpContextData->router.add("get", pattern, [handler](typename HttpContextData::UserData *user, std::pair params) { + httpContextData->router.add(method, pattern, [handler](typename HttpContextData::RouterData user, std::pair params) { // todo: attach params to the req here! - user->httpRequest->setParameters(params); + user.httpRequest->setParameters(params); - handler(user->httpResponse, user->httpRequest); - }); - } - - void onPost(std::string pattern, std::function *, uWS::HttpRequest *)> handler) { - HttpContextData *httpContextData = getSocketContextData(); - - httpContextData->router.add("post", pattern, [handler](typename HttpContextData::UserData *user, std::pair params) { - handler(user->httpResponse, user->httpRequest); + handler(user.httpResponse, user.httpRequest); }); } void onUnhandled(std::function *, uWS::HttpRequest *)> handler) { HttpContextData *httpContextData = getSocketContextData(); - httpContextData->router.unhandled([handler](typename HttpContextData::UserData *user, std::pair params) { - handler(user->httpResponse, user->httpRequest); + httpContextData->router.unhandled([handler](typename HttpContextData::RouterData user, std::pair params) { + handler(user.httpResponse, user.httpRequest); }); } diff --git a/src/HttpContextData.h b/src/HttpContextData.h index 1dbb6d6..5dd8e91 100644 --- a/src/HttpContextData.h +++ b/src/HttpContextData.h @@ -11,15 +11,14 @@ struct HttpRequest; template struct HttpContextData { + template friend struct HttpContext; private: - -public: - struct UserData { + struct RouterData { HttpResponse *httpResponse; HttpRequest *httpRequest; }; - HttpRouter router; + HttpRouter router; }; }