diff --git a/examples/helpers/AsyncFileStreamer.h b/examples/helpers/AsyncFileStreamer.h index edf38d2..1064b63 100644 --- a/examples/helpers/AsyncFileStreamer.h +++ b/examples/helpers/AsyncFileStreamer.h @@ -63,7 +63,7 @@ struct AsyncFileStreamer { // todo: make sure to check for is_closed internally after all callbacks! res->close(); } else { - streamFile(res, asyncFileReader); + AsyncFileStreamer::streamFile(res, asyncFileReader); } }); } @@ -73,7 +73,7 @@ struct AsyncFileStreamer { // här kan skiten avbrytas! - streamFile(res, asyncFileReader); + AsyncFileStreamer::streamFile(res, asyncFileReader); // todo: I don't really know what this is supposed to mean? return false; })->onAborted([]() { diff --git a/misc/main.cpp b/misc/main.cpp index 3659726..cfd33cb 100644 --- a/misc/main.cpp +++ b/misc/main.cpp @@ -12,11 +12,15 @@ int main(int argc, char **argv) { .cert_file_name = "/home/alexhultman/uWebSockets/misc/ssl/cert.pem", .dh_params_file_name = "/home/alexhultman/dhparams.pem", .passphrase = "1234" - }*/).get("/*", [asyncFileStreamer](auto *res, auto *req) { + }*/).get("/hello", [asyncFileStreamer](auto *res, auto *req) { // depending on the file type we want to also add mime! asyncFileStreamer->streamFile(res, req->getUrl()); + }).unhandled([](auto *res, auto *req) { + + res->end("Here's nothing for you to see!"); + }).listen(3000, [](auto *token) { if (token) { std::cout << "Listening on port " << 3000 << std::endl; diff --git a/src/App.h b/src/App.h index 90f3f45..84d46e8 100644 --- a/src/App.h +++ b/src/App.h @@ -33,7 +33,7 @@ public: } TemplatedApp &unhandled(std::function *, HttpRequest *)> handler) { - //httpContext->onGet(pattern, handler); + httpContext->onUnhandled(handler); return *this; } diff --git a/src/HttpContext.h b/src/HttpContext.h index bf77c1e..9e81255 100644 --- a/src/HttpContext.h +++ b/src/HttpContext.h @@ -220,6 +220,14 @@ public: }); } + void onUnhandled(std::function *, uWS::HttpRequest *)> handler) { + HttpContextData *httpContextData = getSocketContextData(); + + httpContextData->router.unhandled([handler](typename HttpContextData::UserData *user, auto *args) { + handler(user->httpResponse, user->httpRequest); + }); + } + /* Listen to port using this HttpContext */ us_listen_socket *listen(const char *host, int port, int options) { return static_dispatch(us_ssl_socket_context_listen, us_socket_context_listen)(getSocketContext(), host, port, options, sizeof(HttpResponseData)); diff --git a/src/HttpRouter.h b/src/HttpRouter.h index 690ce1c..bd82e39 100644 --- a/src/HttpRouter.h +++ b/src/HttpRouter.h @@ -18,6 +18,7 @@ class HttpRouter { private: std::vector *)>> handlers; std::vector params; + std::function *)> unhandledHandler; struct Node { std::string name; @@ -135,6 +136,10 @@ public: params.reserve(100); } + HttpRouter *unhandled(std::function *)> handler) { + unhandledHandler = handler; + } + HttpRouter *add(const char *method, const char *pattern, std::function *)> handler) { // step over any initial slash @@ -178,6 +183,7 @@ public: handlers[index](userData, ¶ms); } else { std::cout << "Did not find route for URL: " << std::string_view(url, url_length) << std::endl; + unhandledHandler(userData, ¶ms); } params.clear();