-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathsimplehttpserver.cpp
More file actions
574 lines (482 loc) · 12 KB
/
simplehttpserver.cpp
File metadata and controls
574 lines (482 loc) · 12 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
/*
* Copyright (C) 2015-2022 Fanout, Inc.
*
* This file is part of Pushpin.
*
* $FANOUT_BEGIN_LICENSE:APACHE2$
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* $FANOUT_END_LICENSE$
*/
#include "simplehttpserver.h"
#include <assert.h>
#include <QFile>
#include <QFileInfo>
#include <QTcpSocket>
#include <QTcpServer>
#include <QLocalSocket>
#include <QLocalServer>
#include "log.h"
#include "defercall.h"
#include "httpheaders.h"
class SimpleHttpRequest::Private : public QObject
{
Q_OBJECT
public:
enum State
{
ReadHeader,
ReadBody,
WriteBody,
WaitForWritten,
Closing
};
SimpleHttpRequest *q;
QIODevice *sock;
State state;
QByteArray inBuf;
bool version1dot0;
QString method;
QByteArray uri;
HttpHeaders reqHeaders;
QByteArray reqBody;
int contentLength;
int pendingWritten;
int maxHeadersSize;
int maxBodySize;
Private(SimpleHttpRequest *_q, int maxHeadersSize, int maxBodySize) :
QObject(_q),
q(_q),
sock(0),
state(ReadHeader),
version1dot0(false),
contentLength(0),
pendingWritten(0),
maxHeadersSize(maxHeadersSize),
maxBodySize(maxBodySize)
{
}
~Private()
{
cleanup();
}
void cleanup()
{
if(sock)
{
sock->disconnect(this);
sock->setParent(0);
DeferCall::deleteLater(sock);
sock = 0;
}
}
void start(QTcpSocket *_sock)
{
QObject::connect(_sock, &QTcpSocket::readyRead, [this]() {
this->sock_readyRead();
});
QObject::connect(_sock, &QTcpSocket::bytesWritten, this, [this](qint64 bytes) {
this->sock_bytesWritten(bytes);
});
QObject::connect(_sock, &QTcpSocket::disconnected, [this]() {
this->sock_disconnected();
});
sock = _sock;
sock->setParent(this);
processIn();
}
void start(QLocalSocket *_sock)
{
QObject::connect(_sock, &QLocalSocket::readyRead, [this]() {
this->sock_readyRead();
});
QObject::connect(_sock, &QLocalSocket::bytesWritten, this, [this](qint64 bytes) {
this->sock_bytesWritten(bytes);
});
QObject::connect(_sock, &QLocalSocket::disconnected, [this]() {
this->sock_disconnected();
});
sock = _sock;
sock->setParent(this);
processIn();
}
void respond(int code, const QByteArray &reason, const HttpHeaders &headers, const QByteArray &body)
{
if(state != WriteBody)
return;
HttpHeaders outHeaders = headers;
outHeaders.removeAll("Connection");
outHeaders.removeAll("Transfer-Encoding");
outHeaders.removeAll("Content-Length");
outHeaders += HttpHeader("Connection", "close");
outHeaders += HttpHeader("Content-Length", QByteArray::number(body.size()));
QByteArray respData = "HTTP/";
if(version1dot0)
respData += "1.0 ";
else
respData += "1.1 ";
respData += QByteArray::number(code) + " " + reason + "\r\n";
foreach(const HttpHeader &h, outHeaders)
respData += h.first + ": " + h.second + "\r\n";
respData += "\r\n";
respData += body;
state = WaitForWritten;
pendingWritten += respData.size();
sock->write(respData);
}
void respond(int code, const QByteArray &reason, const QString &body)
{
HttpHeaders headers;
headers += HttpHeader("Content-Type", "text/plain");
respond(code, reason, headers, body.toUtf8());
}
Signal ready;
private:
void respondError(int code, const QByteArray &reason, const QString &body)
{
state = WriteBody;
respond(code, reason, body + '\n');
}
void respondBadRequest(const QString &body)
{
respondError(400, "Bad Request", body);
}
void respondLengthRequired(const QString &body)
{
respondError(411, "Length Required", body);
}
bool processHeaderData(const QByteArray &headerData)
{
QList<QByteArray> lines;
int at = 0;
while(at < headerData.size())
{
int end = headerData.indexOf("\n", at);
assert(end != -1);
if(end > at && headerData[end - 1] == '\r')
lines += headerData.mid(at, end - at - 1);
else
lines += headerData.mid(at, end - at);
at = end + 1;
}
if(lines.isEmpty())
return false;
QByteArray requestLine = lines[0];
at = requestLine.indexOf(' ');
if(at == -1)
return false;
method = QString::fromLatin1(requestLine.mid(0, at));
if(method.isEmpty())
return false;
++at;
int end = requestLine.indexOf(' ', at);
if(end == -1)
return false;
uri = requestLine.mid(at, end - at);
QByteArray versionStr = requestLine.mid(end + 1);
if(versionStr == "HTTP/1.0")
version1dot0 = true;
for(int n = 1; n < lines.count(); ++n)
{
const QByteArray &line = lines[n];
end = line.indexOf(':');
if(end == -1)
continue;
// skip first space
at = end + 1;
if(at < line.length() && line[at] == ' ')
++at;
QByteArray name = line.mid(0, end);
QByteArray val = line.mid(at);
reqHeaders += HttpHeader(name, val);
}
//log_debug("httpserver: IN method=[%s] uri=[%s] 1.1=%s", qPrintable(method), uri.data(), version1dot0 ? "no" : "yes");
//foreach(const HttpHeader &h, reqHeaders)
// log_debug("httpserver: [%s] [%s]", h.first.data(), h.second.data());
log_debug("httpserver: IN %s %s", qPrintable(method), uri.data());
return true;
}
void processIn()
{
if(state == ReadHeader)
{
inBuf += sock->read(maxHeadersSize - inBuf.size());
// look for double newline
int at = -1;
int next = 0;
for(int n = 0; n < inBuf.size(); ++n)
{
if(n + 1 < inBuf.size() && qstrncmp(inBuf.data() + n, "\n\n", 2) == 0)
{
at = n + 1;
next = n + 2;
break;
}
else if(n + 2 < inBuf.size() && qstrncmp(inBuf.data() + n, "\n\r\n", 3) == 0)
{
at = n + 1;
next = n + 3;
break;
}
}
if(at != -1)
{
QByteArray headerData = inBuf.mid(0, at);
reqBody = inBuf.mid(next);
inBuf.clear();
if(!processHeaderData(headerData))
{
respondBadRequest("Failed to parse request header.");
return;
}
bool methodAssumesBody = (method != "HEAD" && method != "GET" && method != "DELETE" && method != "OPTIONS");
if(!reqHeaders.contains("Content-Length") && (reqHeaders.contains("Transfer-Encoding") || methodAssumesBody))
{
respondLengthRequired("Request requires Content-Length.");
return;
}
if(reqHeaders.contains("Content-Length"))
{
bool ok;
contentLength = reqHeaders.get("Content-Length").toInt(&ok);
if(!ok)
{
respondBadRequest("Bad Content-Length.");
return;
}
if(contentLength > maxBodySize)
{
respondBadRequest("Request body too large.");
return;
}
if(reqHeaders.get("Expect") == "100-continue")
{
QByteArray respData = "HTTP/";
if(version1dot0)
respData += "1.0 ";
else
respData += "1.1 ";
respData += "100 Continue\r\n\r\n";
pendingWritten += respData.size();
sock->write(respData);
}
state = ReadBody;
processIn();
}
else
{
state = WriteBody;
ready();
}
}
else if(inBuf.size() >= maxHeadersSize)
{
inBuf.clear();
respondBadRequest("Request header too large.");
return;
}
}
else if(state == ReadBody)
{
reqBody += sock->read(maxBodySize - reqBody.size() + 1);
if(reqBody.size() > contentLength)
{
respondBadRequest("Request body exceeded Content-Length.");
return;
}
if(reqBody.size() == contentLength)
{
state = WriteBody;
ready();
}
}
}
void sock_readyRead()
{
if(state == ReadHeader || state == ReadBody)
processIn();
}
void sock_bytesWritten(qint64 bytes)
{
pendingWritten -= (int)bytes;
assert(pendingWritten >= 0);
if(state != WaitForWritten)
return;
if(pendingWritten == 0)
{
state = Closing;
sock->close();
}
}
void sock_disconnected()
{
cleanup();
q->finished();
}
};
SimpleHttpRequest::SimpleHttpRequest(int maxHeadersSize, int maxBodySize,QObject *parent) :
QObject(parent)
{
d = new Private(this, maxHeadersSize, maxBodySize);
}
SimpleHttpRequest::~SimpleHttpRequest()
{
delete d;
}
QString SimpleHttpRequest::requestMethod() const
{
return d->method;
}
QByteArray SimpleHttpRequest::requestUri() const
{
return d->uri;
}
HttpHeaders SimpleHttpRequest::requestHeaders() const
{
return d->reqHeaders;
}
QByteArray SimpleHttpRequest::requestBody() const
{
return d->reqBody;
}
void SimpleHttpRequest::respond(int code, const QByteArray &reason, const HttpHeaders &headers, const QByteArray &body)
{
d->respond(code, reason, headers, body);
}
void SimpleHttpRequest::respond(int code, const QByteArray &reason, const QString &body)
{
d->respond(code, reason, body);
}
class SimpleHttpServerPrivate : public QObject
{
Q_OBJECT
public:
SimpleHttpServer *q;
void *server;
bool local;
QSet<SimpleHttpRequest*> accepting;
QList<SimpleHttpRequest*> pending;
int maxHeadersSize;
int maxBodySize;
map<SimpleHttpRequest*, Connection> finishedConnections;
map<SimpleHttpRequest*, Connection> readyConnections;
SimpleHttpServerPrivate(int maxHeadersSize, int maxBodySize, SimpleHttpServer *_q) :
QObject(_q),
q(_q),
server(0),
local(false),
maxHeadersSize(maxHeadersSize),
maxBodySize(maxBodySize)
{
}
~SimpleHttpServerPrivate()
{
qDeleteAll(pending);
qDeleteAll(accepting);
}
bool listen(const QHostAddress &addr, int port)
{
assert(!server);
QTcpServer *s = new QTcpServer(this);
connect(s, &QTcpServer::newConnection, this, &SimpleHttpServerPrivate::server_newConnection);
if(!s->listen(addr, port))
{
delete s;
return false;
}
server = s;
local = false;
return true;
}
bool listenLocal(const QString &name)
{
assert(!server);
QFileInfo fi(name);
QString filePath = fi.absoluteFilePath();
QFile::remove(filePath);
QLocalServer *s = new QLocalServer(this);
connect(s, &QLocalServer::newConnection, this, &SimpleHttpServerPrivate::server_newConnection);
if(!s->listen(filePath))
{
delete s;
return false;
}
server = s;
local = true;
return true;
}
private:
void server_newConnection()
{
if(local)
{
QLocalSocket *sock = ((QLocalServer *)server)->nextPendingConnection();
SimpleHttpRequest *req = new SimpleHttpRequest(maxHeadersSize, maxBodySize);
readyConnections[req] = req->d->ready.connect(boost::bind(&SimpleHttpServerPrivate::req_ready, this, req->d->q));
finishedConnections[req] = req->finished.connect(boost::bind(&SimpleHttpServerPrivate::req_finished, this, req));
accepting += req;
req->d->start(sock);
}
else
{
QTcpSocket *sock = ((QTcpServer *)server)->nextPendingConnection();
SimpleHttpRequest *req = new SimpleHttpRequest(maxHeadersSize, maxBodySize);
readyConnections[req] = req->d->ready.connect(boost::bind(&SimpleHttpServerPrivate::req_ready, this, req->d->q));
finishedConnections[req] = req->finished.connect(boost::bind(&SimpleHttpServerPrivate::req_finished, this, req));
accepting += req;
req->d->start(sock);
}
}
void req_ready(SimpleHttpRequest *req)
{
accepting.remove(req);
pending += req;
q->requestReady();
}
void req_finished(SimpleHttpRequest *req)
{
accepting.remove(req);
pending.removeAll(req);
delete req;
}
};
SimpleHttpServer::SimpleHttpServer(int maxHeadersSize, int maxBodySize, QObject *parent) :
QObject(parent)
{
d = new SimpleHttpServerPrivate(maxHeadersSize, maxBodySize, this);
}
SimpleHttpServer::~SimpleHttpServer()
{
delete d;
}
bool SimpleHttpServer::listen(const QHostAddress &addr, int port)
{
return d->listen(addr, port);
}
bool SimpleHttpServer::listenLocal(const QString &name)
{
return d->listenLocal(name);
}
SimpleHttpRequest *SimpleHttpServer::takeNext()
{
if(!d->pending.isEmpty())
{
SimpleHttpRequest *req = d->pending.takeFirst();
d->finishedConnections.erase(req);
return req;
}
else
return 0;
}
#include "simplehttpserver.moc"