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.
This commit is contained in:
Max Wipfli 2021-06-06 16:51:37 +02:00 committed by Andreas Kling
parent 2d18d3f329
commit e77ca79897
Notes: sideshowbarker 2024-07-18 12:26:28 +09:00
6 changed files with 65 additions and 9 deletions

View file

@ -1,5 +1,6 @@
set(SOURCES
Client.cpp
Configuration.cpp
main.cpp
)

View file

@ -5,7 +5,6 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Client.h"
#include <AK/Base64.h>
#include <AK/Debug.h>
#include <AK/LexicalPath.h>
@ -21,16 +20,17 @@
#include <LibCore/MimeData.h>
#include <LibHTTP/HttpRequest.h>
#include <LibHTTP/HttpResponse.h>
#include <WebServer/Client.h>
#include <WebServer/Configuration.h>
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
namespace WebServer {
Client::Client(NonnullRefPtr<Core::TCPSocket> socket, String const& root, Core::Object* parent)
Client::Client(NonnullRefPtr<Core::TCPSocket> 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();

View file

@ -19,7 +19,7 @@ public:
void start();
private:
Client(NonnullRefPtr<Core::TCPSocket>, String const&, Core::Object* parent);
Client(NonnullRefPtr<Core::TCPSocket>, 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<Core::TCPSocket> m_socket;
String m_root_path;
};
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <WebServer/Configuration.h>
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;
}
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/String.h>
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;
};
}

View file

@ -1,15 +1,17 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Max Wipfli <mail@maxwipfli.ch>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Client.h"
#include <AK/MappedFile.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
#include <LibCore/File.h>
#include <LibCore/TCPServer.h>
#include <WebServer/Client.h>
#include <WebServer/Configuration.h>
#include <stdio.h>
#include <unistd.h>
@ -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();
};