diff --git a/misc/main.cpp b/misc/main.cpp index a84bf4e..1536e36 100644 --- a/misc/main.cpp +++ b/misc/main.cpp @@ -16,6 +16,9 @@ int main(int argc, char **argv) { .key_file_name = "/home/alexhultman/key.pem", .cert_file_name = "/home/alexhultman/cert.pem", .passphrase = "1234" + }).any("/anything", [](auto *res, auto *req) { + std::cout << "Any route with method: " << req->getMethod() << std::endl; + res->end("Hello Any route!"); }).get("/exit", [](auto *res, auto *req) { if (!token) { @@ -28,7 +31,7 @@ int main(int argc, char **argv) { /* Use this route to signal stop listening */ us_listen_socket_close(token); token = nullptr; - }).ws("/*", { + }).ws("/ws", { /* Settings */ .compression = uWS::DEDICATED_COMPRESSOR, .maxPayloadLength = 16 * 1024 * 1024, diff --git a/src/App.h b/src/App.h index e4f1eeb..085fcba 100644 --- a/src/App.h +++ b/src/App.h @@ -268,8 +268,9 @@ public: return std::move(*this); } - TemplatedApp &&unhandled(std::function *, HttpRequest *)> handler) { - httpContext->onUnhandled(handler); + /* This one catches any method */ + TemplatedApp &&any(std::string pattern, std::function *, HttpRequest *)> handler) { + httpContext->onHttp("*", pattern, handler); return std::move(*this); } diff --git a/src/HttpRouter.h b/src/HttpRouter.h index 25d68f9..59bd93b 100644 --- a/src/HttpRouter.h +++ b/src/HttpRouter.h @@ -154,9 +154,17 @@ private: } if (parent != &tree) { - return matchUrlSegment(parent, 0); + int handler = matchUrlSegment(parent, 0); + + /* We failed to match */ + if (!handler && method.length() > 1) { + return lookupNew("*", url); + } + + return handler; } else { - return 0; + /* Unhandled, check if we have any "all" route for this url */ + return method.length() == 1 ? 0 : lookupNew("*", url); } }