2022-07-05 22:13:38 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "RequestManagerQt.h"
|
2022-07-13 22:33:22 +03:00
|
|
|
#include <AK/JsonObject.h>
|
2022-10-12 12:05:08 +03:00
|
|
|
#include <QNetworkCookie>
|
2022-07-05 22:13:38 +03:00
|
|
|
|
|
|
|
RequestManagerQt::RequestManagerQt()
|
|
|
|
{
|
|
|
|
m_qnam = new QNetworkAccessManager(this);
|
|
|
|
|
|
|
|
QObject::connect(m_qnam, &QNetworkAccessManager::finished, this, &RequestManagerQt::reply_finished);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RequestManagerQt::reply_finished(QNetworkReply* reply)
|
|
|
|
{
|
|
|
|
auto request = m_pending.get(reply).value();
|
|
|
|
m_pending.remove(reply);
|
|
|
|
request->did_finish();
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:43:54 +03:00
|
|
|
RefPtr<Web::ResourceLoaderConnectorRequest> RequestManagerQt::start_request(DeprecatedString const& method, AK::URL const& url, HashMap<DeprecatedString, DeprecatedString> const& request_headers, ReadonlyBytes request_body, Core::ProxyData const& proxy)
|
2022-07-05 22:13:38 +03:00
|
|
|
{
|
2023-03-10 10:48:54 +03:00
|
|
|
if (!url.scheme().is_one_of_ignoring_ascii_case("http"sv, "https"sv)) {
|
2022-07-05 22:13:38 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto request_or_error = Request::create(*m_qnam, method, url, request_headers, request_body, proxy);
|
|
|
|
if (request_or_error.is_error()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
auto request = request_or_error.release_value();
|
|
|
|
m_pending.set(&request->reply(), *request);
|
|
|
|
return request;
|
|
|
|
}
|
|
|
|
|
2022-12-04 21:43:54 +03:00
|
|
|
ErrorOr<NonnullRefPtr<RequestManagerQt::Request>> RequestManagerQt::Request::create(QNetworkAccessManager& qnam, DeprecatedString const& method, AK::URL const& url, HashMap<DeprecatedString, DeprecatedString> const& request_headers, ReadonlyBytes request_body, Core::ProxyData const&)
|
2022-07-05 22:13:38 +03:00
|
|
|
{
|
2022-12-04 21:43:54 +03:00
|
|
|
QNetworkRequest request { QString(url.to_deprecated_string().characters()) };
|
2022-07-13 01:16:53 +03:00
|
|
|
request.setAttribute(QNetworkRequest::RedirectPolicyAttribute, QNetworkRequest::ManualRedirectPolicy);
|
2022-12-31 01:29:26 +03:00
|
|
|
request.setAttribute(QNetworkRequest::CookieLoadControlAttribute, QNetworkRequest::Manual);
|
|
|
|
request.setAttribute(QNetworkRequest::CookieSaveControlAttribute, QNetworkRequest::Manual);
|
2022-07-05 22:13:38 +03:00
|
|
|
|
2022-11-07 13:03:36 +03:00
|
|
|
// NOTE: We disable HTTP2 as it's significantly slower (up to 5x, possibly more)
|
|
|
|
request.setAttribute(QNetworkRequest::Http2AllowedAttribute, false);
|
|
|
|
|
2022-07-05 22:13:38 +03:00
|
|
|
QNetworkReply* reply = nullptr;
|
|
|
|
|
2022-07-20 14:01:47 +03:00
|
|
|
for (auto& it : request_headers) {
|
|
|
|
// FIXME: We currently strip the Accept-Encoding header on outgoing requests from LibWeb
|
|
|
|
// since otherwise it'll ask for compression without Qt being aware of it.
|
|
|
|
// This is very hackish and I'm sure we can do it in concert with Qt somehow.
|
|
|
|
if (it.key == "Accept-Encoding")
|
|
|
|
continue;
|
|
|
|
request.setRawHeader(QByteArray(it.key.characters()), QByteArray(it.value.characters()));
|
|
|
|
}
|
|
|
|
|
2023-03-10 10:48:54 +03:00
|
|
|
if (method.equals_ignoring_ascii_case("head"sv)) {
|
2022-07-05 22:13:38 +03:00
|
|
|
reply = qnam.head(request);
|
2023-03-10 10:48:54 +03:00
|
|
|
} else if (method.equals_ignoring_ascii_case("get"sv)) {
|
2022-07-05 22:13:38 +03:00
|
|
|
reply = qnam.get(request);
|
2023-03-10 10:48:54 +03:00
|
|
|
} else if (method.equals_ignoring_ascii_case("post"sv)) {
|
2022-07-05 22:13:38 +03:00
|
|
|
reply = qnam.post(request, QByteArray((char const*)request_body.data(), request_body.size()));
|
2023-03-10 10:48:54 +03:00
|
|
|
} else if (method.equals_ignoring_ascii_case("put"sv)) {
|
2023-02-09 02:25:18 +03:00
|
|
|
reply = qnam.put(request, QByteArray((char const*)request_body.data(), request_body.size()));
|
2023-03-10 10:48:54 +03:00
|
|
|
} else if (method.equals_ignoring_ascii_case("delete"sv)) {
|
2023-02-09 02:25:18 +03:00
|
|
|
reply = qnam.deleteResource(request);
|
|
|
|
} else {
|
|
|
|
reply = qnam.sendCustomRequest(request, QByteArray(method.characters()), QByteArray((char const*)request_body.data(), request_body.size()));
|
2022-07-05 22:13:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return adopt_ref(*new Request(*reply));
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestManagerQt::Request::Request(QNetworkReply& reply)
|
|
|
|
: m_reply(reply)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
RequestManagerQt::Request::~Request() = default;
|
|
|
|
|
|
|
|
void RequestManagerQt::Request::did_finish()
|
|
|
|
{
|
|
|
|
auto buffer = m_reply.readAll();
|
|
|
|
auto http_status_code = m_reply.attribute(QNetworkRequest::Attribute::HttpStatusCodeAttribute).toInt();
|
2022-12-04 21:43:54 +03:00
|
|
|
HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> response_headers;
|
|
|
|
Vector<DeprecatedString> set_cookie_headers;
|
2022-07-05 22:13:38 +03:00
|
|
|
for (auto& it : m_reply.rawHeaderPairs()) {
|
2022-12-04 21:43:54 +03:00
|
|
|
auto name = DeprecatedString(it.first.data(), it.first.length());
|
|
|
|
auto value = DeprecatedString(it.second.data(), it.second.length());
|
2023-03-10 10:48:54 +03:00
|
|
|
if (name.equals_ignoring_ascii_case("set-cookie"sv)) {
|
2022-10-12 12:05:08 +03:00
|
|
|
// NOTE: Qt may have bundled multiple Set-Cookie headers into a single one.
|
|
|
|
// We have to extract the full list of cookies via QNetworkReply::header().
|
|
|
|
auto set_cookie_list = m_reply.header(QNetworkRequest::SetCookieHeader).value<QList<QNetworkCookie>>();
|
|
|
|
for (auto const& cookie : set_cookie_list) {
|
|
|
|
set_cookie_headers.append(cookie.toRawForm().data());
|
|
|
|
}
|
2022-07-05 22:13:38 +03:00
|
|
|
} else {
|
|
|
|
response_headers.set(name, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!set_cookie_headers.is_empty()) {
|
2022-12-04 21:43:54 +03:00
|
|
|
response_headers.set("set-cookie"sv, JsonArray { set_cookie_headers }.to_deprecated_string());
|
2022-07-05 22:13:38 +03:00
|
|
|
}
|
2023-02-24 17:12:32 +03:00
|
|
|
bool success = http_status_code != 0;
|
2022-07-05 22:13:38 +03:00
|
|
|
on_buffered_request_finish(success, buffer.length(), response_headers, http_status_code, ReadonlyBytes { buffer.data(), (size_t)buffer.size() });
|
|
|
|
}
|