1
0
Fork 0
mirror of https://github.com/LadybirdBrowser/ladybird.git synced 2025-06-10 10:01:13 +09:00

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.
This commit is contained in:
Thomas Keppler 2022-12-20 16:04:43 +01:00 committed by Andreas Kling
parent bb91857885
commit 5d305845e3
Notes: sideshowbarker 2024-07-17 02:35:44 +09:00
3 changed files with 9 additions and 9 deletions

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch> * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
* Copyright (c) 2022, Thomas Keppler <serenity@tkeppler.de>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -10,8 +11,9 @@ namespace WebServer {
static Configuration* s_configuration = nullptr; static Configuration* s_configuration = nullptr;
Configuration::Configuration(DeprecatedString document_root_path) Configuration::Configuration(DeprecatedString document_root_path, Optional<HTTP::HttpRequest::BasicAuthenticationCredentials> credentials)
: m_document_root_path(move(document_root_path)) : m_document_root_path(move(document_root_path))
, m_credentials(move(credentials))
{ {
VERIFY(!s_configuration); VERIFY(!s_configuration);
s_configuration = this; s_configuration = this;

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch> * Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
* Copyright (c) 2022, Thomas Keppler <serenity@tkeppler.de>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -14,14 +15,11 @@ namespace WebServer {
class Configuration { class Configuration {
public: public:
Configuration(DeprecatedString document_root_path); Configuration(DeprecatedString document_root_path, Optional<HTTP::HttpRequest::BasicAuthenticationCredentials> credentials = {});
DeprecatedString const& document_root_path() const { return m_document_root_path; } DeprecatedString const& document_root_path() const { return m_document_root_path; }
Optional<HTTP::HttpRequest::BasicAuthenticationCredentials> const& credentials() const { return m_credentials; } Optional<HTTP::HttpRequest::BasicAuthenticationCredentials> 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<HTTP::HttpRequest::BasicAuthenticationCredentials> credentials) { m_credentials = move(credentials); }
static Configuration const& the(); static Configuration const& the();
private: private:

View file

@ -55,7 +55,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
} }
auto real_document_root_path = Core::File::real_path_for(document_root_path); auto real_document_root_path = Core::File::real_path_for(document_root_path);
if (!Core::File::exists(real_document_root_path)) { if (!Core::File::exists(real_document_root_path)) {
warnln("Root path does not exist: '{}'", document_root_path); warnln("Root path does not exist: '{}'", document_root_path);
return 1; return 1;
@ -63,10 +62,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
TRY(Core::System::pledge("stdio accept rpath inet unix")); TRY(Core::System::pledge("stdio accept rpath inet unix"));
WebServer::Configuration configuration(real_document_root_path); Optional<HTTP::HttpRequest::BasicAuthenticationCredentials> credentials;
if (!username.is_empty() && !password.is_empty()) 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; Core::EventLoop loop;