-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathhttpserver.cpp
More file actions
142 lines (118 loc) · 5.39 KB
/
httpserver.cpp
File metadata and controls
142 lines (118 loc) · 5.39 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
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
#include "httpserver.h"
HttpServer::HttpServer(QObject *parent) : SSLServer(parent)
{
QObject::connect(this, &HttpServer::connectionReady, this, &HttpServer::handleNewConnection);
}
void HttpServer::addRewriteRule(QString host, QString path, QDelegate<void(HttpServerRequest, HttpServerResponse)> delegate)
{
if(!this->rewriteRules.contains(host)) this->rewriteRules.insert(host, QMultiMap<QString, QDelegate<void(HttpServerRequest,HttpServerResponse)>>());
this->rewriteRules.find(host).value().insert(path, delegate);
}
void HttpServer::handleNewConnection()
{
QObject::connect(this->nextPendingConnection(), &QTcpSocket::readyRead, this, &HttpServer::handleNewData);
}
void HttpServer::handleNewData()
{
// get socket where data are available
QTcpSocket* socket = qobject_cast<QTcpSocket*>(this->sender());
if(!socket) return;
// parse result
HttpServerRequest request = this->pendingRequests.contains(socket) ?
this->pendingRequests.find(socket).value() :
this->pendingRequests.insert(socket, HttpServerRequest(new HttpServerRequestPrivate())).value();
if(!this->parseRequest(*socket, request)) return;
// remove pending request
this->pendingRequests.remove(socket);
// exit if we have no route for host
if(!this->rewriteRules.contains(request->host)) return;
// invoke routes
HttpServerResponse response(new HttpServerResponsePrivate);
auto& hostRoutes = this->rewriteRules.find(request->host).value();
for(auto itr = hostRoutes.begin(); itr != hostRoutes.end(); itr++) {
if(itr.key().startsWith(request->url)) itr.value().invoke(request, response);
}
// exit if user don't want to send a response back
if(!response->status) return;
// build response
QByteArray responseContent;
responseContent += (response->version.isEmpty() ? "HTTP/1.1" : response->version.toUtf8()) + " "; // Version
responseContent += QByteArray::number((qint32)response->status) + " "
+ response->StatusNames.value((qint32)response->status, "").toUtf8() + "\r\n"; // Status
// add headers
qint64 contentLength = 0;
for(auto itr = response->headers.begin(); itr != response->headers.end(); itr++) {
if(itr.key().toLower() == "content-length") contentLength = itr.value().toLongLong();
responseContent += itr.key().toUtf8() + ": " + itr.value().toUtf8() + "\r\n";
}
// add content length if not available
if(contentLength && !response->content.isEmpty()) responseContent += "Content-Length: " + QByteArray::number(contentLength) + "\r\n";
responseContent += "\r\n";
// add content
if(!response->content.isEmpty()) responseContent += response->content;
// send response
socket->write(responseContent);
}
bool HttpServer::parseRequest(QTcpSocket &device, HttpServerRequest& request)
{
// parse content
QByteArray content = device.readAll();
for(auto itr = content.begin(); itr != content.end(); itr++) {
// parse method
if(request->parseState == HttpServerRequestPrivate::Method) {
if(*itr == ' ') request->parseState = HttpServerRequestPrivate::Url;
else request->method += *itr;
}
// parse url
else if(request->parseState == HttpServerRequestPrivate::Url) {
if(*itr == ' ') request->parseState = HttpServerRequestPrivate::Version;
else request->url += *itr;
}
// parse version
else if(request->parseState == HttpServerRequestPrivate::Version) {
if(*itr == '\r') {
itr++;
request->parseState = HttpServerRequestPrivate::Headers;
}
else request->version += *itr;
}
// parse headers
else if(request->parseState == HttpServerRequestPrivate::Headers) {
// switch from key to value
if(*itr == ':') {
request->currentHeaderValue = &request->headers.insert(request->currentHeaderKey, QString("")).value();
itr++;
} else {
// end header
if(*itr == '\r') {
itr++;
if(*(itr + 1) == '\r') {
itr++;
itr++;
request->parseState = HttpServerRequestPrivate::Content;
}
request->currentHeaderKey = QString();
request->currentHeaderValue = 0;
}
// append to value
else if(request->currentHeaderValue) *request->currentHeaderValue += *itr;
// append to key
else request->currentHeaderKey += *itr;
}
}
// parse content
else if(request->parseState == HttpServerRequestPrivate::Content) {
if(request->contentLength == -1) request->contentLength = request->headers.value("Content-Length").toInt();
if(request->contentLength <= 1) request->parseState = HttpServerRequestPrivate::Done;
// append to content
if(request->contentLength) {
request->content += *itr;
request->contentLength--;
}
}
}
// demoralize data
request->host = request->headers.value("Host");
// if we are done return true, otherwise false
return request->parseState == HttpServerRequestPrivate::Done;
}