From de3225a28be9e09f6540489c3927de4396ab07cd Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Thu, 20 Jan 2022 11:46:00 +0000 Subject: [PATCH] LibProtocol: Overload Request::stream_into() to accept a Core::Stream The code is actually identical to the other stream_into() function. --- Userland/Libraries/LibProtocol/Request.cpp | 34 ++++++++++++++++++++++ Userland/Libraries/LibProtocol/Request.h | 2 ++ 2 files changed, 36 insertions(+) diff --git a/Userland/Libraries/LibProtocol/Request.cpp b/Userland/Libraries/LibProtocol/Request.cpp index 3dd1086d3f..741ff6956b 100644 --- a/Userland/Libraries/LibProtocol/Request.cpp +++ b/Userland/Libraries/LibProtocol/Request.cpp @@ -54,6 +54,40 @@ void Request::stream_into(OutputStream& stream) }; } +void Request::stream_into(Core::Stream::Stream& stream) +{ + VERIFY(!m_internal_stream_data); + + auto notifier = Core::Notifier::construct(fd(), Core::Notifier::Read); + + m_internal_stream_data = make(fd()); + m_internal_stream_data->read_notifier = notifier; + + auto user_on_finish = move(on_finish); + on_finish = [this](auto success, auto total_size) { + m_internal_stream_data->success = success; + m_internal_stream_data->total_size = total_size; + m_internal_stream_data->request_done = true; + }; + + notifier->on_ready_to_read = [this, &stream, user_on_finish = move(user_on_finish)] { + constexpr size_t buffer_size = 4096; + static char buf[buffer_size]; + auto nread = m_internal_stream_data->read_stream.read({ buf, buffer_size }); + if (!stream.write_or_error({ buf, nread })) { + // FIXME: What do we do here? + TODO(); + } + + if (m_internal_stream_data->read_stream.eof() && m_internal_stream_data->request_done) { + m_internal_stream_data->read_notifier->close(); + user_on_finish(m_internal_stream_data->success, m_internal_stream_data->total_size); + } else { + m_internal_stream_data->read_stream.handle_any_error(); + } + }; +} + void Request::set_should_buffer_all_input(bool value) { if (m_should_buffer_all_input == value) diff --git a/Userland/Libraries/LibProtocol/Request.h b/Userland/Libraries/LibProtocol/Request.h index 91f2d1eb9c..cf5cf51262 100644 --- a/Userland/Libraries/LibProtocol/Request.h +++ b/Userland/Libraries/LibProtocol/Request.h @@ -15,6 +15,7 @@ #include #include #include +#include #include namespace Protocol { @@ -38,6 +39,7 @@ public: bool stop(); void stream_into(OutputStream&); + void stream_into(Core::Stream::Stream&); bool should_buffer_all_input() const { return m_should_buffer_all_input; } /// Note: Will override `on_finish', and `on_headers_received', and expects `on_buffered_request_finish' to be set!