Skip to content

Commit

Permalink
Add default 404 error page
Browse files Browse the repository at this point in the history
  • Loading branch information
uNetworkingAB committed Jul 15, 2023
1 parent d5ec941 commit bd1c41a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/HttpContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ struct HttpContext {
/* Route the method and URL */
selectedRouter->getUserData() = {(HttpResponse<SSL> *) 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;
Expand Down
6 changes: 4 additions & 2 deletions src/HttpErrors.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ 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_400_BAD_REQUEST = 3,
HTTP_ERROR_404_FILE_NOT_FOUND = 4
};

/* Returned parser errors match this LUT. */
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\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>",
"HTTP/1.1 431 Request Header Fields Too Large\r\n\r\n<h1>Request Header Fields Too Large</h1><hr><i>uWebSockets/20 Server</i>",
"HTTP/1.1 400 Bad Request\r\n\r\n<h1>Bad Request</h1><hr><i>uWebSockets/20 Server</i>"
"HTTP/1.1 400 Bad Request\r\n\r\n<h1>Bad Request</h1><hr><i>uWebSockets/20 Server</i>",
"HTTP/1.1 404 File Not Found\r\n\r\n<h1>File Not Found</h1><hr><i>uWebSockets/20 Server</i>"
};
}

Expand Down

0 comments on commit bd1c41a

Please sign in to comment.