ladybird/Userland/Services/RequestServer/GeminiRequest.cpp
DexesTTP 71d27abb97 Services: Rename ProtocolServer to RequestServer
The current ProtocolServer was really only used for requests, and with
the recent introduction of the WebSocket service, long-lasting
connections with another server are not part of it. To better reflect
this, this commit renames it to RequestServer.

This commit also changes the existing 'protocol' portal to 'request',
the existing 'protocol' user and group to 'request', and most mentions
of the 'download' aspect of the request to 'request' when relevant, to
make everything consistent across the system.

Note that LibProtocol still exists as-is, but the more generic Client
class and the more specific Download class have both been renamed to a
more accurate RequestClient and Request to match the new names.

This commit only change names, not behaviors.
2021-04-25 19:04:34 +02:00

64 lines
2.2 KiB
C++

/*
* Copyright (c) 2020, The SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGemini/GeminiJob.h>
#include <LibGemini/GeminiResponse.h>
#include <RequestServer/GeminiRequest.h>
namespace RequestServer {
GeminiRequest::GeminiRequest(ClientConnection& client, NonnullRefPtr<Gemini::GeminiJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
: Request(client, move(output_stream))
, m_job(job)
{
m_job->on_finish = [this](bool success) {
if (auto* response = m_job->response()) {
set_downloaded_size(this->output_stream().size());
if (!response->meta().is_empty()) {
HashMap<String, String, CaseInsensitiveStringTraits> headers;
headers.set("meta", response->meta());
// Note: We're setting content-type to meta only on status==SUCCESS
// we should perhaps have a better mechanism for this, since we
// are already shoehorning the concept of "headers" here
if (response->status() >= 20 && response->status() < 30) {
headers.set("content-type", response->meta());
}
set_response_headers(headers);
}
}
// signal 100% request progress so any listeners can react
// appropriately
did_progress(downloaded_size(), downloaded_size());
did_finish(success);
};
m_job->on_progress = [this](Optional<u32> total, u32 current) {
did_progress(total, current);
};
m_job->on_certificate_requested = [this](auto&) {
did_request_certificates();
};
}
void GeminiRequest::set_certificate(String certificate, String key)
{
m_job->set_certificate(move(certificate), move(key));
}
GeminiRequest::~GeminiRequest()
{
m_job->on_finish = nullptr;
m_job->on_progress = nullptr;
m_job->shutdown();
}
NonnullOwnPtr<GeminiRequest> GeminiRequest::create_with_job(Badge<GeminiProtocol>, ClientConnection& client, NonnullRefPtr<Gemini::GeminiJob> job, NonnullOwnPtr<OutputFileStream>&& output_stream)
{
return adopt_own(*new GeminiRequest(client, move(job), move(output_stream)));
}
}