From 2d18d3f329131cce7f91906c7c0e8cc45b9be31a Mon Sep 17 00:00:00 2001 From: Max Wipfli Date: Sat, 5 Jun 2021 15:48:11 +0200 Subject: [PATCH] WebServer: Use canonical reasons phrases for error responses This changes the Client::set_error_response() to not take a "message" anymore. It now uses the canonical reason phrase which is derived from the response code. --- Userland/Services/WebServer/Client.cpp | 12 +++++++----- Userland/Services/WebServer/Client.h | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/Userland/Services/WebServer/Client.cpp b/Userland/Services/WebServer/Client.cpp index 186f18d411..ddb9491a73 100644 --- a/Userland/Services/WebServer/Client.cpp +++ b/Userland/Services/WebServer/Client.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -75,7 +76,7 @@ void Client::handle_request(ReadonlyBytes raw_request) } if (request.method() != HTTP::HttpRequest::Method::GET) { - send_error_response(403, "Forbidden!", request); + send_error_response(501, request); return; } @@ -113,7 +114,7 @@ void Client::handle_request(ReadonlyBytes raw_request) auto file = Core::File::construct(real_path); if (!file->open(Core::OpenMode::ReadOnly)) { - send_error_response(404, "Not found!", request); + send_error_response(404, request); return; } @@ -267,15 +268,16 @@ void Client::handle_directory_listing(String const& requested_path, String const send_response(stream, request, "text/html"); } -void Client::send_error_response(unsigned code, StringView const& message, HTTP::HttpRequest const& request) +void Client::send_error_response(unsigned code, HTTP::HttpRequest const& request) { + auto reason_phrase = HTTP::HttpResponse::reason_phrase_for_code(code); StringBuilder builder; builder.appendff("HTTP/1.0 {} ", code); - builder.append(message); + builder.append(reason_phrase); builder.append("\r\n\r\n"); builder.append("

"); builder.appendff("{} ", code); - builder.append(message); + builder.append(reason_phrase); builder.append("

"); m_socket->write(builder.to_string()); diff --git a/Userland/Services/WebServer/Client.h b/Userland/Services/WebServer/Client.h index 3ba51dfb31..f6806b2c73 100644 --- a/Userland/Services/WebServer/Client.h +++ b/Userland/Services/WebServer/Client.h @@ -24,7 +24,7 @@ private: void handle_request(ReadonlyBytes); void send_response(InputStream&, HTTP::HttpRequest const&, String const& content_type); void send_redirect(StringView redirect, HTTP::HttpRequest const&); - void send_error_response(unsigned code, StringView const& message, HTTP::HttpRequest const&); + void send_error_response(unsigned code, HTTP::HttpRequest const&); void die(); void log_response(unsigned code, HTTP::HttpRequest const&); void handle_directory_listing(String const& requested_path, String const& real_path, HTTP::HttpRequest const&);