/
clienthandler.cpp
861 lines (670 loc) · 21.9 KB
/
clienthandler.cpp
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
/*
#------------------------------------------#
# #
# Todak Server 0.1 #
# Copyright (C) 2015 -2016 Azril Azam #
# #
# azrilazam@gmail.com #
# #
#------------------------------------------#
*/
#include "clienthandler.h"
#include <QDebug>
clientHandler::clientHandler(QObject *parent) : QObject(parent)
{
//this->clientTCP = new QTcpSocket(this);
this->workDir = ".";
}
clientHandler::~clientHandler()
{
}
void clientHandler::setWorkingDirectory(QString dir)
{
this->workDir = dir;
}
void clientHandler::setProcessVector(QList<ProcessStat> *PROCVECT)
{
this->PROCVECT = PROCVECT;
}
multipartArgv clientHandler::processPOSTMultiPart(QByteArray data)
{
multipartArgv rs;
qint64 loc;
QList<QByteArray> hdr;
QString tmp;
QByteArray partData;
QString argv;
QFile tmpFile;
argv.clear();
tmp.clear();
loc = 0;
partData.clear();
tmpFile.fileName().clear();
rs.argv.clear();
rs.tmpFile.clear();
//remove header and content;
loc = data.indexOf("\r\n\r\n");
if (loc < 0){
return rs;
}
//part data
partData.append(data.mid(loc + 4,data.size() - (loc + 4)));
//process header
data = data.remove(loc,data.size() - loc);
data = data.replace('\r',"");
hdr = data.split('\n');
data.clear();
//skip saving as file.. concentrate on base64 first
//save partData is size > 1024
/*if (partData.size() > 1024)
{
qDebug() << "multipart saving data as file";
tmpFile.setFileName("/tmp/" + this->getTmpRandomFileName()+ ".tmp");
if (tmpFile.open(QIODevice::WriteOnly)){
tmpFile.write(partData);
tmpFile.close();
rs.tmpFile = tmpFile.fileName();
}
}*/
//rs.argv.append("'http' ");
for (int i=0;i < hdr.size();i++){
tmp.clear();
QCoreApplication::processEvents();
//we only process if the the line is content disposition
if (hdr.at(i).contains("Content-Disposition:")){
QString name, filename;
name.clear();
filename.clear();
qDebug() << "processing:" << hdr.at(i) << " with data" << partData;
//locate name;
name = QString::fromLocal8Bit(this->utilExtractData("name=",";",0,&hdr.at(i)));
name = name.replace('"',"");
filename = QString::fromLocal8Bit(this->utilExtractData("filename=",";",0,&hdr.at(i)));
filename = filename.replace('"',"");
if (!name.isEmpty())
{
/*if (partData.size() > 1024){
rs.argv.append("'" + name + "?=localfile:" + tmpFile.fileName() + "' ");
}
else
{*/
rs.argv.append("'" + name + "?=" + QString::fromLocal8Bit(partData.toBase64()) + "' ");
//}
}
if (!filename.isEmpty())
{
rs.argv.append("'filename?=" + filename + "' ");
}
}
}
//qDebug() << "Done process multipart\n\n";
return rs;
}
executeData clientHandler::processClientRequest(QByteArray *data,qint64 headerLoc)
{
executeData ed;
qint64 loc, locBoundary;
QByteArray urlData;
QByteArray boundary;
urlData.clear();
boundary.clear();
loc =0;
//ed default value for errror;
ed.contentType ="text/html";
ed.data = "Invalid http format";
ed.OK = false;
//Find GET
if (data->indexOf("GET",0) == 0){
qDebug() << "process GET";
urlData.append(data->mid(0,data->indexOf("\n")).split(' ').at(1));
return this->processGET(QString::fromLocal8Bit(urlData.trimmed()));
}
//Find POST
if (data->indexOf("POST",0) == 0)
{
qDebug() << "process POST";
urlData.append(data->mid(0,data->indexOf("\n")).split(' ').at(1));
//check if there is multipart;
loc = data->indexOf("multipart",0);
if (loc > 0){
//get boundary
loc = data->indexOf("boundary",0);
if (loc > 0){
locBoundary = data->indexOf("\r\n",loc);
if (locBoundary > 0){
boundary.append(data->mid(loc+9,locBoundary - (loc+9)).trimmed());
}
else{
return ed;
}
}
else{
return ed;
}
}
*data = data->remove(0,headerLoc + 4);
qDebug() << "===============================";
return this->processPOST(QString::fromLocal8Bit(urlData),boundary,data);
}
return ed;
}
void clientHandler::processClient(QTcpSocket *socket, QList<FileExec>fileExecList)
{
//QByteArray inData;
//QString inStr;
//QStringList list;
//QStringList ops;
bool getProcess;
//bool processHeader;
//QString resultStr;
executeData ed;
QByteArray inData;
qint64 loc;
qint64 contentLength;
qint64 requestLength;
qint64 headerLength;
quint64 timeout;
bool processHeader;
bool dataRecieved;
processHeader = false;
this->clientTCP = socket;
this->fileExecList = fileExecList;
contentLength = 0;
requestLength = 0;
loc = 0;
getProcess = false;
dataRecieved = false;
timeout = 0;
connect(this->clientTCP,SIGNAL(disconnected()),this,SLOT(socketDisconnected()));
if (!this->clientTCP->waitForReadyRead(100)){
this->clientTCP->disconnectFromHost();
this->clientTCP->deleteLater();
return;
}
inData.clear();
while(1){
if (timeout == 30000){
break;
}
//we need to read the first data and determined content length
if (this->clientTCP->bytesAvailable() > 0){
qDebug() << this->clientTCP->bytesAvailable();
timeout = 0;
inData.append(this->clientTCP->readAll());
QCoreApplication::processEvents();
if (!processHeader){
headerLength = inData.indexOf("\r\n\r\n",0);
if (headerLength > 0){
processHeader = true;
//find content length;
loc = inData.indexOf("Content-Length:");
if (loc < 0){
contentLength = 0;
}
if (loc > 0){
//get the size;
qint64 loc2 = inData.indexOf("\r\n",loc);
if (loc2 < 0){
contentLength = 0;
}
else
{
QByteArray size;
size.clear();
size.append(inData.mid(loc,loc2 - loc).replace("Content-Length:","").trimmed());
contentLength = size.toULongLong();
}
}
}
}
if (processHeader){
requestLength = headerLength + contentLength;
if (inData.size() >= requestLength){
dataRecieved = true;
break;
}
}
}
QCoreApplication::processEvents();
timeout++;
}
if (!dataRecieved){
this->clientTCP->disconnectFromHost();
this->clientTCP->deleteLater();
return;
}
//qDebug() << "DEBUG STOP" << requestLength << timeout;
//qDebug() << "inData size" << inData.size();
ed = this->processClientRequest(&inData,headerLength);
/*
//read from client browser
inStr = QString::fromLocal8Bit(this->clientTCP->readAll());
list = inStr.split('\n');
resultStr.clear();
resultStr = "<br><font size=1>Infoblox TIG DGA Web System<br>\n";
resultStr.append("Copyright (C) 2015 Azril arahimATinfobloxDOTcom</font><br><br><b><br>\n");
for (quint16 i =0;i < list.size();i++){
ops = list.at(i).split(QChar(32));
if (ops.at(0).contains("GET")){
ed = this->processGET(ops.at(1));
getProcess = true;
break;
}
if (ops.at(0).contains("POST")){
QString url = this->getPOSTURL(list);
qDebug() << "POST:" << url;
//qDebug() << "ACTUAL RAW\n" << inStr << "\n\n";
ed = this->processPOST(url);
getProcess = true;
break;
}
}
if(!getProcess){
//none get process by GET or POST
//create error Message
resultStr.append("Invalid Request. Unknown Request</b><br>\n");
}
//qDebug() << resultStr << "\n";
*/
this->clientTCP->write(this->produceHttpReply(ed));
this->clientTCP->flush();
this->clientTCP->waitForBytesWritten();
//close the socker
this->clientTCP->disconnectFromHost();
this->clientTCP->abort();
return;
}
QString clientHandler::getPOSTURL(QStringList postDataList)
{
//to get the target file and its query from post data method
//and reformat it into GET url
quint16 line;
quint16 querySize;
QStringList part;
QString tgtFile;
QString query;
tgtFile = "";
querySize = 0;
query = "";
for (line = 0; line < postDataList.size(); line++){
part = postDataList.at(line).split(QChar(32));
//get the file name
if (part.at(0).compare("POST") == 0){
tgtFile = part.at(1);
}
//get the POST query length
if (part.at(0).compare("Content-Length:") == 0){
querySize = part.at(1).toInt();
}
//stop looping when both values are accepted
if ((!tgtFile.isEmpty()) && (querySize > 0)){
break;
}
}
//to be safe
if (tgtFile.isEmpty())
return "";
//get the post query.. by moving from behind
for (line = postDataList.size() -1; line >=0; line--){
query = postDataList.at(line).trimmed() + query;
if (query.size() >= querySize){
break;
}
}
//create the query similar to GET format
if (query.isEmpty()){
return tgtFile;
}
return tgtFile + "?" + query;
}
QString clientHandler::getRequestURLFile(QString rURL)
{
qint16 queryLoc;
queryLoc = 0;
//backslashLoc = 0;
//check if it has query indication
queryLoc = rURL.indexOf("?");
//return the entire url
if (queryLoc <= 0)
return rURL;
//return the partial without the query
return rURL.mid(0,queryLoc);
}
QString clientHandler::getRequestURLQuery(QString rURL)
{
qint16 queryLoc = 0;
queryLoc = rURL.indexOf("?");
if (queryLoc <= 0)
return "";
queryLoc++;
return rURL.mid(queryLoc,rURL.size() - queryLoc);
}
QString clientHandler::urlDecode(QString url)
{
QString t;
t = url;
if (t.indexOf("'",0) < 0)
{
t.clear();
t = "'" + url;
}
if (t.indexOf("'",t.size() - 1) <0)
{
t.append("'");
}
//replace & with "' '"
t = t.replace("&","' '");
//replace %2C with comma ,
t = t.replace("%2C",",");
//replace %2B with +
t = t.replace("%2B","+");
//replace %20 with " "
t = t.replace("%20", " ");
return t;
}
executeData clientHandler::processGET(QString getURL)
{
executeData ed;
QString file;
QString query;
//we need to decoded
file = this->getRequestURLFile(getURL);
query = this->urlDecode(this->getRequestURLQuery(getURL));
qDebug() << "executing" << file << "with" << query;
if (file.isEmpty()){
ed.contentType = "text/html";
ed.data = "Invalid Request. Bad Structure";
}
//process system data if its call
if (file.compare("/sysstat") == 0){
return this->processSystemStatus();
}
//execute the request
return this->processFile(file,query);
}
executeData clientHandler::processPOST(QString postURL, QByteArray boundary, QByteArray *data){
executeData ed;
QString file;
multipartArgv margv;
QStringList tmpFiles;
qint64 loc1, loc2, qSize;
quint8 dd;
QString argv;
dd = 0;
//we need to decoded
//postURL = this->urlDecode(postURL);
file = this->getRequestURLFile(postURL);
//query = this->getRequestURLQuery(postURL);
if (file.isEmpty()){
ed.contentType = "text/html";
ed.data = "Invalid POST Request. Bad Structure";
}
//if the post contains no boundary, then its not multiform
//the post data will be always the entire content body
if (boundary.isEmpty()){
argv.clear();
argv.append("'data=" + QString::fromLocal8Bit(data->replace("\r\n","").trimmed().toBase64()) + "' ");
}
else {
qDebug() << "processin boundary:" << boundary;
//we process every boundary
while(1){
loc1 = 0;
loc2 = 0;
margv.argv.clear();
margv.tmpFile.clear();
QCoreApplication::processEvents();
//get the first boundary location;
loc1 = data->indexOf(boundary,0);
if (loc1 <0){
break;
}
//boundary correction
if (loc1 != 0){
boundary = data->mid(0,loc1 + boundary.size());
qDebug() << "new boundary" << boundary;
}
loc1 = boundary.size();
loc2 = data->indexOf(boundary,loc1);
if (loc2 < 0){
break;
}
qSize = loc2 - loc1;
margv = this->processPOSTMultiPart(data->mid(loc1,qSize));
if (!margv.tmpFile.isEmpty()){
tmpFiles.append(margv.tmpFile);
}
if (!margv.argv.isEmpty()){
argv.append(margv.argv);
}
//remove the process part data;
*data = data->remove(0,loc2);
}
}
//execute the query;
//format argv
qDebug() << "execute query";
qDebug().noquote() << file << argv;
ed = this->processFile(file,argv);
//after properly execute file, make sure we delete the tmp;
for (int a=0; a < tmpFiles.size();a++){
QFile f(tmpFiles.at(a));
f.remove();
}
return ed;
}
QString clientHandler::getFileExtention(QString file)
{
qint16 loc;
//get file extention from file
loc = file.lastIndexOf(".");
if (loc <=0){
return "";
}
loc++;
return file.mid(loc,file.size() - loc);
}
FileExec clientHandler::getFileExecStatus(QString ext)
{
FileExec fe;
//if the extension is not in declared config list, the it is read only
//default
fe.contentType = "text/html";
fe.ext = ext;
fe.scriptParent = "";
fe.execType = 1; //read only
qDebug() << "EXT:" << ext;
for (quint16 i = 0; i < this->fileExecList.size(); i++){
if (this->fileExecList.at(i).ext.compare(ext) == 0){
return this->fileExecList.at(i);
}
}
return fe;
}
executeData clientHandler::processSystemStatus()
{
//get system data from PROCVECT;
executeData ep;
QString ts;
quint16 id;
ep.contentType = "text/html";
double successEfficient;
double execEfficient;
double efficientScore;
quint16 median;
qDebug() << "vector size:" << this->PROCVECT->size();
ep.data.clear();
ep.data.append("<html><br><b>System Process Status</b><br><br>\n");
ep.data.append("<table border='1' style='width:800' >\n");
ep.data.append("<tr><td align='center'><font size='2'><b>Process Name<b></td>\n");
ep.data.append("<td align='center'><font size='2'><b>Invoked</b></td>\n");
ep.data.append("<td align='center'><font size='2'><b>Failed</b></td>\n");
ep.data.append("<td align='center'><font size='2'><b>Success Score</b></td>\n");
ep.data.append("<td align='center'><font size='2'><b>Fastest</b></td>\n");
ep.data.append("<td align='center'><font size='2'><b>Slowest</b></td>\n");
ep.data.append("<td align='center'><font size='2'><b>Exec score </b></td>\n");
ep.data.append("<td align='center'><font size='2'><b>Efficient %</b></td></tr>\n");
for (id = 0; id < this->PROCVECT->size(); id++){
successEfficient = ((this->PROCVECT->at(id).call - this->PROCVECT->at(id).fail)/this->PROCVECT->at(id).call) * 40;
median = this->PROCVECT->at(id).slowestTIME - this->PROCVECT->at(id).fastestTIME;
qDebug() << "median" << median;
execEfficient = (100 - median);
qDebug() << "execefficient" << execEfficient;
execEfficient = (execEfficient / 100);
qDebug() << "execefficient" << execEfficient;
execEfficient = execEfficient * 60;
qDebug() << "execefficient" << execEfficient;
efficientScore = successEfficient + execEfficient;
ts = "<tr><td align='center'><font size='1'>" + this->PROCVECT->at(id).processName + "</td>\n";
ts.append("<td align='center'><font size='1'>" + QString::number(this->PROCVECT->at(id).call) + "</small></td>\n");
ts.append("<td align='center'><font size='1'>" + QString::number(this->PROCVECT->at(id).fail) + "</td>\n");
ts.append("<td align='center'><font size='1'>" + QString::number(successEfficient) + "</td>\n");
ts.append("<td align='center'><font size='1'>" + QString::number(this->PROCVECT->at(id).fastestTIME) + "</td>\n");
ts.append("<td align='center'><font size='1'>" + QString::number(this->PROCVECT->at(id).slowestTIME) + "</td>\n");
ts.append("<td align='center'><font size='1'>" + QString::number(execEfficient) + "</td>\n");
ts.append("<td align='center'><font size='1'>" + QString::number(efficientScore) + "</td></tr>\n");
ep.data.append(ts);
}
if (id == 0){
ep.data.append("<tr><td align='center'><font size='1'>No Process Found</font></td><td></td><td></td><td></td><td></td></tr>");
}
ep.data.append("</table></html>");
return ep;
}
executeData clientHandler::processFile(QString file, QString argv)
{
executeData ed;
QString ext;
FileExec fe;
ed.contentType ="text/html";
ed.data = "Error: Unknown Extension";
//get file extension
ext = this->getFileExtention(file);
if (ext.isEmpty()){
return ed;
}
//check file extention for execution;
fe = this->getFileExecStatus(ext);
ed.contentType = fe.contentType;
//qDebug() << file << fe.execType;
//qDebug() << "content type" << ed.contentType;
//check file exists
QFile fs(this->workDir + file);
if (!fs.exists())
{
ed.contentType ="text/html";
ed.data = "Error: Resource "; ed.data.append(file); ed.data.append(" does not exist");
return ed;
}
switch (fe.execType)
{
case 1: ed.data = this->readFile(file);break; //readfile;
case 2: ed.data = this->executeFile(file,argv);break;
}
//convert all bin regardless text or not into base64;
return ed;
}
QByteArray clientHandler::produceHttpReply(executeData ed)
{
QByteArray out;
//if content type is ? then the application will produced their own header.
out.clear();
out.append("HTTP/1.0 200 OK\r\n");
out.append("Server: Azril TODAK WEB SERVER v0.1\r\n");
out.append("Connection: Closed\r\n");
if (!ed.contentType.contains("?")){
out.append("Content-Length:");
out.append(QString::number(ed.data.size()));
out.append("\r\n");
out.append("Content-Type:" + ed.contentType + "\r\n");
out.append("\r\n");
out.append(ed.data);
}
else
{
out.append(ed.data);
}
//qDebug() << "\n====REPLY BEGIN=====\n" << out << "\n=====REPLY END======\n";
return out;
}
void clientHandler::socketDisconnected()
{
this->clientTCP->deleteLater();
}
QByteArray clientHandler::executeFile(QString file, QString argv)
{
QProcess pro;
QByteArray out;
QFile fs(this->workDir + file);
if (!fs.exists())
{
out = "Error: Resource "; out.append(file); out.append(" does not exist");
return out;
}
pro.start(this->workDir + file + " " + argv);
//start it
if (!pro.waitForStarted()){
out.append(pro.errorString());
return out;
}
//wait it to finished
if (!pro.waitForFinished()){
out.append(pro.errorString());
pro.close();
return out;
}
//read result
pro.waitForReadyRead();
out = pro.readAllStandardOutput();
pro.close();
return out;
}
QByteArray clientHandler::readFile(QString file){
QByteArray out;
QFile fs(this->workDir + file);
out.clear();
if (!fs.exists())
{
out = "Error: Resource "; out.append(file); out.append(" does not exist");
return out;
}
if (!fs.open(QIODevice::ReadOnly)){
out = "Error: Resource "; out.append(file); out.append(" does not exist");
return out;
}
//read it
qDebug() << "readOnly" << fs.fileName();
out = fs.readAll();
fs.close();
qDebug() << "Total read bytes are" << out.size();
return out;
}
QByteArray clientHandler::utilExtractData(const QByteArray searchKey, const QByteArray endKey, quint64 start, const QByteArray *srcData){
QByteArray result;
qint64 loc, loc2;
result.clear();
loc = srcData->indexOf(searchKey,start);
if (loc < 0){
return result;
}
loc2 = srcData->indexOf(endKey,loc);
if (loc2 < 0){
loc2 = srcData->size();
}
result.append(srcData->mid(loc,loc2 - loc).remove(0,searchKey.size()).trimmed());
return result;
}
QString clientHandler::getTmpRandomFileName()
{
//get new primary is pseudo generate algo for creating DB primary
//key based DDMMYYHHMMSS. This is to replace DB default auto increment
//that will lead inconsistantcy during data update, append, migration
//and rollback
QDateTime now;
QString utc;
quint64 primaryKey = 0;
utc = now.currentDateTimeUtc().toString();
utc.remove(QRegExp("[^0-9]"));
primaryKey = utc.toLongLong() + now.currentMSecsSinceEpoch();
return QString::number(primaryKey);
}