2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2021-01-24 15:28:26 +01:00
|
|
|
#include <AK/Debug.h>
|
2020-02-06 15:04:03 +01:00
|
|
|
#include <LibCore/TCPSocket.h>
|
2020-04-21 01:55:25 +04:30
|
|
|
#include <LibHTTP/HttpJob.h>
|
|
|
|
|
#include <LibHTTP/HttpResponse.h>
|
2019-04-07 14:36:10 +02:00
|
|
|
#include <stdio.h>
|
2019-04-07 19:35:07 +02:00
|
|
|
#include <unistd.h>
|
2019-04-07 14:36:10 +02:00
|
|
|
|
2020-04-21 01:55:25 +04:30
|
|
|
namespace HTTP {
|
2021-09-18 03:48:22 +04:30
|
|
|
void HttpJob::start(NonnullRefPtr<Core::Socket> socket)
|
2019-04-08 04:53:45 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(!m_socket);
|
2021-09-18 03:48:22 +04:30
|
|
|
m_socket = move(socket);
|
2021-08-12 11:28:19 -04:00
|
|
|
m_socket->on_error = [this] {
|
|
|
|
|
dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_error callback");
|
2021-08-30 18:12:48 +00:00
|
|
|
deferred_invoke([this] {
|
2021-08-12 11:28:19 -04:00
|
|
|
did_fail(Core::NetworkJob::Error::ConnectionFailed);
|
|
|
|
|
});
|
|
|
|
|
};
|
2021-10-04 15:14:36 +03:30
|
|
|
m_socket->set_idle(false);
|
2021-09-18 03:48:22 +04:30
|
|
|
if (m_socket->is_connected()) {
|
|
|
|
|
dbgln("Reusing previous connection for {}", url());
|
2021-08-30 18:12:48 +00:00
|
|
|
deferred_invoke([this] {
|
2021-09-18 03:48:22 +04:30
|
|
|
dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_connected callback");
|
|
|
|
|
on_socket_connected();
|
2019-10-08 19:32:34 +02:00
|
|
|
});
|
2021-09-18 03:48:22 +04:30
|
|
|
} else {
|
|
|
|
|
dbgln("Creating new connection for {}", url());
|
|
|
|
|
m_socket->on_connected = [this] {
|
|
|
|
|
dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_connected callback");
|
|
|
|
|
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-08 04:53:45 +02:00
|
|
|
}
|
2019-09-21 17:32:26 +02:00
|
|
|
|
2021-09-30 12:19:54 +03:30
|
|
|
void HttpJob::shutdown(ShutdownMode mode)
|
2019-09-21 17:32:26 +02:00
|
|
|
{
|
|
|
|
|
if (!m_socket)
|
|
|
|
|
return;
|
2021-09-30 12:19:54 +03:30
|
|
|
if (mode == ShutdownMode::CloseSocket) {
|
|
|
|
|
m_socket->close();
|
|
|
|
|
} else {
|
|
|
|
|
m_socket->on_ready_to_read = nullptr;
|
|
|
|
|
m_socket->on_connected = nullptr;
|
2021-10-04 15:14:36 +03:30
|
|
|
m_socket->set_idle(true);
|
2021-09-30 12:19:54 +03:30
|
|
|
m_socket = nullptr;
|
|
|
|
|
}
|
2019-09-21 17:32:26 +02:00
|
|
|
}
|
2020-05-05 09:47:40 +04:30
|
|
|
|
|
|
|
|
void HttpJob::register_on_ready_to_read(Function<void()> callback)
|
|
|
|
|
{
|
2021-09-18 03:48:22 +04:30
|
|
|
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 09:47:40 +04:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 10:45:09 +04:30
|
|
|
bool HttpJob::can_read_line() const
|
2020-05-05 09:47:40 +04:30
|
|
|
{
|
|
|
|
|
return m_socket->can_read_line();
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-13 11:44:53 +01:00
|
|
|
String HttpJob::read_line(size_t size)
|
2020-05-05 09:47:40 +04:30
|
|
|
{
|
|
|
|
|
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 17:14:12 +03:30
|
|
|
bool HttpJob::write(ReadonlyBytes bytes)
|
2020-05-05 09:47:40 +04:30
|
|
|
{
|
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
|
|
|
return m_socket->write(bytes);
|
2020-05-05 09:47:40 +04:30
|
|
|
}
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
}
|