diff options
Diffstat (limited to 'Userland/Libraries/LibHTTP')
-rw-r--r-- | Userland/Libraries/LibHTTP/HttpJob.cpp | 44 | ||||
-rw-r--r-- | Userland/Libraries/LibHTTP/HttpJob.h | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibHTTP/HttpRequest.cpp | 1 | ||||
-rw-r--r-- | Userland/Libraries/LibHTTP/HttpsJob.cpp | 33 | ||||
-rw-r--r-- | Userland/Libraries/LibHTTP/HttpsJob.h | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibHTTP/Job.cpp | 12 | ||||
-rw-r--r-- | Userland/Libraries/LibHTTP/Job.h | 2 |
7 files changed, 70 insertions, 32 deletions
diff --git a/Userland/Libraries/LibHTTP/HttpJob.cpp b/Userland/Libraries/LibHTTP/HttpJob.cpp index 98ffbace48..58ab0e28d6 100644 --- a/Userland/Libraries/LibHTTP/HttpJob.cpp +++ b/Userland/Libraries/LibHTTP/HttpJob.cpp @@ -12,26 +12,35 @@ #include <unistd.h> namespace HTTP { -void HttpJob::start() +void HttpJob::start(NonnullRefPtr<Core::Socket> socket) { VERIFY(!m_socket); - m_socket = Core::TCPSocket::construct(this); - m_socket->on_connected = [this] { - dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_connected callback"); - on_socket_connected(); - }; + m_socket = move(socket); m_socket->on_error = [this] { dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_error callback"); deferred_invoke([this] { did_fail(Core::NetworkJob::Error::ConnectionFailed); }); }; - bool success = m_socket->connect(m_request.url().host(), m_request.url().port_or_default()); - if (!success) { + if (m_socket->is_connected()) { + dbgln("Reusing previous connection for {}", url()); deferred_invoke([this] { - return did_fail(Core::NetworkJob::Error::ConnectionFailed); + dbgln_if(CHTTPJOB_DEBUG, "HttpJob: on_connected callback"); + on_socket_connected(); }); - } + } else { + dbgln("Creating new connection for {}", url()); + m_socket->on_connected = [this] { + dbgln_if(CHTTPJOB_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() @@ -40,13 +49,24 @@ void HttpJob::shutdown() return; m_socket->on_ready_to_read = nullptr; m_socket->on_connected = nullptr; - remove_child(*m_socket); m_socket = nullptr; } void HttpJob::register_on_ready_to_read(Function<void()> callback) { - m_socket->on_ready_to_read = move(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) diff --git a/Userland/Libraries/LibHTTP/HttpJob.h b/Userland/Libraries/LibHTTP/HttpJob.h index 07100cd0a0..16ccb3abfb 100644 --- a/Userland/Libraries/LibHTTP/HttpJob.h +++ b/Userland/Libraries/LibHTTP/HttpJob.h @@ -27,9 +27,12 @@ public: { } - virtual void start() override; + virtual void start(NonnullRefPtr<Core::Socket>) override; virtual void shutdown() 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; diff --git a/Userland/Libraries/LibHTTP/HttpRequest.cpp b/Userland/Libraries/LibHTTP/HttpRequest.cpp index 2bf198290b..5175f75d9c 100644 --- a/Userland/Libraries/LibHTTP/HttpRequest.cpp +++ b/Userland/Libraries/LibHTTP/HttpRequest.cpp @@ -55,7 +55,6 @@ ByteBuffer HttpRequest::to_raw_request() const builder.append(header.value); builder.append("\r\n"); } - builder.append("Connection: close\r\n"); if (!m_body.is_empty()) { builder.appendff("Content-Length: {}\r\n\r\n", m_body.size()); builder.append((char const*)m_body.data(), m_body.size()); diff --git a/Userland/Libraries/LibHTTP/HttpsJob.cpp b/Userland/Libraries/LibHTTP/HttpsJob.cpp index 37e4b7502e..0f249fc5fb 100644 --- a/Userland/Libraries/LibHTTP/HttpsJob.cpp +++ b/Userland/Libraries/LibHTTP/HttpsJob.cpp @@ -14,15 +14,12 @@ namespace HTTP { -void HttpsJob::start() +void HttpsJob::start(NonnullRefPtr<Core::Socket> socket) { VERIFY(!m_socket); - m_socket = TLS::TLSv12::construct(this); - 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(); - }; + 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] { @@ -46,11 +43,22 @@ void HttpsJob::start() if (on_certificate_requested) on_certificate_requested(*this); }; - 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); - }); + if (m_socket->is_established()) { + dbgln("Reusing previous connection for {}", url()); + deferred_invoke([this] { on_socket_connected(); }); + } else { + dbgln("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); + }); + } } } @@ -60,7 +68,6 @@ void HttpsJob::shutdown() return; m_socket->on_tls_ready_to_read = nullptr; m_socket->on_tls_connected = nullptr; - remove_child(*m_socket); m_socket = nullptr; } diff --git a/Userland/Libraries/LibHTTP/HttpsJob.h b/Userland/Libraries/LibHTTP/HttpsJob.h index d3fce85aeb..c7c20ea7e6 100644 --- a/Userland/Libraries/LibHTTP/HttpsJob.h +++ b/Userland/Libraries/LibHTTP/HttpsJob.h @@ -28,10 +28,13 @@ public: { } - virtual void start() override; + virtual void start(NonnullRefPtr<Core::Socket>) override; virtual void shutdown() override; 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: diff --git a/Userland/Libraries/LibHTTP/Job.cpp b/Userland/Libraries/LibHTTP/Job.cpp index 5c75997db8..97c4b317bd 100644 --- a/Userland/Libraries/LibHTTP/Job.cpp +++ b/Userland/Libraries/LibHTTP/Job.cpp @@ -85,7 +85,7 @@ void Job::flush_received_buffers() { if (!m_can_stream_response || m_buffered_size == 0) return; - dbgln_if(JOB_DEBUG, "Job: Flushing received buffers: have {} bytes in {} buffers", m_buffered_size, m_received_buffers.size()); + 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); @@ -100,7 +100,7 @@ void Job::flush_received_buffers() payload = payload.slice(written, payload.size() - written); break; } - dbgln_if(JOB_DEBUG, "Job: Flushing received buffers done: have {} bytes in {} buffers", m_buffered_size, m_received_buffers.size()); + 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::on_socket_connected() @@ -121,6 +121,7 @@ void Job::on_socket_connected() 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()) return; @@ -132,9 +133,12 @@ void Job::on_socket_connected() } if (m_state == State::InStatus) { - if (!can_read_line()) + if (!can_read_line()) { + dbgln_if(JOB_DEBUG, "Job {} cannot read line", m_request.url()); return; + } auto line = read_line(PAGE_SIZE); + dbgln_if(JOB_DEBUG, "Job {} read line of length {}", m_request.url(), line.length()); if (line.is_null()) { dbgln("Job: Expected HTTP status"); return deferred_invoke([this] { did_fail(Core::NetworkJob::Error::TransmissionFailed); }); @@ -287,7 +291,9 @@ void Job::on_socket_connected() } } + 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(); diff --git a/Userland/Libraries/LibHTTP/Job.h b/Userland/Libraries/LibHTTP/Job.h index 338d43192f..99a6e0607f 100644 --- a/Userland/Libraries/LibHTTP/Job.h +++ b/Userland/Libraries/LibHTTP/Job.h @@ -21,7 +21,7 @@ public: explicit Job(const HttpRequest&, OutputStream&); virtual ~Job() override; - virtual void start() override = 0; + virtual void start(NonnullRefPtr<Core::Socket>) override = 0; virtual void shutdown() override = 0; HttpResponse* response() { return static_cast<HttpResponse*>(Core::NetworkJob::response()); } |