diff options
author | Andreas Kling <kling@serenityos.org> | 2020-02-09 12:04:58 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-02-09 14:15:55 +0100 |
commit | 6c752c15a2a7d25773f0b2fec2ac4588e8d3c74a (patch) | |
tree | 5ec55ab58c6d67d0119f3f44bf5c8631a570cbc7 /Servers/WebServer/Client.cpp | |
parent | 271bc4b2f2991ef5306a229495a7a880ec5f1f9b (diff) | |
download | serenity-6c752c15a2a7d25773f0b2fec2ac4588e8d3c74a.zip |
WebServer: Implement a very basic HTTP server :^)
This server listens on port 8000 and serves HTML files from /www.
It's very simple and quite naive, but I think we can start here and
build our way to something pretty neat.
Work towards #792.
Diffstat (limited to 'Servers/WebServer/Client.cpp')
-rw-r--r-- | Servers/WebServer/Client.cpp | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/Servers/WebServer/Client.cpp b/Servers/WebServer/Client.cpp new file mode 100644 index 0000000000..90f9eeeb6d --- /dev/null +++ b/Servers/WebServer/Client.cpp @@ -0,0 +1,101 @@ +#include "Client.h" +#include <AK/FileSystemPath.h> +#include <AK/StringBuilder.h> +#include <LibCore/File.h> +#include <LibCore/HttpRequest.h> +#include <stdio.h> +#include <time.h> + +namespace WebServer { + +Client::Client(NonnullRefPtr<Core::TCPSocket> socket, Core::Object* parent) + : Core::Object(parent) + , m_socket(socket) +{ +} + +void Client::die() +{ + remove_from_parent(); +} + +void Client::start() +{ + m_socket->on_ready_to_read = [this] { + auto raw_request = m_socket->read_all(); + if (raw_request.is_null()) { + die(); + return; + } + + dbg() << "Got raw request: '" << String::copy(raw_request) << "'"; + + handle_request(move(raw_request)); + die(); + }; +} + +void Client::handle_request(ByteBuffer raw_request) +{ + auto request_or_error = Core::HttpRequest::from_raw_request(raw_request); + if (!request_or_error.has_value()) + return; + auto& request = request_or_error.value(); + + dbg() << "Got HTTP request: " << request.method_name() << " " << request.resource(); + for (auto& header : request.headers()) { + dbg() << " " << header.name << " => " << header.value; + } + + if (request.method() != Core::HttpRequest::Method::GET) { + send_error_response(403, "Forbidden, bro!", request); + return; + } + + FileSystemPath canonical_path(request.resource()); + dbg() << "Requested canonical path: '" << canonical_path.string() << "'"; + + StringBuilder path_builder; + path_builder.append("/www/"); + path_builder.append(canonical_path.string()); + + if (Core::File::is_directory(path_builder.to_string())) + path_builder.append("/index.html"); + + auto file = Core::File::construct(path_builder.to_string()); + if (!file->open(Core::File::ReadOnly)) { + send_error_response(404, "Not found, bro!", request); + return; + } + + m_socket->write("HTTP/1.0 200 OK\r\n"); + m_socket->write("Server: WebServer (SerenityOS)\r\n"); + m_socket->write("Content-Type: text/html\r\n"); + m_socket->write("\r\n"); + + m_socket->write(file->read_all()); + + log_response(200, request); +} + +void Client::send_error_response(unsigned code, const StringView& message, const Core::HttpRequest& request) +{ + StringBuilder builder; + builder.appendf("HTTP/1.0 %u", code); + builder.append(message); + builder.append("\r\n\r\n"); + builder.append("<!DOCTYPE html><html><body><h1>"); + builder.appendf("%u ", code); + builder.append(message); + builder.append("</h1></body></html>"); + m_socket->write(builder.to_string()); + + log_response(code, request); +} + +void Client::log_response(unsigned code, const Core::HttpRequest& request) +{ + printf("%lld :: %03u :: %s %s\n", time(nullptr), code, request.method_name().characters(), request.resource().characters()); +} + +} |