2020-05-05 09:47:40 +04:30
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-05-05 09:47:40 +04:30
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-05 09:47:40 +04:30
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
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
|
|
|
#include <AK/FileStream.h>
|
2020-05-05 09:47:40 +04:30
|
|
|
#include <AK/HashMap.h>
|
2020-05-12 02:55:10 +04:30
|
|
|
#include <AK/Optional.h>
|
2020-05-05 09:47:40 +04:30
|
|
|
#include <LibCore/NetworkJob.h>
|
|
|
|
|
#include <LibCore/TCPSocket.h>
|
|
|
|
|
#include <LibHTTP/HttpRequest.h>
|
|
|
|
|
#include <LibHTTP/HttpResponse.h>
|
|
|
|
|
|
|
|
|
|
namespace HTTP {
|
|
|
|
|
|
|
|
|
|
class Job : public Core::NetworkJob {
|
|
|
|
|
public:
|
2021-11-28 23:00:52 +01:00
|
|
|
explicit Job(HttpRequest&&, OutputStream&);
|
2020-05-05 09:47:40 +04:30
|
|
|
virtual ~Job() override;
|
|
|
|
|
|
2021-09-18 03:48:22 +04:30
|
|
|
virtual void start(NonnullRefPtr<Core::Socket>) override = 0;
|
2021-09-30 12:19:54 +03:30
|
|
|
virtual void shutdown(ShutdownMode) override = 0;
|
2020-05-05 09:47:40 +04:30
|
|
|
|
|
|
|
|
HttpResponse* response() { return static_cast<HttpResponse*>(Core::NetworkJob::response()); }
|
|
|
|
|
const HttpResponse* response() const { return static_cast<const HttpResponse*>(Core::NetworkJob::response()); }
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
void finish_up();
|
|
|
|
|
void on_socket_connected();
|
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 flush_received_buffers();
|
2020-05-05 09:47:40 +04:30
|
|
|
virtual void register_on_ready_to_read(Function<void()>) = 0;
|
|
|
|
|
virtual void register_on_ready_to_write(Function<void()>) = 0;
|
2020-05-15 10:45:09 +04:30
|
|
|
virtual bool can_read_line() const = 0;
|
2020-12-13 11:44:53 +01:00
|
|
|
virtual String read_line(size_t) = 0;
|
2020-05-05 09:47:40 +04:30
|
|
|
virtual bool can_read() const = 0;
|
|
|
|
|
virtual ByteBuffer receive(size_t) = 0;
|
|
|
|
|
virtual bool eof() const = 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 17:14:12 +03:30
|
|
|
virtual bool write(ReadonlyBytes) = 0;
|
2020-05-05 09:47:40 +04:30
|
|
|
virtual bool is_established() const = 0;
|
|
|
|
|
virtual bool should_fail_on_empty_payload() const { return true; }
|
|
|
|
|
virtual void read_while_data_available(Function<IterationDecision()> read) { read(); };
|
2021-05-12 04:26:25 +04:30
|
|
|
virtual void timer_event(Core::TimerEvent&) override;
|
2020-05-05 09:47:40 +04:30
|
|
|
|
|
|
|
|
enum class State {
|
|
|
|
|
InStatus,
|
|
|
|
|
InHeaders,
|
|
|
|
|
InBody,
|
2020-08-18 20:34:15 -06:00
|
|
|
Trailers,
|
2020-05-05 09:47:40 +04:30
|
|
|
Finished,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
HttpRequest m_request;
|
|
|
|
|
State m_state { State::InStatus };
|
|
|
|
|
int m_code { -1 };
|
2020-05-12 19:07:42 +02:00
|
|
|
HashMap<String, String, CaseInsensitiveStringTraits> m_headers;
|
2021-08-11 06:09:35 +08:00
|
|
|
Vector<String> m_set_cookie_headers;
|
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
|
|
|
Vector<ByteBuffer, 2> m_received_buffers;
|
|
|
|
|
size_t m_buffered_size { 0 };
|
2020-05-05 09:47:40 +04:30
|
|
|
size_t m_received_size { 0 };
|
|
|
|
|
bool m_sent_data { 0 };
|
2021-10-16 20:17:18 +02:00
|
|
|
Optional<u32> m_content_length;
|
2020-05-12 02:55:10 +04:30
|
|
|
Optional<ssize_t> m_current_chunk_remaining_size;
|
|
|
|
|
Optional<size_t> m_current_chunk_total_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
|
|
|
bool m_can_stream_response { true };
|
2021-04-12 00:47:33 +04:30
|
|
|
bool m_should_read_chunk_ending_line { false };
|
2021-06-29 01:40:18 +04:30
|
|
|
bool m_has_scheduled_finish { false };
|
2020-05-05 09:47:40 +04:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|