diff --git a/15.pro b/15.pro index fd23d61..9bb4024 100644 --- a/15.pro +++ b/15.pro @@ -23,7 +23,11 @@ HEADERS += \ src/websocket/WebSocket.h \ src/Socket.h \ src/websocket/WebSocketApp.h \ - src/http/HttpApp.h + src/http/HttpApp.h \ + src/new_design/HttpContext.h \ + src/new_design/HttpContextData.h \ + src/new_design/HttpResponseData.h \ + src/new_design/HttpResponse.h INCLUDEPATH += uSockets/src src #QMAKE_CXXFLAGS += -fsanitize=address diff --git a/design.dia b/design.dia new file mode 100644 index 0000000..dcfbb85 Binary files /dev/null and b/design.dia differ diff --git a/main.cpp b/main.cpp index 60a21d0..3897329 100644 --- a/main.cpp +++ b/main.cpp @@ -45,8 +45,42 @@ std::string_view getFile(std::string_view file) { } } +#include "new_design/HttpContext.h" +#include "new_design/HttpResponse.h" + int main(int argc, char **argv) { + // new stuff, hope it sticks together? + + uWS::Loop loop; + + HttpContext *httpContext = HttpContext::create(loop.loop); + + // req, res? + httpContext->onGet("/", [](auto *res) { // maybe use the terminology of HttpRequest for both? + + std::cout << "Why hello! How do we access the headers?" << std::endl; + + // read some data being passed + res->read([](std::string_view chunk) { + std::cout << "Reading some streamed in data:" << chunk << std::endl; + }); + + // res->writeHeader()->write(); + + // req + + }); + + httpContext->listen(); + + httpContext->free(); + + return 0; + + //////////////////////////////////////////////////////////////////////////////// + + // 50 mb for huge buffer.resize(52428800); diff --git a/src/new_design/HttpContext.h b/src/new_design/HttpContext.h new file mode 100644 index 0000000..409e0f7 --- /dev/null +++ b/src/new_design/HttpContext.h @@ -0,0 +1,81 @@ +#ifndef HTTPCONTEXT_H +#define HTTPCONTEXT_H + +// a more Cish implementaion + +#include +#include + +// we depend on loop I guess +// yes, the loop does not depend on us +#include "Loop.h" + +// we of course depend on our own data type +#include "HttpContextData.h" + +// we should opaqeuly depend on HttpResponse +namespace uWS { + template + struct HttpResponse; +} + +// we parse everything only in the context? + +// would it be okay to depend on the context? + +// we depend on libusockets +#include + +template +struct HttpContext { + + +private: + HttpContext() = delete; + + // helper to get the socket context + us_socket_context *getSocketContext() { + return (us_socket_context *) this; + } + + HttpContextData *getSocketContextData() { + return (HttpContextData *) us_socket_context_ext(getSocketContext()); + } + +public: + + static HttpContext *create(us_loop *loop) { + + // todo: inplace initialize the data struct! + + return (HttpContext *) us_create_socket_context(loop, sizeof(HttpContextData)); + } + + void free() { + us_socket_context_free((us_socket_context *) this); + } + + void onGet(std::string_view pattern, std::function *)> handler) { + HttpContextData *data = getSocketContextData(); + + // add things to the router + + data->handler = handler; + } + + void listen() { + // simulate routing and parsing a request + + HttpContextData *data = getSocketContextData(); + + // we need to pass an actual socket! not nullptr! + + // simulare + data->handler(nullptr); + + + } + +}; + +#endif // HTTPCONTEXT_H diff --git a/src/new_design/HttpContextData.h b/src/new_design/HttpContextData.h new file mode 100644 index 0000000..8d87a8a --- /dev/null +++ b/src/new_design/HttpContextData.h @@ -0,0 +1,35 @@ +#ifndef HTTPCONTEXTDATA_H +#define HTTPCONTEXTDATA_H + +// this means we will depend on HttpRouter and HttpParser here! +#include "../http/HttpParser.h" + +#include "../http/HttpRouter.h" + +#include + +namespace uWS { +template +struct HttpResponse; +} + + +template +struct HttpContextData { + +private: + +public: + + struct HttpRouterUserData { + + }; + + HttpRouter httpRouter; + + // placeholder handler for response + std::function *)> handler; + +}; + +#endif // HTTPCONTEXTDATA_H diff --git a/src/new_design/HttpResponse.h b/src/new_design/HttpResponse.h new file mode 100644 index 0000000..5977170 --- /dev/null +++ b/src/new_design/HttpResponse.h @@ -0,0 +1,38 @@ +#ifndef HTTPRESPONSE_H +#define HTTPRESPONSE_H + +#include "HttpResponseData.h" + +// we will most probably depend on the LoopData to do corking and such + +namespace uWS { + +template +struct HttpResponse { +private: + + // helpers + HttpResponseData *getHttpResponseData() { + + } + +public: + + void writeHeader() { + + } + + // this will probably not be this clean: it will most probably want to do some active pulling of data? + void read(std::function handler) { + HttpResponseData *data = getHttpResponseData(); + + data->readHandler = handler; + } + + // read and write streams most definitely will be called by the context, thus the context absolutely depends on the httpresponse! + // or, we both depend on each others data structures only? +}; + +} + +#endif // HTTPRESPONSE_H diff --git a/src/new_design/HttpResponseData.h b/src/new_design/HttpResponseData.h new file mode 100644 index 0000000..62075b6 --- /dev/null +++ b/src/new_design/HttpResponseData.h @@ -0,0 +1,15 @@ +#ifndef HTTPRESPONSEDATA_H +#define HTTPRESPONSEDATA_H + +// so what do we depend on? + +#include + +template +struct HttpResponseData { + + std::function readHandler; + +}; + +#endif // HTTPRESPONSEDATA_H