diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-01-20 11:46:00 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-01-24 17:10:01 +0100 |
commit | de3225a28be9e09f6540489c3927de4396ab07cd (patch) | |
tree | db78d32fc1a4dffb712b6c05b649dbe469eb28f6 /Userland/Libraries/LibProtocol | |
parent | 20b4bfc8924f789a10d655cd01e608b388eff182 (diff) | |
download | serenity-de3225a28be9e09f6540489c3927de4396ab07cd.zip |
LibProtocol: Overload Request::stream_into() to accept a Core::Stream
The code is actually identical to the other stream_into() function.
Diffstat (limited to 'Userland/Libraries/LibProtocol')
-rw-r--r-- | Userland/Libraries/LibProtocol/Request.cpp | 34 | ||||
-rw-r--r-- | Userland/Libraries/LibProtocol/Request.h | 2 |
2 files changed, 36 insertions, 0 deletions
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<InternalStreamData>(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 <AK/String.h> #include <AK/WeakPtr.h> #include <LibCore/Notifier.h> +#include <LibCore/Stream.h> #include <LibIPC/Forward.h> 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! |