From 5d305845e3d2c8a9def7de6b218bb89ca26bc9ed Mon Sep 17 00:00:00 2001 From: Thomas Keppler Date: Tue, 20 Dec 2022 16:04:43 +0100 Subject: [PATCH] WebServer: Require document root and credentials as config init params Now, there is nothing that can react to `set_...()` calls, so offering this possibility can cause wrong assumptions as to what one can do as soon as a WebServer instance has launched. The main program can still decide whether to supply the optional credentials or not, but this way, the configuration can become a Value Object that won't change after initial creation. --- Userland/Services/WebServer/Configuration.cpp | 4 +++- Userland/Services/WebServer/Configuration.h | 6 ++---- Userland/Services/WebServer/main.cpp | 8 ++++---- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Userland/Services/WebServer/Configuration.cpp b/Userland/Services/WebServer/Configuration.cpp index aeea581b3a..1a340fd78d 100644 --- a/Userland/Services/WebServer/Configuration.cpp +++ b/Userland/Services/WebServer/Configuration.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Max Wipfli + * Copyright (c) 2022, Thomas Keppler * * SPDX-License-Identifier: BSD-2-Clause */ @@ -10,8 +11,9 @@ namespace WebServer { static Configuration* s_configuration = nullptr; -Configuration::Configuration(DeprecatedString document_root_path) +Configuration::Configuration(DeprecatedString document_root_path, Optional credentials) : m_document_root_path(move(document_root_path)) + , m_credentials(move(credentials)) { VERIFY(!s_configuration); s_configuration = this; diff --git a/Userland/Services/WebServer/Configuration.h b/Userland/Services/WebServer/Configuration.h index e6b238c522..846168dddf 100644 --- a/Userland/Services/WebServer/Configuration.h +++ b/Userland/Services/WebServer/Configuration.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Max Wipfli + * Copyright (c) 2022, Thomas Keppler * * SPDX-License-Identifier: BSD-2-Clause */ @@ -14,14 +15,11 @@ namespace WebServer { class Configuration { public: - Configuration(DeprecatedString document_root_path); + Configuration(DeprecatedString document_root_path, Optional credentials = {}); DeprecatedString const& document_root_path() const { return m_document_root_path; } Optional const& credentials() const { return m_credentials; } - void set_document_root_path(DeprecatedString root_path) { m_document_root_path = move(root_path); } - void set_credentials(Optional credentials) { m_credentials = move(credentials); } - static Configuration const& the(); private: diff --git a/Userland/Services/WebServer/main.cpp b/Userland/Services/WebServer/main.cpp index 7b2e281c1e..d51f683f16 100644 --- a/Userland/Services/WebServer/main.cpp +++ b/Userland/Services/WebServer/main.cpp @@ -55,7 +55,6 @@ ErrorOr serenity_main(Main::Arguments arguments) } auto real_document_root_path = Core::File::real_path_for(document_root_path); - if (!Core::File::exists(real_document_root_path)) { warnln("Root path does not exist: '{}'", document_root_path); return 1; @@ -63,10 +62,11 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(Core::System::pledge("stdio accept rpath inet unix")); - WebServer::Configuration configuration(real_document_root_path); - + Optional credentials; if (!username.is_empty() && !password.is_empty()) - configuration.set_credentials(HTTP::HttpRequest::BasicAuthenticationCredentials { username, password }); + credentials = HTTP::HttpRequest::BasicAuthenticationCredentials { username, password }; + + WebServer::Configuration configuration(real_document_root_path, credentials); Core::EventLoop loop;