summaryrefslogtreecommitdiff
path: root/Servers/WebServer
diff options
context:
space:
mode:
authorBogdan <cbsirb@gmail.com>2020-02-13 13:22:00 +0200
committerAndreas Kling <kling@serenityos.org>2020-02-13 20:06:32 +0100
commit7590270e138b38960d6f71f1c120808270bf3794 (patch)
treea09fd5b87dd2c6a973ce000888311da4c154f02a /Servers/WebServer
parentf88fe5dc3feba0a6a3e79fbd5dcee538d5e42d95 (diff)
downloadserenity-7590270e138b38960d6f71f1c120808270bf3794.zip
WebServer: Ensure directory listing URLs end with a slash
We will now send a redirect (301) if a directory listing is requested and the request URL doesn't end with a slash character. This makes relative file URLs that we generate in directory listings actually work as expected.
Diffstat (limited to 'Servers/WebServer')
-rw-r--r--Servers/WebServer/Client.cpp25
-rw-r--r--Servers/WebServer/Client.h1
2 files changed, 26 insertions, 0 deletions
diff --git a/Servers/WebServer/Client.cpp b/Servers/WebServer/Client.cpp
index 08a12d89c1..3ad486eeaf 100644
--- a/Servers/WebServer/Client.cpp
+++ b/Servers/WebServer/Client.cpp
@@ -91,6 +91,17 @@ void Client::handle_request(ByteBuffer raw_request)
auto real_path = path_builder.to_string();
if (Core::File::is_directory(real_path)) {
+
+ if (!request.resource().ends_with("/")) {
+ StringBuilder red;
+
+ red.append(requested_path);
+ red.append("/");
+
+ send_redirect(red.to_string(), request);
+ return;
+ }
+
StringBuilder index_html_path_builder;
index_html_path_builder.append(real_path);
index_html_path_builder.append("/index.html");
@@ -125,6 +136,20 @@ void Client::send_response(StringView response, const Core::HttpRequest& request
log_response(200, request);
}
+void Client::send_redirect(StringView redirect_path, const Core::HttpRequest& request)
+{
+ StringBuilder builder;
+ builder.append("HTTP/1.0 301 Moved Permanently\r\n");
+ builder.append("Location: ");
+ builder.append(redirect_path);
+ builder.append("\r\n");
+ builder.append("\r\n");
+
+ m_socket->write(builder.to_string());
+
+ log_response(301, request);
+}
+
void Client::handle_directory_listing(const String& requested_path, const String& real_path, const Core::HttpRequest& request)
{
StringBuilder builder;
diff --git a/Servers/WebServer/Client.h b/Servers/WebServer/Client.h
index 9b0061b133..bf43ffdfbb 100644
--- a/Servers/WebServer/Client.h
+++ b/Servers/WebServer/Client.h
@@ -45,6 +45,7 @@ private:
void handle_request(ByteBuffer);
void send_response(StringView, const Core::HttpRequest&);
+ void send_redirect(StringView redirect, const Core::HttpRequest& request);
void send_error_response(unsigned code, const StringView& message, const Core::HttpRequest&);
void die();
void log_response(unsigned code, const Core::HttpRequest&);