Save URL in NetworkRequest.

This commit is contained in:
Patrick Sean Klein 2023-05-09 22:59:40 +01:00
parent 7de7c8bcb3
commit b4249ea941
No known key found for this signature in database
GPG Key ID: B6D50F39A56F6906
2 changed files with 85 additions and 73 deletions

View File

@ -1,10 +1,10 @@
#include "NetworkRequest.h"
#include "NetworkManager.h"
#include <QString>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QtNetwork/QNetworkRequest>
#include <QString>
namespace
{
@ -17,12 +17,14 @@ namespace
}
return url;
}
}
} // namespace
void NetworkRequest::fetch(const QUrl& url)
{
reset();
m_url = url;
QNetworkRequest request(url);
// Set headers
@ -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<QPair<QString, QString>> 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<QPair<QString, QString>> 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);

View File

@ -17,11 +17,11 @@
#ifndef KEEPASSXC_NETWORKREQUEST_H
#define KEEPASSXC_NETWORKREQUEST_H
#include <QObject>
#include <QList>
#include <QUrl>
#include <QHash>
#include <QList>
#include <QObject>
#include <QTimer>
#include <QUrl>
class QNetworkReply;
class QNetworkAccessManager;
@ -32,7 +32,8 @@ class QNetworkAccessManager;
* response can be obtained by calling the url(), ContentType(), and ContentTypeParameters()
* methods.
*/
class NetworkRequest : public QObject {
class NetworkRequest : public QObject
{
Q_OBJECT
QNetworkAccessManager* m_manager;
@ -49,10 +50,14 @@ class NetworkRequest : public QObject {
int m_redirects;
std::chrono::milliseconds m_timeoutDuration;
QList<QPair<QString, QString>> m_headers;
QUrl m_url;
public:
// TODO Disallow insecure connections by default?
explicit NetworkRequest(int maxRedirects, std::chrono::milliseconds timeoutDuration,
QList<QPair<QString, QString>> headers, QNetworkAccessManager* manager = nullptr);
explicit NetworkRequest(int maxRedirects,
std::chrono::milliseconds timeoutDuration,
QList<QPair<QString, QString>> headers,
QNetworkAccessManager* manager = nullptr);
~NetworkRequest() override;
void setMaxRedirects(int maxRedirects);
@ -71,6 +76,7 @@ class NetworkRequest : public QObject {
* @return Any parameters set in the Content-Type header.
*/
const QHash<QString, QString>& ContentTypeParameters() const;
private:
void reset();
private slots:
@ -93,6 +99,7 @@ class NetworkRequest : public QObject {
*/
NetworkRequest createRequest(int maxRedirects = 5,
std::chrono::milliseconds timeoutDuration = std::chrono::milliseconds(5000),
QList<QPair<QString, QString>> additionalHeaders = {}, QNetworkAccessManager* manager = nullptr);
QList<QPair<QString, QString>> additionalHeaders = {},
QNetworkAccessManager* manager = nullptr);
#endif // KEEPASSXC_NETWORKREQUEST_H