2020-05-14 05:35:46 -03:00
|
|
|
/*
|
2021-04-28 17:46:44 -03:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-05-14 05:35:46 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-14 05:35:46 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-09-17 20:18:22 -03:00
|
|
|
#include "ConnectionCache.h"
|
2020-05-17 11:33:09 -03:00
|
|
|
#include <LibGemini/GeminiRequest.h>
|
2022-02-02 12:51:55 -03:00
|
|
|
#include <LibGemini/Job.h>
|
2021-04-23 17:45:52 -03:00
|
|
|
#include <RequestServer/GeminiProtocol.h>
|
|
|
|
|
#include <RequestServer/GeminiRequest.h>
|
2020-05-14 05:35:46 -03:00
|
|
|
|
2021-04-23 17:45:52 -03:00
|
|
|
namespace RequestServer {
|
2020-05-17 11:33:09 -03:00
|
|
|
|
2020-05-14 05:35:46 -03:00
|
|
|
GeminiProtocol::GeminiProtocol()
|
|
|
|
|
: Protocol("gemini")
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-07 13:40:33 -03:00
|
|
|
OwnPtr<Request> GeminiProtocol::start_request(ConnectionFromClient& client, String const&, const URL& url, HashMap<String, String> const&, ReadonlyBytes, Core::ProxyData proxy_data)
|
2020-05-14 05:35:46 -03:00
|
|
|
{
|
|
|
|
|
Gemini::GeminiRequest request;
|
|
|
|
|
request.set_url(url);
|
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
|
|
|
|
2021-04-23 17:45:52 -03:00
|
|
|
auto pipe_result = get_pipe_for_request();
|
2020-12-30 19:12:44 -03:00
|
|
|
if (pipe_result.is_error())
|
2021-01-10 20:29:28 -03:00
|
|
|
return {};
|
2020-12-30 19:12:44 -03:00
|
|
|
|
2022-02-02 12:51:55 -03:00
|
|
|
auto output_stream = MUST(Core::Stream::File::adopt_fd(pipe_result.value().write_fd, Core::Stream::OpenMode::Write));
|
|
|
|
|
auto job = Gemini::Job::construct(request, *output_stream);
|
|
|
|
|
auto protocol_request = GeminiRequest::create_with_job({}, client, *job, move(output_stream));
|
2021-04-23 17:45:52 -03:00
|
|
|
protocol_request->set_request_fd(pipe_result.value().read_fd);
|
2021-09-17 20:18:22 -03:00
|
|
|
|
2022-04-07 13:40:33 -03:00
|
|
|
ConnectionCache::get_or_create_connection(ConnectionCache::g_tls_connection_cache, url, *job, proxy_data);
|
2021-09-17 20:18:22 -03:00
|
|
|
|
2021-04-23 17:45:52 -03:00
|
|
|
return protocol_request;
|
2020-05-14 05:35:46 -03:00
|
|
|
}
|
2020-05-17 11:33:09 -03:00
|
|
|
|
|
|
|
|
}
|