3#include <Kanoop/utility/abstractthreadclass.h>
4#include <Kanoop/http/httpheaders.h>
6#include <QNetworkCookie>
7#include <QNetworkReply>
8#include <Kanoop/kanoopprotocol.h>
10class QNetworkAccessManager;
12class LIBKANOOPPROTOCOL_EXPORT
HttpOperation :
public AbstractThreadClass
18 UnknownMethod = 0x0000,
25 MultipartUpload = 0x1000,
40 QString
url()
const {
return _url; }
53 void appendHeader(QNetworkRequest::KnownHeaders type,
const QByteArray& value) { _headers.insert(type, value); }
58 void appendHeader(
const QString& headerName,
const QByteArray& value) { _customHeaders.insert(headerName, value); }
100 QNetworkReply::NetworkError
networkError()
const {
return _networkError; }
157 void setUrl(
const QString& value) { _url = value; }
179 virtual void threadStarted()
override;
186 QList<QNetworkCookie> _requestCookies;
189 QNetworkReply::NetworkError _networkError = QNetworkReply::UnknownServerError;
190 QString _reasonPhrase;
191 QByteArray _responseBody;
192 QList<QNetworkCookie> _responseCookies;
194 QList<QSslError::SslError> _ignoreSslErrors;
196 bool _verifyPeer =
true;
198 QDateTime _operationStartTime;
199 TimeSpan _transferTimeout = TimeSpan::fromMilliseconds(QNetworkRequest::DefaultTransferTimeoutConstant);
202 QNetworkAccessManager* _networkAccessManager =
nullptr;
203 QNetworkReply* _reply =
nullptr;
205 class RequestMethodToStringMap :
public KANOOP::EnumToStringMap<RequestMethod>
208 RequestMethodToStringMap()
210 insert(UnknownMethod,
"UnknownMethod");
213 insert(Delete,
"DELETE");
214 insert(Post,
"POST");
215 insert(Head,
"HEAD");
216 insert(Patch,
"PATCH");
217 insert(MultipartUpload,
"UPLOAD");
221 static const RequestMethodToStringMap _RequestMethodToStringMap;
222 static const QList<QSslError::SslError> SelfSignedCertificateErrors;
A simple key-value pair used for HTTP parameters.
KeyValuePair()
Construct an empty key-value pair.
KeyValuePair(const QString &key, const QString &value)
Construct a key-value pair with the given key and value.
Base class for HTTP operations executed asynchronously on a dedicated thread.
TimeSpan transferTimeout() const
Return the transfer timeout duration.
QNetworkAccessManager * networkAccessManager()
Return the shared network access manager, creating it if necessary.
HttpOperation(const QString &url, RequestMethod method)
Construct an HTTP operation for the given URL and request method.
void setRequestCookies(const QList< QNetworkCookie > &value)
Set the cookies to be sent with the request.
void appendHeader(QNetworkRequest::KnownHeaders type, const QByteArray &value)
Append a known HTTP header to the request.
QList< QNetworkCookie > requestCookies() const
Return the cookies to be sent with the request.
QString getRequestMethodString() const
Return a human-readable string for this operation's request method.
void appendHeadersToRequest(QNetworkRequest *request)
Append all configured headers to the given network request.
virtual void threadFinished() override
Called when the operation thread finishes.
void operationComplete()
Emitted when the HTTP operation has completed.
void setUrl(const QString &value)
Set the target URL of this operation.
static QString getRequestMethodString(RequestMethod method)
Return a human-readable string for the given request method.
QNetworkReply::NetworkError networkError() const
Return the network error from the completed operation.
void abortOperation()
Abort the running HTTP operation.
HttpKnownHeaders headers() const
Return the known HTTP headers set on this operation.
void appendHeader(const QString &headerName, const QByteArray &value)
Append a custom HTTP header to the request.
void setReply(QNetworkReply *reply)
Store the network reply and connect its signals.
virtual void postReplyHook(QNetworkReply *)
Hook called after the network reply is received.
HttpCustomHeaders customHeaders() const
Return the custom HTTP headers set on this operation.
int statusCode() const
Return the HTTP status code from the response.
void onSslErrors(const QList< QSslError > &errors)
Handle SSL errors from the network reply.
QByteArray responseBody() const
Return the response body data.
virtual void execute()=0
Execute the HTTP operation (implemented by subclasses).
void configureSsl(QNetworkRequest *request)
Configure SSL settings on the given network request.
virtual ~HttpOperation()
Destructor.
bool isVerifyPeer() const
Return whether peer SSL certificate verification is enabled.
QString url() const
Return the target URL of this operation.
bool isHttps() const
Return whether this operation uses HTTPS.
void ignoreSelfSignedCertificate()
Configure the operation to ignore self-signed certificate errors.
RequestMethod
Enumeration of supported HTTP request methods.
QString reasonPhrase() const
Return the HTTP reason phrase from the response.
void setTransferTimeout(const TimeSpan &value)
Set the transfer timeout duration.
void onReplyError(QNetworkReply::NetworkError error)
Handle a network error from the reply.
bool isSelfSignedCertificateErrorIgnored() const
Return whether self-signed certificate errors are being ignored.
void setVerifyPeer(bool value)
Enable or disable peer SSL certificate verification.
TimeSpan duration() const
Return the wall-clock duration of the HTTP operation.
void onReplyFinished()
Handle the network reply finished signal.
QList< QNetworkCookie > responseCookies() const
Return the cookies received in the response.