mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 04:17:35 +00:00
ProtocolServer: Stream the downloaded data if possible
This patchset makes ProtocolServer stream the downloads to its client (LibProtocol), and as such changes the download API; a possible download lifecycle could be as such: notation = client->server:'>', server->client:'<', pipe activity:'*' ``` > StartDownload(GET, url, headers, {}) < Response(0, fd 8) * {data, 1024b} < HeadersBecameAvailable(0, response_headers, 200) < DownloadProgress(0, 4K, 1024) * {data, 1024b} * {data, 1024b} < DownloadProgress(0, 4K, 2048) * {data, 1024b} < DownloadProgress(0, 4K, 1024) < DownloadFinished(0, true, 4K) ``` Since managing the received file descriptor is a pain, LibProtocol implements `Download::stream_into(OutputStream)`, which can be used to stream the download into any given output stream (be it a file, or memory, or writing stuff with a delay, etc.). Also, as some of the users of this API require all the downloaded data upfront, LibProtocol also implements `set_should_buffer_all_input()`, which causes the download instance to buffer all the data until the download is complete, and to call the `on_buffered_download_finish` hook.
This commit is contained in:
parent
36d642ee75
commit
4a2da10e38
55 changed files with 528 additions and 235 deletions
|
@ -29,6 +29,7 @@
|
|||
#include <AK/SharedBuffer.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/FileStream.h>
|
||||
#include <LibCore/StandardPaths.h>
|
||||
#include <LibDesktop/Launcher.h>
|
||||
#include <LibGUI/BoxLayout.h>
|
||||
|
@ -61,9 +62,19 @@ DownloadWidget::DownloadWidget(const URL& url)
|
|||
m_download->on_progress = [this](Optional<u32> total_size, u32 downloaded_size) {
|
||||
did_progress(total_size.value(), downloaded_size);
|
||||
};
|
||||
m_download->on_finish = [this](bool success, auto payload, auto payload_storage, auto& response_headers, auto) {
|
||||
did_finish(success, payload, payload_storage, response_headers);
|
||||
};
|
||||
|
||||
{
|
||||
auto file_or_error = Core::File::open(m_destination_path, Core::IODevice::WriteOnly);
|
||||
if (file_or_error.is_error()) {
|
||||
GUI::MessageBox::show(window(), String::formatted("Cannot open {} for writing", m_destination_path), "Download failed", GUI::MessageBox::Type::Error);
|
||||
window()->close();
|
||||
return;
|
||||
}
|
||||
m_output_file_stream = make<Core::OutputFileStream>(*file_or_error.value());
|
||||
}
|
||||
|
||||
m_download->on_finish = [this](bool success, auto) { did_finish(success); };
|
||||
m_download->stream_into(*m_output_file_stream);
|
||||
|
||||
set_fill_with_background_color(true);
|
||||
auto& layout = set_layout<GUI::VerticalBoxLayout>();
|
||||
|
@ -149,7 +160,7 @@ void DownloadWidget::did_progress(Optional<u32> total_size, u32 downloaded_size)
|
|||
}
|
||||
}
|
||||
|
||||
void DownloadWidget::did_finish(bool success, [[maybe_unused]] ReadonlyBytes payload, [[maybe_unused]] RefPtr<SharedBuffer> payload_storage, [[maybe_unused]] const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers)
|
||||
void DownloadWidget::did_finish(bool success)
|
||||
{
|
||||
dbg() << "did_finish, success=" << success;
|
||||
|
||||
|
@ -166,17 +177,6 @@ void DownloadWidget::did_finish(bool success, [[maybe_unused]] ReadonlyBytes pay
|
|||
window()->close();
|
||||
return;
|
||||
}
|
||||
|
||||
auto file_or_error = Core::File::open(m_destination_path, Core::IODevice::WriteOnly);
|
||||
if (file_or_error.is_error()) {
|
||||
GUI::MessageBox::show(window(), String::formatted("Cannot open {} for writing", m_destination_path), "Download failed", GUI::MessageBox::Type::Error);
|
||||
window()->close();
|
||||
return;
|
||||
}
|
||||
|
||||
auto& file = *file_or_error.value();
|
||||
bool write_success = file.write(payload.data(), payload.size());
|
||||
ASSERT(write_success);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
|
||||
#include <AK/URL.h>
|
||||
#include <LibCore/ElapsedTimer.h>
|
||||
#include <LibCore/FileStream.h>
|
||||
#include <LibGUI/ProgressBar.h>
|
||||
#include <LibGUI/Widget.h>
|
||||
#include <LibProtocol/Download.h>
|
||||
|
@ -44,7 +45,7 @@ private:
|
|||
explicit DownloadWidget(const URL&);
|
||||
|
||||
void did_progress(Optional<u32> total_size, u32 downloaded_size);
|
||||
void did_finish(bool success, ReadonlyBytes payload, RefPtr<SharedBuffer> payload_storage, const HashMap<String, String, CaseInsensitiveStringTraits>& response_headers);
|
||||
void did_finish(bool success);
|
||||
|
||||
URL m_url;
|
||||
String m_destination_path;
|
||||
|
@ -53,6 +54,7 @@ private:
|
|||
RefPtr<GUI::Label> m_progress_label;
|
||||
RefPtr<GUI::Button> m_cancel_button;
|
||||
RefPtr<GUI::Button> m_close_button;
|
||||
OwnPtr<Core::OutputFileStream> m_output_file_stream;
|
||||
Core::ElapsedTimer m_elapsed_timer;
|
||||
};
|
||||
|
||||
|
|
|
@ -68,7 +68,7 @@ int main(int argc, char** argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
if (pledge("stdio shared_buffer accept unix cpath rpath wpath fattr", nullptr) < 0) {
|
||||
if (pledge("stdio shared_buffer accept unix cpath rpath wpath fattr sendfd recvfd", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
return 1;
|
||||
}
|
||||
|
@ -86,7 +86,7 @@ int main(int argc, char** argv)
|
|||
Web::ResourceLoader::the();
|
||||
|
||||
// FIXME: Once there is a standalone Download Manager, we can drop the "unix" pledge.
|
||||
if (pledge("stdio shared_buffer accept unix cpath rpath wpath", nullptr) < 0) {
|
||||
if (pledge("stdio shared_buffer accept unix cpath rpath wpath sendfd recvfd", nullptr) < 0) {
|
||||
perror("pledge");
|
||||
return 1;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue