This repository has been archived by the owner on Feb 22, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.h
105 lines (87 loc) · 2.98 KB
/
http.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#ifndef HTTP_H
#define HTTP_H
#include <QtNetwork>
class HttpRequest {
public:
HttpRequest() : operation(QNetworkAccessManager::GetOperation), offset(0) {}
QUrl url;
QNetworkAccessManager::Operation operation;
QByteArray body;
uint offset;
QMap<QByteArray, QByteArray> headers;
};
class HttpReply : public QObject {
Q_OBJECT
public:
HttpReply(QObject *parent = nullptr) : QObject(parent) {}
virtual QUrl url() const = 0;
virtual int statusCode() const = 0;
int isSuccessful() const { return statusCode() >= 200 && statusCode() < 300; }
virtual QString reasonPhrase() const { return QString(); }
virtual const QList<QNetworkReply::RawHeaderPair> headers() const {
return QList<QNetworkReply::RawHeaderPair>();
}
virtual QByteArray header(const QByteArray &headerName) const {
Q_UNUSED(headerName);
return QByteArray();
}
virtual QByteArray body() const = 0;
signals:
void data(const QByteArray &bytes);
void error(const QString &message);
void finished(const HttpReply &reply);
};
class Http {
public:
static Http &instance();
static const QMap<QByteArray, QByteArray> &getDefaultRequestHeaders();
static void setDefaultReadTimeout(int timeout);
Http();
void setRequestHeaders(const QMap<QByteArray, QByteArray> &headers);
QMap<QByteArray, QByteArray> &getRequestHeaders();
void addRequestHeader(const QByteArray &name, const QByteArray &value);
void setReadTimeout(int timeout);
int getReadTimeout() { return readTimeout; }
QNetworkReply *networkReply(const HttpRequest &req);
virtual HttpReply *request(const HttpRequest &req);
HttpReply *
request(const QUrl &url,
QNetworkAccessManager::Operation operation = QNetworkAccessManager::GetOperation,
const QByteArray &body = QByteArray(),
uint offset = 0);
HttpReply *get(const QUrl &url);
HttpReply *head(const QUrl &url);
HttpReply *post(const QUrl &url, const QMap<QString, QString> ¶ms);
HttpReply *post(const QUrl &url, const QByteArray &body, const QByteArray &contentType);
private:
QMap<QByteArray, QByteArray> requestHeaders;
int readTimeout;
};
class NetworkHttpReply : public HttpReply {
Q_OBJECT
public:
NetworkHttpReply(const HttpRequest &req, Http &http);
QUrl url() const;
int statusCode() const;
QString reasonPhrase() const;
const QList<QNetworkReply::RawHeaderPair> headers() const;
QByteArray header(const QByteArray &headerName) const;
QByteArray body() const;
private slots:
void replyFinished();
void replyError(QNetworkReply::NetworkError);
void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
void readTimeout();
private:
void setupReply();
QString errorMessage();
void emitError();
void emitFinished();
Http &http;
HttpRequest req;
QNetworkReply *networkReply;
QTimer *readTimeoutTimer;
int retryCount;
QByteArray bytes;
};
#endif // HTTP_H