diff --git a/design.dia b/design.dia index dcfbb85..9e05a8d 100644 Binary files a/design.dia and b/design.dia differ diff --git a/main.cpp b/main.cpp index f3bf5da..b08eccf 100644 --- a/main.cpp +++ b/main.cpp @@ -57,9 +57,11 @@ int main(int argc, char **argv) { HttpContext *httpContext = HttpContext::create(loop.loop); // req, res? - httpContext->onGet("/", [](auto *res) { // maybe use the terminology of HttpRequest for both? + httpContext->onGet("/", [](auto *res, auto *req) { // maybe use the terminology of HttpRequest for both? - std::cout << "Why hello! How do we access the headers?" << std::endl; + std::cout << "URL: <" << req->getUrl() << ">" << std::endl; + std::cout << "Query: <" << req->getQuery() << ">" << std::endl; + std::cout << "User-Agent: <" << req->getHeader("user-agent") << ">" << std::endl; // read some data being passed res->read([](std::string_view chunk) { diff --git a/src/new_design/HttpContext.h b/src/new_design/HttpContext.h index caa4606..835820a 100644 --- a/src/new_design/HttpContext.h +++ b/src/new_design/HttpContext.h @@ -12,6 +12,7 @@ // we of course depend on our own data type #include "HttpContextData.h" +#include "HttpResponseData.h" // we should opaqeuly depend on HttpResponse namespace uWS { @@ -67,15 +68,15 @@ public: static_dispatch(us_ssl_socket_timeout, us_socket_timeout)(s, HTTP_IDLE_TIMEOUT_S); - /*new (static_dispatch(us_ssl_socket_ext, us_socket_ext)(s)) HTTP_SOCKET_DATA_TYPE; -*/ + new (static_dispatch(us_ssl_socket_ext, us_socket_ext)(s)) HttpResponseData; + return s; }); static_dispatch(us_ssl_socket_context_on_close, us_socket_context_on_close)(getSocketContext(), [](auto *s) { HttpContextData *httpContextData = getSocketContextData(s); - //((HTTP_SOCKET_DATA_TYPE *) static_dispatch(us_ssl_socket_ext, us_socket_ext)(s))->~HTTP_SOCKET_DATA_TYPE(); + ((HttpResponseData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)(s))->~HttpResponseData(); return s; }); @@ -83,29 +84,25 @@ public: static_dispatch(us_ssl_socket_context_on_data, us_socket_context_on_data)(getSocketContext(), [](auto *s, char *data, int length) { HttpContextData *httpContextData = getSocketContextData(s); - // warning: should NOT reset timer on any data, ONLY reset data on full HTTP requests! - // warning: if we are in shutdown state, resetting the timer is a security issue! - static_dispatch(us_ssl_socket_timeout, us_socket_timeout)(s, HTTP_IDLE_TIMEOUT_S); + HttpResponseData *httpResponseData = (HttpResponseData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)(s); + httpResponseData->consumePostPadded(data, length, s, [httpContextData](void *s, HttpRequest *httpRequest) { + // warning: if we are in shutdown state, resetting the timer is a security issue! + static_dispatch(us_ssl_socket_timeout, us_socket_timeout)((SOCKET_TYPE *) s, HTTP_IDLE_TIMEOUT_S); - // here we should totally parse and route this all on our own, no involving the HttpResponse at all! + // todo: route this according to our router - std::cout << "Got data!" << std::endl; + httpContextData->handler((uWS::HttpResponse *) s, httpRequest); - //Data *httpData = (Data *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this); - - // we can reach the HttpResponseData which holds the parser, so let's just run the data through it from here (no onData) bullshit in the httpsocket! - - // todo: this is where the HttpSocket binds together HttpParser and HttpRouter into one - /*httpData->httpParser.consumePostPadded(data, length, this, [&onHttpRequest](void *user, HttpRequest *httpRequest) { - onHttpRequest((HttpSocket *) user, httpRequest); - }, [httpData](void *user, std::string_view data) { - if (httpData->inStream) { - httpData->inStream(data); + }, [httpResponseData](void *user, std::string_view data) { + if (httpResponseData->readHandler) { + httpResponseData->readHandler(data); } }, [](void *user) { std::cout << "INVALID HTTP!" << std::endl; - });*/ + + // close it down + }); return s; }); @@ -152,6 +149,8 @@ public: HttpContext *httpContext = (HttpContext *) us_create_socket_context(loop, sizeof(HttpContextData)); + new ((HttpContextData *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)((SOCKET_CONTEXT_TYPE *) httpContext)) HttpContextData(); + return httpContext->init(); } @@ -159,7 +158,7 @@ public: static_dispatch(us_ssl_socket_context_free, us_socket_context_free)(getSocketContext()); } - void onGet(std::string_view pattern, std::function *)> handler) { + void onGet(std::string_view pattern, std::function *, HttpRequest *)> handler) { HttpContextData *data = getSocketContextData(); // add things to the router diff --git a/src/new_design/HttpContextData.h b/src/new_design/HttpContextData.h index 4bbea49..8b05c22 100644 --- a/src/new_design/HttpContextData.h +++ b/src/new_design/HttpContextData.h @@ -27,7 +27,7 @@ public: HttpRouter httpRouter; // placeholder handler for response - std::function *)> handler; + std::function *, HttpRequest *)> handler; }; diff --git a/src/new_design/HttpResponse.h b/src/new_design/HttpResponse.h index 5977170..9cef5d3 100644 --- a/src/new_design/HttpResponse.h +++ b/src/new_design/HttpResponse.h @@ -2,18 +2,22 @@ #define HTTPRESPONSE_H #include "HttpResponseData.h" +#include "StaticDispatch.h" // we will most probably depend on the LoopData to do corking and such namespace uWS { template -struct HttpResponse { +struct HttpResponse : StaticDispatch { private: + using SOCKET_TYPE = typename StaticDispatch::SOCKET_TYPE; + using StaticDispatch::static_dispatch; + // helpers HttpResponseData *getHttpResponseData() { - + return (HttpResponseData *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this); } public: diff --git a/src/new_design/HttpResponseData.h b/src/new_design/HttpResponseData.h index 62075b6..f65baf5 100644 --- a/src/new_design/HttpResponseData.h +++ b/src/new_design/HttpResponseData.h @@ -3,10 +3,11 @@ // so what do we depend on? +#include "HttpParser.h" #include template -struct HttpResponseData { +struct HttpResponseData : HttpParser { std::function readHandler; diff --git a/uSockets b/uSockets index 7453ee4..2a19eab 160000 --- a/uSockets +++ b/uSockets @@ -1 +1 @@ -Subproject commit 7453ee4ec8eee404229e23021197c1d7c2a58369 +Subproject commit 2a19eab0ea4d9a48026d4d29c840bcf03c2138e7