/* * Copyright (c) 2018-2020, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ #include #include namespace Protocol { RequestClient::RequestClient(NonnullOwnPtr socket) : IPC::ConnectionToServer(*this, move(socket)) { } void RequestClient::ensure_connection(URL const& url, ::RequestServer::CacheLevel cache_level) { async_ensure_connection(url, cache_level); } template RefPtr RequestClient::start_request(DeprecatedString const& method, URL const& url, HashMap const& request_headers, ReadonlyBytes request_body, Core::ProxyData const& proxy_data) { IPC::Dictionary header_dictionary; for (auto& it : request_headers) header_dictionary.add(it.key, it.value); auto body_result = ByteBuffer::copy(request_body); if (body_result.is_error()) return nullptr; auto response = IPCProxy::start_request(method, url, header_dictionary, body_result.release_value(), proxy_data); auto request_id = response.request_id(); if (request_id < 0 || !response.response_fd().has_value()) return nullptr; auto response_fd = response.response_fd().value().take_fd(); auto request = Request::create_from_id({}, *this, request_id); request->set_request_fd({}, response_fd); m_requests.set(request_id, request); return request; } bool RequestClient::stop_request(Badge, Request& request) { if (!m_requests.contains(request.id())) return false; return IPCProxy::stop_request(request.id()); } bool RequestClient::set_certificate(Badge, Request& request, DeprecatedString certificate, DeprecatedString key) { if (!m_requests.contains(request.id())) return false; return IPCProxy::set_certificate(request.id(), move(certificate), move(key)); } void RequestClient::request_finished(i32 request_id, bool success, u32 total_size) { RefPtr request; if ((request = m_requests.get(request_id).value_or(nullptr))) { request->did_finish({}, success, total_size); } m_requests.remove(request_id); } void RequestClient::request_progress(i32 request_id, Optional const& total_size, u32 downloaded_size) { if (auto request = const_cast(m_requests.get(request_id).value_or(nullptr))) { request->did_progress({}, total_size, downloaded_size); } } void RequestClient::headers_became_available(i32 request_id, IPC::Dictionary const& response_headers, Optional const& status_code) { if (auto request = const_cast(m_requests.get(request_id).value_or(nullptr))) { HashMap headers; response_headers.for_each_entry([&](auto& name, auto& value) { headers.set(name, value); }); request->did_receive_headers({}, headers, status_code); } } void RequestClient::certificate_requested(i32 request_id) { if (auto request = const_cast(m_requests.get(request_id).value_or(nullptr))) { request->did_request_certificates({}); } } } template RefPtr Protocol::RequestClient::start_request(DeprecatedString const& method, URL const&, HashMap const& request_headers, ReadonlyBytes request_body, Core::ProxyData const&); template RefPtr Protocol::RequestClient::start_request(DeprecatedString const& method, URL const&, HashMap const& request_headers, ReadonlyBytes request_body, Core::ProxyData const&);