-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpServ.cpp
More file actions
2938 lines (2623 loc) · 141 KB
/
HttpServ.cpp
File metadata and controls
2938 lines (2623 loc) · 141 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
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2016-2020 Thomas Hauck - All Rights Reserved.
Distributed under MIT license.
See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
The author would be happy if changes and
improvements were reported back to him.
Author: Thomas Hauck
Email: Thomas@fam-hauck.de
*/
#include <regex>
#include <map>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <chrono>
#include <codecvt>
#include "HttpServ.h"
#include "Timer.h"
#include <fstream>
#include "H2Proto.h"
#include "LogFile.h"
#include "CommonLib/Base64.h"
#include "GZip.h"
#include <brotli/encode.h>
#include "CommonLib/md5.h"
#include "CommonLib/sha256.h"
#include "CommonLib/UrlCode.h"
#include "SpawnProcess.h"
#include "FastCgi/FastCgi.h"
#include "MappedFile.h"
using namespace std;
using namespace std::placeholders;
#if defined(_WIN32) || defined(_WIN64)
#if _MSC_VER < 1700
using namespace tr1;
#endif
#define FN_CA(x) x.c_str()
#define FN_STR(x) x
constexpr wchar_t g_szHttpFetch[]{L".//Http2Fetch.exe/$1"};
#else
#ifndef __USE_LARGEFILE64
#define __USE_LARGEFILE64
#define _LARGEFILE_SOURCE
#define _LARGEFILE64_SOURCE
#endif
#include <sys/stat.h>
#include <unistd.h>
#include <math.h>
#define _stat stat
#define _wstat stat
#define _stat64 stat64
#define _wstat64 stat64
#define _waccess access
#define _S_IFDIR S_IFDIR
#define _S_IFREG S_IFREG
#define FN_CA(x) wstring_convert<codecvt_utf8<wchar_t>, wchar_t>().to_bytes(x).c_str()
#define FN_STR(x) wstring_convert<codecvt_utf8<wchar_t>, wchar_t>().to_bytes(x)
const wchar_t* g_szHttpFetch{L".//Http2Fetch/$1"};
extern void OutputDebugString(const wchar_t* pOut);
extern void OutputDebugStringA(const char* pOut);
#endif
const char* CHttpServ::SERVERSIGNATUR = "Http2Serv/1.0.0";
atomic_size_t s_nInstCount(0);
map<string, FastCgiClient> s_mapFcgiConnections;
mutex s_mxFcgi;
map<string, int32_t> CHttpServ::s_lstIpConnect;
mutex CHttpServ::s_mxIpConnect;
CHttpServ::CHttpServ(const wstring& strRootPath/* = wstring(L".")*/, const string& strBindIp/* = string("127.0.0.1")*/, uint16_t sPort/* = 80*/, bool bSSL/* = false*/) : m_pSocket(nullptr), m_strBindIp(strBindIp), m_sPort(sPort), m_cLocal(locale("C"))
{
++s_nInstCount;
HOSTPARAM hp;
hp.m_strRootPath = strRootPath;
hp.m_bSSL = bSSL;
hp.m_nMaxConnPerIp = -1;
m_vHostParam.emplace(string(), hp);
}
CHttpServ& CHttpServ::operator=(CHttpServ&& other) noexcept
{
++s_nInstCount;
swap(m_pSocket, other.m_pSocket);
other.m_pSocket = nullptr;
swap(m_vConnections, other.m_vConnections);
swap(m_strBindIp, other.m_strBindIp);
swap(m_sPort, other.m_sPort);
swap(m_vHostParam, other.m_vHostParam);
swap(m_cLocal, other.m_cLocal);
swap(m_umActionThreads, other.m_umActionThreads);
return *this;
}
CHttpServ::~CHttpServ()
{
Stop();
while (IsStopped() == false)
this_thread::sleep_for(chrono::milliseconds(10));
const lock_guard<mutex> lock(s_mxFcgi);
if (--s_nInstCount == 0 && s_mapFcgiConnections.size() > 0)
s_mapFcgiConnections.clear();
}
bool CHttpServ::Start()
{
if (m_vHostParam[""].m_bSSL == true)
{
auto pSocket = make_unique<SslTcpServer>();
if (m_vHostParam[""].m_strCAcertificate.empty() == false && m_vHostParam[""].m_strHostCertificate.empty() == false && m_vHostParam[""].m_strHostKey.empty() == false)
{
if (pSocket->AddCertificate(m_vHostParam[""].m_strCAcertificate.c_str(), m_vHostParam[""].m_strHostCertificate.c_str(), m_vHostParam[""].m_strHostKey.c_str()) == false)
{
return false;
}
if (m_vHostParam[""].m_strSslCipher.empty() == false)
pSocket->SetCipher(m_vHostParam[""].m_strSslCipher.c_str());
}
for (auto& Item : m_vHostParam)
{
if (Item.first != "" && Item.second.m_bSSL == true)
{
if (pSocket->AddCertificate(Item.second.m_strCAcertificate.c_str(), Item.second.m_strHostCertificate.c_str(), Item.second.m_strHostKey.c_str()) == false)
{
return false;
}
if (Item.second.m_strSslCipher.empty() == false)
pSocket->SetCipher(Item.second.m_strSslCipher.c_str());
}
}
const vector<string> Alpn({ { "h2" },{ "http/1.1" } });
pSocket->SetAlpnProtokollNames(Alpn);
m_pSocket = move(pSocket);
}
else
m_pSocket = make_unique<TcpServer>();
m_pSocket->BindNewConnection(static_cast<function<void(const vector<TcpSocket*>&)>>(bind(&CHttpServ::OnNewConnection, this, _1)));
m_pSocket->BindErrorFunction(static_cast<function<void(BaseSocket* const)>>(bind(&CHttpServ::OnSocketError, this, _1)));
return m_pSocket->Start(m_strBindIp.c_str(), m_sPort);
}
bool CHttpServ::Stop()
{
if (m_pSocket != nullptr)
{
m_pSocket->Close();
m_pSocket.reset(nullptr);
}
m_mtxConnections.lock();
for (auto& item : m_vConnections)
{
item.second.pTimer->Stop();
item.first->Close();
}
m_mtxConnections.unlock();
return true;
}
bool CHttpServ::IsStopped() noexcept
{
return m_vConnections.size() == 0 ? true : false;
}
CHttpServ::HOSTPARAM& CHttpServ::GetParameterBlockRef(const string& szHostName)
{
if (szHostName != string() && m_vHostParam.find(szHostName) == end(m_vHostParam))
m_vHostParam[szHostName] = m_vHostParam[string()];
return m_vHostParam[szHostName];
}
void CHttpServ::ClearAllParameterBlocks()
{
HOSTPARAM hp;
hp.m_strRootPath = m_vHostParam[string()].m_strRootPath;
hp.m_bSSL = false;
m_vHostParam.clear();
m_vHostParam.emplace(string(), hp);
}
const string& CHttpServ::GetBindAdresse() noexcept
{
return m_strBindIp;
}
uint16_t CHttpServ::GetPort() noexcept
{
return m_sPort;
}
void CHttpServ::OnNewConnection(const vector<TcpSocket*>& vNewConnections)
{
vector<TcpSocket*> vCache;
for (auto& pSocket : vNewConnections)
{
if (pSocket != nullptr)
{
if (m_vHostParam[string()].m_nMaxConnPerIp > 0)
{
s_mxIpConnect.lock();
auto itIpItem = s_lstIpConnect.find(pSocket->GetClientAddr());
if (itIpItem != s_lstIpConnect.end())
{
if (itIpItem->second > m_vHostParam[string()].m_nMaxConnPerIp)
{
s_mxIpConnect.unlock();
CLogFile::GetInstance(m_vHostParam[string()].m_strErrLog).WriteToLog("[", CLogFile::LOGTYPES::PUTTIME, "] [error] [client ", pSocket->GetClientAddr(), "] to many connections");
pSocket->Close();
continue;
}
itIpItem->second++;
}
else
s_lstIpConnect.insert({ pSocket->GetClientAddr(), 1 });
s_mxIpConnect.unlock();
}
pSocket->BindFuncBytesReceived(static_cast<function<void(TcpSocket* const)>>(bind(&CHttpServ::OnDataReceived, this, _1)));
pSocket->BindErrorFunction(static_cast<function<void(BaseSocket* const)>>(bind(&CHttpServ::OnSocketError, this, _1)));
pSocket->BindCloseFunction(static_cast<function<void(BaseSocket* const)>>(bind(&CHttpServ::OnSocketClosing, this, _1)));
vCache.push_back(pSocket);
}
}
if (vCache.size())
{
m_mtxConnections.lock();
for (auto& pSocket : vCache)
{
m_vConnections.emplace(pSocket, CONNECTIONDETAILS({ make_shared<Timer<TcpSocket>>(30000, bind(&CHttpServ::OnTimeout, this, _1, _2), pSocket), string(), false, 0, 0, {}, {}, make_shared<mutex>(), {}, make_tuple(UINT32_MAX, 65535, 16384, UINT32_MAX, 4096), {}, make_shared<atomic_bool>(false), make_shared<mutex>(), {}, {} }));
pSocket->StartReceiving();
}
m_mtxConnections.unlock();
}
}
void CHttpServ::OnDataReceived(TcpSocket* const pTcpSocket)
{
const size_t nAvailable = pTcpSocket->GetBytesAvailable();
if (nAvailable == 0)
{
pTcpSocket->Close();
return;
}
const unique_ptr<char[]> spBuffer = make_unique<char[]>(nAvailable);
const size_t nRead = pTcpSocket->Read(&spBuffer[0], nAvailable);
if (nRead > 0)
{
m_mtxConnections.lock();
const CONNECTIONLIST::iterator item = m_vConnections.find(pTcpSocket);
if (item != end(m_vConnections))
{
CONNECTIONDETAILS* pConDetails = &item->second;
pConDetails->pTimer->Reset();
pConDetails->strBuffer.append(&spBuffer[0], nRead);
if (pConDetails->bIsH2Con == false)
{
if (pConDetails->nContentsSoll == 0 && pConDetails->strBuffer.size() >= 24 && pConDetails->strBuffer.compare(0, 24, "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n") == 0)
{
pTcpSocket->Write("\x0\x0\xc\x4\x0\x0\x0\x0\x0\x0\x4\x0\x60\x0\x0\x0\x5\x0\x0\x40\x0", 21);// SETTINGS frame (4) with ParaID(4) and ?6291456? Value + ParaID(5) and 16384 Value
pTcpSocket->Write("\x0\x0\x4\x8\x0\x0\x0\x0\x0\x0\x5f\x0\x1", 13); // WINDOW_UPDATE frame (8) with value ??6291456?? (minus 65535) == ?6225921?
pConDetails->bIsH2Con = true;
pConDetails->strBuffer.erase(0, 24);
if (pConDetails->strBuffer.size() == 0)
{
m_mtxConnections.unlock();
return;
}
}
else if (pConDetails->nContentsSoll != 0 && pConDetails->nContentRecv < pConDetails->nContentsSoll) // File upload in progress
{
const size_t nBytesToWrite = static_cast<size_t>(min(static_cast<uint64_t>(pConDetails->strBuffer.size()), pConDetails->nContentsSoll - pConDetails->nContentRecv));
if (nBytesToWrite > 0)
{
pConDetails->mutReqData->lock();
pConDetails->vecReqData.emplace_back(make_unique<char[]>(nBytesToWrite + 4));
copy(&pConDetails->strBuffer[0], &pConDetails->strBuffer[nBytesToWrite], pConDetails->vecReqData.back().get() + 4);
*reinterpret_cast<uint32_t*>(pConDetails->vecReqData.back().get()) = static_cast<uint32_t>(nBytesToWrite);
//OutputDebugString(wstring(L"X. Datenempfang: " + to_wstring(nBytesToWrite) + L" Bytes\r\n").c_str());
pConDetails->mutReqData->unlock();
pConDetails->nContentRecv += nBytesToWrite;
pConDetails->strBuffer.erase(0, nBytesToWrite);
}
if (pConDetails->nContentRecv == pConDetails->nContentsSoll) // Last byte of Data, we signal this with a empty vector entry
{
pConDetails->mutReqData->lock();
pConDetails->vecReqData.emplace_back(unique_ptr<char[]>(nullptr));
//OutputDebugString(wstring(L"Datenempfang beendet\r\n").c_str());
pConDetails->nContentRecv = pConDetails->nContentsSoll = 0;
pConDetails->mutReqData->unlock();
if (pConDetails->strBuffer.size() == 0)
{
m_mtxConnections.unlock();
return;
}
}
else
{
m_mtxConnections.unlock();
return;
}
}
}
if (pConDetails->bIsH2Con == true) // bool is http2
{
size_t nLen = pConDetails->strBuffer.size();
if (nLen < H2HEADERLEN)
{
m_mtxConnections.unlock();
return;
}
const MetaSocketData soMetaDa { pTcpSocket->GetClientAddr(), pTcpSocket->GetClientPort(), pTcpSocket->GetInterfaceAddr(), pTcpSocket->GetInterfacePort(), pTcpSocket->IsSslConnection(), bind(&TcpSocket::Write, pTcpSocket, _1, _2), bind(&TcpSocket::Close, pTcpSocket), bind(&TcpSocket::GetOutBytesInQue, pTcpSocket), bind(&Timer<TcpSocket>::Reset, pConDetails->pTimer), bind(&Timer<TcpSocket>::SetNewTimeout, pConDetails->pTimer, _1) };
size_t nRet;
if (nRet = Http2StreamProto(soMetaDa, &pConDetails->strBuffer[0], nLen, pConDetails->lstDynTable, pConDetails->StreamParam, pConDetails->H2Streams, ref(*pConDetails->mutStreams.get()), pConDetails->StreamResWndSizes, ref(*pConDetails->atStop.get()), ref(pConDetails->lstAuthInfo)), nRet != SIZE_MAX)
{
pConDetails->strBuffer.erase(0, pConDetails->strBuffer.size() - nLen);
m_mtxConnections.unlock();
return;
}
// After a GOAWAY we terminate the connection
// we wait, until all action thread's are finished, otherwise we remove the connection while the action thread is still using it = crash
*pConDetails->atStop.get() = true;
auto patStop = pConDetails->atStop.get();
m_ActThrMutex.lock();
m_mtxConnections.unlock();
for (unordered_multimap<thread::id, atomic<bool>&>::iterator iter = begin(m_umActionThreads); iter != end(m_umActionThreads);)
{
if (&iter->second == patStop)
{
m_ActThrMutex.unlock();
this_thread::sleep_for(chrono::milliseconds(1));
m_ActThrMutex.lock();
iter = begin(m_umActionThreads);
continue;
}
++iter;
}
m_ActThrMutex.unlock();
soMetaDa.fSocketClose();
return;
}
const size_t nPosEndOfHeader = pConDetails->strBuffer.find("\r\n\r\n");
if (nPosEndOfHeader != string::npos)
{
//auto dwStart = chrono::high_resolution_clock::now();
// If we get here we should have a HTTP request in strPuffer
HeadList::iterator parLastHeader = end(pConDetails->HeaderList);
const static regex crlfSeperator("\r\n");
sregex_token_iterator line(begin(pConDetails->strBuffer), begin(pConDetails->strBuffer) + nPosEndOfHeader, crlfSeperator, -1);
while (line != sregex_token_iterator())
{
if (pConDetails->HeaderList.size() == 0) // 1 Zeile
{
const string& strLine = line->str();
const static regex SpaceSeperator(" ");
sregex_token_iterator token(begin(strLine), end(strLine), SpaceSeperator, -1);
if (token != sregex_token_iterator() && token->str().empty() == false)
pConDetails->HeaderList.emplace_back(make_pair(":method", token++->str()));
if (token != sregex_token_iterator() && token->str().empty() == false)
pConDetails->HeaderList.emplace_back(make_pair(":path", token++->str()));
if (token != sregex_token_iterator() && token->str().empty() == false)
{
const auto parResult = pConDetails->HeaderList.emplace(pConDetails->HeaderList.end(), make_pair(":version", token++->str()));
if (parResult != end(pConDetails->HeaderList))
parResult->second.erase(0, parResult->second.find_first_of('.') + 1);
}
if (pConDetails->HeaderList.size() != 3) // The first line should have 3 part. method, path and HTTP/1.x version
{
pConDetails->HeaderList.emplace_back(make_pair(":1stline", strLine));
SendErrorRespons(pTcpSocket, pConDetails->pTimer, 400, HTTPVERSION11, pConDetails->HeaderList);
pTcpSocket->Close();
m_mtxConnections.unlock();
return;
}
}
else
{
const size_t nPos1 = line->str().find(':');
if (nPos1 != string::npos)
{
string strTmp = line->str().substr(0, nPos1);
transform(begin(strTmp), begin(strTmp) + nPos1, begin(strTmp), [](char c) noexcept { return static_cast<char>(::tolower(c)); });
const auto parResult = pConDetails->HeaderList.emplace(pConDetails->HeaderList.end(), make_pair(strTmp, line->str().substr(nPos1 + 1)));
if (parResult != end(pConDetails->HeaderList))
{
parResult->second.erase(parResult->second.find_last_not_of(" \r\n\t") + 1);
parResult->second.erase(0, parResult->second.find_first_not_of(" \t"));
parLastHeader = parResult;
}
}
else if (line->str().find(" \t") == 0 && parLastHeader != end(pConDetails->HeaderList)) // Multi line Header
{
line->str().erase(line->str().find_last_not_of(" \r\n\t") + 1);
line->str().erase(0, line->str().find_first_not_of(" \t"));
parLastHeader->second += " " + line->str();
}
else
{ // header without a : char are a bad request
parLastHeader = end(pConDetails->HeaderList);
SendErrorRespons(pTcpSocket, pConDetails->pTimer, 400, 0, pConDetails->HeaderList);
pTcpSocket->Close();
m_mtxConnections.unlock();
return;
}
}
++line;
}
pConDetails->strBuffer.erase(0, nPosEndOfHeader + 4);
const auto expect = pConDetails->HeaderList.find("expect");
if (expect != end(pConDetails->HeaderList))
{
if (expect->second == "100-continue")
pTcpSocket->Write("HTTP/1.1 100 Continue\r\n\r\n", 25);
else
{ // other expect is not yet supported
SendErrorRespons(pTcpSocket, pConDetails->pTimer, 417, 0, pConDetails->HeaderList);
pTcpSocket->Close();
m_mtxConnections.unlock();
return;
}
}
// set end of data signal preliminary
pConDetails->mutReqData->lock();
pConDetails->vecReqData.emplace_back(unique_ptr<char[]>(nullptr));
pConDetails->mutReqData->unlock();
// is data (body) on the request, only HTTP/1.1
const auto contentLength = pConDetails->HeaderList.find("content-length");
if (contentLength != end(pConDetails->HeaderList) && contentLength->second != "18446744073709551615") // SSTL senden ULONGLONG_MAX as Content-length
{
try
{
if (stoll(contentLength->second) < 0)
{ // other expect is not yet supported
SendErrorRespons(pTcpSocket, pConDetails->pTimer, 400, 0, pConDetails->HeaderList);
pTcpSocket->Close();
m_mtxConnections.unlock();
return;
}
pConDetails->nContentsSoll = stoull(contentLength->second);
}
catch (const std::exception& /*ex*/)
{
SendErrorRespons(pTcpSocket, pConDetails->pTimer, 400, 0, pConDetails->HeaderList);
pTcpSocket->Close();
m_mtxConnections.unlock();
return;
}
if (pConDetails->nContentsSoll > 0)
{
pConDetails->mutReqData->lock();
pConDetails->vecReqData.clear(); // Clear the end of Data entry
const size_t nBytesToWrite = static_cast<size_t>(min(static_cast<uint64_t>(pConDetails->strBuffer.size()), pConDetails->nContentsSoll));
if (nBytesToWrite > 0)
{
pConDetails->vecReqData.push_back(make_unique<char[]>(nBytesToWrite + 4));
copy(&pConDetails->strBuffer[0], &pConDetails->strBuffer[nBytesToWrite], pConDetails->vecReqData.back().get() + 4);
*reinterpret_cast<uint32_t*>(pConDetails->vecReqData.back().get()) = static_cast<uint32_t>(nBytesToWrite);
//OutputDebugString(wstring(L"1. Datenempfang: " + to_wstring(nBytesToWrite) + L" Bytes\r\n").c_str());
pConDetails->nContentRecv = nBytesToWrite;
pConDetails->strBuffer.erase(0, nBytesToWrite);
}
if (pConDetails->nContentRecv == pConDetails->nContentsSoll) // Last byte of Data, we signal this with a empty vector entry
{
pConDetails->vecReqData.push_back(unique_ptr<char[]>(nullptr));
//OutputDebugString(wstring(L"Datenempfang sofort beendet\r\n").c_str());
pConDetails->nContentRecv = pConDetails->nContentsSoll = 0;
}
pConDetails->mutReqData->unlock();
}
}
//auto dwDif = chrono::high_resolution_clock::now();
//MyTrace("Time in ms for Header parsing ", (chrono::duration<float, chrono::milliseconds::period>(dwDif - dwStart).count()));
}
else if (pConDetails->nContentsSoll == 0) // Noch kein End of Header, und kein Content empfangen
{
m_mtxConnections.unlock();
return;
}
uint32_t nStreamId = 0;
const auto upgradeHeader = pConDetails->HeaderList.find("upgrade");
if (upgradeHeader != end(pConDetails->HeaderList) && upgradeHeader->second == "h2c")
{
const auto http2SettingsHeader = pConDetails->HeaderList.find("http2-settings");
if (http2SettingsHeader != end(pConDetails->HeaderList))
{
string strHttp2Settings = Base64::Decode(http2SettingsHeader->second, true);
size_t nHeaderLen = strHttp2Settings.size();
const MetaSocketData soMetaDa { pTcpSocket->GetClientAddr(), pTcpSocket->GetClientPort(), pTcpSocket->GetInterfaceAddr(), pTcpSocket->GetInterfacePort(), pTcpSocket->IsSslConnection(), bind(&TcpSocket::Write, pTcpSocket, _1, _2), bind(&TcpSocket::Close, pTcpSocket), bind(&TcpSocket::GetOutBytesInQue, pTcpSocket), bind(&Timer<TcpSocket>::Reset, pConDetails->pTimer), bind(&Timer<TcpSocket>::SetNewTimeout, pConDetails->pTimer, _1) };
pTcpSocket->Write("HTTP/1.1 101 Switching Protocols\r\nConnection: Upgrade\r\nUpgrade: h2c\r\n\r\n", 71);
pTcpSocket->Write("\x0\x0\xc\x4\x0\x0\x0\x0\x0\x0\x4\x0\x10\x0\x0\x0\x5\x0\x0\x40\x0", 21);// SETTINGS frame (4) with ParaID(4) and 1048576 Value + ParaID(5) and 16384 Value
pTcpSocket->Write("\x0\x0\x4\x8\x0\x0\x0\x0\x0\x0\xf\x0\x1", 13); // WINDOW_UPDATE frame (8) with value ?1048576? (minus 65535) == 983041
nStreamId = 1;
size_t nRet;
if (nRet = Http2StreamProto(soMetaDa, &strHttp2Settings[0], nHeaderLen, pConDetails->lstDynTable, pConDetails->StreamParam, pConDetails->H2Streams, ref(*pConDetails->mutStreams.get()), pConDetails->StreamResWndSizes, ref(*pConDetails->atStop.get()), ref(pConDetails->lstAuthInfo)), nRet == SIZE_MAX)
{
*pConDetails->atStop.get() = true;
// After a GOAWAY we terminate the connection
soMetaDa.fSocketClose();
m_mtxConnections.unlock();
return;
}
else if (nHeaderLen != 0)
pConDetails->strBuffer.insert(0, strHttp2Settings.substr(strHttp2Settings.size() - nHeaderLen));
}
}
if (pConDetails->bIsH2Con == false) // If we received or send no GOAWAY Frame in HTTP/2 we end up here, and send the response to the request how made the upgrade
{
const MetaSocketData soMetaDa { pTcpSocket->GetClientAddr(), pTcpSocket->GetClientPort(), pTcpSocket->GetInterfaceAddr(), pTcpSocket->GetInterfacePort(), pTcpSocket->IsSslConnection(), bind(&TcpSocket::Write, pTcpSocket, _1, _2), bind(&TcpSocket::Close, pTcpSocket), bind(&TcpSocket::GetOutBytesInQue, pTcpSocket), bind(&Timer<TcpSocket>::Reset, pConDetails->pTimer), bind(&Timer<TcpSocket>::SetNewTimeout, pConDetails->pTimer, _1) };
pConDetails->mutStreams->lock();
const uint32_t nNextId = static_cast<uint32_t>(pConDetails->H2Streams.size());
pConDetails->H2Streams.emplace(nNextId, STREAMITEM({ 0, deque<DATAITEM>(), move(pConDetails->HeaderList), 0, 0, INITWINDOWSIZE(pConDetails->StreamParam), make_shared<mutex>(), {} }));
pConDetails->mutStreams->unlock();
//m_mtxConnections.unlock();
//DoAction(soMetaDa, nStreamId, pConDetails->H2Streams, pConDetails->StreamParam, pConDetails->mutStreams.get(), pConDetails->StreamResWndSizes, bind(nStreamId != 0 ? &CHttpServ::BuildH2ResponsHeader : &CHttpServ::BuildResponsHeader, this, _1, _2, _3, _4, _5, _6), pConDetails->atStop.get());
m_ActThrMutex.lock();
thread thTemp(&CHttpServ::DoAction, this, soMetaDa, static_cast<uint8_t>(1), nNextId, ref(pConDetails->H2Streams), ref(pConDetails->StreamParam), ref(*pConDetails->mutStreams.get()), ref(pConDetails->StreamResWndSizes), bind(nStreamId != 0 ? &CHttpServ::BuildH2ResponsHeader : &CHttpServ::BuildResponsHeader, this, _1, _2, _3, _4, _5, _6), ref(*pConDetails->atStop.get()), ref(*pConDetails->mutReqData.get()), ref(pConDetails->vecReqData), ref(pConDetails->lstAuthInfo));
m_umActionThreads.emplace(thTemp.get_id(), *pConDetails->atStop.get());
m_ActThrMutex.unlock();
thTemp.detach();
if (nStreamId != 0)
pConDetails->bIsH2Con = true;
/*
lock_guard<mutex> lock1(m_mtxConnections);
if (m_vConnections.find(pTcpSocket) == end(m_vConnections))
return; // Sollte bei Socket Error oder Time auftreten
if (nStreamId != 0)
pConDetails->bIsH2Con = true;
else
{
lock_guard<mutex> lock2(*pConDetails->mutStreams.get());
pConDetails->H2Streams.clear();
}
pConDetails->nContentRecv = pConDetails->nContentsSoll = 0;
pConDetails->HeaderList.clear();
return;*/
}
}
m_mtxConnections.unlock();
}
}
void CHttpServ::OnSocketError(BaseSocket* const pBaseSocket)
{
MyTrace("Error: Network error ", pBaseSocket->GetErrorNo());
string szHost;
m_mtxConnections.lock();
const auto item = m_vConnections.find(reinterpret_cast<TcpSocket*>(pBaseSocket));
if (item != end(m_vConnections))
{
item->second.pTimer->Stop();
*item->second.atStop.get() = true;
auto host = item->second.HeaderList.find("host");
if (host == end(item->second.HeaderList))
host = item->second.HeaderList.find(":authority");
if (host != end(item->second.HeaderList))
{
const string strTmp = host->second + (host->second.find(":") == string::npos ? (":" + to_string(GetPort())) : "");
if (m_vHostParam.find(strTmp) != end(m_vHostParam))
szHost = strTmp;
}
}
m_mtxConnections.unlock();
if (m_vHostParam[szHost].m_strErrLog.empty() == false)
{
TcpSocket* pTcpSocket = dynamic_cast<TcpSocket*>(pBaseSocket);
CLogFile::GetInstance(m_vHostParam[szHost].m_strErrLog).WriteToLog("[", CLogFile::LOGTYPES::PUTTIME, "] [error] [client ", (pTcpSocket != nullptr ? pTcpSocket->GetClientAddr() : "0.0.0.0"), "] network error no.: ", pBaseSocket->GetErrorNo(), " @ ", pBaseSocket->GetErrorLoc());
}
pBaseSocket->Close();
}
void CHttpServ::OnSocketClosing(BaseSocket* const pBaseSocket)
{
//OutputDebugString(L"CHttpServ::OnSocketClosing\r\n");
m_mtxConnections.lock();
auto item = m_vConnections.find(reinterpret_cast<TcpSocket*>(pBaseSocket));
if (item != end(m_vConnections))
{
Timer<TcpSocket>* pTimer = item->second.pTimer.get();
m_mtxConnections.unlock();
pTimer->Stop();
while (pTimer->IsStopped() == false)
{
this_thread::sleep_for(chrono::microseconds(1));
pTimer->Stop();
}
m_mtxConnections.lock();
item = m_vConnections.find(reinterpret_cast<TcpSocket*>(pBaseSocket));
if (item != end(m_vConnections))
{
// we wait, until all action thread's are finished for this connection, otherwise we remove the connection while the action thread is still using it = crash
m_ActThrMutex.lock();
for (auto iter = begin(m_umActionThreads); iter != end(m_umActionThreads);)
{
if (&iter->second == item->second.atStop.get())
{
iter->second = true; // Stop the DoAction thread
m_ActThrMutex.unlock();
m_mtxConnections.unlock();
this_thread::sleep_for(chrono::milliseconds(1));
m_mtxConnections.lock();
m_ActThrMutex.lock();
item = m_vConnections.find(reinterpret_cast<TcpSocket*>(pBaseSocket));
if (item == end(m_vConnections))
break;
iter = begin(m_umActionThreads);
continue;
}
++iter;
}
m_ActThrMutex.unlock();
if (item != end(m_vConnections))
m_vConnections.erase(item->first);
s_mxIpConnect.lock();
auto itIpItem = s_lstIpConnect.find(reinterpret_cast<TcpSocket*>(pBaseSocket)->GetClientAddr());
if (itIpItem != s_lstIpConnect.end())
{
itIpItem->second--;
if (itIpItem->second == 0)
s_lstIpConnect.erase(itIpItem);
}
s_mxIpConnect.unlock();
}
}
m_mtxConnections.unlock();
}
void CHttpServ::OnTimeout(const Timer<TcpSocket>* const /*pTimer*/, TcpSocket* vpData)
{
const lock_guard<mutex> lock(m_mtxConnections);
const auto item = m_vConnections.find(vpData);
if (item != end(m_vConnections))
{
if (item->second.nContentsSoll != 0 && item->second.nContentRecv < item->second.nContentsSoll) // File upload in progress HTTP/1.1
SendErrorRespons(vpData, item->second.pTimer, 408, 0, item->second.HeaderList);
if (item->second.bIsH2Con == true)
Http2Goaway(bind(&TcpSocket::Write, vpData, _1, _2), 0, 0, 0); // 0 = Graceful shutdown
*item->second.atStop.get() = true;
vpData->Close();
}
}
size_t CHttpServ::BuildH2ResponsHeader(uint8_t* const szBuffer, size_t nBufLen, int iFlag, int iRespCode, const HeadList& umHeaderList, uint64_t nContentSize/* = 0*/)
{
size_t nHeaderSize = 0;
size_t nReturn = HPackEncode(szBuffer + nHeaderSize, nBufLen - nHeaderSize, ":status", to_string(iRespCode).c_str());
if (nReturn == SIZE_MAX)
return 0;
nHeaderSize += nReturn;
const auto in_time_t = chrono::system_clock::to_time_t(chrono::system_clock::now());
stringstream strTemp;
strTemp.imbue(m_cLocal);
strTemp << put_time(::gmtime(&in_time_t), "%a, %d %b %Y %H:%M:%S GMT");
nReturn = HPackEncode(szBuffer + nHeaderSize, nBufLen - nHeaderSize, "date", strTemp.str().c_str());
if (nReturn == SIZE_MAX)
return 0;
nHeaderSize += nReturn;
nReturn = HPackEncode(szBuffer + nHeaderSize, nBufLen - nHeaderSize, "server", SERVERSIGNATUR);
if (nReturn == SIZE_MAX)
return 0;
nHeaderSize += nReturn;
if (nContentSize != 0 || iFlag & ADDCONENTLENGTH)
{
nReturn = HPackEncode(szBuffer + nHeaderSize, nBufLen - nHeaderSize, "content-length", to_string(nContentSize).c_str());
if (nReturn == SIZE_MAX)
return 0;
nHeaderSize += nReturn;
}
/*
if ((iRespCode / 100) == 2 || (iRespCode / 100) == 3)
{
nReturn = HPackEncode(szBuffer + nHeaderSize, nBufLen - nHeaderSize, "last-modified", strTemp.str().c_str());
if (nReturn == SIZE_MAX)
return 0;
nHeaderSize += nReturn;
}
*/
for (const auto& item : umHeaderList)
{
string strHeaderFiled(item.first);
transform(begin(strHeaderFiled), end(strHeaderFiled), begin(strHeaderFiled), [](char c) noexcept { return static_cast<char>(::tolower(c)); });
if (strHeaderFiled == "cache-control")
iFlag &= ~ADDNOCACHE;
if (strHeaderFiled == "connection" || strHeaderFiled == "transfer-encoding")
continue;
nReturn = HPackEncode(szBuffer + nHeaderSize, nBufLen - nHeaderSize, strHeaderFiled.c_str(), item.second.c_str());
if (nReturn == SIZE_MAX)
return 0;
nHeaderSize += nReturn;
}
if (iFlag & ADDNOCACHE)
{
nReturn = HPackEncode(szBuffer + nHeaderSize, nBufLen - nHeaderSize, "cache-control", "no-cache, no-store, must-revalidate, pre-check = 0, post-check = 0");
if (nReturn == SIZE_MAX)
return 0;
nHeaderSize += nReturn;
nReturn = HPackEncode(szBuffer + nHeaderSize, nBufLen - nHeaderSize, "expires", "Mon, 03 Apr 1961 05:00:00 GMT");
if (nReturn == SIZE_MAX)
return 0;
nHeaderSize += nReturn;
}
if (iFlag & GZIPENCODING)
{
nReturn = HPackEncode(szBuffer + nHeaderSize, nBufLen - nHeaderSize, "content-encoding", "gzip");
if (nReturn == SIZE_MAX)
return 0;
nHeaderSize += nReturn;
}
else if (iFlag & DEFLATEENCODING)
{
nReturn = HPackEncode(szBuffer + nHeaderSize, nBufLen - nHeaderSize, "content-encoding", "deflate");
if (nReturn == SIZE_MAX)
return 0;
nHeaderSize += nReturn;
}
else if (iFlag & BROTLICODING)
{
nReturn = HPackEncode(szBuffer + nHeaderSize, nBufLen - nHeaderSize, "content-encoding", "br");
if (nReturn == SIZE_MAX)
return 0;
nHeaderSize += nReturn;
}
return nHeaderSize;
}
size_t CHttpServ::BuildResponsHeader(uint8_t* const szBuffer, size_t nBufLen, int iFlag, int iRespCode, const HeadList& umHeaderList, uint64_t nContentSize/* = 0*/)
{
string strRespons;
strRespons.reserve(2048);
//strRespons.imbue(m_cLocal);
strRespons += "HTTP/1.";
strRespons += ((iFlag & HTTPVERSION11) == HTTPVERSION11 ? "1 " : "0 ") + to_string(iRespCode) + " ";
const auto& itRespText = RespText.find(iRespCode);
strRespons += itRespText != end(RespText) ? itRespText->second : "undefined";
strRespons += "\r\n";
strRespons += "Server: ";
strRespons += SERVERSIGNATUR;
strRespons += "\r\n";
strRespons += "Date: ";
auto in_time_t = chrono::system_clock::to_time_t(chrono::system_clock::now());
stringstream ss;
ss.imbue(m_cLocal);
ss << put_time(::gmtime(&in_time_t), "%a, %d %b %Y %H:%M:%S GMT\r\n");
strRespons += ss.str();
if (nContentSize != 0 || iFlag & ADDCONENTLENGTH)
strRespons += "Content-Length: " + to_string(nContentSize) + "\r\n";
for (const auto& item : umHeaderList)
{
string strHeaderFiled(item.first);
transform(begin(strHeaderFiled), end(strHeaderFiled), begin(strHeaderFiled), [](char c) noexcept { return static_cast<char>(::tolower(c)); });
if (strHeaderFiled == "pragma" || strHeaderFiled == "cache-control")
iFlag &= ~ADDNOCACHE;
strRespons += item.first + ": " + item.second + "\r\n";
}
if (iFlag & ADDNOCACHE)
{
strRespons += (iFlag & HTTPVERSION11) == HTTPVERSION11 ? "" : "Pragma: no-cache\r\n";
strRespons += "Cache-Control: no-cache\r\nExpires: Mon, 03 Apr 1961 05:00:00 GMT\r\n";
}
if (iFlag & ADDCONNECTIONCLOSE)
strRespons += "Connection: close\r\n";
else
strRespons += "Connection: Keep-Alive\r\n";
if (iFlag & GZIPENCODING)
strRespons += "Content-Encoding: gzip\r\n";
else if (iFlag & DEFLATEENCODING)
strRespons += "Content-Encoding: deflate\r\n";
else if (iFlag & BROTLICODING)
strRespons += "Content-Encoding: br\r\n";
if (iFlag & TERMINATEHEADER)
strRespons += "\r\n";
const size_t nRet = strRespons.size();
if (nRet > nBufLen)
return 0;
copy(begin(strRespons), begin(strRespons) + nRet, szBuffer);
return nRet;
}
string CHttpServ::LoadErrorHtmlMessage(HeadList& HeaderList, int iRespCode, const wstring& strMsgDir)
{
bool bSend = false;
const auto& accept = HeaderList.find("accept");
if (accept != end(HeaderList))
{
const static regex s_rxSepComma("\\s*,\\s*");
vector<string> token(sregex_token_iterator(begin(accept->second), end(accept->second), s_rxSepComma, -1), sregex_token_iterator());
for (size_t n = 0; n < token.size(); ++n)
{
if (token[n] == "text/html")
{
bSend = true;
break;
}
}
}
if (accept == end(HeaderList) || bSend == true) // No accept header -> accept all
{
ifstream src(FN_STR(strMsgDir + to_wstring(iRespCode) + L".html"), ios::in | ios::binary);
if (src.is_open() == true)
{
stringstream ssIn;
copy(istreambuf_iterator<char>(src), istreambuf_iterator<char>(), ostreambuf_iterator<char>(ssIn));
src.close();
return ssIn.str();
}
}
return string();
}
void CHttpServ::SendErrorRespons(TcpSocket* const pTcpSocket, const shared_ptr<Timer<TcpSocket>> pTimer, int iRespCode, int iFlag, HeadList& HeaderList, HeadList umHeaderList/* = HeadList()*/)
{
if (HeaderList.find(":version") != end(HeaderList) && HeaderList.find(":version")->second == "1")
iFlag |= HTTPVERSION11;
string szHost;
const auto& host = HeaderList.find("host"); // Get the Host Header from the request
if (host != end(HeaderList))
{
const string strTmp = host->second + (host->second.find(":") == string::npos ? (":" + to_string(GetPort())) : "");
if (m_vHostParam.find(strTmp) != end(m_vHostParam)) // If we have it in our configuration, we use the host parameter for logging
szHost = strTmp;
}
const string strHtmlRespons = LoadErrorHtmlMessage(HeaderList, iRespCode, m_vHostParam[szHost].m_strMsgDir.empty() == false ? m_vHostParam[szHost].m_strMsgDir : L"./msg/");
umHeaderList.insert(end(umHeaderList), begin(m_vHostParam[szHost].m_vHeader), end(m_vHostParam[szHost].m_vHeader));
constexpr uint32_t nBufSize = 1024;
const unique_ptr<uint8_t[]> pBuffer = make_unique<uint8_t[]>(nBufSize);
uint8_t* caBuffer = pBuffer.get();
const size_t nHeaderLen = BuildResponsHeader(caBuffer, nBufSize, iFlag | TERMINATEHEADER | ADDCONNECTIONCLOSE, iRespCode, umHeaderList, strHtmlRespons.size());
pTcpSocket->Write(caBuffer, nHeaderLen);
if (strHtmlRespons.size() > 0)
pTcpSocket->Write(strHtmlRespons.c_str(), strHtmlRespons.size());
pTimer.get()->Reset();
if (HeaderList.find(":method") != end(HeaderList) && HeaderList.find(":path") != end(HeaderList))
{
CLogFile::GetInstance(m_vHostParam[szHost].m_strLogFile) << pTcpSocket->GetClientAddr() << " - - [" << CLogFile::LOGTYPES::PUTTIME << "] \""
<< HeaderList.find(":method")->second << " " << HeaderList.find(":path")->second << " HTTP/1.1\" " << to_string(iRespCode) << " - \""
<< (HeaderList.find("referer") != end(HeaderList) ? HeaderList.find("referer")->second : "-") << "\" \""
<< (HeaderList.find("user-agent") != end(HeaderList) ? HeaderList.find("user-agent")->second : "-") << "\""
<< CLogFile::LOGTYPES::END;
}
else if (HeaderList.find(":1stline") != end(HeaderList))
{
CLogFile::GetInstance(m_vHostParam[szHost].m_strLogFile) << pTcpSocket->GetClientAddr() << " - - [" << CLogFile::LOGTYPES::PUTTIME << "] \""
<< HeaderList.find(":1stline")->second << "\" " << to_string(iRespCode) << " - \""
<< (HeaderList.find("referer") != end(HeaderList) ? HeaderList.find("referer")->second : "-") << "\" \""
<< (HeaderList.find("user-agent") != end(HeaderList) ? HeaderList.find("user-agent")->second : "-") << "\""
<< CLogFile::LOGTYPES::END;
}
CLogFile::GetInstance(m_vHostParam[szHost].m_strErrLog).WriteToLog("[", CLogFile::LOGTYPES::PUTTIME, "] [error] [client ", pTcpSocket->GetClientAddr(), "] ", RespText.find(iRespCode) != end(RespText) ? RespText.find(iRespCode)->second : "");
}
void CHttpServ::SendErrorRespons(const MetaSocketData& soMetaDa, const uint8_t httpVers, const uint32_t nStreamId, function<size_t(uint8_t*, size_t, int, int, HeadList, uint64_t)> BuildRespHeader, int iRespCode, int iFlag, const string& strHttpVersion, HeadList& HeaderList, HeadList umHeaderList/* = HeadList()*/)
{
string szHost;
const auto& host = HeaderList.find("host"); // Get the Host Header from the request
if (host != end(HeaderList))
{
const string strTmp = host->second + (host->second.find(":") == string::npos ? (":" + to_string(GetPort())) : "");
if (m_vHostParam.find(strTmp) != end(m_vHostParam)) // If we have it in our configuration, we use the host parameter for logging
szHost = strTmp;
}
const string strHtmlRespons = LoadErrorHtmlMessage(HeaderList, iRespCode, m_vHostParam[szHost].m_strMsgDir.empty() == false ? m_vHostParam[szHost].m_strMsgDir : L"./msg/");
umHeaderList.insert(end(umHeaderList), begin(m_vHostParam[szHost].m_vHeader), end(m_vHostParam[szHost].m_vHeader));
const uint32_t nHttp2Offset = httpVers == 2 ? H2HEADERLEN : 0;
constexpr uint32_t nBufSize = 1024;
const unique_ptr<uint8_t[]> pBuffer = make_unique<uint8_t[]>(nBufSize);
uint8_t* caBuffer = pBuffer.get();
const size_t nHeaderLen = BuildRespHeader(caBuffer + nHttp2Offset, nBufSize - nHttp2Offset, /*iHeaderFlag | ADDNOCACHE |*/ iFlag | TERMINATEHEADER | ADDCONNECTIONCLOSE, iRespCode, umHeaderList, strHtmlRespons.size());
if (nHeaderLen < 1024)
{
if (httpVers == 2)
BuildHttp2Frame(caBuffer, nHeaderLen, 0x1, strHtmlRespons.size() == 0 ? 0x5 : 0x4, nStreamId);
soMetaDa.fSocketWrite(caBuffer, nHeaderLen + nHttp2Offset);
if (strHtmlRespons.size() > 0)
{
if (httpVers == 2)
{
BuildHttp2Frame(caBuffer, strHtmlRespons.size(), 0x0, 0x1, nStreamId);
soMetaDa.fSocketWrite(caBuffer, nHttp2Offset);
}
soMetaDa.fSocketWrite(strHtmlRespons.c_str(), strHtmlRespons.size());
}
}
soMetaDa.fResetTimer();
if (HeaderList.find(":method") != end(HeaderList) && HeaderList.find(":path") != end(HeaderList))
{
CLogFile::GetInstance(m_vHostParam[szHost].m_strLogFile) << soMetaDa.strIpClient << " - - [" << CLogFile::LOGTYPES::PUTTIME << "] \""
<< HeaderList.find(":method")->second << " " << HeaderList.find(":path")->second << (httpVers == 2 ? " HTTP/2." : " HTTP/1.") << strHttpVersion << "\" " << to_string(iRespCode) << " - \""
<< (HeaderList.find("referer") != end(HeaderList) ? HeaderList.find("referer")->second : "-") << "\" \""
<< (HeaderList.find("user-agent") != end(HeaderList) ? HeaderList.find("user-agent")->second : "-") << "\""
<< CLogFile::LOGTYPES::END;
}
CLogFile::GetInstance(m_vHostParam[szHost].m_strErrLog).WriteToLog("[", CLogFile::LOGTYPES::PUTTIME, "] [error] [client ", soMetaDa.strIpClient, "] ", RespText.find(iRespCode) != end(RespText) ? RespText.find(iRespCode)->second : "");
}
void CHttpServ::DoAction(const MetaSocketData soMetaDa, const uint8_t httpVers, const uint32_t nStreamId, STREAMLIST& StreamList, STREAMSETTINGS& tuStreamSettings, mutex& pmtxStream, RESERVEDWINDOWSIZE& maResWndSizes, function<size_t(uint8_t*, size_t, int, int, const HeadList&, uint64_t)> BuildRespHeader, atomic<bool>& patStop, mutex& pmtxReqdata, deque<unique_ptr<char[]>>& vecData, deque<AUTHITEM>& lstAuthInfo)
{
const static unordered_map<string, int> arMethoden = { {"GET", 0}, {"HEAD", 1}, {"POST", 2}, {"OPTIONS", 3} };
function<void()> fuExitDoAction = [&]()
{
if (httpVers == 2)
{
const lock_guard<mutex> lock(pmtxStream);
const auto StreamItem = StreamList.find(nStreamId);
if (StreamItem != end(StreamList))