From 53e835c1fa71ea64303b48418fec46533318bf91 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Tue, 19 Dec 2023 04:38:23 +0100 Subject: [PATCH] 404 page should not be an error, but a full default response --- src/App.h | 6 ++++++ src/HttpContext.h | 4 ---- src/HttpErrors.h | 7 ++----- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/App.h b/src/App.h index a9ffd4c..10ca423 100644 --- a/src/App.h +++ b/src/App.h @@ -216,6 +216,12 @@ public: TemplatedApp(SocketContextOptions options = {}) { httpContext = HttpContext::create(Loop::get(), options); + + /* Register default handler for 404 (can be overridden by user) */ + this->any("/*", [](auto *res, auto */*req*/) { + res->writeStatus("404 File Not Found"); + res->end("

File Not Found


uWebSockets/20 Server"); + }); } bool constructorFailed() { diff --git a/src/HttpContext.h b/src/HttpContext.h index 123f0fb..0049ac2 100644 --- a/src/HttpContext.h +++ b/src/HttpContext.h @@ -171,10 +171,6 @@ private: /* Route the method and URL */ selectedRouter->getUserData() = {(HttpResponse *) s, httpRequest}; if (!selectedRouter->route(httpRequest->getCaseSensitiveMethod(), httpRequest->getUrl())) { - /* We don't care if it reaches the client or not */ - us_socket_write(SSL, (us_socket_t *) s, httpErrorResponses[HTTP_ERROR_404_FILE_NOT_FOUND].data(), (int) httpErrorResponses[HTTP_ERROR_404_FILE_NOT_FOUND].length(), false); - us_socket_shutdown(SSL, (us_socket_t *) s); - /* We have to force close this socket as we have no handler for it */ us_socket_close(SSL, (us_socket_t *) s, 0, nullptr); return nullptr; diff --git a/src/HttpErrors.h b/src/HttpErrors.h index b2209af..a17a1c7 100644 --- a/src/HttpErrors.h +++ b/src/HttpErrors.h @@ -25,8 +25,7 @@ namespace uWS { enum HttpError { HTTP_ERROR_505_HTTP_VERSION_NOT_SUPPORTED = 1, HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE = 2, - HTTP_ERROR_400_BAD_REQUEST = 3, - HTTP_ERROR_404_FILE_NOT_FOUND = 4 + HTTP_ERROR_400_BAD_REQUEST = 3 }; #ifndef UWS_HTTPRESPONSE_NO_WRITEMARK @@ -37,7 +36,6 @@ static const std::string_view httpErrorResponses[] = { "HTTP/1.1 505 HTTP Version Not Supported\r\nConnection: close\r\n\r\n

HTTP Version Not Supported

This server does not support HTTP/1.0.


uWebSockets/20 Server", "HTTP/1.1 431 Request Header Fields Too Large\r\nConnection: close\r\n\r\n

Request Header Fields Too Large


uWebSockets/20 Server", "HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n

Bad Request


uWebSockets/20 Server", - "HTTP/1.1 404 File Not Found\r\nConnection: close\r\n\r\n

File Not Found


uWebSockets/20 Server" }; #else @@ -46,8 +44,7 @@ static const std::string_view httpErrorResponses[] = { "", /* Zeroth place is no error so don't use it */ "HTTP/1.1 505 HTTP Version Not Supported\r\nConnection: close\r\n\r\n", "HTTP/1.1 431 Request Header Fields Too Large\r\nConnection: close\r\n\r\n", - "HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n", - "HTTP/1.1 404 File Not Found\r\nConnection: close\r\n\r\n" + "HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n" }; #endif