-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFastCgi.cpp
More file actions
1120 lines (944 loc) · 41.6 KB
/
FastCgi.cpp
File metadata and controls
1120 lines (944 loc) · 41.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
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
*/
// http://www.mit.edu/~yandros/doc/specs/fcgi-spec.html
#if !defined(NOMINMAX)
#define NOMINMAX
#endif
#include <fstream>
#include <algorithm>
#include "FastCgi.h"
#if !defined(_WIN32) && !defined(_WIN64)
#include <regex>
#include <codecvt>
#include <string.h>
#include <unistd.h>
#include <spawn.h>
#include <sys/wait.h>
#define _environ environ
extern void OutputDebugString(const wchar_t* pOut);
extern void OutputDebugStringA(const char* pOut);
static const std::vector<std::string> vEnvFilter{"USER=", "HOME="};
#else
static const std::vector<std::string> vEnvFilter{"COMPUTERNAME=","HOMEDRIVE=","HOMEPATH=","USERNAME=","USERPROFILE=","SystemRoot=","TMP=","TEMP=","Path="};
#endif
using namespace std;
using namespace std::placeholders;
#define FCGI_BEGIN_REQUEST 1
#define FCGI_ABORT_REQUEST 2
#define FCGI_END_REQUEST 3
#define FCGI_PARAMS 4
#define FCGI_STDIN 5
#define FCGI_STDOUT 6
#define FCGI_STDERR 7
#define FCGI_DATA 8
#define FCGI_GET_VALUES 9
#define FCGI_GET_VALUES_RESULT 10
#define FCGI_UNKNOWN_TYPE 11
#define FCGI_MAXTYPE (FCGI_UNKNOWN_TYPE)
#define FCGI_RESPONDER 1
#define FCGI_AUTHORIZER 2
#define FCGI_FILTER 3
#define FCGI_KEEP_CONN 1
/*
* Values for protocolStatus component of FCGI_EndRequestBody
*/
#define FCGI_REQUEST_COMPLETE 0
#define FCGI_CANT_MPX_CONN 1
#define FCGI_OVERLOADED 2
#define FCGI_UNKNOWN_ROLE 3
/*
* Variable names for FCGI_GET_VALUES / FCGI_GET_VALUES_RESULT records
*/
#define FCGI_MAX_CONNS "FCGI_MAX_CONNS"
#define FCGI_MAX_REQS "FCGI_MAX_REQS"
#define FCGI_MPXS_CONNS "FCGI_MPXS_CONNS"
typedef struct {
uint8_t version;
uint8_t type;
uint8_t requestIdB1;
uint8_t requestIdB0;
uint8_t contentLengthB1;
uint8_t contentLengthB0;
uint8_t paddingLength;
uint8_t reserved;
} FCGI_Header;
typedef struct {
unsigned char roleB1;
unsigned char roleB0;
unsigned char flags;
unsigned char reserved[5];
} FCGI_BeginRequestBody;
typedef struct {
FCGI_Header header;
FCGI_BeginRequestBody body;
} FCGI_BeginRequestRecord;
typedef struct {
unsigned char appStatusB3;
unsigned char appStatusB2;
unsigned char appStatusB1;
unsigned char appStatusB0;
unsigned char protocolStatus;
unsigned char reserved[3];
} FCGI_EndRequestBody;
typedef struct {
FCGI_Header header;
FCGI_EndRequestBody body;
} FCGI_EndRequestRecord;
FastCgiClient::FastCgiClient() noexcept : m_bConnected(false), m_cClosed(2), m_usResquestId(0), m_nCountCurRequest(0), m_hProcess(Null)
{
m_FCGI_MAX_CONNS = UINT32_MAX;
m_FCGI_MAX_REQS = UINT32_MAX;
m_FCGI_MPXS_CONNS = 0;
}
FastCgiClient::FastCgiClient(const wstring& strProcessPath) : m_bConnected(false), m_cClosed(2), m_usResquestId(0), m_nCountCurRequest(0), m_strProcessPath(strProcessPath), m_hProcess(Null)
{
m_FCGI_MAX_CONNS = UINT32_MAX;
m_FCGI_MAX_REQS = UINT32_MAX;
m_FCGI_MPXS_CONNS = 0;
StartFcgiProcess();
}
FastCgiClient::FastCgiClient(FastCgiClient&& src) noexcept : m_bConnected(false), m_cClosed(2), m_usResquestId(0), m_nCountCurRequest(0), m_hProcess(Null)
{
swap(m_pSocket, src.m_pSocket);
swap(m_usResquestId, src.m_usResquestId);
swap(m_lstRequest, src.m_lstRequest);
swap(m_nCountCurRequest, src.m_nCountCurRequest);
swap(m_bConnected, src.m_bConnected);
//swap(m_cClosed, src.m_cClosed);
swap(m_strRecBuf, src.m_strRecBuf);
swap(m_FCGI_MAX_CONNS, src.m_FCGI_MAX_CONNS);
swap(m_FCGI_MAX_REQS, src.m_FCGI_MAX_REQS);
swap(m_FCGI_MPXS_CONNS, src.m_FCGI_MPXS_CONNS);
swap(m_strProcessPath, src.m_strProcessPath);
swap(m_hProcess, src.m_hProcess);
}
FastCgiClient::~FastCgiClient() noexcept
{
if (m_pSocket != nullptr)
{
if (m_cClosed == 0)
{
m_pSocket->BindFuncBytesReceived(static_cast<function<void(TcpSocket* const)>>(nullptr));
m_pSocket->BindErrorFunction(static_cast<function<void(BaseSocket* const)>>(nullptr));
m_pSocket->BindCloseFunction(static_cast<function<void(BaseSocket* const)>>(nullptr));
m_pSocket->Close();
m_pSocket.reset();
}
}
if (m_hProcess != Null)
{
#if defined(_WIN32) || defined(_WIN64)
TerminateProcess(m_hProcess, 0);
CloseHandle(m_hProcess);
#else
kill(m_hProcess, SIGTERM);
sleep(2); // The process has 2 sec. to shut down until we kill him hard
kill(m_hProcess, SIGKILL);
#endif
}
}
uint32_t FastCgiClient::Connect(const string strIpServer, uint16_t usPort, bool bSecondConnection/* = false*/)
{
while ((m_cClosed & 2) != 2)
this_thread::sleep_for(chrono::milliseconds(10));
m_pSocket = make_unique<TcpSocket>();
m_pSocket->BindFuncConEstablished(static_cast<function<void(TcpSocket* const)>>(bind(&FastCgiClient::Connected, this, _1)));
m_pSocket->BindFuncBytesReceived(static_cast<function<void(TcpSocket* const)>>(bind(&FastCgiClient::DatenEmpfangen, this, _1)));
m_pSocket->BindErrorFunction(static_cast<function<void(BaseSocket* const)>>(bind(&FastCgiClient::SocketError, this, _1)));
m_pSocket->BindCloseFunction(static_cast<function<void(BaseSocket* const)>>(bind(&FastCgiClient::SocketClosing, this, _1)));
m_bConnected = false;
if (m_pSocket->Connect(strIpServer.c_str(), usPort) == true)
{
mutex mxConnected;
unique_lock<mutex> lock(mxConnected);
m_cvConnected.wait(lock, [&]() noexcept { return m_bConnected; });
// If no error we get the fstcgi parameter of the application server
if (m_pSocket->GetErrorNo() == 0 && bSecondConnection == false)
{
m_bConnected = false;
basic_string<uint8_t> qBuf(128, 0);
FCGI_Header* pHeader = reinterpret_cast<FCGI_Header*>(&qBuf[0]);
pHeader->version = 1;
pHeader->type = FCGI_GET_VALUES;
FromShort(&pHeader->requestIdB1, 0);
pHeader->paddingLength = 0;
pHeader->reserved = 0;
uint8_t* pContent = &qBuf[sizeof(FCGI_Header)];
uint16_t nContentLen = 0;
nContentLen += AddNameValuePair(&pContent, FCGI_MAX_CONNS, strlen(FCGI_MAX_CONNS), "", 0);
nContentLen += AddNameValuePair(&pContent, FCGI_MAX_REQS, strlen(FCGI_MAX_REQS), "", 0);
nContentLen += AddNameValuePair(&pContent, FCGI_MPXS_CONNS, strlen(FCGI_MPXS_CONNS), "", 0);
FromShort(&pHeader->contentLengthB1, nContentLen);
pHeader->paddingLength = (8 - (nContentLen % 8)) & 7;
m_pSocket->Write(&qBuf[0], sizeof(FCGI_Header) + nContentLen + pHeader->paddingLength);
// wait until the answer is here
if (m_cvConnected.wait_for(lock, chrono::milliseconds(500), [&]() noexcept { return m_bConnected; }) == false) // Timeout
return 0;
m_pSocket->Close();
return Connect(strIpServer, usPort, true);
}
return m_pSocket->GetErrorNo() == 0 ? 1 : 0;
}
return 0;
}
void FastCgiClient::Connected(TcpSocket* const /*pTcpSocket*/) noexcept
{
m_cClosed = 0;
m_bConnected = true;
m_cvConnected.notify_all();
}
void FastCgiClient::DatenEmpfangen(TcpSocket* const pTcpSocket)
{
size_t nAvailable = pTcpSocket->GetBytesAvailable();
if (nAvailable == 0)
{
pTcpSocket->Close();
return;
}
auto spBuffer = make_unique<uint8_t[]>(nAvailable + 1 + m_strRecBuf.size());
if (m_strRecBuf.size() > 0)
copy(begin(m_strRecBuf), end(m_strRecBuf), &spBuffer[0]);
size_t nRead = pTcpSocket->Read(&spBuffer[m_strRecBuf.size()], nAvailable);
if (nRead > 0)
{
nRead += m_strRecBuf.size();
m_strRecBuf.clear();
nAvailable = nRead; // Merken
FCGI_Header* pHeader = reinterpret_cast<FCGI_Header*>(&spBuffer[0]);
while (nRead >= sizeof(FCGI_Header) && pHeader->version == 1)
{
const uint16_t nRequestId = ToShort(&pHeader->requestIdB1);
m_mxReqList.lock();
auto itReqParam = m_lstRequest.find(nRequestId);
m_mxReqList.unlock();
if (pHeader->type == FCGI_GET_VALUES_RESULT && nRequestId == 0)
{
uint16_t nContentLen = ToShort(&pHeader->contentLengthB1);
uint8_t* pContent = &spBuffer[sizeof(FCGI_Header)];
if (sizeof(FCGI_Header) + nContentLen + pHeader->paddingLength > nRead)
break;
nRead -= sizeof(FCGI_Header) + nContentLen + pHeader->paddingLength;
while (nContentLen != 0)
{
const uint32_t nVarNameLen = ToNumber(&pContent, nContentLen);
const uint32_t nVarValueLen = ToNumber(&pContent, nContentLen);
string strVarName, strVarValue;
if (nVarNameLen > 0)
strVarName = string(reinterpret_cast<char*>(pContent), nVarNameLen), pContent += nVarNameLen, nContentLen -= static_cast<uint16_t>(nVarNameLen);
if (nVarValueLen > 0)
strVarValue = string(reinterpret_cast<char*>(pContent), nVarValueLen), pContent += nVarValueLen, nContentLen -= static_cast<uint16_t>(nVarValueLen);
try
{
if (strVarName == FCGI_MAX_CONNS)
m_FCGI_MAX_CONNS = stoul(strVarValue);
if (strVarName == FCGI_MAX_REQS)
m_FCGI_MAX_REQS = stoul(strVarValue);
if (strVarName == FCGI_MPXS_CONNS)
m_FCGI_MPXS_CONNS = stoul(strVarValue);
}
catch (const std::exception& /*ex*/)
{ // In case of wrong digit strings we leave de default settings
}
}
pHeader = reinterpret_cast<FCGI_Header*>(&pContent[pHeader->paddingLength]);
m_bConnected = true;
m_cvConnected.notify_all();
}
else if ((pHeader->type == FCGI_STDOUT || pHeader->type == FCGI_STDERR) && nRequestId != 0)
{
const uint16_t nContentLen = ToShort(&pHeader->contentLengthB1);
unsigned char* pContent = reinterpret_cast<unsigned char*>(pHeader) + sizeof(FCGI_Header);
if (sizeof(FCGI_Header) + nContentLen + pHeader->paddingLength > nRead)
break;
if (nContentLen > 0 && itReqParam != end(m_lstRequest) && itReqParam->second.bIsAbort == false)
{
if (pHeader->type == FCGI_STDOUT)
itReqParam->second.fnDataOutput(nRequestId, pContent, nContentLen, itReqParam->second.vpCbParam);
else
itReqParam->second.strRecBuf = string(reinterpret_cast<char*>(pContent), nContentLen);
}
nRead -= sizeof(FCGI_Header) + nContentLen + pHeader->paddingLength;
pHeader = reinterpret_cast<FCGI_Header*>(&pContent[nContentLen + pHeader->paddingLength]);
}
else if(pHeader->type == FCGI_END_REQUEST && nRequestId != 0)
{
if (sizeof(FCGI_EndRequestRecord) + pHeader->paddingLength > nRead)
break;
FCGI_EndRequestRecord* pRecord = reinterpret_cast<FCGI_EndRequestRecord*>(pHeader);
//uint16_t nContentLen = ToShort(&pRecord->header.contentLengthB1);
nRead -= sizeof(FCGI_EndRequestRecord) + pHeader->paddingLength;
pHeader = reinterpret_cast<FCGI_Header*>(pRecord + sizeof(FCGI_EndRequestRecord) + pHeader->paddingLength);
m_mxReqList.lock();
itReqParam = m_lstRequest.find(nRequestId);
if (itReqParam != end(m_lstRequest))
{
if (itReqParam->second.strRecBuf.empty() == false)
itReqParam->second.fnDataOutput(nRequestId, reinterpret_cast<unsigned char*>(&itReqParam->second.strRecBuf[0]), static_cast<uint16_t>(itReqParam->second.strRecBuf.size()), itReqParam->second.vpCbParam);
if (itReqParam->second.pbReqEnde != nullptr)
*itReqParam->second.pbReqEnde = true;
if (itReqParam->second.pcvReqEnd != nullptr)
itReqParam->second.pcvReqEnd->notify_all();
if (itReqParam->second.bIsAbort == false && m_nCountCurRequest >= 1)
m_nCountCurRequest--;
m_lstRequest.erase(itReqParam);
}
m_mxReqList.unlock();
}
else
{
OutputDebugStringA(string("Record Typ = " + to_string(static_cast<int>(pHeader->type)) + " empfangen\r\n").c_str());
break;
}
}
if (nRead > 0)
m_strRecBuf = string(reinterpret_cast<char*>(&spBuffer[nAvailable - nRead]), nRead);
}
}
void FastCgiClient::SocketError(BaseSocket* const pBaseSocket)
{
m_cClosed = 1;
pBaseSocket->Close();
}
void FastCgiClient::SocketClosing(BaseSocket* const pBaseSocket)
{
if (m_bConnected == false)
{
m_bConnected = true;
m_cvConnected.notify_all();
}
if (m_pSocket.get() == pBaseSocket && reinterpret_cast<TcpSocket*>(pBaseSocket)->GetBytesAvailable() > 0)
DatenEmpfangen(reinterpret_cast<TcpSocket*>(pBaseSocket));
m_mxReqList.lock();
for (REQLIST::iterator iter = begin(m_lstRequest); iter != end(m_lstRequest); ++iter)
{
if (iter->second.strRecBuf.empty() == false)
iter->second.fnDataOutput(iter->first, reinterpret_cast<unsigned char*>(&iter->second.strRecBuf[0]), static_cast<uint16_t>(iter->second.strRecBuf.size()), iter->second.vpCbParam);
if (iter->second.pbReqEnde != nullptr)
*iter->second.pbReqEnde = true;
if (iter->second.pcvReqEnd != nullptr)
iter->second.pcvReqEnd->notify_all();
}
m_lstRequest.clear();
m_nCountCurRequest = 0;
m_strRecBuf.clear();
m_mxReqList.unlock();
m_cClosed |= 2;
}
uint16_t FastCgiClient::SendRequest(vector<pair<string, string>>& vCgiParam, condition_variable* pcvReqEnd, bool* pbReqEnde, FN_OUTPUT fnDataOutput, void* vpCbParam/* = nullptr*/)
{
m_mxReqList.lock();
if (IsConnected() == false || m_nCountCurRequest >= m_FCGI_MAX_REQS)
{
m_mxReqList.unlock();
return 0;
}
if (m_FCGI_MPXS_CONNS == 0 && m_lstRequest.size() > 0)
{
m_mxReqList.unlock();
return 0;
}
++m_nCountCurRequest;
if (m_usResquestId > 65530)
m_usResquestId = 0;
++m_usResquestId;
m_lstRequest.emplace(m_usResquestId, REQPARAM({ fnDataOutput, vpCbParam, pcvReqEnd, pbReqEnde, "", false }));
const uint16_t nRetValue = m_usResquestId;
m_mxReqList.unlock();
auto uqBuf = make_unique<uint8_t[]>(16384);
// Start Record senden
FCGI_BeginRequestRecord* pRecord = reinterpret_cast<FCGI_BeginRequestRecord*>(&uqBuf[0]);
pRecord->header.version = 1;
pRecord->header.type = FCGI_BEGIN_REQUEST;
FromShort(&pRecord->header.requestIdB1, nRetValue);
FromShort(&pRecord->header.contentLengthB1, sizeof(FCGI_BeginRequestBody));
pRecord->header.paddingLength = 0;
pRecord->header.reserved = 0;
FromShort(&pRecord->body.roleB1, FCGI_RESPONDER);
pRecord->body.flags = FCGI_KEEP_CONN;
m_pSocket->Write(pRecord, sizeof(FCGI_BeginRequestRecord));
// Header Record senden
FCGI_Header* pHeader = reinterpret_cast<FCGI_Header*>(&uqBuf[0]);
pHeader->type = FCGI_PARAMS;
unsigned char* pContent = &uqBuf[sizeof(FCGI_Header)];
uint16_t nContentLen = 0;
for (auto& item : vCgiParam)
{
nContentLen += AddNameValuePair(&pContent, item.first.c_str(), item.first.size(), item.second.c_str(), item.second.size());
if (nContentLen > 16300)
{
break;
}
}
FromShort(&pHeader->contentLengthB1, nContentLen);
pHeader->paddingLength = (8 - (nContentLen % 8)) & 7;
std::fill_n(pContent, pHeader->paddingLength, 0);
m_pSocket->Write(pHeader, sizeof(FCGI_Header) + nContentLen + pHeader->paddingLength);
// End of Header Record senden
FromShort(&pHeader->contentLengthB1, 0);
pHeader->paddingLength = 0;
m_pSocket->Write(pHeader, sizeof(FCGI_Header));
return nRetValue;
}
void FastCgiClient::SendRequestData(const uint16_t nRequestId, const char* szBuffer, const uint32_t nBufLen)
{
uint32_t nMaxLen = min(nBufLen, static_cast<uint32_t>(0x7fff));
auto uqBuf = make_unique<uint8_t[]>(nMaxLen + sizeof(FCGI_Header) + 8);
// Start Record senden
FCGI_Header* pHeader = reinterpret_cast<FCGI_Header*>(&uqBuf[0]);
pHeader->version = 1;
pHeader->type = FCGI_STDIN;
FromShort(&pHeader->requestIdB1, nRequestId);
pHeader->paddingLength = 0;
pHeader->reserved = 0;
if (nBufLen == 0) // Data end
{
FromShort(&pHeader->contentLengthB1, static_cast<uint16_t>(0));
m_pSocket->Write(&uqBuf[0], sizeof(FCGI_Header));
return;
}
char* pDataContent = reinterpret_cast<char*>(&uqBuf[sizeof(FCGI_Header)]);
uint32_t nOffset = 0;
while (nBufLen > nOffset)
{
nMaxLen = min(nMaxLen, nBufLen - nOffset);
copy(szBuffer + nOffset, szBuffer + nOffset + nMaxLen, pDataContent);
FromShort(&pHeader->contentLengthB1, static_cast<uint16_t>(nMaxLen));
pHeader->paddingLength = (8 - (nMaxLen % 8)) & 7;
std::fill_n(pDataContent + nMaxLen, pHeader->paddingLength, 0);
m_pSocket->Write(&uqBuf[0], sizeof(FCGI_Header) + nMaxLen + pHeader->paddingLength);
nOffset += nMaxLen;
}
}
bool FastCgiClient::AbortRequest(uint16_t nRequestId)
{
string caBuffer(sizeof(FCGI_Header), 0);
// Header Record senden
FCGI_Header* pHeader = reinterpret_cast<FCGI_Header*>(&caBuffer[0]);
pHeader->version = 1;
pHeader->type = FCGI_ABORT_REQUEST;
FromShort(&pHeader->contentLengthB1, 0);
FromShort(&pHeader->requestIdB1, nRequestId);
pHeader->paddingLength = 0;
pHeader->reserved = 0;
m_pSocket->Write(pHeader, sizeof(FCGI_Header));
m_mxReqList.lock();
const auto itReqParam = m_lstRequest.find(nRequestId);
if (itReqParam != end(m_lstRequest))
itReqParam->second.bIsAbort = true;
m_mxReqList.unlock();
return true;
}
void FastCgiClient::RemoveRequest(uint16_t nRequestId)
{
m_mxReqList.lock();
const auto itReqParam = m_lstRequest.find(nRequestId);
if (itReqParam != end(m_lstRequest))
m_lstRequest.erase(itReqParam);
m_mxReqList.unlock();
}
void FastCgiClient::StartFcgiProcess()
{
#if defined(_WIN32) || defined(_WIN64)
STARTUPINFO stInfo{};
PROCESS_INFORMATION ProcInfo = { nullptr, nullptr, 0, 0 };
stInfo.cb = sizeof(STARTUPINFO);
stInfo.dwFlags = STARTF_USESHOWWINDOW;
stInfo.wShowWindow = 0/*SW_HIDE*/;
string strEntvirment;
char** aszEnv = _environ;
while (*aszEnv)
{
//OutputDebugStringA(std::string(string(*aszEnv) + "\r\n").c_str());
if (std::find_if(vEnvFilter.begin(), vEnvFilter.end(), [&](auto& strFilter) { return std::string(*aszEnv).find(strFilter) == 0 ? true : false; }) != vEnvFilter.end())
strEntvirment += string(*aszEnv) + '\0';
++aszEnv;
}
strEntvirment += '\0';
wstring strPath(m_strProcessPath);
strPath.erase(strPath.find_last_of(L"\\/") + 1); // If the backslash is not found, the entire string is deleted
if (CreateProcess(nullptr, &m_strProcessPath[0], nullptr, nullptr, FALSE, CREATE_DEFAULT_ERROR_MODE | CREATE_NEW_PROCESS_GROUP | CREATE_NO_WINDOW, &strEntvirment[0], strPath.c_str(), &stInfo, &ProcInfo) == TRUE)
{
CloseHandle(ProcInfo.hThread);
m_hProcess = ProcInfo.hProcess;
}
else
OutputDebugString(wstring(L"CreateProcess error: " + to_wstring(GetLastError()) + L"\r\n").c_str());
#else
string l_strCmd = wstring_convert<codecvt_utf8<wchar_t>, wchar_t>().to_bytes(m_strProcessPath);
const static regex rx("([^\\s\\\"]+)(?:\\s|$)|((?:[^\\s]*\\\"[^\\\"]*\\\"[^\\s\\\"]*)+)+(?:\\s|$)");
vector<string> token;
smatch mr;
while (regex_search(l_strCmd, mr, rx) == true && mr[0].matched == true)
{
token.push_back(mr[0].str());
l_strCmd.erase(0, mr[0].length());
token.back().erase(token.back().find_last_not_of("\" \t\r\n") + 1); // Trim Whitespace and " character on the right
token.back().erase(0, token.back().find_first_not_of("\" \t")); // Trim Whitespace and " character on the left
}
unique_ptr<char*[]> wargv = make_unique<char*[]>(token.size() + 1);
for (size_t n = 0; n < token.size(); ++n)
wargv[n] = &token[n][0];
wargv[token.size()] = nullptr;
vector<char*> envp;
char** aszEnv = _environ;
while (*aszEnv)
{
if (std::find_if(vEnvFilter.begin(), vEnvFilter.end(), [&](auto& strFilter) { return std::string(*aszEnv).find(strFilter) == 0 ? true : false; }) != vEnvFilter.end())
envp.push_back(*aszEnv);
++aszEnv;
}
envp.push_back(nullptr);
int iRes = posix_spawn(&m_hProcess, wargv[0], NULL, NULL, &wargv[0], &envp[0]);
if (iRes != 0)
OutputDebugString(wstring(L"posix_spawn result: " + to_wstring(iRes) + L", pid: " + to_wstring(m_hProcess) + L", errno = " + to_wstring(errno) + L"\r\n").c_str());
#endif
this_thread::sleep_for(chrono::milliseconds(500));
}
bool FastCgiClient::IsFcgiProcessActiv(size_t nCount/* = 0*/)
{
if (m_hProcess != Null)
{
#if defined(_WIN32) || defined(_WIN64)
DWORD nExitCode = STILL_ACTIVE;
if (!GetExitCodeProcess(m_hProcess, &nExitCode))
return false;
if (nExitCode == STILL_ACTIVE)
return true;
CloseHandle(m_hProcess);
#else
int nExitCode;
int status = waitpid(m_hProcess, &nExitCode, WNOHANG);
if (status == 0)
return true;
#endif
m_mxReqList.lock();
if (m_lstRequest.size() > 0)
{
for (REQLIST::iterator iter = begin(m_lstRequest); iter != end(m_lstRequest); ++iter)
{
if (iter->second.pbReqEnde != nullptr)
*iter->second.pbReqEnde = true;
if (iter->second.pcvReqEnd != nullptr)
iter->second.pcvReqEnd->notify_all();
}
m_lstRequest.clear();
m_nCountCurRequest = 0;
}
m_mxReqList.unlock();
m_cClosed |= 4;
m_hProcess = Null;
if (nCount >= 5)
return false;
StartFcgiProcess();
return IsFcgiProcessActiv(++nCount);
}
return m_strProcessPath.empty(); // If no process path is given, we return true, we assume that the process is externally controlled and running
}
uint16_t FastCgiBase::AddNameValuePair(uint8_t** pBuffer, const char* pKey, size_t nKeyLen, const char* pValue, size_t nValueLen) noexcept
{
uint16_t nRetLen = 0;
nRetLen += FromNumber(pBuffer, static_cast<uint32_t>(nKeyLen));
nRetLen += FromNumber(pBuffer, static_cast<uint32_t>(nValueLen));
for (size_t n = 0; n < nKeyLen; ++n, ++nRetLen)
*(*pBuffer)++ = *pKey++;
for (size_t n = 0; n < nValueLen; ++n, ++nRetLen)
*(*pBuffer)++ = *pValue++;
return nRetLen;
}
uint16_t FastCgiBase::ToShort(const uint8_t* const pBuffer) noexcept
{
uint16_t nResult = *pBuffer;
nResult <<= 8, nResult |= *(pBuffer + 1);
return nResult;
}
uint32_t FastCgiBase::ToNumber(uint8_t** pBuffer, uint16_t& nContentLen) noexcept
{
uint16_t nLen = 1;
uint32_t nResult = **pBuffer;
if ((**pBuffer & 0x80) == 0x80)
nResult &= 0x7F, nLen = 4;
(*pBuffer)++;
for (uint16_t n = 1; n < nLen; ++n)
nResult <<= 8, nResult |= *(*pBuffer)++;
nContentLen -= nLen;
return nResult;
}
void FastCgiBase::FromShort(uint8_t* const pBuffer, uint16_t sNumber) noexcept
{
*pBuffer = (sNumber >> 8) & 0xff;
*(pBuffer + 1) = sNumber & 0xff;
}
uint16_t FastCgiBase::FromNumber(uint8_t** pBuffer, uint32_t nNumber) noexcept
{
uint16_t nLen = 1;
if (nNumber < 128)
*(*pBuffer)++ = static_cast<uint8_t>(nNumber);
else
{
*(*pBuffer)++ = 0x80;
*(*pBuffer)++ = (nNumber >> 16) & 0xff; // Should always be 0, the largest number is 65535
*(*pBuffer)++ = (nNumber >> 8) & 0xff;
*(*pBuffer)++ = nNumber & 0xff;
nLen = 4;
}
return nLen;
}
//---------------- Server ---------------------------
template <typename Callback>
struct callback_ostreambuf : public streambuf
{
using callback_t = Callback;
callback_ostreambuf(Callback cb, void* userdata1 = nullptr, void* userdata2 = nullptr) :
callback_(cb), user_data1(userdata1), user_data2(userdata2) {}
protected:
streamsize xsputn(const char_type* s, streamsize n) override
{
return callback_(s, n, user_data1, user_data2); // returns the number of characters successfully written.
};
int_type overflow(int_type ch) override
{
return static_cast<int_type>(callback_(&ch, 1, user_data1, user_data2)); // returns the number of characters successfully written.
}
private:
Callback callback_;
void* user_data1;
void* user_data2;
};
template <typename Callback>
auto make_callback_ostreambuf(Callback cb, void* user_data1 = nullptr, void* user_data2 = nullptr)
{
return new callback_ostreambuf<Callback>(cb, user_data1, user_data2);
}
class StreamInBuffer : public streambuf
{
public:
StreamInBuffer() : m_bEof(false)
{
setg(nullptr, nullptr, nullptr);
setp(nullptr, nullptr);
}
void SetEof() noexcept {
m_bEof = true;
}
protected:
streamsize xsputn(const char_type* s, streamsize n) override
{
if (n == 0) return n;
auto pBuf = make_unique<char[]>(n);
copy(s, s + n, &pBuf[0]);
m_mxLock.lock();
m_vBuffers.emplace_back(make_tuple(move(pBuf), n));
m_mxLock.unlock();
if (pbase() == nullptr && pptr() == nullptr)
{
setg(&get<0>(m_vBuffers.front())[0], &get<0>(m_vBuffers.front())[0], &get<0>(m_vBuffers.front())[get<1>(m_vBuffers.front())]);
setp(&get<0>(m_vBuffers.front())[0], &get<0>(m_vBuffers.front())[ get<1>(m_vBuffers.front())]);
}
return n; // returns the number of characters successfully written.
};
int_type underflow() override
{
if (gptr() == egptr() && m_vBuffers.size() > 0)
{
m_mxLock.lock();
m_vBuffers.erase(m_vBuffers.begin());
m_mxLock.unlock();
}
while (gptr() == egptr() && m_vBuffers.size() == 0 && m_bEof == false)
this_thread::sleep_for(chrono::milliseconds(1));
if (gptr() == egptr() && m_vBuffers.size() == 0 && m_bEof == true)
return traits_type::eof();
setg(&get<0>(m_vBuffers.front())[0], &get<0>(m_vBuffers.front())[0], &get<0>(m_vBuffers.front())[get<1>(m_vBuffers.front())]);
setp(&get<0>(m_vBuffers.front())[0], &get<0>(m_vBuffers.front())[get<1>(m_vBuffers.front())]);
const int_type nRet = traits_type::to_int_type(*gptr());
return nRet;
}
private:
mutex m_mxLock;
bool m_bEof;
vector<tuple<unique_ptr<char[]>, streamsize>> m_vBuffers;
};
FastCgiServer::FastCgiServer(const string strBindAddr, const uint16_t sPort, FN_DOACTION fnCallBack) : m_strBindAddr(strBindAddr), m_sPort(sPort), m_fnDoAction(fnCallBack)
{
}
FastCgiServer::~FastCgiServer()
{
while (m_Connections.size() > 0)
this_thread::sleep_for(chrono::milliseconds(10));
}
bool FastCgiServer::Start()
{
m_pSocket = make_unique<TcpServer>();
m_pSocket->BindNewConnection(static_cast<function<void(const vector<TcpSocket*>&)>>(bind(&FastCgiServer::OnNewConnection, this, _1)));
m_pSocket->BindErrorFunction(static_cast<function<void(BaseSocket* const)>>(bind(&FastCgiServer::OnSocketError, this, _1)));
return m_pSocket->Start(m_strBindAddr.c_str(), m_sPort);
}
bool FastCgiServer::Stop()
{
if (m_pSocket != nullptr)
{
m_pSocket->Close();
m_pSocket.reset(nullptr);
}
m_mxConnections.lock();
for (auto& item : m_Connections)
{
item.first->Close();
}
m_mxConnections.unlock();
return true;
}
int FastCgiServer::GetError()
{
if (m_pSocket != nullptr)
return m_pSocket->GetErrorNo();
return -1;
}
void FastCgiServer::OnNewConnection(const vector<TcpSocket*>& vNewConnections)
{
vector<TcpSocket*> vCache;
for (auto& pSocket : vNewConnections)
{
if (pSocket != nullptr)
{
pSocket->BindFuncBytesReceived(static_cast<function<void(TcpSocket* const)>>(bind(&FastCgiServer::OnDataReceived, this, _1)));
pSocket->BindErrorFunction(static_cast<function<void(BaseSocket* const)>>(bind(&FastCgiServer::OnSocketError, this, _1)));
pSocket->BindCloseFunction(static_cast<function<void(BaseSocket* const)>>(bind(&FastCgiServer::OnSocketClosing, this, _1)));
vCache.push_back(pSocket);
}
}
if (vCache.size())
{
m_mxConnections.lock();
for (auto& pSocket : vCache)
{
m_Connections.emplace(pSocket, REQUEST());
pSocket->StartReceiving();
}
m_mxConnections.unlock();
}
}
void FastCgiServer::OnDataReceived(TcpSocket* pSocket)
{
const size_t nAvailable = pSocket->GetBytesAvailable();
if (nAvailable == 0)
{
pSocket->Close();
return;
}
auto spBuffer = make_unique<uint8_t[]>(nAvailable);
size_t nRead = pSocket->Read(&spBuffer[0], nAvailable);
if (nRead > 0)
{
m_mxConnections.lock();
const auto itConnection = m_Connections.find(pSocket);
if (itConnection != end(m_Connections))
{
FCGI_Header* pHeader = reinterpret_cast<FCGI_Header*>(&spBuffer[0]);
while (nRead > 0)
{
const uint16_t nRequestId = ToShort(&pHeader->requestIdB1);
uint16_t nContentLen = ToShort(&pHeader->contentLengthB1);
const uint8_t nPaddingLen = pHeader->paddingLength;
uint8_t* pContent = reinterpret_cast<uint8_t*>(pHeader) + sizeof(FCGI_Header);
FCGI_Header* pNextHeader = reinterpret_cast<FCGI_Header*>(pContent + nContentLen + nPaddingLen);
const auto itRequest = itConnection->second.find(nRequestId); // Get the Request from the Request ID
if (nRead < sizeof(FCGI_Header) + nContentLen + nPaddingLen)
{
pSocket->PutBackRead(pHeader, nRead);
m_mxConnections.unlock();
return;
}
nRead -= sizeof(FCGI_Header) + nContentLen + nPaddingLen;
switch (pHeader->type)
{
case FCGI_GET_VALUES:
if (itRequest != end(itConnection->second))
{
pSocket->Close();
nRead = 0;
}
else
{
vector<string> vstrVariablen;
while (nContentLen != 0)
{
const uint32_t nVarNameLen = ToNumber(&pContent, nContentLen);
const uint32_t nVarValueLen = ToNumber(&pContent, nContentLen);
const string strVarName, strVarValue;
if (nVarNameLen > 0)
vstrVariablen.push_back(string(reinterpret_cast<char*>(pContent), nVarNameLen)), pContent += nVarNameLen, nContentLen -= static_cast<uint16_t>(nVarNameLen);
pContent += nVarValueLen, nContentLen -= static_cast<uint16_t>(nVarValueLen);
}
auto spBufWrite = make_unique<uint8_t[]>(1024); // Reserve new buffer with more room
FCGI_Header* pNewHeader = reinterpret_cast<FCGI_Header*>(&spBufWrite[0]); // get the pointer to the header
copy(pHeader, pHeader + 1/*sizeof(FCGI_Header)*/, pNewHeader); // copy the received header to your new buffer
pContent = &spBufWrite[sizeof(FCGI_Header)];
nContentLen = 0;
for (const auto& strVariable : vstrVariablen)
{
if (strVariable == FCGI_MAX_CONNS)
nContentLen += AddNameValuePair(&pContent, strVariable.c_str(), strVariable.size(), "10", 2);
else if (strVariable == FCGI_MAX_REQS)
nContentLen += AddNameValuePair(&pContent, strVariable.c_str(), strVariable.size(), "50", 2);
else if (strVariable == FCGI_MPXS_CONNS)
nContentLen += AddNameValuePair(&pContent, strVariable.c_str(), strVariable.size(), "1", 1);
}
FromShort(&pNewHeader->contentLengthB1, nContentLen);
pNewHeader->type = FCGI_GET_VALUES_RESULT;
pNewHeader->paddingLength = (8 - (nContentLen % 8)) & 7;
std::fill_n(pContent, pNewHeader->paddingLength, 0);
pSocket->Write(&spBufWrite[0], sizeof(FCGI_Header) + nContentLen + pNewHeader->paddingLength);
}
pHeader = pNextHeader;
break;
case FCGI_BEGIN_REQUEST:
if (itRequest != end(itConnection->second))
{
pSocket->Close();
nRead = 0;
}
else
{
FCGI_BeginRequestRecord* pRecord = reinterpret_cast<FCGI_BeginRequestRecord*>(&spBuffer[0]);
itConnection->second.emplace(nRequestId, REQUESTPARAM());
ToShort(&pRecord->body.roleB1); // FCGI_RESPONDER , FCGI_AUTHORIZER , FCGI_FILTER
//pRecord->body.flags; // FCGI_KEEP_CONN
}
pHeader = pNextHeader;
break;
case FCGI_PARAMS:
if (itRequest == end(itConnection->second) || itRequest->second.nState != 0)
{
pSocket->Close();
nRead = 0;
}
else if (nContentLen == 0)
{
itRequest->second.nState++;
itRequest->second.obuf = make_unique<streambuf*>(make_callback_ostreambuf([&](const void* buf, std::streamsize sz, void* user_data1, void* user_data2) -> std::streamsize
{
TcpSocket* pSock = reinterpret_cast<TcpSocket*>(user_data2);
auto pSendBuffer = make_unique<uint8_t[]>(sizeof(FCGI_Header) + 16368 + 8);
FCGI_Header* pHeader = reinterpret_cast<FCGI_Header*>(&pSendBuffer[0]);
pHeader->version = 1;
pHeader->type = FCGI_STDOUT;
FromShort(&pHeader->requestIdB1, static_cast<uint16_t>(reinterpret_cast<size_t>(user_data1)));
pHeader->paddingLength = 0;
pHeader->reserved = 0;
const size_t nAnzahl = sz;
size_t nOffset = 0;
while (nAnzahl - nOffset != 0)
{