Analysis/Listener/HTTP.pm untested

filter: Statementstmt 0.0% 0 / 122 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 13 totaltotal 0.0% 0 / 207 CC 19 SCAR 31.0
CC
19
Cov
0%
tcp_monologue
72.0
_unchunk_response
30.0
tcp_session_end
12.0
CRAP 22.1
1 package Net::Analysis::Listener::HTTP;
2 # $Id: HTTP.pm 140 2005-10-21 16:31:29Z abworrall $
3  
4 # {{{ Boilerplate
5  
6 use 5.008000;
7 our $VERSION = '0.02';
8 use strict;
9 use warnings;
10  
11 use Carp qw(carp croak confess);
12  
13 use Params::Validate qw(:all);
14  
15 use base qw(Net::Analysis::Listener::Base);
16  
17 # }}}
18  
19 use HTTP::Response;
20 use HTTP::Request;
21  
22 #### Callbacks
23 #
24 # {{{ validate_configuration
25  
26 sub validate_configuration {
27 my $self = shift;
28  
29 my %h = validate (@_, { v => {type => SCALAR,
30 default => 0} });
31  
32 return \%h;
33 }
34  
35 # }}}
36  
37 # {{{ setup
38  
39 # A chance to setup stuff for our listener
40 sub setup {
41 my ($self) = shift;
42  
43 $self->{sesh} = {}; # TCP sessions
44 }
45  
46 # }}}
47 # {{{ teardown
48  
49 sub teardown {
50 my ($self) = shift;
51 }
52  
53 # }}}
54  
55 # {{{ tcp_session_start
56  
57 sub tcp_session_start {
58 my ($self, $args) = @_;
59 my $pkt = $args->{pkt}; # Might well be undef
60 my $k = $args->{socketpair_key};
61  
62 if ($self->{v} & 0x04) {
63 $self->trace (" ==== tcp session start [$pkt->{from} -> $pkt->{to}]");
64 }
65 }
66  
67 # }}}
68 # {{{ tcp_session_end
69  
70 sub tcp_session_end {
71 my ($self, $args) = @_;
72 my $pkt = $args->{pkt}; # Might well be undef
73 my $k = $args->{socketpair_key};
74  
75 $self->trace(" ==== tcp session end [$k]") if ($self->{v} & 0x04);
76  
77 my $sesh = $self->_remove_sesh ($k);
78  
79 if (defined $sesh) {
80 # XXXX we have not yet seen the response to this ...
81 delete ($sesh->{req_mono}); # In case it ends up leaking _somehow_
82 }
83 }
84  
85 # }}}
86 # {{{ tcp_monologue
87  
88 sub tcp_monologue {
89 my ($self, $args) = @_;
90 my $k = $args->{socketpair_key};
91 my $mono = $args->{monologue};
92 my $sesh = $self->_get_sesh ($k);
93 my $d = $mono->{data};
94  
95 my ($l) = (split('\n', $d))[0];
96 my ($first_line) = '';
97 if (defined $l) {
98 $l = substr($l,0,40) if (length($l) > 40);
99 $first_line = _printable($l);
100 }
101  
102 our $TRACE=0;
103  
104 # $TRACE = 1 if ($k eq '10.6.94.7:8080-159.206.22.101:2647');
105 # if ($k eq '10.6.94.7:8080-159.206.22.101:2647') {
106 # print "mono $k ".$mono->first_packet()->{time}."\n";
107 # }
108  
109 if ($d =~ m!^(get|post|head)\s+([^ ]+)(HTTP/\d.\d)?!i) {
110 if (exists $sesh->{req}) {
111 carp "already have a req for $k, overwriting it\n";
112 }
113 $sesh->{req} = HTTP::Request->parse($d);
114 $sesh->{req_mono} = $mono; # Careful ! Must delete this ...
115  
116 my $host = $sesh->{req}->header('host') || '(nohost)';
117 my $uri = $sesh->{req}->uri() || '/noURI';
118 $self->_trace (">>!> $host$uri <<\n");
119  
120  
121 } elsif ($d =~ m!^HTTP/\d.\d\s+(\d{3})!i) {
122 my $resp = HTTP::Response->parse($d);
123  
124 _unchunk_response ($resp); # Should really port this to HTTP::Message
125  
126 if (defined $sesh->{req}) {
127 my $host = $sesh->{req}->header('host') || '(nohost)';
128 my $uri = $sesh->{req}->uri() || '/noURI';
129 $self->_trace (">>>> $host$uri <<\n");
130 } else {
131 $self->_trace (">>>> ????? (no req found in sesh) <<\n");
132 }
133  
134 $self->_trace (" << ".$resp->code().", ".
135 length($resp->content())." bytes");
136  
137 my $req_mono = $sesh->{req_mono};
138 my $args = {socketpair_key => $k,
139 req => $sesh->{req},
140 req_mono => $req_mono,
141 resp => $resp,
142 resp_mono => $mono,
143 t_end => $mono->t_end()->clone()};
144 if (defined $req_mono) {
145 $args->{t_start} = $sesh->{req_mono}->t_start()->clone();
146 $args->{t_elapsed} = $args->{t_end} - $args->{t_start};
147 }
148  
149 $self->emit (name => 'http_transaction', args => $args);
150  
151 delete ($sesh->{req});
152 delete ($sesh->{req_mono});
153  
154 } else {
155 $self->_trace ("malformed HTTP monologue in $k starts: $first_line\n");
156 }
157 }
158  
159 # }}}
160 # {{{ http_transaction
161  
162 # Listen to our own event and print very basic report, if asked
163 sub http_transaction {
164 my ($self, $args) = @_;
165 return if (! $self->{v});
166  
167 my $req = $args->{req};
168 my $uri = (defined $req) ? $req->uri() : "(no uri)";
169 my $t = $args->{t_elapsed} || -1.0;
170 printf "%8.4fs : %s\n", $t, $uri;
171 }
172  
173 # }}}
174  
175 # {{{ as_string
176  
177 sub as_string {
178 my ($self) = @_;
179 my $s = '';
180  
181 $s .= "[".ref($self)."]";
182  
183 return $s;
184 }
185  
186 # }}}
187  
188 #### Support funcs
189 #
190 # {{{ _trace
191  
192 # This may become more clever ...
193  
194 our $TRACE=0;
195  
196 sub _trace {
197 my ($self) = shift;
198  
199 return if (! $TRACE);
200  
201 foreach (@_) {
202 my $l = $_; # Skip 'Modification of a read-only value' errors
203 chomp ($l);
204 print "$l\n";
205 }
206 }
207  
208 # }}}
209 # {{{ _{get|remove}_sesh
210  
211 sub _get_sesh {
212 my ($self, $sesh_key) = @_;
213  
214 if (! exists $self->{sesh}{$sesh_key}) {
215 $self->{sesh}{$sesh_key} = {};
216 }
217  
218 return $self->{sesh}{$sesh_key};
219 }
220  
221 sub _remove_sesh {
222 my ($self, $sesh_key) = @_;
223  
224 return delete ($self->{sesh}{$sesh_key});
225 }
226  
227 # }}}
228 # {{{ _printable
229  
230 sub _printable {
231 my $raw = shift;
232 $raw =~ s {([^\x20-\x7e])} {.}g;
233 return $raw;
234 }
235  
236 # }}}
237 # {{{ _unchunk_response
238  
239 sub _unchunk_response {
240 my ($resp) = @_;
241  
242 my $transfer_encoding = $resp->header('transfer-encoding');
243  
244 return if (!$transfer_encoding);
245  
246 # http://www.jmarshall.com/easy/http/#http1.1c2
247 if ($transfer_encoding eq 'chunked') {
248 my $chunked_data = $resp->content();
249 my $unchunked_data = '';
250  
251 my ($chunk_size_hex, $chunk_size, $chunk);
252 while ($chunked_data) {
253 # Read chunk size. Discard chunking comments.
254 ($chunk_size_hex, $chunked_data) = ($chunked_data =~ /^([0-9a-fA-F]+)(?:;.*)?\r\n(.*)/s);
255 last if (!defined $chunk_size_hex);
256 $chunk_size = oct("0x$chunk_size_hex");
257  
258 last if ($chunk_size == 0); # Sod trailing headers!
259  
260 # allow for \r\n trailing the chunk
261 $chunk = substr ($chunked_data, 0, $chunk_size+2, '');
262 substr ($chunk, -2, 2, '');
263  
264 $unchunked_data .= $chunk;
265 }
266  
267 $resp->content($unchunked_data);
268 }
269 }
270  
271 # }}}
272  
273 1;
274 __END__