Bunch of stuff

This commit is contained in:
Alex Hultman
2018-09-10 22:00:08 +02:00
parent 335b7e1dcd
commit 03d249ab48
7 changed files with 208 additions and 1 deletions
+5 -1
View File
@@ -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
BIN
View File
Binary file not shown.
+34
View File
@@ -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<false> *httpContext = HttpContext<false>::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);
+81
View File
@@ -0,0 +1,81 @@
#ifndef HTTPCONTEXT_H
#define HTTPCONTEXT_H
// a more Cish implementaion
#include <string_view>
#include <functional>
// 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<bool>
struct HttpResponse;
}
// we parse everything only in the context?
// would it be okay to depend on the context?
// we depend on libusockets
#include <libusockets.h>
template <bool SSL>
struct HttpContext {
private:
HttpContext() = delete;
// helper to get the socket context
us_socket_context *getSocketContext() {
return (us_socket_context *) this;
}
HttpContextData<SSL> *getSocketContextData() {
return (HttpContextData<SSL> *) 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<SSL>));
}
void free() {
us_socket_context_free((us_socket_context *) this);
}
void onGet(std::string_view pattern, std::function<void(uWS::HttpResponse<SSL> *)> handler) {
HttpContextData<SSL> *data = getSocketContextData();
// add things to the router
data->handler = handler;
}
void listen() {
// simulate routing and parsing a request
HttpContextData<SSL> *data = getSocketContextData();
// we need to pass an actual socket! not nullptr!
// simulare
data->handler(nullptr);
}
};
#endif // HTTPCONTEXT_H
+35
View File
@@ -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 <functional>
namespace uWS {
template<bool>
struct HttpResponse;
}
template <bool SSL>
struct HttpContextData {
private:
public:
struct HttpRouterUserData {
};
HttpRouter<HttpRouterUserData> httpRouter;
// placeholder handler for response
std::function<void(uWS::HttpResponse<SSL> *)> handler;
};
#endif // HTTPCONTEXTDATA_H
+38
View File
@@ -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 <bool SSL>
struct HttpResponse {
private:
// helpers
HttpResponseData<SSL> *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<void(std::string_view)> handler) {
HttpResponseData<SSL> *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
+15
View File
@@ -0,0 +1,15 @@
#ifndef HTTPRESPONSEDATA_H
#define HTTPRESPONSEDATA_H
// so what do we depend on?
#include <functional>
template <bool SSL>
struct HttpResponseData {
std::function<void(std::string_view)> readHandler;
};
#endif // HTTPRESPONSEDATA_H