From e77ca79897ca47ebd6b36e4ba14995474afc84c3 Mon Sep 17 00:00:00 2001 From: Max Wipfli Date: Sun, 6 Jun 2021 16:51:37 +0200 Subject: [PATCH] WebServer: Move server configuration into WebServer::Configuration This moves the configuration of the web server, which currently only consists of the root path, into a new class, Configuration. Since the configuration is global and not per client, it is accessed by a singleton getter. This change simplifies future extensions of the configurable parameters. --- Userland/Services/WebServer/CMakeLists.txt | 1 + Userland/Services/WebServer/Client.cpp | 9 +++---- Userland/Services/WebServer/Client.h | 3 +-- Userland/Services/WebServer/Configuration.cpp | 26 ++++++++++++++++++ Userland/Services/WebServer/Configuration.h | 27 +++++++++++++++++++ Userland/Services/WebServer/main.cpp | 8 ++++-- 6 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 Userland/Services/WebServer/Configuration.cpp create mode 100644 Userland/Services/WebServer/Configuration.h diff --git a/Userland/Services/WebServer/CMakeLists.txt b/Userland/Services/WebServer/CMakeLists.txt index 34b1f037c9..5cb8b7384f 100644 --- a/Userland/Services/WebServer/CMakeLists.txt +++ b/Userland/Services/WebServer/CMakeLists.txt @@ -1,5 +1,6 @@ set(SOURCES Client.cpp + Configuration.cpp main.cpp ) diff --git a/Userland/Services/WebServer/Client.cpp b/Userland/Services/WebServer/Client.cpp index ddb9491a73..bef0da16e4 100644 --- a/Userland/Services/WebServer/Client.cpp +++ b/Userland/Services/WebServer/Client.cpp @@ -5,7 +5,6 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include "Client.h" #include #include #include @@ -21,16 +20,17 @@ #include #include #include +#include +#include #include #include #include namespace WebServer { -Client::Client(NonnullRefPtr socket, String const& root, Core::Object* parent) +Client::Client(NonnullRefPtr socket, Core::Object* parent) : Core::Object(parent) , m_socket(socket) - , m_root_path(root) { } @@ -84,8 +84,7 @@ void Client::handle_request(ReadonlyBytes raw_request) dbgln_if(WEBSERVER_DEBUG, "Canonical requested path: '{}'", requested_path); StringBuilder path_builder; - path_builder.append(m_root_path); - path_builder.append('/'); + path_builder.append(Configuration::the().root_path()); path_builder.append(requested_path); auto real_path = path_builder.to_string(); diff --git a/Userland/Services/WebServer/Client.h b/Userland/Services/WebServer/Client.h index f6806b2c73..991f2ec649 100644 --- a/Userland/Services/WebServer/Client.h +++ b/Userland/Services/WebServer/Client.h @@ -19,7 +19,7 @@ public: void start(); private: - Client(NonnullRefPtr, String const&, Core::Object* parent); + Client(NonnullRefPtr, Core::Object* parent); void handle_request(ReadonlyBytes); void send_response(InputStream&, HTTP::HttpRequest const&, String const& content_type); @@ -30,7 +30,6 @@ private: void handle_directory_listing(String const& requested_path, String const& real_path, HTTP::HttpRequest const&); NonnullRefPtr m_socket; - String m_root_path; }; } diff --git a/Userland/Services/WebServer/Configuration.cpp b/Userland/Services/WebServer/Configuration.cpp new file mode 100644 index 0000000000..790f6d9e94 --- /dev/null +++ b/Userland/Services/WebServer/Configuration.cpp @@ -0,0 +1,26 @@ +/* + * Copyright (c) 2021, Max Wipfli + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include + +namespace WebServer { + +static Configuration* s_configuration = nullptr; + +Configuration::Configuration(String root_path) + : m_root_path(move(root_path)) +{ + VERIFY(!s_configuration); + s_configuration = this; +} + +Configuration const& Configuration::the() +{ + VERIFY(s_configuration); + return *s_configuration; +} + +} diff --git a/Userland/Services/WebServer/Configuration.h b/Userland/Services/WebServer/Configuration.h new file mode 100644 index 0000000000..f4581fabce --- /dev/null +++ b/Userland/Services/WebServer/Configuration.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2021, Max Wipfli + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include + +namespace WebServer { + +class Configuration { +public: + Configuration(String root_path); + + String const& root_path() const { return m_root_path; } + + void set_root_path(String root_path) { m_root_path = move(root_path); } + + static Configuration const& the(); + +private: + String m_root_path; +}; + +} diff --git a/Userland/Services/WebServer/main.cpp b/Userland/Services/WebServer/main.cpp index 573f690fbc..299a95f199 100644 --- a/Userland/Services/WebServer/main.cpp +++ b/Userland/Services/WebServer/main.cpp @@ -1,15 +1,17 @@ /* * Copyright (c) 2018-2020, Andreas Kling + * Copyright (c) 2021, Max Wipfli * * SPDX-License-Identifier: BSD-2-Clause */ -#include "Client.h" #include #include #include #include #include +#include +#include #include #include @@ -51,6 +53,8 @@ int main(int argc, char** argv) return 1; } + WebServer::Configuration configuration(real_root_path); + Core::EventLoop loop; auto server = Core::TCPServer::construct(); @@ -58,7 +62,7 @@ int main(int argc, char** argv) server->on_ready_to_accept = [&] { auto client_socket = server->accept(); VERIFY(client_socket); - auto client = WebServer::Client::construct(client_socket.release_nonnull(), real_root_path, server); + auto client = WebServer::Client::construct(client_socket.release_nonnull(), server); client->start(); };