2020-05-14 18:34:18 +10:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, The SerenityOS developers.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <LibGemini/GeminiResponse.h>
|
|
|
|
#include <LibGemini/Job.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
//#define JOB_DEBUG
|
|
|
|
|
|
|
|
namespace Gemini {
|
|
|
|
|
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 17:14:12 +03:30
|
|
|
Job::Job(const GeminiRequest& request, OutputStream& output_stream)
|
|
|
|
: Core::NetworkJob(output_stream)
|
|
|
|
, m_request(request)
|
2020-05-14 18:34:18 +10:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Job::~Job()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
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 17:14:12 +03:30
|
|
|
void Job::flush_received_buffers()
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < m_received_buffers.size(); ++i) {
|
|
|
|
auto& payload = m_received_buffers[i];
|
|
|
|
auto written = do_write(payload);
|
|
|
|
m_received_size -= written;
|
|
|
|
if (written == payload.size()) {
|
|
|
|
// FIXME: Make this a take-first-friendly object?
|
|
|
|
m_received_buffers.take_first();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ASSERT(written < payload.size());
|
|
|
|
payload = payload.slice(written, payload.size() - written);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 18:34:18 +10:00
|
|
|
void Job::on_socket_connected()
|
|
|
|
{
|
|
|
|
register_on_ready_to_write([this] {
|
|
|
|
if (m_sent_data)
|
|
|
|
return;
|
|
|
|
m_sent_data = true;
|
|
|
|
auto raw_request = m_request.to_raw_request();
|
|
|
|
#ifdef JOB_DEBUG
|
|
|
|
dbg() << "Job: raw_request:";
|
|
|
|
dbg() << String::copy(raw_request).characters();
|
|
|
|
#endif
|
|
|
|
bool success = write(raw_request);
|
|
|
|
if (!success)
|
|
|
|
deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
|
|
|
|
});
|
|
|
|
register_on_ready_to_read([this] {
|
|
|
|
if (is_cancelled())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (m_state == State::InStatus) {
|
|
|
|
if (!can_read_line())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto line = read_line(PAGE_SIZE);
|
|
|
|
if (line.is_null()) {
|
|
|
|
fprintf(stderr, "Job: Expected status line\n");
|
|
|
|
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
|
|
|
|
}
|
|
|
|
|
2020-12-13 11:44:53 +01:00
|
|
|
auto parts = line.split_limit(' ', 2);
|
2020-05-14 18:34:18 +10:00
|
|
|
if (parts.size() != 2) {
|
2020-12-13 11:44:53 +01:00
|
|
|
warnln("Job: Expected 2-part status line, got '{}'", line);
|
2020-05-14 18:34:18 +10:00
|
|
|
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
|
|
|
}
|
|
|
|
|
2020-06-12 21:07:52 +02:00
|
|
|
auto status = parts[0].to_uint();
|
|
|
|
if (!status.has_value()) {
|
2020-05-14 18:34:18 +10:00
|
|
|
fprintf(stderr, "Job: Expected numeric status code\n");
|
|
|
|
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
|
|
|
}
|
|
|
|
|
2020-06-12 21:07:52 +02:00
|
|
|
m_status = status.value();
|
2020-05-14 18:34:18 +10:00
|
|
|
m_meta = parts[1];
|
|
|
|
|
|
|
|
if (m_status >= 10 && m_status < 20) {
|
|
|
|
m_state = State::Finished;
|
|
|
|
} else if (m_status >= 20 && m_status < 30) {
|
|
|
|
m_state = State::InBody;
|
|
|
|
} else if (m_status >= 30 && m_status < 40) {
|
|
|
|
m_state = State::Finished;
|
|
|
|
} else if (m_status >= 40 && m_status < 50) {
|
|
|
|
m_state = State::Finished;
|
|
|
|
} else if (m_status >= 50 && m_status < 60) {
|
|
|
|
m_state = State::Finished;
|
|
|
|
} else if (m_status >= 60 && m_status < 70) {
|
|
|
|
m_state = State::InBody;
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "Job: Expected status between 10 and 69; instead got %d\n", m_status);
|
|
|
|
return deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ASSERT(m_state == State::InBody || m_state == State::Finished);
|
|
|
|
|
|
|
|
read_while_data_available([&] {
|
AK: Rename KB, MB, GB to KiB, MiB, GiB
The SI prefixes "k", "M", "G" mean "10^3", "10^6", "10^9".
The IEC prefixes "Ki", "Mi", "Gi" mean "2^10", "2^20", "2^30".
Let's use the correct name, at least in code.
Only changes the name of the constants, no other behavior change.
2020-08-15 13:55:00 -04:00
|
|
|
auto read_size = 64 * KiB;
|
2020-05-14 18:34:18 +10:00
|
|
|
|
|
|
|
auto payload = receive(read_size);
|
|
|
|
if (!payload) {
|
|
|
|
if (eof()) {
|
|
|
|
finish_up();
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_fail_on_empty_payload()) {
|
|
|
|
deferred_invoke([this](auto&) { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
|
|
|
return IterationDecision::Break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
m_received_buffers.append(payload);
|
|
|
|
m_received_size += payload.size();
|
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 17:14:12 +03:30
|
|
|
flush_received_buffers();
|
2020-05-14 18:34:18 +10:00
|
|
|
|
2020-05-17 10:29:20 +10:00
|
|
|
deferred_invoke([this](auto&) { did_progress({}, m_received_size); });
|
2020-05-14 18:34:18 +10:00
|
|
|
|
|
|
|
return IterationDecision::Continue;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!is_established()) {
|
|
|
|
#ifdef JOB_DEBUG
|
|
|
|
dbg() << "Connection appears to have closed, finishing up";
|
|
|
|
#endif
|
|
|
|
finish_up();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Job::finish_up()
|
|
|
|
{
|
|
|
|
m_state = State::Finished;
|
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 17:14:12 +03:30
|
|
|
flush_received_buffers();
|
|
|
|
if (m_received_size != 0) {
|
|
|
|
// FIXME: What do we do? ignore it?
|
|
|
|
// "Transmission failed" is not strictly correct, but let's roll with it for now.
|
|
|
|
deferred_invoke([this](auto&) {
|
|
|
|
did_fail(Error::TransmissionFailed);
|
|
|
|
});
|
|
|
|
return;
|
2020-05-14 18:34:18 +10: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 17:14:12 +03:30
|
|
|
auto response = GeminiResponse::create(m_status, m_meta);
|
2020-05-14 18:34:18 +10:00
|
|
|
deferred_invoke([this, response](auto&) {
|
|
|
|
did_finish(move(response));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|