-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.c
1499 lines (1043 loc) · 34.3 KB
/
http.c
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
/*
ratproxy - HTTP request handling
--------------------------------
The following routines take care of HTTP request handling, parsing,
and error reporting.
Note that this code is one-shot, process is terminated when request
handling is done - and as such, we rely on the OS to do garbage
collection.
Author: Michal Zalewski <[email protected]>
Copyright 2007, 2008 by Google Inc. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <sys/wait.h>
#include <ctype.h>
#include <netdb.h>
#include <openssl/md5.h>
#include <time.h>
#include "config.h"
#include "types.h"
#include "debug.h"
#include "nlist.h"
#include "http.h"
#include "ssl.h"
#include "string-inl.h"
extern _u8* use_proxy; /* Runtime setting exports from ratproxy. */
extern _u32 proxy_port;
extern _u8 use_len;
static _u8 srv_buf[MAXLINE], /* libc IO buffers */
cli_buf[MAXLINE];
/* Read a single line of HTTP headers, strip whitespaces */
static _u8* grab_line(FILE* where) {
static _u8 inbuf[MAXLINE];
_u32 l;
if (!fgets(inbuf,MAXLINE,where)) return 0;
l = strlen(inbuf);
/* Excessive line length is bad, let's bail out. */
if (l == MAXLINE-1) return 0;
while (l && isspace(inbuf[l-1])) inbuf[--l] = 0;
return inbuf;
}
/* Return a generic HTTP error message, end current process.
Note that this function should not handle user-controlled data. */
static void http_error(FILE* client, _u8* message,_u8 sink) {
if (client) {
_u8* l;
if (sink) while ((l=grab_line(client)) && l[0]);
fprintf(client,
"HTTP/1.0 500 %s\n"
"Content-type: text/html\n\n"
"<font face=\"Bitstream Vera Sans Mono,Andale Mono,Lucida Console\">\n"
"The proxy is unable to process your request.\n"
"<h1><font color=red><b>%s.</b></font></h1>\n", message, message);
fflush(client);
fclose(client);
}
debug("[!] WARNING: %s.\n", message);
exit(0);
}
static _u8* BASE16 = "0123456789ABCDEF";
/* Decode URL-encoded parameter string */
void parse_urlencoded(struct naive_list_p* p, _u8* string) {
_u8 val_now = 0;
_u8 name[MAXLINE+1], val[MAXLINE+1];
_u32 nlen = 0, vlen = 0;
name[0] = 0;
val[0] = 0;
do {
_u8 dec = 0;
switch (*string) {
case '+':
dec = ' ';
break;
case '=':
val_now = 1;
break;
case '%': {
_u8 *a, *b;
/* Parse %nn code, if valid; default to '?nn' if not, replace with ? if \0. */
if (!string[1] || !string[2] || !(a=strchr(BASE16,toupper(string[1]))) ||
!(b=strchr(BASE16,toupper(string[2])))) { dec = '?'; break; }
dec = (a-BASE16) * 16 + (b-BASE16);
string += 2;
if (!dec) dec = '?';
break;
}
case '&':
case 0:
/* Handle parameter terminator; note that we also iterate over \0
because of loop condition placement. */
if (nlen) {
name[nlen] = 0;
val[vlen] = 0;
DYN_ADDP(*p,name,val,"");
}
val_now = 0;
nlen = 0;
vlen = 0;
break;
default:
if (!(dec=*string)) dec = '?';
}
/* Append decoded char, if any, to field name or value as needed. */
if (dec) {
if (!val_now) { if (nlen < MAXLINE) name[nlen++] = dec; }
else { if (vlen < MAXLINE) val[vlen++] = dec; }
}
} while (*(string++));
}
/* Read a line of multipart data from a linear buffer, advance buffer pointer. */
static _u8* get_multipart_line(_u8** buf) {
static _u8* retbuf;
_u8* x;
_u32 cnt;
if (retbuf) free(retbuf);
/* We assume \r\n formatting here, which is RFC-mandated and implemtned
by well-behaved browsers. */
x = strchr(*buf,'\r');
if (!x || x[1] != '\n') {
_u32 l = strlen(*buf);
retbuf = malloc(l + 1);
if (!retbuf) fatal("out of memory");
strcpy(retbuf,*buf);
*buf += l;
return retbuf;
}
cnt = x - *buf;
retbuf = malloc(cnt + 1);
if (!retbuf) fatal("out of memory");
memcpy(retbuf,*buf,cnt);
retbuf[cnt] = 0;
*buf += cnt + 2;
return retbuf;
}
/* Collect multipart data from a reasonably well-behaved browser. This routine
makes multiple assumptions that might be not true for maliciously formatted
data, but we do not strive to serve such requests well. */
void parse_multipart(struct naive_list_p* p, _u8* string, _u32 slen) {
_u8* field, *fname;
_u8* endptr = string + slen;
do {
_u8 *l, *end, *c;
field = 0;
fname = 0;
/* Skip boundary */
l = get_multipart_line(&string);
if (l[0] != '-' || l[1] != '-') return;
/* Sink headers, but grab field name if any */
while ((l = get_multipart_line(&string)) && l[0]) {
if (!strncasecmp(l,"Content-Disposition:",20)) {
/* Grab field name. */
_u8* f = rp_strcasestr(l,"; name=\"");
if (!f) continue;
f += 7;
c = strchr(++f,'"');
if (!c) continue;
*c = 0;
field = strdup(f);
if (!field) fatal("out of memory");
/* Grab file name, if any. */
f = rp_strcasestr(c + 1,"; filename=\"");
if (!f) continue;
f += 11;
c = strchr(++f,'"');
if (!c) continue;
*c = 0;
fname = strdup(f);
if (!fname) fatal("out of memory");
}
}
end = rp_memmem(string,endptr - string, "\r\n--", 4);
if (!end) return;
if (field)
DYN_ADDP_RAWMEM(*p,field,string,end-string,fname ? fname : (_u8*)"");
string = end + 2;
} while (1);
}
#define BASE64 "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ+/_-"
/* Looks for what could pass for a reasonably robust session token or XSRF protection. */
_u8 contains_token(_u8* name, _u8* value) {
_u32 run16 = 0, run64 = 0, run64_true = 0, run64_num = 0, run64_up = 0;
_u8* st = 0;
static _u32 tmin,tmax;
_u32 fno = 0;
if (!tmin) {
tmin = time(0);
tmax = tmin + (60 * 60 * 24 * 30); /* One month forward */
tmin -= (60 * 60 * 24 * 365 * 5); /* Five years back */
}
/* Known bad field names - return 0. */
fno = 0;
while (no_xsrf_fields[fno]) {
if (no_xsrf_fields[fno][0] == '=') {
if (!strcasecmp(name,no_xsrf_fields[fno] + 1)) return 0;
} else {
if (rp_strcasestr(name,no_xsrf_fields[fno])) return 0;
}
fno++;
}
/* Known safe field names - return 1. */
fno = 0;
while (xsrf_fields[fno]) {
if (xsrf_fields[fno][0] == '=') {
if (!strcasecmp(name,xsrf_fields[fno] + 1)) return 1;
} else {
if (rp_strcasestr(name,xsrf_fields[fno])) return 1;
}
fno++;
}
/* URLs are not anti-XSRF tokens, no matter how random they look. */
if (!strncmp(value,"http",4)) return 0;
/* Iterate over value data, compute base16 / base64 runs, collect
basic character disttributin data, rule out patterns such as unix
time, and make the call. */
do {
if (*value && strchr(BASE16,toupper(*value))) {
run16++;
} else {
if (run16 >= XSRF_B16_MIN && run16 <= XSRF_B16_MAX) {
_u8 tmp[5];
_u32 val;
strncpy(tmp,st,4);
tmp[4] = 0;
val = atoi(tmp);
if ((val < tmin / 1000000 || val > tmax / 1000000) &&
(st[0] != st[1] || st[0] != st[2])) return 1;
}
run16 = 0;
}
if (*value && strchr(BASE64,toupper(*value))) {
if (!isalpha(*value)) run64_num++;
if (isupper(*value)) run64_up++;
if (!run16) run64_true = 1;
if (!run64) st = value;
run64++;
} else {
if (run64 >= XSRF_B64_MIN && run64 <= XSRF_B64_MAX &&
((run64_num >= XSRF_B64_NUM && run64_up >= XSRF_B64_UP) ||
(run64_num >= XSRF_B64_NUM2)) && run64_true)
if (st[0] != st[1] || st[0] != st[2]) return 1;
run64 = 0;
run64_num = 0;
run64_true = 0;
st = 0;
}
} while (*(value++));
return 0;
}
/* Try to parse cookie header values. */
static void parse_cookies(_u8* str, struct naive_list2* c) {
_u8 name[128], val[128];
/* Iterate over cookies. We ignore cookies over 128 bytes for
name / value, and "special" values such as expiration date,
version, etc. */
while (str) {
while (isspace(*str)) str++;
if (sscanf(str,"%127[^;=]=%127[^;]",name,val) == 2) {
if (strcasecmp(name,"expires") && strcasecmp(name,"comment") &&
strcasecmp(name,"version") && strcasecmp(name,"max-age") &&
strcasecmp(name,"path") && strcasecmp(name,"domain") && name[0] != '$')
DYN_ADD2(*c,name,val);
}
str = strchr(str + 1 ,';');
if (str) str++;
}
}
/* Process the entire HTTP request, parse fields, and extract some preliminary signals. */
struct http_request* collect_request(FILE* client,_u8* ssl_host, _u32 ssl_port) {
struct http_request* ret;
_u8 *line, *x;
_u32 i;
/* Begin carefully - on CONNECT requests, we do not want to read more than
absolutely necessary. As soon as non-CONNECT is confirmed, we switch
to proper buffering. */
setvbuf(client, cli_buf, _IONBF, 0);
ret = calloc(1, sizeof(struct http_request));
if (!ret) fatal("out of memory");
line = grab_line(client);
if (!line || !line[0]) exit(0);
x = strchr(line,' ');
if (!x || x == line) http_error(client, "URL address missing or malformed request",1);
*(x++) = 0;
ret->method = strdup(line);
if (!ret->method) fatal("out of memory");
if (strcmp(line,"CONNECT")) {
/* Ok, safe to handle HTTP at full speed now. */
setvbuf(client, cli_buf, _IOFBF, sizeof(cli_buf));
if (!ssl_host) {
/* Unless coming from within CONNECT, we want a
properly specified protocol and so forth. */
if (x[0] == '/')
http_error(client, "Direct HTTP requests not allowed",1);
if (strncmp(x,"http://",7))
http_error(client, "Unsupported protocol",1);
x += 7;
}
} else {
/* We do not want CONNECT requests within CONNECT requests, really. */
if (ssl_host) http_error(client,"Evil CONNECT nesting",1);
ret->is_connect = 1;
}
ret->host = x;
x = strchr(ret->host,' ');
if (!x) http_error(client,"Missing HTTP protocol version",1);
if (strcmp(x," HTTP/1.0") && strcmp(x," HTTP/1.1"))
http_error(client,"unsupported HTTP protocol version",1);
/* Trim HTTP/1.x part now, we do not need it */
*x = 0;
if (!ret->is_connect) {
ret->path = strchr(ret->host,'/');
if (!ret->path) http_error(client,"Incomplete request URL",1);
*(ret->path++) = 0;
}
/* Try to find port, if any */
x = strchr(ret->host,':');
if (x) {
ret->port = atoi(x+1);
if (!ret->port || ret->port > 65535)
http_error(client,"Illegal port specification",1);
if (ret->port < 1024 && ret->port != 80 && ret->port != 443)
http_error(client,"Access to this port denied",1);
*x = 0;
} else {
if (ret->is_connect) ret->port = 443;
else ret->port = 80;
}
/* Populate HTTP envelope data with higher-level CONNECT
information if one present. */
if (ssl_host) {
ret->host = ssl_host;
ret->port = ssl_port;
ret->from_ssl = 1;
}
if (!ret->host[0])
http_error(client,"Host name is missing",1);
ret->host = strdup(ret->host);
if (!ret->host) fatal("out of memory");
/* Grab query data */
if (!ret->is_connect && (x = strchr(ret->path,'?'))) {
*(x++) = 0;
ret->query = strdup(x);
if (!ret->query) fatal("out of memory");
}
/* Grab path data */
if (!ret->is_connect) {
ret->path = strdup(ret->path);
if (!ret->path) fatal("out of memory");
x = strrchr(ret->path,'.');
if (x) ret->ext = x + 1;
}
/* Request target is now fully parsed. Let's collect headers, if any. */
while (1) {
line = grab_line(client);
if (!line) http_error(client,"Incomplete or malformed request headers",1);
/* Empty line == end of headers */
if (!line[0]) break;
x = strchr(line,':');
if (!x) http_error(client,"Invalid request header",1);
*x = 0;
while (isspace(*(++x)));
if (!strcasecmp(line,"Content-Length")) {
ret->payload_len = atoi(x);
if (ret->payload_len > MAXPAYLOAD)
http_error(client,"Payload size limit exceeded",1);
}
if (!strncasecmp(line,"Cookie",6))
parse_cookies(x,&ret->cookies);
if (!strcasecmp(line,"Referer")) {
_u8* rh;
ret->referer = strdup(x);
if (!ret->referer) fatal("out of memory");
/* Extract referer host to simplify other checks later on. */
if ((rh = strstr(x,"://"))) {
_u8* x;
rh = strdup(rh + 3);
if (!rh) fatal("out of memory");
if ((x = strchr(rh,'/'))) *x = 0;
if ((x = strchr(rh,':'))) *x = 0;
ret->ref_host = rh;
}
}
if (!strcasecmp(line,"X-Ratproxy-Loop"))
http_error(client,"Proxy loop detected",1);
/* These are specific to publicly documented anti-XSRF features of
Google Web Toolkit and Google Data APIs; this might be further
extended to accomodate other custom schemes in popular frameworks. */
if (!strcasecmp(line,"Authorization") && !strncasecmp(x,"GoogleLogin auth=",17)) {
ret->xsrf_safe = 1;
ret->authsub = 1;
}
if (!strcasecmp(line,"Content-Type")) {
if (rp_strcasestr(x,"text/x-gwt-rpc")) { ret->xsrf_safe = 1; ret->authsub = 1; }
if (rp_strcasestr(x,"multipart/form-data")) ret->multipart = 1;
else if (!rp_strcasestr(x,"application/x-www-form-urlencoded")) ret->non_param = 1;
}
DYN_ADD2(ret->h,line,x);
}
/* Get POST payload */
if (ret->payload_len) {
ret->payload = malloc(ret->payload_len + 1);
if (!ret->payload) fatal("out of memory");
if (fread(ret->payload,ret->payload_len,1,client) != 1)
http_error(client,"Premature end of payload data",0);
/* To make string matching safe. */
ret->payload[ret->payload_len] = 0;
}
/* Parse GET/POST parameters */
if (ret->query) parse_urlencoded(&ret->p, ret->query);
ret->ppar_bound = ret->p.c;
/* Do not parse payloads of arcane types. */
if (ret->payload && !ret->non_param) {
if (ret->multipart) parse_multipart(&ret->p, ret->payload, ret->payload_len);
else parse_urlencoded(&ret->p, ret->payload);
}
/* Locate XSRF tokens, if any */
/* Do not perform contains_token() checks on file fields. */
for (i=0;i<ret->p.c;i++)
if (!ret->p.fn[i][0] && contains_token(ret->p.v1[i],ret->p.v2[i]))
{ ret->xsrf_safe = 1; break; }
return ret;
}
/* Connect to server */
static FILE* open_server(FILE* client, _u8* host, _u32 port) {
FILE* ret;
struct sockaddr_in sin;
struct hostent* he;
_s32 ss;
if (!(he = gethostbyname(host)) || !(he->h_addr_list[0]))
http_error(client,"Unable to find target host",0);
ss = socket(PF_INET, SOCK_STREAM, 0);
if (ss < 0) pfatal("socket() failed");
sin.sin_family = PF_INET;
sin.sin_port = htons(port);
memcpy(&sin.sin_addr, he->h_addr_list[0], 4);
if (connect(ss,(struct sockaddr*)&sin,sizeof(struct sockaddr_in)))
http_error(client,"Connection to target failed",0);
ret = fdopen(ss,"w+");
if (!ret) fatal("fdopen() failed");
setvbuf(ret, srv_buf, _IOFBF, sizeof(srv_buf));
return ret;
}
/* Connect to server, take proxy CONNECT handling into account */
FILE* open_server_complete(FILE* client, struct http_request* r) {
FILE* ret;
_u8* l;
if (use_proxy)
ret = open_server(client, use_proxy, proxy_port);
else
ret = open_server(client, r->host, r->port);
if (r->is_connect) {
if (use_proxy) {
fprintf(ret,"CONNECT %s:%u HTTP/1.0\r\n\r\n",r->host,r->port);
fflush(ret);
setvbuf(ret, srv_buf, _IONBF, 0);
/* Sink proxy response */
while ((l=grab_line(ret)) && l[0]);
}
if (client) {
fprintf(client,"HTTP/1.0 200 Go ahead, please.\r\n\r\n");
fflush(client);
}
}
return ret;
}
#define NEEDS_URLENC(x) \
(!(x) || !strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.",toupper(x)))
/* Rewrite GET and POST parameters as needed. */
void reconstruct_request(struct http_request* r) {
struct dyn_str p = { 0, 0 }, q = { 0, 0 };
_u32 cp = 0, i;
_u8 c;
_u8 tmp[32];
/* Encode params to query string, until ppar boundary is hit. */
for (;cp<r->p.c;cp++) {
if (cp == r->ppar_bound) break;
if (q.l) STR_APPEND_CHAR(q,'&');
i = 0;
while ((c=r->p.v1[cp][i])) {
if (NEEDS_URLENC(c)) {
sprintf(tmp,"%%%02X",c);
} else {
tmp[0] = c;
tmp[1] = 0;
}
STR_APPEND(q,tmp);
i++;
}
STR_APPEND_CHAR(q,'=');
i = 0;
while ((c=r->p.v2[cp][i])) {
if (NEEDS_URLENC(c)) {
sprintf(tmp,"%%%02X",c);
} else {
tmp[0] = c;
tmp[1] = 0;
}
STR_APPEND(q,tmp);
i++;
}
}
/* Update query string. */
if (q.l) r->query = q.v;
/* Deal with the rest of parameters, putting them in a multipart
envelope or as urlencoded payload, as needed. */
if (r->multipart) {
/* Update boundary; be just random enough to prevent accidents. */
sprintf(tmp,"ratproxybound%08x",rand());
r->use_boundary = strdup(tmp);
if (!r->use_boundary) fatal("out of memory");
for (;cp<r->p.c;cp++) {
STR_APPEND(p,"--");
STR_APPEND(p,r->use_boundary);
STR_APPEND(p,"\r\nContent-Disposition: form-data; name=\"");
STR_APPEND(p,r->p.v1[cp]);
if (r->p.fn[cp][0]) {
STR_APPEND(p,"\"; filename=\"");
STR_APPEND(p,r->p.fn[cp]);
}
STR_APPEND(p,"\"\r\n\r\n");
if (r->p.l2[cp]) {
STR_APPEND_RAWMEM(p,r->p.v2[cp],r->p.l2[cp]);
} else {
STR_APPEND(p,r->p.v2[cp]);
}
STR_APPEND(p,"\r\n");
}
STR_APPEND(p,"--");
STR_APPEND(p,r->use_boundary);
STR_APPEND(p,"--\r\n");
} else if (!r->non_param) {
for (;cp<r->p.c;cp++) {
if (p.l) STR_APPEND_CHAR(p,'&');
i = 0;
while ((c=r->p.v1[cp][i])) {
if (NEEDS_URLENC(c)) {
sprintf(tmp,"%%%02X",c);
} else {
tmp[0] = c;
tmp[1] = 0;
}
STR_APPEND(p,tmp);
i++;
}
STR_APPEND_CHAR(p,'=');
i = 0;
while ((c=r->p.v2[cp][i])) {
if (NEEDS_URLENC(c)) {
sprintf(tmp,"%%%02X",c);
} else {
tmp[0] = c;
tmp[1] = 0;
}
STR_APPEND(p,tmp);
i++;
}
}
if (p.l) STR_APPEND(p,"\r\n");
} else return; /* Leave payload intact. */
/* Update POST string. */
if (p.l) {
r->payload = p.v;
r->payload_len = p.l;
}
return;
}
/* Detect and convert GWT RPC syntax where appropriate. This is specific to
Google Web Toolkit. */
static _u8* maybe_gwt_rpc(_u8* str) {
struct dyn_str p = { 0, 0 };
_u8 *c = str, *n;
_u32 num = 0;
_u32 l = strlen(str);
if (l < 3 || str[l-3] != 0xEF || str[l-2] != 0xBF || str[l-1] != 0xBF) return str;
STR_APPEND(p,"GWT_RPC[");
while ((n = strstr(c,"\xEF\xBF\xBF"))) {
*n = 0;
if (num > 4) {
if (num != 5) STR_APPEND_CHAR(p,',');
STR_APPEND_CHAR(p,'\'');
if (!strncmp(c,"[L",2)) c += 2;
if (!strncmp(c,"com.google.",11) || !strncmp(c,"java.",5)) c = strrchr(c,'.') + 1;
/* We *could* escape here, but it's probably not worth the effort. */
STR_APPEND(p,c);
STR_APPEND_CHAR(p,'\'');
}
num++;
*n = '\xEF';
c = n + 3;
}
STR_APPEND_CHAR(p,']');
return p.v;
}
/* Convert multipart data to URLencoded string, to simplify reporting. */
_u8* stringify_payload(struct http_request* r) {
struct dyn_str p = { 0, 0 };
_u32 cp, i, c;
_u8 tmp[32];
if (!r->multipart) return maybe_gwt_rpc(r->payload);
/* Reconstruct payload from multipart boundary... */
for (cp=r->ppar_bound;cp<r->p.c;cp++) {
if (p.l) STR_APPEND_CHAR(p,'&');
i = 0;
while ((c=r->p.v1[cp][i])) {
if (NEEDS_URLENC(c)) {
sprintf(tmp,"%%%02X",c);
} else {
tmp[0] = c;
tmp[1] = 0;
}
STR_APPEND(p,tmp);
i++;
}
STR_APPEND_CHAR(p,'=');
/* When dealing with a file field, use field name, rather than
field data. */
if (r->p.fn[cp][0]) {
STR_APPEND(p,"FILE[");
i = 0;
while ((c=r->p.fn[cp][i])) {
if (NEEDS_URLENC(c)) {
sprintf(tmp,"%%%02X",c);
} else {
tmp[0] = c;
tmp[1] = 0;
}
STR_APPEND(p,tmp);
i++;
}
STR_APPEND_CHAR(p,']');
} else {
i = 0;
while ((c=r->p.v2[cp][i])) {
if (NEEDS_URLENC(c)) {
sprintf(tmp,"%%%02X",c);
} else {
tmp[0] = c;
tmp[1] = 0;
}
STR_APPEND(p,tmp);
i++;
}
}
}
return p.v;
}
/* Do a naive date comparison for t-1 sec/min/hr scenarios. */
_u8 comp_dates(_u8* exp, _u8* dat) {
_s32 i = strlen(dat), dc = 0;
if (i != strlen(exp)) return 1;
while (--i >= 0) {
if (exp[i] != dat[i]) {
if (!isdigit(dat[i]) || exp[i] > dat[i] || ++dc > 1) return 1;
}
}
return 0;
}
/* Send HTTP request, collect and parse response, spot header-related problems. */
struct http_response* send_request(FILE* client, FILE* server, struct http_request* r,
_u8 strip_state) {
struct http_response* ret;
_u8 *line, *x;
_s32 decl_clen = -1;
_u32 i;
_u8 port_spec[16] = { 0 };
_u8 *exp_value = 0, *dat_value = 0;
/* Send the request... unfortunately, we cannot specify :80 on all
standard requests, as some URL rewriters that redirect to https
will copy this over and cause problems. */
if (!r->from_ssl) {
if (r->port != 80) sprintf(port_spec,":%u",r->port);
} else {