This repository has been archived by the owner on Oct 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
mod_http.c
1658 lines (1395 loc) · 42.1 KB
/
mod_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
/*
* Copyright (c) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001,
* 2002, 2003, 2004
* Ohio University.
*
* ---
*
* Starting with the release of tcptrace version 6 in 2001, tcptrace
* is licensed under the GNU General Public License (GPL). We believe
* that, among the available licenses, the GPL will do the best job of
* allowing tcptrace to continue to be a valuable, freely-available
* and well-maintained tool for the networking community.
*
* Previous versions of tcptrace were released under a license that
* was much less restrictive with respect to how tcptrace could be
* used in commercial products. Because of this, I am willing to
* consider alternate license arrangements as allowed in Section 10 of
* the GNU GPL. Before I would consider licensing tcptrace under an
* alternate agreement with a particular individual or company,
* however, I would have to be convinced that such an alternative
* would be to the greater benefit of the networking community.
*
* ---
*
* This file is part of Tcptrace.
*
* Tcptrace was originally written and continues to be maintained by
* Shawn Ostermann with the help of a group of devoted students and
* users (see the file 'THANKS'). The work on tcptrace has been made
* possible over the years through the generous support of NASA GRC,
* the National Science Foundation, and Sun Microsystems.
*
* Tcptrace is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Tcptrace is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tcptrace (in the file 'COPYING'); if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
* Author: Shawn Ostermann
* School of Electrical Engineering and Computer Science
* Ohio University
* Athens, OH
* ostermann@cs.ohiou.edu
* http://www.tcptrace.org/
*/
#include "tcptrace.h"
static char const GCC_UNUSED rcsid[] =
"$Header$";
#ifdef LOAD_MODULE_HTTP
#include <sys/mman.h>
#include "mod_http.h"
#define DEFAULT_SERVER_PORT 80
/* Revised HTTP module with a new HTTP parser provided by Bruce Mah */
/* codes for different message types */
typedef enum {
MethodCodeOptions,
MethodCodeGet,
MethodCodeHead,
MethodCodePost,
MethodCodePut,
MethodCodeDelete,
MethodCodeTrace,
MethodCodeUnknown
} MethodCode;
char *MethodCodeString[] = {
"OPTIONS",
"GET",
"HEAD",
"POST",
"PUT",
"DELETE",
"TRACE"
};
/* info gathered for each GET */
struct get_info {
timeval get_time; /* when CLIENT sent GET */
timeval send_time; /* when SERVER sent CONTENT */
timeval lastbyte_time; /* when SERVER sent last byte of CONTENT */
timeval ack_time; /* when CLIENT acked CONTENT */
unsigned request_position; /* byte offset for this request */
unsigned reply_position; /* byte offset for this reply */
MethodCode method; /* HTTP method code */
unsigned response_code; /* HTTP response code */
unsigned content_length; /* as reported by server */
char *get_string; /* content of GET string */
char *content_type; /* MIME type */
struct get_info *next;
};
/* linked list of times */
struct time_stamp {
timeval thetime;
u_long position;
struct time_stamp *next;
struct time_stamp *prev;
};
/* info kept for each client */
static struct client_info {
PLOTTER plotter;
char *clientname;
struct client_info *next;
} *client_head = NULL;
/* info kept for each connection */
static struct http_info {
timeval c_syn_time; /* when CLIENT sent SYN */
timeval s_syn_time; /* when SERVER sent SYN */
timeval c_fin_time; /* when CLIENT sent FIN */
timeval s_fin_time; /* when SERVER sent FIN */
/* client record */
struct client_info *pclient;
/* info about the TCP connection */
tcp_pair *ptp;
tcb *tcb_client;
tcb *tcb_server;
/* aggregate statistics for HTTP requests on this connection*/
/* some of this info is available in *tcb_client and *tcb_server */
/* but we keep a copy of it here to keep all useful info in one place */
unsigned total_request_length;
unsigned total_reply_length;
unsigned total_request_count;
unsigned total_reply_count;
/* when querries (GETs) were sent by client */
struct time_stamp get_head;
struct time_stamp get_tail;
/* when answers (CONTENT) were sent by server */
struct time_stamp data_head;
struct time_stamp data_tail;
/* when answers (CONTENT) were acked by client */
struct time_stamp ack_head;
struct time_stamp ack_tail;
/* linked list of requests */
struct get_info *gets_head;
struct get_info *gets_tail;
struct http_info *next;
} *httphead = NULL, *httptail = NULL;
/* which port are we monitoring?? */
static unsigned httpd_port;
/* local routines */
static timeval WhenSent(struct time_stamp *phead, struct time_stamp *ptail,
u_long position);
static timeval WhenAcked(struct time_stamp *phead, struct time_stamp *ptail,
u_long position);
static void MFUnMap(MFILE *mf, char *firstbyte);
static void MFMap(MFILE *mf, char **firstbyte, char **lastbyte);
static void FindGets(struct http_info *ph);
static void FindContent(struct http_info *ph);
static void HttpGather(struct http_info *ph);
static struct http_info *MakeHttpRec(void);
static struct get_info *MakeGetRec(struct http_info *ph);
static u_long DataOffset(tcb *tcb, seqnum seq);
static void AddGetTS(struct http_info *ph, u_long position);
static void AddDataTS(struct http_info *ph, u_long position);
static void AddAckTS(struct http_info *ph, u_long position);
static void AddTS(struct time_stamp *phead, struct time_stamp *ptail,
u_long position);
static double ts2d(timeval *pt);
static void HttpPrintone(MFILE *pmf, struct http_info *ph);
static void HttpDoPlot(void);
static struct client_info *FindClient(char *clientname);
/* useful macros */
#define IS_CLIENT(ptcp) (ntohs((ptcp)->th_dport) == httpd_port)
#define IS_SERVER(ptcp) (ntohs((ptcp)->th_dport) != httpd_port)
/* Mostly as a module example, here's a plug in that records HTTP info */
int
http_init(
int argc,
char *argv[])
{
int i;
int enable=0;
/* look for "-xhttp[N]" */
for (i=1; i < argc; ++i) {
if (!argv[i])
continue; /* argument already taken by another module... */
if (strncmp(argv[i],"-x",2) == 0) {
if (strncasecmp(argv[i]+2,"http",4) == 0) {
/* I want to be called */
enable = 1;
if (isdigit((int)(argv[i][6]))) {
httpd_port = atoi(argv[i]+6);
} else {
httpd_port = DEFAULT_SERVER_PORT;
}
printf("mod_http: Capturing HTTP traffic (port %d)\n", httpd_port);
argv[i] = NULL;
}
}
}
if (!enable)
return(0); /* don't call me again */
/* init stuff */
/* We need to save the contents for accurate reconstruction of questions */
/* and answers */
save_tcp_data = TRUE;
return(1); /* TRUE means call http_read and http_done later */
}
/* N.B. first byte is position _1_ */
static u_long
DataOffset(
tcb *tcb,
seqnum seq)
{
u_long off;
/* we're going to be a little lazy and assume that a http connection */
/* can't be longer than 2**32 */
if (seq > tcb->syn)
off = seq-tcb->syn;
else
off = tcb->syn-seq;
if (debug>1)
fprintf(stderr,"DataOffset: seq is %lu, syn is %lu, offset is %ld\n",
seq, tcb->syn, off);
return(off);
}
static struct get_info *
MakeGetRec(
struct http_info *ph)
{
struct get_info *pg;
pg = MallocZ(sizeof(struct get_info));
/* initialize some fields */
pg->get_string = "- -";
pg->content_type = "-/-";
/* put at the end of the chain */
if (ph->gets_head == NULL) {
ph->gets_head = pg;
ph->gets_tail = pg;
} else {
ph->gets_tail->next = pg;
ph->gets_tail = pg;
}
return(pg);
}
static struct http_info *
MakeHttpRec()
{
struct http_info *ph;
ph = MallocZ(sizeof(struct http_info));
ph->get_head.next = &ph->get_tail;
ph->get_tail.prev = &ph->get_head;
ph->get_tail.position = 0xffffffff;
ph->data_head.next = &ph->data_tail;
ph->data_tail.prev = &ph->data_head;
ph->data_tail.position = 0xffffffff;
ph->ack_head.next = &ph->ack_tail;
ph->ack_tail.prev = &ph->ack_head;
ph->ack_tail.position = 0xffffffff;
/* chain it in (at the tail of the list) */
if (httphead == NULL) {
httphead = ph;
httptail = ph;
} else {
httptail->next = ph;
httptail = ph;
}
return(ph);
}
static void
AddGetTS(
struct http_info *ph,
u_long position)
{
AddTS(&ph->get_head,&ph->get_tail,position);
}
static void
AddDataTS(
struct http_info *ph,
u_long position)
{
AddTS(&ph->data_head,&ph->data_tail,position);
}
static void
AddAckTS(
struct http_info *ph,
u_long position)
{
AddTS(&ph->ack_head,&ph->ack_tail,position);
}
/* add a timestamp to the record */
/* HEAD points to the smallest position numbers */
/* TAIL points to the largest position numbers */
static void
AddTS(
struct time_stamp *phead,
struct time_stamp *ptail,
u_long position)
{
struct time_stamp *pts;
struct time_stamp *pts_new;
pts_new = MallocZ(sizeof(struct time_stamp));
pts_new->thetime = current_time;
pts_new->position = position;
for (pts = ptail->prev; pts != NULL; pts = pts->prev) {
if (position == pts->position)
return; /* ignore duplicates */
if (position > pts->position) {
/* it goes AFTER this one (pts) */
pts_new->next = pts->next;
pts_new->prev = pts;
pts->next = pts_new;
pts_new->next->prev = pts_new;
return;
}
}
/* can't fail, the tail has timestamp 0 */
}
static struct client_info *
FindClient(
char *clientname)
{
struct client_info *p;
for (p=client_head; p; p = p->next) {
if (strcmp(clientname,p->clientname)==0) {
return(p);
}
}
/* else, make one up */
p = MallocZ(sizeof(struct client_info));
p->next = client_head;
client_head = p;
p->clientname = strdup(clientname);
p->plotter = NO_PLOTTER;
return(p);
}
void
http_read(
struct ip *pip, /* the packet */
tcp_pair *ptp, /* info I have about this connection */
void *plast, /* past byte in the packet */
void *mod_data) /* module specific info for this connection */
{
struct tcphdr *ptcp;
unsigned tcp_length;
unsigned tcp_data_length;
char *pdata;
struct http_info *ph = mod_data;
/* find the start of the TCP header */
ptcp = (struct tcphdr *) ((char *)pip + 4*IP_HL(pip));
tcp_length = ntohs(pip->ip_len) - (4 * IP_HL(pip));
tcp_data_length = tcp_length - (4 * TH_OFF(ptcp));
/* verify port */
if ((ntohs(ptcp->th_sport) != httpd_port) && (ntohs(ptcp->th_dport) != httpd_port))
return;
/* find the data */
pdata = (char *)ptcp + (unsigned)TH_OFF(ptcp)*4;
/* for client, record both ACKs and DATA time stamps */
if (ph && IS_CLIENT(ptcp)) {
if (tcp_data_length > 0) {
AddGetTS(ph,DataOffset(ph->tcb_client,ntohl(ptcp->th_seq)));
}
if (ACK_SET(ptcp)) {
if (debug > 4)
printf("Client acks %ld\n", DataOffset(ph->tcb_server,ntohl(ptcp->th_ack)));
AddAckTS(ph,DataOffset(ph->tcb_server,ntohl(ptcp->th_ack)));
}
}
/* for server, record DATA time stamps */
if (ph && IS_SERVER(ptcp)) {
if (tcp_data_length > 0) {
AddDataTS(ph,DataOffset(ph->tcb_server,ntohl(ptcp->th_seq)));
if (debug > 5) {
printf("Server sends %ld thru %ld\n",
DataOffset(ph->tcb_server,ntohl(ptcp->th_seq)),
DataOffset(ph->tcb_server,ntohl(ptcp->th_seq))+tcp_data_length-1);
}
}
}
/* we also want the time that the FINs were sent */
if (ph && FIN_SET(ptcp)) {
if (IS_SERVER(ptcp)) {
/* server */
if (ZERO_TIME(&(ph->s_fin_time)))
ph->s_fin_time = current_time;
} else {
/* client */
if (ZERO_TIME(&ph->c_fin_time))
ph->c_fin_time = current_time;
}
}
/* we also want the time that the SYNs were sent */
if (ph && SYN_SET(ptcp)) {
if (IS_SERVER(ptcp)) {
/* server */
if (ZERO_TIME(&ph->s_syn_time))
ph->s_syn_time = current_time;
} else {
/* client */
if (ZERO_TIME(&ph->c_syn_time))
ph->c_syn_time = current_time;
}
}
}
static double
ts2d(timeval *pt)
{
double d;
d = pt->tv_sec;
d += (double)pt->tv_usec/1000000;
return(d);
}
static void
MFMap(
MFILE *mf,
char **firstbyte,
char **lastbyte)
{
int fd;
char *vaddr;
int len;
/* find length of file */
if (Mfseek(mf,0,SEEK_END) != 0) {
perror("fseek");
exit(-1);
}
len = Mftell(mf);
if (len == 0) {
*firstbyte = NULL;
*lastbyte = NULL;
return;
}
/* Memory map the entire file */
fd = Mfileno(mf);
vaddr = mmap((caddr_t) 0, /* put it anywhere */
len, /* fixed size */
PROT_READ, /* read only */
MAP_PRIVATE, /* won't be sharing... */
fd, /* attach to 'fd' */
(off_t) 0); /* ... offset 0 in 'fd' */
if (vaddr == (char *) -1) {
perror("mmap");
exit(-1);
}
*firstbyte = vaddr;
*lastbyte = vaddr+len-1;
return;
}
static void
MFUnMap(
MFILE *mf,
char *firstbyte)
{
int fd;
int len;
/* find length of file */
if (Mfseek(mf,0,SEEK_END) != 0) {
perror("fseek");
exit(-1);
}
len = Mftell(mf);
/* unmap it */
fd = Mfileno(mf);
if (munmap(firstbyte,len) != 0) {
perror("munmap");
exit(-1);
}
return;
}
static void
HttpGather(
struct http_info *ph)
{
while (ph) {
if (ph->tcb_client->extr_contents_file &&
ph->tcb_server->extr_contents_file)
{
FindGets(ph);
FindContent(ph);
}
ph = ph->next;
}
}
static void
PrintTSChain(
struct time_stamp *phead,
struct time_stamp *ptail)
{
struct time_stamp *pts;
for (pts = phead->next; pts != ptail; pts = pts->next) {
printf("Pos: %ld time: %s\n",
pts->position, ts2ascii(&pts->thetime));
}
}
/* when was the byte at offset "position" acked?? */
/* return the timeval for the record of the smallest position >= "position" */
static timeval
WhenAcked(
struct time_stamp *phead,
struct time_stamp *ptail,
u_long position)
{
struct time_stamp *pts;
timeval epoch = {0,0};
if (debug > 10) {
printf("pos:%ld, Chain:\n", position);
PrintTSChain(phead,ptail);
}
for (pts = phead->next; pts != NULL; pts = pts->next) {
/* fprintf(stderr,"Checking pos %ld against %ld\n", */
/* position, pts->position); */
if (pts->position >= position) {
/* sent after this one */
return(pts->thetime);
}
}
/* fails if we can't find it */
return(epoch);
}
/* when was the byte at offset "position" sent?? */
/* return the timeval for the record of the largest position <= "position" */
static timeval
WhenSent(
struct time_stamp *phead,
struct time_stamp *ptail,
u_long position)
{
struct time_stamp *pts;
timeval epoch = {0,0};
if (debug > 10) {
printf("pos:%ld, Chain:\n", position);
PrintTSChain(phead,ptail);
}
for (pts = ptail->prev; pts != phead; pts = pts->prev) {
/* fprintf(stderr,"Checking pos %ld against %ld\n", */
/* position, pts->position); */
if (pts->position <= position) {
/* sent after this one */
return(pts->thetime);
}
}
/* fails if we can't find it */
return(epoch);
}
static void
FindContent(
struct http_info *ph)
{
tcb *tcb = ph->tcb_server;
MFILE *mf = tcb->extr_contents_file;
char *pdata;
char *plast;
char *pch;
char *pch2;
struct get_info *pget;
char getbuf[1024];
u_long position = 0;
unsigned last_position = 0;
int done;
int i;
typedef enum {
ContentStateStartHttp,
ContentStateFinishHttp,
ContentStateFindResponse,
ContentStateFindContentLength,
ContentStateFinishHeader} StateType;
StateType state;
pget = ph->gets_head;
if ((mf) && (pget)) {
state = ContentStateStartHttp;
done = 0;
/* Memory map the entire file (I hope it's short!) */
MFMap(mf,&pdata,&plast);
if (pdata == NULL) {
return;
}
pch = pdata;
ph->total_reply_length = (unsigned) (plast - pdata);
ph->total_reply_count= 0;
while ((!done) && (pch <= (char *) plast)) {
switch (state) {
/* Start state: Find "HTTP/" that begins a response */
case (ContentStateStartHttp): {
if (strncasecmp(pch, "HTTP/", 5) == 0) {
/* Found start of a response */
state = ContentStateFinishHttp;
position = pch - pdata + 1;
pget->reply_position = position;
pch += 5;
}
else {
pch++;
}
}
break;
/* Finish off HTTP string (version number) by looking for whitespace */
case (ContentStateFinishHttp): {
if (*(pch) == ' ') {
state = ContentStateFindResponse;
}
else {
pch++;
}
}
break;
/* Look for response code by finding non-whitespace. */
case (ContentStateFindResponse): {
if (*(pch) != ' ') {
pget->response_code = atoi(pch);
pch += 3;
state = ContentStateFindContentLength;
}
else {
pch++;
}
}
break;
/* this state is now misnamed since we pull out other */
/* headers than just content-length now. */
case (ContentStateFindContentLength): {
if (strncasecmp(pch, "\r\nContent-Length:", 17) == 0) {
/* Got content-length field, ignore rest of header */
pget->content_length = atoi(&(pch[17]));
pch += 18;
}
else if (strncasecmp(pch, "\r\nContent-Type:", 15) == 0) {
/* Get content-type field, skipping leading spaces */
pch += 15;
while (*pch == ' ') {
pch++;
}
for (i=0,pch2 = pch; ; ++i, ++pch2) {
if ((*pch2 == '\n') || (*pch2 == '\r') ||
(i >= sizeof(getbuf)-1)) {
getbuf[i] = '\00';
pch = pch2; /* skip forward */
break;
}
getbuf[i] = *pch2;
}
/* If there are any spaces in the Content-Type */
/* field, we need to truncate at that point */
{
char *sp;
sp = (char *)index(getbuf, ' ');
if (sp) {
*sp = '\00';
}
}
pget->content_type = strdup(getbuf);
}
else if (strncmp(pch, "\r\n\r\n", 4) == 0) {
/* No content-length header detected */
/* No increment for pch here, effectively fall through */
/* pget->content_length = 0; */
state = ContentStateFinishHeader;
}
else {
pch++;
}
}
break;
/* Skip over the rest of the header */
case (ContentStateFinishHeader): {
if (strncmp(pch, "\r\n\r\n", 4) == 0) {
/* Found end of header */
pch += 4;
/*
* At this point, we need to find the end of the
* response body. There's a variety of ways to
* do this, but in any case, we need to make sure
* that pget->content_length, pch, and last_postiion
* are all set appropriately.
*
* See if we can ignore the body. We can do this
* for the reply to HEAD, for a 204 (no content),
* 205 (reset content), or 304 (not modified).
*/
if ((pget->method == MethodCodeHead) ||
(pget->response_code == 204) ||
(pget->response_code == 205) ||
(pget->response_code == 304)) {
pget->content_length = 0;
}
/*
* Use content-length header if one was present.
* XXX is content_length > 0 the right test?
*/
else if (pget->content_length > 0) {
pch += pget->content_length;
}
/*
* No content-length header, so delimit response
* by end of file.
*
* But, make sure we do not have a "\r\n\r\n" string
* in the response, because that might indicate the
* beginning of a following response.
* (Patch from Yufei Wang)
*/
else {
char *start = pch;
while (pch <= (char *)plast) {
if (strncmp(pch, "\r\n\r\n", 4) == 0) {
pch += 4;
state = ContentStateStartHttp;
break;
} else {
pch++;
}
}
if (state == ContentStateStartHttp) {
pget->content_length = pch - start;
} else {
/* calculate the content length */
pget->content_length = plast - start + 1;
pch = plast + 1;
}
}
/* Set next state and do original tcptrace
* processing based on what we learned above.
*/
state = ContentStateStartHttp;
last_position = pch - pdata + 1;
/* when was the first byte sent? */
pget->send_time = WhenSent(&ph->data_head,&ph->data_tail,position);
/* when was the LAST byte sent? */
pget->lastbyte_time = WhenSent(&ph->data_head,&ph->data_tail,last_position);
/* when was the last byte ACKed? */
if (debug > 4)
printf("Content length: %d\n", pget->content_length);
pget->ack_time = WhenAcked(&ph->ack_head,&ph->ack_tail,last_position);
/* increment our counts */
ph->total_reply_count++;
/* skip to the next request */
pget = pget->next;
if (!pget) {
/* no more questions, quit */
done = 1;
break;
}
}
else {
pch++;
}
}
break;
}
}
MFUnMap(mf,pdata);
}
else {
if (debug > 4) {
printf("FindContent() with null server contents");
}
}
}
static char * formatGetString(char * s)
{
int len = strlen(s);
int i = 0;
int j = 0;
char *buf = (char *)malloc(len);
char ascii[2];
while (i < len) {
if (s[i] == '%') {
ascii[0] = s[i+1];
ascii[1] = s[i+2];
buf[j++] = atoi(ascii);
i = i+3;
} else {
buf[j++] = s[i];
i++;
}
}
buf[j] = 0;
return buf;
}
static void
FindGets(
struct http_info *ph)
{
tcb *tcb = ph->tcb_client;
MFILE *mf = tcb->extr_contents_file;
char *pdata;
char *plast;
char *pch;
char *pch2;
struct get_info *pget = NULL;
char getbuf[1024];
u_long position = 0;
int j;
int methodlen;
unsigned long long contentLength = 0;
typedef enum {
GetStateStartMethod,
GetStateFinishMethod,
GetStateFindContentLength,
GetStateFinishHeader
} StateType;
StateType state;
if (mf) {
/* Memory map the entire file (I hope it's short!) */
MFMap(mf,&pdata,&plast);
if (pdata == NULL) {
return;
}
ph->total_request_length = (unsigned) (plast - pdata);
ph->total_request_count = 0;
state = GetStateStartMethod;
/* search for method string*/
pch = pdata;
while (pch <= (char *)plast) {
switch (state) {
/* Start state: Find access method keyword */
case (GetStateStartMethod): {
/* Try to find a word describing a method. These
* are all the methods defined in
* draft-ietf-http-v11-spec-rev-06
*/
MethodCode method = MethodCodeUnknown;
methodlen = 0;
if (strncasecmp(pch, "options ", 8) == 0) {
methodlen = 8;
method = MethodCodeOptions;
}
else if (strncasecmp(pch, "get ", 4) == 0) {
methodlen = 4;
method = MethodCodeGet;
}
else if (strncasecmp(pch, "head ", 5) == 0) {
methodlen = 5;
method = MethodCodeHead;
}
else if (strncasecmp(pch, "post ", 5) == 0) {
methodlen = 5;
method = MethodCodePost;