mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 20:47:45 +00:00
Userland: Convert TLS::TLSv12 to a Core::Stream::Socket
This commit converts TLS::TLSv12 to a Core::Stream object, and in the process allows TLS to now wrap other Core::Stream::Socket objects. As a large part of LibHTTP and LibGemini depend on LibTLS's interface, this also converts those to support Core::Stream, which leads to a simplification of LibHTTP (as there's no need to care about the underlying socket type anymore). Note that RequestServer now controls the TLS socket options, which is a better place anyway, as RS is the first receiver of the user-requested options (though this is currently not particularly useful).
This commit is contained in:
parent
7a95c451a3
commit
aafc451016
47 changed files with 841 additions and 1157 deletions
|
@ -1,5 +1,4 @@
|
|||
set(SOURCES
|
||||
HttpJob.cpp
|
||||
HttpRequest.cpp
|
||||
HttpResponse.cpp
|
||||
HttpsJob.cpp
|
||||
|
|
|
@ -10,7 +10,6 @@ namespace HTTP {
|
|||
|
||||
class HttpRequest;
|
||||
class HttpResponse;
|
||||
class HttpJob;
|
||||
class HttpsJob;
|
||||
class Job;
|
||||
|
||||
|
|
|
@ -1,114 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Debug.h>
|
||||
#include <LibCore/TCPSocket.h>
|
||||
#include <LibHTTP/HttpJob.h>
|
||||
#include <LibHTTP/HttpResponse.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace HTTP {
|
||||
void HttpJob::start(NonnullRefPtr<Core::Socket> socket)
|
||||
{
|
||||
VERIFY(!m_socket);
|
||||
m_socket = move(socket);
|
||||
m_socket->on_error = [this] {
|
||||
dbgln_if(HTTPJOB_DEBUG, "HttpJob: on_error callback");
|
||||
deferred_invoke([this] {
|
||||
did_fail(Core::NetworkJob::Error::ConnectionFailed);
|
||||
});
|
||||
};
|
||||
m_socket->set_idle(false);
|
||||
if (m_socket->is_connected()) {
|
||||
dbgln_if(HTTPJOB_DEBUG, "Reusing previous connection for {}", url());
|
||||
deferred_invoke([this] {
|
||||
dbgln_if(HTTPJOB_DEBUG, "HttpJob: on_connected callback");
|
||||
on_socket_connected();
|
||||
});
|
||||
} else {
|
||||
dbgln_if(HTTPJOB_DEBUG, "Creating new connection for {}", url());
|
||||
m_socket->on_connected = [this] {
|
||||
dbgln_if(HTTPJOB_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);
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void HttpJob::shutdown(ShutdownMode mode)
|
||||
{
|
||||
if (!m_socket)
|
||||
return;
|
||||
if (mode == ShutdownMode::CloseSocket) {
|
||||
m_socket->close();
|
||||
} else {
|
||||
m_socket->on_ready_to_read = nullptr;
|
||||
m_socket->on_connected = nullptr;
|
||||
m_socket->set_idle(true);
|
||||
m_socket = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void HttpJob::register_on_ready_to_read(Function<void()> callback)
|
||||
{
|
||||
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();
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void HttpJob::register_on_ready_to_write(Function<void()> callback)
|
||||
{
|
||||
// There is no need to wait, the connection is already established
|
||||
callback();
|
||||
}
|
||||
|
||||
bool HttpJob::can_read_line() const
|
||||
{
|
||||
return m_socket->can_read_line();
|
||||
}
|
||||
|
||||
String HttpJob::read_line(size_t size)
|
||||
{
|
||||
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();
|
||||
}
|
||||
|
||||
bool HttpJob::write(ReadonlyBytes bytes)
|
||||
{
|
||||
return m_socket->write(bytes);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,52 +0,0 @@
|
|||
/*
|
||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/HashMap.h>
|
||||
#include <LibCore/NetworkJob.h>
|
||||
#include <LibCore/TCPSocket.h>
|
||||
#include <LibHTTP/HttpRequest.h>
|
||||
#include <LibHTTP/HttpResponse.h>
|
||||
#include <LibHTTP/Job.h>
|
||||
|
||||
namespace HTTP {
|
||||
|
||||
class HttpJob final : public Job {
|
||||
C_OBJECT(HttpJob)
|
||||
public:
|
||||
virtual ~HttpJob() override
|
||||
{
|
||||
}
|
||||
|
||||
virtual void start(NonnullRefPtr<Core::Socket>) override;
|
||||
virtual void shutdown(ShutdownMode) override;
|
||||
|
||||
Core::Socket const* socket() const { return m_socket; }
|
||||
URL url() const { return m_request.url(); }
|
||||
|
||||
protected:
|
||||
virtual bool should_fail_on_empty_payload() const override { return false; }
|
||||
virtual void register_on_ready_to_read(Function<void()>) override;
|
||||
virtual void register_on_ready_to_write(Function<void()>) override;
|
||||
virtual bool can_read_line() const override;
|
||||
virtual String read_line(size_t) override;
|
||||
virtual bool can_read() const override;
|
||||
virtual ByteBuffer receive(size_t) override;
|
||||
virtual bool eof() const override;
|
||||
virtual bool write(ReadonlyBytes) override;
|
||||
virtual bool is_established() const override { return true; }
|
||||
|
||||
private:
|
||||
explicit HttpJob(HttpRequest&& request, OutputStream& output_stream)
|
||||
: Job(move(request), output_stream)
|
||||
{
|
||||
}
|
||||
|
||||
RefPtr<Core::Socket> m_socket;
|
||||
};
|
||||
|
||||
}
|
|
@ -6,8 +6,8 @@
|
|||
|
||||
#include <AK/Base64.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibHTTP/HttpJob.h>
|
||||
#include <LibHTTP/HttpRequest.h>
|
||||
#include <LibHTTP/Job.h>
|
||||
|
||||
namespace HTTP {
|
||||
|
||||
|
|
|
@ -8,9 +8,10 @@
|
|||
|
||||
namespace HTTP {
|
||||
|
||||
HttpResponse::HttpResponse(int code, HashMap<String, String, CaseInsensitiveStringTraits>&& headers)
|
||||
HttpResponse::HttpResponse(int code, HashMap<String, String, CaseInsensitiveStringTraits>&& headers, size_t size)
|
||||
: m_code(code)
|
||||
, m_headers(move(headers))
|
||||
, m_downloaded_size(size)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -15,22 +15,24 @@ namespace HTTP {
|
|||
class HttpResponse : public Core::NetworkResponse {
|
||||
public:
|
||||
virtual ~HttpResponse() override;
|
||||
static NonnullRefPtr<HttpResponse> create(int code, HashMap<String, String, CaseInsensitiveStringTraits>&& headers)
|
||||
static NonnullRefPtr<HttpResponse> create(int code, HashMap<String, String, CaseInsensitiveStringTraits>&& headers, size_t downloaded_size)
|
||||
{
|
||||
return adopt_ref(*new HttpResponse(code, move(headers)));
|
||||
return adopt_ref(*new HttpResponse(code, move(headers), downloaded_size));
|
||||
}
|
||||
|
||||
int code() const { return m_code; }
|
||||
size_t downloaded_size() const { return m_downloaded_size; }
|
||||
StringView reason_phrase() const { return reason_phrase_for_code(m_code); }
|
||||
HashMap<String, String, CaseInsensitiveStringTraits> const& headers() const { return m_headers; }
|
||||
|
||||
static StringView reason_phrase_for_code(int code);
|
||||
|
||||
private:
|
||||
HttpResponse(int code, HashMap<String, String, CaseInsensitiveStringTraits>&&);
|
||||
HttpResponse(int code, HashMap<String, String, CaseInsensitiveStringTraits>&&, size_t size);
|
||||
|
||||
int m_code { 0 };
|
||||
HashMap<String, String, CaseInsensitiveStringTraits> m_headers;
|
||||
size_t m_downloaded_size { 0 };
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -1,143 +1,17 @@
|
|||
/*
|
||||
* Copyright (c) 2020, the SerenityOS developers.
|
||||
* Copyright (c) 2020-2022, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Debug.h>
|
||||
#include <LibCore/EventLoop.h>
|
||||
#include <LibHTTP/HttpResponse.h>
|
||||
#include <LibHTTP/HttpsJob.h>
|
||||
#include <LibTLS/TLSv12.h>
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace HTTP {
|
||||
|
||||
void HttpsJob::start(NonnullRefPtr<Core::Socket> socket)
|
||||
void HttpsJob::set_certificate(String certificate, String key)
|
||||
{
|
||||
VERIFY(!m_socket);
|
||||
VERIFY(is<TLS::TLSv12>(*socket));
|
||||
|
||||
m_socket = static_ptr_cast<TLS::TLSv12>(socket);
|
||||
m_socket->on_tls_error = [&](TLS::AlertDescription error) {
|
||||
if (error == TLS::AlertDescription::HandshakeFailure) {
|
||||
deferred_invoke([this] {
|
||||
return did_fail(Core::NetworkJob::Error::ProtocolFailed);
|
||||
});
|
||||
} else if (error == TLS::AlertDescription::DecryptError) {
|
||||
deferred_invoke([this] {
|
||||
return did_fail(Core::NetworkJob::Error::ConnectionFailed);
|
||||
});
|
||||
} else {
|
||||
deferred_invoke([this] {
|
||||
return did_fail(Core::NetworkJob::Error::TransmissionFailed);
|
||||
});
|
||||
}
|
||||
};
|
||||
m_socket->on_tls_finished = [this] {
|
||||
if (!m_has_scheduled_finish)
|
||||
finish_up();
|
||||
};
|
||||
m_socket->on_tls_certificate_request = [this](auto&) {
|
||||
if (on_certificate_requested)
|
||||
on_certificate_requested(*this);
|
||||
};
|
||||
m_socket->set_idle(false);
|
||||
if (m_socket->is_established()) {
|
||||
dbgln_if(HTTPSJOB_DEBUG, "Reusing previous connection for {}", url());
|
||||
deferred_invoke([this] { on_socket_connected(); });
|
||||
} else {
|
||||
dbgln_if(HTTPSJOB_DEBUG, "Creating a new connection for {}", url());
|
||||
m_socket->set_root_certificates(m_override_ca_certificates ? *m_override_ca_certificates : DefaultRootCACertificates::the().certificates());
|
||||
m_socket->on_tls_connected = [this] {
|
||||
dbgln_if(HTTPSJOB_DEBUG, "HttpsJob: on_connected callback");
|
||||
on_socket_connected();
|
||||
};
|
||||
bool success = ((TLS::TLSv12&)*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);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HttpsJob::shutdown(ShutdownMode mode)
|
||||
{
|
||||
if (!m_socket)
|
||||
return;
|
||||
if (mode == ShutdownMode::CloseSocket) {
|
||||
m_socket->close();
|
||||
} else {
|
||||
m_socket->on_tls_ready_to_read = nullptr;
|
||||
m_socket->on_tls_connected = nullptr;
|
||||
m_socket->set_on_tls_ready_to_write(nullptr);
|
||||
m_socket->set_idle(true);
|
||||
m_socket = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void HttpsJob::set_certificate(String certificate, String private_key)
|
||||
{
|
||||
if (!m_socket->add_client_key(certificate.bytes(), private_key.bytes())) {
|
||||
dbgln("LibHTTP: Failed to set a client certificate");
|
||||
// FIXME: Do something about this failure
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
}
|
||||
|
||||
void HttpsJob::read_while_data_available(Function<IterationDecision()> read)
|
||||
{
|
||||
while (m_socket->can_read()) {
|
||||
if (read() == IterationDecision::Break)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void HttpsJob::register_on_ready_to_read(Function<void()> callback)
|
||||
{
|
||||
m_socket->on_tls_ready_to_read = [callback = move(callback)](auto&) {
|
||||
callback();
|
||||
};
|
||||
}
|
||||
|
||||
void HttpsJob::register_on_ready_to_write(Function<void()> callback)
|
||||
{
|
||||
m_socket->set_on_tls_ready_to_write([callback = move(callback)](auto& tls) {
|
||||
Core::deferred_invoke([&tls] { tls.set_on_tls_ready_to_write(nullptr); });
|
||||
callback();
|
||||
});
|
||||
}
|
||||
|
||||
bool HttpsJob::can_read_line() const
|
||||
{
|
||||
return m_socket->can_read_line();
|
||||
}
|
||||
|
||||
String HttpsJob::read_line(size_t size)
|
||||
{
|
||||
return m_socket->read_line(size);
|
||||
}
|
||||
|
||||
ByteBuffer HttpsJob::receive(size_t size)
|
||||
{
|
||||
return m_socket->read(size);
|
||||
}
|
||||
|
||||
bool HttpsJob::can_read() const
|
||||
{
|
||||
return m_socket->can_read();
|
||||
}
|
||||
|
||||
bool HttpsJob::eof() const
|
||||
{
|
||||
return m_socket->eof();
|
||||
}
|
||||
|
||||
bool HttpsJob::write(ReadonlyBytes data)
|
||||
{
|
||||
return m_socket->write(data);
|
||||
m_received_client_certificates = TLS::TLSv12::parse_pem_certificate(certificate.bytes(), key.bytes());
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
|
||||
#include <AK/HashMap.h>
|
||||
#include <LibCore/NetworkJob.h>
|
||||
#include <LibCore/Stream.h>
|
||||
#include <LibHTTP/HttpRequest.h>
|
||||
#include <LibHTTP/HttpResponse.h>
|
||||
#include <LibHTTP/Job.h>
|
||||
|
@ -22,37 +23,20 @@ public:
|
|||
{
|
||||
}
|
||||
|
||||
virtual void start(NonnullRefPtr<Core::Socket>) override;
|
||||
virtual void shutdown(ShutdownMode) override;
|
||||
bool received_client_certificates() const { return m_received_client_certificates.has_value(); }
|
||||
Vector<TLS::Certificate> take_client_certificates() const { return m_received_client_certificates.release_value(); }
|
||||
|
||||
void set_certificate(String certificate, String key);
|
||||
|
||||
Core::Socket const* socket() const { return m_socket; }
|
||||
URL url() const { return m_request.url(); }
|
||||
|
||||
Function<void(HttpsJob&)> on_certificate_requested;
|
||||
|
||||
protected:
|
||||
virtual void register_on_ready_to_read(Function<void()>) override;
|
||||
virtual void register_on_ready_to_write(Function<void()>) override;
|
||||
virtual bool can_read_line() const override;
|
||||
virtual String read_line(size_t) override;
|
||||
virtual bool can_read() const override;
|
||||
virtual ByteBuffer receive(size_t) override;
|
||||
virtual bool eof() const override;
|
||||
virtual bool write(ReadonlyBytes) override;
|
||||
virtual bool is_established() const override { return m_socket->is_established(); }
|
||||
virtual bool should_fail_on_empty_payload() const override { return false; }
|
||||
virtual void read_while_data_available(Function<IterationDecision()>) override;
|
||||
Function<Vector<TLS::Certificate>()> on_certificate_requested;
|
||||
|
||||
private:
|
||||
explicit HttpsJob(HttpRequest&& request, OutputStream& output_stream, const Vector<Certificate>* override_certs = nullptr)
|
||||
explicit HttpsJob(HttpRequest&& request, Core::Stream::Stream& output_stream)
|
||||
: Job(move(request), output_stream)
|
||||
, m_override_ca_certificates(override_certs)
|
||||
{
|
||||
}
|
||||
|
||||
RefPtr<TLS::TLSv12> m_socket;
|
||||
const Vector<Certificate>* m_override_ca_certificates { nullptr };
|
||||
mutable Optional<Vector<TLS::Certificate>> m_received_client_certificates;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
|
@ -72,7 +72,7 @@ static Optional<ByteBuffer> handle_content_encoding(const ByteBuffer& buf, const
|
|||
return buf;
|
||||
}
|
||||
|
||||
Job::Job(HttpRequest&& request, OutputStream& output_stream)
|
||||
Job::Job(HttpRequest&& request, Core::Stream::Stream& output_stream)
|
||||
: Core::NetworkJob(output_stream)
|
||||
, m_request(move(request))
|
||||
{
|
||||
|
@ -82,6 +82,29 @@ Job::~Job()
|
|||
{
|
||||
}
|
||||
|
||||
void Job::start(Core::Stream::Socket& socket)
|
||||
{
|
||||
VERIFY(!m_socket);
|
||||
m_socket = static_cast<Core::Stream::BufferedSocketBase*>(&socket);
|
||||
dbgln_if(HTTPJOB_DEBUG, "Reusing previous connection for {}", url());
|
||||
deferred_invoke([this] {
|
||||
dbgln_if(HTTPJOB_DEBUG, "HttpJob: on_connected callback");
|
||||
on_socket_connected();
|
||||
});
|
||||
}
|
||||
|
||||
void Job::shutdown(ShutdownMode mode)
|
||||
{
|
||||
if (!m_socket)
|
||||
return;
|
||||
if (mode == ShutdownMode::CloseSocket) {
|
||||
m_socket->close();
|
||||
} else {
|
||||
m_socket->on_ready_to_read = nullptr;
|
||||
m_socket = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void Job::flush_received_buffers()
|
||||
{
|
||||
if (!m_can_stream_response || m_buffered_size == 0)
|
||||
|
@ -89,7 +112,19 @@ void Job::flush_received_buffers()
|
|||
dbgln_if(JOB_DEBUG, "Job: Flushing received buffers: have {} bytes in {} buffers for {}", m_buffered_size, m_received_buffers.size(), m_request.url());
|
||||
for (size_t i = 0; i < m_received_buffers.size(); ++i) {
|
||||
auto& payload = m_received_buffers[i];
|
||||
auto written = do_write(payload);
|
||||
auto result = do_write(payload);
|
||||
if (result.is_error()) {
|
||||
if (!result.error().is_errno()) {
|
||||
dbgln_if(JOB_DEBUG, "Job: Failed to flush received buffers: {}", result.error());
|
||||
continue;
|
||||
}
|
||||
if (result.error().code() == EINTR) {
|
||||
i--;
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
auto written = result.release_value();
|
||||
m_buffered_size -= written;
|
||||
if (written == payload.size()) {
|
||||
// FIXME: Make this a take-first-friendly object?
|
||||
|
@ -104,23 +139,63 @@ void Job::flush_received_buffers()
|
|||
dbgln_if(JOB_DEBUG, "Job: Flushing received buffers done: have {} bytes in {} buffers for {}", m_buffered_size, m_received_buffers.size(), m_request.url());
|
||||
}
|
||||
|
||||
void Job::register_on_ready_to_read(Function<void()> callback)
|
||||
{
|
||||
m_socket->on_ready_to_read = [this, callback = move(callback)] {
|
||||
callback();
|
||||
|
||||
// As `m_socket` is a buffered object, we might not get notifications for data in the buffer
|
||||
// so exhaust the buffer to ensure we don't end up waiting forever.
|
||||
if (MUST(m_socket->can_read_without_blocking()) && m_state != State::Finished && !has_error()) {
|
||||
deferred_invoke([this] {
|
||||
if (m_socket && m_socket->on_ready_to_read)
|
||||
m_socket->on_ready_to_read();
|
||||
});
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
String Job::read_line(size_t size)
|
||||
{
|
||||
auto buffer = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors();
|
||||
auto nread = m_socket->read_until(buffer, "\r\n"sv).release_value_but_fixme_should_propagate_errors();
|
||||
return String::copy(buffer.span().slice(0, nread));
|
||||
}
|
||||
|
||||
ByteBuffer Job::receive(size_t size)
|
||||
{
|
||||
if (size == 0)
|
||||
return {};
|
||||
|
||||
auto buffer = ByteBuffer::create_uninitialized(size).release_value_but_fixme_should_propagate_errors();
|
||||
size_t nread;
|
||||
do {
|
||||
auto result = m_socket->read(buffer);
|
||||
if (result.is_error() && result.error().is_errno() && result.error().code() == EINTR)
|
||||
continue;
|
||||
if (result.is_error()) {
|
||||
dbgln_if(JOB_DEBUG, "Failed while reading: {}", result.error());
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
nread = MUST(result);
|
||||
break;
|
||||
} while (true);
|
||||
return buffer.slice(0, nread);
|
||||
}
|
||||
|
||||
void Job::on_socket_connected()
|
||||
{
|
||||
register_on_ready_to_write([&] {
|
||||
if (m_sent_data)
|
||||
return;
|
||||
m_sent_data = true;
|
||||
auto raw_request = m_request.to_raw_request();
|
||||
auto raw_request = m_request.to_raw_request();
|
||||
|
||||
if constexpr (JOB_DEBUG) {
|
||||
dbgln("Job: raw_request:");
|
||||
dbgln("{}", String::copy(raw_request));
|
||||
}
|
||||
if constexpr (JOB_DEBUG) {
|
||||
dbgln("Job: raw_request:");
|
||||
dbgln("{}", String::copy(raw_request));
|
||||
}
|
||||
|
||||
bool success = m_socket->write_or_error(raw_request);
|
||||
if (!success)
|
||||
deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
|
||||
|
||||
bool success = write(raw_request);
|
||||
if (!success)
|
||||
deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
|
||||
});
|
||||
register_on_ready_to_read([&] {
|
||||
dbgln_if(JOB_DEBUG, "Ready to read for {}, state = {}, cancelled = {}", m_request.url(), to_underlying(m_state), is_cancelled());
|
||||
if (is_cancelled())
|
||||
|
@ -133,12 +208,16 @@ void Job::on_socket_connected()
|
|||
return;
|
||||
}
|
||||
|
||||
if (eof())
|
||||
if (m_socket->is_eof()) {
|
||||
dbgln_if(JOB_DEBUG, "Read failure: Actually EOF!");
|
||||
return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
||||
}
|
||||
|
||||
if (m_state == State::InStatus) {
|
||||
if (!can_read_line()) {
|
||||
while (m_state == State::InStatus) {
|
||||
if (!MUST(m_socket->can_read_line())) {
|
||||
dbgln_if(JOB_DEBUG, "Job {} cannot read line", m_request.url());
|
||||
auto buf = receive(64);
|
||||
dbgln_if(JOB_DEBUG, "{} bytes was read", buf.bytes().size());
|
||||
return;
|
||||
}
|
||||
auto line = read_line(PAGE_SIZE);
|
||||
|
@ -159,11 +238,14 @@ void Job::on_socket_connected()
|
|||
}
|
||||
m_code = code.value();
|
||||
m_state = State::InHeaders;
|
||||
return;
|
||||
}
|
||||
if (m_state == State::InHeaders || m_state == State::Trailers) {
|
||||
if (!can_read_line())
|
||||
if (!MUST(m_socket->can_read_without_blocking()))
|
||||
return;
|
||||
}
|
||||
while (m_state == State::InHeaders || m_state == State::Trailers) {
|
||||
if (!MUST(m_socket->can_read_line())) {
|
||||
dbgln_if(JOB_DEBUG, "Can't read lines anymore :(");
|
||||
return;
|
||||
}
|
||||
// There's no max limit defined on headers, but for our sanity, let's limit it to 32K.
|
||||
auto line = read_line(32 * KiB);
|
||||
if (line.is_null()) {
|
||||
|
@ -179,14 +261,13 @@ void Job::on_socket_connected()
|
|||
if (line.is_empty()) {
|
||||
if (m_state == State::Trailers) {
|
||||
return finish_up();
|
||||
} else {
|
||||
if (on_headers_received) {
|
||||
if (!m_set_cookie_headers.is_empty())
|
||||
m_headers.set("Set-Cookie", JsonArray { m_set_cookie_headers }.to_string());
|
||||
on_headers_received(m_headers, m_code > 0 ? m_code : Optional<u32> {});
|
||||
}
|
||||
m_state = State::InBody;
|
||||
}
|
||||
if (on_headers_received) {
|
||||
if (!m_set_cookie_headers.is_empty())
|
||||
m_headers.set("Set-Cookie", JsonArray { m_set_cookie_headers }.to_string());
|
||||
on_headers_received(m_headers, m_code > 0 ? m_code : Optional<u32> {});
|
||||
}
|
||||
m_state = State::InBody;
|
||||
|
||||
// We've reached the end of the headers, there's a possibility that the server
|
||||
// responds with nothing (content-length = 0 with normal encoding); if that's the case,
|
||||
|
@ -195,7 +276,9 @@ void Job::on_socket_connected()
|
|||
if (result.value() == 0 && !m_headers.get("Transfer-Encoding"sv).value_or(""sv).view().trim_whitespace().equals_ignoring_case("chunked"sv))
|
||||
return finish_up();
|
||||
}
|
||||
return;
|
||||
if (!MUST(m_socket->can_read_line()))
|
||||
return;
|
||||
break;
|
||||
}
|
||||
auto parts = line.split_view(':');
|
||||
if (parts.is_empty()) {
|
||||
|
@ -223,9 +306,9 @@ void Job::on_socket_connected()
|
|||
if (name.equals_ignoring_case("Set-Cookie")) {
|
||||
dbgln_if(JOB_DEBUG, "Job: Received Set-Cookie header: '{}'", value);
|
||||
m_set_cookie_headers.append(move(value));
|
||||
return;
|
||||
}
|
||||
if (auto existing_value = m_headers.get(name); existing_value.has_value()) {
|
||||
if (!MUST(m_socket->can_read_without_blocking()))
|
||||
return;
|
||||
} else if (auto existing_value = m_headers.get(name); existing_value.has_value()) {
|
||||
StringBuilder builder;
|
||||
builder.append(existing_value.value());
|
||||
builder.append(',');
|
||||
|
@ -244,12 +327,16 @@ void Job::on_socket_connected()
|
|||
m_content_length = length.value();
|
||||
}
|
||||
dbgln_if(JOB_DEBUG, "Job: [{}] = '{}'", name, value);
|
||||
return;
|
||||
if (!MUST(m_socket->can_read_without_blocking())) {
|
||||
dbgln_if(JOB_DEBUG, "Can't read headers anymore, byebye :(");
|
||||
return;
|
||||
}
|
||||
}
|
||||
VERIFY(m_state == State::InBody);
|
||||
VERIFY(can_read());
|
||||
if (!MUST(m_socket->can_read_without_blocking()))
|
||||
return;
|
||||
|
||||
read_while_data_available([&] {
|
||||
while (MUST(m_socket->can_read_without_blocking())) {
|
||||
auto read_size = 64 * KiB;
|
||||
if (m_current_chunk_remaining_size.has_value()) {
|
||||
read_chunk_size:;
|
||||
|
@ -260,16 +347,16 @@ void Job::on_socket_connected()
|
|||
if (m_should_read_chunk_ending_line) {
|
||||
VERIFY(size_data.is_empty());
|
||||
m_should_read_chunk_ending_line = false;
|
||||
return IterationDecision::Continue;
|
||||
continue;
|
||||
}
|
||||
auto size_lines = size_data.view().lines();
|
||||
dbgln_if(JOB_DEBUG, "Job: Received a chunk with size '{}'", size_data);
|
||||
if (size_lines.size() == 0) {
|
||||
if (!eof())
|
||||
return AK::IterationDecision::Break;
|
||||
if (!m_socket->is_eof())
|
||||
break;
|
||||
dbgln("Job: Reached end of stream");
|
||||
finish_up();
|
||||
return IterationDecision::Break;
|
||||
break;
|
||||
} else {
|
||||
auto chunk = size_lines[0].split_view(';', true);
|
||||
String size_string = chunk[0];
|
||||
|
@ -278,7 +365,7 @@ void Job::on_socket_connected()
|
|||
if (*endptr) {
|
||||
// invalid number
|
||||
deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); });
|
||||
return IterationDecision::Break;
|
||||
break;
|
||||
}
|
||||
if (size == 0) {
|
||||
// This is the last chunk
|
||||
|
@ -323,19 +410,14 @@ void Job::on_socket_connected()
|
|||
}
|
||||
}
|
||||
|
||||
if (!MUST(m_socket->can_read_without_blocking()))
|
||||
break;
|
||||
|
||||
dbgln_if(JOB_DEBUG, "Waiting for payload for {}", m_request.url());
|
||||
auto payload = receive(read_size);
|
||||
dbgln_if(JOB_DEBUG, "Received {} bytes of payload from {}", payload.size(), m_request.url());
|
||||
if (payload.is_empty()) {
|
||||
if (eof()) {
|
||||
finish_up();
|
||||
return IterationDecision::Break;
|
||||
}
|
||||
|
||||
if (should_fail_on_empty_payload()) {
|
||||
deferred_invoke([this] { did_fail(Core::NetworkJob::Error::ProtocolFailed); });
|
||||
return IterationDecision::Break;
|
||||
}
|
||||
if (payload.is_empty() && m_socket->is_eof()) {
|
||||
finish_up();
|
||||
break;
|
||||
}
|
||||
|
||||
bool read_everything = false;
|
||||
|
@ -357,7 +439,7 @@ void Job::on_socket_connected()
|
|||
if (read_everything) {
|
||||
VERIFY(m_received_size <= m_content_length.value());
|
||||
finish_up();
|
||||
return IterationDecision::Break;
|
||||
break;
|
||||
}
|
||||
|
||||
if (m_current_chunk_remaining_size.has_value()) {
|
||||
|
@ -369,12 +451,12 @@ void Job::on_socket_connected()
|
|||
|
||||
if (m_current_chunk_total_size.value() == 0) {
|
||||
m_state = State::Trailers;
|
||||
return IterationDecision::Break;
|
||||
break;
|
||||
}
|
||||
|
||||
// we've read everything, now let's get the next chunk
|
||||
size = -1;
|
||||
if (can_read_line()) {
|
||||
if (MUST(m_socket->can_read_line())) {
|
||||
auto line = read_line(PAGE_SIZE);
|
||||
VERIFY(line.is_empty());
|
||||
} else {
|
||||
|
@ -383,11 +465,9 @@ void Job::on_socket_connected()
|
|||
}
|
||||
m_current_chunk_remaining_size = size;
|
||||
}
|
||||
}
|
||||
|
||||
return IterationDecision::Continue;
|
||||
});
|
||||
|
||||
if (!is_established()) {
|
||||
if (!m_socket->is_open()) {
|
||||
dbgln_if(JOB_DEBUG, "Connection appears to have closed, finishing up");
|
||||
finish_up();
|
||||
}
|
||||
|
@ -443,7 +523,7 @@ void Job::finish_up()
|
|||
}
|
||||
|
||||
m_has_scheduled_finish = true;
|
||||
auto response = HttpResponse::create(m_code, move(m_headers));
|
||||
auto response = HttpResponse::create(m_code, move(m_headers), m_received_size);
|
||||
deferred_invoke([this, response = move(response)] {
|
||||
// If the server responded with "Connection: close", close the connection
|
||||
// as the server may or may not want to close the socket.
|
||||
|
|
|
@ -17,12 +17,17 @@
|
|||
namespace HTTP {
|
||||
|
||||
class Job : public Core::NetworkJob {
|
||||
C_OBJECT(Job);
|
||||
|
||||
public:
|
||||
explicit Job(HttpRequest&&, OutputStream&);
|
||||
explicit Job(HttpRequest&&, Core::Stream::Stream&);
|
||||
virtual ~Job() override;
|
||||
|
||||
virtual void start(NonnullRefPtr<Core::Socket>) override = 0;
|
||||
virtual void shutdown(ShutdownMode) override = 0;
|
||||
virtual void start(Core::Stream::Socket&) override;
|
||||
virtual void shutdown(ShutdownMode) override;
|
||||
|
||||
Core::Stream::Socket const* socket() const { return m_socket; }
|
||||
URL url() const { return m_request.url(); }
|
||||
|
||||
HttpResponse* response() { return static_cast<HttpResponse*>(Core::NetworkJob::response()); }
|
||||
const HttpResponse* response() const { return static_cast<const HttpResponse*>(Core::NetworkJob::response()); }
|
||||
|
@ -31,18 +36,10 @@ protected:
|
|||
void finish_up();
|
||||
void on_socket_connected();
|
||||
void flush_received_buffers();
|
||||
virtual void register_on_ready_to_read(Function<void()>) = 0;
|
||||
virtual void register_on_ready_to_write(Function<void()>) = 0;
|
||||
virtual bool can_read_line() const = 0;
|
||||
virtual String read_line(size_t) = 0;
|
||||
virtual bool can_read() const = 0;
|
||||
virtual ByteBuffer receive(size_t) = 0;
|
||||
virtual bool eof() const = 0;
|
||||
virtual bool write(ReadonlyBytes) = 0;
|
||||
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(); };
|
||||
virtual void timer_event(Core::TimerEvent&) override;
|
||||
void register_on_ready_to_read(Function<void()>);
|
||||
String read_line(size_t);
|
||||
ByteBuffer receive(size_t);
|
||||
void timer_event(Core::TimerEvent&) override;
|
||||
|
||||
enum class State {
|
||||
InStatus,
|
||||
|
@ -54,13 +51,13 @@ protected:
|
|||
|
||||
HttpRequest m_request;
|
||||
State m_state { State::InStatus };
|
||||
Core::Stream::BufferedSocketBase* m_socket { nullptr };
|
||||
int m_code { -1 };
|
||||
HashMap<String, String, CaseInsensitiveStringTraits> m_headers;
|
||||
Vector<String> m_set_cookie_headers;
|
||||
Vector<ByteBuffer, 2> m_received_buffers;
|
||||
size_t m_buffered_size { 0 };
|
||||
size_t m_received_size { 0 };
|
||||
bool m_sent_data { 0 };
|
||||
Optional<u32> m_content_length;
|
||||
Optional<ssize_t> m_current_chunk_remaining_size;
|
||||
Optional<size_t> m_current_chunk_total_size;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue