Get HTTP working, kind of

This commit is contained in:
Alex Hultman
2018-09-10 23:28:51 +02:00
parent 549c97363c
commit cc284e0861
7 changed files with 33 additions and 27 deletions
BIN
View File
Binary file not shown.
+4 -2
View File
@@ -57,9 +57,11 @@ int main(int argc, char **argv) {
HttpContext<false> *httpContext = HttpContext<false>::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) {
+19 -20
View File
@@ -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<SSL>;
return s;
});
static_dispatch(us_ssl_socket_context_on_close, us_socket_context_on_close)(getSocketContext(), [](auto *s) {
HttpContextData<SSL> *httpContextData = getSocketContextData(s);
//((HTTP_SOCKET_DATA_TYPE *) static_dispatch(us_ssl_socket_ext, us_socket_ext)(s))->~HTTP_SOCKET_DATA_TYPE();
((HttpResponseData<SSL> *) static_dispatch(us_ssl_socket_ext, us_socket_ext)(s))->~HttpResponseData<SSL>();
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<SSL> *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<SSL> *httpResponseData = (HttpResponseData<SSL> *) 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<SSL> *) 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<SSL> *) 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<SSL>));
new ((HttpContextData<SSL> *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)((SOCKET_CONTEXT_TYPE *) httpContext)) HttpContextData<SSL>();
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<void(uWS::HttpResponse<SSL> *)> handler) {
void onGet(std::string_view pattern, std::function<void(uWS::HttpResponse<SSL> *, HttpRequest *)> handler) {
HttpContextData<SSL> *data = getSocketContextData();
// add things to the router
+1 -1
View File
@@ -27,7 +27,7 @@ public:
HttpRouter<HttpRouterUserData> httpRouter;
// placeholder handler for response
std::function<void(uWS::HttpResponse<SSL> *)> handler;
std::function<void(uWS::HttpResponse<SSL> *, HttpRequest *)> handler;
};
+6 -2
View File
@@ -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 <bool SSL>
struct HttpResponse {
struct HttpResponse : StaticDispatch<SSL> {
private:
using SOCKET_TYPE = typename StaticDispatch<SSL>::SOCKET_TYPE;
using StaticDispatch<SSL>::static_dispatch;
// helpers
HttpResponseData<SSL> *getHttpResponseData() {
return (HttpResponseData<SSL> *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this);
}
public:
+2 -1
View File
@@ -3,10 +3,11 @@
// so what do we depend on?
#include "HttpParser.h"
#include <functional>
template <bool SSL>
struct HttpResponseData {
struct HttpResponseData : HttpParser {
std::function<void(std::string_view)> readHandler;