-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulkrays.hcpp
More file actions
2487 lines (2180 loc) · 74.6 KB
/
bulkrays.hcpp
File metadata and controls
2487 lines (2180 loc) · 74.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
#include "config.h"
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/time.h>
#include <pwd.h>
#include <grp.h>
#include <errno.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef HAVE_PR_SET_DUMPABLE
#include <sys/prctl.h> // linux for toggling core dumps
#endif
#define QICONN_H_GLOBINST
#define BULKRAYS_H_GLOBINST
#include "bulkrays/bulkrays.h"
namespace bulkrays {
using namespace std;
using namespace qiconn;
ostream& operator<< (ostream& out, HttppConn::State const& state) {
switch (state) {
case HttppConn::HTTPRequestLine: // waiting for an http Request-Line
return cout << "HTTPRequestLine" ; break;
case HttppConn::MIMEHeader: // first mime-entry of mime header
return cout << "MIMEHeader" ; break;
case HttppConn::NextMIMEHeader: // either continuing a mime value or next mime entry
return cout << "NextMIMEHeader" ; break;
// case HttppConn::MessageBody: // starting the datas following mime header (Content-Length troubles)
// return cout << "MessageBody" ; break;
case HttppConn::ReadBody: // buffering the body of the message itself
return cout << "ReadBody" ; break;
case HttppConn::NowTreatRequest: // all message suposely read, we treat ...
return cout << "NowTreatRequest" ; break;
case HttppConn::TreatPending: // we're waiting a call-back from treatment that will finish pushing the output
return cout << "TreatPending" ; break;
case HttppConn::WaitingEOW: // the treatment is finished we're waiting for the end of normal transmission
return cout << "WaitingEOW" ; break;
case HttppConn::BuggyConn: // the connection is considered buggy no further reading should be performed and close asap
return cout << "BuggyConn" ; break;
}
return cout;
}
#define BEGIN_TERM_IDENT "\033[33m"
#define BEGIN_TERM_VALUE "\033[36m"
#define END_TERM_IDENT "\033[m"
const char * beginparsereq = "\033[35m";
const char * endparsereq = "\033[m";
const char * beginparsebody = "\033[36m";
const char * endparsebody = "\033[m";
Property::Property (const string &s) : s(s) {
i = atoi (s.c_str());
if ((s=="true") || (s=="Y") || (s=="y") || (s=="yes") || (s=="YES") || (s=="TRUE"))
b = true;
else
b = false;
}
inline int hexfromchar (char c) {
switch (c) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
case 'a': case 'A': return 10;
case 'b': case 'B': return 11;
case 'c': case 'C': return 12;
case 'd': case 'D': return 13;
case 'e': case 'E': return 14;
case 'f': case 'F': return 15;
default: return -1;
}
}
int percentdecode (const string &src, string &result) {
size_t i, l = src.length();
int nberror = 0;
for (i=0 ; i<l ; ) {
if (src[i] == '%') {
if (i+2 > l) {
nberror++;
break;
}
int d = hexfromchar(src[i+1]);
int u = hexfromchar(src[i+2]);
if ((d<0) || (u<0)) {
nberror++;
result += src[i++];
result += src[i++];
result += src[i++];
} else {
result += (char)(d*16+u);
i += 3;
}
} else {
result += src[i++];
}
}
// cerr << "percentdecode (" << src << ")=" << result << endl;
return nberror;
}
int percentdecodeform (const string &src, string &result) {
size_t i, l = src.length();
int nberror = 0;
for (i=0 ; i<l ; ) {
if (src[i] == '%') {
if (i+2 > l) {
nberror++;
break;
}
int d = hexfromchar(src[i+1]);
int u = hexfromchar(src[i+2]);
if ((d<0) || (u<0)) {
nberror++;
result += src[i++];
result += src[i++];
result += src[i++];
} else {
result += (char)(d*16+u);
i += 3;
}
} else if (src[i] == '+') {
result += " ", i++;
} else {
result += src[i++];
}
}
// cerr << "percentdecodeform (" << src << ")=" << result << endl;
return nberror;
}
string xmlencode (const string &s) {
string r;
size_t i, l=s.size();
for (i=0 ; i<l ; i++) {
switch (s[i]) {
case '"': r += """; break;
case '&': r += "&"; break;
case '\'': r += "'"; break;
case '<': r += "<"; break;
case '>': r += ">"; break;
default: r += s[i]; break;
}
}
return r;
}
void xmlencode (ostream & cout, const string &s) {
size_t i, l=s.size();
for (i=0 ; i<l ; i++) {
switch (s[i]) {
case '"': cout << """; break;
case '&': cout << "&"; break;
case '\'': cout << "'"; break;
case '<': cout << "<"; break;
case '>': cout << ">"; break;
default: cout << s[i]; break;
}
}
}
void urlencode (ostream & cout, const string &s) {
static const char* hex="0123456789ABCDEF";
size_t i, l=s.size();
for (i=0 ; i<l ; i++) {
switch (s[i]) {
default:
if (isascii(s[i]) && (!iscntrl(s[i]))) {
cout << s[i];
break;
}
case '=':
case '+':
case '&':
case ' ':
cout << '%'
<< hex[ (((unsigned char)s[i]) & 0xf0) >> 4 ]
<< hex[ (((unsigned char)s[i]) & 0x0f) ];
break;
}
}
}
void FieldsMapR::import (FieldsMap const &m) {
FieldsMap::const_iterator mi;
for (mi=m.begin() ; mi!=m.end() ; mi++)
this->insert ( pair<string, const string&> (mi->first, mi->second) );
}
ostream & operator<< (ostream& cout, FieldsMapR const &m ) {
FieldsMapR::const_iterator mi;
if (m.empty()) {
cout << " " << m.name << "[]={empty}" << endl;
return cout;
}
for (mi=m.begin() ; mi!=m.end() ; mi++)
cout << " " << m.name << "[" << mi->first << "]=\"" << mi->second << '"' << endl;
return cout;
}
ostream & operator<< (ostream& cout, map <string, BodySubEntry> const &m ) {
map <string, BodySubEntry>::const_iterator mi;
if (m.empty()) {
cout << " content_fields[]={empty}" << endl;
return cout;
}
for (mi=m.begin() ; mi!=m.end() ; mi++) {
cout << " content_fields[" << mi->first << "]=\"" << mi->second.contenttype << '"' << endl;
// cout << " {" << mi->second.body.substr(mi->second.begin, mi->second.length) << "}" << endl;
}
return cout;
}
int populate_reqfields_from_urlencodebody (const string& body, FieldsMap &reqfields, size_t p /* =0 */) {
int nberror = 0;
size_t q = p,
l = body.size();
while (q < l) {
// cerr << "----(" << body.substr(q) << ")------------------" << endl;
q = body.find('=', q);
if (q == string::npos) {
nberror ++;
break;
}
string ident = body.substr (p, q-p);
// cerr << "------------" << ident << endl;
FieldsMap::iterator mi = reqfields.find (ident);
if (mi != reqfields.end()) { // JDJDJDJD if it was a member function we could populate the error message
cerr << "populate_reqfields_from_urlencodebody : field[" << ident << "] repopulated !" << endl;
nberror ++;
}
p = q+1;
q = body.find('&', q);
string value;
if (q == string::npos) {
nberror += percentdecodeform (body.substr (p), value);
reqfields[ident] = value;
break;
}
nberror += percentdecodeform (body.substr (p, q-p), value);
reqfields[ident] = value;
p = q+1;
}
return nberror;
}
string fetch_localcr (const string &s, size_t p /* =0 */) {
size_t l = s.size();
string lcr;
while ((p<l) && ((s[p]==' ') || (s[p] == '\t'))) p++; // JDJDJD is this white space walking overkill ?
if (p > l)
return lcr;
if ((s[p] != 10) && (s[p] != 13)) {
cerr << "fetch_localcr bad carriage return at end of boundary ?" << endl;
return lcr;
}
lcr += s[p];
p++;
if (p > l)
return lcr;
if ((s[p] != 10) && (s[p] != 13))
return lcr;
if (s[p] == lcr[0])
return lcr;
lcr += s[p];
return lcr;
}
int read_mimes_in_string (string const &s, FieldsMap &mime, string const &lcr, size_t &p, size_t l /* = string::npos */) {
int nberror = 0;
if (l == string::npos)
l = s.size();
size_t lcrs = lcr.size();
string firstofmatch (":"); firstofmatch += lcr[0];
while (p<l) {
if (s.substr (p,lcrs) == lcr) { // end of mime header
p += lcrs;
break;
}
if (isspace(s[p])) { // we should not start an identifier with a space
return nberror ++;
}
size_t q = s.find_first_of(firstofmatch, p);
if (q == string::npos) { // we didn't find the ':'
nberror ++;
break;
}
if (s[q] != ':') { // we should find a : in order to build an identifier
nberror ++;
break;
}
string name = s.substr(p, q-p);
p = q+1;
while ((p<l) && isspace(s[p])) p++; // remove in-between spaces
q = s.find (lcr, p);
if (q == string::npos) { // we didn't find the end of line
nberror ++;
break;
}
mime[name] = s.substr (p, q-p); // JDJDJDJD we sgould check duplicate mime entries ?
p = q + lcrs;
while (p<l) {
if (s[p] == lcr[0]) break;
if (!isspace(s[p])) break;
while ((p<l) && isspace(s[p])) p++;
q = s.find(lcr, p);
if (q == string::npos) // we could not find the end of line
return nberror ++;
mime[name] += s.substr (p, q-p);
p = q + lcrs;
}
}
return nberror;
}
bool getrange (FieldsMap const & mime, size_t &rangestart, size_t &rangeend) {
#define BULKNPOS ((size_t)-1) // JDJDJDJD on devrait pouvoir trouver meilleure definition
rangestart = BULKNPOS;
rangeend = BULKNPOS;
FieldsMap::const_iterator mi = mime.find("Range");
if (mi == mime.end()) { // we don't have a range request ...
return false;
}
const string & range = mi->second;
if (range.empty()) return false;
// Range: bytes=0-999
// Range: bytes=1000-
size_t p = range.find ('-', 6); // JDJDJDJD if not found, shall we return false ???
if (p == 6) return false;
size_t q = p;
size_t val1 = atoi (range.substr(0,p).c_str()); // this could be better ????
size_t size = range.size();
if (p == string::npos) p = size - 1; else p--;
rangestart = 0;
size_t mult = 1;
while (true) {
if (isdigit (range[p]))
rangestart += (range[p]-'0') * mult;
if (p == 6) break;
mult *= 10, p--;
}
// verification
if (val1 != rangestart) {
cerr << "Range calculation mismatch : [" << range << "] => " << val1 << " != " << rangestart << endl;
}
p = q+1;
rangeend = 0;
while (p < size) {
if (isdigit (range[p])) {
rangeend *= 10;
rangeend += (range[p]-'0');
} else
break;
p++;
}
// verification
size_t val2 = atoi (range.substr (q+1).c_str());
if (val2 != rangeend) {
cerr << "Range calculation mismatch : [" << range << "] => " << val2 << " != " << rangeend << endl;
}
cerr << "got range : " << range << " => " << rangestart << " - " << rangeend << endl;
return true;
}
int populate_reqfields_from_uri (const string& uri, string &document_uri, FieldsMap &reqfields) {
size_t p;
int nberror = 0;
p = uri.find ('?');
nberror += percentdecode (uri.substr (0,p), document_uri);
if (p == string::npos)
return nberror;
else
return populate_reqfields_from_urlencodebody (uri, reqfields, p+1);
p++;
size_t q = p,
l = uri.size();
while (q < l) {
// cerr << "----(" << uri.substr(q) << ")------------------" << endl;
q = uri.find('=', q);
if (q == string::npos) {
nberror ++;
break;
}
string ident = uri.substr (p, q-p);
// cerr << "------------" << ident << endl;
FieldsMap::iterator mi = reqfields.find (ident);
if (mi != reqfields.end()) {
cerr << "populate_reqfields_from_uri : field[" << ident << "] repopulated !" << endl;
nberror ++;
}
p = q+1;
q = uri.find('&', q);
string value;
if (q == string::npos) {
nberror += percentdecodeform (uri.substr (p), value);
reqfields[ident] = value;
break;
}
nberror += percentdecodeform (uri.substr (p, q-p), value);
reqfields[ident] = value;
p = q+1;
}
return nberror;
}
MimeTypes::MimeTypes (const char* fname) {
ifstream f(fname);
if (!f) {
int e = errno;
cerr << "MimeTypes::MimeTypes : could not open " << fname << " : " << e << " " << strerror (e) << endl;
}
while (f) {
char c;
string line;
while (f && (f.get(c)) && (c != 10) && (c != 13)) line += c;
while (f && (f.get(c)) && ((c == 10) || (c == 13))) ;
f.unget();
// remove first spaces
size_t p = 0;
size_t l = line.size();
while ((p < l) && (isspace(line[p]))) p++;
if ((p<l) && (p == '#')) // have-we a comment line ?
continue;
string mime_type;
while ((p<l) && (!isspace(line[p])))
mime_type += line[p++];
// remove the inner spaces
while ((p < l) && (isspace(line[p]))) p++;
while (p < l) {
string term;
while ((p<l) && (!isspace(line[p])))
term += tolower (line[p++]); // terminaisons are lowered case
while ((p < l) && (isspace(line[p]))) p++;
if (! (term.empty() || mime_type.empty())) {
(*this)[term] = mime_type;
}
}
}
}
const string * MimeTypes::getmimefromterminaison (const string &term) {
string s;
size_t p=0, l=term.size();
while (p<l) s += tolower (term[p++]);
map<string,string>::iterator mi = find (s);
if (mi == end())
return NULL;
else
return &mi->second;
}
const string * MimeTypes::getmimefromterminaison (const char *term) {
if (term == NULL) return NULL;
string s;
const char *p = term;
while (*p != 0) s += tolower (*p++);
map<string,string>::iterator mi = find (s);
if (mi == end())
return NULL;
else
return &mi->second;
}
bool FieldsMap::match (const string &k, const string &v) const {
FieldsMap::const_iterator mi = find (k);
if (mi == end())
return false;
if (mi->second == v)
return true;
return false;
}
bool FieldsMap::notempty (const string &k) const {
FieldsMap::const_iterator mi = find (k);
if (mi == end())
return false;
if (mi->second.empty())
return false;
else
return true;
}
bool FieldsMap::notempty (const string &k, FieldsMap::iterator &mi) {
mi = find (k);
if (mi == end())
return false;
if (mi->second.empty())
return false;
else
return true;
}
bool FieldsMap::verif (const string &k, FieldsMap::iterator& mi) {
mi = find (k);
if (mi == end())
return false;
return true;
}
void HTTPRequest::logger () {
time_t t;
time (&t);
struct tm tm;
gmtime_r(&t, &tm);
// clog << httppconn->getname() << " " << method << " " << host << req_uri << " " << statuscode << " " << msg << endl;
(*clog)
<< millidiff (httppconn->entering_ReadBody, httppconn->entering_HTTPRequestLine) << " "
<< millidiff (httppconn->entering_NowTreatRequest, httppconn->entering_ReadBody) << " "
<< millidiff (httppconn->entering_WaitingEOW, httppconn->entering_NowTreatRequest) << " "
<< millidiff (httppconn->ending_WaitingEOW, httppconn->entering_WaitingEOW) << " "
<< httppconn->getname()
<< " - - " // JDJDJDJD here stuff about authentication
<< "[" << setfill('0')
<< setw(2) << tm.tm_mday << '/'
<< setw(2) << tm.tm_mon+1 << '/'
<< setw(4) << tm.tm_year + 1900 << ':'
<< setw(2) << tm.tm_hour << ':'
<< setw(2) << tm.tm_min << ':'
<< setw(2) << tm.tm_sec << "] \""
<< method << " " << host << req_uri << " " << version << "\" "
<< statuscode << " "
<< (int)(httppconn->gettotw() - httppconn->lastbwindex)
<< " \"";
MimeHeader::iterator mi = mime.find("Referer");
if (mi == mime.end())
(*clog) << " - ";
else
(*clog) << mi->second;
(*clog) << "\" ";
mi = mime.find("User-Agent");
if (mi == mime.end())
(*clog) << "\" - \"";
else
(*clog) << mi->second;
(*clog) << endl;
}
// void HTTPRequest::logger (const char *msg /*=NULL*/) {
// if (msg != NULL)
// logger (string(msg));
// else
// logger (string(""));
// // if (msg == NULL)
// // cout << httppconn->getname() << " " << method << " " << host << req_uri << " " << statuscode << endl;
// // else
// // cout << httppconn->getname() << " " << method << " " << host << req_uri << " " << statuscode << " " << msg << endl;
// }
char * rfc1123date_offset (char *buf, time_t offset) {
time_t t;
time (&t);
t += offset;
struct tm tm;
// gmtime_r(&t, &tm);
localtime_r(&t, &tm);
static const char* dayname[] = {
"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
};
static const char* monthname[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};
snprintf (buf, 39, "%3s, %02d %3s %4d %02d:%02d:%02d GMT",
dayname[tm.tm_wday],
tm.tm_mday,
monthname[tm.tm_mon],
tm.tm_year + 1900,
tm.tm_hour,
tm.tm_min,
tm.tm_sec
);
return buf;
}
void HTTPRequest::set_relative_expires (time_t seconds) {
char buf[40];
outmime["Expires"] = rfc1123date_offset (buf, seconds);
expires_set = true;
}
void set_relative_expires_jitter (size_t seconds, float jitter = 7.0) {
}
void HTTPRequest::publish_header (void) {
if (headerpublished) {
errlog() << "HTTPRequest::publish_header : header already published !!!" << endl;
return;
}
char buf[40];
ostream &cout = *(httppconn->out) ;
const char * outputerror;
if (errormsg == NULL) {
map <int,const char *>::iterator mi = status_message.find(statuscode);
if (mi == status_message.end()) {
errlog() << "HTTPRequest::publish_header : statuscode [" << statuscode << "] has no associated message" << endl;
outputerror = "Sorry, No Message";
} else {
outputerror = mi->second;
}
} else {
outputerror = errormsg;
}
cout << "HTTP/1.1 " << statuscode << ' ' << outputerror << bendl
<< "Date: " << rfc1123date_offset(buf, 0) << bendl;
MimeHeader::iterator mi;
for (mi=outmime.begin() ; mi!=outmime.end() ; mi++)
cout << mi->first << ": " << mi->second << bendl;
cout << bendl;
headerpublished = true;
}
void HTTPRequest::set_contentlength (size_t l) {
char buf[40];
snprintf (buf, 39, "%ld", (long) l);
outmime["Content-Length"] = buf;
}
bool HTTPRequest::cookcookies (void) {
if (cookiescooked)
return cookiescookedresult;
MimeHeader::iterator mi = mime.find("Cookie");
if (mi == mime.end()) {
cookiescooked = true;
cookiescookedresult = false;
return cookiescookedresult;
}
string &c = mi->second;
size_t p = 0, s= c.size();
while (p < s) {
while ((p<s) && isspace(c[p])) p++;
size_t q = c.find('=',p);
if (q == string::npos)
break;
string ident = c.substr (p,q-p);
p = q+1;
q = c.find ("; ", p);
if (q == string::npos) {
cookies[ident] = c.substr (p);
break;
}
cookies[ident] = c.substr (p, q-p);
p = q+2;
}
if (cookies.empty()) {
cookiescooked = true;
cookiescookedresult = false;
return cookiescookedresult;
}
cookiescooked = true;
cookiescookedresult = true;
return cookiescookedresult;
}
void HTTPRequest::initoutmime (void) {
outmime.clear();
outmime["Server"] = "bulkrays/" BULKRAYSVERSION;
outmime["Connection"] = "Keep-Alive";
}
HTTPRequest::~HTTPRequest () {
}
int status_message_globalinit (void) {
status_message [100] = "Continue";
status_message [101] = "Switching Protocols";
status_message [200] = "OK";
status_message [201] = "Created";
status_message [202] = "Accepted";
status_message [203] = "Non-Authoritative Information";
status_message [204] = "No Content";
status_message [205] = "Reset Content";
status_message [206] = "Partial Content";
status_message [300] = "Multiple Choices";
status_message [301] = "Moved Permanently";
status_message [302] = "Found";
status_message [303] = "See Other";
status_message [304] = "Not Modified";
status_message [305] = "Use Proxy";
status_message [307] = "Temporary Redirect";
status_message [400] = "Bad Request";
status_message [401] = "Unauthorized";
status_message [402] = "Payment Required";
status_message [403] = "Forbidden";
status_message [404] = "Not Found";
status_message [405] = "Method Not Allowed";
status_message [406] = "Not Acceptable";
status_message [407] = "Proxy Authentication Required";
status_message [408] = "Request Time-out";
status_message [409] = "Conflict";
status_message [410] = "Gone";
status_message [411] = "Length Required";
status_message [412] = "Precondition Failed";
status_message [413] = "Request Entity Too Large";
status_message [414] = "Request-URI Too Large";
status_message [415] = "Unsupported Media Type";
status_message [416] = "Requested range not satisfiable";
status_message [417] = "Expectation Failed";
status_message [418] = "I'm a teapot";
status_message [500] = "Internal Server Error";
status_message [501] = "Not Implemented";
status_message [502] = "Bad Gateway";
status_message [503] = "Service Unavailable";
status_message [504] = "Gateway Time-out";
status_message [505] = "HTTP Version not supported";
return 0;
}
TReqResult ReturnError::output (ostream &cout, HTTPRequest &req) {
stringstream body;
if (req.errormsg == NULL) {
map <int,const char *>::iterator mi = status_message.find (req.statuscode);
if (mi == status_message.end()) {
req.errlog() << "ReturnError::output : wrong internal error code : " << req.statuscode << endl;
req.statuscode = 500;
mi = status_message.find (req.statuscode);
}
req.errormsg = mi->second;
}
body << xhtml_header;
// body << "<html>" << bendl
// << "<head>" << bendl
// << " <title>" << req.statuscode << " " << req.errormsg << "</title>" << bendl
// << " <style>" << bendl
// << " .bomb64 { background: url(data:image/gif;base64," << bomb64_gif << ")" << bendl
// << " top left no-repeat;" << bendl
// << " height: 72px; width: 64px; margin: 0.1em; float: left;" << bendl
// << " }" << bendl
// << " </style>" << bendl
// << "</head>" << bendl
// << "<body>" << bendl
// << " <h1>" << req.statuscode << " " << req.errormsg << "</h1>" << bendl;
//
body << {{
<html>
<head>
<title>}} << req.statuscode << " " << req.errormsg << {{</title>
<style>
.bomb64 { background: url(data:image/gif;base64,}} << bomb64_gif << {{)
top left no-repeat;
height: 72px; width: 64px; margin: 0.1em; float: left;
}
</style>
</head>
<body>
<h1>}} << req.statuscode << " " << req.errormsg << {{</h1>}} ;
if (req.suberrormsg != NULL)
body << {{<p>}} << req.suberrormsg << {{</p>}} << bendl;
// << "<img src=\"data:image/gif;base64," << bomb64_gif << "\">" << bendl
int i;
body << {{<div>}};
for (i=0 ; i<req.statuscode/100 ; i++)
body << {{ <span class="bomb64"> </span>}};
body << {{</div>}} << bendl;
body << {{
<hr style="clear: both;">
<p>bulkrays http server</p>
</body>}};
req.outmime ["Content-Type"] = "text/html;charset=ASCII";
req.outmime ["Accept-Ranges"] = "bytes";
// req.outmime ["Retry-After"] = "5";
req.set_contentlength (body.str().size());
if (!req.expires_set)
req.set_relative_expires (10);
if (closeconnection)
req.outmime["Connection"] = "close";
req.publish_header();
cout << body.str();
return TRCompleted;
}
int ReturnError::shortcuterror (ostream &cout, HTTPRequest &req, int statuscode, const char* message /*=NULL*/, const char* submessage /*=NULL*/, bool closeconnection) {
ReturnError::closeconnection = closeconnection;
req.errormsg = message, req.suberrormsg = submessage, req.statuscode = statuscode;
int e = output (cout, req);
return e;
}
TReqResult TreatRequest::error (ostream &cout, HTTPRequest &req, int statuscode, const char* message /*=NULL*/, const char* submessage /*=NULL*/, bool closeconnection) {
req.errormsg = message, req.suberrormsg = submessage, req.statuscode = statuscode;
return returnerror.output (cout, req);
}
HttppConn::HttppConn (int fd, struct sockaddr_storage const &client_addr) : SocketConnection(fd, client_addr),request(*this) {
corking = true;
cork ();
id = idnum;
idnum ++;
state = HTTPRequestLine;
lastbwindex = gettotw();
if (lastbwindex != 0)
errlog() << "lastbwindex = " << lastbwindex << " shouldn't it be 0 ???" << endl;
if (debugconstructor) errlog() << "### ### ### Construction ### ### ###" << endl;
}
HttppConn::~HttppConn (void) {
if (debugconstructor) errlog() << "### ### ### Destruction ### ### ###" << endl;
// nothing to do there yet
}
// [2]GET /blouga HTTP/1.1
// [2]Host: bulkrays.zz
// [2]User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:15.0) Gecko/20100101 Firefox/15.0.1
// [2]Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
// [2]Accept-Language: en-us,en;q=0.5
// [2]X-Forwarded-For: 192.168.42.51
// [2]X-Varnish: 834650459
// [2]Accept-Encoding: gzip
// [2]
// [0]GET /tests/nav_logo114.png HTTP/1.1
// [0]Host: apache.zz
// [0]Connection: close
// [0]
ostream& HttppConn::shorterrlog (void) {
time_t t;
time (&t);
struct tm tm;
gmtime_r(&t, &tm);
return cerr
<< "[" << setfill('0')
<< setw(2) << tm.tm_mday << '/'
<< setw(2) << tm.tm_mon+1 << '/'
<< setw(4) << tm.tm_year + 1900 << ':'
<< setw(2) << tm.tm_hour << ':'
<< setw(2) << tm.tm_min << ':'
<< setw(2) << tm.tm_sec << "] \""
<< "[" << fd << " : " << id << " : " << name << "] ";
}
ostream& HttppConn::errlog (void) {
return shorterrlog()
<< request.method << " ("
<< request.host << ") "
<< request.document_uri
<< " ";
}
ostream& HTTPRequest::errlog (void) {
return httppconn->errlog();
}
const char * charitnull = "NULL";
const char * boolittrue = "TRUE";
const char * boolitfalse = "FALSE";
inline const char * charit (const char *p) { return (p == NULL) ? charitnull : p; }
inline const char * boolit (bool b) { return b ? boolittrue : boolitfalse; }
#define BEGIN_TERM_IDENT "\033[33m"
#define BEGIN_TERM_VALUE "\033[36m"
#define END_TERM_IDENT "\033[m"
ostream& HTTPRequest::dump (ostream& out) const {
const char *I = BEGIN_TERM_IDENT,
*V = BEGIN_TERM_VALUE,
*E = END_TERM_IDENT;
return out
// << I << "httppconn -> " << E << "= " << E << V << (*httppconn) << E << endl
<< I << "statuscode " << E << "= " << E << V << statuscode << E << endl
<< I << "errormsg " << E << "= " << E << V << charit(errormsg) << E << endl
<< I << "suberrormsg " << E << "= " << E << V << charit(errormsg) << E << endl
<< I << "expires_set " << E << "= " << E << V << boolit(expires_set) << E << endl
<< I << "headerpublished " << E << "= " << E << V << boolit(headerpublished) << E << endl
<< I << "method " << E << "= " << E << V << method << E << endl
<< I << "host " << E << "= " << E << V << host << E << endl
<< I << "req_uri " << E << "= " << E << V << req_uri << E << endl
<< I << "document_uri " << E << "= " << E << V << document_uri << E << endl
<< I << "version " << E << "= " << E << V << version << E << endl
<< I << "reqbodylen " << E << "= " << E << V << reqbodylen << E << endl
<< I << "readbodybytes " << E << "= " << E << V << readbodybytes << E << endl
<< "req_body NOTDUMPED" << endl
<< endl
<< ostreamMap(outmime, BEGIN_TERM_IDENT "outmime" END_TERM_IDENT) << endl
<< ostreamMap(mime, BEGIN_TERM_IDENT "mime" END_TERM_IDENT) << endl
// << ostreamMap(req_fields, BEGIN_TERM_IDENT "req_fields" END_TERM_IDENT) << endl
<< ostreamMap(uri_fields, BEGIN_TERM_IDENT "uri_fields" END_TERM_IDENT) << endl
<< ostreamMap(body_fields, BEGIN_TERM_IDENT "body_fields" END_TERM_IDENT) << endl
// << ostreamMap(content_fields, BEGIN_TERM_IDENT "content_fields" END_TERM_IDENT) << endl
;
}
bool set_default_host (string const &hostname) {
THostMapper::iterator mi = hostmapper.find (hostname);
mi_defaulthost = mi;
if (mi == hostmapper.end()) {
wehaveadefaulthost = false;
return false;
} else {
wehaveadefaulthost = true;
return true;
}
}
void unset_default_host (void) {
wehaveadefaulthost = false;
}
void HttppConn::compute_reqbodylen (void) {
// errlog() << "HttppConn::compute_reqbodylen" << endl;
MimeHeader::iterator mi = request.mime.find("Content-Length");
if (mi == request.mime.end()) {
if (request.method == "PUT") {
errlog() << "MessageBody : no Content-Length header " << hexdump(bufin) << endl;
}
request.reqbodylen = 0;
request.readbodybytes = 0;