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