blib/lib/HTTP/Parser.pm

filter: Statementstmt 68.6% 83 / 121 Branchbran 43.4% 33 / 76 Conditioncond 29.1% 7 / 24 MC/DCmcdc 14.2% 2 / 14 Subroutinesub 78.5% 11 / 14 Podpod 100% 6 / 6 totaltotal 55.6% 142 / 255 CC 50 SCAR 38.7
CC
50
Cov
56%
_parse_chunk
90.0
_parse_header
77.6
add
14.4
CRAP 48.1
↑ Summary
1 =head1 NAME
2  
3 HTTP::Parser - parse HTTP/1.1 request into HTTP::Request/Response object
4  
5 =head1 SYNOPSIS
6  
7 my $parser = HTTP::Parser->new();
8  
9 ...
10  
11 my $status = $parser->add($text);
12  
13 if(0 == $status) {
14 print "request: ".$parser->request()->as_string(); # HTTP::Request
15 } elsif(-3 == $status) {
16 print "no content length header!\n";
17 } elsif(-2 == $status) {
18 print "need a line of data\n";
19 } elsif(-1 == $status) {
20 print "need more data\n";
21 } else { # $status > 0
22 print "need $status byte(s)\n";
23 }
24  
25 =head1 DESCRIPTION
26  
27 This is an HTTP request parser. It takes chunks of text as received and
28 returns a 'hint' as to what is required, or returns the HTTP::Request when
29 a complete request has been read. HTTP/1.1 chunking is supported. It dies
30 if it finds an error.
31  
32 =cut
33 1 ▶use 5.006_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 package HTTP::Parser;
37  
38 1 our $VERSION = '0.06';
39  
40 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 1 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 # token is (RFC 2616, ASCII)
45 1 my $Token =
46 qr/[\x21\x23-\x27\x2a\x2b\x2d\x2e\x30-\x39\x41-\x5a\x5e-\x7a\x7c\x7e]+/;
47  
48  
49 =head2 new ( named params... )
50  
51 Create a new HTTP::Parser object. Takes named parameters, e.g.:
52  
53 my $parser = HTTP::Parser->new(request => 1);
54  
55 =over 4
56  
57 =item request
58  
59 Allows or denies parsing an HTTP request and returning an C<HTTP::Request>
60 object.
61  
62 =item response
63  
64 Allows or denies parsing an HTTP response and returning an C<HTTP::Response>
65 object.
66  
67 =back
68  
69 If you pass neither C<request> nor C<response>, only requests are parsed (for
70 backwards compatibility); if you pass either, the other defaults to false
71 (disallowing both requests and responses is a fatal error).
72  
73 =cut
74 4 ▶sub new {
Subroutine new: called CC=6 SCAR=18.5
Pod documented
75 4 my ($class, %p) = @_;
76 4 ▶ $p{request} = 1 unless exists $p{response} or exists $p{request};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p{"response"} or exists $p{"request"} 2 2 4
Condition100%3/3
exists $p{"response"} or exists $p{"request"}
l!l&&r!l&&!r
112
MC/DC100%2/2
exists $p{"response"} or exists $p{"request"}
exists $p{"response"} exists $p{"request"}
Truth table100%3/3
A or B
A= exists $p{"response"}B= exists $p{"request"}
ABresult
1X1
011
000
77 die 'must allow request or response to be parsed'
78 4 ▶ unless $p{request} or $p{response};
Branch: 1 branch
BranchTrueFalseTotal
unless $p{"request"} or $p{"response"} 0 4 4
Condition66%2/3
$p{"request"} or $p{"response"}
l!l&&r!l&&!r
310
MC/DC0%0/2
$p{"request"} or $p{"response"}
$p{"request"} $p{"response"}
Truth table66%2/3
A or B
A= $p{"request"}B= $p{"response"}
ABresult
1X1
011
000
79 4 @p{qw(state data)} = ('blank', '');
80 4 ▶ my $self = bless \%p, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
040
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
81 4 return $self;
82 }
83  
84  
85 =head2 add ( string )
86  
87 Parse request. Returns:
88  
89 =over 8
90  
91 =item 0
92  
93 if finished (call C<object> to get an HTTP::Request or Response object)
94  
95 =item -1
96  
97 if not finished but not sure how many bytes remain
98  
99 =item -2
100  
101 if waiting for a line (like 0 with a hint)
102  
103 =item -3
104  
105 if there was no content-length header, so we can't tell whether we are
106 waiting for more data or not.
107  
108 If you are reading from a TCP stream, you can keep adding data until
109 the connection closes gracefully (the HTTP RFC allows this).
110  
111 If you are reading from a file, you should keep adding until you have
112 all the data.
113  
114 Once you have added all data, you may call C<object>. if you are not
115 sure whether you have all the data, the HTTP::Response object might be
116 incomplete.
117  
118 =item count
119  
120 if waiting for that many bytes
121  
122 =back
123  
124 Dies on error.
125  
126 This method of parsing makes it easier to parse a request from an event-based
127 system, on the other hand, it's quite alright to pass in the whole request.
128 Ideally, the first chunk passed in is the header (up to the double newline),
129 then whatever byte counts are requested.
130  
131 When a request object is returned, the X-HTTP-Version header has the HTTP
132 version, the uri() method will always return a URI object, not a string.
133  
134 Note that a nonzero return is just a hint, and any amount of data can be
135 passed in to a subsequent add() call.
136  
137 =cut
138 13 ▶sub add {
Subroutine add: called CC=10 SCAR=26.7
Pod documented
139 13 my ($self,$s) = @_;
140 13 ▶ $s = '' if not defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 13 13
141  
142 13 $self->{data} .= $s;
143  
144 # pre-header blank lines are allowed (RFC 2616 4.1)
145 13 ▶ if($self->{state} eq 'blank') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"state"} eq "blank") 5 8 13
146 5 $self->{data} =~ s/^(\x0d?\x0a)+//;
147 5 ▶ return -2 unless length $self->{data};
Branch: 1 branch
BranchTrueFalseTotal
unless length $self->{"data"} 1 4 5
148 4 $self->{state} = 'header'; # done with blank lines; fall through
149 }
150  
151 # still waiting for the header
152 12 ▶ if($self->{state} eq 'header') {
Branch: 4 branches
BranchTrueFalseTotal
if ($self->{'state'} eq 'header') { } 11 1 12
elsif ($self->{'state'} eq 'body') { } 1 0 1
elsif ($self->{'state'} eq 'chunked') { } 0 0 0
elsif ($self->{'state'} eq 'trailer') { } 0 0 0
153 # double line break indicates end of header; parse it
154 11 ▶ if($self->{data} =~ /^(.*?)\x0d?\x0a\x0d?\x0a/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"data"} =~ /^(.*?)\x0d?\x0a\x0d?\x0a/s) 4 7 11
155 4 return $self->_parse_header(length $1);
156 }
157 7 return -2; # still waiting for unknown amount of header lines
158  
159 # waiting for main body of request
160 } elsif($self->{state} eq 'body') {
161 1 return $self->_parse_body();
162  
163 # chunked data
164 } elsif($self->{state} eq 'chunked') {
165 0 return $self->_parse_chunk();
166  
167 # trailers
168 } elsif($self->{state} eq 'trailer') {
169 # double line break indicates end of trailer; parse it
170 return $self->_parse_header(length $1,1)
171 0 ▶ if $self->{data} =~ /^(.*?)\x0d?\x0a\x0d?\x0a/s;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"} =~ /^(.*?)\x0d?\x0a\x0d?\x0a/s 0 0 0
172 0 return -1; # still waiting for unknown amount of trailer data
173 }
174  
175 0 die "unknown state '$self->{state}'";
176 }
177  
178  
179 =head2 data
180  
181 Returns current data not parsed. Mainly useful after a request has been
182 parsed. The data is not removed from the object's buffer, and will be
183 seen before the data next passed to add().
184  
185 =cut
186 0 ▶sub data {
Subroutine data: not called CC=1 SCAR=6.9
Pod documented
187 shift->{data}
188 0 }
189  
190  
191 =head2 extra
192  
193 Returns the count of extra bytes (length of data()) after a request.
194  
195 =cut
196 0 ▶sub extra {
Subroutine extra: not called CC=1 SCAR=6.9
Pod documented
197 length shift->{data}
198 0 }
199  
200  
201 =head2 object
202  
203 Returns the object request. Only useful after the parse has completed.
204  
205 =cut
206 1 ▶sub object {
Subroutine object: called CC=1 SCAR=0.0
Pod documented
207 shift->{obj}
208 1 }
209  
210 # keep this for compatibility with 0.02
211 2 ▶sub request {
Subroutine request: called CC=1 SCAR=0.0
Pod documented
212 shift->{obj}
213 2 }
214  
215  
216 # _parse_header ( position of double newline in data [, trailer flag] )
217 #
218 # helper for parse that parses an HTTP header
219 # prerequisite: we have data up to a double newline in $self->{data}
220 # if the trailer flag is set, we're parsing trailers
221 #
222 4 ▶sub _parse_header {
Subroutine _parse_header: called CC=26 SCAR=43.5
223 4 my ($self,$eoh,$trailer) = @_;
224 4 my $header = substr($self->{data},0,$eoh,'');
225 4 $self->{data} =~ s/^\x0d?\x0a\x0d?\x0a//;
226  
227 # parse into lines
228 4 my @header = split /\x0d?\x0a/,$header;
229 4 ▶ my $request = shift @header unless $trailer;
Branch: 1 branch
BranchTrueFalseTotal
unless $trailer 4 0 4
230  
231 # join folded lines
232 4 my @out;
233 4 for(@header) {
234 5 ▶ if(s/^[ \t]+//) {
Branch: 1 branch
BranchTrueFalseTotal
if (s/^[ \t]+//) { } 0 5 5
235 0 ▶ die 'LWS on first header line' unless @out;
Branch: 1 branch
BranchTrueFalseTotal
unless @out 0 0 0
236 0 $out[-1] .= $_;
237 } else {
238 5 push @out, $_;
239 }
240 }
241  
242 # parse request or response line
243 4 my $obj;
244 4 ▶ unless($trailer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trailer) { } 0 4 4
245 4 my ($major, $minor);
246  
247 # is it an HTTP response?
248 4 ▶ if ($request =~ /^HTTP\/(\d+)\.(\d+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request =~ m[^HTTP/(\d+)\.(\d+)]i) { } 2 2 4
249 2 ▶ die 'HTTP responses not allowed' unless $self->{response};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"response"} 1 1 2
250 1 ($major,$minor) = ($1,$2);
251 1 $request =~ /^HTTP\/\d+\.\d+ (\d+) (.+)$/;
252 1 my $state = $1;
253 1 my $msg = $2;
254 1 $obj = $self->{obj} = HTTP::Response->new($state, $msg);
255  
256 # perhaps a request?
257 } else {
258 2 my ($method,$uri,$http) = split / /,$request;
259 2 ▶ die "'$request' is not the start of a valid HTTP request or response"
Branch: 1 branch
BranchTrueFalseTotal
unless $http and $http =~ m[^HTTP/(\d+)\.(\d+)$]i 0 2 2
Condition33%1/3
$http and $http =~ m[^HTTP/(\d+)\.(\d+)$]i
!ll&&!rl&&r
002
MC/DC0%0/2
$http and $http =~ m[^HTTP/(\d+)\.(\d+)$]i
$http $http =~ m[^HTTP/(\d+)\.(\d+)$]i
Truth table33%1/3
A and B
A= $httpB= $http =~ m[^HTTP/(\d+)\.(\d+)$]i
ABresult
0X0
100
111
260 unless $http and $http =~ /^HTTP\/(\d+)\.(\d+)$/i;
261 2 ($major,$minor) = ($1,$2);
262 2 ▶ die 'HTTP requests not allowed' unless $self->{request};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"request"} 0 2 2
263  
264 # If the Request-URI is an abs_path, we need to tell URI that we don't
265 # know the scheme, otherwise it will misinterpret paths that start with
266 # // as being scheme-relative uris, and will interpret the first
267 # component after // as the host (see rfc 2616)
268 2 ▶ $uri = "//$uri" if $uri =~ m(^/);
Branch: 1 branch
BranchTrueFalseTotal
if $uri =~ m[^/] 2 0 2
269 2 $obj = $self->{obj} = HTTP::Request->new($method, URI->new($uri));
270 }
271  
272 3 $obj->header(X_HTTP_Version => "$major.$minor"); # pseudo-header
273  
274 # we've already seen the initial line and created the object
275 } else {
276 0 $obj = $self->{obj};
277 }
278  
279 # import headers
280 3 my $token = qr/[^][\x00-\x1f\x7f()<>@,;:\\"\/?={} \t]+/;
281 3 for $header(@header) {
282 5 ▶ die "bad header name in '$header'" unless $header =~ s/^($token):[\t ]*//;
Branch: 1 branch
BranchTrueFalseTotal
unless $header =~ s/^($token):[\t ]*// 0 5 5
283 5 $obj->push_header($1 => $header);
284 }
285  
286 # if we're parsing trailers we don't need to look at content
287 3 ▶ return 0 if $trailer;
Branch: 1 branch
BranchTrueFalseTotal
if $trailer 0 3 3
288  
289 # see what sort of content we have, if any
290 3 ▶ if(my $length = $obj->header('content_length')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $length = $obj->header("content_length")) 1 2 3
291 1 s/^\s+//, s/\s+$// for $length;
292 1 ▶ die "bad content-length '$length'" unless $length =~ /^(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $length =~ /^(\d+)$/ 0 1 1
293 1 $self->{state} = 'body';
294 1 return $self->_parse_body();
295 }
296  
297 # check for transfer-encoding, and handle chunking
298 2 ▶ if(my @te = $obj->header('transfer_encoding')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@te) = $obj->header("transfer_encoding")) 0 2 2
299 0 ▶ if(grep { lc $_ eq 'chunked' } @te) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {lc $_ eq "chunked";} @te) 0 0 0
300 0 $self->{state} = 'chunked';
301 0 return $self->_parse_chunk();
302 }
303 }
304  
305 # section 14.13 of the spec says an HTTP response "SHOULD" return a
306 # content-length header unless there are reasons not to
307 # however, the same RFC does allow "end of connection" as a valid marker
308 # of the end of data and means the server does not need to set a content
309 # length header. the only status codes that "MAY NOT" return data are
310 # 1xx, 204 and 304.
311 # therefore if there is no content length header, return -3 to the caller
312 # so they can decide whether to keep feeding data. if using HTTP::Parser
313 # with data from tcp, you could assume that the end of a connection is
314 # the end of the response data
315 2 ▶ if($self->{response}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"response"}) 0 2 2
316 0 ▶ if (!defined $obj->header('content_length') &&
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $obj->header("content_length") and $self->object->code ne 204 and $self->object->code ne 304 and not $self->object->code =~ /1\d\d/) 0 0 0
Condition0%0/9
not defined $obj->header("content_length") and $self->object->code ne 204 and $self->object->code ne 304 and not $self->object->code =~ /1\d\d/
!ll&&!rl&&r
000
not defined $obj->header("content_length") and $self->object->code ne 204 and $self->object->code ne 304
!ll&&!rl&&r
000
not defined $obj->header("content_length") and $self->object->code ne 204
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $obj->header("content_length") and $self->object->code ne 204 and $self->object->code ne 304 and not $self->object->code =~ /1\d\d/
not defined $obj->header("content_length") $self->object->code ne 204 $self->object->code ne 304 not $self->object->code =~ /1\d\d/
Truth table0%0/5
not(A) and B and C and not(D)
A= not defined $obj->header("content_length")B= $self->object->code ne 204C= $self->object->code ne 304D= not $self->object->code =~ /1\d\d/
ABCDresult
0XXX0
10XX0
110X0
11100
11111
317 $self->object->code ne '204' &&
318 $self->object->code ne '304' &&
319 $self->object->code !~ /1\d\d/) {
320  
321 # Assume headers are finished and we are moving into body mode
322 0 $self->{state} = 'body';
323 0 $self->{no_content_length} = 1;
324  
325 # Parse any data that might be left
326 0 ▶ return $self->_parse_body() if length $self->data;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->data 0 0 0
327 0 return -3;
328 }
329 }
330  
331 # else we have no content so return success
332 2 return 0;
333 }
334  
335  
336 # _parse_body
337 #
338 # helper for parse, returns request object with content if done, else
339 # count of bytes remaining
340 #
341 2 ▶sub _parse_body {
Subroutine _parse_body: called CC=3 SCAR=11.3
342 2 my $self = shift;
343 2 my $length = $self->{obj}->header('content_length');
344  
345 # if the server didn't include a content length header, inform the
346 # caller. they may choose to ignore this response or wait for
347 # the end of connection (which is a valid reason to assume that
348 # the response is finished)
349 2 ▶ if($self->{no_content_length}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"no_content_length"}) 0 2 2
350 0 $self->{obj}->content($self->{data});
351 0 return -3;
352 }
353  
354 2 ▶ if(length $self->{data} >= $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"data"} >= $length) 1 1 2
355 1 $self->{obj}->content(substr($self->{data},0,$length,''));
356 1 return 0;
357 }
358 1 return $length-length $self->{data};
359 }
360  
361  
362 # _parse_chunk
363 #
364 # helper for parse, parse chunked transfer-encoded message; returns like parse
365 #
366 0 ▶sub _parse_chunk {
Subroutine _parse_chunk: not called CC=9 SCAR=45.0
367 0 my $self = shift;
368  
369 CHUNK:
370  
371 # need beginning of chunk with size
372 0 ▶ if(not $self->{chunk}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"chunk"}) 0 0 0
373 0 ▶ if($self->{data} =~ s/^([0-9a-fA-F]+)[^\x0d\x0a]*?\x0d?\x0a//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'data'} =~ s/^([0-9a-fA-F]+)[^\x0d\x0a]*?\x0d?\x0a//) { } 0 0 0
374  
375 # a zero-size chunk marks the end
376 0 ▶ unless($self->{chunk} = hex $1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"chunk"} = hex $1) 0 0 0
377 0 $self->{state} = 'trailer';
378  
379 # double line break indicates end of trailer; parse it
380 0 $self->{data} = "\x0d\x0a".$self->{data}; # count previous line break
381 return $self->_parse_header(length $1,1)
382 0 ▶ if $self->{data} =~ /^(.*?)\x0d?\x0a\x0d?\x0a/s;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"} =~ /^(.*?)\x0d?\x0a\x0d?\x0a/s 0 0 0
383 0 return -1; # still waiting for unknown amount of trailer data
384 }
385  
386 } else {
387 die "expected chunked encoding, got '".substr($self->{data},0,40)."...'"
388 0 ▶ if $self->{data} =~ /\x0d?\x0a/;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"data"} =~ /\x0d?\x0a/ 0 0 0
389 0 return -2; # waiting for a line with chunk information
390 }
391 }
392  
393 # do we have a current chunk size?
394 0 ▶ if($self->{chunk}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"chunk"}) 0 0 0
395  
396 # do we have enough data to fill it, plus a CR LF?
397 0 ▶ if(length $self->{data} > $self->{chunk} and
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->{"data"} > $self->{"chunk"} and substr($self->{"data"}, $self->{"chunk"}, 2) =~ /^(\x0d?\x0a)/) 0 0 0
Condition0%0/3
length $self->{"data"} > $self->{"chunk"} and substr($self->{"data"}, $self->{"chunk"}, 2) =~ /^(\x0d?\x0a)/
!ll&&!rl&&r
000
MC/DC0%0/2
length $self->{"data"} > $self->{"chunk"} and substr($self->{"data"}, $self->{"chunk"}, 2) =~ /^(\x0d?\x0a)/
length $self->{"data"} > $self->{"chunk"} substr($self->{"data"}, $self->{"chunk"}, 2) =~ /^(\x0d?\x0a)/
Truth table0%0/3
A and B
A= length $self->{"data"} > $self->{"chunk"}B= substr($self->{"data"}, $self->{"chunk"}, 2) =~ /^(\x0d?\x0a)/
ABresult
0X0
100
111
398 substr($self->{data},$self->{chunk},2) =~ /^(\x0d?\x0a)/) {
399 0 my $crlf = $1;
400 0 $self->{obj}->add_content(substr($self->{data},0,$self->{chunk}));
401 0 substr($self->{data},0,length $crlf) = '';
402  
403 # remove data from the buffer that we've already parsed
404 0 $self->{data} = substr($self->{data},delete $self->{chunk});
405  
406 # got chunks?
407 0 goto CHUNK;
408 }
409  
410 0 return $self->{chunk}-length($self->{data})+2; # extra CR LF
411 }
412 }
413  
414  
415 =head1 AUTHOR
416  
417 David Robins E<lt>dbrobins@davidrobins.netE<gt>
418 Fixes for 0.05 by David Cannings E<lt>david@edeca.netE<gt>
419  
420 =head1 SEE ALSO
421  
422 L<HTTP::Request>, L<HTTP::Response>.
423  
424 =cut
425  
426  
427 1 1;
↑ Summary