summaryrefslogtreecommitdiff
path: root/Userland/Services/WebServer/Client.cpp
diff options
context:
space:
mode:
authorMax Wipfli <mail@maxwipfli.ch>2021-06-05 15:48:11 +0200
committerAndreas Kling <kling@serenityos.org>2021-06-11 11:37:15 +0200
commit2d18d3f329131cce7f91906c7c0e8cc45b9be31a (patch)
treed426fc864a78d471efe2b127f6f11a9a98fb1111 /Userland/Services/WebServer/Client.cpp
parent631faec32f1cd46186f3cf3dadf269c5b5867cab (diff)
downloadserenity-2d18d3f329131cce7f91906c7c0e8cc45b9be31a.zip
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.
Diffstat (limited to 'Userland/Services/WebServer/Client.cpp')
-rw-r--r--Userland/Services/WebServer/Client.cpp12
1 files changed, 7 insertions, 5 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 <LibCore/FileStream.h>
#include <LibCore/MimeData.h>
#include <LibHTTP/HttpRequest.h>
+#include <LibHTTP/HttpResponse.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
@@ -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("<!DOCTYPE html><html><body><h1>");
builder.appendff("{} ", code);
- builder.append(message);
+ builder.append(reason_phrase);
builder.append("</h1></body></html>");
m_socket->write(builder.to_string());