@@ -24,48 +24,87 @@ def __init__(self):
2424 self .gunzip = False # Not interested in response body
2525
2626 def HTTPHandler (self , conn , request , response , requesttime , responsetime ):
27- host = ''
28- loc = ''
29- lastmodified = ''
30-
31- #request_time, request, response = self.httpDict[conn.addr]
32-
33- # extract method,uri,host from response
34- host = util .getHeader (request , 'host' )
35- if host == '' :
36- host = conn .serverip
3727
38- try :
39- status = response .status
40- except :
41- status = ''
42- try :
43- reason = response .reason
44- except :
45- reason = ''
28+ #
29+ # Establish kw_items dictionary for extracted details from tcp/ip layer and request/response
30+ #
31+ kw_items = conn .info ()
32+
33+ #
34+ # Extract useful information from HTTP *request*
35+ #
36+ for h in request .headers .keys ():
37+ kw_items [h ] = util .getHeader (request , h )
38+ # Rename user-agent for backward compatability
39+ if 'user-agent' in kw_items :
40+ kw_items ['useragent' ] = kw_items .pop ('user-agent' )
41+
42+ # Override non-existent host header with server IP address
43+ if kw_items ['host' ] == '' :
44+ kw_items ['host' ] = conn .serverip
45+
46+ # request info string for standard output
47+ requestInfo = '%s %s%s HTTP/%s' % (request .method ,
48+ kw_items ['host' ] if kw_items ['host' ] != request .uri else '' , # With CONNECT method, the URI is or contains the host, making this redudant
49+ request .uri [:self .maxurilen ] + '[truncated]' if self .maxurilen > 0 and len (
50+ request .uri ) > self .maxurilen else request .uri ,
51+ request .version )
4652
53+ #
54+ # Extract useful information from HTTP *response* (if available)
55+ #
56+ status = ''
57+ reason = ''
58+ responsesize = 0
4759 loc = ''
48- if status [:2 ] == '30' :
49- loc = util .getHeader (response , 'location' )
50- if len (loc ):
51- loc = '-> ' + loc
60+ lastmodified = ''
61+ md5 = ''
62+ if response != None :
5263
53- lastmodified = util . HTTPlastmodified ( response )
54- referer = util . getHeader ( request , 'referer' )
55- useragent = util . getHeader ( request , 'user-agent' )
56- via = util . getHeader ( request , 'via' )
64+ try :
65+ responsesize = len ( response . body . rstrip ( ' \0 ' ) )
66+ except :
67+ responsesize = 0
5768
58- try :
59- responsesize = len (response .body .rstrip ('\0 ' ))
60- except :
61- responsesize = 0
69+ if self .md5 :
70+ md5 = self ._bodyMD5 (response )
71+ else :
72+ md5 = ''
73+
74+ try :
75+ status = response .status
76+ except :
77+ status = ''
78+ try :
79+ reason = response .reason
80+ except :
81+ reason = ''
82+
83+ for h in response .headers .keys ():
84+ if not h in kw_items :
85+ kw_items [h ] = util .getHeader (response , h )
86+ else :
87+ kw_items ['server_' + h ] = util .getHeader (response , h )
88+ if 'content-type' in kw_items :
89+ kw_items ['contenttype' ] = kw_items .pop ('content-type' )
90+
91+ loc = ''
92+ if status [:2 ] == '30' :
93+ loc = util .getHeader (response , 'location' )
94+ if len (loc ):
95+ loc = '-> ' + loc
96+
97+ lastmodified = util .HTTPlastmodified (response )
98+
99+ # response info string for standard output
100+ responseInfo = '%s %s %s %s' % (status , reason , loc , lastmodified )
62101
63- if self .md5 :
64- md5 = self ._bodyMD5 (response )
65102 else :
66- md5 = ''
103+ responseInfo = ''
67104
105+ #
68106 # File objects
107+ #
69108 try :
70109 if len (response .body ) > 0 :
71110 responsefile = dfile .dfile (
@@ -80,18 +119,14 @@ def HTTPHandler(self, conn, request, response, requesttime, responsetime):
80119 else :
81120 uploadfile = None
82121
83- requestInfo = '%s %s%s HTTP/%s' % (request .method ,
84- host if host != request .uri else '' , # With CONNECT method, the URI is or contains the host, making this redudant
85- request .uri [:self .maxurilen ] + '[truncated]' if self .maxurilen > 0 and len (
86- request .uri ) > self .maxurilen else request .uri ,
87- request .version )
88- if response :
89- responseInfo = '%s %s %s %s' % (status , reason , loc , lastmodified )
90- else :
91- responseInfo = ''
122+ #
123+ # Call alert with text info and kw values
124+ #
125+ self .alert ("%-80s // %s" % (requestInfo , responseInfo ), request = requestInfo , response = responseInfo ,
126+ request_time = requesttime , response_time = responsetime , request_method = request .method ,
127+ uri = request .uri , status = status , reason = reason , lastmodified = lastmodified ,
128+ md5 = md5 , responsesize = responsesize , responsefile = responsefile , uploadfile = uploadfile , ** kw_items )
92129
93- self .alert ("%-80s // %s" % (requestInfo , responseInfo ), referer = referer , useragent = useragent , request = requestInfo , response = responseInfo , request_time = requesttime , response_time = responsetime , request_method = request .method , host = host ,
94- uri = request .uri , status = status , reason = reason , lastmodified = lastmodified , md5 = md5 , responsesize = responsesize , contenttype = util .getHeader (response , 'content-type' ), responsefile = responsefile , uploadfile = uploadfile , via = via , ** conn .info ())
95130 if self .out .sessionwriter :
96131 self .write (request .data , direction = 'cs' )
97132 if response :
0 commit comments