LibCore+Userland: Convert TCPServer to use the Serenity Stream API

This is intended as a real-usecase test of the Serenity Stream API, and
seemed like a good candidate due to its low amount of users.
This commit is contained in:
sin-ack 2021-09-12 11:55:40 +00:00 committed by Ali Mohammad Pur
parent 2341b0159a
commit dfdb52efa7
Notes: sideshowbarker 2024-07-17 22:42:40 +09:00
11 changed files with 263 additions and 124 deletions

View File

@ -75,7 +75,7 @@ void TCPServer::set_blocking(bool blocking)
VERIFY(flags == 0);
}
RefPtr<TCPSocket> TCPServer::accept()
ErrorOr<Stream::TCPSocket> TCPServer::accept()
{
VERIFY(m_listening);
sockaddr_in in;
@ -87,17 +87,20 @@ RefPtr<TCPSocket> TCPServer::accept()
#endif
if (accepted_fd < 0) {
perror("accept");
return nullptr;
return Error::from_errno(errno);
}
auto socket = TRY(Stream::TCPSocket::adopt_fd(accepted_fd));
#ifdef AK_OS_MACOS
int option = 1;
(void)ioctl(m_fd, FIONBIO, &option);
(void)fcntl(accepted_fd, F_SETFD, FD_CLOEXEC);
// FIXME: Ideally, we should let the caller decide whether it wants the
// socket to be nonblocking or not, but there are currently places
// which depend on this.
TRY(socket.set_blocking(false));
TRY(socket.set_close_on_exec(true));
#endif
return TCPSocket::construct(accepted_fd);
return socket;
}
Optional<IPv4Address> TCPServer::local_address() const

View File

