1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:57:44 +00:00

LibCore: Make IODevice::read_line() return a String

Almost everyone using this API actually wanted String instead of a
ByteBuffer anyway, and there were a bunch of slightly different ways
clients would convert to String.

Let's just cut out all the confusion and make it return String. :^)
This commit is contained in:
Andreas Kling 2020-12-13 11:44:53 +01:00
parent 4da327d650
commit b9b7b2b28a
22 changed files with 50 additions and 66 deletions

View file

@ -78,7 +78,7 @@ bool HttpJob::can_read_line() const
return m_socket->can_read_line();
}
ByteBuffer HttpJob::read_line(size_t size)
String HttpJob::read_line(size_t size)
{
return m_socket->read_line(size);
}

View file

@ -55,7 +55,7 @@ protected:
virtual void register_on_ready_to_read(Function<void()>) override;
virtual void register_on_ready_to_write(Function<void()>) override;
virtual bool can_read_line() const override;
virtual ByteBuffer read_line(size_t) override;
virtual String read_line(size_t) override;
virtual bool can_read() const override;
virtual ByteBuffer receive(size_t) override;
virtual bool eof() const override;

View file

@ -126,7 +126,7 @@ bool HttpsJob::can_read_line() const
return m_socket->can_read_line();
}
ByteBuffer HttpsJob::read_line(size_t size)
String HttpsJob::read_line(size_t size)
{
return m_socket->read_line(size);
}

View file

@ -58,7 +58,7 @@ protected:
virtual void register_on_ready_to_read(Function<void()>) override;
virtual void register_on_ready_to_write(Function<void()>) override;
virtual bool can_read_line() const override;
virtual ByteBuffer read_line(size_t) override;
virtual String read_line(size_t) override;
virtual bool can_read() const override;
virtual ByteBuffer receive(size_t) override;
virtual bool eof() const override;

View file

@ -103,9 +103,9 @@ void Job::on_socket_connected()
fprintf(stderr, "Job: Expected HTTP status\n");
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
}
auto parts = String::copy(line, Chomp).split(' ');
auto parts = line.split_view(' ');
if (parts.size() < 3) {
fprintf(stderr, "Job: Expected 3-part HTTP status, got '%s'\n", line.data());
warnln("Job: Expected 3-part HTTP status, got '{}'", line);
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}
auto code = parts[1].to_uint();
@ -131,8 +131,7 @@ void Job::on_socket_connected()
fprintf(stderr, "Job: Expected HTTP header\n");
return did_fail(Core::NetworkJob::Error::ProtocolFailed);
}
auto chomped_line = String::copy(line, Chomp);
if (chomped_line.is_empty()) {
if (line.is_empty()) {
if (m_state == State::Trailers) {
return finish_up();
} else {
@ -140,7 +139,7 @@ void Job::on_socket_connected()
}
return;
}
auto parts = chomped_line.split(':');
auto parts = line.split_view(':');
if (parts.is_empty()) {
if (m_state == State::Trailers) {
// Some servers like to send two ending chunks
@ -152,17 +151,17 @@ void Job::on_socket_connected()
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}
auto name = parts[0];
if (chomped_line.length() < name.length() + 2) {
if (line.length() < name.length() + 2) {
if (m_state == State::Trailers) {
// Some servers like to send two ending chunks
// use this fact as an excuse to ignore anything after the last chunk
// that is not a valid trailing header.
return finish_up();
}
fprintf(stderr, "Job: Malformed HTTP header: '%s' (%zu)\n", chomped_line.characters(), chomped_line.length());
warnln("Job: Malformed HTTP header: '{}' ({})", line, line.length());
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
}
auto value = chomped_line.substring(name.length() + 2, chomped_line.length() - name.length() - 2);
auto value = line.substring(name.length() + 2, line.length() - name.length() - 2);
m_headers.set(name, value);
#ifdef JOB_DEBUG
dbg() << "Job: [" << name << "] = '" << value << "'";
@ -180,7 +179,7 @@ void Job::on_socket_connected()
if (remaining == -1) {
// read size
auto size_data = read_line(PAGE_SIZE);
auto size_lines = StringView { size_data.data(), size_data.size() }.lines();
auto size_lines = size_data.view().lines();
#ifdef JOB_DEBUG
dbg() << "Job: Received a chunk with size _" << size_data << "_";
#endif

View file

@ -52,7 +52,7 @@ protected:
virtual void register_on_ready_to_read(Function<void()>) = 0;
virtual void register_on_ready_to_write(Function<void()>) = 0;
virtual bool can_read_line() const = 0;
virtual ByteBuffer read_line(size_t) = 0;
virtual String read_line(size_t) = 0;
virtual bool can_read() const = 0;
virtual ByteBuffer receive(size_t) = 0;
virtual bool eof() const = 0;