Promise/Parser.pm

filter: Statementstmt 70.8% 374 / 528 Branchbran 45.2% 134 / 296 Conditioncond 36.4% 79 / 217 MC/DCmcdc 15.0% 18 / 120 Subroutinesub 71.7% 38 / 53 Podpod 93.9% 31 / 33 totaltotal 54.0% 674 / 1247 CC 245 SCAR 53.2
CC
245
Cov
56%
parse_singleton
863.4
parse_headers_xs
114.7
looks_like_what
110.0
CRAP 203.4
« Promise/Message.pm ↑ Summary Promise/Response.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Parser.pm
3 ## Version v0.2.3
4 ## Copyright(c) 2025 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/03/25
7 ## Modified 2025/10/19
8 ## All rights reserved.
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTTP::Promise::Parser;
15 BEGIN
16 9 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
17 9 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 9 warnings::register_categories( 'HTTP::Promise' );
20 9 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶ use vars qw( @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION $DEBUG $EXCEPTION_CLASS
Subroutine BEGIN: called CC=1 SCAR=0.0
22 $CRLF $LWS $TEXT $TOKEN $HEADER $HTTP_VERSION $REQUEST $RESPONSE
23 $MAX_HEADERS_SIZE $MAX_READ_BUFFER $MAX_BODY_IN_MEMORY_SIZE
24 9 $DEFAULT_MIME_TYPE );
25 9 ▶ use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 # use HTTP::Parser::XS 0.17 ();
27 9 ▶ use HTTP::Parser2::XS 0.01 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28 9 ▶ use HTTP::Promise::Entity;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 9 ▶ use HTTP::Promise::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 9 ▶ use HTTP::Promise::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 9 ▶ use Module::Generic::File qw( sys_tmpdir );
Subroutine BEGIN: called CC=1 SCAR=0.0
32 # use Nice::Try;
33 9 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 9 ▶ use URI::Encode::XS;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 9 ▶ use Wanted;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 9 ▶ use constant (
Subroutine BEGIN: called CC=1 SCAR=0.0
37 9 TYPE_URL_ENCODED => 'application/x-www-form-urlencoded',
38 9 );
39 9 our @EXPORT = ();
40 9 our @EXPORT_OK = qw( parse_headers parse_request parse_request_line parse_response
41 parse_response_line parse_version );
42 9 our %EXPORT_TAGS = (
43 'all' => [@EXPORT_OK],
44 'request' => [qw( parse_headers parse_request parse_request_line parse_version )],
45 'response' => [qw( parse_headers parse_response parse_response_line parse_version )],
46 );
47 # rfc2616, section 2.2 on basic rules
48 # <https://tools.ietf.org/html/rfc2616#section-2.2>
49 9 our $CRLF = qr/\x0D?\x0A/;
50 9 our $LWS = qr/$CRLF[\x09\x20]|[\x09\x20]/;
51 9 our $TEXT = qr/[\x20-\xFF]/;
52 # !, #, $, %, &, ', *, +, -, ., 0..9, A..Z, ^, _, `, a..z, |, ~
53 9 our $TOKEN = qr/[\x21\x23-\x27\x2A\x2B\x2D\x2E\x30-\x39\x41-\x5A\x5E-\x7A\x7C\x7E]/;
54 # rfc2616, section 4.2 on message headers
55 # <https://tools.ietf.org/html/rfc2616#section-4.2>
56 # rfc7230, section 3.2
57 # <https://tools.ietf.org/html/rfc7230#section-3.2>
58 9 our $HEADER = qr/($TOKEN+)$LWS*:$LWS*((?:$TEXT|$LWS)*)$CRLF/;
59 # HTTP/1.0, HTTP/1.1, HTTP/2
60 9 our $HTTP_VERSION = qr/(?<http_protocol>HTTP\/(?<http_version>(?<http_vers_major>[0-9])(?:\.(?<http_vers_minor>[0-9]))?))/;
61 # rfc7230 superseding rfc2616 on request line
62 # <https://tools.ietf.org/html/rfc7230#page-21>
63 9 our $REQUEST = qr/(?<method>$TOKEN+)[\x09\x20]+(?<uri>[\x21-\xFF]+)[\x09\x20]+(?<protocol>$HTTP_VERSION)$CRLF/;
64 9 our $REQUEST_RFC2616 = qr/(?:$CRLF)*($TOKEN+)[\x09\x20]+([\x21-\xFF]+)(?:[\x09\x20]+($HTTP_VERSION))?$CRLF/;
65 9 our $RESPONSE = qr/(?<protocol>$HTTP_VERSION)[\x09\x20]+(?<code>[0-9]{3})[\x09\x20]+(?<status>$TEXT*)$CRLF/;
66 # 8Kb
67 # Beyond this, we return a 413 Entity Too Large
68 # Ref: <https://stackoverflow.com/questions/686217/maximum-on-http-header-values>
69 9 our $MAX_HEADERS_SIZE = 8192;
70 9 our $MAX_READ_BUFFER = 2048;
71 # 100Kb
72 9 our $MAX_BODY_IN_MEMORY_SIZE = 102400;
73 9 our $DEFAULT_MIME_TYPE = 'application/octet-stream';
74 9 our $DEBUG = 0;
75 9 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
76 9 our $VERSION = 'v0.2.3';
77 };
78  
79 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
80 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
81  
82 sub init
83 22 ▶{
Subroutine init: called CC=6 SCAR=18.4
Pod documented
84 22 my $self = shift( @_ );
85 22 $self->{decode_body} = 1;
86 22 $self->{decode_headers} = 0;
87 22 $self->{ignore_filename}= 0;
88 22 $self->{max_body_in_memory_size} = $MAX_BODY_IN_MEMORY_SIZE;
89 22 $self->{max_headers_size} = $MAX_HEADERS_SIZE;
90 22 $self->{max_read_buffer} = $MAX_READ_BUFFER;
91 22 $self->{output_dir} = sys_tmpdir();
92 22 $self->{tmp_dir} = undef;
93 22 $self->{tmp_to_core} = 0;
94 22 $self->{_init_strict_use_sub} = 1;
95 22 $self->{_exception_class} = $EXCEPTION_CLASS;
96 22 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 22 0 22
97 22 my $dir = $self->output_dir;
98 22 ▶ return( $self->error( "No output directory set." ) ) if( !defined( $dir ) || !length( $dir ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $dir and length $dir 0 22 22
Condition33%1/3
defined $dir and length $dir
!ll&&!rl&&r
0022
MC/DC0%0/2
defined $dir and length $dir
defined $dir length $dir
Truth table33%1/3
A and B
A= defined $dirB= length $dir
ABresult
0X0
100
111
99 22 ▶ return( $self->error( "Output directory set \"$dir\" does not exist." ) ) if( !$dir->exists );
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->exists 0 22 22
100 22 ▶ return( $self->error( "Output directory set \"$dir\" is not actually a directory." ) ) if( !$dir->is_dir );
Branch: 1 branch
BranchTrueFalseTotal
unless $dir->is_dir 0 22 22
101 22 return( $self );
102 }
103  
104 4 ▶sub decode_body { return( shift->_set_get_boolean( 'decode_body', @_ ) ); }
Subroutine decode_body: called CC=1 SCAR=0.0
Pod documented
105  
106 0 ▶sub decode_headers { return( shift->_set_get_boolean( 'decode_headers', @_ ) ); }
Subroutine decode_headers: not called CC=1 SCAR=6.9
Pod documented
107  
108 0 ▶sub ignore_filename { return( shift->_set_get_boolean( 'ignore_filename', @_ ) ); }
Subroutine ignore_filename: not called CC=1 SCAR=6.9
Pod documented
109  
110 sub looks_like_request
111 0 ▶{
Subroutine looks_like_request: not called CC=5 SCAR=34.0
Pod documented
112 0 my $self = shift( @_ );
113 0 my $this = shift( @_ );
114 0 ▶ return( $self->error( "Invalid argument. You can only provide either a string or a scalar reference." ) ) if( ref( $this ) && !$self->_is_scalar( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $this and not $self->_is_scalar($this) 0 0 0
Condition0%0/3
ref $this and not $self->_is_scalar($this)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $this and not $self->_is_scalar($this)
ref $this not $self->_is_scalar($this)
Truth table0%0/3
A and not(B)
A= ref $thisB= not $self->_is_scalar($this)
ABresult
0X0
100
111
115 0 ▶ my $ref = $self->_is_scalar( $this ) ? $this : \$this;
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_scalar($this) ? : 0 0 0
116 0 ▶ if( $$ref =~ /^$REQUEST/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref =~ /^$HTTP::Promise::Parser::REQUEST/) { } 0 0 0
117 {
118 0 my $def = {%+};
119 0 return( $def );
120 }
121 else
122 {
123 # undef is for errors
124 0 return( '' );
125 }
126 }
127  
128 sub looks_like_response
129 0 ▶{
Subroutine looks_like_response: not called CC=5 SCAR=34.0
Pod documented
130 0 my $self = shift( @_ );
131 0 my $this = shift( @_ );
132 0 ▶ return( $self->error( "Invalid argument. You can only provide either a string or a scalar reference." ) ) if( ref( $this ) && !$self->_is_scalar( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $this and not $self->_is_scalar($this) 0 0 0
Condition0%0/3
ref $this and not $self->_is_scalar($this)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $this and not $self->_is_scalar($this)
ref $this not $self->_is_scalar($this)
Truth table0%0/3
A and not(B)
A= ref $thisB= not $self->_is_scalar($this)
ABresult
0X0
100
111
133 0 ▶ my $ref = $self->_is_scalar( $this ) ? $this : \$this;
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_scalar($this) ? : 0 0 0
134 0 ▶ if( $$ref =~ /^$RESPONSE/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($$ref =~ /^$HTTP::Promise::Parser::RESPONSE/) { } 0 0 0
135 {
136 0 my $def = {%+};
137 0 return( $def );
138 }
139 else
140 {
141 # undef is for errors
142 0 return( '' );
143 }
144 }
145  
146 sub looks_like_what
147 0 ▶{
Subroutine looks_like_what: not called CC=10 SCAR=47.0
Pod documented
148 0 my $self = shift( @_ );
149 0 my $this = shift( @_ );
150 # Shortcut
151 0 ▶ return( '' ) if( !defined( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this 0 0 0
152 0 ▶ return( $self->error( "Invalid argument. You can only provide either a string or a scalar reference." ) ) if( ref( $this ) && !$self->_is_scalar( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $this and not $self->_is_scalar($this) 0 0 0
Condition0%0/3
ref $this and not $self->_is_scalar($this)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $this and not $self->_is_scalar($this)
ref $this not $self->_is_scalar($this)
Truth table0%0/3
A and not(B)
A= ref $thisB= not $self->_is_scalar($this)
ABresult
0X0
100
111
153 0 ▶ my $ref = $self->_is_scalar( $this ) ? $this : \$this;
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_scalar($this) ? : 0 0 0
154 # No need to go further
155 0 ▶ return( '' ) if( !defined( $$ref ) || !length( $$ref ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$ref and length $$ref 0 0 0
Condition0%0/3
defined $$ref and length $$ref
!ll&&!rl&&r
000
MC/DC0%0/2
defined $$ref and length $$ref
defined $$ref length $$ref
Truth table0%0/3
A and B
A= defined $$refB= length $$ref
ABresult
0X0
100
111
156 0 my( $type, $def );
157 0 ▶ if( $$ref =~ /^$REQUEST/ )
Branch: 2 branches
BranchTrueFalseTotal
if ($$ref =~ /^$HTTP::Promise::Parser::REQUEST/) { } 0 0 0
elsif ($$ref =~ /^$HTTP::Promise::Parser::RESPONSE/) { } 0 0 0
158 {
159 0 $def = {%+};
160 0 $type = 'request';
161 }
162 elsif( $$ref =~ /^$RESPONSE/ )
163 {
164 0 $def = {%+};
165 0 $type = 'response';
166 }
167 # undef is for errors
168 0 ▶ return( '' ) if( !defined( $def ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def 0 0 0
169 0 $def->{type} = $type;
170 0 return( $def );
171 }
172  
173 21 ▶sub max_body_in_memory_size { return( shift->_set_get_number( 'max_body_in_memory_size', @_ ) ); }
Subroutine max_body_in_memory_size: called CC=1 SCAR=0.0
Pod documented
174  
175 15 ▶sub max_headers_size { return( shift->_set_get_number( 'max_headers_size', @_ ) ); }
Subroutine max_headers_size: called CC=1 SCAR=0.0
Pod documented
176  
177 8 ▶sub max_read_buffer { return( shift->_set_get_number( 'max_read_buffer', @_ ) ); }
Subroutine max_read_buffer: called CC=1 SCAR=0.0
Pod documented
178  
179 sub new_tmpfile
180 0 ▶{
Subroutine new_tmpfile: not called CC=6 SCAR=37.4
Pod documented
181 0 my $self = shift( @_ );
182 0 my $io;
183 0 ▶ if( $self->tmp_to_core )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->tmp_to_core) { } 0 0 0
184 {
185 0 my $var = $self->new_scalar;
186 0 ▶ $io = $var->open( '+>' ) || return( $self->pass_error( $var->error ) );
Condition0%0/2
$var->open('+>') || (return $self->pass_error($var->error))
l!l
00
MC/DC0%0/1
$var->open('+>') || (return $self->pass_error($var->error))
$var->open('+>')
Truth table0%0/2
A
A= $var->open('+>')
Aresult
11
00
187 }
188 else
189 {
190 0 my $tmpdir = $self->tmp_dir;
191 0 ▶ $io = $self->new_tempfile( $tmpdir ? ( dir => $tmpdir ) : () ) ||
Branch: 1 branch
BranchTrueFalseTotal
$tmpdir ? : 0 0 0
Condition0%0/2
$self->new_tempfile($tmpdir ? ('dir', $tmpdir) : ()) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_tempfile($tmpdir ? ('dir', $tmpdir) : ()) || (return $self->pass_error)
$self->new_tempfile($tmpdir ? ('dir', $tmpdir) : ())
Truth table0%0/2
A
A= $self->new_tempfile($tmpdir ? ('dir', $tmpdir) : ())
Aresult
11
00
192 return( $self->pass_error );
193 0 ▶ $io->open( '>', { binmode => 'raw', autoflush => 1 } ) || return( $self->pass_error( $io->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$io->open(">", {"binmode", "raw", "autoflush", 1}) or return $self->pass_error($io->error) 0 0 0
194 }
195 0 return( $io );
196 }
197  
198 24 ▶sub output_dir { return( shift->_set_get_file( 'output_dir', @_ ) ); }
Subroutine output_dir: called CC=1 SCAR=0.0
Pod documented
199  
200 sub parse
201 9 ▶{
Subroutine parse: called CC=8 SCAR=21.2
Pod documented
202 9 my $self = shift( @_ );
203 9 ▶ my $this = shift( @_ ) || return( $self->error( "Nothing to parse was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('Nothing to parse was provided.'))
l!l
90
MC/DC0%0/1
shift @_ || (return $self->error('Nothing to parse was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
204 # Can be glob, scalar reference, file path
205 9 my $io;
206 9 ▶ if( $self->_is_glob( $this ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_glob($this)) { } 2 7 9
elsif ($self->_is_scalar($this)) { } 5 2 7
207 {
208 2 $io = $this;
209 }
210 elsif( $self->_is_scalar( $this ) )
211 {
212 5 ▶ $self->_load_class( 'Module::Generic::Scalar::IO' ) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Module::Generic::Scalar::IO") or return $self->pass_error 5 0 5
213 return( $self->pass_error );
214 5 ▶ $io = Module::Generic::Scalar::IO->new( $this, '<' ) ||
Condition50%1/2
'Module::Generic::Scalar::IO'->new($this, '<') || (return $self->pass_error('Module::Generic::Scalar::IO'->error))
l!l
50
MC/DC0%0/1
'Module::Generic::Scalar::IO'->new($this, '<') || (return $self->pass_error('Module::Generic::Scalar::IO'->error))
'Module::Generic::Scalar::IO'->new($this, '<')
Truth table50%1/2
A
A= 'Module::Generic::Scalar::IO'->new($this, '<')
Aresult
11
00
215 return( $self->pass_error( Module::Generic::Scalar::IO->error ) );
216 }
217 else
218 {
219 2 ▶ my $f = $self->new_file( $this ) || return( $self->pass_error );
Condition50%1/2
$self->new_file($this) || (return $self->pass_error)
l!l
20
MC/DC0%0/1
$self->new_file($this) || (return $self->pass_error)
$self->new_file($this)
Truth table50%1/2
A
A= $self->new_file($this)
Aresult
11
00
220 2 ▶ $io = $f->open( '<', { binmode => 'raw' } ) || return( $self->pass_error( $f->error ) );
Condition100%2/2
$f->open('<', {'binmode', 'raw'}) || (return $self->pass_error($f->error))
l!l
11
MC/DC100%1/1
$f->open('<', {'binmode', 'raw'}) || (return $self->pass_error($f->error))
$f->open('<', {'binmode', 'raw'})
Truth table100%2/2
A
A= $f->open('<', {'binmode', 'raw'})
Aresult
11
00
221 }
222 8 return( $self->parse_fh( $io, @_ ) );
223 }
224  
225 sub parse_data
226 1 ▶{
Subroutine parse_data: called CC=3 SCAR=12.5
Pod documented
227 1 my $self = shift( @_ );
228 1 ▶ return( $self->error( "Invalid argument. parse_data() only accepts either a string or something that stringifies." ) ) if( ref( $_[0] ) && !overload::Method( $_[0] => '""' ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] and not overload::Method($_[0], "\"\"") 0 1 1
Condition33%1/3
ref $_[0] and not overload::Method($_[0], "\"\"")
!ll&&!rl&&r
100
MC/DC0%0/2
ref $_[0] and not overload::Method($_[0], "\"\"")
ref $_[0] not overload::Method($_[0], "\"\"")
Truth table33%1/3
A and not(B)
A= ref $_[0]B= not overload::Method($_[0], "\"\"")
ABresult
0X0
100
111
229 1 return( $self->parse( \"$_[0]" ) );
230 }
231  
232 sub parse_fh
233 8 ▶{
Subroutine parse_fh: called CC=40 SCAR=46.6
Pod documented
234 8 my $self = shift( @_ );
235 8 my $fh = shift( @_ );
236 8 ▶ return( $self->error( "No filehandle was provided to read from." ) ) if( !defined( $fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 8 8
237 8 ▶ return( $self->error( "Filehandle provided (", overload::StrVal( $fh ), ") is not a valid filehandle." ) ) if( !$self->_is_glob( $fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_glob($fh) 0 8 8
238 8 my $opts = $self->_get_args_as_hash( @_ );
239 8 my $reader;
240 8 ▶ if( $reader = $opts->{reader} )
Branch: 1 branch
BranchTrueFalseTotal
if ($reader = $opts->{'reader'}) { } 0 8 8
241 {
242 0 ▶ return( $self->error( "Reader object provided (", overload::StrVal( $reader ), ") is not a HTTP::Promise::IO." ) ) if( !$self->_is_a( $reader => 'HTTP::Promise::IO' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($reader, "HTTP::Promise::IO") 0 0 0
243 }
244 else
245 {
246 8 ▶ $reader = HTTP::Promise::IO->new( $fh, max_read_buffer => $self->max_read_buffer, debug => $self->debug ) ||
Condition50%1/2
'HTTP::Promise::IO'->new($fh, 'max_read_buffer', $self->max_read_buffer, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::IO'->error))
l!l
80
MC/DC0%0/1
'HTTP::Promise::IO'->new($fh, 'max_read_buffer', $self->max_read_buffer, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::IO'->error))
'HTTP::Promise::IO'->new($fh, 'max_read_buffer', $self->max_read_buffer, 'debug', $self->debug)
Truth table50%1/2
A
A= 'HTTP::Promise::IO'->new($fh, 'max_read_buffer', $self->max_read_buffer, 'debug', $self->debug)
Aresult
11
00
247 return( $self->pass_error( HTTP::Promise::IO->error ) );
248 }
249 # "It is RECOMMENDED that all HTTP senders and recipients support, at a minimum, request-line lengths of 8000 octets."
250 # Ref: <https://tools.ietf.org/html/rfc7230#section-3.1.1>
251 8 my $buff = $reader->getline( max_read_buffer => 8192 );
252 # "A server that receives a method longer than any that it implements SHOULD respond with a 501 (Not Implemented) status code."
253 # Ref: <https://tools.ietf.org/html/rfc7230#section-3.1.1>
254 8 ▶ return( $self->pass_error( $reader->error ) ) if( !defined( $buff ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buff 0 8 8
255 8 ▶ unless( $opts->{request} || $opts->{response} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"request"} or $opts->{"response"}) 4 4 8
Condition66%2/3
$opts->{"request"} or $opts->{"response"}
l!l&&r!l&&!r
404
MC/DC50%1/2
$opts->{"request"} or $opts->{"response"}
$opts->{"request"} $opts->{"response"}
Truth table66%2/3
A or B
A= $opts->{"request"}B= $opts->{"response"}
ABresult
1X1
011
000
256 {
257 # Ref: <https://tools.ietf.org/html/rfc7230#section-3.1.2>
258 4 ▶ if( $buff =~ m,^(${HTTP_VERSION})[[:blank:]]+, )
Branch: 2 branches
BranchTrueFalseTotal
if ($buff =~ /^($HTTP::Promise::Parser::HTTP_VERSION)[[:blank:]]+/) { } 0 4 4
elsif ($buff =~ /^(?:CONNECT|DELETE|GET|HEAD|OPTIONS|PATCH|POST|PUT|TRACE)[[:blank:]]+(\S+)[[:blank:]]+$HTTP::Promise::Parser::HTTP_VERSION/) { } 2 2 4
259 {
260 0 $opts->{response} = 1;
261 }
262 # rfc7230, section 3.1
263 # Ref: <https://tools.ietf.org/html/rfc7230#section-3.1>
264 elsif( $buff =~ m,^(?:CONNECT|DELETE|GET|HEAD|OPTIONS|PATCH|POST|PUT|TRACE)[[:blank:]]+(\S+)[[:blank:]]+${HTTP_VERSION}, )
265 {
266 2 $opts->{request} = 1;
267 }
268 # Actually we accept an http message that does not have a first line.
269 # We just won't know what it is for, but it is ok, since we return an entity object
270 # else
271 # {
272 # return( $self->error( "Unknown http message type and no 'request' or 'response' boolean value was provided," ) );
273 # }
274 }
275
276 # Maximum headers size is not oficial, but we definitely need to set some limit.
277 # <https://security.stackexchange.com/questions/110565/large-over-sizesd-http-header-lengths-and-security-implications>
278 8 my $max = $self->max_headers_size;
279 8 my( $n, $def, $headers );
280 8 $n = -1;
281 8 ▶ while( $n != 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($n != 0) 8 0 8
282 {
283 8 $n = $reader->read( $buff, 2048, length( $buff ) );
284 8 ▶ return( $self->pass_error( $reader->error ) ) if( !defined( $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 8 8
285 8 ▶ if( $n == 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($n == 0) 0 8 8
286 {
287 0 ▶ if( !length( $buff ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not length $buff) { } 0 0 0
288 {
289 0 return( $self->error({ code => 400, message => "No data could be retrieved from filehandle." }) );
290 }
291 else
292 {
293 0 return( $self->error({ code => 425, message => "No headers data could be retrieved in the first " . length( $buff ) . " bytes of data read." }) );
294 }
295 }
296
297 # If we know the type of http message we are dealing with, we use the fast XS method
298 8 ▶ if( $opts->{request} || $opts->{response} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'request'} or $opts->{'response'}) { } 6 2 8
Condition66%2/3
$opts->{"request"} or $opts->{"response"}
l!l&&r!l&&!r
602
MC/DC50%1/2
$opts->{"request"} or $opts->{"response"}
$opts->{"request"} $opts->{"response"}
Truth table66%2/3
A or B
A= $opts->{"request"}B= $opts->{"response"}
ABresult
1X1
011
000
299 {
300 6 $def = $self->parse_headers_xs( \$buff, $opts );
301 # $def = $self->parse_headers( \$buff, $opts );
302 }
303 # otherwise, we use a more lenient one that does not require a request or response line
304 else
305 {
306 2 $def = $self->parse_headers( \$buff, $opts );
307 }
308
309 8 ▶ if( !defined( $def ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $def) { } 0 8 8
310 {
311 # 400 Bad request
312 0 ▶ return( $self->error({ code => 400, message => "Unable to find the headers in the request provided, within the first ${max} bytes of data. Do you need to increase the value for max_headers_size() ?" }) ) if( $self->error->code == 400 && length( $buff ) > $max );
Branch: 1 branch
BranchTrueFalseTotal
if $self->error->code == 400 and length $buff > $max 0 0 0
Condition0%0/3
$self->error->code == 400 and length $buff > $max
!ll&&!rl&&r
000
MC/DC0%0/2
$self->error->code == 400 and length $buff > $max
$self->error->code == 400 length $buff > $max
Truth table0%0/3
A and B
A= $self->error->code == 400B= length $buff > $max
ABresult
0X0
100
111
313 # Is it an error 425 Too Early, it means we need more data.
314 0 ▶ next if( $self->error->code == 425 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->error->code == 425 0 0 0
315 # For other errors, we stop and pass the error received
316 0 return( $self->pass_error );
317 }
318 else
319 {
320 $headers = $def->{headers} ||
321 8 ▶ return( $self->error( "No headers object set by parse_headers_xs() !" ) );
Condition50%1/2
$def->{'headers'} || (return $self->error('No headers object set by parse_headers_xs() !'))
l!l
80
MC/DC0%0/1
$def->{'headers'} || (return $self->error('No headers object set by parse_headers_xs() !'))
$def->{'headers'}
Truth table50%1/2
A
A= $def->{'headers'}
Aresult
11
00
322 8 ▶ return( $self->error( "parse_headers_xs() did not return the headers length as an integer ($def->{length})" ) ) if( !$self->_is_integer( $def->{length} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_integer($def->{"length"}) 0 8 8
323 8 ▶ return( $self->error( "Headers length returned by parse_headers_xs() ($def->{length}) is higher than our buffer size (", length( $buff ), ") !" ) ) if( $def->{length} > length( $buff ) );
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"length"} > length $buff 0 8 8
324 8 substr( $buff, 0, $def->{length}, '' );
325 8 ▶ $reader->unread( $buff ) if( length( $buff ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $buff 8 0 8
326 8 last;
327 }
328 }
329 # We need to consume the blank line separating the headers and the body, so it does
330 # not become part of the body, and because it does not belong anywhere
331 8 my $trash = $reader->read_until_in_memory( qr/${CRLF}/, include => 1 );
332
333 8 my $ent = HTTP::Promise::Entity->new( headers => $headers, debug => $DEBUG );
334 8 ▶ if( ( $opts->{request} || $opts->{response} ) && $def->{protocol} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"request"} || $opts->{"response"} and $def->{"protocol"}) 6 2 8
Condition66%4/6
$opts->{"request"} || $opts->{"response"} and $def->{"protocol"}
!ll&&!rl&&r
206
$opts->{"request"} or $opts->{"response"}
l!l&&r!l&&!r
602
MC/DC33%1/3
$opts->{"request"} || $opts->{"response"} and $def->{"protocol"}
$opts->{"request"} $opts->{"response"} $def->{"protocol"}
Truth table40%2/5
A or B and C
A= $opts->{"request"}B= $opts->{"response"}C= $def->{"protocol"}
ABCresult
00X0
1X00
0100
1X11
0111
335 {
336 6 my $message;
337 6 ▶ if( $opts->{request} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'request'}) { } 6 0 6
338 {
339 6 ▶ $self->_load_class( 'HTTP::Promise::Request' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Request") or return $self->pass_error 6 0 6
340 $message = HTTP::Promise::Request->new( @$def{qw( method uri headers )}, {
341 protocol => $def->{protocol},
342 version => $def->{version},
343 6 ▶ debug => $DEBUG
Condition100%2/2
'HTTP::Promise::Request'->new(@$def{'method', 'uri', 'headers'}, {'protocol', $def->{'protocol'}, 'version', $def->{'version'}, 'debug', $HTTP::Promise::Parser::DEBUG}) || (return $self->pass_error('HTTP::Promise::Request'->error))
l!l
51
MC/DC100%1/1
'HTTP::Promise::Request'->new(@$def{'method', 'uri', 'headers'}, {'protocol', $def->{'protocol'}, 'version', $def->{'version'}, 'debug', $HTTP::Promise::Parser::DEBUG}) || (return $self->pass_error('HTTP::Promise::Request'->error))
'HTTP::Promise::Request'->new(@$def{'method', 'uri', 'headers'}, {'protocol', $def->{'protocol'}, 'version', $def->{'version'}, 'debug', $HTTP::Promise::Parser::DEBUG})
Truth table100%2/2
A
A= 'HTTP::Promise::Request'->new(@$def{'method', 'uri', 'headers'}, {'protocol', $def->{'protocol'}, 'version', $def->{'version'}, 'debug', $HTTP::Promise::Parser::DEBUG})
Aresult
11
00
344 } ) ||
345 return( $self->pass_error( HTTP::Promise::Request->error ) );
346 }
347 else
348 {
349 0 ▶ $self->_load_class( 'HTTP::Promise::Response' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Response") or return $self->pass_error 0 0 0
350 $message = HTTP::Promise::Response->new( @$def{qw( code status headers )}, {
351 protocol => $def->{protocol},
352 version => $def->{version},
353 0 ▶ debug => $DEBUG
Condition0%0/2
'HTTP::Promise::Response'->new(@$def{'code', 'status', 'headers'}, {'protocol', $def->{'protocol'}, 'version', $def->{'version'}, 'debug', $HTTP::Promise::Parser::DEBUG}) || (return $self->pass_error('HTTP::Promise::Response'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Response'->new(@$def{'code', 'status', 'headers'}, {'protocol', $def->{'protocol'}, 'version', $def->{'version'}, 'debug', $HTTP::Promise::Parser::DEBUG}) || (return $self->pass_error('HTTP::Promise::Response'->error))
'HTTP::Promise::Response'->new(@$def{'code', 'status', 'headers'}, {'protocol', $def->{'protocol'}, 'version', $def->{'version'}, 'debug', $HTTP::Promise::Parser::DEBUG})
Truth table0%0/2
A
A= 'HTTP::Promise::Response'->new(@$def{'code', 'status', 'headers'}, {'protocol', $def->{'protocol'}, 'version', $def->{'version'}, 'debug', $HTTP::Promise::Parser::DEBUG})
Aresult
11
00
354 } ) ||
355 return( $self->pass_error( HTTP::Promise::Response->error ) );
356 }
357 # Mutual assignment for convenience
358 5 $message->entity( $ent );
359 5 $ent->http_message( $message );
360 }
361 7 my $type = $ent->mime_type;
362 7 my $part_ent;
363 # Request body can be one of 3 types:
364 # application/x-www-form-urlencoded
365 # multipart/form-data
366 # text/plain or other mime types
367 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST>
368 7 ▶ if( $type =~ m,^multipart/,i )
Branch: 1 branch
BranchTrueFalseTotal
if ($type =~ m[^multipart/]i) { } 3 4 7
369 {
370 3 ▶ $part_ent = $self->parse_multi_part( entity => $ent, reader => $reader ) ||
Condition50%1/2
$self->parse_multi_part('entity', $ent, 'reader', $reader) || (return $self->pass_error)
l!l
30
MC/DC0%0/1
$self->parse_multi_part('entity', $ent, 'reader', $reader) || (return $self->pass_error)
$self->parse_multi_part('entity', $ent, 'reader', $reader)
Truth table50%1/2
A
A= $self->parse_multi_part('entity', $ent, 'reader', $reader)
Aresult
11
00
371 return( $self->pass_error );
372 # Post process to assign a name to each part, to make it easy to manipulate them
373 3 for( $ent->parts->list )
374 {
375 12 ▶ if( my $dispo = $_->headers->content_disposition )
Branch: 1 branch
BranchTrueFalseTotal
if (my $dispo = $_->headers->content_disposition) 4 8 12
376 {
377 4 my $cd = $_->headers->new_field( 'Content-Disposition' => $dispo );
378 4 ▶ return( $self->pass_error( $_->headers->error ) ) if( !defined( $cd ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cd 0 4 4
379 4 ▶ if( my $name = $cd->name )
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $cd->name) 4 0 4
380 {
381 4 $_->name( $name );
382 }
383 }
384 }
385 }
386 else
387 {
388 4 ▶ $part_ent = $self->parse_singleton( entity => $ent, reader => $reader ) ||
Condition50%1/2
$self->parse_singleton('entity', $ent, 'reader', $reader) || (return $self->pass_error)
l!l
40
MC/DC0%0/1
$self->parse_singleton('entity', $ent, 'reader', $reader) || (return $self->pass_error)
$self->parse_singleton('entity', $ent, 'reader', $reader)
Truth table50%1/2
A
A= $self->parse_singleton('entity', $ent, 'reader', $reader)
Aresult
11
00
389 return( $self->pass_error );
390 }
391 7 return( $ent );
392 }
393  
394 sub parse_headers($$)
395 50 ▶{
Subroutine parse_headers: called CC=19 SCAR=29.5
Pod documented
396 50 my $self = shift( @_ );
397 50 my $str = $self->_get_args( shift( @_ ) );
398 50 ▶ return( $self->pass_error ) if( !defined( $str ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 50 50
399 50 my $opts = $self->_get_args_as_hash( @_ );
400 50 ▶ $opts->{convert_dash} //= 0;
Condition50%1/2
$opts->{"convert_dash"} //= 0
l!l
050
MC/DC0%0/1
$opts->{"convert_dash"} //= 0
$opts->{"convert_dash"}
Truth table50%1/2
A
A= $opts->{"convert_dash"}
Aresult
11
00
401 50 ▶ $opts->{no_headers_ok} //= 0;
Condition100%2/2
$opts->{"no_headers_ok"} //= 0
l!l
1337
MC/DC100%1/1
$opts->{"no_headers_ok"} //= 0
$opts->{"no_headers_ok"}
Truth table100%2/2
A
A= $opts->{"no_headers_ok"}
Aresult
11
00
402 50 ▶ if( !CORE::length( $$str ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (length $$str) 0 50 50
403 {
404 return({
405 0 length => 0,
406 headers => HTTP::Promise::Headers->new,
407 });
408 }
409
410 # Basic error catching
411 50 ▶ if( $$str !~ /${CRLF}${CRLF}/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($$str =~ /$HTTP::Promise::Parser::CRLF$HTTP::Promise::Parser::CRLF/) 4 46 50
412 {
413 4 return( $self->error({ code => 425, message => 'Incomplete request, call again when there is more data.', class => $EXCEPTION_CLASS }) );
414 }
415
416 46 my $def = {};
417 46 pos( $$str ) = 0;
418 46 ▶ if( $$str =~ m,^(${HTTP_VERSION})[[:blank:]]+(?<code>\d{3})[[:blank:]]+(?<status>.*?)$CRLF,gc )
Branch: 4 branches
BranchTrueFalseTotal
if ($$str =~ /^($HTTP::Promise::Parser::HTTP_VERSION)[[:blank:]]+(?<code>\d{3})[[:blank:]]+(?<status>.*?)$HTTP::Promise::Parser::CRLF/cg) { } 9 37 46
elsif ($$str =~ /^(?<method>[A-Z]{3,12})[[:blank:]]+(?<uri>\S+)[[:blank:]]+($HTTP::Promise::Parser::HTTP_VERSION)$HTTP::Promise::Parser::CRLF/cg) { } 9 28 37
elsif ($$str =~ /^(GET)[[:blank:]]+(\S+)[[:blank:]]*$HTTP::Promise::Parser::CRLF/cg) { } 2 26 28
elsif ($$str =~ m[^(?:[A-Z]+/\d+\.\d+|[A-Z]+[[:blank:]\h]+(?!\:))]) { } 5 21 26
419 {
420 9 @$def{qw( protocol code status )} = @+{qw( http_protocol code status )};
421 9 $def->{version} = version->parse( $+{http_version} );
422 }
423 # rfc7230, section 3.1
424 # Ref: <https://tools.ietf.org/html/rfc7230#section-3.1>
425 # CONNECT, DELETE, GET, HEAD, OPTIONS, PATCH, POST, PUT, TRACE
426 # But there could be others including non-standard ones
427 elsif( $$str =~ m|^(?<method>[A-Z]{3,12})[[:blank:]]+(?<uri>\S+)[[:blank:]]+(${HTTP_VERSION})$CRLF|gc )
428 {
429 9 @$def{qw( method uri protocol )} = @+{qw( method uri http_protocol )};
430 9 $def->{version} = version->parse( $+{http_version} );
431 }
432 # HTTP/0.9 "Simple Request"
433 # See rfc1945, section 5
434 # <https://tools.ietf.org/html/rfc1945#page-23>
435 elsif( $$str =~ m|^(GET)[[:blank:]]+(\S+)[[:blank:]]*$CRLF|gc )
436 {
437 2 @$def{qw( method uri )} = ( $1, $2 );
438 2 $def->{protocol} = 'HTTP/0.9';
439 2 $def->{version} = version->parse( '0.9' );
440 # There should not be any header in a simple request
441 2 ▶ return( $self->error({ code => 400, message => 'Bad request', class => $EXCEPTION_CLASS }) ) if( $$str =~ /^$HEADER/ );
Branch: 1 branch
BranchTrueFalseTotal
if $$str =~ /^$HTTP::Promise::Parser::HEADER/ 0 2 2
442 2 $def->{length} = pos( $$str );
443 2 $def->{headers} = HTTP::Promise::Headers->new;
444 2 return( $def );
445 }
446 elsif( $$str =~ /^(?:[A-Z]+\/\d+\.\d+|[A-Z]+[[:blank:]\h]+(?!\:))/ )
447 {
448 5 ▶ my $type = $$str =~ m,^[A-Z]+\/\d+\.\d+, ? 'Response' : 'Request';
Branch: 1 branch
BranchTrueFalseTotal
$$str =~ m[^[A-Z]+\/\d+\.\d+] ? : 2 3 5
449 5 return( $self->error({ code => 400, message => "Bad ${type}-Line", class => $EXCEPTION_CLASS }) );
450 }
451  
452 39 ▶ if( pos( $$str ) == 0 && $$str !~ /^$HEADER/ )
Branch: 1 branch
BranchTrueFalseTotal
if (pos $$str == 0 and not $$str =~ /^$HTTP::Promise::Parser::HEADER/) 3 36 39
Condition100%3/3
pos $$str == 0 and not $$str =~ /^$HTTP::Promise::Parser::HEADER/
!ll&&!rl&&r
18183
MC/DC100%2/2
pos $$str == 0 and not $$str =~ /^$HTTP::Promise::Parser::HEADER/
pos $$str == 0 not $$str =~ /^$HTTP::Promise::Parser::HEADER/
Truth table100%3/3
A and not(B)
A= pos $$str == 0B= not $$str =~ /^$HTTP::Promise::Parser::HEADER/
ABresult
0X0
100
111
453 {
454 3 return( $self->error({ code => 400, message => 'Bad request', class => $EXCEPTION_CLASS }) );
455 }
456  
457 36 my $headers = $self->new_array;
458
459 36 my $len = 0;
460 # my $remain = CORE::length( $$str );
461 # while( $$str =~ s/^$HEADER// )
462 36 ▶ while( $$str =~ /\G$HEADER/gc )
Branch: 1 branch
BranchTrueFalseTotal
if ($$str =~ /\G$HTTP::Promise::Parser::HEADER/cg) 52 36 88
463 {
464 # $len += ( $remain - CORE::length( $$str ) );
465 # $remain = CORE::length( $$str );
466 52 my( $n, $v ) = ( lc( $1 ), $2 );
467 52 ▶ $n =~ tr/-/_/ if( $opts->{convert_dash} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"convert_dash"} 0 52 52
468 52 $headers->push( $n => $v );
469 }
470 36 $len = pos( $$str );
471
472 36 ▶ unless( $$str =~ /\G${CRLF}/gc )
Branch: 1 branch
BranchTrueFalseTotal
unless ($$str =~ /\G$HTTP::Promise::Parser::CRLF/cg) 2 34 36
473 {
474 2 ▶ my $type = $def->{code} ? 'response' : 'request';
Branch: 1 branch
BranchTrueFalseTotal
$def->{'code'} ? : 1 1 2
475 2 return( $self->error({ code => 400, message => "Bad ${type}", class => $EXCEPTION_CLASS }) );
476 }
477  
478 34 foreach( @$headers )
479 {
480 104 s/$LWS+/\x20/g;
481 104 s/^$LWS//;
482 104 s/$LWS$//;
483 }
484 # return( want( 'LIST' ) ? $headers->list : $headers );
485 34 $def->{length} = $len;
486 34 $def->{headers} = HTTP::Promise::Headers->new( @$headers );
487 34 return( $def );
488 }
489  
490 sub parse_headers_xs($$)
491 7 ▶{
Subroutine parse_headers_xs: called CC=28 SCAR=47.4
Pod documented
492 7 my $self = shift( @_ );
493 7 ▶ my $str = $self->_get_args( shift( @_ ) ) || return( $self->pass_error );
Condition50%1/2
$self->_get_args(shift @_) || (return $self->pass_error)
l!l
70
MC/DC0%0/1
$self->_get_args(shift @_) || (return $self->pass_error)
$self->_get_args(shift @_)
Truth table50%1/2
A
A= $self->_get_args(shift @_)
Aresult
11
00
494 7 my $opts = $self->_get_args_as_hash( @_ );
495 7 ▶ if( ( !exists( $opts->{request} ) && !exists( $opts->{response} ) ) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $opts->{"request"} || exists $opts->{"response"} and length $opts->{"request"} || length $opts->{"response"}) 0 7 7
Condition33%3/9
exists $opts->{"request"} || exists $opts->{"response"} and length $opts->{"request"} || length $opts->{"response"}
!ll&&!rl&&r
007
exists $opts->{"request"} or exists $opts->{"response"}
l!l&&r!l&&!r
700
length $opts->{"request"} or length $opts->{"response"}
l!l&&r!l&&!r
700
MC/DC0%0/4
exists $opts->{"request"} || exists $opts->{"response"} and length $opts->{"request"} || length $opts->{"response"}
exists $opts->{"request"} exists $opts->{"response"} length $opts->{"request"} length $opts->{"response"}
Truth table14%1/7
A or B and C or D
A= exists $opts->{"request"}B= exists $opts->{"response"}C= length $opts->{"request"}D= length $opts->{"response"}
ABCDresult
00XX0
1X000
01000
1X1X1
1X011
011X1
01011
496 ( !length( $opts->{request} ) && !length( $opts->{response} ) ) )
497 {
498 0 return( $self->error({ code => 500, message => "Missing 'request' or 'response' property to ste how to parse the http headers.", class => $EXCEPTION_CLASS }) );
499 }
500 7 ▶ my $max_headers_size = $self->max_headers_size // 0;
Condition50%1/2
$self->max_headers_size // 0
l!l
70
MC/DC0%0/1
$self->max_headers_size // 0
$self->max_headers_size
Truth table50%1/2
A
A= $self->max_headers_size
Aresult
11
00
501
502 7 my $r = {};
503 7 my $len;
504 my $bkp_version;
505 # try-catch
506 7 local $@;
507 eval
508 7 {
509 7 ▶ if( $opts->{request} )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'request'}) { } 7 0 7
elsif ($opts->{'response'}) { } 0 0 0
510 {
511 # We have to do this, because of a bug in TTP::Parser2::XS where HTTP/2 is not supported.
512 # We save the value and replace it with one supported and we put it back after in
513 # the data we return.
514 # <https://rt.cpan.org/Ticket/Display.html?id=142808>
515 7 ▶ if( index( $$str, 'HTTP/2' ) != -1 )
Branch: 1 branch
BranchTrueFalseTotal
if (index($$str, "HTTP/2") != -1) 0 7 7
516 {
517 0 ▶ if( $$str =~ s,^((?:\S+)[[:blank:]\h]+(?:\S+)[[:blank:]\h]+HTTP/)(2(?:\.\d)?),${1}1.1, )
Branch: 1 branch
BranchTrueFalseTotal
if ($$str =~ s[^((?:\S+)[[:blank:]\h]+(?:\S+)[[:blank:]\h]+HTTP/)(2(?:\.\d)?)][$11.1]) 0 0 0
518 {
519 0 $bkp_version = $2;
520 }
521 }
522 7 $len = HTTP::Parser2::XS::parse_http_request( $$str, $r );
523 }
524 elsif( $opts->{response} )
525 {
526 0 ▶ if( index( $$str, 'HTTP/2' ) != -1 )
Branch: 1 branch
BranchTrueFalseTotal
if (index($$str, "HTTP/2") != -1) 0 0 0
527 {
528 0 ▶ if( $$str =~ s,^(HTTP/)(2(?:\.\d)?),${1}1.1, )
Branch: 1 branch
BranchTrueFalseTotal
if ($$str =~ s[^(HTTP/)(2(?:\.\d)?)][$11.1]) 0 0 0
529 {
530 0 $bkp_version = $2;
531 }
532 }
533 0 $len = HTTP::Parser2::XS::parse_http_response( $$str, $r );
534 }
535 };
536 7 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 7 7
537 {
538 0 return( $self->error({ code => 400, message => $@, class => $EXCEPTION_CLASS }) );
539 }
540
541 7 ▶ if( $len == -1 )
Branch: 3 branches
BranchTrueFalseTotal
if ($len == -1) { } 0 7 7
elsif ($len == -2 and $max_headers_size > 0 and length $$str > $max_headers_size) { } 0 7 7
elsif ($len == -2) { } 0 7 7
542 {
543 0 return( $self->error({ code => 400, message => 'Bad request', class => $EXCEPTION_CLASS }) );
544 }
545 ▶ elsif( $len == -2 && $max_headers_size > 0 && length( $$str ) > $max_headers_size )
Condition33%2/6
$len == -2 and $max_headers_size > 0 and length $$str > $max_headers_size
!ll&&!rl&&r
700
$len == -2 and $max_headers_size > 0
!ll&&!rl&&r
700
MC/DC0%0/3
$len == -2 and $max_headers_size > 0 and length $$str > $max_headers_size
$len == -2 $max_headers_size > 0 length $$str > $max_headers_size
Truth table25%1/4
A and B and C
A= $len == -2B= $max_headers_size > 0C= length $$str > $max_headers_size
ABCresult
0XX0
10X0
1100
1111
546 {
547 # 431: HTTP request header fields too large
548 # 413: Request entity too large
549 0 return( $self->error({ code => 413, message => 'Incomplete and too long request', class => $EXCEPTION_CLASS }) );
550 }
551 # Which one is best?
552 # 406 Unacceptable
553 # 411 Length required
554 # 417 Expectation failed
555 # 422 Unprocessable entity
556 # 425 Too early
557 elsif( $len == -2 )
558 {
559 0 return( $self->error({ code => 425, message => 'Incomplete request, call again when there is more data.', class => $EXCEPTION_CLASS }) );
560 }
561 # response headers:
562 # {
563 # "_content_length" => 15,
564 # "_keepalive" => 0,
565 # "_message" => "OK",
566 # "_protocol" => "HTTP/1.0",
567 # "_status" => 200,
568 # "content-length" => [15],
569 # "content-type" => ["text/plain"],
570 # "host" => ["example.com"],
571 # "user-agent" => ["hoge"],
572 # }
573 # request headers:
574 # {
575 # "_content_length" => 27,
576 # "_keepalive" => 1,
577 # "_method" => "POST",
578 # "_protocol" => "HTTP/1.1",
579 # "_query_string" => "",
580 # "_request_uri" => "/test",
581 # "_uri" => "/test",
582 # "content-length" => [27],
583 # "content-type" => ["application/x-www-form-urlencoded"],
584 # "host" => ["foo.example"],
585 # }
586 7 ▶ $r->{_protocol} = "HTTP/${bkp_version}" if( defined( $bkp_version ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $bkp_version 0 7 7
587 # warn( "HTTP::Parser2::XS->parse_headers_xs: bytes read ($len) differs from _content_length (", ( $r->{_content_length} // '' ), ")\n" ) if( defined( $r->{_content_length} ) && length( $r->{_content_length} ) && $len != $r->{_content_length} && $self->_warnings_is_enabled );
588 7 my $def = { length => $len };
589 # Sadly enough, HTTP::Parser2::XS does not provide the order of the header and
590 # although we could find out ourself, it would defeat the purpose of using an XS module
591 # so we default to alphabetical order
592 # If this is really important, you can use parse_request method instead
593 7 my $headers = $self->new_array;
594 # Skip keys that start with _. They are private properties
595 7 for( sort( grep( !/^_/, keys( %$r ) ) ) )
596 {
597 32 my $k = $_;
598 32 ▶ $k =~ tr/-/_/ if( $opts->{convert_dash} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"convert_dash"} 0 32 32
599 32 $headers->push( $k => $r->{ $_ } );
600 }
601 7 $def->{headers} = HTTP::Promise::Headers->new( @$headers );
602 7 ▶ if( $opts->{request} )
Branch: 2 branches
BranchTrueFalseTotal
if ($opts->{'request'}) { } 7 0 7
elsif ($opts->{'response'}) { } 0 0 0
603 {
604 7 @$def{qw( method protocol )} = @$r{qw( _method _protocol )};
605 7 ▶ $def->{uri} = URI->new( $r->{_request_uri} ) if( exists( $r->{_request_uri} ) && length( $r->{_request_uri} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $r->{"_request_uri"} and length $r->{"_request_uri"} 7 0 7
Condition33%1/3
exists $r->{"_request_uri"} and length $r->{"_request_uri"}
!ll&&!rl&&r
007
MC/DC0%0/2
exists $r->{"_request_uri"} and length $r->{"_request_uri"}
exists $r->{"_request_uri"} length $r->{"_request_uri"}
Truth table33%1/3
A and B
A= exists $r->{"_request_uri"}B= length $r->{"_request_uri"}
ABresult
0X0
100
111
606 }
607 elsif( $opts->{response} )
608 {
609 0 @$def{qw( code status protocol )} = @$r{qw( _status _message _protocol )};
610 }
611 7 ▶ $def->{version} = $self->parse_version( $r->{_protocol} ) || return( $self->pass_error );
Condition50%1/2
$self->parse_version($r->{'_protocol'}) || (return $self->pass_error)
l!l
70
MC/DC0%0/1
$self->parse_version($r->{'_protocol'}) || (return $self->pass_error)
$self->parse_version($r->{'_protocol'})
Truth table50%1/2
A
A= $self->parse_version($r->{'_protocol'})
Aresult
11
00
612 # It seems
613 7 ▶ while( substr( $$str, 0, $len ) =~ /$CRLF($CRLF)$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$str, 0, $len) =~ /$HTTP::Promise::Parser::CRLF($HTTP::Promise::Parser::CRLF)$/) 7 7 14
614 {
615 7 $len -= length( $1 );
616 }
617 7 $def->{length} = $len;
618 7 return( $def );
619 }
620  
621 sub parse_multi_part
622 4 ▶{
Subroutine parse_multi_part: called CC=29 SCAR=35.8
Pod documented
623 4 my $self = shift( @_ );
624 4 my $opts = $self->_get_args_as_hash( @_ );
625 my $ent = $opts->{entity} ||
626 4 ▶ return( $self->error({ code => 500, message => "No entity object was provided." }) );
Condition50%1/2
$opts->{'entity'} || (return $self->error({'code', 500, 'message', 'No entity object was provided.'}))
l!l
40
MC/DC0%0/1
$opts->{'entity'} || (return $self->error({'code', 500, 'message', 'No entity object was provided.'}))
$opts->{'entity'}
Truth table50%1/2
A
A= $opts->{'entity'}
Aresult
11
00
627 my $reader = $opts->{reader} ||
628 4 ▶ return( $self->error({ code => 500, message => "No reader object was provided." }) );
Condition50%1/2
$opts->{'reader'} || (return $self->error({'code', 500, 'message', 'No reader object was provided.'}))
l!l
40
MC/DC0%0/1
$opts->{'reader'} || (return $self->error({'code', 500, 'message', 'No reader object was provided.'}))
$opts->{'reader'}
Truth table50%1/2
A
A= $opts->{'reader'}
Aresult
11
00
629 4 ▶ my $headers = $ent->headers ||
Condition50%1/2
$ent->headers || (return $self->error({'code', 500, 'message', 'No headers object found in entity object.'}))
l!l
40
MC/DC0%0/1
$ent->headers || (return $self->error({'code', 500, 'message', 'No headers object found in entity object.'}))
$ent->headers
Truth table50%1/2
A
A= $ent->headers
Aresult
11
00
630 return( $self->error({ code => 500, message => "No headers object found in entity object." }) );
631 4 my $ct = $headers->content_type;
632 4 ▶ my $h = $headers->new_field( 'Content-Type' => $ct ) || return( $self->pass_error( $headers->error ) );
Condition50%1/2
$headers->new_field('Content-Type', $ct) || (return $self->pass_error($headers->error))
l!l
40
MC/DC0%0/1
$headers->new_field('Content-Type', $ct) || (return $self->pass_error($headers->error))
$headers->new_field('Content-Type', $ct)
Truth table50%1/2
A
A= $headers->new_field('Content-Type', $ct)
Aresult
11
00
633 4 ▶ my $boundary = $h->boundary ||
Condition50%1/2
$h->boundary || (return $self->error("No boundary could be found in the Content-Type header '${ct}'"))
l!l
40
MC/DC0%0/1
$h->boundary || (return $self->error("No boundary could be found in the Content-Type header '${ct}'"))
$h->boundary
Truth table50%1/2
A
A= $h->boundary
Aresult
11
00
634 return( $self->error( "No boundary could be found in the Content-Type header '$ct'" ) );
635 4 my $max_in_memory = $self->max_body_in_memory_size;
636 4 ▶ my $default_mime = $DEFAULT_MIME_TYPE || 'application/octet-stream';
Condition50%1/2
$HTTP::Promise::Parser::DEFAULT_MIME_TYPE || 'application/octet-stream'
l!l
40
MC/DC0%0/1
$HTTP::Promise::Parser::DEFAULT_MIME_TYPE || 'application/octet-stream'
$HTTP::Promise::Parser::DEFAULT_MIME_TYPE
Truth table50%1/2
A
A= $HTTP::Promise::Parser::DEFAULT_MIME_TYPE
Aresult
11
00
637
638 # Position ourself right after the first boundary, including the trailing CRLF sequence
639 4 my $trash = $reader->read_until_in_memory( qr/--${boundary}${CRLF}/, include => 1, capture => 1 );
640 4 ▶ return( $self->pass_error( $reader->error ) ) if( !defined( $trash ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $trash 0 4 4
641 4 ▶ return( $self->error( "Unable to find the initial boundary '${boundary}'" ) ) if( !length( $trash ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $trash 0 4 4
642 4 my $delim = $reader->last_delimiter;
643 4 my $preamble;
644 4 ▶ if( length( $delim ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $delim) 4 0 4
645 {
646 4 $preamble = substr( $trash, 0, index( $trash, $delim ) );
647 4 $self->_trim_crlf( \$preamble );
648 4 ▶ $ent->preamble( [split( /$CRLF/, $preamble )] ) if( length( $preamble ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $preamble 2 2 4
649 }
650
651 4 my( $buff, $mime, $def );
652 4 while(1)
653 {
654 # I expect an header token and value, but if we find a CRLF sequence right now,
655 # this just means there is no header.
656 14 my $hdr = $reader->read_until_in_memory( qr/(?:^${CRLF}|(?:(?:$TOKEN+)[\x09\x20]*\:(?:.+?)${CRLF}${CRLF}))/, include => 1 );
657 14 ▶ return( $self->pass_error( $reader->error ) ) if( !defined( $hdr ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hdr 0 14 14
658 14 ▶ last if( !length( $hdr ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $hdr 0 14 14
659 14 my( $ph, $file, $mime_type, $ext, $io );
660 14 ▶ if( length( $hdr ) && $hdr !~ /^$CRLF$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (length $hdr and not $hdr =~ /^$HTTP::Promise::Parser::CRLF$/) { } 10 4 14
Condition66%2/3
length $hdr and not $hdr =~ /^$HTTP::Promise::Parser::CRLF$/
!ll&&!rl&&r
0410
MC/DC50%1/2
length $hdr and not $hdr =~ /^$HTTP::Promise::Parser::CRLF$/
length $hdr not $hdr =~ /^$HTTP::Promise::Parser::CRLF$/
Truth table66%2/3
A and not(B)
A= length $hdrB= not $hdr =~ /^$HTTP::Promise::Parser::CRLF$/
ABresult
0X0
100
111
661 {
662 10 ▶ $def = $self->parse_headers( $hdr ) || return( $self->pass_error );
Condition50%1/2
$self->parse_headers($hdr) || (return $self->pass_error)
l!l
100
MC/DC0%0/1
$self->parse_headers($hdr) || (return $self->pass_error)
$self->parse_headers($hdr)
Truth table50%1/2
A
A= $self->parse_headers($hdr)
Aresult
11
00
663 10 $ph = $def->{headers};
664 10 my $dispo = $ph->new_field( 'Content-Disposition' => $ph->content_disposition );
665 # rfc7231, section 3.1.1.5 says we can assume applicatin/octet-stream if there
666 # is no Content-Type header
667 # <https://tools.ietf.org/html/rfc7231#section-3.1.1.5>
668 10 $mime_type = $ph->mime_type( $default_mime );
669 }
670 # If no headers are set, we create a dummy object
671 else
672 {
673 4 ▶ $reader->unread( $hdr ) if( $hdr =~ /^$CRLF$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $hdr =~ /^$HTTP::Promise::Parser::CRLF$/ 4 0 4
674 4 $ph = HTTP::Promise::Headers->new;
675 }
676 14 ▶ my $part_ent = HTTP::Promise::Entity->new( headers => $ph, debug => $self->debug ) ||
Condition50%1/2
'HTTP::Promise::Entity'->new('headers', $ph, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::Entity'->error))
l!l
140
MC/DC0%0/1
'HTTP::Promise::Entity'->new('headers', $ph, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::Entity'->error))
'HTTP::Promise::Entity'->new('headers', $ph, 'debug', $self->debug)
Truth table50%1/2
A
A= 'HTTP::Promise::Entity'->new('headers', $ph, 'debug', $self->debug)
Aresult
11
00
677 return( $self->pass_error( HTTP::Promise::Entity->error ) );
678 14 ▶ if( defined( $mime_type ) && $mime_type eq 'multipart/form-data' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $mime_type and $mime_type eq 'multipart/form-data') { } 1 13 14
Condition100%3/3
defined $mime_type and $mime_type eq "multipart/form-data"
!ll&&!rl&&r
491
MC/DC100%2/2
defined $mime_type and $mime_type eq "multipart/form-data"
defined $mime_type $mime_type eq "multipart/form-data"
Truth table100%3/3
A and B
A= defined $mime_typeB= $mime_type eq "multipart/form-data"
ABresult
0X0
100
111
679 {
680 # $sub_part_ent and $part_ent should be the same
681 1 ▶ my $sub_part_ent = $self->parse_multi_part( entity => $part_ent, reader => $reader ) ||
Condition50%1/2
$self->parse_multi_part('entity', $part_ent, 'reader', $reader) || (return $self->pass_error)
l!l
10
MC/DC0%0/1
$self->parse_multi_part('entity', $part_ent, 'reader', $reader) || (return $self->pass_error)
$self->parse_multi_part('entity', $part_ent, 'reader', $reader)
Truth table50%1/2
A
A= $self->parse_multi_part('entity', $part_ent, 'reader', $reader)
Aresult
11
00
682 return( $self->pass_error );
683 # We need to consume the boundary for this part
684 # Position ourself just before the next delimiter, but do not include it.
685 # That way, we can capture epilogue data, if any, and although rare in HTTP parlance.
686 1 my $trash = $reader->read_until_in_memory( qr/${CRLF}--${boundary}(?:\-{2})?${CRLF}/, include => 1, capture => 1 );
687 1 my $delim = $reader->last_delimiter;
688 1 my $epilogue;
689 1 ▶ if( length( $delim ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $delim) 1 0 1
690 {
691 1 $epilogue = substr( $trash, 0, index( $trash, $delim ) );
692 1 $self->_trim_crlf( \$epilogue );
693 1 ▶ $ent->epilogue( [split( /$CRLF/, $epilogue )] ) if( length( $epilogue ) );
Branch: 1 branch
BranchTrueFalseTotal
if length $epilogue 1 0 1
694 }
695 # Post process to assign a name to each part, to make it easy to manipulate them
696 1 for( $part_ent->parts->list )
697 {
698 2 ▶ if( my $dispo = $_->headers->content_disposition )
Branch: 1 branch
BranchTrueFalseTotal
if (my $dispo = $_->headers->content_disposition) 2 0 2
699 {
700 2 my $cd = $_->headers->new_field( 'Content-Disposition' => $dispo );
701 2 ▶ return( $self->pass_error( $_->headers->error ) ) if( !defined( $cd ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cd 0 2 2
702 2 ▶ if( my $name = $cd->name )
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $cd->name) 2 0 2
703 {
704 2 $_->name( $name );
705 }
706 }
707 }
708 }
709 # Otherwise, we are dealing with a simple part
710 else
711 {
712 13 ▶ my $sub_part_ent = $self->parse_singleton(
Condition50%1/2
$self->parse_singleton('entity', $part_ent, 'reader', $reader, 'read_until', qr/$HTTP::Promise::Parser::CRLF--$boundary(?:\-{2})?$HTTP::Promise::Parser::CRLF/) || (return $self->pass_error)
l!l
130
MC/DC0%0/1
$self->parse_singleton('entity', $part_ent, 'reader', $reader, 'read_until', qr/$HTTP::Promise::Parser::CRLF--$boundary(?:\-{2})?$HTTP::Promise::Parser::CRLF/) || (return $self->pass_error)
$self->parse_singleton('entity', $part_ent, 'reader', $reader, 'read_until', qr/$HTTP::Promise::Parser::CRLF--$boundary(?:\-{2})?$HTTP::Promise::Parser::CRLF/)
Truth table50%1/2
A
A= $self->parse_singleton('entity', $part_ent, 'reader', $reader, 'read_until', qr/$HTTP::Promise::Parser::CRLF--$boundary(?:\-{2})?$HTTP::Promise::Parser::CRLF/)
Aresult
11
00
713 entity => $part_ent,
714 reader => $reader,
715 read_until => qr/${CRLF}--${boundary}(?:\-{2})?${CRLF}/,
716 ) || return( $self->pass_error );
717 }
718 14 $ent->parts->push( $part_ent );
719 # Have we reached the last delimiter yet?
720 14 my $last = $reader->last_delimiter;
721 # We hit the ending delimiter, stop here.
722 # If there are any epilogue, they will be dealt with by our caller
723 14 ▶ last if( $last =~ /\-\-${boundary}\-\-${CRLF}$/ );
Branch: 1 branch
BranchTrueFalseTotal
if $last =~ /\-\-$boundary\-\-$HTTP::Promise::Parser::CRLF$/ 4 10 14
724 }
725 4 return( $ent );
726 }
727  
728 sub parse_open
729 0 ▶{
Subroutine parse_open: not called CC=6 SCAR=37.4
Pod documented
730 0 my $self = shift( @_ );
731 0 ▶ my $expr = shift( @_ ) ||
Condition0%0/2
shift @_ || (return $self->error('No file to open was provided.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No file to open was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
732 return( $self->error( "No file to open was provided." ) );
733 0 my $ent;
734 0 ▶ my $f = $self->new_file( $expr ) ||
Condition0%0/2
$self->new_file($expr) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_file($expr) || (return $self->pass_error)
$self->new_file($expr)
Truth table0%0/2
A
A= $self->new_file($expr)
Aresult
11
00
735 return( $self->pass_error );
736 0 ▶ my $io = $f->open ||
Condition0%0/2
$f->open || (return $self->pass_error($f->error))
l!l
00
MC/DC0%0/1
$f->open || (return $self->pass_error($f->error))
$f->open
Truth table0%0/2
A
A= $f->open
Aresult
11
00
737 return( $self->pass_error( $f->error ) );
738 0 ▶ $ent = $self->parse( $io ) ||
Condition0%0/2
$self->parse($io) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->parse($io) || (return $self->pass_error)
$self->parse($io)
Truth table0%0/2
A
A= $self->parse($io)
Aresult
11
00
739 return( $self->pass_error );
740 0 ▶ $io->close or return( $self->pass_error( $io->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$io->close or return $self->pass_error($io->error) 0 0 0
741 0 return( $ent );
742 }
743  
744 sub parse_request($$)
745 0 ▶{
Subroutine parse_request: not called CC=3 SCAR=24.8
Pod documented
746 0 my $self = shift( @_ );
747 0 ▶ my $str = $self->_get_args( shift( @_ ) ) || return( $self->pass_error );
Condition0%0/2
$self->_get_args(shift @_) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_get_args(shift @_) || (return $self->pass_error)
$self->_get_args(shift @_)
Truth table0%0/2
A
A= $self->_get_args(shift @_)
Aresult
11
00
748 0 my $opts = $self->_get_args_as_hash( @_ );
749 0 ▶ my $req = $self->parse_request_headers( $str, $opts ) || return( $self->pass_error );
Condition0%0/2
$self->parse_request_headers($str, $opts) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->parse_request_headers($str, $opts) || (return $self->pass_error)
$self->parse_request_headers($str, $opts)
Truth table0%0/2
A
A= $self->parse_request_headers($str, $opts)
Aresult
11
00
750 0 substr( $$str, 0, $req->{length}, '' );
751 0 $req->{content} = $str;
752 0 return( $req );
753 }
754  
755 sub parse_request_headers($$)
756 1 ▶{
Subroutine parse_request_headers: called CC=2 SCAR=7.0
Pod documented
757 1 my $self = shift( @_ );
758 1 ▶ my $str = $self->_get_args( shift( @_ ) ) || return( $self->pass_error );
Condition50%1/2
$self->_get_args(shift @_) || (return $self->pass_error)
l!l
10
MC/DC0%0/1
$self->_get_args(shift @_) || (return $self->pass_error)
$self->_get_args(shift @_)
Truth table50%1/2
A
A= $self->_get_args(shift @_)
Aresult
11
00
759 1 my $opts = $self->_get_args_as_hash( @_ );
760 1 $opts->{request} = 1;
761 1 return( $self->parse_headers_xs( $str, $opts ) );
762 }
763  
764 sub parse_request_line($$)
765 0 ▶{
Subroutine parse_request_line: not called CC=4 SCAR=30.0
Pod documented
766 0 my $self = shift( @_ );
767 0 ▶ my $str = $self->_get_args( shift( @_ ) ) || return( $self->pass_error );
Condition0%0/2
$self->_get_args(shift @_) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_get_args(shift @_) || (return $self->pass_error)
$self->_get_args(shift @_)
Truth table0%0/2
A
A= $self->_get_args(shift @_)
Aresult
11
00
768 0 my $len = CORE::length( $$str );
769 0 ▶ $$str =~ s/^$REQUEST// or return( $self->error({ code => 400, message => 'Bad request-line', class => $EXCEPTION_CLASS }) );
Branch: 1 branch
BranchTrueFalseTotal
$$str =~ s/^$HTTP::Promise::Parser::REQUEST// or return $self->error({"code", 400, "message", "Bad request-line", "class", $HTTP::Promise::Parser::EXCEPTION_CLASS}) 0 0 0
770 0 ▶ my $res =
Condition0%0/2
$3 || 'HTTP/0.9'
l!l
00
MC/DC0%0/1
$3 || 'HTTP/0.9'
$3
Truth table0%0/2
A
A= $3
Aresult
11
00
771 {
772 method => $1,
773 path => $2,
774 protocol => ( $3 || 'HTTP/0.9' ),
775 length => ( $len - CORE::length( $$str ) ),
776 };
777 0 $res->{version} = $self->parse_version( $res->{protocol} );
778 0 return( $res );
779 }
780  
781 sub parse_request_pp($$)
782 19 ▶{
Subroutine parse_request_pp: called CC=6 SCAR=18.0
Pod documented
783 19 my $self = shift( @_ );
784 19 ▶ my $str = $self->_get_args( shift( @_ ) ) || return( $self->pass_error );
Condition50%1/2
$self->_get_args(shift @_) || (return $self->pass_error)
l!l
190
MC/DC0%0/1
$self->_get_args(shift @_) || (return $self->pass_error)
$self->_get_args(shift @_)
Truth table50%1/2
A
A= $self->_get_args(shift @_)
Aresult
11
00
785 19 ▶ my $req = $self->parse_headers( $str, @_ ) || return( $self->pass_error );
Condition100%2/2
$self->parse_headers($str, @_) || (return $self->pass_error)
l!l
109
MC/DC100%1/1
$self->parse_headers($str, @_) || (return $self->pass_error)
$self->parse_headers($str, @_)
Truth table100%2/2
A
A= $self->parse_headers($str, @_)
Aresult
11
00
786 10 substr( $$str, 0, $req->{length}, '' );
787 10 ▶ $$str =~ s/^$CRLF// or return( $self->error({ code => 400, message => 'Bad request', class => $EXCEPTION_CLASS }) );
Branch: 1 branch
BranchTrueFalseTotal
$$str =~ s/^$HTTP::Promise::Parser::CRLF// or return $self->error({"code", 400, "message", "Bad request", "class", $HTTP::Promise::Parser::EXCEPTION_CLASS}) 9 1 10
788 9 ▶ if( $req->{version} < version->parse( '1.0' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($req->{"version"} < "version"->parse("1.0")) 1 8 9
789 {
790 1 ▶ $$str eq '' or return( $self->error({ code => 400, message => 'Bad request', class => $EXCEPTION_CLASS }) );
Branch: 1 branch
BranchTrueFalseTotal
$$str eq "" or return $self->error({"code", 400, "message", "Bad request", "class", $HTTP::Promise::Parser::EXCEPTION_CLASS}) 1 0 1
791 }
792 9 $req->{content} = $str;
793 9 return( $req );
794 }
795  
796 sub parse_response($$)
797 0 ▶{
Subroutine parse_response: not called CC=3 SCAR=24.8
Pod documented
798 0 my $self = shift( @_ );
799 0 ▶ my $str = $self->_get_args( shift( @_ ) ) || return( $self->pass_error );
Condition0%0/2
$self->_get_args(shift @_) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_get_args(shift @_) || (return $self->pass_error)
$self->_get_args(shift @_)
Truth table0%0/2
A
A= $self->_get_args(shift @_)
Aresult
11
00
800 0 my $opts = $self->_get_args_as_hash( @_ );
801 0 ▶ my $resp = $self->parse_response_headers( $str, $opts ) || return( $self->pass_error );
Condition0%0/2
$self->parse_response_headers($str, $opts) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->parse_response_headers($str, $opts) || (return $self->pass_error)
$self->parse_response_headers($str, $opts)
Truth table0%0/2
A
A= $self->parse_response_headers($str, $opts)
Aresult
11
00
802 0 substr( $$str, 0, $resp->{length}, '' );
803 0 $resp->{content} = $str;
804 0 return( $resp );
805 }
806  
807 sub parse_response_headers($$)
808 0 ▶{
Subroutine parse_response_headers: not called CC=2 SCAR=17.9
Pod documented
809 0 my $self = shift( @_ );
810 0 ▶ my $str = $self->_get_args( shift( @_ ) ) || return( $self->pass_error );
Condition0%0/2
$self->_get_args(shift @_) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_get_args(shift @_) || (return $self->pass_error)
$self->_get_args(shift @_)
Truth table0%0/2
A
A= $self->_get_args(shift @_)
Aresult
11
00
811 0 my $opts = $self->_get_args_as_hash( @_ );
812 0 $opts->{response} = 1;
813 0 return( $self->parse_headers_xs( $str, $opts ) );
814 }
815  
816 sub parse_response_line($$)
817 0 ▶{
Subroutine parse_response_line: not called CC=3 SCAR=24.8
Pod documented
818 0 my $self = shift( @_ );
819 0 ▶ my $str = $self->_get_args( shift( @_ ) ) || return( $self->pass_error );
Condition0%0/2
$self->_get_args(shift @_) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->_get_args(shift @_) || (return $self->pass_error)
$self->_get_args(shift @_)
Truth table0%0/2
A
A= $self->_get_args(shift @_)
Aresult
11
00
820 0 my $len = CORE::length( $$str );
821 0 ▶ $$str =~ s/^$RESPONSE// or return( $self->error({ code => 400, message => 'Bad Status-Line', class => $EXCEPTION_CLASS }) );
Branch: 1 branch
BranchTrueFalseTotal
$$str =~ s/^$HTTP::Promise::Parser::RESPONSE// or return $self->error({"code", 400, "message", "Bad Status-Line", "class", $HTTP::Promise::Parser::EXCEPTION_CLASS}) 0 0 0
822 0 my $res =
823 {
824 protocol => $1,
825 code => $2,
826 status => $3,
827 length => ( $len - CORE::length( $$str ) ),
828 };
829 0 $res->{version} = $self->parse_version( $res->{protocol} );
830 0 return( $res );
831 }
832  
833 sub parse_response_pp($$)
834 13 ▶{
Subroutine parse_response_pp: called CC=4 SCAR=14.0
Pod documented
835 13 my $self = shift( @_ );
836 13 ▶ my $str = $self->_get_args( shift( @_ ) ) || return( $self->pass_error );
Condition50%1/2
$self->_get_args(shift @_) || (return $self->pass_error)
l!l
130
MC/DC0%0/1
$self->_get_args(shift @_) || (return $self->pass_error)
$self->_get_args(shift @_)
Truth table50%1/2
A
A= $self->_get_args(shift @_)
Aresult
11
00
837 13 my $opts = $self->_get_args_as_hash( @_ );
838 13 $opts->{no_headers_ok} = 1;
839 13 ▶ my $resp = $self->parse_headers( $str, $opts ) || return( $self->pass_error );
Condition100%2/2
$self->parse_headers($str, $opts) || (return $self->pass_error)
l!l
85
MC/DC100%1/1
$self->parse_headers($str, $opts) || (return $self->pass_error)
$self->parse_headers($str, $opts)
Truth table100%2/2
A
A= $self->parse_headers($str, $opts)
Aresult
11
00
840 8 substr( $$str, 0, $resp->{length}, '' );
841 8 ▶ $$str =~ s/^$CRLF// or return( $self->error({ code => 400, message => 'Bad response', class => $EXCEPTION_CLASS }) );
Branch: 1 branch
BranchTrueFalseTotal
$$str =~ s/^$HTTP::Promise::Parser::CRLF// or return $self->error({"code", 400, "message", "Bad response", "class", $HTTP::Promise::Parser::EXCEPTION_CLASS}) 8 0 8
842 8 $resp->{content} = $str;
843 8 return( $resp );
844 }
845  
846 # Called when there is no multipart
847 sub parse_singleton
848 17 ▶{
Subroutine parse_singleton: called CC=57 SCAR=67.6
Pod documented
849 17 my $self = shift( @_ );
850 17 my $opts = $self->_get_args_as_hash( @_ );
851 my $ent = $opts->{entity} ||
852 17 ▶ return( $self->error({ code => 500, message => "No entity object was provided." }) );
Condition50%1/2
$opts->{'entity'} || (return $self->error({'code', 500, 'message', 'No entity object was provided.'}))
l!l
170
MC/DC0%0/1
$opts->{'entity'} || (return $self->error({'code', 500, 'message', 'No entity object was provided.'}))
$opts->{'entity'}
Truth table50%1/2
A
A= $opts->{'entity'}
Aresult
11
00
853 my $reader = $opts->{reader} ||
854 17 ▶ return( $self->error({ code => 500, message => "No reader object was provided." }) );
Condition50%1/2
$opts->{'reader'} || (return $self->error({'code', 500, 'message', 'No reader object was provided.'}))
l!l
170
MC/DC0%0/1
$opts->{'reader'} || (return $self->error({'code', 500, 'message', 'No reader object was provided.'}))
$opts->{'reader'}
Truth table50%1/2
A
A= $opts->{'reader'}
Aresult
11
00
855 17 ▶ my $headers = $ent->headers ||
Condition50%1/2
$ent->headers || (return $self->error({'code', 500, 'message', 'No headers object found in entity object.'}))
l!l
170
MC/DC0%0/1
$ent->headers || (return $self->error({'code', 500, 'message', 'No headers object found in entity object.'}))
$ent->headers
Truth table50%1/2
A
A= $ent->headers
Aresult
11
00
856 return( $self->error({ code => 500, message => "No headers object found in entity object." }) );
857 17 my $type = $headers->type;
858 17 my $read_until = $opts->{read_until};
859 17 my $max_in_memory = $self->max_body_in_memory_size;
860 # rfc7231, section 3.1.1.5 says we can assume applicatin/octet-stream if there
861 # is no Content-Type header
862 # <https://tools.ietf.org/html/rfc7231#section-3.1.1.5>
863 17 ▶ my $default_mime = $DEFAULT_MIME_TYPE || 'application/octet-stream';
Condition50%1/2
$HTTP::Promise::Parser::DEFAULT_MIME_TYPE || 'application/octet-stream'
l!l
170
MC/DC0%0/1
$HTTP::Promise::Parser::DEFAULT_MIME_TYPE || 'application/octet-stream'
$HTTP::Promise::Parser::DEFAULT_MIME_TYPE
Truth table50%1/2
A
A= $HTTP::Promise::Parser::DEFAULT_MIME_TYPE
Aresult
11
00
864 17 my $len = $headers->content_length;
865 17 my $chunk_size = 2048;
866 17 my( $body, $file, $mime_type, $mime, $ext );
867 17 my $data = '';
868 17 my $total_bytes = 0;
869
870 my $get_temp_file = sub
871 0 ▶ {
Subroutine __ANON__: not called CC=5 SCAR=34.0
872 # Guessing extension
873 0 $mime_type = $headers->mime_type( $default_mime );
874 0 ▶ $self->_load_class( 'HTTP::Promise::MIME' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::MIME") or return $self->pass_error 0 0 0
875 0 $mime = HTTP::Promise::MIME->new;
876 0 $ext = $mime->suffix( $type );
877 0 ▶ return( $self->pass_error( $mime->error ) ) if( !defined( $ext ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext 0 0 0
878 0 ▶ $ext ||= 'dat';
Condition0%0/2
$ext ||= "dat"
l!l
00
MC/DC0%0/1
$ext ||= "dat"
$ext
Truth table0%0/2
A
A= $ext
Aresult
11
00
879 0 ▶ my $f = $self->new_tempfile( extension => $ext ) ||
Condition0%0/2
$self->new_tempfile('extension', $ext) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_tempfile('extension', $ext) || (return $self->pass_error)
$self->new_tempfile('extension', $ext)
Truth table0%0/2
A
A= $self->new_tempfile('extension', $ext)
Aresult
11
00
880 return( $self->pass_error );
881 0 return( $f );
882 17 };
883
884 17 ▶ if( defined( $len ) && !defined( $read_until ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $len and not defined $read_until) { } 1 16 17
Condition66%2/3
defined $len and not defined $read_until
!ll&&!rl&&r
1601
MC/DC50%1/2
defined $len and not defined $read_until
defined $len not defined $read_until
Truth table66%2/3
A and not(B)
A= defined $lenB= not defined $read_until
ABresult
0X0
100
111
885 {
886 # Nothing to be done. The body is zero byte, like in HEAD request
887 1 ▶ if( !$len )
Branch: 2 branches
BranchTrueFalseTotal
if (not $len) { } 0 1 1
elsif ($len > $max_in_memory) { } 0 1 1
888 {
889 #
890 }
891 # Too big, saving it to file
892 elsif( $len > $max_in_memory )
893 {
894 # We cannot save this to file if the type is application/x-www-form-urlencoded
895 0 ▶ if( $type eq TYPE_URL_ENCODED && $self->decode_body )
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "application/x-www-form-urlencoded" and $self->decode_body) 0 0 0
Condition0%0/3
$type eq "application/x-www-form-urlencoded" and $self->decode_body
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "application/x-www-form-urlencoded" and $self->decode_body
$type eq "application/x-www-form-urlencoded" $self->decode_body
Truth table0%0/3
A and B
A= $type eq "application/x-www-form-urlencoded"B= $self->decode_body
ABresult
0X0
100
111
896 {
897 # Payload too large
898 0 return( $self->error({ code => 413, message => "The data are url-encoded, but its total amount (${len}) exceeds the maximum allowed ($max_in_memory}). You may want to increase that limit or investigate this HTTP message." }) );
899 }
900 0 ▶ $file = $get_temp_file->() || return( $self->pass_error );
Condition0%0/2
&$get_temp_file() || (return $self->pass_error)
l!l
00
MC/DC0%0/1
&$get_temp_file() || (return $self->pass_error)
&$get_temp_file()
Truth table0%0/2
A
A= &$get_temp_file()
Aresult
11
00
901 0 ▶ my $io = $file->open( '+>', { binmode => 'raw', autoflush => 1 } ) ||
Condition0%0/2
$file->open('+>', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($file->error))
l!l
00
MC/DC0%0/1
$file->open('+>', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($file->error))
$file->open('+>', {'binmode', 'raw', 'autoflush', 1})
Truth table0%0/2
A
A= $file->open('+>', {'binmode', 'raw', 'autoflush', 1})
Aresult
11
00
902 return( $self->pass_error( $file->error ) );
903 0 my $buff = '';
904 0 my $bytes;
905 0 ▶ $chunk_size = $len if( $chunk_size > $len );
Branch: 1 branch
BranchTrueFalseTotal
if $chunk_size > $len 0 0 0
906 0 ▶ while( $bytes = $reader->read( $buff, $chunk_size ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes = $reader->read($buff, $chunk_size)) 0 0 0
907 {
908 0 ▶ $io->print( $buff ) || return( $self->pass_error( $io->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$io->print($buff) or return $self->pass_error($io->error) 0 0 0
909 # We do not want to read more than we should
910 0 ▶ $chunk_size = ( $len - $total_bytes ) if( $total_bytes + $chunk_size > $len );
Branch: 1 branch
BranchTrueFalseTotal
if $total_bytes + $chunk_size > $len 0 0 0
911 0 $total_bytes += $bytes;
912 0 ▶ last if( $total_bytes == $len );
Branch: 1 branch
BranchTrueFalseTotal
if $total_bytes == $len 0 0 0
913 }
914 0 $io->close;
915 0 ▶ return( $self->error( "Error reading http body from filehandle: ", $reader->error ) ) if( !defined( $bytes ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 0 0
916 }
917 else
918 {
919 1 my $bytes = $reader->read( $data, $len );
920 1 ▶ return( $self->error( "Error reading HTTP body from filehandle: ", $reader->error ) ) if( !defined( $bytes ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 1 1
921 # Assignment used for the warning below
922 1 $total_bytes = $bytes;
923 }
924 1 ▶ warn( ref( $self ), "->parse_singleton: Warning only: HTTP body size advertised ($len) does not match the size actually read from filehandle ($total_bytes)\n" ) if( $total_bytes != $len && warnings::enabled( 'HTTP::Promise' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $total_bytes != $len and warnings::enabled("HTTP::Promise") 0 1 1
Condition33%1/3
$total_bytes != $len and warnings::enabled("HTTP::Promise")
!ll&&!rl&&r
100
MC/DC0%0/2
$total_bytes != $len and warnings::enabled("HTTP::Promise")
$total_bytes != $len warnings::enabled("HTTP::Promise")
Truth table33%1/3
A and B
A= $total_bytes != $lenB= warnings::enabled("HTTP::Promise")
ABresult
0X0
100
111
925 }
926 # No Content-Length defined or there is a boundary expected
927 else
928 {
929 16 my $buff = '';
930 16 my $bytes = -1;
931 16 my $io;
932 16 ▶ while( $bytes )
Branch: 1 branch
BranchTrueFalseTotal
if ($bytes) 16 6 22
933 {
934 16 ▶ if( defined( $read_until ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $read_until) { } 13 3 16
935 {
936 13 $bytes = $reader->read_until( $buff, $chunk_size, { string => $read_until, ignore => 1, capture => 1 } );
937 }
938 else
939 {
940 3 $bytes = $reader->read( $buff, $chunk_size );
941 }
942 16 ▶ return( $self->pass_error( $reader->error ) ) if( !defined( $bytes ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 16 16
943
944 16 ▶ if( defined( $io ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $io) { } 0 16 16
elsif (length($data) + length($buff) > $max_in_memory) { } 0 16 16
945 {
946 0 ▶ $io->print( $buff ) || return( $self->pass_error( $io->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$io->print($buff) or return $self->pass_error($io->error) 0 0 0
947 }
948 # The cumulative bytes total for this part exceeds the allowed maximum in memory
949 elsif( ( length( $data ) + length( $buff ) ) > $max_in_memory )
950 {
951 0 ▶ if( $type eq TYPE_URL_ENCODED && $self->decode_body )
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "application/x-www-form-urlencoded" and $self->decode_body) 0 0 0
Condition0%0/3
$type eq "application/x-www-form-urlencoded" and $self->decode_body
!ll&&!rl&&r
000
MC/DC0%0/2
$type eq "application/x-www-form-urlencoded" and $self->decode_body
$type eq "application/x-www-form-urlencoded" $self->decode_body
Truth table0%0/3
A and B
A= $type eq "application/x-www-form-urlencoded"B= $self->decode_body
ABresult
0X0
100
111
952 {
953 # Payload too large
954 0 return( $self->error({ code => 413, message => sprintf( "The data are url-encoded, but its total amount so far (%d) is about to exceed the maximum allowed ($max_in_memory}). You may want to increase that limit or investigate this HTTP message.",length( $data ) ) }) );
955 }
956 0 ▶ $file = $get_temp_file->() || return( $self->pass_error );
Condition0%0/2
&$get_temp_file() || (return $self->pass_error)
l!l
00
MC/DC0%0/1
&$get_temp_file() || (return $self->pass_error)
&$get_temp_file()
Truth table0%0/2
A
A= &$get_temp_file()
Aresult
11
00
957 0 ▶ $io = $file->open( '+>', { binmode => 'raw', autoflush => 1 } ) ||
Condition0%0/2
$file->open('+>', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($file->error))
l!l
00
MC/DC0%0/1
$file->open('+>', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($file->error))
$file->open('+>', {'binmode', 'raw', 'autoflush', 1})
Truth table0%0/2
A
A= $file->open('+>', {'binmode', 'raw', 'autoflush', 1})
Aresult
11
00
958 return( $self->pass_error( $file->error ) );
959 0 ▶ $io->print( $data ) || return( $self->pass_error( $io->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$io->print($data) or return $self->pass_error($io->error) 0 0 0
960 0 $data = '';
961 }
962 else
963 {
964 16 $data .= $buff;
965 }
966 # reader returns negative bytes if those are the last bytes read until it reached the boundary
967 16 ▶ last if( $bytes < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $bytes < 0 10 6 16
968 }
969 16 ▶ $total_bytes = defined( $file ) ? $file->length : length( $data );
Branch: 1 branch
BranchTrueFalseTotal
defined $file ? : 0 16 16
970 }
971
972  
973 # If we used a file and the extension is 'dat', because we were clueless based on
974 # the provided Content-Type, or maybe even the Content-Type is absent, we use the
975 # XS module in HTTP::Promise::MIME to guess the mime-type based on the actual file
976 # content
977 17 ▶ if( defined( $file ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) { } 0 17 17
978 {
979 0 ▶ if( defined( $file ) && $mime_type eq $default_mime )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file and $mime_type eq $default_mime) 0 0 0
Condition0%0/3
defined $file and $mime_type eq $default_mime
!ll&&!rl&&r
000
MC/DC0%0/2
defined $file and $mime_type eq $default_mime
defined $file $mime_type eq $default_mime
Truth table0%0/3
A and B
A= defined $fileB= $mime_type eq $default_mime
ABresult
0X0
100
111
980 {
981 0 ▶ unless( $mime )
Branch: 1 branch
BranchTrueFalseTotal
unless ($mime) 0 0 0
982 {
983 0 ▶ $self->_load_class( 'HTTP::Promise::MIME' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::MIME") or return $self->pass_error 0 0 0
984 0 $mime = HTTP::Promise::MIME->new;
985 }
986
987 # Guess the mime type from the file magic
988 0 my $mtype = $mime->mime_type( $file );
989 0 ▶ return( $self->pass_error( $mime->error ) ) if( !defined( $mime_type ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mime_type 0 0 0
990 0 ▶ if( $mtype && $mtype ne $default_mime )
Branch: 1 branch
BranchTrueFalseTotal
if ($mtype and $mtype ne $default_mime) 0 0 0
Condition0%0/3
$mtype and $mtype ne $default_mime
!ll&&!rl&&r
000
MC/DC0%0/2
$mtype and $mtype ne $default_mime
$mtype $mtype ne $default_mime
Truth table0%0/3
A and B
A= $mtypeB= $mtype ne $default_mime
ABresult
0X0
100
111
991 {
992 0 $mime_type = $mtype;
993 # Also update the type value in HTTP::Promise::Headers
994 # It does not affect the actual Content-Type header
995 0 $headers->type( $mtype );
996 0 my $new_ext = $mime->suffix( $mtype );
997 0 ▶ return( $self->pass_error( $mime->error ) ) if( !defined( $new_ext ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $new_ext 0 0 0
998 0 ▶ if( $new_ext && $new_ext ne $ext )
Branch: 1 branch
BranchTrueFalseTotal
if ($new_ext and $new_ext ne $ext) 0 0 0
Condition0%0/3
$new_ext and $new_ext ne $ext
!ll&&!rl&&r
000
MC/DC0%0/2
$new_ext and $new_ext ne $ext
$new_ext $new_ext ne $ext
Truth table0%0/3
A and B
A= $new_extB= $new_ext ne $ext
ABresult
0X0
100
111
999 {
1000 0 ▶ my $new_file = $file->extension( $ext ) || return( $self->pass_error( $file->error ) );
Condition0%0/2
$file->extension($ext) || (return $self->pass_error($file->error))
l!l
00
MC/DC0%0/1
$file->extension($ext) || (return $self->pass_error($file->error))
$file->extension($ext)
Truth table0%0/2
A
A= $file->extension($ext)
Aresult
11
00
1001 0 ▶ my $this_file = $file->move( $new_file ) || return( $self->pass_error( $file->error ) );
Condition0%0/2
$file->move($new_file) || (return $self->pass_error($file->error))
l!l
00
MC/DC0%0/1
$file->move($new_file) || (return $self->pass_error($file->error))
$file->move($new_file)
Truth table0%0/2
A
A= $file->move($new_file)
Aresult
11
00
1002 0 $file = $this_file;
1003 }
1004 }
1005 }
1006
1007 0 ▶ $body = $ent->new_body( file => $file ) ||
Condition0%0/2
$ent->new_body('file', $file) || (return $self->pass_error($ent->error))
l!l
00
MC/DC0%0/1
$ent->new_body('file', $file) || (return $self->pass_error($ent->error))
$ent->new_body('file', $file)
Truth table0%0/2
A
A= $ent->new_body('file', $file)
Aresult
11
00
1008 return( $self->pass_error( $ent->error ) );
1009 }
1010 # in memory
1011 else
1012 {
1013 # If this is a application/x-www-form-urlencoded type, we save it as such, and
1014 # the HTTP::Promise::Body::Form makes those data accessible as an hash object
1015 17 ▶ if( defined( $type ) && $type eq TYPE_URL_ENCODED )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type and $type eq 'application/x-www-form-urlencoded') { } 1 16 17
Condition100%3/3
defined $type and $type eq "application/x-www-form-urlencoded"
!ll&&!rl&&r
971
MC/DC100%2/2
defined $type and $type eq "application/x-www-form-urlencoded"
defined $type $type eq "application/x-www-form-urlencoded"
Truth table100%3/3
A and B
A= defined $typeB= $type eq "application/x-www-form-urlencoded"
ABresult
0X0
100
111
1016 {
1017 1 ▶ $body = $ent->new_body( form => $data ) ||
Condition50%1/2
$ent->new_body('form', $data) || (return $self->pass_error($ent->error))
l!l
10
MC/DC0%0/1
$ent->new_body('form', $data) || (return $self->pass_error($ent->error))
$ent->new_body('form', $data)
Truth table50%1/2
A
A= $ent->new_body('form', $data)
Aresult
11
00
1018 return( $self->pass_error( $ent->error ) );
1019 }
1020 else
1021 {
1022 16 ▶ $body = $ent->new_body( string => $data ) ||
Condition50%1/2
$ent->new_body('string', $data) || (return $self->pass_error($ent->error))
l!l
160
MC/DC0%0/1
$ent->new_body('string', $data) || (return $self->pass_error($ent->error))
$ent->new_body('string', $data)
Truth table50%1/2
A
A= $ent->new_body('string', $data)
Aresult
11
00
1023 return( $self->pass_error( $ent->error ) );
1024 }
1025 }
1026 17 $ent->body( $body );
1027 17 my $enc = $headers->content_encoding;
1028 17 ▶ if( $enc && $body->length && $self->decode_body( $enc ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($enc and $body->length and $self->decode_body($enc)) 4 13 17
Condition66%4/6
$enc and $body->length and $self->decode_body($enc)
!ll&&!rl&&r
1304
$enc and $body->length
!ll&&!rl&&r
1340
MC/DC0%0/3
$enc and $body->length and $self->decode_body($enc)
$enc $body->length $self->decode_body($enc)
Truth table25%1/4
A and B and C
A= $encB= $body->lengthC= $self->decode_body($enc)
ABCresult
0XX0
10X0
1100
1111
1029 {
1030 4 ▶ $ent->decode_body( $enc ) || return( $self->pass_error( $ent->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$ent->decode_body($enc) or return $self->pass_error($ent->error) 4 0 4
1031 4 $ent->is_encoded(0);
1032 }
1033 17 return( $ent );
1034 }
1035  
1036 sub parse_version($$)
1037 7 ▶{
Subroutine parse_version: called CC=5 SCAR=17.3
Pod documented
1038 7 my $self = shift( @_ );
1039 7 my $str = shift( @_ );
1040 7 ▶ return( $self->error({ code => 400, message => "No data was provided to parse the http version", class => $EXCEPTION_CLASS }) ) if( !defined( $str ) || !length( $str ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and length $str 0 7 7
Condition33%1/3
defined $str and length $str
!ll&&!rl&&r
007
MC/DC0%0/2
defined $str and length $str
defined $str length $str
Truth table33%1/3
A and B
A= defined $strB= length $str
ABresult
0X0
100
111
1041 7 ▶ $str =~ m/^${HTTP_VERSION}$/ or
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /^$HTTP::Promise::Parser::HTTP_VERSION$/ or return $self->error({"code", 400, "message", "Bad HTTP-Version '${str}'", "class", $HTTP::Promise::Parser::EXCEPTION_CLASS}) 7 0 7
1042 return( $self->error({ code => 400, message => "Bad HTTP-Version '$str'", class => $EXCEPTION_CLASS }) );
1043 7 my $major = $+{http_vers_major};
1044 # May be undef if HTTP/2 for example
1045 7 my $minor = $+{http_vers_minor};
1046 7 my $v = version->parse( $+{http_version} );
1047 7 ▶ return( want( 'LIST' ) ? ( $major, $minor ) : $v );
Branch: 1 branch
BranchTrueFalseTotal
want('LIST') ? : 0 7 7
1048 }
1049  
1050 0 ▶sub tmp_dir { return( shift->_set_get_file( 'tmp_dir', @_ ) ); }
Subroutine tmp_dir: not called CC=1 SCAR=6.9
Pod documented
1051  
1052 0 ▶sub tmp_to_core { return( shift->_set_get_boolean( 'tmp_to_core', @_ ) ); }
Subroutine tmp_to_core: not called CC=1 SCAR=6.9
Pod documented
1053  
1054 sub _get_args
1055 90 ▶{
Subroutine _get_args: called CC=3 SCAR=11.2
1056 90 my $self = shift( @_ );
1057 90 my $str = shift( @_ );
1058 90 ▶ if( !ref( $str ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $str) { } 16 74 90
elsif (not $self->_is_scalar($str)) { } 0 74 74
1059 {
1060 16 $str = \"$str";
1061 }
1062 elsif( !$self->_is_scalar( $str ) )
1063 {
1064 0 return( $self->error({ code => 401, message => "Value provided (" . overload::StrVal( $str ) . ") is not a string", class => $EXCEPTION_CLASS }) );
1065 }
1066 90 return( $str );
1067 }
1068  
1069 sub _trim_crlf
1070 5 ▶{
Subroutine _trim_crlf: called CC=7 SCAR=19.8
1071 5 my $self = shift( @_ );
1072 5 my $ref = shift( @_ );
1073 5 ▶ return( $ref ) if( !defined( $ref ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ref 0 5 5
1074 5 ▶ die( "Bad argument. This must be provided a scalar reference.\n" ) if( !$self->_is_scalar( $ref ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_scalar($ref) 0 5 5
1075 5 my $n = 0;
1076 5 ▶ substr( $$ref, $n, 1, '' ), $n++ while( substr( $$ref, $n, 1 ) eq "\015" || substr( $$ref, $n, 1 ) eq "\012" );
Branch: 1 branch
BranchTrueFalseTotal
substr($$ref, $n, 1) eq "\r" || substr($$ref, $n, 1) eq "\n" and do { substr($$ref, $n, 1, ""), ++$n } 1 5 6
Condition66%2/3
substr($$ref, $n, 1) eq "\r" or substr($$ref, $n, 1) eq "\n"
l!l&&r!l&&!r
015
MC/DC50%1/2
substr($$ref, $n, 1) eq "\r" or substr($$ref, $n, 1) eq "\n"
substr($$ref, $n, 1) eq "\r" substr($$ref, $n, 1) eq "\n"
Truth table66%2/3
A or B
A= substr($$ref, $n, 1) eq "\r"B= substr($$ref, $n, 1) eq "\n"
ABresult
1X1
011
000
1077 5 $n = length( $$ref );
1078 5 ▶ $n-- while( substr( $$ref, ($n - 1), 1 ) eq "\015" || substr( $$ref, ($n - 1), 1 ) eq "\012" );
Branch: 1 branch
BranchTrueFalseTotal
substr($$ref, $n - 1, 1) eq "\r" || substr($$ref, $n - 1, 1) eq "\n" and do { --$n } 3 5 8
Condition66%2/3
substr($$ref, $n - 1, 1) eq "\r" or substr($$ref, $n - 1, 1) eq "\n"
l!l&&r!l&&!r
035
MC/DC50%1/2
substr($$ref, $n - 1, 1) eq "\r" or substr($$ref, $n - 1, 1) eq "\n"
substr($$ref, $n - 1, 1) eq "\r" substr($$ref, $n - 1, 1) eq "\n"
Truth table66%2/3
A or B
A= substr($$ref, $n - 1, 1) eq "\r"B= substr($$ref, $n - 1, 1) eq "\n"
ABresult
1X1
011
000
1079 5 substr( $$ref, $n, length( $$ref ), '' );
1080 5 return( $ref );
1081 }
1082  
1083 # NOTE: sub FREEZE is inherited
1084  
1085 1 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
1086  
1087 1 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
1088  
1089 # NOTE: sub THAW is inherited
1090  
1091 9 1;
1092 # NOTE: POD
1093 __END__
« Promise/Message.pm ↑ Summary Promise/Response.pm »