@ -9,6 +9,7 @@
#include <AK/IPv4Address.h>
#include <LibCore/Notifier.h>
#include <LibCore/Object.h>
#include <LibCore/Stream.h>
namespace Core {
@ -21,7 +22,7 @@ public:
bool listen(const IPv4Address& address, u16 port);
void set_blocking(bool blocking);
RefPtr<TCPSocket> accept();
ErrorOr<Stream::TCPSocket> accept();
Optional<IPv4Address> local_address() const;
Optional<u16> local_port() const;

View File

@ -5,34 +5,52 @@
*/
#include "Client.h"
#include "LibCore/EventLoop.h"
Client::Client(int id, RefPtr<Core::TCPSocket> socket)
Client::Client(int id, Core::Stream::TCPSocket socket)
: m_id(id)
, m_socket(move(socket))
{
m_socket->on_ready_to_read = [this] { drain_socket(); };
m_socket.on_ready_to_read = [this] {
if (m_socket.is_eof())
return;
auto result = drain_socket();
if (result.is_error()) {
dbgln("Failed while trying to drain the socket: {}", result.error());
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
}
};
}
void Client::drain_socket()
ErrorOr<void> Client::drain_socket()
{
NonnullRefPtr<Client> protect(*this);
while (m_socket->can_read()) {
auto buf = m_socket->read(1024);
dbgln("Read {} bytes.", buf.size());
auto maybe_buffer = ByteBuffer::create_uninitialized(1024);
if (!maybe_buffer.has_value())
return ENOMEM;
auto buffer = maybe_buffer.release_value();
if (m_socket->eof()) {
quit();
while (TRY(m_socket.can_read_without_blocking())) {
auto nread = TRY(m_socket.read(buffer));
dbgln("Read {} bytes.", nread);
if (m_socket.is_eof()) {
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
break;
}
m_socket->write(buf);
TRY(m_socket.write({ buffer.data(), nread }));
}
return {};
}
void Client::quit()
{
m_socket->close();
m_socket.close();
if (on_exit)
on_exit();
}

View File

@ -6,11 +6,11 @@
#pragma once
#include <LibCore/TCPSocket.h>
#include <LibCore/Stream.h>
class Client : public RefCounted<Client> {
public:
static NonnullRefPtr<Client> create(int id, RefPtr<Core::TCPSocket> socket)
static NonnullRefPtr<Client> create(int id, Core::Stream::TCPSocket socket)
{
return adopt_ref(*new Client(id, move(socket)));
}
@ -18,12 +18,12 @@ public:
Function<void()> on_exit;
protected:
Client(int id, RefPtr<Core::TCPSocket> socket);
Client(int id, Core::Stream::TCPSocket socket);
void drain_socket();
ErrorOr<void> drain_socket();
void quit();
private:
int m_id { 0 };
RefPtr<Core::TCPSocket> m_socket;
Core::Stream::TCPSocket m_socket;
};

View File

@ -52,15 +52,15 @@ int main(int argc, char** argv)
server->on_ready_to_accept = [&next_id, &clients, &server] {
int id = next_id++;
auto client_socket = server->accept();
if (!client_socket) {
perror("accept");
auto maybe_client_socket = server->accept();
if (maybe_client_socket.is_error()) {
warnln("accept: {}", maybe_client_socket.error());
return;
}
outln("Client {} connected", id);
auto client = Client::create(id, move(client_socket));
auto client = Client::create(id, maybe_client_socket.release_value());
client->on_exit = [&clients, id] {
Core::deferred_invoke([&clients, id] {
clients.remove(id);

View File

@ -11,59 +11,100 @@
#include <AK/StringBuilder.h>
#include <AK/StringView.h>
#include <AK/Types.h>
#include <LibCore/EventLoop.h>
#include <LibCore/Notifier.h>
#include <LibCore/TCPSocket.h>
#include <stdio.h>
#include <unistd.h>
Client::Client(int id, RefPtr<Core::TCPSocket> socket, int ptm_fd)
Client::Client(int id, Core::Stream::TCPSocket socket, int ptm_fd)
: m_id(id)
, m_socket(move(socket))
, m_ptm_fd(ptm_fd)
, m_ptm_notifier(Core::Notifier::construct(ptm_fd, Core::Notifier::Read))
{
m_socket->on_ready_to_read = [this] { drain_socket(); };
m_ptm_notifier->on_ready_to_read = [this] { drain_pty(); };
m_parser.on_command = [this](const Command& command) { handle_command(command); };
m_socket.on_ready_to_read = [this] {
auto result = drain_socket();
if (result.is_error()) {
dbgln("Failed to drain the socket: {}", result.error());
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
}
};
m_ptm_notifier->on_ready_to_read = [this] {
auto result = drain_pty();
if (result.is_error()) {
dbgln("Failed to drain the PTY: {}", result.error());
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
}
};
m_parser.on_command = [this](const Command& command) {
auto result = handle_command(command);
if (result.is_error()) {
dbgln("Failed to handle the command: {}", result.error());
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
}
};
m_parser.on_data = [this](StringView data) { handle_data(data); };
m_parser.on_error = [this]() { handle_error(); };
send_commands({
}
ErrorOr<NonnullRefPtr<Client>> Client::create(int id, Core::Stream::TCPSocket socket, int ptm_fd)
{
auto client = adopt_ref(*new Client(id, move(socket), ptm_fd));
auto result = client->send_commands({
{ CMD_WILL, SUB_SUPPRESS_GO_AHEAD },
{ CMD_WILL, SUB_ECHO },
{ CMD_DO, SUB_SUPPRESS_GO_AHEAD },
{ CMD_DONT, SUB_ECHO },
});
if (result.is_error()) {
client->quit();
return result.release_error();
}
return client;
}
void Client::drain_socket()
ErrorOr<void> Client::drain_socket()
{
NonnullRefPtr<Client> protect(*this);
while (m_socket->can_read()) {
auto buf = m_socket->read(1024);
m_parser.write(buf);
auto maybe_buffer = ByteBuffer::create_uninitialized(1024);
if (!maybe_buffer.has_value())
return ENOMEM;
auto buffer = maybe_buffer.release_value();
if (m_socket->eof()) {
quit();
while (TRY(m_socket.can_read_without_blocking())) {
auto nread = TRY(m_socket.read(buffer));
m_parser.write({ buffer.data(), nread });
if (m_socket.is_eof()) {
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
break;
}
}
return {};
}
void Client::drain_pty()
ErrorOr<void> Client::drain_pty()
{
u8 buffer[BUFSIZ];
ssize_t nread = read(m_ptm_fd, buffer, sizeof(buffer));
if (nread < 0) {
perror("read(ptm)");
quit();
return;
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
return static_cast<ErrnoCode>(errno);
}
if (nread == 0) {
quit();
return;
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
return {};
}
send_data(StringView(buffer, (size_t)nread));
return send_data({ buffer, (size_t)nread });
}
void Client::handle_data(StringView data)
@ -71,7 +112,7 @@ void Client::handle_data(StringView data)
write(m_ptm_fd, data.characters_without_null_termination(), data.length());
}
void Client::handle_command(const Command& command)
ErrorOr<void> Client::handle_command(const Command& command)
{
switch (command.command) {
case CMD_DO:
@ -87,10 +128,10 @@ void Client::handle_command(const Command& command)
case SUB_ECHO:
// we always want to be the ones in control of the output. tell
// the client to disable local echo.
send_command({ CMD_DONT, SUB_ECHO });
TRY(send_command({ CMD_DONT, SUB_ECHO }));
break;
case SUB_SUPPRESS_GO_AHEAD:
send_command({ CMD_DO, SUB_SUPPRESS_GO_AHEAD });
TRY(send_command({ CMD_DO, SUB_SUPPRESS_GO_AHEAD }));
break;
default:
// don't respond to unknown commands
@ -102,14 +143,16 @@ void Client::handle_command(const Command& command)
// won't do.
break;
}
return {};
}
void Client::handle_error()
{
quit();
Core::deferred_invoke([this, strong_this = NonnullRefPtr(*this)] { quit(); });
}
void Client::send_data(StringView data)
ErrorOr<void> Client::send_data(StringView data)
{
bool fast = true;
for (size_t i = 0; i < data.length(); i++) {
@ -119,8 +162,8 @@ void Client::send_data(StringView data)
}
if (fast) {
m_socket->write(data);
return;
TRY(m_socket.write({ data.characters_without_null_termination(), data.length() }));
return {};
}
StringBuilder builder;
@ -140,31 +183,37 @@ void Client::send_data(StringView data)
}
}
m_socket->write(builder.to_string());
auto builder_contents = builder.to_byte_buffer();
TRY(m_socket.write(builder_contents));
return {};
}
void Client::send_command(Command command)
ErrorOr<void> Client::send_command(Command command)
{
send_commands({ command });
return send_commands({ command });
}
void Client::send_commands(Vector<Command> commands)
ErrorOr<void> Client::send_commands(Vector<Command> commands)
{
auto buffer = ByteBuffer::create_uninitialized(commands.size() * 3).release_value(); // FIXME: Handle possible OOM situation.
auto maybe_buffer = ByteBuffer::create_uninitialized(commands.size() * 3);
if (!maybe_buffer.has_value())
return ENOMEM;
auto buffer = maybe_buffer.release_value();
OutputMemoryStream stream { buffer };
for (auto& command : commands)
stream << (u8)IAC << command.command << command.subcommand;
VERIFY(stream.is_end());
m_socket->write(buffer.data(), buffer.size());
TRY(m_socket.write({ buffer.data(), buffer.size() }));
return {};
}
void Client::quit()
{
m_ptm_notifier->set_enabled(false);
close(m_ptm_fd);
m_socket->close();
m_socket.close();
if (on_exit)
on_exit();
}

View File

@ -10,38 +10,35 @@
#include <AK/StringView.h>
#include <AK/Types.h>
#include <LibCore/Notifier.h>
#include <LibCore/TCPSocket.h>
#include <LibCore/Stream.h>
#include "Command.h"
#include "Parser.h"
class Client : public RefCounted<Client> {
public:
static NonnullRefPtr<Client> create(int id, RefPtr<Core::TCPSocket> socket, int ptm_fd)
{
return adopt_ref(*new Client(id, move(socket), ptm_fd));
}
static ErrorOr<NonnullRefPtr<Client>> create(int id, Core::Stream::TCPSocket socket, int ptm_fd);
Function<void()> on_exit;
protected:
Client(int id, RefPtr<Core::TCPSocket> socket, int ptm_fd);
private:
Client(int id, Core::Stream::TCPSocket socket, int ptm_fd);
ErrorOr<void> drain_socket();
ErrorOr<void> drain_pty();
ErrorOr<void> handle_command(Command const& command);
ErrorOr<void> send_data(StringView str);
ErrorOr<void> send_command(Command command);
ErrorOr<void> send_commands(Vector<Command> commands);
void drain_socket();
void drain_pty();
void handle_data(StringView);
void handle_command(const Command& command);
void handle_error();
void send_data(StringView str);
void send_command(Command command);
void send_commands(Vector<Command> commands);
void quit();
private:
// client id
int m_id { 0 };
// client resources
RefPtr<Core::TCPSocket> m_socket;
Core::Stream::TCPSocket m_socket;
Parser m_parser;
// pty resources
int m_ptm_fd { -1 };

View File

@ -109,32 +109,39 @@ int main(int argc, char** argv)
server->on_ready_to_accept = [&next_id, &clients, &server, command] {
int id = next_id++;
auto client_socket = server->accept();
if (!client_socket) {
perror("accept");
ErrorOr<Core::Stream::TCPSocket> maybe_client_socket = server->accept();
if (maybe_client_socket.is_error()) {
warnln("accept: {}", maybe_client_socket.error());
return;
}
auto client_socket = maybe_client_socket.release_value();
int ptm_fd = posix_openpt(O_RDWR);
if (ptm_fd < 0) {
perror("posix_openpt");
client_socket->close();
client_socket.close();
return;
}
if (grantpt(ptm_fd) < 0) {
perror("grantpt");
client_socket->close();
client_socket.close();
return;
}
if (unlockpt(ptm_fd) < 0) {
perror("unlockpt");
client_socket->close();
client_socket.close();
return;
}
run_command(ptm_fd, command);
auto client = Client::create(id, move(client_socket), ptm_fd);
auto maybe_client = Client::create(id, move(client_socket), ptm_fd);
if (maybe_client.is_error()) {
warnln("Failed to create the client: {}", maybe_client.error());
return;
}
auto client = maybe_client.release_value();
client->on_exit = [&clients, id] {
Core::deferred_invoke([&clients, id] { clients.remove(id); });
};

View File

@ -28,41 +28,75 @@
namespace WebServer {
Client::Client(NonnullRefPtr<Core::TCPSocket> socket, Core::Object* parent)
Client::Client(Core::Stream::BufferedTCPSocket socket, Core::Object* parent)
: Core::Object(parent)
, m_socket(socket)
, m_socket(move(socket))
{
}
void Client::die()
{
m_socket.close();
deferred_invoke([this] { remove_from_parent(); });
}
void Client::start()
{
m_socket->on_ready_to_read = [this] {
m_socket.on_ready_to_read = [this] {
StringBuilder builder;
auto maybe_buffer = ByteBuffer::create_uninitialized(m_socket.buffer_size());
if (!maybe_buffer.has_value()) {
warnln("Could not create buffer for client (possibly out of memory)");
die();
return;
}
auto buffer = maybe_buffer.release_value();
for (;;) {
auto line = m_socket->read_line();
if (line.is_empty())
auto maybe_can_read = m_socket.can_read_without_blocking();
if (maybe_can_read.is_error()) {
warnln("Failed to get the blocking status for the socket: {}", maybe_can_read.error());
die();
return;
}
if (!maybe_can_read.value())
break;
builder.append(line);
auto maybe_nread = m_socket.read_until_any_of(buffer, Array { "\r"sv, "\n"sv, "\r\n"sv });
if (maybe_nread.is_error()) {
warnln("Failed to read a line from the request: {}", maybe_nread.error());
die();
return;
}
if (m_socket.is_eof()) {
die();
break;
}
builder.append(StringView { buffer.data(), maybe_nread.value() });
builder.append("\r\n");
}
auto request = builder.to_byte_buffer();
dbgln_if(WEBSERVER_DEBUG, "Got raw request: '{}'", String::copy(request));
handle_request(request);
auto maybe_did_handle = handle_request(request);
if (maybe_did_handle.is_error()) {
warnln("Failed to handle the request: {}", maybe_did_handle.error());
}
die();
};
}
void Client::handle_request(ReadonlyBytes raw_request)
ErrorOr<bool> Client::handle_request(ReadonlyBytes raw_request)
{
auto request_or_error = HTTP::HttpRequest::from_raw_request(raw_request);
if (!request_or_error.has_value())
return;
return false;
auto& request = request_or_error.value();
if constexpr (WEBSERVER_DEBUG) {
@ -73,16 +107,16 @@ void Client::handle_request(ReadonlyBytes raw_request)
}
if (request.method() != HTTP::HttpRequest::Method::GET) {
send_error_response(501, request);
return;
TRY(send_error_response(501, request));
return false;
}
// Check for credentials if they are required
if (Configuration::the().credentials().has_value()) {
bool has_authenticated = verify_credentials(request.headers());
if (!has_authenticated) {
send_error_response(401, request, { "WWW-Authenticate: Basic realm=\"WebServer\", charset=\"UTF-8\"" });
return;
TRY(send_error_response(401, request, { "WWW-Authenticate: Basic realm=\"WebServer\", charset=\"UTF-8\"" }));
return false;
}
}
@ -102,8 +136,8 @@ void Client::handle_request(ReadonlyBytes raw_request)
red.append(requested_path);
red.append("/");
send_redirect(red.to_string(), request);
return;
TRY(send_redirect(red.to_string(), request));
return true;
}
StringBuilder index_html_path_builder;
@ -111,29 +145,30 @@ void Client::handle_request(ReadonlyBytes raw_request)
index_html_path_builder.append("/index.html");
auto index_html_path = index_html_path_builder.to_string();
if (!Core::File::exists(index_html_path)) {
handle_directory_listing(requested_path, real_path, request);
return;
TRY(handle_directory_listing(requested_path, real_path, request));
return true;
}
real_path = index_html_path;
}
auto file = Core::File::construct(real_path);
if (!file->open(Core::OpenMode::ReadOnly)) {
send_error_response(404, request);
return;
TRY(send_error_response(404, request));
return false;
}
if (file->is_device()) {
send_error_response(403, request);
return;
TRY(send_error_response(403, request));
return false;
}
Core::InputFileStream stream { file };
send_response(stream, request, Core::guess_mime_type_based_on_filename(real_path));
TRY(send_response(stream, request, Core::guess_mime_type_based_on_filename(real_path)));
return true;
}
void Client::send_response(InputStream& response, HTTP::HttpRequest const& request, String const& content_type)
ErrorOr<void> Client::send_response(InputStream& response, HTTP::HttpRequest const& request, String const& content_type)
{
StringBuilder builder;
builder.append("HTTP/1.0 200 OK\r\n");
@ -146,7 +181,8 @@ void Client::send_response(InputStream& response, HTTP::HttpRequest const& reque
builder.append("\r\n");
builder.append("\r\n");
m_socket->write(builder.to_string());
auto builder_contents = builder.to_byte_buffer();
TRY(m_socket.write(builder_contents));
log_response(200, request);
char buffer[PAGE_SIZE];
@ -155,11 +191,22 @@ void Client::send_response(InputStream& response, HTTP::HttpRequest const& reque
if (response.unreliable_eof() && size == 0)
break;
m_socket->write({ buffer, size });
ReadonlyBytes write_buffer { buffer, size };
while (!write_buffer.is_empty()) {
auto nwritten = TRY(m_socket.write(write_buffer));
if (nwritten == 0) {
dbgln("EEEEEE got 0 bytes written!");
}
write_buffer = write_buffer.slice(nwritten);
}
} while (true);
return {};
}
void Client::send_redirect(StringView redirect_path, HTTP::HttpRequest const& request)
ErrorOr<void> Client::send_redirect(StringView redirect_path, HTTP::HttpRequest const& request)
{
StringBuilder builder;
builder.append("HTTP/1.0 301 Moved Permanently\r\n");
@ -168,9 +215,11 @@ void Client::send_redirect(StringView redirect_path, HTTP::HttpRequest const& re
builder.append("\r\n");
builder.append("\r\n");
m_socket->write(builder.to_string());
auto builder_contents = builder.to_byte_buffer();
TRY(m_socket.write(builder_contents));
log_response(301, request);
return {};
}
static String folder_image_data()
@ -195,7 +244,7 @@ static String file_image_data()
return cache;
}
void Client::handle_directory_listing(String const& requested_path, String const& real_path, HTTP::HttpRequest const& request)
ErrorOr<void> Client::handle_directory_listing(String const& requested_path, String const& real_path, HTTP::HttpRequest const& request)
{
StringBuilder builder;
@ -270,10 +319,10 @@ void Client::handle_directory_listing(String const& requested_path, String const
auto response = builder.to_string();
InputMemoryStream stream { response.bytes() };
send_response(stream, request, "text/html");
return send_response(stream, request, "text/html");
}
void Client::send_error_response(unsigned code, HTTP::HttpRequest const& request, Vector<String> const& headers)
ErrorOr<void> Client::send_error_response(unsigned code, HTTP::HttpRequest const& request, Vector<String> const& headers)
{
auto reason_phrase = HTTP::HttpResponse::reason_phrase_for_code(code);
StringBuilder builder;
@ -292,9 +341,12 @@ void Client::send_error_response(unsigned code, HTTP::HttpRequest const& request
builder.appendff("{} ", code);
builder.append(reason_phrase);
builder.append("</h1></body></html>");
m_socket->write(builder.to_string());
auto builder_contents = builder.to_byte_buffer();
TRY(m_socket.write(builder_contents));
log_response(code, request);
return {};
}
void Client::log_response(unsigned code, HTTP::HttpRequest const& request)

View File

@ -7,8 +7,9 @@
#pragma once
#include <LibCore/Object.h>
#include <LibCore/TCPSocket.h>
#include <LibCore/Stream.h>
#include <LibHTTP/Forward.h>
#include <LibHTTP/HttpRequest.h>
namespace WebServer {
@ -19,18 +20,18 @@ public:
void start();
private:
Client(NonnullRefPtr<Core::TCPSocket>, Core::Object* parent);
Client(Core::Stream::BufferedTCPSocket, Core::Object* parent);
void handle_request(ReadonlyBytes);
void send_response(InputStream&, HTTP::HttpRequest const&, String const& content_type);
void send_redirect(StringView redirect, HTTP::HttpRequest const&);
void send_error_response(unsigned code, HTTP::HttpRequest const&, Vector<String> const& headers = {});
ErrorOr<bool> handle_request(ReadonlyBytes);
ErrorOr<void> send_response(InputStream&, HTTP::HttpRequest const&, String const& content_type);
ErrorOr<void> send_redirect(StringView redirect, HTTP::HttpRequest const&);
ErrorOr<void> send_error_response(unsigned code, HTTP::HttpRequest const&, Vector<String> const& headers = {});
void die();
void log_response(unsigned code, HTTP::HttpRequest const&);
void handle_directory_listing(String const& requested_path, String const& real_path, HTTP::HttpRequest const&);
ErrorOr<void> handle_directory_listing(String const& requested_path, String const& real_path, HTTP::HttpRequest const&);
bool verify_credentials(Vector<HTTP::HttpRequest::Header> const&);
NonnullRefPtr<Core::TCPSocket> m_socket;
Core::Stream::BufferedTCPSocket m_socket;
};
}

View File

@ -72,9 +72,20 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto server = TRY(Core::TCPServer::try_create());
server->on_ready_to_accept = [&] {
auto client_socket = server->accept();
VERIFY(client_socket);
auto client = WebServer::Client::construct(client_socket.release_nonnull(), server);
auto maybe_client_socket = server->accept();
if (maybe_client_socket.is_error()) {
warnln("Failed to accept the client: {}", maybe_client_socket.error());
return;
}
auto maybe_buffered_socket = Core::Stream::BufferedTCPSocket::create(maybe_client_socket.release_value());
if (maybe_buffered_socket.is_error()) {
warnln("Could not obtain a buffered socket for the client: {}", maybe_buffered_socket.error());
return;
}
VERIFY(!maybe_buffered_socket.value().set_blocking(true).is_error());
auto client = WebServer::Client::construct(maybe_buffered_socket.release_value(), server);
client->start();
};