From b4249ea941daf50606ffaa32bd74df892bec1611 Mon Sep 17 00:00:00 2001 From: Patrick Sean Klein Date: Tue, 9 May 2023 22:59:40 +0100 Subject: [PATCH] Save URL in NetworkRequest. --- src/core/NetworkRequest.cpp | 39 ++++++------ src/core/NetworkRequest.h | 119 +++++++++++++++++++----------------- 2 files changed, 85 insertions(+), 73 deletions(-) diff --git a/src/core/NetworkRequest.cpp b/src/core/NetworkRequest.cpp index 4e4e941bb..7f362f0cc 100644 --- a/src/core/NetworkRequest.cpp +++ b/src/core/NetworkRequest.cpp @@ -1,10 +1,10 @@ #include "NetworkRequest.h" #include "NetworkManager.h" +#include #include #include #include -#include namespace { @@ -17,16 +17,18 @@ namespace } return url; } -} +} // namespace void NetworkRequest::fetch(const QUrl& url) { reset(); + m_url = url; + QNetworkRequest request(url); // Set headers - for(const auto &[header, value] : qAsConst(m_headers)) { + for (const auto& [header, value] : qAsConst(m_headers)) { request.setRawHeader(header.toUtf8(), value.toUtf8()); } @@ -47,9 +49,9 @@ void NetworkRequest::fetchFinished() m_reply->deleteLater(); m_reply = nullptr; - if(error != QNetworkReply::NoError) { + if (error != QNetworkReply::NoError) { // Do not emit on abort. - if(error != QNetworkReply::OperationCanceledError) { + if (error != QNetworkReply::OperationCanceledError) { emit failure(); } return; @@ -74,7 +76,7 @@ void NetworkRequest::fetchFinished() // Parse content type auto tokens = contentTypeHeader.split(";", Qt::SkipEmptyParts); m_content_type = tokens[0].trimmed(); - for(int i = 1; i < tokens.size(); ++i) { + for (int i = 1; i < tokens.size(); ++i) { auto parameterTokens = tokens[i].split("="); m_content_type_parameters[parameterTokens[0]] = parameterTokens[1]; } @@ -99,7 +101,7 @@ void NetworkRequest::reset() void NetworkRequest::cancel() { - if(m_reply) { + if (m_reply) { m_reply->abort(); } } @@ -111,10 +113,7 @@ NetworkRequest::~NetworkRequest() QUrl NetworkRequest::url() const { - if(m_reply) { - return m_reply->url(); - } - return {}; + return m_url; } void NetworkRequest::setMaxRedirects(int maxRedirects) @@ -122,9 +121,15 @@ void NetworkRequest::setMaxRedirects(int maxRedirects) m_maxRedirects = std::max(0, maxRedirects); } -NetworkRequest::NetworkRequest(int maxRedirects, std::chrono::milliseconds timeoutDuration, - QList> headers, QNetworkAccessManager* manager) - : m_reply(nullptr), m_maxRedirects(maxRedirects), m_redirects(0), m_timeoutDuration(timeoutDuration), m_headers(headers) +NetworkRequest::NetworkRequest(int maxRedirects, + std::chrono::milliseconds timeoutDuration, + QList> headers, + QNetworkAccessManager* manager) + : m_reply(nullptr) + , m_maxRedirects(maxRedirects) + , m_redirects(0) + , m_timeoutDuration(timeoutDuration) + , m_headers(headers) { m_manager = manager ? manager : getNetMgr(); connect(&m_timeout, &QTimer::timeout, this, &NetworkRequest::fetchTimeout); @@ -156,9 +161,9 @@ NetworkRequest createRequest(int maxRedirects, QNetworkAccessManager* manager) { // Append user agent unless given - if(std::none_of(additionalHeaders.begin(), additionalHeaders.end(), [](const auto& pair) { - return pair.first == "User-Agent"; - })) { + if (std::none_of(additionalHeaders.begin(), additionalHeaders.end(), [](const auto& pair) { + return pair.first == "User-Agent"; + })) { additionalHeaders.append(QPair{"User-Agent", "KeePassXC"}); } return NetworkRequest(maxRedirects, timeoutDuration, additionalHeaders, manager); diff --git a/src/core/NetworkRequest.h b/src/core/NetworkRequest.h index 9249aafb0..020eaa8bc 100644 --- a/src/core/NetworkRequest.h +++ b/src/core/NetworkRequest.h @@ -17,11 +17,11 @@ #ifndef KEEPASSXC_NETWORKREQUEST_H #define KEEPASSXC_NETWORKREQUEST_H -#include -#include -#include #include +#include +#include #include +#include class QNetworkReply; class QNetworkAccessManager; @@ -32,67 +32,74 @@ class QNetworkAccessManager; * response can be obtained by calling the url(), ContentType(), and ContentTypeParameters() * methods. */ -class NetworkRequest : public QObject { - Q_OBJECT +class NetworkRequest : public QObject +{ + Q_OBJECT - QNetworkAccessManager* m_manager; - QNetworkReply* m_reply; - QByteArray m_bytes; + QNetworkAccessManager* m_manager; + QNetworkReply* m_reply; + QByteArray m_bytes; - // Response information - QString m_content_type; - QHash m_content_type_parameters; + // Response information + QString m_content_type; + QHash m_content_type_parameters; - // Request parameters - QTimer m_timeout; - int m_maxRedirects; - int m_redirects; - std::chrono::milliseconds m_timeoutDuration; - QList> m_headers; - public: - // TODO Disallow insecure connections by default? - explicit NetworkRequest(int maxRedirects, std::chrono::milliseconds timeoutDuration, - QList> headers, QNetworkAccessManager* manager = nullptr); - ~NetworkRequest() override; + // Request parameters + QTimer m_timeout; + int m_maxRedirects; + int m_redirects; + std::chrono::milliseconds m_timeoutDuration; + QList> m_headers; + QUrl m_url; - void setMaxRedirects(int maxRedirects); - void setTimeout(std::chrono::milliseconds timeoutDuration); +public: + // TODO Disallow insecure connections by default? + explicit NetworkRequest(int maxRedirects, + std::chrono::milliseconds timeoutDuration, + QList> headers, + QNetworkAccessManager* manager = nullptr); + ~NetworkRequest() override; - // TODO Should it be single shot vs multiple shot? - void fetch(const QUrl& url); - void cancel(); + void setMaxRedirects(int maxRedirects); + void setTimeout(std::chrono::milliseconds timeoutDuration); - QUrl url() const; - /** - * @return The MIME Type set in the Content-Type header. Empty string if Content-Type was not set. - */ - const QString& ContentType() const; - /** - * @return Any parameters set in the Content-Type header. - */ - const QHash& ContentTypeParameters() const; - private: - void reset(); - private slots: - void fetchFinished(); - void fetchReadyRead(); - void fetchTimeout(); - - signals: - void success(QByteArray); - void failure(); - }; + // TODO Should it be single shot vs multiple shot? + void fetch(const QUrl& url); + void cancel(); + QUrl url() const; /** - * Creates a NetworkRequest with default parameters. - * @param maxRedirects - * @param timeoutDuration - * @param headers - * @param manager - * @return + * @return The MIME Type set in the Content-Type header. Empty string if Content-Type was not set. */ - NetworkRequest createRequest(int maxRedirects = 5, - std::chrono::milliseconds timeoutDuration = std::chrono::milliseconds(5000), - QList> additionalHeaders = {}, QNetworkAccessManager* manager = nullptr); + const QString& ContentType() const; + /** + * @return Any parameters set in the Content-Type header. + */ + const QHash& ContentTypeParameters() const; + +private: + void reset(); +private slots: + void fetchFinished(); + void fetchReadyRead(); + void fetchTimeout(); + +signals: + void success(QByteArray); + void failure(); +}; + +/** + * Creates a NetworkRequest with default parameters. + * @param maxRedirects + * @param timeoutDuration + * @param headers + * @param manager + * @return + */ +NetworkRequest createRequest(int maxRedirects = 5, + std::chrono::milliseconds timeoutDuration = std::chrono::milliseconds(5000), + QList> additionalHeaders = {}, + QNetworkAccessManager* manager = nullptr); #endif // KEEPASSXC_NETWORKREQUEST_H