diff --git a/Userland/Libraries/LibCore/Stream.h b/Userland/Libraries/LibCore/Stream.h index 2a16e052bc5..067cf238fc7 100644 --- a/Userland/Libraries/LibCore/Stream.h +++ b/Userland/Libraries/LibCore/Stream.h @@ -560,16 +560,16 @@ public: // read. Returns the amount of bytes read. ErrorOr read_line(Bytes buffer) { - return read_until(buffer, "\n"sv); + return TRY(read_until(buffer, "\n"sv)).size(); } - ErrorOr read_until(Bytes buffer, StringView candidate) + ErrorOr read_until(Bytes buffer, StringView candidate) { return read_until_any_of(buffer, Array { candidate }); } template - ErrorOr read_until_any_of(Bytes buffer, Array candidates) + ErrorOr read_until_any_of(Bytes buffer, Array candidates) { if (!stream().is_open()) return Error::from_errno(ENOTCONN); @@ -579,7 +579,7 @@ public: // We fill the buffer through can_read_line. if (!TRY(can_read_line())) - return 0; + return Bytes {}; if (stream().is_eof()) { if (buffer.size() < m_buffered_size) { @@ -623,7 +623,7 @@ public: m_buffered_size -= size_written_to_user_buffer + match_size; - return size_written_to_user_buffer; + return buffer.slice(0, size_written_to_user_buffer); } // If we still haven't found anything, then it's most likely the case @@ -638,7 +638,7 @@ public: m_buffered_size -= readable_size; - return readable_size; + return buffer.slice(0, readable_size); } // Returns whether a line can be read, populating the buffer in the process. @@ -770,9 +770,9 @@ public: } ErrorOr read_line(Bytes buffer) { return m_helper.read_line(move(buffer)); } - ErrorOr read_until(Bytes buffer, StringView candidate) { return m_helper.read_until(move(buffer), move(candidate)); } + ErrorOr read_until(Bytes buffer, StringView candidate) { return m_helper.read_until(move(buffer), move(candidate)); } template - ErrorOr read_until_any_of(Bytes buffer, Array candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); } + ErrorOr read_until_any_of(Bytes buffer, Array candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); } ErrorOr can_read_line() { return m_helper.can_read_line(); } size_t buffer_size() const { return m_helper.buffer_size(); } @@ -791,7 +791,7 @@ private: class BufferedSocketBase : public Socket { public: virtual ErrorOr read_line(Bytes buffer) = 0; - virtual ErrorOr read_until(Bytes buffer, StringView candidate) = 0; + virtual ErrorOr read_until(Bytes buffer, StringView candidate) = 0; virtual ErrorOr can_read_line() = 0; virtual size_t buffer_size() const = 0; }; @@ -839,9 +839,9 @@ public: virtual void set_notifications_enabled(bool enabled) override { m_helper.stream().set_notifications_enabled(enabled); } virtual ErrorOr read_line(Bytes buffer) override { return m_helper.read_line(move(buffer)); } - virtual ErrorOr read_until(Bytes buffer, StringView candidate) override { return m_helper.read_until(move(buffer), move(candidate)); } + virtual ErrorOr read_until(Bytes buffer, StringView candidate) override { return m_helper.read_until(move(buffer), move(candidate)); } template - ErrorOr read_until_any_of(Bytes buffer, Array candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); } + ErrorOr read_until_any_of(Bytes buffer, Array candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); } virtual ErrorOr can_read_line() override { return m_helper.can_read_line(); } virtual size_t buffer_size() const override { return m_helper.buffer_size(); } diff --git a/Userland/Libraries/LibGemini/Job.cpp b/Userland/Libraries/LibGemini/Job.cpp index 171bfd71ba2..f7cd212d0f9 100644 --- a/Userland/Libraries/LibGemini/Job.cpp +++ b/Userland/Libraries/LibGemini/Job.cpp @@ -56,8 +56,8 @@ bool Job::can_read_line() const String Job::read_line(size_t size) { ByteBuffer buffer = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors(); - auto nread = MUST(m_socket->read_until(buffer, "\r\n"sv)); - return String::copy(buffer.span().slice(0, nread)); + auto bytes_read = MUST(m_socket->read_until(buffer, "\r\n"sv)); + return String::copy(bytes_read); } ByteBuffer Job::receive(size_t size) diff --git a/Userland/Libraries/LibHTTP/Job.cpp b/Userland/Libraries/LibHTTP/Job.cpp index f39972b4eb5..a24ed25d323 100644 --- a/Userland/Libraries/LibHTTP/Job.cpp +++ b/Userland/Libraries/LibHTTP/Job.cpp @@ -157,8 +157,8 @@ void Job::register_on_ready_to_read(Function callback) ErrorOr Job::read_line(size_t size) { auto buffer = TRY(ByteBuffer::create_uninitialized(size)); - auto nread = TRY(m_socket->read_until(buffer, "\r\n"sv)); - return String::copy(buffer.span().slice(0, nread)); + auto bytes_read = TRY(m_socket->read_until(buffer, "\r\n"sv)); + return String::copy(bytes_read); } ErrorOr Job::receive(size_t size) diff --git a/Userland/Services/WebServer/Client.cpp b/Userland/Services/WebServer/Client.cpp index 72e7ae5ab15..204d4010505 100644 --- a/Userland/Services/WebServer/Client.cpp +++ b/Userland/Services/WebServer/Client.cpp @@ -64,9 +64,9 @@ void Client::start() if (!maybe_can_read.value()) break; - 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()); + auto maybe_bytes_read = m_socket->read_until_any_of(buffer, Array { "\r"sv, "\n"sv, "\r\n"sv }); + if (maybe_bytes_read.is_error()) { + warnln("Failed to read a line from the request: {}", maybe_bytes_read.error()); die(); return; } @@ -76,7 +76,7 @@ void Client::start() break; } - builder.append(StringView { buffer.data(), maybe_nread.value() }); + builder.append(StringView { maybe_bytes_read.value() }); builder.append("\r\n"); }