From 083802d41ac2ce1a420d9def08da8c3c028ca4a7 Mon Sep 17 00:00:00 2001 From: Arda Cinar Date: Fri, 13 Jan 2023 20:27:14 +0300 Subject: LibGemini: Propagate errors in Job::read_line and Job::receive This patch removes a FIXME --- Userland/Libraries/LibGemini/Job.cpp | 49 +++++++++++++++++++++++------------- Userland/Libraries/LibGemini/Job.h | 4 +-- 2 files changed, 33 insertions(+), 20 deletions(-) (limited to 'Userland/Libraries/LibGemini') diff --git a/Userland/Libraries/LibGemini/Job.cpp b/Userland/Libraries/LibGemini/Job.cpp index 43412e1363..8f03aeef24 100644 --- a/Userland/Libraries/LibGemini/Job.cpp +++ b/Userland/Libraries/LibGemini/Job.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -54,19 +55,18 @@ bool Job::can_read_line() const return MUST(m_socket->can_read_line()); } -DeprecatedString Job::read_line(size_t size) +ErrorOr Job::read_line(size_t size) { - ByteBuffer buffer = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors(); - auto bytes_read = MUST(m_socket->read_until(buffer, "\r\n"sv)); - return DeprecatedString::copy(bytes_read); + ByteBuffer buffer = TRY(ByteBuffer::create_uninitialized(size)); + auto bytes_read = TRY(m_socket->read_until(buffer, "\r\n"sv)); + return String::from_utf8(StringView { bytes_read.data(), bytes_read.size() }); } -ByteBuffer Job::receive(size_t size) +ErrorOr Job::receive(size_t size) { - ByteBuffer buffer = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors(); - auto nread = MUST(m_socket->read(buffer)).size(); - // FIXME: Propagate errors. - return MUST(buffer.slice(0, nread)); + ByteBuffer buffer = TRY(ByteBuffer::create_uninitialized(size)); + auto nread = TRY(m_socket->read(buffer)).size(); + return buffer.slice(0, nread); } bool Job::can_read() const @@ -129,26 +129,33 @@ void Job::on_socket_connected() if (!can_read_line()) return; - auto line = read_line(PAGE_SIZE); - if (line.is_null()) { - dbgln("Job: Expected status line"); + auto line_or_error = read_line(PAGE_SIZE); + if (line_or_error.is_error()) { + dbgln("Job: Error getting status line {}", line_or_error.error()); return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); }); } - auto parts = line.split_limit(' ', 2); - if (parts.size() != 2) { + auto line = line_or_error.release_value(); + auto view = line.bytes_as_string_view(); + + auto maybe_space_index = view.find(' '); + if (!maybe_space_index.has_value()) { dbgln("Job: Expected 2-part status line, got '{}'", line); return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::ProtocolFailed); }); } - auto status = parts[0].to_uint(); + auto space_index = maybe_space_index.release_value(); + auto first_part = view.substring_view(0, space_index); + auto second_part = view.substring_view(space_index + 1); + + auto status = first_part.to_uint(); if (!status.has_value()) { dbgln("Job: Expected numeric status code"); return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::ProtocolFailed); }); } - m_status = status.value(); - m_meta = parts[1]; + m_status = status.release_value(); + m_meta = second_part; if (m_status >= 10 && m_status < 20) { m_state = State::Finished; @@ -178,7 +185,13 @@ void Job::on_socket_connected() while (MUST(m_socket->can_read_without_blocking())) { auto read_size = 64 * KiB; - auto payload = receive(read_size); + auto payload_or_error = receive(read_size); + if (payload_or_error.is_error()) { + dbgln("Job: Error in receive {}", payload_or_error.error()); + return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); }); + } + auto payload = payload_or_error.release_value(); + if (payload.is_empty()) { if (m_socket->is_eof()) { finish_up(); diff --git a/Userland/Libraries/LibGemini/Job.h b/Userland/Libraries/LibGemini/Job.h index afa883721b..aea8c17b87 100644 --- a/Userland/Libraries/LibGemini/Job.h +++ b/Userland/Libraries/LibGemini/Job.h @@ -37,9 +37,9 @@ protected: void flush_received_buffers(); void register_on_ready_to_read(Function); bool can_read_line() const; - DeprecatedString read_line(size_t); + ErrorOr read_line(size_t); bool can_read() const; - ByteBuffer receive(size_t); + ErrorOr receive(size_t); bool write(ReadonlyBytes); enum class State { -- cgit v1.2.3