From 03d249ab481c6c31156546b2e17a1313ac8907bf Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Mon, 10 Sep 2018 22:00:08 +0200 Subject: [PATCH] Bunch of stuff --- 15.pro | 6 ++- design.dia | Bin 0 -> 2424 bytes main.cpp | 34 +++++++++++++ src/new_design/HttpContext.h | 81 ++++++++++++++++++++++++++++++ src/new_design/HttpContextData.h | 35 +++++++++++++ src/new_design/HttpResponse.h | 38 ++++++++++++++ src/new_design/HttpResponseData.h | 15 ++++++ 7 files changed, 208 insertions(+), 1 deletion(-) create mode 100644 design.dia create mode 100644 src/new_design/HttpContext.h create mode 100644 src/new_design/HttpContextData.h create mode 100644 src/new_design/HttpResponse.h create mode 100644 src/new_design/HttpResponseData.h 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 0000000000000000000000000000000000000000..dcfbb85ed0c16f324c9816435d091fd04f8582d5 GIT binary patch literal 2424 zcmZwGdpHvc9|!O;rzuK8REbex#U|HE7zZ;9!z!1t!caym z&LY=z9Cw#6V#s?711oK7ME{d!Ikry7qA|Lu~y*U-Z^PzIMiVonnSC zVU-mVhc6!xdpi@wxcnR3r!v`Yy5=e~X`d$N{YwCH)u?6V>y}7fZT1ba6uA!h>u$Ho z0wWW_i-}{Fdo9+wCf4tbP-!IG#de0`@sHowM#pRRXiB$TRXC;`angoG-#BrHd6>s- z2}v|FxS)nPk5c=*CVkZgpCFGXpTs=s!0E1nX`5T4ll7d` zdNskyO|E%NnJ(Fa*&K944(;QAvXB8aKwAq@OT|qa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oCNK?)P0Gcq$y76y?kK(g{BPA=>Vq0{Y08&t=z$N_Slxu@t5jJodxG$ z6_VXQ*Li$#j(C;r+BK3g>%<6rIO{QQ;j$Ig`VF_$vCp(W@V_niuUfDHg`L}iO{-Yn zmlPtx|NHRYBMJ0;C#}(AjO5_*S_xKLu){7vW@+V*B7xZQ;HO)U59tZt9Tyk?J_DK7 z93F5;^v4Ff8(oJyH!8@HE9^wt-jaRqco%OM5u?{UW!)SC3hG?WVFiLXwTutTUO$!Y zE16m8GXW)_%VKE|u%Skj_h#F2!T|6;utR4q=a8LjgH1mR7O(h=Nb0X`h3P#?FRk#H z&$&*wMJH{_B&2DBy|atDa8O*}|E=ZUIq~R%OF2+i zTebNc*`s*X&>~ag>Q-&pJFtxOWZgeAXFmSPLZ#Cu!qo5hY>1v;DRt|&D6DotPLJrs z&k1w_(?>BuGKuFWy)D^QC>zj#h#^HPGEUE|`THaWeNp^GKG^L#Lnbz>rxyPFnm)=( z>0FHbB+|ckpy4K~=7qcRYi43-a?4SyJhwQyCAIPx_CxE@H!)D!;SA^$irr literal 0 HcmV?d00001 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