LibCore+Everywhere: Make Core::Stream read_until() return Bytes

This affects BufferedSeekable::read_until() and ::read_until_any_of().
For the reasoning, see the previous commit about Core::Stream::read().
This commit is contained in:
Sam Atkins 2022-04-15 13:44:23 +01:00 committed by Tim Flynn
parent 3b1e063d30
commit c4134e9794
Notes: sideshowbarker 2024-07-17 11:46:32 +09:00
4 changed files with 19 additions and 19 deletions

View file

@ -560,16 +560,16 @@ public:
// read. Returns the amount of bytes read.
ErrorOr<size_t> read_line(Bytes buffer)
{
return read_until(buffer, "\n"sv);
return TRY(read_until(buffer, "\n"sv)).size();
}
ErrorOr<size_t> read_until(Bytes buffer, StringView candidate)
ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate)
{
return read_until_any_of(buffer, Array { candidate });
}
template<size_t N>
ErrorOr<size_t> read_until_any_of(Bytes buffer, Array<StringView, N> candidates)
ErrorOr<Bytes> read_until_any_of(Bytes buffer, Array<StringView, N> 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<size_t> read_line(Bytes buffer) { return m_helper.read_line(move(buffer)); }
ErrorOr<size_t> read_until(Bytes buffer, StringView candidate) { return m_helper.read_until(move(buffer), move(candidate)); }
ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) { return m_helper.read_until(move(buffer), move(candidate)); }
template<size_t N>
ErrorOr<size_t> read_until_any_of(Bytes buffer, Array<StringView, N> candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); }
ErrorOr<Bytes> read_until_any_of(Bytes buffer, Array<StringView, N> candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); }
ErrorOr<bool> 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<size_t> read_line(Bytes buffer) = 0;
virtual ErrorOr<size_t> read_until(Bytes buffer, StringView candidate) = 0;
virtual ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) = 0;
virtual ErrorOr<bool> 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<size_t> read_line(Bytes buffer) override { return m_helper.read_line(move(buffer)); }
virtual ErrorOr<size_t> read_until(Bytes buffer, StringView candidate) override { return m_helper.read_until(move(buffer), move(candidate)); }
virtual ErrorOr<Bytes> read_until(Bytes buffer, StringView candidate) override { return m_helper.read_until(move(buffer), move(candidate)); }
template<size_t N>
ErrorOr<size_t> read_until_any_of(Bytes buffer, Array<StringView, N> candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); }
ErrorOr<Bytes> read_until_any_of(Bytes buffer, Array<StringView, N> candidates) { return m_helper.read_until_any_of(move(buffer), move(candidates)); }
virtual ErrorOr<bool> can_read_line() override { return m_helper.can_read_line(); }
virtual size_t buffer_size() const override { return m_helper.buffer_size(); }

View file

@ -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)

View file

@ -157,8 +157,8 @@ void Job::register_on_ready_to_read(Function<void()> callback)
ErrorOr<String> 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<ByteBuffer> Job::receive(size_t size)

View file

@ -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");
}