forked from 3s3s/3s3s.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StartupInfo.h
596 lines (506 loc) · 15.1 KB
/
StartupInfo.h
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
#ifndef _startupinfo
#define _startupinfo
#include "log.h"
#include <unordered_map>
#include "simple_server.h"
using namespace curl;
extern CUrl gCURL;
namespace startup
{
enum ACTION
{
A_START,
A_CONTINUE,
A_END
};
class CRegisteredDNS
{
string m_strDNS, m_strIP;
int m_nPort, m_nSSLPort;
public:
CRegisteredDNS(const string strDNS, const string strIP, int nPort, int nSSLPort) :
m_strDNS(strDNS), m_strIP(strIP), m_nPort(nPort), m_nSSLPort(nSSLPort) {}
const string DNS() const {return m_strDNS;}
const string IP() const {return m_strIP;}
const int Port() const {return m_nPort;}
const int SSLPort() const {return m_nSSLPort;}
};
class CPostContent
{
map<string, vector<string> > m_mapHeader;
vector<BYTE> m_vBody;
public:
CPostContent(string strHeader, vector<BYTE> vBuffer) :
m_vBody(vBuffer)
{
if (strHeader.length())
ParseHeader(strHeader);
}
vector<BYTE> GetBody()
{
return m_vBody;
}
static size_t ParseLine(int nPos, string &strLines, string &strFirst, string &strSecond)
{
size_t nRet = strLines.length();
string substr = strLines.substr(nPos);
if (substr.length() < 4)
return nRet;
int nEnd = substr.find("\r\n");
if ((nEnd == substr.npos) || (nEnd == 0))
return nRet;
nRet = nPos + nEnd+2;
string strLine = substr.substr(0, nEnd);
int nSpace = strLine.find(" ");
if (nPos > 0)
nSpace = strLine.find(": ");
if (nSpace != strLine.npos)
strFirst = strLine.substr(0, nSpace);
else
nSpace = -1;
string substr2 = strLine.substr(nSpace+1);
if (nPos != 0)
{
strSecond = substr2;
return nRet;
}
nSpace = substr2.find(" ");
if (nSpace == substr2.npos)
strSecond = substr2;
else
strSecond = substr2.substr(0, nSpace);
return nRet;
}
void AddBuffer(vector<BYTE> vBuffer)
{
m_vBody.insert(m_vBody.end(), vBuffer.begin(), vBuffer.end());
}
void EraseBody()
{
m_vBody.clear();
}
vector<string> ParseSecond(string strSecond)
{
vector<string> ret;
//strSecond.erase(remove_if(strSecond.begin(), strSecond.end(), isspace), strSecond.end());
string str = strSecond;
int nPos = 0;
string strNext = str;
while (nPos != str.npos)
{
str = strNext;
nPos = str.find(";");
if (nPos != str.npos)
ret.push_back(str.substr(1, nPos-1));
else
{
ret.push_back(str.substr(1));
break;
}
strNext = str.substr(nPos+1);
}
return ret;
}
string GetName()
{
string strRet;
if (m_mapHeader.find("Content-Disposition") == m_mapHeader.end())
return strRet;
vector<string> values = m_mapHeader["Content-Disposition"];
for (size_t n=0; n<values.size(); n++)
{
int nPos = values[n].find("name=");
if (nPos == values[n].npos)
continue;
return values[n].substr(nPos+5);
}
return strRet;
}
void ParseHeader(string strHeader)
{
size_t nPos = strHeader.find("\r\n")+2;
while (1)
{
string strFirst, strSecond;
nPos = ParseLine(nPos, strHeader, strFirst, strSecond);
if (nPos == strHeader.length())
break;
vector<string> vSecond = ParseSecond(strSecond);
m_mapHeader[strFirst] = vSecond;
}
}
};
class CClient
{
public:
virtual bool ContinueGet(vector<BYTE> *pOutBuffer) = 0;
const bool IsSSL() const {return m_bIsSSL;}
protected:
bool m_bAllRecieved;
unsigned long long m_llContentLength, m_llCurrentBodyLength;
const bool IsAllRecieved() const {return m_bAllRecieved;}
const string GetIP() const {return m_strIP;}
const map<string, string> &GetMapNameToValue() const {return m_mapNameToValue;}
const string GetBrowserLang()
{
string strVal;
if (m_mapNameToValue.find("Accept-Language") != m_mapNameToValue.end())
strVal = m_mapNameToValue["Accept-Language"];
if (m_mapNameToValue.find("accept-language") != m_mapNameToValue.end())
strVal = m_mapNameToValue["accept-language"];
if (strVal.find("ru") != strVal.npos)
return "ru";
if (strVal.find("en") != strVal.npos)
return "en";
if (strVal.find("jp") != strVal.npos)
return "jp";
return "";
}
private:
int m_nID;
ACTION m_nAction;
bool m_bNeadDelete, m_bGzip;
vector<string> m_vQueryes;
string m_strMethod, m_strURI, m_strContentType;
string m_strBoundary, m_strIP, m_strQuery;
map<string, string> m_mapNameToValue;
bool m_bIsSSL;
vector<CPostContent> m_PostContent;
vector<BYTE> m_vUnparsedPost;
void OnAllRecieved()
{
m_bAllRecieved = true;
if ((m_strContentType.find("application/x-www-form-urlencoded") != m_strContentType.npos) && (!m_vQueryes.size()))
{
vector<BYTE> vBody(GetPostBody("", true));
vBody.push_back(0);
m_vQueryes = simple_server::CHttpHeader::ParseQuery((const char *)&(vBody[0]));
}
}
string GetHeaderAndTail(vector<BYTE> *pBody)
{
string strRet;
pBody->clear();
if (m_vUnparsedPost.size() < m_strBoundary.length())
return strRet;
//Èùåì ãðàíèöó â ïîñòóïèâøèõ äàííûõ
vector<BYTE>::iterator itStart = m_vUnparsedPost.end(), itEnd;
if (m_strBoundary.length())
itStart = search (
m_vUnparsedPost.begin(), m_vUnparsedPost.end(),
m_strBoundary.c_str(), m_strBoundary.c_str()+m_strBoundary.length());
//int nPos = itStart-m_vUnparsedPost.begin();
if (itStart == m_vUnparsedPost.end())
{
//ãðàíèöà íå íàéäåíà
//Âîçâðàùàåì ÷àñòü õâîñòà, êàê òåëî
vector<BYTE> vBody(m_vUnparsedPost.begin(), m_vUnparsedPost.end()-m_strBoundary.length());
if (!vBody.size())
return strRet;
vector<BYTE> vTail(m_vUnparsedPost.end()-m_strBoundary.length(), m_vUnparsedPost.end());
*pBody = vBody;
m_vUnparsedPost = vTail;
return strRet;
}
//Ãðàíèöà íàéäåíà, ñìîòðèì - åñòü ëè íàä íåé äàííûå
vector<BYTE> vPrevBody(m_vUnparsedPost.begin(), itStart);
if (vPrevBody.size())
{
//Íàéäåíû äàííûå íàä ãðíèöåé - âîçâðàùàåì èõ áåç çàãîëîâêà
//ïîòîìó ÷òî îíè îòíîñÿòñÿ ê ïðåäûäóùåìó çàãîëîâêó
vector<BYTE> vTail(itStart, m_vUnparsedPost.end());
*pBody = vPrevBody;
m_vUnparsedPost = vTail;
return strRet;
}
//èùåì êîíåö çàãîëîâêà
const char *pszEnd = "\r\n\r\n";
itEnd = search (
itStart, m_vUnparsedPost.end(),
pszEnd, pszEnd + 4);
if (itEnd == m_vUnparsedPost.end())
{
//Êîíåö çàãîëîâêà íå íàéäåí, íàâåðíîå îí åùå íå âåñü ïðèøåë.
//Íî ïðîâåðèì, ìîæåò áûòü óæå ïðèøåë êîíåö âñåõ äàííûõ
string strEnd = m_strBoundary + "--";
itEnd = search (
itStart, m_vUnparsedPost.end(),
strEnd.c_str(), strEnd.c_str()+strEnd.length());
if (itEnd != m_vUnparsedPost.end())
{
//Äà âñå äàííûå ïðèíÿòû
OnAllRecieved();
}
return strRet;
}
//Êîíåö çàãîëîâêà íàéäåí çàïèñûâàåì åãî è õâîñò â ðàçíûå ìàññèâû
vector<BYTE> vHeader(itStart, itEnd+4);
vector<BYTE> vTail(itEnd+4, m_vUnparsedPost.end());
m_vUnparsedPost = vTail;
vHeader.push_back(0);
strRet = (const char *)(&vHeader[0]);
//Èùåì â õâîñòå ñëåäóþùóþ ãðàíèöó
if (m_vUnparsedPost.size() < m_strBoundary.length())
return strRet; //ñëèøêîì êîðîòêèé õâîñò, âîçâðàùàåì çàãîëîâîê áåç òåëà
itStart = search (
m_vUnparsedPost.begin(), m_vUnparsedPost.end(),
m_strBoundary.c_str(), m_strBoundary.c_str()+m_strBoundary.length());
if (itStart == m_vUnparsedPost.end())
{
//ñëåäóþùàÿ ãðàíèöà íå íàéäåíà
//Âîçâðàùàåì ÷àñòü õâîñòà, êàê òåëî
vector<BYTE> vBody(m_vUnparsedPost.begin(), m_vUnparsedPost.end()-m_strBoundary.length());
vector<BYTE> vLastTail(m_vUnparsedPost.end()-m_strBoundary.length(), m_vUnparsedPost.end());
*pBody = vBody;
m_vUnparsedPost = vLastTail;
return strRet;
}
vector<BYTE> vBody(m_vUnparsedPost.begin(), itStart);
vector<BYTE> vNextTail(itStart, m_vUnparsedPost.end());
*pBody = vBody;
m_vUnparsedPost = vNextTail;
return strRet;
}
void ParsePost()
{
if (m_bAllRecieved)
return;
string strHeader;
vector<BYTE> vBody;
while(!m_bAllRecieved)
{
strHeader = GetHeaderAndTail(&vBody);
m_llCurrentBodyLength += vBody.size();
if (vBody.size() || strHeader.length())
{
if (!strHeader.length())
{
//Åñëè ïðèøëî òåëî áåç çàãîëîâêà, çíà÷èò ýòî òåëî
//îòíîñèñÿ ê ïðåäûäóùåìó çàãîëîâêó
if (m_PostContent.size())
m_PostContent[m_PostContent.size()-1].AddBuffer(vBody);
else
{
//íó èëè çàãîëîâêà íå ïðåäóñìîòðåíî
CPostContent content(strHeader, vBody);
m_PostContent.push_back(content);
break;
}
}
else
{
CPostContent content(strHeader, vBody);
m_PostContent.push_back(content);
}
}
if (!vBody.size())
break;
}
if (m_llCurrentBodyLength >= m_llContentLength)
OnAllRecieved();
//vBody.resize()
//CPostContent content((const char*)(&vHeader[0]), itEnd+4);
//m_PostContent.push_back(content);
}
public:
//CVoiceFile m_VoiceFile;
//CRegisterModule m_Register;
CClient() {}
~CClient()
{
}
CClient(int nID, vector<string> &vQueryes, string strURI, unsigned long long llLength, const string strBoundary, const string strContentType,
const string strIP, const string strQuery, const map<string, string> &mapNameToValue, const bool bIsSSL) :
m_nID(nID), m_vQueryes(vQueryes), m_nAction(A_START), m_bNeadDelete(false), m_llContentLength(llLength),
m_bAllRecieved(false), m_strURI(strURI), m_strContentType(strContentType), m_llCurrentBodyLength(0),
m_strIP(strIP), m_strQuery(strQuery), m_mapNameToValue(mapNameToValue), m_bIsSSL(bIsSSL)
{
string strEncode = ((mapNameToValue.find("Accept-Encoding") == mapNameToValue.end()) ? "" : mapNameToValue.at("Accept-Encoding"));
m_bGzip = false;
if (strEncode.find("gzip") != strEncode.npos)
m_bGzip = true;
m_strMethod = (mapNameToValue.find("Method") == mapNameToValue.end()) ? "" : mapNameToValue.at("Method");
if (strBoundary.length())
m_strBoundary = "--" + strBoundary;
}
const string GetHeaderValue(const string strKey) const
{
if (m_mapNameToValue.find(strKey) == m_mapNameToValue.end())
return "";
return m_mapNameToValue.at(strKey);
}
const vector<string> GetAllHeaderValues() const
{
vector<string> vRet;
for (auto it = m_mapNameToValue.begin(); it != m_mapNameToValue.end(); ++it)
vRet.push_back(it->first + ": " + it->second + "\r\n");
return vRet;
}
virtual bool ContinuePost(vector<BYTE> *pInBuffer, vector<BYTE> *pOutBuffer)
{
m_vUnparsedPost.insert(m_vUnparsedPost.end(), pInBuffer->begin(), pInBuffer->end());
ParsePost();
return true;
}
inline void Delete()
{
m_bNeadDelete = true;
}
inline void Continue()
{
if (m_bNeadDelete)
return;
switch(m_nAction)
{
case A_START:
break;
case A_CONTINUE:
break;
case A_END:
break;
}
}
const bool NeadDelete() const {return m_bNeadDelete;}
const string GetMethod() const {return m_strMethod;}
const string GetURI() const {return m_strURI;}
const string GetQuery() const {return m_strQuery;}
const string GetHost() const
{
const string str = GetHeaderValue("Host");
if (str.length()) return str;
return GetHeaderValue("host");
}
const bool HasGzip() const {return m_bGzip;}
const std::string GetContentType() const {return m_strContentType;}
vector<BYTE> GetPostBody(string strName, bool bErase = false)
{
vector<BYTE> ret;
for (size_t n=0; n<m_PostContent.size(); n++)
{
const string strContentName = m_PostContent[n].GetName();
if (strContentName.length())
{
if (strContentName != "\""+strName+"\"")
continue;
}
else
{
if (strContentName != strName)
continue;
}
ret = m_PostContent[n].GetBody();
if (bErase)
m_PostContent[n].EraseBody();
break;
}
return ret;
}
const string GetValue(const string &strKey) const
{
string strRet;
if (NeadDelete())
return strRet;
for (size_t n=0; n<m_vQueryes.size(); n++)
{
size_t nPos = m_vQueryes[n].find("=");
if (nPos == m_vQueryes[n].npos)
nPos = m_vQueryes[n].find(":");
if (nPos == m_vQueryes[n].npos)
continue;
if (strKey != m_vQueryes[n].substr(0, nPos))
continue;
strRet = m_vQueryes[n].substr(nPos+1);
break;
}
return strRet;
}
const map<string, string> GetAllValues() const
{
map<string, string> ret;
if (NeadDelete())
return ret;
for (size_t n=0; n<m_vQueryes.size(); n++)
{
size_t nPos = m_vQueryes[n].find("=");
if (nPos == m_vQueryes[n].npos)
nPos = m_vQueryes[n].find(":");
if (nPos == -1)
ret[m_vQueryes[n]] = "";
else
ret[m_vQueryes[n].substr(0, nPos)] = m_vQueryes[n].substr(nPos+1);
}
return ret;
}
};
template <class clientType>
class CStartupInfo// : public IStartupInfo
{
unordered_map<int, clientType> m_Clients;
public:
explicit CStartupInfo() {}
CStartupInfo(const CStartupInfo& info) :
m_nPort(info.m_nPort), m_nPortSSL(info.m_nPortSSL), m_strRoot(info.m_strRoot), m_strErrors(info.m_strErrors), m_strIndex(info.m_strIndex)
{
}
CStartupInfo(int nPort, int nPortSSL, string strRoot, string strErrors, string strIndex) :
m_nPort(nPort), m_strRoot(strRoot), m_strErrors(strErrors), m_strIndex(strIndex), m_nPortSSL(nPortSSL)
{
}
inline string GetRoot() {return m_strRoot;}
inline string GetErrors() {return m_strErrors;}
const string GetIndex() const {return m_strIndex;}
inline int GetPort() {return m_nPort;}
inline int GetPortSSL() {return m_nPortSSL;}
inline void ContinuePlugins()
{
gCURL.Continue();
size_t nFirstForDelete = -1;
for (auto it = m_Clients.begin(); it != m_Clients.end(); ++it)
{
it->second.Continue();
if ((nFirstForDelete == -1) && (it->second.NeadDelete()))
{
nFirstForDelete = it->first;
}
}
if (nFirstForDelete != -1)
m_Clients.erase(nFirstForDelete);
}
inline void RegisterCGIClient(
int nClientID, vector<string> &vQueryes, string strURI, unsigned long long llLength,
const string &strBoundary, const string &strContentType, const string strIP, const string strQuery, const map<string, string> &mapNameToValue,
const bool bIsSSL)
{
EraseClient(nClientID);
m_Clients[nClientID] = clientType(nClientID, vQueryes, strURI, llLength, strBoundary, strContentType, strIP, strQuery, mapNameToValue, bIsSSL);
}
bool NeadCGI(const string &strURL, const string &strQuery, const map<string, string> &mapNameToValue) const
{
return clientType::NeadCGI(strURL, strQuery, mapNameToValue);
}
inline bool GetCGIInfo(int nClientID, vector<BYTE> *pInBuffer, vector<BYTE> *pOutBuffer)
{
//return thisPlugin.GetCGIInfo(nClientID, pInBuffer, pOutBuffer);
pOutBuffer->clear();
if ((m_Clients.find(nClientID) == m_Clients.end()) ||
(m_Clients[nClientID].NeadDelete()))
{
DEBUG_LOG("GetCGIInfo return false (client not found or nead for delete) id=%i", nClientID);
return false;
}
if (m_Clients[nClientID].GetMethod() == "POST")
return m_Clients[nClientID].ContinuePost(pInBuffer, pOutBuffer);
return m_Clients[nClientID].ContinueGet(pOutBuffer);
}
void EraseClient(int nClientID)
{
if (m_Clients.find(nClientID) == m_Clients.end())
return;
m_Clients.erase(nClientID);
}
private:
int m_nPort, m_nPortSSL;
string m_strRoot, m_strErrors, m_strIndex;
};
}
#endif