-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathHttpRequestHandler.h
More file actions
80 lines (66 loc) · 2.36 KB
/
HttpRequestHandler.h
File metadata and controls
80 lines (66 loc) · 2.36 KB
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
#ifndef HTTPREQUESTHANDLER_H
#define HTTPREQUESTHANDLER_H
#include "cppCORE_global.h"
#include <QObject>
#include <QString>
#include <QSslError>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QHttpMultiPart>
using HttpHeaders = QMap<QByteArray, QByteArray>;
struct CPPCORESHARED_EXPORT RequestUrlParams
: public QMap<QByteArray, QByteArray>
{
QString asString()
{
QString output;
QMap<QByteArray, QByteArray>::iterator i;
for (i = begin(); i != end(); ++i)
{
if (!output.isEmpty()) output += "&";
output += i.key() + "=" + i.value();
}
return "?" + output;
}
};
struct CPPCORESHARED_EXPORT ServerReply
{
int status_code = 200;
QMap<QByteArray, QByteArray> headers;
QByteArray body;
};
///Helper class for HTTP(S) communication with webserver
class CPPCORESHARED_EXPORT HttpRequestHandler
: public QObject
{
Q_OBJECT
public:
///Constructor
HttpRequestHandler(QNetworkProxy proxy, QObject* parent=0);
///Returns basic headers used for all get/post requests. Additional headers that are only used for one request can be given in the get/post methods.
const HttpHeaders& headers() const;
///Adds/overrides a basic header.
void setHeader(const QByteArray& key, const QByteArray& value);
///Returns headers for a specific file (needed to get the size of a file without fetching its content)
ServerReply head(QString url, const HttpHeaders& add_headers);
///Performs GET request
ServerReply get(QString url, const HttpHeaders& add_headers = HttpHeaders());
///Performs PUT request (modifies existing resources)
ServerReply put(QString url, const QByteArray& data, const HttpHeaders& add_headers = HttpHeaders());
///Performs POST request
ServerReply post(QString url, const QByteArray& data, const HttpHeaders& add_headers = HttpHeaders());
///Performs POST request for content type multipart
ServerReply post(QString url, QHttpMultiPart* parts, const HttpHeaders& add_headers = HttpHeaders());
HttpRequestHandler() = delete;
signals:
void proxyAuthenticationRequired(const QNetworkProxy& , QAuthenticator*);
private slots:
//Handles SSL errors (by ignoring them)
void handleSslErrors(QNetworkReply*, const QList<QSslError>&);
private:
QString networkErrorAsString(QNetworkReply::NetworkError error);
QNetworkAccessManager nmgr_;
HttpHeaders headers_;
//declared away
};
#endif // HTTPREQUESTHANDLER_H