mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 00:47:45 +00:00
LibGemini: Propagate errors in Job::read_line and Job::receive
This patch removes a FIXME
This commit is contained in:
parent
f883dc3eb0
commit
083802d41a
2 changed files with 33 additions and 20 deletions
|
@ -6,6 +6,7 @@
|
||||||
|
|
||||||
#include <AK/Debug.h>
|
#include <AK/Debug.h>
|
||||||
#include <AK/Error.h>
|
#include <AK/Error.h>
|
||||||
|
#include <AK/String.h>
|
||||||
#include <LibCore/Stream.h>
|
#include <LibCore/Stream.h>
|
||||||
#include <LibGemini/GeminiResponse.h>
|
#include <LibGemini/GeminiResponse.h>
|
||||||
#include <LibGemini/Job.h>
|
#include <LibGemini/Job.h>
|
||||||
|
@ -54,19 +55,18 @@ bool Job::can_read_line() const
|
||||||
return MUST(m_socket->can_read_line());
|
return MUST(m_socket->can_read_line());
|
||||||
}
|
}
|
||||||
|
|
||||||
DeprecatedString Job::read_line(size_t size)
|
ErrorOr<String> Job::read_line(size_t size)
|
||||||
{
|
{
|
||||||
ByteBuffer buffer = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors();
|
ByteBuffer buffer = TRY(ByteBuffer::create_uninitialized(size));
|
||||||
auto bytes_read = MUST(m_socket->read_until(buffer, "\r\n"sv));
|
auto bytes_read = TRY(m_socket->read_until(buffer, "\r\n"sv));
|
||||||
return DeprecatedString::copy(bytes_read);
|
return String::from_utf8(StringView { bytes_read.data(), bytes_read.size() });
|
||||||
}
|
}
|
||||||
|
|
||||||
ByteBuffer Job::receive(size_t size)
|
ErrorOr<ByteBuffer> Job::receive(size_t size)
|
||||||
{
|
{
|
||||||
ByteBuffer buffer = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors();
|
ByteBuffer buffer = TRY(ByteBuffer::create_uninitialized(size));
|
||||||
auto nread = MUST(m_socket->read(buffer)).size();
|
auto nread = TRY(m_socket->read(buffer)).size();
|
||||||
// FIXME: Propagate errors.
|
return buffer.slice(0, nread);
|
||||||
return MUST(buffer.slice(0, nread));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Job::can_read() const
|
bool Job::can_read() const
|
||||||
|
@ -129,26 +129,33 @@ void Job::on_socket_connected()
|
||||||
if (!can_read_line())
|
if (!can_read_line())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto line = read_line(PAGE_SIZE);
|
auto line_or_error = read_line(PAGE_SIZE);
|
||||||
if (line.is_null()) {
|
if (line_or_error.is_error()) {
|
||||||
dbgln("Job: Expected status line");
|
dbgln("Job: Error getting status line {}", line_or_error.error());
|
||||||
return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
|
return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
|
||||||
}
|
}
|
||||||
|
|
||||||
auto parts = line.split_limit(' ', 2);
|
auto line = line_or_error.release_value();
|
||||||
if (parts.size() != 2) {
|
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);
|
dbgln("Job: Expected 2-part status line, got '{}'", line);
|
||||||
return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
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()) {
|
if (!status.has_value()) {
|
||||||
dbgln("Job: Expected numeric status code");
|
dbgln("Job: Expected numeric status code");
|
||||||
return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
||||||
}
|
}
|
||||||
|
|
||||||
m_status = status.value();
|
m_status = status.release_value();
|
||||||
m_meta = parts[1];
|
m_meta = second_part;
|
||||||
|
|
||||||
if (m_status >= 10 && m_status < 20) {
|
if (m_status >= 10 && m_status < 20) {
|
||||||
m_state = State::Finished;
|
m_state = State::Finished;
|
||||||
|
@ -178,7 +185,13 @@ void Job::on_socket_connected()
|
||||||
while (MUST(m_socket->can_read_without_blocking())) {
|
while (MUST(m_socket->can_read_without_blocking())) {
|
||||||
auto read_size = 64 * KiB;
|
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 (payload.is_empty()) {
|
||||||
if (m_socket->is_eof()) {
|
if (m_socket->is_eof()) {
|
||||||
finish_up();
|
finish_up();
|
||||||
|
|
|
@ -37,9 +37,9 @@ protected:
|
||||||
void flush_received_buffers();
|
void flush_received_buffers();
|
||||||
void register_on_ready_to_read(Function<void()>);
|
void register_on_ready_to_read(Function<void()>);
|
||||||
bool can_read_line() const;
|
bool can_read_line() const;
|
||||||
DeprecatedString read_line(size_t);
|
ErrorOr<String> read_line(size_t);
|
||||||
bool can_read() const;
|
bool can_read() const;
|
||||||
ByteBuffer receive(size_t);
|
ErrorOr<ByteBuffer> receive(size_t);
|
||||||
bool write(ReadonlyBytes);
|
bool write(ReadonlyBytes);
|
||||||
|
|
||||||
enum class State {
|
enum class State {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue