blib/lib/POE/Filter/HTTPD/Chunked.pm

filter: Statementstmt 94.9% 282 / 297 Branchbran 78.4% 69 / 88 Conditioncond 66.6% 26 / 39 MC/DCmcdc 31.8% 7 / 22 Subroutinesub 95.6% 44 / 46 Podpod 100% 7 / 7 totaltotal 87.1% 435 / 499 CC 63 SCAR 19.4
CC
63
Cov
89%
_parse_body
15.1
_parse_header
14.6
get
11.0
CRAP 6.9
↑ Summary
1 # POE::Filter::HTTPD::Chunked Copyright 2010 Mark Morgan <makk384@gmail.com>.
2 # based on POE::Filter::HTTPD Copyright 1998 Artur Bergman <artur@vogon.se>.
3  
4 package POE::Filter::HTTPD::Chunked;
5  
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 our $VERSION = 0.90;
10  
11 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use HTTP::Status qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
13 status_message
14 RC_BAD_REQUEST
15 RC_OK
16 RC_LENGTH_REQUIRED
17 2 );
18  
19 2 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 2 ▶use base qw( POE::Filter );
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 # set the following to get info on what the filter is doing. This is
25 # *very* noisy.
26 2 ▶use constant DEBUG => $ENV{ POE_FILTER_HTTPD_DEBUG } || 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{'POE_FILTER_HTTPD_DEBUG'} || 0
l!l
02
MC/DC0%0/1
$ENV{'POE_FILTER_HTTPD_DEBUG'} || 0
$ENV{'POE_FILTER_HTTPD_DEBUG'}
Truth table50%1/2
A
A= $ENV{'POE_FILTER_HTTPD_DEBUG'}
Aresult
11
00
27  
28 # indices into our self
29 2 ▶use constant BUFFER => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 2 ▶use constant IS_CHUNKED => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 2 ▶use constant CHUNK_BUFFER => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 2 ▶use constant FINISH => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 2 ▶use constant REQUEST => 4;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 2 ▶use constant STATE => 5;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 2 ▶use constant EVENT_ON_PARTIAL_CHUNK => 6;
Subroutine BEGIN: called CC=1 SCAR=0.0
36  
37 # states for our mini-state machine
38 2 ▶use constant STATE_REQUEST_LINE => 'parsing request line';
Subroutine BEGIN: called CC=1 SCAR=0.0
39 2 ▶use constant STATE_PARSING_HEADER => 'parsing header';
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶use constant STATE_PARSING_BODY => 'parsing body';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶use constant STATE_PARSING_COMPLETE => 'parsing complete';
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶use constant STATE_PARSING_TRAILER => 'parsing trailer';
Subroutine BEGIN: called CC=1 SCAR=0.0
43  
44 # very lenient CRLF matcher, matches CRLF, LFCR, CR, LF
45 2 my $CRLF = qr/(?:\x0D\x0A?|\x0A\x0D?)/;
46  
47 # for matching an HTTP header. Currently ignores continuation header lines...
48 2 my $HEADER_REGEX = qr/^([^()<>@,;:\\"\/\[\]?={}\s]+):\s*(.*?)$CRLF/;
49  
50 # for matching chunked transfer-encoding definition, within header value
51 2 my $CHUNKED_REGEX = qr/(?:,\s*|^)chunked\s*$/;
52  
53 # mapping of supported request types, and whether they support body
54 2 ▶use constant DENY_CONTENT_BODY => 1; # can't have
Subroutine BEGIN: called CC=1 SCAR=0.0
55 2 ▶use constant ALLOW_CONTENT_BODY => 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use constant REQUIRE_CONTENT_BODY => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 2 ▶use constant REQUEST_TYPES => {
Subroutine BEGIN: called CC=1 SCAR=0.0
59 OPTIONS => ALLOW_CONTENT_BODY,
60 GET => DENY_CONTENT_BODY,
61 HEAD => DENY_CONTENT_BODY,
62 POST => ALLOW_CONTENT_BODY,
63 PUT => ALLOW_CONTENT_BODY,
64 DELETE => DENY_CONTENT_BODY,
65 TRACE => ALLOW_CONTENT_BODY,
66 2 };
67  
68 2 my $HTTP_0_9 = "HTTP/0.9"; # not real, just give us something to compare with
69 2 my $HTTP_1_0 = "HTTP/1.0";
70 2 my $HTTP_1_1 = "HTTP/1.1";
71  
72 #------------------------------------------------------------------------------
73  
74 49 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
75 49 my ( $class, %args ) = @_;
76  
77 49 my $self = bless [], $class;
78 49 $self->__reset_state( 1 );
79  
80 49 ▶ $self->[ EVENT_ON_PARTIAL_CHUNK ] = $args{ event_on_chunk } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'event_on_chunk'} ? : 1 48 49
81  
82 49 return $self;
83 }
84  
85 #------------------------------------------------------------------------------
86  
87 0 ▶sub get_one_start {
Subroutine get_one_start: not called CC=2 SCAR=17.9
Pod documented
88 0 my ($self, $stream) = @_;
89  
90 # TODO TESTS FOR THIS!
91  
92 0 ▶ $stream = [ $stream ] unless ( ref( $stream ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $stream 0 0 0
93 0 $self->[BUFFER] .= join( '', @$stream );
94 }
95  
96 0 ▶sub get_one {
Subroutine get_one: not called CC=2 SCAR=17.9
Pod documented
97 0 my ($self) = @_;
98 0 ▶ return ( $self->[FINISH] ) ? [] : $self->get( [] );
Branch: 1 branch
BranchTrueFalseTotal
$self->[3] ? : 0 0 0
99 }
100  
101 124 ▶sub get {
Subroutine get: called CC=11 SCAR=24.0
Pod documented
102 124 my ($self, $stream) = @_;
103  
104 # append current stream input into buffer
105 124 $self->[ BUFFER ] .= join '', @$stream;
106  
107 124 DEBUG && warn "buffer = " . $self->[ BUFFER ];
108  
109 124 my $initial_chunk_size = do {
110 2 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
111 124 length $self->[ CHUNK_BUFFER ];
112 };
113  
114 # a basic state machine, to parse the request. Flows are basically:
115 # - no request -> request -> parsed_header -> parsed_body -> done (send and reset)
116 # At any point, can error out of this, at which point we reset the state machine
117 124 while ( 1 ) {
118 263 my $to_return;
119  
120 263 ▶ if ( $self->[ STATE ] eq STATE_REQUEST_LINE ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($self->[5] eq 'parsing request line') { } 76 187 263
elsif ($self->[5] eq 'parsing header') { } 81 106 187
elsif ($self->[5] eq 'parsing body') { } 59 47 106
elsif ($self->[5] eq 'parsing trailer') { } 7 40 47
elsif ($self->[5] eq 'parsing complete') { } 40 0 40
121 76 $to_return = $self->_parse_request_line;
122 } elsif ( $self->[ STATE ] eq STATE_PARSING_HEADER ) {
123 81 $to_return = $self->_parse_header;
124 } elsif ( $self->[ STATE ] eq STATE_PARSING_BODY ) {
125 59 $to_return = $self->_parse_body;
126 } elsif ( $self->[ STATE ] eq STATE_PARSING_TRAILER ) {
127 7 $to_return = $self->_parse_trailer;
128 } elsif ( $self->[ STATE ] eq STATE_PARSING_COMPLETE ) {
129 # request is done, clean up what we have now, and return it.
130 40 $to_return = [ $self->[ REQUEST ] ];
131  
132 40 DEBUG && warn "completed request: " . $self->[ REQUEST ]->as_string;
133  
134 40 $self->__reset_state( 0 );
135  
136 40 $self->[ FINISH ] = 1;
137 } else {
138 0 die "Unexpected state '$self->[ STATE ]'!!!";
139 }
140  
141 263 ▶ if ( $to_return and not scalar @{ $to_return } ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($to_return and not scalar @{$to_return;}) 72 191 263
Condition100%3/3
$to_return and not scalar @{$to_return;}
!ll&&!rl&&r
1395272
MC/DC100%2/2
$to_return and not scalar @{$to_return;}
$to_return not scalar @{$to_return;}
Truth table100%3/3
A and not(B)
A= $to_returnB= not scalar @{$to_return;}
ABresult
0X0
100
111
142 72 ▶ if ( $self->[ EVENT_ON_PARTIAL_CHUNK ] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[6]) 2 70 72
143 # if we are still in body parsing state, and have more chunk data
144 # than when we first ran through this loop, then return a marker
145 # response, to indicate that we've received partial chunk data.
146 # This is intended to allow the wheel/component to reset any
147 # timeouts or the like.
148 2 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
149  
150 2 my $current_chunk_size = length $self->[ CHUNK_BUFFER ];
151  
152 2 ▶ if ( $current_chunk_size > $initial_chunk_size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($current_chunk_size > $initial_chunk_size) 2 0 2
153 2 my $chunked = HTTP::Request::Chunked->new;
154  
155 # set headers to indicate the offset and size of
156 # the chunk, and the content to the current chunk
157  
158 2 my $chunk_size = $current_chunk_size - $initial_chunk_size;
159  
160 2 $chunked->header( 'x-chunk-offset' => $initial_chunk_size );
161 2 $chunked->header( 'x-chunk-size' => $chunk_size );
162  
163 2 $chunked->content( substr( $self->[ CHUNK_BUFFER ], $initial_chunk_size, $chunk_size ) );
164  
165 2 DEBUG && warn "got partial chunk of size $chunk_size at offset $initial_chunk_size\n";
166  
167 2 push @{ $to_return }, $chunked;
168 }
169 }
170 }
171  
172 263 ▶ $to_return && return $to_return;
Branch: 1 branch
BranchTrueFalseTotal
$to_return and return $to_return 124 139 263
173 }
174 }
175  
176 #------------------------------------------------------------------------------
177  
178 2 ▶sub put {
Subroutine put: called CC=7 SCAR=19.9
Pod documented
179 2 my ($self, $responses) = @_;
180 2 my @raw;
181  
182 # HTTP::Response's as_string method returns the header lines
183 # terminated by "\n", which does not do the right thing if we want
184 # to send it to a client. Here I've stolen HTTP::Response's
185 # as_string's code and altered it to use network newlines so picky
186 # browsers like lynx get what they expect.
187  
188 2 foreach (@$responses) {
189 2 my $code = $_->code;
190 2 ▶ my $status_message = status_message($code) || "Unknown Error";
Condition50%1/2
&status_message($code) || 'Unknown Error'
l!l
20
MC/DC0%0/1
&status_message($code) || 'Unknown Error'
&status_message($code)
Truth table50%1/2
A
A= &status_message($code)
Aresult
11
00
191 2 ▶ my $message = $_->message || "";
Condition50%1/2
$_->message || ''
l!l
20
MC/DC0%0/1
$_->message || ''
$_->message
Truth table50%1/2
A
A= $_->message
Aresult
11
00
192 2 ▶ my $proto = $_->protocol || 'HTTP/1.0';
Condition50%1/2
$_->protocol || 'HTTP/1.0'
l!l
02
MC/DC0%0/1
$_->protocol || 'HTTP/1.0'
$_->protocol
Truth table50%1/2
A
A= $_->protocol
Aresult
11
00
193  
194 2 my $status_line = "$proto $code";
195 2 ▶ $status_line .= " ($status_message)" if $status_message ne $message;
Branch: 1 branch
BranchTrueFalseTotal
if $status_message ne $message 2 0 2
196 2 ▶ $status_line .= " $message" if length($message);
Branch: 1 branch
BranchTrueFalseTotal
if length $message 2 0 2
197  
198 # Use network newlines, and be sure not to mangle newlines in the
199 # response's content.
200  
201 2 my @headers;
202 2 push @headers, $status_line;
203 2 push @headers, $_->headers_as_string("\x0D\x0A");
204  
205 2 push @raw, join("\x0D\x0A", @headers, "") . $_->content;
206 }
207  
208 # Allow next request after we're done sending the response.
209 2 $self->[FINISH] = 0;
210  
211 2 \@raw;
212 }
213  
214 1 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
215 # implement our own clone, as we want to reset the state completely,
216 # and ensure that we honour the current 'event_on_chunk' option
217 1 my ( $self ) = @_;
218  
219 1 my $class = ref $self;
220  
221 1 return $class->new( event_on_chunk => $self->[ EVENT_ON_PARTIAL_CHUNK ] );
222 }
223 #------------------------------------------------------------------------------
224  
225 2 ▶sub get_pending {
Subroutine get_pending: called CC=1 SCAR=0.4
Pod documented
226 2 my $self = shift;
227 2 croak ref($self)." does not support the get_pending() method\n";
228 0 return;
229 }
230  
231 #------------------------------------------------------------------------------
232 # Functions specific to HTTPD;
233 #------------------------------------------------------------------------------
234  
235 # Build a basic response, given a status, a content type, and some
236 # content.
237  
238 12 ▶sub _build_basic_response {
Subroutine _build_basic_response: called CC=3 SCAR=11.0
239 12 my ($self, $content, $content_type, $status) = @_;
240  
241 # Need to check lengths in octets, not characters.
242 2 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
243  
244 12 ▶ $content_type ||= 'text/html';
Condition50%1/2
$content_type ||= "text/html"
l!l
120
MC/DC0%0/1
$content_type ||= "text/html"
$content_type
Truth table50%1/2
A
A= $content_type
Aresult
11
00
245 12 ▶ $status ||= RC_OK;
Condition50%1/2
$status ||= 200
l!l
120
MC/DC0%0/1
$status ||= 200
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
246  
247 12 my $response = HTTP::Response->new($status);
248  
249 2 ▶ my $length = do { use bytes; length $content };
Subroutine BEGIN: called CC=1 SCAR=0.0
250  
251 12 $response->push_header( 'Content-Type', $content_type );
252 12 $response->push_header( 'Content-Length', $length );
253 12 $response->content($content);
254  
255 12 return $response;
256 }
257  
258 526 ▶sub _default_values {
Subroutine _default_values: called CC=1 SCAR=0.0
259 return {
260 526 STATE() => STATE_REQUEST_LINE,
261 IS_CHUNKED() => 0,
262 BUFFER() => '',
263 FINISH() => 0,
264 REQUEST() => undef,
265 CHUNK_BUFFER() => '',
266 };
267 }
268  
269 101 ▶sub __reset_state {
Subroutine __reset_state: called CC=3 SCAR=11.0
270 101 my ( $self, $clean_buffer ) = @_;
271  
272 101 my @fields = ( STATE, FINISH, REQUEST, IS_CHUNKED );
273  
274 101 ▶ if ( $clean_buffer ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clean_buffer) 61 40 101
275 61 push( @fields, BUFFER, CHUNK_BUFFER );
276 }
277  
278 101 foreach my $field ( @fields ) {
279 526 $self->[ $field ] = $self->_default_values->{ $field };
280 }
281 }
282  
283  
284 12 ▶sub _build_error {
Subroutine _build_error: called CC=4 SCAR=14.5
285 12 my($self, $status, $details) = @_;
286  
287 # when we want to return an error, this object is pretty much dead.
288 # Clear out the state, including the buffers.
289  
290 12 $self->__reset_state( 1 );
291  
292 12 ▶ $status ||= RC_BAD_REQUEST;
Condition50%1/2
$status ||= 400
l!l
120
MC/DC0%0/1
$status ||= 400
$status
Truth table50%1/2
A
A= $status
Aresult
11
00
293 12 ▶ $details ||= '';
Condition50%1/2
$details ||= ""
l!l
120
MC/DC0%0/1
$details ||= ""
$details
Truth table50%1/2
A
A= $details
Aresult
11
00
294 12 ▶ my $message = status_message($status) || "Unknown Error";
Condition50%1/2
&status_message($status) || 'Unknown Error'
l!l
120
MC/DC0%0/1
&status_message($status) || 'Unknown Error'
&status_message($status)
Truth table50%1/2
A
A= &status_message($status)
Aresult
11
00
295  
296 12 return $self->_build_basic_response(
297 ( "<html>" .
298 "<head>" .
299 "<title>Error $status: $message</title>" .
300 "</head>" .
301 "<body>" .
302 "<h1>Error $status: $message</h1>" .
303 "<p>$details</p>" .
304 "</body>" .
305 "</html>"
306 ),
307 "text/html",
308 $status
309 );
310 }
311  
312 76 ▶sub _parse_request_line {
Subroutine _parse_request_line: called CC=5 SCAR=16.2
313 76 my ( $self ) = @_;
314  
315 76 DEBUG && warn "attempting to parse request line\n";
316  
317 # return no packets, if we haven't seen the end of line
318 76 ▶ if ( not $self->[ BUFFER ] =~ /$CRLF/s ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->[0] =~ /$CRLF/s) 23 53 76
319 23 return [];
320 }
321  
322 # get the request line, or return an error
323 53 ▶ if ( $self->[ BUFFER ] =~ s/^\s*(\w+)[ \t]+(\S+)(?:[ \t]+(HTTP\/\d+\.\d+))?$CRLF// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->[0] =~ s[^\s*(\w+)[ \t]+(\S+)(?:[ \t]+(HTTP/\d+\.\d+))?$CRLF][]) { } 47 6 53
elsif ($self->[0] =~ /^.*?$CRLF/s) { } 6 0 6
324 47 my ( $method, $uri, $version ) = ( $1, $2, $3 );
325  
326 47 ▶ $version ||= $HTTP_0_9;
Condition66%2/3
$version ||= $HTTP_0_9
l!l&&r!l&&!r
31160
MC/DC0%0/2
$version ||= $HTTP_0_9
$version $HTTP_0_9
Truth table66%2/3
A ||= B
A= $versionB= $HTTP_0_9
ABresult
1X1
011
000
327  
328 47 DEBUG && warn "got request of method = $method, uri = $uri, version = $version\n";
329  
330 47 my $request = HTTP::Request->new;
331  
332 47 $request->method( $method );
333 47 $request->uri( $uri );
334 47 $request->protocol( $version );
335  
336 47 $self->[ REQUEST ] = $request;
337 47 $self->[ STATE ] = STATE_PARSING_HEADER;
338 } elsif ( $self->[ BUFFER ] =~ /^.*?$CRLF/s ) {
339 6 DEBUG && warn "error in parsing request line\n";
340  
341 return [
342 6 $self->_build_error(RC_BAD_REQUEST, "Request line parse failure."),
343 ];
344 } else {
345 0 return [];
346 }
347  
348 47 return;
349 }
350  
351 81 ▶sub _parse_header {
Subroutine _parse_header: called CC=13 SCAR=26.8
352 81 my ( $self ) = @_;
353  
354 81 DEBUG && warn "attempting to parse headers\n";
355  
356 81 ▶ while ( $self->[ BUFFER ] =~ s/$HEADER_REGEX// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0] =~ s/$HEADER_REGEX//) 37 81 118
357 37 my ( $header, $value ) = ( $1, $2 );
358  
359 37 DEBUG && warn "got header line '$header' => '$value'\n";
360  
361 # pulled a header off the buffer
362 37 $self->[ REQUEST ]->header( $header => $value );
363 }
364  
365 # blank line to end the headers
366 81 ▶ if ( $self->[ BUFFER ] =~ s/^$CRLF// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0] =~ s/^$CRLF//) { } 47 34 81
367 47 DEBUG && warn "end of headers\n";
368  
369 # set IS_CHUNKED, if a transfer-encoding headers exists, and has
370 # 'chunked' as the last (or only) value. Adjust the header value
371 # in this case.
372 47 ▶ if ( my $te_header = $self->[ REQUEST ]->header( 'transfer-encoding' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $te_header = $self->[4]->header("transfer-encoding")) 10 37 47
373 10 ▶ if ( $te_header =~ s/$CHUNKED_REGEX// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($te_header =~ s/$CHUNKED_REGEX//) 10 0 10
374 10 DEBUG && warn "request is chunked\n";
375  
376 10 $self->[ IS_CHUNKED ] = 1;
377  
378 # set or clear the header, as appropriate
379 10 ▶ if ( $te_header =~ /\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($te_header =~ /\S/) { } 1 9 10
380 1 $self->[ REQUEST ]->header( 'transfer-encoding' => $te_header );
381 } else {
382 9 $self->[ REQUEST ]->remove_header( 'transfer-encoding' );
383 }
384 10 DEBUG && warn "new header = $te_header\n";
385 }
386 }
387
388 47 $self->[ STATE ] = STATE_PARSING_BODY;
389  
390 # have enough information now, to determine whether we have met the
391 # allowance/requirement for body, for the given request type
392  
393 47 my $request_type = $self->[ REQUEST ]->method;
394  
395 47 my $requirement = REQUEST_TYPES->{ uc $request_type };
396 47 ▶ if ( defined $requirement ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $requirement) { } 45 2 47
397 # rfc-defined method; check whether body allowed or required
398  
399 45 ▶ my $has_body = $self->[ REQUEST ]->header( 'content-length' ) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->[4]->header('content-length') ? : 9 36 45
400  
401 45 my $error;
402 45 ▶ if ( $requirement == DENY_CONTENT_BODY and $has_body ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($requirement == 1 and $has_body) { } 0 45 45
elsif ($requirement == 3 and not $has_body) { } 0 45 45
Condition66%2/3
$requirement == 1 and $has_body
!ll&&!rl&&r
22230
MC/DC0%0/2
$requirement == 1 and $has_body
$requirement == 1 $has_body
Truth table66%2/3
A and B
A= $requirement == 1B= $has_body
ABresult
0X0
100
111
403 0 DEBUG && warn "for request type $request_type, can't have a body\n";
404  
405 0 $error = $self->_build_error(
406 RC_BAD_REQUEST,
407 'request type does not allow a body'
408 );
409 ▶ } elsif ( $requirement == REQUIRE_CONTENT_BODY and not $has_body ) {
Condition33%1/3
$requirement == 3 and not $has_body
!ll&&!rl&&r
4500
MC/DC0%0/2
$requirement == 3 and not $has_body
$requirement == 3 not $has_body
Truth table33%1/3
A and not(B)
A= $requirement == 3B= not $has_body
ABresult
0X0
100
111
410 0 DEBUG && warn "for request type $request_type, must have a body\n";
411  
412 0 $error = $self->_build_error(
413 RC_LENGTH_REQUIRED,
414 'request type requires body'
415 );
416 } else {
417 # noop, ALLOW_CONTENT_BODY can have or not
418 }
419  
420 45 ▶ if ( $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 45 45
421 0 return [ $error ];
422 }
423 } else {
424 return [
425 2 $self->_build_error( RC_BAD_REQUEST, "invalid request type '$request_type'" )
426 ];
427 }
428 } else {
429 # still haven't got full data, return empty array ref to
430 # cause return
431 34 return []
432 }
433 }
434  
435 59 ▶sub _parse_body {
Subroutine _parse_body: called CC=15 SCAR=27.1
436 59 my ( $self ) = @_;
437  
438 59 my $is_chunked = $self->[ IS_CHUNKED ];
439  
440 59 my $content_length = $self->[ REQUEST ]->content_length;
441  
442 59 ▶ if ( defined $content_length and $content_length and $is_chunked ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $content_length and $content_length and $is_chunked) { } 1 58 59
elsif ($is_chunked and $self->[4]->protocol eq 'HTTP/1.0') { } 1 104 105
Condition100%6/6
defined $content_length and $content_length and $is_chunked
!ll&&!rl&&r
38201
defined $content_length and $content_length
!ll&&!rl&&r
36221
MC/DC100%3/3
defined $content_length and $content_length and $is_chunked
defined $content_length $content_length $is_chunked
Truth table100%4/4
A and B and C
A= defined $content_lengthB= $content_lengthC= $is_chunked
ABCresult
0XX0
10X0
1100
1111
443 # can't have both content_length and transfer_encoding of chunked set
444 return [
445 1 $self->_build_error(
446 RC_BAD_REQUEST,
447 'Both content-length and transfer-encoding cannot be set in headers'
448 )
449 ];
450 ▶ } elsif ( $is_chunked and $self->[ REQUEST ]->protocol eq 'HTTP/1.0' ) {
Condition100%3/3
$is_chunked and $self->[4]->protocol eq "HTTP/1.0"
!ll&&!rl&&r
47101
MC/DC100%2/2
$is_chunked and $self->[4]->protocol eq "HTTP/1.0"
$is_chunked $self->[4]->protocol eq "HTTP/1.0"
Truth table100%3/3
A and B
A= $is_chunkedB= $self->[4]->protocol eq "HTTP/1.0"
ABresult
0X0
100
111
451 # chunked encoding isn't valid for HTTP/1.0
452 return [
453 1 $self->_build_error(
454 RC_BAD_REQUEST,
455 "Can't use chunked encoding with HTTP/1.0"
456 )
457 ];
458 }
459  
460 57 DEBUG && warn "attempting to parse body, chunked = $is_chunked\n";
461  
462 57 ▶ if ( $content_length ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($content_length) { } 20 37 57
elsif ($is_chunked) { } 10 27 37
463 20 DEBUG && warn "looking for $content_length bytes\n";
464  
465 # if we have enough bytes in the buffer, then pull them off and complete the request.
466 2 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
467  
468 20 ▶ if ( length $self->[ BUFFER ] >= $content_length ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $self->[0] >= $content_length) { } 7 13 20
469 7 my $content = substr( $self->[ BUFFER ], 0, $content_length, '' );
470  
471 7 DEBUG && warn "got content of '$content'\n";
472  
473 7 $self->[ REQUEST ]->content( $content );
474  
475 7 $self->[ STATE ] = STATE_PARSING_COMPLETE;
476 } else {
477 13 return [];
478 }
479 } elsif ( $is_chunked ) {
480 10 DEBUG && warn "looking for chunk portion\n";
481  
482 # first line of each chunk will be the chunk size (in hex),
483 # followed by that number of bytes of chunk data. Keep
484 # dechunking, till we get a chunk size of 0.
485 2 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
486  
487 10 my $processed_data = 0;
488 10 ▶ while ( $self->[ BUFFER ] =~ /^((.*?)$CRLF)/s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0] =~ /^((.*?)$CRLF)/s) 66 0 66
489 66 DEBUG && warn "found a complete line\n";
490  
491 66 $processed_data = 1;
492  
493 66 my ( $line, $chunk_hex ) = ( $1, $2 );
494  
495 66 ▶ if ( $chunk_hex =~ /^([0-9a-f]+)(;.*)?$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk_hex =~ /^([0-9a-f]+)(;.*)?$/i) { } 65 1 66
496 65 $chunk_hex = $1; # ignore semicolon, and everything after
497 } else {
498 # got an invalid chunk size, return an error
499 return [
500 1 $self->_build_error(
501 RC_BAD_REQUEST,
502 "invalid chunk size '$chunk_hex'"
503 )
504 ];
505 }
506  
507 65 my ( $line_size, $chunk_size ) = ( length( $line ), hex( $chunk_hex ) );
508  
509 65 ▶ if ( $chunk_size == 0 ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($chunk_size == 0) { } 7 58 65
elsif ($self->[0] =~ s/^(?:.{$line_size})(.{$chunk_size})$CRLF//s) { } 56 2 58
510 7 DEBUG && warn "got a chunk size of 0, done with chunking\n";
511  
512 7 ▶ if ( $self->[ BUFFER ] =~ s/(?:.{$line_size})//s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0] =~ s/(?:.{$line_size})//s) 7 0 7
513 # signify that trailers still need handling
514 7 $self->[ STATE ] = STATE_PARSING_TRAILER;
515 7 return;
516 }
517 } elsif ( $self->[ BUFFER ] =~ s/^(?:.{$line_size})(.{$chunk_size})$CRLF//s ) {
518 56 my ( $chunk ) = ( $1 );
519 56 DEBUG && warn "got a complete chunk of length $chunk_size\n";
520  
521 56 $self->[ CHUNK_BUFFER ] .= $chunk;
522 } else {
523 # not enough data for a whole chunk, end current run through
524 2 return [];
525 }
526 }
527  
528 0 ▶ $processed_data || return [];
Branch: 1 branch
BranchTrueFalseTotal
$processed_data or return [] 0 0 0
529 } else { # no content_length
530 27 DEBUG && warn "finished body parsing\n";
531  
532 27 $self->[ STATE ] = STATE_PARSING_COMPLETE;
533 }
534  
535 34 return;
536 }
537  
538 7 ▶sub _parse_trailer {
Subroutine _parse_trailer: called CC=6 SCAR=18.0
539 7 my ( $self ) = @_;
540  
541 7 DEBUG && warn "looking for trailer\n";
542  
543 7 my $done = 0;
544  
545 # read up to a blank line, and pull off headers^Her, trailers.
546 7 ▶ if ( $self->[ BUFFER ] =~ s/^$CRLF// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->[0] =~ s/^$CRLF//) { } 5 2 7
elsif ($self->[0] =~ s/^(.*?($CRLF)\2)//s) { } 2 0 2
547 # no trailers, just mark that we're done
548 5 $done = 1;
549 } elsif ( $self->[ BUFFER ] =~ s/^(.*?($CRLF)\2)//s ) {
550 2 my $trailer = $1;
551  
552 2 ▶ while ( $trailer =~ s/$HEADER_REGEX//s ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($trailer =~ s/$HEADER_REGEX//s) 3 1 4
553 3 my ( $header, $value ) = ( $1, $2 );
554  
555 3 DEBUG && warn "got trailer line '$header' => '$value'\n";
556  
557 # can't have trailers of 'content-length', 'transfer-encoding'
558 # or 'trailers'
559 3 ▶ if ( $header =~ /^(content-length|transfer-encoding|trailer)$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($header =~ /^(content-length|transfer-encoding|trailer)$/i) 1 2 3
560 return [
561 1 $self->_build_error(
562 RC_BAD_REQUEST,
563 "Trailer of '$header' not allowed"
564 )
565 ]
566 }
567  
568 2 $self->[ REQUEST ]->header( $header => $value );
569 }
570 1 $done = 1;
571 } else {
572 0 return [];
573 }
574  
575 6 ▶ if ( $done ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($done) 6 0 6
576 # rewrite the headers and advance our state
577 6 $self->[ STATE ] = STATE_PARSING_COMPLETE;
578  
579 # rewrite the headers, as appropriate
580 6 my $request = $self->[ REQUEST ];
581  
582 6 $request->content( $self->[ CHUNK_BUFFER ] );
583 6 $request->content_length( length $self->[ CHUNK_BUFFER ] );
584 6 $request->remove_header( 'trailer' );
585 6 $self->[ CHUNK_BUFFER ] = '';
586 }
587  
588 6 return;
589 }
590  
591 2 1;
592  
593 package HTTP::Request::Chunked;
594  
595 2 ▶use base qw( HTTP::Request );
Subroutine BEGIN: called CC=1 SCAR=0.0
596  
597 2 1;
598  
599 __END__
↑ Summary