Hook up router, delete old code
This commit is contained in:
@@ -15,15 +15,12 @@ SOURCES += \
|
||||
HEADERS += \
|
||||
src/new_design/HttpRouter.h \
|
||||
src/Loop.h \
|
||||
src/App.h \
|
||||
src/http/HttpSocket.h \
|
||||
src/new_design/HttpParser.h \
|
||||
src/websocket/libwshandshake.hpp \
|
||||
src/websocket/WebSocketProtocol.h \
|
||||
src/websocket/WebSocket.h \
|
||||
src/Socket.h \
|
||||
src/websocket/WebSocketApp.h \
|
||||
src/http/HttpApp.h \
|
||||
src/new_design/HttpContext.h \
|
||||
src/new_design/HttpContextData.h \
|
||||
src/new_design/HttpResponseData.h \
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#include "App.h"
|
||||
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
std::string buffer;
|
||||
int connections = 0;
|
||||
@@ -16,6 +15,7 @@ void respond(T *s) {
|
||||
#define USE_SSL
|
||||
|
||||
#include <map>
|
||||
#include <cstring>
|
||||
|
||||
std::string_view getFile(std::string_view file) {
|
||||
static std::map<std::string_view, std::string_view> cache;
|
||||
@@ -54,7 +54,7 @@ int main(int argc, char **argv) {
|
||||
|
||||
uWS::Loop loop;
|
||||
|
||||
HttpContext<false> *httpContext = HttpContext<false>::create(loop.loop);
|
||||
uWS::HttpContext<false> *httpContext = uWS::HttpContext<false>::create(loop.loop);
|
||||
|
||||
// req, res?
|
||||
httpContext->onGet("/", [](auto *res, auto *req) { // maybe use the terminology of HttpRequest for both?
|
||||
@@ -74,6 +74,12 @@ int main(int argc, char **argv) {
|
||||
|
||||
});
|
||||
|
||||
httpContext->onGet("/yolo", [](auto *res, auto *req) {
|
||||
std::cout << "URL (/yolo route): <" << req->getUrl() << ">" << std::endl;
|
||||
std::cout << "Query: <" << req->getQuery() << ">" << std::endl;
|
||||
std::cout << "User-Agent: <" << req->getHeader("user-agent") << ">" << std::endl;
|
||||
});
|
||||
|
||||
httpContext->listen(nullptr, 3000, 0);
|
||||
|
||||
loop.run();
|
||||
@@ -86,7 +92,7 @@ int main(int argc, char **argv) {
|
||||
|
||||
|
||||
// 50 mb for huge
|
||||
buffer.resize(52428800);
|
||||
/* buffer.resize(52428800);
|
||||
|
||||
//uWS::init();
|
||||
//uWS::Loop loop();
|
||||
@@ -119,13 +125,13 @@ int main(int argc, char **argv) {
|
||||
|
||||
std::cout << "WebSocket connected to /wsApi" << std::endl;
|
||||
|
||||
}).onMessage<true>([](auto *ws, auto message/*, auto opCode*/) {
|
||||
}).onMessage<true>([](auto *ws, auto message) {
|
||||
|
||||
std::cout << "WebSocket data: " << message << std::endl;
|
||||
|
||||
//ws->send(message, opCode);
|
||||
|
||||
}).onClose([](/*auto *ws, int code, auto message*/) {
|
||||
}).onClose([]() {
|
||||
|
||||
//std::cout << "WebSocket disconnected from /wsApi" << std::endl;
|
||||
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
#ifndef CONTEXT_H
|
||||
#define CONTEXT_H
|
||||
|
||||
#include "libusockets.h"
|
||||
#include "websocket/WebSocketApp.h"
|
||||
#include "Loop.h"
|
||||
#include <string>
|
||||
|
||||
namespace uWS {
|
||||
|
||||
class SSLOptions {
|
||||
public:
|
||||
us_ssl_socket_context_options options = {};
|
||||
|
||||
SSLOptions() {
|
||||
|
||||
}
|
||||
|
||||
SSLOptions &keyFileName(std::string fileName) {
|
||||
options.key_file_name = fileName.c_str();
|
||||
return *this;
|
||||
}
|
||||
|
||||
SSLOptions &certFileName(std::string fileName) {
|
||||
options.cert_file_name = fileName.c_str();
|
||||
return *this;
|
||||
}
|
||||
|
||||
SSLOptions &passphrase(std::string passphrase) {
|
||||
options.passphrase = passphrase.c_str();
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
class App : public WebSocketApp<false> {
|
||||
|
||||
public:
|
||||
App() : WebSocketApp<false>(us_create_socket_context(defaultLoop.loop, sizeof(Data))) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
class SSLApp : public WebSocketApp<true> {
|
||||
|
||||
public:
|
||||
SSLApp(SSLOptions &sslOptions) : WebSocketApp<true>(us_create_ssl_socket_context(defaultLoop.loop, sizeof(Data), sslOptions.options)) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // CONTEXT_H
|
||||
@@ -1,31 +0,0 @@
|
||||
#ifndef SOCKET_H
|
||||
#define SOCKET_H
|
||||
|
||||
#include "libusockets.h"
|
||||
#include <type_traits>
|
||||
|
||||
template <bool SSL>
|
||||
struct Socket {
|
||||
|
||||
template <class A, class B>
|
||||
static constexpr typename std::conditional<SSL, A, B>::type *static_dispatch(A *a, B *b) {
|
||||
if constexpr(SSL) {
|
||||
return a;
|
||||
} else {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
typedef typename std::conditional<SSL, us_ssl_socket, us_socket>::type SOCKET_TYPE;
|
||||
typedef typename std::conditional<SSL, us_ssl_socket_context, us_socket_context>::type SOCKET_CONTEXT_TYPE;
|
||||
|
||||
|
||||
void *getSocketContextExt() {
|
||||
SOCKET_CONTEXT_TYPE *socketContext = static_dispatch(us_ssl_socket_get_context, us_socket_get_context)((SOCKET_TYPE *) this);
|
||||
|
||||
return static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)(socketContext);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
#endif // SOCKET_H
|
||||
@@ -1,180 +0,0 @@
|
||||
#ifndef HTTPAPP_H
|
||||
#define HTTPAPP_H
|
||||
|
||||
// todo: move all this logic to HttpContext
|
||||
|
||||
#include <type_traits>
|
||||
#include "Loop.h"
|
||||
#include "HttpSocket.h"
|
||||
#include "../new_design/HttpRouter.h"
|
||||
#include "websocket/libwshandshake.hpp"
|
||||
#include "websocket/WebSocket.h"
|
||||
|
||||
template <bool SSL, class CHILD>
|
||||
class HttpApp {
|
||||
|
||||
protected:
|
||||
|
||||
static const unsigned int HTTP_IDLE_TIMEOUT_S = 10;
|
||||
|
||||
template <class A, class B>
|
||||
static constexpr typename std::conditional<SSL, A, B>::type *static_dispatch(A *a, B *b) {
|
||||
if constexpr(SSL) {
|
||||
return a;
|
||||
} else {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
typedef typename std::conditional<SSL, us_ssl_socket, us_socket>::type SOCKET_TYPE;
|
||||
typedef typename std::conditional<SSL, us_ssl_socket_context, us_socket_context>::type SOCKET_CONTEXT_TYPE;
|
||||
typedef typename HttpSocket<SSL>::Data HTTP_SOCKET_DATA_TYPE;
|
||||
|
||||
// todo_ rename to HttpContextData
|
||||
struct Data {
|
||||
Data() {
|
||||
|
||||
// default http handler is a router
|
||||
onHttpRequest = [this](auto *s, HttpRequest *req) {
|
||||
UserData user = {s, req};
|
||||
r.route("GET", 3, req->getUrl().data(), req->getUrl().length(), &user);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
struct UserData {
|
||||
HttpSocket<SSL> *httpSocket;
|
||||
HttpRequest *httpRequest;
|
||||
};
|
||||
|
||||
HttpRouter<UserData *> r;
|
||||
|
||||
std::function<void(HttpSocket<SSL> *)> onHttpConnection;
|
||||
std::function<void(HttpSocket<SSL> *)> onHttpDisconnection;
|
||||
std::function<void(HttpSocket<SSL> *, HttpRequest *)> onHttpRequest;
|
||||
} *data;
|
||||
|
||||
// server protocols
|
||||
SOCKET_CONTEXT_TYPE *httpServerContext;
|
||||
|
||||
HttpApp(SOCKET_CONTEXT_TYPE *httpServerContext) : httpServerContext(httpServerContext) {
|
||||
new (data = (Data *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)(httpServerContext)) Data();
|
||||
|
||||
static_dispatch(us_ssl_socket_context_on_open, us_socket_context_on_open)(httpServerContext, [](auto *s, int is_client) {
|
||||
Data *appData = (Data *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)(static_dispatch(us_ssl_socket_get_context, us_socket_get_context)(s));
|
||||
|
||||
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;
|
||||
|
||||
if (appData->onHttpConnection) {
|
||||
appData->onHttpConnection((HttpSocket<SSL> *) s);
|
||||
}
|
||||
|
||||
return s;
|
||||
});
|
||||
|
||||
static_dispatch(us_ssl_socket_context_on_close, us_socket_context_on_close)(httpServerContext, [](auto *s) {
|
||||
Data *appData = (Data *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)(static_dispatch(us_ssl_socket_get_context, us_socket_get_context)(s));
|
||||
|
||||
((HTTP_SOCKET_DATA_TYPE *) static_dispatch(us_ssl_socket_ext, us_socket_ext)(s))->~HTTP_SOCKET_DATA_TYPE();
|
||||
|
||||
if (appData->onHttpDisconnection) {
|
||||
appData->onHttpDisconnection((HttpSocket<SSL> *) s);
|
||||
}
|
||||
|
||||
return s;
|
||||
});
|
||||
|
||||
static_dispatch(us_ssl_socket_context_on_data, us_socket_context_on_data)(httpServerContext, [](auto *s, char *data, int length) {
|
||||
Data *appData = (Data *) static_dispatch(us_ssl_socket_context_ext, us_socket_context_ext)(static_dispatch(us_ssl_socket_get_context, us_socket_get_context)(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);
|
||||
|
||||
// onHttpRequest should probably be hard-coded to HttpRouter
|
||||
((HttpSocket<SSL> *) s)->onData(data, length, appData->onHttpRequest);
|
||||
|
||||
// compared to routing directly
|
||||
//typename Data::UserData user = {(HttpSocket<SSL> *) s, nullptr};
|
||||
//appData->r.route("GET", 3, "/", 1, &user);
|
||||
|
||||
return s;
|
||||
});
|
||||
|
||||
static_dispatch(us_ssl_socket_context_on_writable, us_socket_context_on_writable)(httpServerContext, [](auto *s) {
|
||||
|
||||
// what if the client
|
||||
|
||||
// I think it's fair to never mind this one -> if we keep writing data after shutting down then that's an issue for us
|
||||
static_dispatch(us_ssl_socket_timeout, us_socket_timeout)(s, HTTP_IDLE_TIMEOUT_S);
|
||||
|
||||
((HttpSocket<SSL> *) s)->onWritable();
|
||||
|
||||
return s;
|
||||
});
|
||||
|
||||
static_dispatch(us_ssl_socket_context_on_end, us_socket_context_on_end)(httpServerContext, [](auto *s) {
|
||||
std::cout << "Socket was half-closed!" << std::endl;
|
||||
|
||||
return s;
|
||||
});
|
||||
|
||||
static_dispatch(us_ssl_socket_context_on_timeout, us_socket_context_on_timeout)(httpServerContext, [](auto *s) {
|
||||
|
||||
if (static_dispatch(us_ssl_socket_is_shut_down, us_socket_is_shut_down)(s)) {
|
||||
std::cout << "Forcefully closing socket since shutdown was not answered in time" << std::endl;
|
||||
static_dispatch(us_ssl_socket_close, us_socket_close)(s);
|
||||
} else {
|
||||
std::cout << "Shutting down socket now" << std::endl;
|
||||
static_dispatch(us_ssl_socket_timeout, us_socket_timeout)(s, HTTP_IDLE_TIMEOUT_S);
|
||||
static_dispatch(us_ssl_socket_shutdown, us_socket_shutdown)(s);
|
||||
}
|
||||
|
||||
return s;
|
||||
|
||||
});
|
||||
}
|
||||
|
||||
public:
|
||||
|
||||
// for server
|
||||
void listen(const char *host, int port, int options) {
|
||||
static_dispatch(us_ssl_socket_context_listen, us_socket_context_listen)(httpServerContext, host, port, options, sizeof(typename HttpSocket<SSL>::Data));
|
||||
}
|
||||
|
||||
HttpApp &onPost(std::string pattern, std::function<void(HttpSocket<SSL> *, HttpRequest *, std::vector<std::string_view> *)> handler) {
|
||||
data->r.add("POST", pattern.c_str(), [handler](typename Data::UserData *user, auto *args) {
|
||||
handler(user->httpSocket, user->httpRequest, args);
|
||||
});
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
CHILD &onGet(std::string pattern, std::function<void(HttpSocket<SSL> *, HttpRequest *, std::vector<std::string_view> *)> handler) {
|
||||
data->r.add("GET", pattern.c_str(), [handler](typename Data::UserData *user, auto *args) {
|
||||
handler(user->httpSocket, user->httpRequest, args);
|
||||
});
|
||||
|
||||
return *(CHILD *) this;
|
||||
}
|
||||
|
||||
// why even bother with these?
|
||||
HttpApp &onHttpConnection(std::function<void(HttpSocket<SSL> *)> handler) {
|
||||
data->onHttpConnection = handler;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
HttpApp &onHttpDisconnection(std::function<void(HttpSocket<SSL> *)> handler) {
|
||||
data->onHttpDisconnection = handler;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
~HttpApp() {
|
||||
}
|
||||
};
|
||||
|
||||
#endif // HTTPAPP_H
|
||||
@@ -1,4 +1,4 @@
|
||||
#ifndef HTTP_H
|
||||
#ifdef HTTP_H
|
||||
#define HTTP_H
|
||||
|
||||
#include "Socket.h"
|
||||
@@ -39,7 +39,7 @@ struct HttpSocket : Socket<SSL> {
|
||||
// httpheaders should only have 1 stream in and 1 stream out, but we can have helper wrappers
|
||||
|
||||
struct Data {
|
||||
HttpParser httpParser;
|
||||
uWS::HttpParser httpParser;
|
||||
|
||||
std::function<void(std::string_view)> inStream;
|
||||
|
||||
@@ -144,7 +144,7 @@ struct HttpSocket : Socket<SSL> {
|
||||
static_dispatch(us_ssl_socket_write, us_socket_write)((SOCKET_TYPE *) this, chunk.data(), chunk.length(), 0);
|
||||
}
|
||||
|
||||
void onData(char *data, int length, std::function<void(HttpSocket<SSL> *, HttpRequest *)> &onHttpRequest) {
|
||||
void onData(char *data, int length, std::function<void(HttpSocket<SSL> *, uWS::HttpRequest *)> &onHttpRequest) {
|
||||
Data *httpData = (Data *) static_dispatch(us_ssl_socket_ext, us_socket_ext)((SOCKET_TYPE *) this);
|
||||
|
||||
// todo: this is where the HttpSocket binds together HttpParser and HttpRouter into one
|
||||
|
||||
@@ -1,31 +1,16 @@
|
||||
#ifndef HTTPCONTEXT_H
|
||||
#define HTTPCONTEXT_H
|
||||
|
||||
// a more Cish implementaion
|
||||
#include "Loop.h"
|
||||
#include "HttpContextData.h"
|
||||
#include "HttpResponseData.h"
|
||||
#include "StaticDispatch.h"
|
||||
|
||||
#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"
|
||||
#include "HttpResponseData.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?
|
||||
|
||||
// this basically should mean: libusockets wrapper
|
||||
#include "StaticDispatch.h"
|
||||
template<bool> struct HttpResponse;
|
||||
|
||||
template <bool SSL>
|
||||
struct HttpContext : StaticDispatch<SSL> {
|
||||
@@ -85,14 +70,16 @@ public:
|
||||
HttpContextData<SSL> *httpContextData = getSocketContextData(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) {
|
||||
httpResponseData->consumePostPadded(data, length, s, [httpContextData](void *s, uWS::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);
|
||||
|
||||
// todo: route this according to our router
|
||||
|
||||
httpContextData->handler((uWS::HttpResponse<SSL> *) s, httpRequest);
|
||||
// route it!
|
||||
typename uWS::HttpContextData<SSL>::UserData userData = {
|
||||
(HttpResponse<SSL> *) s, httpRequest
|
||||
};
|
||||
httpContextData->router.route("get", 3, httpRequest->getUrl().data(), httpRequest->getUrl().length(), &userData);
|
||||
|
||||
}, [httpResponseData](void *user, std::string_view data) {
|
||||
if (httpResponseData->readHandler) {
|
||||
@@ -116,7 +103,7 @@ public:
|
||||
|
||||
|
||||
// why? WE should emit this event via the socket data that we access!
|
||||
((HttpSocket<SSL> *) s)->onWritable();
|
||||
//((HttpSocket<SSL> *) s)->onWritable();
|
||||
|
||||
return s;
|
||||
});
|
||||
@@ -158,12 +145,15 @@ 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> *, HttpRequest *)> handler) {
|
||||
void onGet(std::string pattern, std::function<void(uWS::HttpResponse<SSL> *, uWS::HttpRequest *)> handler) {
|
||||
HttpContextData<SSL> *data = getSocketContextData();
|
||||
|
||||
// add things to the router
|
||||
|
||||
data->handler = handler;
|
||||
data->router.add("get", pattern.c_str(), [handler](typename HttpContextData<SSL>::UserData *user, auto *args) {
|
||||
handler(user->httpResponse, user->httpRequest);
|
||||
});
|
||||
|
||||
}
|
||||
|
||||
void listen(const char *host, int port, int options) {
|
||||
@@ -172,4 +162,6 @@ public:
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // HTTPCONTEXT_H
|
||||
|
||||
@@ -1,17 +1,13 @@
|
||||
#ifndef HTTPCONTEXTDATA_H
|
||||
#define HTTPCONTEXTDATA_H
|
||||
|
||||
// we depend on these
|
||||
#include "HttpParser.h"
|
||||
#include "HttpRouter.h"
|
||||
|
||||
#include <functional>
|
||||
|
||||
namespace uWS {
|
||||
template<bool>
|
||||
struct HttpResponse;
|
||||
}
|
||||
|
||||
template<bool> struct HttpResponse;
|
||||
struct HttpRequest;
|
||||
|
||||
template <bool SSL>
|
||||
struct HttpContextData {
|
||||
@@ -20,15 +16,15 @@ private:
|
||||
|
||||
public:
|
||||
|
||||
struct HttpRouterUserData {
|
||||
|
||||
struct UserData {
|
||||
HttpResponse<SSL> *httpResponse;
|
||||
HttpRequest *httpRequest;
|
||||
};
|
||||
|
||||
HttpRouter<HttpRouterUserData> httpRouter;
|
||||
|
||||
// placeholder handler for response
|
||||
std::function<void(uWS::HttpResponse<SSL> *, HttpRequest *)> handler;
|
||||
HttpRouter<UserData *> router;
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // HTTPCONTEXTDATA_H
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
#include <functional>
|
||||
#include <cstring>
|
||||
|
||||
namespace uWS {
|
||||
|
||||
class HttpRequest {
|
||||
|
||||
friend class HttpParser;
|
||||
@@ -202,4 +204,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // HTTPPARSER_H
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include "HttpParser.h"
|
||||
#include <functional>
|
||||
|
||||
namespace uWS {
|
||||
|
||||
template <bool SSL>
|
||||
struct HttpResponseData : HttpParser {
|
||||
|
||||
@@ -13,4 +15,6 @@ struct HttpResponseData : HttpParser {
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // HTTPRESPONSEDATA_H
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
#include <iostream>
|
||||
#include <string_view>
|
||||
|
||||
namespace uWS {
|
||||
|
||||
template <class USERDATA>
|
||||
class HttpRouter {
|
||||
private:
|
||||
@@ -168,4 +170,6 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // HTTPROUTER_HPP
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#ifndef WEBSOCKETAPP_H
|
||||
#ifdef WEBSOCKETAPP_H
|
||||
#define WEBSOCKETAPP_H
|
||||
|
||||
#include "http/HttpApp.h"
|
||||
|
||||
Reference in New Issue
Block a user