Skip to content

Commit 53e835c

Browse files
committed
404 page should not be an error, but a full default response
1 parent 97e8099 commit 53e835c

File tree

3 files changed

+8
-9
lines changed

3 files changed

+8
-9
lines changed

src/App.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,12 @@ struct TemplatedApp {
216216

217217
TemplatedApp(SocketContextOptions options = {}) {
218218
httpContext = HttpContext<SSL>::create(Loop::get(), options);
219+
220+
/* Register default handler for 404 (can be overridden by user) */
221+
this->any("/*", [](auto *res, auto */*req*/) {
222+
res->writeStatus("404 File Not Found");
223+
res->end("<html><body><h1>File Not Found</h1><hr><i>uWebSockets/20 Server</i></body></html>");
224+
});
219225
}
220226

221227
bool constructorFailed() {

src/HttpContext.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,6 @@ struct HttpContext {
171171
/* Route the method and URL */
172172
selectedRouter->getUserData() = {(HttpResponse<SSL> *) s, httpRequest};
173173
if (!selectedRouter->route(httpRequest->getCaseSensitiveMethod(), httpRequest->getUrl())) {
174-
/* We don't care if it reaches the client or not */
175-
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);
176-
us_socket_shutdown(SSL, (us_socket_t *) s);
177-
178174
/* We have to force close this socket as we have no handler for it */
179175
us_socket_close(SSL, (us_socket_t *) s, 0, nullptr);
180176
return nullptr;

src/HttpErrors.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ namespace uWS {
2525
enum HttpError {
2626
HTTP_ERROR_505_HTTP_VERSION_NOT_SUPPORTED = 1,
2727
HTTP_ERROR_431_REQUEST_HEADER_FIELDS_TOO_LARGE = 2,
28-
HTTP_ERROR_400_BAD_REQUEST = 3,
29-
HTTP_ERROR_404_FILE_NOT_FOUND = 4
28+
HTTP_ERROR_400_BAD_REQUEST = 3
3029
};
3130

3231
#ifndef UWS_HTTPRESPONSE_NO_WRITEMARK
@@ -37,7 +36,6 @@ static const std::string_view httpErrorResponses[] = {
3736
"HTTP/1.1 505 HTTP Version Not Supported\r\nConnection: close\r\n\r\n<h1>HTTP Version Not Supported</h1><p>This server does not support HTTP/1.0.</p><hr><i>uWebSockets/20 Server</i>",
3837
"HTTP/1.1 431 Request Header Fields Too Large\r\nConnection: close\r\n\r\n<h1>Request Header Fields Too Large</h1><hr><i>uWebSockets/20 Server</i>",
3938
"HTTP/1.1 400 Bad Request\r\nConnection: close\r\n\r\n<h1>Bad Request</h1><hr><i>uWebSockets/20 Server</i>",
40-
"HTTP/1.1 404 File Not Found\r\nConnection: close\r\n\r\n<h1>File Not Found</h1><hr><i>uWebSockets/20 Server</i>"
4139
};
4240

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

0 commit comments

Comments
 (0)