2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
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.
2020-12-26 16:44:12 +03:00
|
|
|
#include <AK/FileStream.h>
|
2020-08-29 14:08:36 +03:00
|
|
|
#include <AK/GenericLexer.h>
|
|
|
|
#include <AK/LexicalPath.h>
|
2020-05-03 07:31:06 +03:00
|
|
|
#include <AK/NumberFormat.h>
|
2020-01-01 20:53:34 +03:00
|
|
|
#include <AK/SharedBuffer.h>
|
2020-05-03 07:31:06 +03:00
|
|
|
#include <AK/URL.h>
|
2020-08-05 21:53:16 +03:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2020-02-06 17:04:03 +03:00
|
|
|
#include <LibCore/EventLoop.h>
|
2020-08-29 14:08:36 +03:00
|
|
|
#include <LibCore/File.h>
|
2019-11-23 23:48:39 +03:00
|
|
|
#include <LibProtocol/Client.h>
|
2019-11-24 15:20:44 +03:00
|
|
|
#include <LibProtocol/Download.h>
|
2020-09-27 13:44:03 +03:00
|
|
|
#include <ctype.h>
|
2019-11-23 23:48:39 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-08-29 14:08:36 +03:00
|
|
|
// FIXME: Move this somewhere else when it's needed (e.g. in the Browser)
|
|
|
|
class ContentDispositionParser {
|
|
|
|
public:
|
|
|
|
ContentDispositionParser(const StringView& value)
|
|
|
|
{
|
|
|
|
GenericLexer lexer(value);
|
|
|
|
|
2020-09-27 13:44:03 +03:00
|
|
|
lexer.ignore_while(isspace);
|
2020-08-29 14:08:36 +03:00
|
|
|
|
|
|
|
if (lexer.consume_specific("inline")) {
|
|
|
|
m_kind = Kind::Inline;
|
|
|
|
if (!lexer.is_eof())
|
|
|
|
m_might_be_wrong = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lexer.consume_specific("attachment")) {
|
|
|
|
m_kind = Kind::Attachment;
|
|
|
|
if (lexer.consume_specific(";")) {
|
2020-09-27 13:44:03 +03:00
|
|
|
lexer.ignore_while(isspace);
|
2020-08-29 14:08:36 +03:00
|
|
|
if (lexer.consume_specific("filename=")) {
|
2020-08-31 17:54:46 +03:00
|
|
|
// RFC 2183: "A short (length <= 78 characters)
|
|
|
|
// parameter value containing only non-`tspecials' characters SHOULD be
|
|
|
|
// represented as a single `token'."
|
|
|
|
// Some people seem to take this as generic advice of "if it doesn't have special characters,
|
|
|
|
// it's safe to specify as a single token"
|
|
|
|
// So let's just be as lenient as possible.
|
|
|
|
if (lexer.next_is('"'))
|
|
|
|
m_filename = lexer.consume_quoted_string();
|
|
|
|
else
|
|
|
|
m_filename = lexer.consume_until(is_any_of("()<>@,;:\\\"/[]?= "));
|
2020-08-29 14:08:36 +03:00
|
|
|
} else {
|
|
|
|
m_might_be_wrong = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lexer.consume_specific("form-data")) {
|
|
|
|
m_kind = Kind::FormData;
|
|
|
|
while (lexer.consume_specific(";")) {
|
2020-09-27 13:44:03 +03:00
|
|
|
lexer.ignore_while(isspace);
|
2020-08-29 14:08:36 +03:00
|
|
|
if (lexer.consume_specific("name=")) {
|
|
|
|
m_name = lexer.consume_quoted_string();
|
|
|
|
} else if (lexer.consume_specific("filename=")) {
|
2020-08-31 17:54:46 +03:00
|
|
|
if (lexer.next_is('"'))
|
|
|
|
m_filename = lexer.consume_quoted_string();
|
|
|
|
else
|
|
|
|
m_filename = lexer.consume_until(is_any_of("()<>@,;:\\\"/[]?= "));
|
2020-08-29 14:08:36 +03:00
|
|
|
} else {
|
|
|
|
m_might_be_wrong = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Support 'filename*'
|
|
|
|
m_might_be_wrong = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
enum class Kind {
|
|
|
|
Inline,
|
|
|
|
Attachment,
|
|
|
|
FormData,
|
|
|
|
};
|
|
|
|
|
|
|
|
const StringView& filename() const { return m_filename; }
|
|
|
|
const StringView& name() const { return m_name; }
|
|
|
|
Kind kind() const { return m_kind; }
|
|
|
|
bool might_be_wrong() const { return m_might_be_wrong; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
StringView m_filename;
|
|
|
|
StringView m_name;
|
|
|
|
Kind m_kind { Kind::Inline };
|
|
|
|
bool m_might_be_wrong { false };
|
|
|
|
};
|
|
|
|
|
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.
2020-12-26 16:44:12 +03:00
|
|
|
template<typename ConditionT>
|
|
|
|
class ConditionalOutputFileStream final : public OutputFileStream {
|
|
|
|
public:
|
|
|
|
template<typename... Args>
|
|
|
|
ConditionalOutputFileStream(ConditionT&& condition, Args... args)
|
|
|
|
: OutputFileStream(args...)
|
|
|
|
, m_condition(condition)
|
|
|
|
{
|
|
|
|
}
|
2020-08-29 14:08:36 +03: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.
2020-12-26 16:44:12 +03:00
|
|
|
~ConditionalOutputFileStream()
|
|
|
|
{
|
|
|
|
if (!m_condition())
|
2020-08-29 14:08:36 +03:00
|
|
|
return;
|
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.
2020-12-26 16:44:12 +03:00
|
|
|
|
|
|
|
if (!m_buffer.is_empty()) {
|
|
|
|
OutputFileStream::write(m_buffer);
|
|
|
|
m_buffer.clear();
|
2020-08-29 14:08:36 +03: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.
2020-12-26 16:44:12 +03:00
|
|
|
}
|
2020-08-29 14:08:36 +03: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.
2020-12-26 16:44:12 +03:00
|
|
|
private:
|
|
|
|
size_t write(ReadonlyBytes bytes) override
|
|
|
|
{
|
|
|
|
if (!m_condition()) {
|
|
|
|
write_to_buffer:;
|
|
|
|
m_buffer.append(bytes.data(), bytes.size());
|
|
|
|
return bytes.size();
|
2020-08-29 14:08:36 +03: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.
2020-12-26 16:44:12 +03:00
|
|
|
|
|
|
|
if (!m_buffer.is_empty()) {
|
|
|
|
auto size = OutputFileStream::write(m_buffer);
|
|
|
|
m_buffer = m_buffer.slice(size, m_buffer.size() - size);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!m_buffer.is_empty())
|
|
|
|
goto write_to_buffer;
|
|
|
|
|
|
|
|
return OutputFileStream::write(bytes);
|
2020-08-29 14:08:36 +03: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.
2020-12-26 16:44:12 +03:00
|
|
|
|
|
|
|
ConditionT m_condition;
|
|
|
|
ByteBuffer m_buffer;
|
|
|
|
};
|
2020-08-29 14:08:36 +03:00
|
|
|
|
2019-11-23 23:48:39 +03:00
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
2020-08-05 21:53:16 +03:00
|
|
|
const char* url_str = nullptr;
|
2020-08-29 14:08:36 +03:00
|
|
|
bool save_at_provided_name = false;
|
2020-12-26 16:43:23 +03:00
|
|
|
const char* data = nullptr;
|
|
|
|
String method = "GET";
|
|
|
|
HashMap<String, String, CaseInsensitiveStringTraits> request_headers;
|
2020-08-05 21:53:16 +03:00
|
|
|
|
|
|
|
Core::ArgsParser args_parser;
|
2020-12-06 16:18:25 +03:00
|
|
|
args_parser.set_general_help(
|
|
|
|
"Download a file from an arbitrary URL. This command uses ProtocolServer, "
|
|
|
|
"and thus supports at least http, https, and gemini.");
|
2020-08-29 14:08:36 +03:00
|
|
|
args_parser.add_option(save_at_provided_name, "Write to a file named as the remote file", nullptr, 'O');
|
2020-12-26 16:43:23 +03:00
|
|
|
args_parser.add_option(data, "(HTTP only) Send the provided data via an HTTP POST request", "data", 'd', "data");
|
|
|
|
args_parser.add_option(Core::ArgsParser::Option {
|
|
|
|
.requires_argument = true,
|
|
|
|
.help_string = "Add a header entry to the request",
|
|
|
|
.long_name = "header",
|
|
|
|
.short_name = 'H',
|
|
|
|
.value_name = "header-value",
|
|
|
|
.accept_value = [&](auto* s) {
|
|
|
|
StringView header { s };
|
|
|
|
auto split = header.find_first_of(':');
|
|
|
|
if (!split.has_value())
|
|
|
|
return false;
|
|
|
|
request_headers.set(header.substring_view(0, split.value()), header.substring_view(split.value() + 1));
|
|
|
|
return true;
|
|
|
|
} });
|
2020-08-05 21:53:16 +03:00
|
|
|
args_parser.add_positional_argument(url_str, "URL to download from", "url");
|
|
|
|
args_parser.parse(argc, argv);
|
2019-11-24 00:16:23 +03:00
|
|
|
|
2020-12-26 16:43:23 +03:00
|
|
|
if (data) {
|
|
|
|
method = "POST";
|
|
|
|
// FIXME: Content-Type?
|
|
|
|
}
|
|
|
|
|
2020-08-05 21:53:16 +03:00
|
|
|
URL url(url_str);
|
2019-11-24 00:16:23 +03:00
|
|
|
if (!url.is_valid()) {
|
2020-08-05 21:53:16 +03:00
|
|
|
fprintf(stderr, "'%s' is not a valid URL\n", url_str);
|
2019-11-24 00:16:23 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-02-02 14:34:39 +03:00
|
|
|
Core::EventLoop loop;
|
2020-02-05 20:21:30 +03:00
|
|
|
auto protocol_client = Protocol::Client::construct();
|
2019-11-23 23:48:39 +03:00
|
|
|
|
2020-12-26 16:43:23 +03:00
|
|
|
auto download = protocol_client->start_download(method, url.to_string(), request_headers, data ? StringView { data }.bytes() : ReadonlyBytes {});
|
2020-04-04 21:00:07 +03:00
|
|
|
if (!download) {
|
2020-08-05 21:53:16 +03:00
|
|
|
fprintf(stderr, "Failed to start download for '%s'\n", url_str);
|
2020-04-04 21:00:07 +03:00
|
|
|
return 1;
|
|
|
|
}
|
2020-08-29 14:08:36 +03:00
|
|
|
|
2020-05-03 07:31:06 +03:00
|
|
|
u32 previous_downloaded_size { 0 };
|
|
|
|
timeval prev_time, current_time, time_diff;
|
|
|
|
gettimeofday(&prev_time, nullptr);
|
|
|
|
|
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.
2020-12-26 16:44:12 +03:00
|
|
|
bool received_actual_headers = false;
|
|
|
|
|
2020-05-03 07:31:06 +03:00
|
|
|
download->on_progress = [&](Optional<u32> maybe_total_size, u32 downloaded_size) {
|
|
|
|
fprintf(stderr, "\r\033[2K");
|
2020-05-30 23:17:46 +03:00
|
|
|
if (maybe_total_size.has_value()) {
|
|
|
|
fprintf(stderr, "\033]9;%d;%d;\033\\", downloaded_size, maybe_total_size.value());
|
2020-05-03 07:31:06 +03:00
|
|
|
fprintf(stderr, "Download progress: %s / %s", human_readable_size(downloaded_size).characters(), human_readable_size(maybe_total_size.value()).characters());
|
2020-05-30 23:17:46 +03:00
|
|
|
} else {
|
2020-05-03 07:31:06 +03:00
|
|
|
fprintf(stderr, "Download progress: %s / ???", human_readable_size(downloaded_size).characters());
|
2020-05-30 23:17:46 +03:00
|
|
|
}
|
2020-05-03 07:31:06 +03:00
|
|
|
|
|
|
|
gettimeofday(¤t_time, nullptr);
|
|
|
|
timersub(¤t_time, &prev_time, &time_diff);
|
|
|
|
|
|
|
|
auto time_diff_ms = time_diff.tv_sec * 1000 + time_diff.tv_usec / 1000;
|
|
|
|
auto size_diff = downloaded_size - previous_downloaded_size;
|
|
|
|
|
|
|
|
fprintf(stderr, " at %s/s", human_readable_size(((float)size_diff / (float)time_diff_ms) * 1000).characters());
|
|
|
|
|
|
|
|
previous_downloaded_size = downloaded_size;
|
|
|
|
prev_time = current_time;
|
2019-11-23 23:48:39 +03: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.
2020-12-26 16:44:12 +03:00
|
|
|
|
|
|
|
if (save_at_provided_name) {
|
|
|
|
download->on_headers_received = [&](auto& response_headers, auto status_code) {
|
|
|
|
if (received_actual_headers)
|
|
|
|
return;
|
2021-01-09 17:09:40 +03:00
|
|
|
dbgln("Received headers! response code = {}", status_code.value_or(0));
|
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.
2020-12-26 16:44:12 +03:00
|
|
|
received_actual_headers = true; // And not trailers!
|
2020-08-29 14:08:36 +03:00
|
|
|
String output_name;
|
|
|
|
if (auto content_disposition = response_headers.get("Content-Disposition"); content_disposition.has_value()) {
|
|
|
|
auto& value = content_disposition.value();
|
|
|
|
ContentDispositionParser parser(value);
|
|
|
|
output_name = parser.filename();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (output_name.is_empty())
|
|
|
|
output_name = url.path();
|
|
|
|
|
|
|
|
LexicalPath path { output_name };
|
|
|
|
output_name = path.basename();
|
|
|
|
|
|
|
|
// The URL didn't have a name component, e.g. 'serenityos.org'
|
|
|
|
if (output_name.is_empty() || output_name == "/") {
|
|
|
|
int i = -1;
|
|
|
|
do {
|
|
|
|
output_name = url.host();
|
|
|
|
if (i > -1)
|
|
|
|
output_name = String::format("%s.%d", output_name.characters(), i);
|
|
|
|
++i;
|
|
|
|
} while (Core::File::exists(output_name));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (freopen(output_name.characters(), "w", stdout) == nullptr) {
|
|
|
|
perror("freopen");
|
|
|
|
loop.quit(1);
|
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.
2020-12-26 16:44:12 +03:00
|
|
|
return;
|
2020-08-29 14:08:36 +03: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.
2020-12-26 16:44:12 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
download->on_finish = [&](bool success, auto) {
|
|
|
|
fprintf(stderr, "\033]9;-1;\033\\");
|
|
|
|
fprintf(stderr, "\n");
|
|
|
|
if (!success)
|
2019-11-24 15:20:44 +03:00
|
|
|
fprintf(stderr, "Download failed :(\n");
|
|
|
|
loop.quit(0);
|
2019-11-23 23:48:39 +03: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.
2020-12-26 16:44:12 +03:00
|
|
|
|
|
|
|
auto output_stream = ConditionalOutputFileStream { [&] { return save_at_provided_name ? received_actual_headers : true; }, stdout };
|
|
|
|
download->stream_into(output_stream);
|
|
|
|
|
2019-11-24 15:20:44 +03:00
|
|
|
dbgprintf("started download with id %d\n", download->id());
|
2019-11-23 23:48:39 +03: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.
2020-12-26 16:44:12 +03:00
|
|
|
auto rc = loop.exec();
|
|
|
|
// FIXME: This shouldn't be needed.
|
|
|
|
fclose(stdout);
|
|
|
|
return rc;
|
2019-11-23 23:48:39 +03:00
|
|
|
}
|