-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlshttpd.h
204 lines (168 loc) · 5.77 KB
/
lshttpd.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#ifndef LSHTTPD_H
#define LSHTTPD_H
#include "lshttpd_global.h"
#include <QObject>
#include <QTcpServer>
#include <QHostAddress>
#include <QTcpSocket>
#include <QSsl>
#include <QSslKey>
#include <QSslCertificate>
#include <QByteArray>
#include <QPair>
#include <QSslSocket>
#include <QRegularExpression>
class LSHttpdPrivate;
class LSHttpdRequestPrivate;
class LSHttpdResource;
typedef QPair<QString,QString> LSHttpdHeaderPair;
class LSHTTPDSHARED_EXPORT LSHttpd : public QObject
{
Q_OBJECT
public:
LSHttpd(QHostAddress address=QHostAddress::Any, quint16 port=80, bool useSSL=false);
void setCertificate(const QString &path, QSsl::EncodingFormat format = QSsl::Pem);
void setCertificate(const QSslCertificate & certificate);
void setPrivateKey(const QString &path, QSsl::KeyAlgorithm keyAlgorithm = QSsl::Rsa, QSsl::EncodingFormat format = QSsl::Pem, const QByteArray & passPhrase = QByteArray());
void setPrivateKey(const QSslKey &key);
QSharedPointer<LSHttpdResource> registerFallback(); //Custom handling of 404 etc.
void unregisterFallback();
QSharedPointer<LSHttpdResource> registerResource(QRegularExpression rx); //rx match for relative Url
void unregisterResource(QSharedPointer<LSHttpdResource> resource);
signals:
protected:
LSHttpd(LSHttpdPrivate &d);
LSHttpdPrivate *d_ptr;
};
class LSHTTPDSHARED_EXPORT LSHttpdRequest : public QObject
{
Q_OBJECT
public:
enum ResponseCode : int {
OK = 200, //Ok Response Codes 2xx
Created = 201,
Accepted = 202,
NoContent = 204,
Redirection = 301, //Redirect / Modification 3xx
NotModified = 304,
RedirectionTemporarely = 307,
AuthRequired = 401,
Forbidden = 403,
NotFound = 404, //Request Error Responses 4xx
MethodNotAllowed = 405,
Gone = 410,
ContentLengthRequired = 411,
PreconditionFailed = 412,
NotImplemented = 501, //Server Error Response 5xx
BadGateway = 502,
ServerError = 503,
GatewayTimeout = 504
};
#if (QT_VERSION < QT_VERSION_CHECK(5,5,0))
Q_ENUMS(LSHttpdRequest::ResponseCode)
#else
Q_ENUM(LSHttpdRequest::ResponseCode)
#endif
enum RequestMethod : int {
GET = 0,
HEAD,
POST,
PUT,
DELETE,
OTHER
};
#if (QT_VERSION < QT_VERSION_CHECK(5,5,0))
Q_ENUMS(LSHttpdRequest::RequestMethod)
#else
Q_ENUM(LSHttpdRequest::RequestMethod)
#endif
private:
QString m_resource;
QString m_method;
RequestMethod m_methodId;
int m_responseCode;
QList<LSHttpdHeaderPair> m_requestHeaderList;
QList<LSHttpdHeaderPair> m_responseHeaderList;
QByteArray m_requestBodyData;
QByteArray m_responseBodyData;
LSHttpdRequestPrivate *d_ptr;
friend class LSHttpdPrivate;
friend class LSHttpdRequestPrivate;
LSHttpdRequest(QTcpSocket* socket, QObject *parent=0);
void closeRequest();
signals:
void requestFinished();
public:
~LSHttpdRequest();
//Requested Url
QString resource() const;
QString method() const;
QString method(LSHttpdRequest::RequestMethod key) const;
RequestMethod methodId() const;
//RemoteHost
QHostAddress remoteHost() const;
//Status Code Response
int responseCode() const;
//Headers
QList<LSHttpdHeaderPair> requestHeaderList() const;
QList<LSHttpdHeaderPair> responseHeaderList() const;
//Body
QByteArray requestBodyData() const;
QByteArray responseBodyData() const;
QByteArray requestRaw();
QByteArray responseRaw();
void createResponse(int in_status, QList<LSHttpdHeaderPair> in_headerList, QByteArray in_bodyData);
bool validateResponse(QByteArray outData);
bool validateResponse();
bool sendResponse();
// Code Responses
bool responseDefault(int responseCode);
void response200();
void response204();
void response301(QByteArray redirectLocation);
void response302(QByteArray redirectLocation);
void response303(QByteArray redirectLocation);
void response304(QDateTime modificationDate);
void response307(QByteArray redirectLocation);
void response400();
void response401Basic(QByteArray realm);
void response401Digest(QByteArray realm, QByteArray nonce);
void response403();
void response404();
void response405(QStringList allowedMethods);
void response410();
void response411();
void response412();
void response500();
void response501();
void response502();
void response503();
void response504();
// Named Responses proxy to Code Responses
void responseOk();
void responseNoContent();
void responseMovedPermanently(QByteArray redirectLocation);
void responseFound(QByteArray redirectLocation);
void responseSeeOther(QByteArray redirectLocation);
void responseNotModified(QDateTime modificationDate);
void responseTemporaryRedirect(QByteArray redirectLocation);
void responseBadRequest();
void responseBasicAuth(QByteArray realm);
void responseDigetsAuth(QByteArray realm, QByteArray nonce);
void responseForbidden();
void responseNotFound();
void responseMethodNotAllowed(QStringList allowedMethods);
void responseGone();
void responseLengthRequired();
void responsePreconditionFailed();
void responseServerError();
void responseNotImplemented();
void responseBadGateway();
void responseServiceUnavailable();
void responseGateWayTimeout();
QByteArray extractOption(QByteArray headerValue, QByteArray optionTag);
QByteArray extractUser(QByteArray headerValue);
QByteArray extractDigest(QByteArray headerValue);
QByteArray calculateDigestMD5(QString user, QString password, QByteArray realm, QByteArray nonce);
};
#endif // LSHTTPD_H