2020-01-18 05:38:21 -03:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-01-24 11:28:26 -03:00
|
|
|
#include <AK/Debug.h>
|
2020-02-06 11:04:03 -03:00
|
|
|
#include <LibCore/TCPSocket.h>
|
2020-04-20 18:25:25 -03:00
|
|
|
#include <LibHTTP/HttpJob.h>
|
|
|
|
|
#include <LibHTTP/HttpResponse.h>
|
2019-04-07 09:36:10 -03:00
|
|
|
#include <stdio.h>
|
2019-04-07 14:35:07 -03:00
|
|
|
#include <unistd.h>
|
2019-04-07 09:36:10 -03:00
|
|
|
|
2020-04-20 18:25:25 -03:00
|
|
|
namespace HTTP {
|
2021-09-17 20:18:22 -03:00
|
|
|
void HttpJob::start(NonnullRefPtr<Core::Socket> socket)
|
2019-04-07 23:53:45 -03:00
|
|
|
{
|
2021-02-23 16:42:32 -03:00
|
|
|
VERIFY(!m_socket);
|
2021-09-17 20:18:22 -03:00
|
|
|
m_socket = move(socket);
|
2021-08-12 12:28:19 -03:00
|
|
|
m_socket->on_error = [this] {
|
2022-01-22 13:55:29 -03:00
|
|
|
dbgln_if(HTTPJOB_DEBUG, "HttpJob: on_error callback");
|
2021-08-30 15:12:48 -03:00
|
|
|
deferred_invoke([this] {
|
2021-08-12 12:28:19 -03:00
|
|
|
did_fail(Core::NetworkJob::Error::ConnectionFailed);
|
|
|
|
|
});
|
|
|
|
|
};
|
2021-10-04 08:44:36 -03:00
|
|
|
m_socket->set_idle(false);
|
2021-09-17 20:18:22 -03:00
|
|
|
if (m_socket->is_connected()) {
|
2022-01-22 13:55:29 -03:00
|
|
|
dbgln_if(HTTPJOB_DEBUG, "Reusing previous connection for {}", url());
|
2021-08-30 15:12:48 -03:00
|
|
|
deferred_invoke([this] {
|
2022-01-22 13:55:29 -03:00
|
|
|
dbgln_if(HTTPJOB_DEBUG, "HttpJob: on_connected callback");
|
2021-09-17 20:18:22 -03:00
|
|
|
on_socket_connected();
|
2019-10-08 14:32:34 -03:00
|
|
|
});
|
2021-09-17 20:18:22 -03:00
|
|
|
} else {
|
2022-01-22 13:55:29 -03:00
|
|
|
dbgln_if(HTTPJOB_DEBUG, "Creating new connection for {}", url());
|
2021-09-17 20:18:22 -03:00
|
|
|
m_socket->on_connected = [this] {
|
2022-01-22 13:55:29 -03:00
|
|
|
dbgln_if(HTTPJOB_DEBUG, "HttpJob: on_connected callback");
|
2021-09-17 20:18:22 -03:00
|
|
|
on_socket_connected();
|
|
|
|
|
};
|
|
|
|
|
bool success = m_socket->connect(m_request.url().host(), m_request.url().port_or_default());
|
|
|
|
|
if (!success) {
|
|
|
|
|
deferred_invoke([this] {
|
|
|
|
|
return did_fail(Core::NetworkJob::Error::ConnectionFailed);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2019-04-07 23:53:45 -03:00
|
|
|
}
|
2019-09-21 12:32:26 -03:00
|
|
|
|
2021-09-30 05:49:54 -03:00
|
|
|
void HttpJob::shutdown(ShutdownMode mode)
|
2019-09-21 12:32:26 -03:00
|
|
|
{
|
|
|
|
|
if (!m_socket)
|
|
|
|
|
return;
|
2021-09-30 05:49:54 -03:00
|
|
|
if (mode == ShutdownMode::CloseSocket) {
|
|
|
|
|
m_socket->close();
|
|
|
|
|
} else {
|
|
|
|
|
m_socket->on_ready_to_read = nullptr;
|
|
|
|
|
m_socket->on_connected = nullptr;
|
2021-10-04 08:44:36 -03:00
|
|
|
m_socket->set_idle(true);
|
2021-09-30 05:49:54 -03:00
|
|
|
m_socket = nullptr;
|
|
|
|
|
}
|
2019-09-21 12:32:26 -03:00
|
|
|
}
|
2020-05-05 02:17:40 -03:00
|
|
|
|
|
|
|
|
void HttpJob::register_on_ready_to_read(Function<void()> callback)
|
|
|
|
|
{
|
2021-09-17 20:18:22 -03:00
|
|
|
m_socket->on_ready_to_read = [callback = move(callback), this] {
|
|
|
|
|
callback();
|
|
|
|
|
// As IODevice so graciously buffers everything, there's a possible
|
|
|
|
|
// scenario where it buffers the entire response, and we get stuck waiting
|
|
|
|
|
// for select() in the notifier (which will never return).
|
|
|
|
|
// So handle this case by exhausting the buffer here.
|
|
|
|
|
if (m_socket->can_read_only_from_buffer() && m_state != State::Finished && !has_error()) {
|
|
|
|
|
deferred_invoke([this] {
|
|
|
|
|
if (m_socket && m_socket->on_ready_to_read)
|
|
|
|
|
m_socket->on_ready_to_read();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
2020-05-05 02:17:40 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void HttpJob::register_on_ready_to_write(Function<void()> callback)
|
|
|
|
|
{
|
|
|
|
|
// There is no need to wait, the connection is already established
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-15 03:15:09 -03:00
|
|
|
bool HttpJob::can_read_line() const
|
2020-05-05 02:17:40 -03:00
|
|
|
{
|
|
|
|
|
return m_socket->can_read_line();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-13 07:44:53 -03:00
|
|
|
String HttpJob::read_line(size_t size)
|
2020-05-05 02:17:40 -03:00
|
|
|
{
|
|
|
|
|
return m_socket->read_line(size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ByteBuffer HttpJob::receive(size_t size)
|
|
|
|
|
{
|
|
|
|
|
return m_socket->receive(size);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HttpJob::can_read() const
|
|
|
|
|
{
|
|
|
|
|
return m_socket->can_read();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool HttpJob::eof() const
|
|
|
|
|
{
|
|
|
|
|
return m_socket->eof();
|
|
|
|
|
}
|
|
|
|
|
|
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 10:44:12 -03:00
|
|
|
bool HttpJob::write(ReadonlyBytes bytes)
|
2020-05-05 02:17:40 -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 10:44:12 -03:00
|
|
|
return m_socket->write(bytes);
|
2020-05-05 02:17:40 -03:00
|
|
|
}
|
|
|
|
|
|
2020-02-02 08:34:39 -03:00
|
|
|
}
|