forked from citizenfx/fivem
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHttp1Server.cpp
More file actions
658 lines (515 loc) · 15.6 KB
/
Http1Server.cpp
File metadata and controls
658 lines (515 loc) · 15.6 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
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/*
* This file is part of the CitizenFX project - http://citizen.re/
*
* See LICENSE and MENTIONS in the root of the source tree for information
* regarding licensing.
*/
#include "StdInc.h"
#include "HttpServer.h"
#include "HttpServerImpl.h"
#include <picohttpparser.h>
#include <ctime>
#include <deque>
#include <iomanip>
#include <sstream>
#include <string_view>
#include <chrono>
namespace net
{
class Http1Response : public HttpResponse
{
private:
fwRefContainer<TcpServerStream> m_clientStream;
std::shared_ptr<HttpState> m_requestState;
bool m_chunked;
bool m_sentWriteHead;
bool m_disableKeepAlive;
public:
inline Http1Response(fwRefContainer<TcpServerStream> clientStream, fwRefContainer<HttpRequest> request, const std::shared_ptr<HttpState>& reqState, bool disableKeepAlive)
: HttpResponse(request), m_requestState(reqState), m_clientStream(clientStream), m_chunked(false), m_sentWriteHead(false), m_disableKeepAlive(disableKeepAlive)
{
}
void StartConnectionTimeout(std::chrono::duration<uint64_t, std::milli> timeout) override
{
m_clientStream->StartConnectionTimeout(timeout);
}
void WriteDateHeader(std::ostringstream& outData)
{
auto now = std::chrono::system_clock::now();
auto time = std::chrono::system_clock::to_time_t(now);
std::tm timeInfo;
#ifdef WIN32
if (gmtime_s(&timeInfo, &time) == 0)
#else
if (gmtime_r(&time, &timeInfo) != nullptr)
#endif
{
outData << "Date: " << std::put_time(&timeInfo, "%a, %d %b %Y %H:%M:%S GMT") << "\r\n";
}
}
virtual void WriteHead(int statusCode, const std::string& statusMessage, const HeaderMap& headers) override
{
if (m_sentHeaders)
{
return;
}
BeforeWriteHead({});
std::ostringstream outData;
outData.imbue(std::locale());
outData << "HTTP/1.1 " << std::to_string(statusCode) << " " << (statusMessage.empty() ? GetStatusMessage(statusCode) : statusMessage) << "\r\n";
auto usedHeaders = (headers.size() == 0) ? m_headerList : headers;
if (usedHeaders.find("date") == usedHeaders.end())
{
WriteDateHeader(outData);
}
auto requestConnection = m_request->GetHeader("connection", "keep-alive");
if (_strnicmp(requestConnection.data(), "keep-alive", requestConnection.size()) != 0 || m_disableKeepAlive)
{
outData << "Connection: close\r\n";
m_closeConnection = true;
}
else
{
outData << "Connection: keep-alive\r\n";
}
for (auto& header : usedHeaders)
{
outData << std::string_view{ header.first.data(), header.first.size() } << ": "
<< std::string_view{ header.second.data(), header.second.size() } << "\r\n";
}
outData << "\r\n";
std::string outStr = outData.str();
m_clientStream->Write(std::move(outStr));
m_sentHeaders = true;
}
virtual void BeforeWriteHead(size_t length) override
{
// only execute WriteHead filtering once
if (m_sentWriteHead)
{
return;
}
m_sentWriteHead = true;
// HACK: disallow chunking for ROS requests
if (m_request->GetHttpVersion() == std::make_pair(1, 0) ||
m_request->GetHeader("host").find("rockstargames.com") != std::string::npos ||
GetHeader("transfer-encoding").find("identity") != std::string::npos)
{
if (m_headerList.find("content-length") == m_headerList.end())
{
SetHeader("Content-Length", HeaderString{ std::to_string(length).c_str() });
}
// unset transfer-encoding, if present
m_headerList.erase("transfer-encoding");
}
else
{
SetHeader("Transfer-Encoding", "chunked");
// if client code set Content-Length, unset it.
//
// setting both Transfer-Encoding: chunked and Content-Length is considered ill-formed.
m_headerList.erase("content-length");
m_chunked = true;
}
}
private:
template<typename TContainer>
void WriteOutInternal(TContainer data, fu2::unique_function<void(bool)>&& cb = {})
{
size_t length = data.size();
if (m_chunked)
{
// we _don't_ want to send a 0-sized chunk
if (length > 0)
{
// assume chunked
m_clientStream->Write(fmt::sprintf("%x\r\n", length));
m_clientStream->Write(std::forward<TContainer>(data), std::move(cb));
m_clientStream->Write("\r\n");
}
}
else
{
m_clientStream->Write(std::forward<TContainer>(data), std::move(cb));
}
}
public:
virtual void WriteOut(const std::vector<uint8_t>& data, fu2::unique_function<void(bool)>&& onComplete = {}) override
{
WriteOutInternal<decltype(data)>(data, std::move(onComplete));
}
virtual void WriteOut(std::vector<uint8_t>&& data, fu2::unique_function<void(bool)>&& onComplete = {}) override
{
WriteOutInternal<decltype(data)>(std::move(data), std::move(onComplete));
}
virtual void WriteOut(const std::string& data, fu2::unique_function<void(bool)>&& onComplete = {}) override
{
WriteOutInternal<decltype(data)>(data, std::move(onComplete));
}
virtual void WriteOut(std::string&& data, fu2::unique_function<void(bool)>&& onComplete = {}) override
{
WriteOutInternal<decltype(data)>(std::move(data), std::move(onComplete));
}
virtual void WriteOut(std::unique_ptr<char[]> data, size_t length, fu2::unique_function<void(bool)>&& cb = {}) override
{
if (m_chunked)
{
// we _don't_ want to send a 0-sized chunk
if (length > 0)
{
// assume chunked
m_clientStream->Write(fmt::sprintf("%x\r\n", length));
m_clientStream->Write(std::move(data), length, std::move(cb));
m_clientStream->Write("\r\n");
}
}
else
{
m_clientStream->Write(std::move(data), length, std::move(cb));
}
}
virtual void End() override
{
HttpResponse::End();
bool skipClose = false;
fwRefContainer thisRef = this;
auto doClose = [thisRef]()
{
if (thisRef->m_closeConnection)
{
auto clientStream = thisRef->m_clientStream;
if (clientStream.GetRef())
{
clientStream->Close();
}
}
};
if (m_chunked && m_clientStream.GetRef())
{
// assume chunked
m_clientStream->Write("0\r\n\r\n", [doClose](bool)
{
doClose();
});
skipClose = true;
}
else
{
m_clientStream->Write("", [doClose](bool)
{
doClose();
});
skipClose = true;
}
if (m_requestState->blocked)
{
m_requestState->blocked = false;
decltype(m_requestState->ping) ping;
{
std::unique_lock<std::mutex> lock(m_requestState->pingLock);
ping = m_requestState->ping;
}
if (ping)
{
m_clientStream->ScheduleCallback(std::move(ping), false);
}
}
if (!skipClose)
{
doClose();
}
}
virtual void CloseSocket() override
{
auto s = m_clientStream;
if (s.GetRef())
{
s->Close();
}
}
};
HttpServerImpl::HttpServerImpl()
{
}
HttpServerImpl::~HttpServerImpl()
{
}
void HttpServerImpl::OnConnection(fwRefContainer<TcpServerStream> stream)
{
enum HttpConnectionReadState
{
ReadStateRequest,
ReadStateBody,
ReadStateChunked
};
struct HttpConnectionData
{
HttpConnectionReadState readState;
std::deque<uint8_t> readBuffer;
std::vector<uint8_t> requestData;
size_t lastLength;
phr_header headers[50]{ 0 };
phr_chunked_decoder decoder{ 0 };
fwRefContainer<HttpRequest> request;
fwRefContainer<HttpResponse> response;
int contentLength;
int pipelineLength;
bool invalid;
HttpConnectionData()
: readState(ReadStateRequest), lastLength(0), contentLength(0), pipelineLength(0), invalid(false)
{
}
};
std::shared_ptr<HttpConnectionData> connectionData = std::make_shared<HttpConnectionData>();
std::shared_ptr<HttpState> reqState = std::make_shared<HttpState>();
std::function<void(const std::vector<uint8_t>&)> readCallback;
readCallback = [this, stream, connectionData, reqState](const std::vector<uint8_t>& data)
{
// keep a reference to the connection data locally
std::shared_ptr<HttpConnectionData> localConnectionData = connectionData;
// if the connection is supposed to be closed, don't try using it
if (localConnectionData->invalid)
{
return;
}
// place bytes in the read buffer
auto& readQueue = connectionData->readBuffer;
size_t origSize = readQueue.size();
readQueue.resize(origSize + data.size());
// close the stream if the length is too big
if (readQueue.size() > (1024 * 1024 * 5))
{
stream->Close();
return;
}
// actually copy
std::copy(data.begin(), data.end(), readQueue.begin() + origSize);
// process request data until there's no need anymore
bool continueProcessing = true;
while (continueProcessing)
{
// second check: if the connection is supposed to be closed, don't try using it
if (localConnectionData->invalid)
{
return;
}
// depending on the state, perform an action
if (localConnectionData->readState == ReadStateRequest)
{
if (reqState->blocked)
{
break;
}
// increment the pipeline length, so we can close every 10 requests
localConnectionData->pipelineLength++;
// copy the deque into a vector for data purposes
std::vector<uint8_t> requestData(readQueue.begin(), readQueue.end());
// define output variables
const char* requestMethod;
size_t requestMethodLength;
const char* path;
size_t pathLength;
int minorVersion;
size_t numHeaders = 50;
int result = -2;
if (requestData.size() > 0)
{
result = phr_parse_request(reinterpret_cast<const char*>(&requestData[0]), requestData.size(), &requestMethod, &requestMethodLength,
&path, &pathLength, &minorVersion, localConnectionData->headers, &numHeaders, localConnectionData->lastLength);
}
if (result > 0)
{
// prepare data for a request instance
HeaderString requestMethodStr(requestMethod, requestMethodLength);
HeaderString pathStr(path, pathLength);
HeaderMap headerList;
for (int i = 0; i < numHeaders; i++)
{
auto& header = localConnectionData->headers[i];
headerList.insert({ { header.name, header.name_len }, { header.value, header.value_len } });
}
// remove the original bytes from the queue
readQueue.erase(readQueue.begin(), readQueue.begin() + result);
localConnectionData->lastLength = 0;
// store the request in a request instance
fwRefContainer<HttpRequest> request = new HttpRequest(1, minorVersion, requestMethodStr, pathStr, headerList, stream->GetPeerAddress());
fwRefContainer<HttpResponse> response = new Http1Response(stream, request, reqState, localConnectionData->pipelineLength > 10);
reqState->blocked = true;
localConnectionData->request = request;
localConnectionData->response = response;
for (auto& handler : m_handlers)
{
if (handler->HandleRequest(request, response) || response->HasEnded())
{
break;
}
}
continueProcessing = (readQueue.size() > 0);
if (!response->HasEnded())
{
// check to see if we'll have to read user data
static HeaderString contentLengthKey = "content-length";
static std::string contentLengthDefault = "0";
auto contentLengthStr = request->GetHeader(contentLengthKey, contentLengthDefault);
int contentLength = atoi(contentLengthStr.data());
if (contentLength > 0)
{
localConnectionData->contentLength = contentLength;
localConnectionData->readState = ReadStateBody;
}
else
{
static HeaderString transferEncodingKey = "transfer-encoding";
static std::string transferEncodingDefault = "";
static std::string_view transferEncodingComparison = "chunked";
if (request->GetHeader(transferEncodingKey, transferEncodingDefault) == transferEncodingComparison)
{
localConnectionData->contentLength = -1;
localConnectionData->lastLength = 0;
localConnectionData->readState = ReadStateChunked;
memset(&localConnectionData->decoder, 0, sizeof(localConnectionData->decoder));
localConnectionData->decoder.consume_trailer = true;
}
}
}
}
else if (result == -1)
{
// should probably send 'bad request'?
localConnectionData->invalid = true;
stream->Close();
return;
}
else if (result == -2)
{
localConnectionData->lastLength = requestData.size();
continueProcessing = false;
}
}
else if (connectionData->readState == ReadStateBody)
{
// skip if this is an empty write
if (data.empty())
{
break;
}
int contentLength = localConnectionData->contentLength;
if (readQueue.size() >= contentLength)
{
// copy the deque into a vector for data purposes, again
std::vector<uint8_t> requestData(readQueue.begin(), readQueue.begin() + contentLength);
// remove the original bytes from the queue
readQueue.erase(readQueue.begin(), readQueue.begin() + contentLength);
if (localConnectionData->request.GetRef())
{
// call the data handler
auto dataHandler = localConnectionData->request->GetDataHandler();
if (dataHandler)
{
localConnectionData->request->SetDataHandler();
(*dataHandler)(requestData);
}
else
{
localConnectionData->request->SetPendingData(std::move(requestData));
}
}
// clean up the req/res
//localConnectionData->request = nullptr;
localConnectionData->response = nullptr;
localConnectionData->readState = ReadStateRequest;
continueProcessing = (readQueue.size() > 0);
}
else
{
continueProcessing = false;
}
}
else if (connectionData->readState == ReadStateChunked)
{
// skip if this is an empty write
if (data.empty())
{
break;
}
// append the remnant of the read queue to the vector
auto& requestData = localConnectionData->requestData;
size_t addedSize = readQueue.size() - requestData.size();
size_t oldSize = requestData.size();
requestData.resize(readQueue.size());
// copy the appendant
std::copy(readQueue.begin() + oldSize, readQueue.end(), requestData.begin() + localConnectionData->lastLength);
// decode stuff
size_t requestSize = addedSize;
int result = phr_decode_chunked(&localConnectionData->decoder, reinterpret_cast<char*>(&requestData[localConnectionData->lastLength]), &requestSize);
if (result == -2)
{
localConnectionData->lastLength += requestSize;
continueProcessing = false;
}
else if (result == -1)
{
localConnectionData->invalid = true;
stream->Close();
return;
}
else
{
// remove the original bytes from the queue
readQueue.erase(readQueue.begin(), readQueue.begin() + readQueue.size() - result);
auto request = localConnectionData->request;
if (request.GetRef())
{
// call the data handler
auto dataHandler = request->GetDataHandler();
if (dataHandler)
{
request->SetDataHandler();
requestData.resize(localConnectionData->lastLength);
(*dataHandler)(requestData);
}
}
// clean up the response
localConnectionData->response = nullptr;
localConnectionData->requestData.clear();
localConnectionData->readState = ReadStateRequest;
localConnectionData->lastLength = 0;
continueProcessing = (readQueue.size() > 0);
}
}
}
};
{
std::unique_lock<std::mutex> lock(reqState->pingLock);
reqState->ping = [stream, readCallback]()
{
if (readCallback)
{
stream->ScheduleCallback([readCallback]()
{
readCallback({});
});
}
};
}
stream->SetReadCallback(readCallback);
stream->SetCloseCallback([=]()
{
if (connectionData && connectionData->request.GetRef())
{
auto cancelHandler = connectionData->request->GetCancelHandler();
if (cancelHandler)
{
(*cancelHandler)();
connectionData->request->SetCancelHandler();
}
connectionData->request = nullptr;
}
if (reqState)
{
std::unique_lock<std::mutex> lock(reqState->pingLock);
reqState->ping = {};
}
});
}
}