2020-05-14 18:35:46 +10:00
|
|
|
/*
|
2021-04-28 22:46:44 +02:00
|
|
|
* Copyright (c) 2020, the SerenityOS developers.
|
2020-05-14 18:35:46 +10:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-05-14 18:35:46 +10:00
|
|
|
*/
|
|
|
|
|
2021-09-18 03:48:22 +04:30
|
|
|
#include "ConnectionCache.h"
|
|
|
|
#include <LibCore/EventLoop.h>
|
2020-05-17 16:33:09 +02:00
|
|
|
#include <LibGemini/GeminiResponse.h>
|
2022-02-02 19:21:55 +03:30
|
|
|
#include <LibGemini/Job.h>
|
2021-04-23 22:45:52 +02:00
|
|
|
#include <RequestServer/GeminiRequest.h>
|
2020-05-14 18:35:46 +10:00
|
|
|
|
2021-04-23 22:45:52 +02:00
|
|
|
namespace RequestServer {
|
2020-05-17 16:33:09 +02:00
|
|
|
|
2022-02-25 12:18:30 +02:00
|
|
|
GeminiRequest::GeminiRequest(ConnectionFromClient& client, NonnullRefPtr<Gemini::Job> job, NonnullOwnPtr<Core::Stream::File>&& output_stream)
|
2021-04-23 22:45:52 +02:00
|
|
|
: Request(client, move(output_stream))
|
2022-02-02 19:21:55 +03:30
|
|
|
, m_job(move(job))
|
2020-05-14 18:35:46 +10:00
|
|
|
{
|
|
|
|
m_job->on_finish = [this](bool success) {
|
2021-09-18 03:48:22 +04:30
|
|
|
Core::deferred_invoke([url = m_job->url(), socket = m_job->socket()] {
|
|
|
|
ConnectionCache::request_did_finish(url, socket);
|
|
|
|
});
|
2020-05-14 18:35:46 +10:00
|
|
|
if (auto* response = m_job->response()) {
|
2022-02-02 19:21:55 +03:30
|
|
|
set_downloaded_size(MUST(const_cast<Core::Stream::File&>(this->output_stream()).size()));
|
2020-05-14 18:35:46 +10:00
|
|
|
if (!response->meta().is_empty()) {
|
2022-12-04 18:02:33 +00:00
|
|
|
HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> headers;
|
2020-05-14 18:35:46 +10:00
|
|
|
headers.set("meta", response->meta());
|
2020-05-16 15:38:13 +04:30
|
|
|
// Note: We're setting content-type to meta only on status==SUCCESS
|
2020-10-02 22:14:37 +01:00
|
|
|
// we should perhaps have a better mechanism for this, since we
|
2020-05-16 15:38:13 +04:30
|
|
|
// are already shoehorning the concept of "headers" here
|
|
|
|
if (response->status() >= 20 && response->status() < 30) {
|
|
|
|
headers.set("content-type", response->meta());
|
|
|
|
}
|
2020-05-14 18:35:46 +10:00
|
|
|
set_response_headers(headers);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-23 22:45:52 +02:00
|
|
|
// signal 100% request progress so any listeners can react
|
2020-05-14 18:35:46 +10:00
|
|
|
// appropriately
|
|
|
|
did_progress(downloaded_size(), downloaded_size());
|
|
|
|
|
|
|
|
did_finish(success);
|
|
|
|
};
|
|
|
|
m_job->on_progress = [this](Optional<u32> total, u32 current) {
|
2022-02-02 19:21:55 +03:30
|
|
|
did_progress(move(total), current);
|
2020-08-02 05:27:42 +04:30
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void GeminiRequest::set_certificate(DeprecatedString, DeprecatedString)
|
2020-08-02 05:27:42 +04:30
|
|
|
{
|
2020-05-14 18:35:46 +10:00
|
|
|
}
|
|
|
|
|
2021-04-23 22:45:52 +02:00
|
|
|
GeminiRequest::~GeminiRequest()
|
2020-05-14 18:35:46 +10:00
|
|
|
{
|
2020-05-16 05:36:40 +10:00
|
|
|
m_job->on_finish = nullptr;
|
|
|
|
m_job->on_progress = nullptr;
|
2021-09-19 18:18:19 +04:30
|
|
|
m_job->cancel();
|
2020-05-14 18:35:46 +10:00
|
|
|
}
|
|
|
|
|
2022-02-25 12:18:30 +02:00
|
|
|
NonnullOwnPtr<GeminiRequest> GeminiRequest::create_with_job(Badge<GeminiProtocol>, ConnectionFromClient& client, NonnullRefPtr<Gemini::Job> job, NonnullOwnPtr<Core::Stream::File>&& output_stream)
|
2020-05-14 18:35:46 +10:00
|
|
|
{
|
2021-04-23 22:45:52 +02:00
|
|
|
return adopt_own(*new GeminiRequest(client, move(job), move(output_stream)));
|
2020-05-14 18:35:46 +10:00
|
|
|
}
|
2020-05-17 16:33:09 +02:00
|
|
|
|
|
|
|
}
|