Promise/Entity.pm

filter: Statementstmt 51.8% 502 / 968 Branchbran 32.0% 225 / 702 Conditioncond 31.2% 131 / 419 MC/DCmcdc 8.4% 20 / 237 Subroutinesub 77.3% 58 / 75 Podpod 94.7% 54 / 57 totaltotal 40.2% 990 / 2458 CC 530 SCAR 58.6
CC
530
Cov
41%
dump
1640.0
decode_body
1071.9
save_file
756.0
CRAP 349.6
« Promise/Headers.pm ↑ Summary Promise/IO.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Entity.pm
3 ## Version v0.2.2
4 ## Copyright(c) 2023 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/04/19
7 ## Modified 2025/08/30
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::Entity;
15 BEGIN
16 10 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 warnings::register_categories( 'HTTP::Promise' );
20 10 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶ use vars qw( $VERSION $EXCEPTION_CLASS $BOUNDARY_DELIMITER $BOM2ENC $ENC2BOM $BOM_RE
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 $BOM_MAX_LENGTH $DEFAULT_MIME_TYPE );
23 10 ▶ use Crypt::Misc ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 10 ▶ use HTTP::Promise::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 10 ▶ use HTTP::Promise::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 10 ▶ use HTTP::Promise::Body;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 10 ▶ use Module::Generic::HeaderValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 # use Nice::Try;
29 10 ▶ use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 10 ▶ use URI::Escape::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 10 ▶ use constant CRLF => "\015\012";
Subroutine BEGIN: called CC=1 SCAR=0.0
32 10 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
33 10 our $BOUNDARY_DELIMITER = "\015\012";
34 10 our $DEFAULT_MIME_TYPE = 'application/octet-stream';
35 10 our $VERSION = 'v0.2.2';
36 };
37  
38 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 sub init
42 77 ▶{
Subroutine init: called CC=2 SCAR=6.9
Pod documented
43 77 my $self = shift( @_ );
44 77 $self->{body} = undef;
45 # Sie minimum from which compression is enabled, if mime type is suitable.
46 # Defaults to 200Kb
47 77 $self->{compression_min}= 204800;
48 77 $self->{effective_type} = undef;
49 77 $self->{epilogue} = undef;
50 77 $self->{ext_vary} = undef;
51 77 $self->{headers} = undef;
52 77 $self->{is_encoded} = 0;
53 77 $self->{output_dir} = undef;
54 77 $self->{preamble} = undef;
55 77 $self->{_init_strict_use_sub} = 1;
56 77 $self->{_exception_class} = $EXCEPTION_CLASS;
57 77 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 77 0 77
58 77 $self->{_parts} = [];
59 77 return( $self );
60 }
61  
62 sub add_part
63 4 ▶{
Subroutine add_part: called CC=4 SCAR=14.2
Pod documented
64 4 my $self = shift( @_ );
65 4 my( $part, $index ) = @_;
66 4 ▶ return( $self->error( "Part provided is not a HTTP::Promise::Entity object." ) ) if( !$self->_is_a( $part => 'HTTP::Promise::Entity' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($part, "HTTP::Promise::Entity") 0 4 4
67 4 my $parts = $self->_parts;
68 4 ▶ $index = -1 if( !defined( $index ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $index 4 0 4
69 4 ▶ $index = $parts->size + 2 + $index if( $index < 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $index < 0 4 0 4
70 4 $parts->splice( $index, 0, $part );
71 4 return( $part );
72 }
73  
74 sub as_form_data
75 1 ▶{
Subroutine as_form_data: called CC=15 SCAR=32.1
Pod documented
76 1 my $self = shift( @_ );
77 1 my $type = $self->headers->type;
78 1 ▶ return(0) unless( lc( $type ) eq 'multipart/form-data' );
Branch: 1 branch
BranchTrueFalseTotal
unless lc $type eq "multipart/form-data" 0 1 1
79 1 ▶ $self->_load_class( 'HTTP::Promise::Body::Form::Data' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Body::Form::Data") or return $self->pass_error 1 0 1
80 1 my $form = HTTP::Promise::Body::Form::Data->new;
81 1 ▶ $form->debug( $self->debug // 0 );
Condition50%1/2
$self->debug // 0
l!l
01
MC/DC0%0/1
$self->debug // 0
$self->debug
Truth table50%1/2
A
A= $self->debug
Aresult
11
00
82 1 my $parts = $self->parts;
83 # nothing to do
84 1 ▶ return( $form ) if( $parts->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if $parts->is_empty 0 1 1
85 1 foreach my $part ( @$parts )
86 {
87 4 my $headers = $part->headers;
88 4 my $body = $part->body;
89 4 my $name;
90 4 my $dispo = $headers->content_disposition;
91 4 ▶ next unless( $dispo );
Branch: 1 branch
BranchTrueFalseTotal
unless $dispo 0 4 4
92 4 my $cd = $headers->new_field( 'Content-Disposition' => "$dispo" );
93 4 ▶ return( $self->pass_error( $headers->error ) ) if( !defined( $cd ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cd 0 4 4
94 4 $name = $cd->name;
95 4 ▶ next if( !defined( $name ) || !length( "$name" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length "$name" 0 4 4
Condition33%1/3
defined $name and length "$name"
!ll&&!rl&&r
004
MC/DC0%0/2
defined $name and length "$name"
defined $name length "$name"
Truth table33%1/3
A and B
A= defined $nameB= length "$name"
ABresult
0X0
100
111
96 4 my $encodings = $headers->content_encoding;
97 4 ▶ if( $part->is_encoded && $encodings )
Branch: 1 branch
BranchTrueFalseTotal
if ($part->is_encoded and $encodings) 0 4 4
Condition33%1/3
$part->is_encoded and $encodings
!ll&&!rl&&r
400
MC/DC0%0/2
$part->is_encoded and $encodings
$part->is_encoded $encodings
Truth table33%1/3
A and B
A= $part->is_encodedB= $encodings
ABresult
0X0
100
111
98 {
99 0 ▶ $body = $part->decode_body( encoding => $encodings ) ||
Condition0%0/2
$part->decode_body('encoding', $encodings) || (return $self->pass_error($part->error))
l!l
00
MC/DC0%0/1
$part->decode_body('encoding', $encodings) || (return $self->pass_error($part->error))
$part->decode_body('encoding', $encodings)
Truth table0%0/2
A
A= $part->decode_body('encoding', $encodings)
Aresult
11
00
100 return( $self->pass_error( $part->error ) );
101 }
102
103 4 my $field = $form->new_field(
104 name => $name,
105 body => $body,
106 headers => $headers,
107 );
108 4 ▶ return( $self->pass_error( $form->error ) ) if( !defined( $field ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 4 4
109
110 4 ▶ if( exists( $form->{ $name } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $form->{$name}) { } 0 4 4
111 {
112 0 $form->{ $name } = [$form->{ $name }];
113 0 push( @{$form->{ $name }}, $field );
114 }
115 else
116 {
117 4 $form->{ $name } = $field;
118 }
119 }
120 1 return( $form );
121 }
122  
123 sub as_string
124 8 ▶{
Subroutine as_string: called CC=11 SCAR=31.4
Pod documented
125 8 my $self = shift( @_ );
126 8 my $eol = shift( @_ );
127 8 my $opts = $self->_get_args_as_hash( @_ );
128 8 ▶ $opts->{eol} = $eol if( defined( $eol ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $eol 7 1 8
129 8 my $output = $self->new_scalar;
130 # Because of an edge case where open with :binmode(utf-8) layer does not decode properly \x{FF}
131 # but Encode::decode( 'utf-8', $buff ) does, and since the body is loaded into a string
132 # anyway, we first read the data as raw and then decode it with Encode
133 8 my $binmode;
134 8 ▶ if( exists( $opts->{binmode} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"binmode"} and length $opts->{"binmode"} and lc substr($opts->{"binmode"}, 0, 3) eq "utf") 0 8 8
Condition33%2/6
exists $opts->{"binmode"} and length $opts->{"binmode"} and lc substr($opts->{"binmode"}, 0, 3) eq "utf"
!ll&&!rl&&r
800
exists $opts->{"binmode"} and length $opts->{"binmode"}
!ll&&!rl&&r
800
MC/DC0%0/3
exists $opts->{"binmode"} and length $opts->{"binmode"} and lc substr($opts->{"binmode"}, 0, 3) eq "utf"
exists $opts->{"binmode"} length $opts->{"binmode"} lc substr($opts->{"binmode"}, 0, 3) eq "utf"
Truth table25%1/4
A and B and C
A= exists $opts->{"binmode"}B= length $opts->{"binmode"}C= lc substr($opts->{"binmode"}, 0, 3) eq "utf"
ABCresult
0XX0
10X0
1100
1111
135 length( $opts->{binmode} ) &&
136 lc( substr( $opts->{binmode}, 0, 3 ) ) eq 'utf' )
137 {
138 0 $binmode = delete( $opts->{binmode} );
139 0 $opts->{binmode} = 'raw';
140 }
141 8 ▶ my $fh = $output->open( '>' ) || return( $self->pass_error( $output->error ) );
Condition50%1/2
$output->open('>') || (return $self->pass_error($output->error))
l!l
80
MC/DC0%0/1
$output->open('>') || (return $self->pass_error($output->error))
$output->open('>')
Truth table50%1/2
A
A= $output->open('>')
Aresult
11
00
142 8 $fh->binmode( ':raw' );
143 # $self->print( $fh, ( scalar( keys( %$opts ) ) ? $opts : () ) ) || return( $self->pass_error );
144 8 ▶ $self->print( $fh, ( scalar( keys( %$opts ) ) ? $opts : () ) ) || return( $self->pass_error );
Branch: 2 branches
BranchTrueFalseTotal
$self->print($fh, scalar keys %$opts ? $opts : ()) or return $self->pass_error 8 0 8
scalar keys %$opts ? : 7 1 8
145 8 $fh->close;
146 8 ▶ if( defined( $binmode ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $binmode) 0 8 8
147 {
148 0 ▶ $self->_load_class( 'Encode' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Encode") or return $self->pass_error 0 0 0
149 # try-catch
150 0 local $@;
151 eval
152 0 {
153 0 $$output = Encode::decode( $binmode, $$output, ( Encode::FB_DEFAULT | Encode::LEAVE_SRC ) );
154 };
155 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
156 {
157 0 return( $self->error( "Error decoding body content with character encoding '$binmode': $@" ) );
158 }
159 }
160 8 return( $output );
161 }
162  
163 sub attach
164 3 ▶{
Subroutine attach: called CC=4 SCAR=15.2
Pod documented
165 3 my $self = shift( @_ );
166 3 ▶ my $class = ref( $self ) || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
300
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
167 3 ▶ $self->make_multipart || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->make_multipart or return $self->pass_error 3 0 3
168 3 ▶ my $part = $class->build( @_, top => 0 ) ||
Condition50%1/2
$class->build(@_, 'top', 0) || (return $self->pass_error($class->error))
l!l
30
MC/DC0%0/1
$class->build(@_, 'top', 0) || (return $self->pass_error($class->error))
$class->build(@_, 'top', 0)
Truth table50%1/2
A
A= $class->build(@_, 'top', 0)
Aresult
11
00
169 return( $self->pass_error( $class->error ) );
170 3 return( $self->add_part( $part ) );
171 }
172  
173 118 ▶sub body { return( shift->_set_get_object_without_init( 'body', [qw( HTTP::Promise::Body HTTP::Promise::Body::Form )], @_ ) ); }
Subroutine body: called CC=1 SCAR=0.0
Pod documented
174  
175 sub body_as_array
176 0 ▶{
Subroutine body_as_array: not called CC=6 SCAR=37.4
Pod documented
177 0 my $self = shift( @_ );
178 0 ▶ my $eol = @_ ? shift( @_ ) : CRLF;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
179 0 ▶ return( $self->error( "You cannot use the method body() to set the encoded contents." ) ) if( scalar( @_ ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 0 0
180 0 my $output = $self->new_scalar;
181 0 ▶ my $fh = $output->open( '>' ) ||
Condition0%0/2
$output->open('>') || (return $self->pass_error($output->error))
l!l
00
MC/DC0%0/1
$output->open('>') || (return $self->pass_error($output->error))
$output->open('>')
Truth table0%0/2
A
A= $output->open('>')
Aresult
11
00
182 return( $self->pass_error( $output->error ) );
183 0 ▶ $self->print_body( $fh ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->print_body($fh) or return $self->pass_error 0 0 0
184 0 $fh->close;
185 0 my $ary = $output->split( qr/\015?\012/ );
186 0 for( @$ary )
187 {
188 0 $_ .= $eol;
189 }
190 0 return( $ary );
191 }
192  
193 sub body_as_string
194 1 ▶{
Subroutine body_as_string: called CC=10 SCAR=32.9
Pod documented
195 1 my $self = shift( @_ );
196 1 my $opts = $self->_get_args_as_hash( @_ );
197 1 my $output = $self->new_scalar;
198 # Because of an edge case where open with :binmode(utf-8) layer does not decode properly \x{FF}
199 # but Encode::decode( 'utf-8', $buff ) does, and since the body is loaded into a string
200 # anyway, we first read the data as raw and then decode it with Encode
201 1 my $binmode;
202 1 ▶ if( exists( $opts->{binmode} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{"binmode"} and length $opts->{"binmode"} and lc substr($opts->{"binmode"}, 0, 3) eq "utf") 0 1 1
Condition33%2/6
exists $opts->{"binmode"} and length $opts->{"binmode"} and lc substr($opts->{"binmode"}, 0, 3) eq "utf"
!ll&&!rl&&r
100
exists $opts->{"binmode"} and length $opts->{"binmode"}
!ll&&!rl&&r
100
MC/DC0%0/3
exists $opts->{"binmode"} and length $opts->{"binmode"} and lc substr($opts->{"binmode"}, 0, 3) eq "utf"
exists $opts->{"binmode"} length $opts->{"binmode"} lc substr($opts->{"binmode"}, 0, 3) eq "utf"
Truth table25%1/4
A and B and C
A= exists $opts->{"binmode"}B= length $opts->{"binmode"}C= lc substr($opts->{"binmode"}, 0, 3) eq "utf"
ABCresult
0XX0
10X0
1100
1111
203 length( $opts->{binmode} ) &&
204 lc( substr( $opts->{binmode}, 0, 3 ) ) eq 'utf' )
205 {
206 0 $binmode = delete( $opts->{binmode} );
207 0 $opts->{binmode} = 'raw';
208 }
209 1 ▶ my $fh = $output->open( '>' ) ||
Condition50%1/2
$output->open('>') || (return $self->pass_error($output->error))
l!l
10
MC/DC0%0/1
$output->open('>') || (return $self->pass_error($output->error))
$output->open('>')
Truth table50%1/2
A
A= $output->open('>')
Aresult
11
00
210 return( $self->pass_error( $output->error ) );
211 1 ▶ $self->print_body( $fh, ( scalar( keys( %$opts ) ) ? $opts : () ) ) || return( $self->pass_error );
Branch: 2 branches
BranchTrueFalseTotal
$self->print_body($fh, scalar keys %$opts ? $opts : ()) or return $self->pass_error 1 0 1
scalar keys %$opts ? : 0 1 1
212 1 $fh->close;
213 1 ▶ if( defined( $binmode ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $binmode) 0 1 1
214 {
215 0 ▶ $self->_load_class( 'Encode' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Encode") or return $self->pass_error 0 0 0
216 # try-catch
217 0 local $@;
218 eval
219 0 {
220 0 $$output = Encode::decode( $binmode, $$output, ( Encode::FB_DEFAULT | Encode::LEAVE_SRC ) );
221 };
222 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
223 {
224 0 return( $self->error( "Error decoding body content with character encoding '$binmode': $@" ) );
225 }
226 }
227 1 return( $output );
228 }
229  
230 sub build
231 17 ▶{
Subroutine build: called CC=60 SCAR=50.0
Pod documented
232 17 my $self = shift( @_ );
233 17 my( $opts, $order ) = $self->_get_args_as_hash( @_ );
234 17 my( $field, $filename, $boundary );
235 17 ▶ my $type = delete( $opts->{type} ) || 'text/plain';
Condition100%2/2
delete $opts->{'type'} || 'text/plain'
l!l
710
MC/DC100%1/1
delete $opts->{'type'} || 'text/plain'
delete $opts->{'type'}
Truth table100%2/2
A
A= delete $opts->{'type'}
Aresult
11
00
236 17 my $charset = delete( $opts->{charset} );
237 17 ▶ my $is_multipart = ( $type =~ m{^multipart/}i ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$type =~ m[^multipart/]i ? : 2 15 17
238 17 ▶ my $encoding = delete( $opts->{encoding} ) || '';
Condition100%2/2
delete $opts->{'encoding'} || ''
l!l
710
MC/DC100%1/1
delete $opts->{'encoding'} || ''
delete $opts->{'encoding'}
Truth table100%2/2
A
A= delete $opts->{'encoding'}
Aresult
11
00
239 17 my $desc = delete( $opts->{description} );
240 17 ▶ my $top = exists( $opts->{top} ) ? delete( $opts->{top} ) : 1;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'top'} ? : 3 14 17
241 # my $disposition = $opts->{disposition} || 'inline';
242 # inline, attachment or multipart/form-data
243 # Ref: <https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition>
244 # We could, technically, default to 'inline' and end up with something like:
245 # Content-Disposition: inline; filename=foo.txt
246 # But, even though it would be ok for mail, for HTTP, it would be weird, so, no default
247 # and instead if a path is provided, but no Content-Disposition, we fall back to 'attachment'
248 17 my $disposition = delete( $opts->{disposition} );
249 17 my $id = delete( $opts->{id} );
250 17 ▶ my $debug = delete( $opts->{debug} ) // 0;
Condition100%2/2
delete $opts->{"debug"} // 0
l!l
314
MC/DC100%1/1
delete $opts->{"debug"} // 0
delete $opts->{"debug"}
Truth table100%2/2
A
A= delete $opts->{"debug"}
Aresult
11
00
251 # Ensure this is an object
252 17 ▶ my $new = $self->new( debug => $debug ) || return( $self->pass_error );
Condition50%1/2
$self->new('debug', $debug) || (return $self->pass_error)
l!l
170
MC/DC0%0/1
$self->new('debug', $debug) || (return $self->pass_error)
$self->new('debug', $debug)
Truth table50%1/2
A
A= $self->new('debug', $debug)
Aresult
11
00
253 17 ▶ my $headers = HTTP::Promise::Headers->new( { debug => $self->debug } ) ||
Condition50%1/2
'HTTP::Promise::Headers'->new({'debug', $self->debug}) || (return $self->pass_error('HTTP::Promise::Headers'->error))
l!l
170
MC/DC0%0/1
'HTTP::Promise::Headers'->new({'debug', $self->debug}) || (return $self->pass_error('HTTP::Promise::Headers'->error))
'HTTP::Promise::Headers'->new({'debug', $self->debug})
Truth table50%1/2
A
A= 'HTTP::Promise::Headers'->new({'debug', $self->debug})
Aresult
11
00
254 return( $self->pass_error( HTTP::Promise::Headers->error ) );
255 17 $new->headers( $headers );
256
257 # Either data or path
258 17 my $data = delete( $opts->{data} );
259 17 my $path = delete( $opts->{path} );
260 17 ▶ my( $path_fname ) = ( ( $path || '' ) =~ m{([^/]+)\Z} );
Condition100%2/2
$path || ''
l!l
125
MC/DC100%1/1
$path || ''
$path
Truth table100%2/2
A
A= $path
Aresult
11
00
261 17 ▶ $filename = ( exists( $opts->{filename} ) ? delete( $opts->{filename} ) : $path_fname );
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'filename'} ? : 2 15 17
262 17 ▶ $filename = undef() if( defined( $filename ) and $filename eq '' );
Branch: 1 branch
BranchTrueFalseTotal
if defined $filename and $filename eq "" 0 17 17
Condition66%2/3
defined $filename and $filename eq ""
!ll&&!rl&&r
6110
MC/DC0%0/2
defined $filename and $filename eq ""
defined $filename $filename eq ""
Truth table66%2/3
A and B
A= defined $filenameB= $filename eq ""
ABresult
0X0
100
111
263 17 my $filename_utf8;
264 17 ▶ if( defined( $filename ) && length( $filename ) && $filename =~ /[^\w\.]+/ )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $filename and length $filename and $filename =~ /[^\w\.]+/) 3 14 17
Condition83%5/6
defined $filename and length $filename and $filename =~ /[^\w\.]+/
!ll&&!rl&&r
683
defined $filename and length $filename
!ll&&!rl&&r
6011
MC/DC66%2/3
defined $filename and length $filename and $filename =~ /[^\w\.]+/
defined $filename length $filename $filename =~ /[^\w\.]+/
Truth table75%3/4
A and B and C
A= defined $filenameB= length $filenameC= $filename =~ /[^\w\.]+/
ABCresult
0XX0
10X0
1100
1111
265 {
266 3 $filename_utf8 = $new->headers->encode_filename( $filename );
267 }
268 17 ▶ if( defined( $encoding ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding and $type =~ m[^(multipart/|message/(rfc822|partial|external-body|delivery-status|disposition-notification|feedback-report|http)$)]i) 3 14 17
Condition66%2/3
defined $encoding and $type =~ m[^(multipart/|message/(rfc822|partial|external-body|delivery-status|disposition-notification|feedback-report|http)$)]i
!ll&&!rl&&r
0143
MC/DC50%1/2
defined $encoding and $type =~ m[^(multipart/|message/(rfc822|partial|external-body|delivery-status|disposition-notification|feedback-report|http)$)]i
defined $encoding $type =~ m[^(multipart/|message/(rfc822|partial|external-body|delivery-status|disposition-notification|feedback-report|http)$)]i
Truth table66%2/3
A and B
A= defined $encodingB= $type =~ m[^(multipart/|message/(rfc822|partial|external-body|delivery-status|disposition-notification|feedback-report|http)$)]i
ABresult
0X0
100
111
269 $type =~ m{^(multipart/|message/(rfc822|partial|external-body|delivery-status|disposition-notification|feedback-report|http)$)}i )
270 {
271 3 undef( $encoding );
272 }
273
274 # Multipart or not? Do sanity check and fixup:
275 17 ▶ if( $is_multipart )
Branch: 1 branch
BranchTrueFalseTotal
if ($is_multipart) { } 2 15 17
276 {
277 # Get any supplied boundary, and check it:
278 2 ▶ if( defined( $boundary = delete( $opts->{boundary} ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($boundary = delete $opts->{"boundary"})) 0 2 2
279 {
280 0 ▶ if( !length( $boundary ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not length $boundary) { } 0 0 0
elsif ($boundary =~ m[[^0-9a-zA-Z_\'\(\)\+\,\.\/\:\=\?\- ]]) { } 0 0 0
281 {
282 0 ▶ warn( "Empty string not a legal boundary: I am ignoring it\n" ) if( $self->_warnings_is_enabled );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_warnings_is_enabled 0 0 0
283 0 $boundary = undef();
284 }
285 elsif( $boundary =~ m{[^0-9a-zA-Z_\'\(\)\+\,\.\/\:\=\?\- ]} )
286 {
287 0 ▶ warn( "Boundary ignored: illegal characters ($boundary)\n" ) if( $self->_warnings_is_enabled );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_warnings_is_enabled 0 0 0
288 0 $boundary = undef();
289 }
290 }
291 # If we have to roll our own boundary, do so:
292 2 ▶ $boundary = $new->make_boundary if( !defined( $boundary ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $boundary 2 0 2
293 }
294 # Or this is a single part
295 else
296 {
297 # Create body:
298 15 ▶ if( defined( $path ) && length( $path ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $path and length $path) { } 12 3 15
elsif (defined $data and length $data) { } 3 0 3
Condition66%2/3
defined $path and length $path
!ll&&!rl&&r
3012
MC/DC50%1/2
defined $path and length $path
defined $path length $path
Truth table66%2/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
299 {
300 12 ▶ my $f = HTTP::Promise::Body::File->new( $path ) ||
Condition50%1/2
'HTTP::Promise::Body::File'->new($path) || (return $self->pass_error('HTTP::Promise::Body::File'->error))
l!l
120
MC/DC0%0/1
'HTTP::Promise::Body::File'->new($path) || (return $self->pass_error('HTTP::Promise::Body::File'->error))
'HTTP::Promise::Body::File'->new($path)
Truth table50%1/2
A
A= 'HTTP::Promise::Body::File'->new($path)
Aresult
11
00
301 return( $self->pass_error( HTTP::Promise::Body::File->error ) );
302 12 ▶ $new->body( $f ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$new->body($f) or return $self->pass_error 12 0 12
303 # Set the Content-Disposition to 'attachment' by default if not set
304 # $disposition = 'attachment' if( !defined( $disposition ) || !length( $disposition ) );
305 }
306 ▶ elsif( defined( $data ) && length( $data ) )
Condition33%1/3
defined $data and length $data
!ll&&!rl&&r
003
MC/DC0%0/2
defined $data and length $data
defined $data length $data
Truth table33%1/3
A and B
A= defined $dataB= length $data
ABresult
0X0
100
111
307 {
308 3 ▶ my $s = HTTP::Promise::Body::InCore->new( $data ) ||
Condition50%1/2
'HTTP::Promise::Body::InCore'->new($data) || (return $self->pass_error('HTTP::Promise::Body::InCore'->error))
l!l
30
MC/DC0%0/1
'HTTP::Promise::Body::InCore'->new($data) || (return $self->pass_error('HTTP::Promise::Body::InCore'->error))
'HTTP::Promise::Body::InCore'->new($data)
Truth table50%1/2
A
A= 'HTTP::Promise::Body::InCore'->new($data)
Aresult
11
00
309 return( $self->pass_error( HTTP::Promise::Body::InCore->error ) );
310 3 ▶ $new->body( $s ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$new->body($s) or return $self->pass_error 3 0 3
311 }
312 else
313 {
314 0 return( $self->error( "Unable to build HTTP entity: no body, and not multipart" ) );
315 }
316 # $self->body->binmode(1) unless( $self->textual_type( $type ) );
317 }
318
319 17 my $ct = Module::Generic::HeaderValue->new_from_header( $type );
320 17 ▶ return( $self->pass_error( Module::Generic::HeaderValue->error ) ) if( !defined( $ct ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ct 0 17 17
321 17 ▶ $ct->param( charset => $charset ) if( $charset );
Branch: 1 branch
BranchTrueFalseTotal
if $charset 1 16 17
322 17 ▶ if( defined( $filename_utf8 ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $filename_utf8) { } 3 14 17
elsif (defined $filename) { } 8 6 14
323 {
324 3 $ct->param( 'name*' => sprintf( "UTF-8''%s", $filename_utf8 ) );
325 }
326 elsif( defined( $filename ) )
327 {
328 8 $ct->param( name => $filename );
329 }
330 17 ▶ $ct->param( boundary => $boundary ) if( defined( $boundary ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $boundary 2 15 17
331 17 $headers->replace( 'Content-Type' => "$ct" );
332
333 17 ▶ if( defined( $encoding ) && lc( $encoding ) eq 'suggest' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding and lc $encoding eq "suggest") 3 14 17
Condition100%3/3
defined $encoding and lc $encoding eq "suggest"
!ll&&!rl&&r
3113
MC/DC100%2/2
defined $encoding and lc $encoding eq "suggest"
defined $encoding lc $encoding eq "suggest"
Truth table100%3/3
A and B
A= defined $encodingB= lc $encoding eq "suggest"
ABresult
0X0
100
111
334 {
335 3 $encoding = $new->suggest_encoding;
336 }
337
338 # unless( $is_multipart )
339 17 ▶ if( !$is_multipart && ( defined( $disposition ) || defined( $filename ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not $is_multipart and defined $disposition || defined $filename) 11 6 17
Condition100%6/6
not $is_multipart and defined $disposition || defined $filename
!ll&&!rl&&r
2411
defined $disposition or defined $filename
l!l&&r!l&&!r
1104
MC/DC100%3/3
not $is_multipart and defined $disposition || defined $filename
not $is_multipart defined $disposition defined $filename
Truth table100%4/4
not(A) and B or C
A= not $is_multipartB= defined $dispositionC= defined $filename
ABCresult
0XX0
1000
11X1
1011
340 {
341 11 ▶ $disposition = 'attachment' if( !defined( $disposition ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $disposition 10 1 11
342 11 ▶ $field = Module::Generic::HeaderValue->new_from_header( ( defined( $disposition ) ? $disposition : () ) );
Branch: 1 branch
BranchTrueFalseTotal
defined $disposition ? : 11 0 11
343 11 ▶ return( $self->pass_error( Module::Generic::HeaderValue->error ) ) if( !defined( $field ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $field 0 11 11
344 11 ▶ if( defined( $filename_utf8 ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $filename_utf8) { } 3 8 11
elsif (defined $filename) { } 8 0 8
345 {
346 3 $field->param( 'filename*' => sprintf( "UTF-8''%s", $filename_utf8 ) );
347 }
348 elsif( defined( $filename ) )
349 {
350 8 $field->param( filename => $filename );
351 }
352 11 $headers->replace( 'Content-disposition', "$field" );
353 }
354 17 ▶ $headers->replace( 'Content-encoding', $encoding ) if( defined( $encoding ) && length( $encoding ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $encoding and length $encoding 2 15 17
Condition100%3/3
defined $encoding and length $encoding
!ll&&!rl&&r
3122
MC/DC100%2/2
defined $encoding and length $encoding
defined $encoding length $encoding
Truth table100%3/3
A and B
A= defined $encodingB= length $encoding
ABresult
0X0
100
111
355 17 ▶ if( defined( $desc ) && length( $desc ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $desc and length $desc) 0 17 17
Condition33%1/3
defined $desc and length $desc
!ll&&!rl&&r
1700
MC/DC0%0/2
defined $desc and length $desc
defined $desc length $desc
Truth table33%1/3
A and B
A= defined $descB= length $desc
ABresult
0X0
100
111
356 {
357 0 ▶ warn( "There is no Content-Description in HTTP protocole\n" ) if( $self->_warnings_is_enabled );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_warnings_is_enabled 0 0 0
358 }
359  
360 17 ▶ if( defined( $id ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 0 17 17
361 {
362 0 ▶ warn( "There is no Content-ID for HTTP multipart data\n" ) if( $self->_warnings_is_enabled );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_warnings_is_enabled 0 0 0
363 }
364
365 17 foreach( @$order )
366 {
367 # Maybe it has been removed since then? So that only headers remain
368 40 ▶ next if( !exists( $opts->{ $_ } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{$_} 40 0 40
369 # Value is undef -> remove the header, if any.
370 0 ▶ if( !defined( $opts->{ $_ } ) )
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $opts->{$_}) { } 0 0 0
elsif (length $opts->{$_}) { } 0 0 0
371 {
372 0 $headers->remove_header( $_ );
373 }
374 elsif( length( $opts->{ $_ } ) )
375 {
376 0 $headers->delete( $_ );
377 0 ▶ foreach my $val ( $self->_is_array( $opts->{ $_ } ) ? @{$opts->{ $_ }} : ( $opts->{ $_ } ) )
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_array($opts->{$_}) ? : 0 0 0
378 {
379 0 $headers->add( $_ => $val );
380 }
381 }
382 }
383 17 return( $new );
384 }
385  
386 sub clone
387 0 ▶{
Subroutine clone: not called CC=12 SCAR=50.5
Pod documented
388 0 my $self = shift( @_ );
389 0 my $opts = $self->_get_args_as_hash( @_ );
390 0 ▶ $opts->{clone_message} //= 1;
Condition0%0/2
$opts->{"clone_message"} //= 1
l!l
00
MC/DC0%0/1
$opts->{"clone_message"} //= 1
$opts->{"clone_message"}
Truth table0%0/2
A
A= $opts->{"clone_message"}
Aresult
11
00
391 0 my $addr = $self->_refaddr( $self );
392 0 my $new = $self->new;
393 0 my( $new_headers, $new_body, $new_parts );
394 0 my $headers = $self->headers;
395 0 my $body = $self->body;
396 0 ▶ $new_headers = $headers->clone if( defined( $headers ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $headers 0 0 0
397 0 ▶ $new_body = $body->clone if( defined( $body ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $body 0 0 0
398 0 my $parts = $self->parts;
399 0 ▶ if( !$parts->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
unless ($parts->is_empty) 0 0 0
400 {
401 0 $new_parts = $self->new_array;
402 # Each part is an HTTP::Promise::Entity
403 0 for( @$parts )
404 {
405 0 my $paddr = $self->_refaddr( $_ );
406 # This would be weird, but let's do it anyway
407 0 ▶ if( $paddr eq $addr )
Branch: 1 branch
BranchTrueFalseTotal
if ($paddr eq $addr) 0 0 0
408 {
409 0 $new_parts->push( $new );
410 0 next;
411 }
412 0 my $new_part = $_->clone;
413 0 $new_parts->push( $new_part );
414 }
415 0 $new->parts( $new_parts );
416 }
417 0 ▶ $new->headers( $new_headers ) if( defined( $new_headers ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_headers 0 0 0
418 0 ▶ $new->body( $new_body ) if( defined( $new_body ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $new_body 0 0 0
419 0 ▶ $new->name( $self->name ) if( $self->name );
Branch: 1 branch
BranchTrueFalseTotal
if $self->name 0 0 0
420 0 $new->is_encoded( $self->is_encoded );
421 0 $new->debug( $self->debug );
422 0 $new->preamble( $self->preamble->clone );
423 0 $new->epilogue( $self->epilogue->clone );
424 0 $new->compression_min( $self->compression_min );
425 0 $new->effective_type( $self->effective_type );
426 0 my $msg;
427 0 ▶ if( ( $msg = $self->http_message ) && $opts->{clone_message} )
Branch: 1 branch
BranchTrueFalseTotal
if ($msg = $self->http_message and $opts->{"clone_message"}) 0 0 0
Condition0%0/3
$msg = $self->http_message and $opts->{"clone_message"}
!ll&&!rl&&r
000
MC/DC0%0/2
$msg = $self->http_message and $opts->{"clone_message"}
$msg = $self->http_message $opts->{"clone_message"}
Truth table0%0/3
A and B
A= $msg = $self->http_messageB= $opts->{"clone_message"}
ABresult
0X0
100
111
428 {
429 # To prevent endless recursion
430 0 my $new_msg = $msg->clone( clone_entity => 0 );
431 0 $new_msg->headers( $new_headers );
432 0 $new_msg->entity( $new );
433 0 $new->http_message( $new_msg );
434 }
435 0 return( $new );
436 }
437  
438 3 ▶sub compression_min { return( shift->_set_get_number( 'compression_min', @_ ) ); }
Subroutine compression_min: called CC=1 SCAR=0.0
Pod documented
439  
440 # NOTE: an outdated method since nowadays everyone use UTF-8
441 # This is not intended to be a generic method, but instead to be used specifically for this entity
442 # content parameter can be provided to avoid reading from the body if we already have data handy.
443 sub content_charset
444 4 ▶{
Subroutine content_charset: called CC=42 SCAR=62.3
Pod documented
445 4 my $self = shift( @_ );
446 4 my $opts = $self->_get_args_as_hash( @_ );
447 4 my $headers = $self->headers;
448 # If parameter content_type_charset is set to false, this means it was just tried and
449 # we should not try it again.
450 4 ▶ if( ( my $charset = $headers->content_type_charset ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (my $charset = $headers->content_type_charset and !exists $opts->{"content_type_charset"} || $opts->{"content_type_charset"}) 0 4 4
Condition16%1/6
my $charset = $headers->content_type_charset and !exists $opts->{"content_type_charset"} || $opts->{"content_type_charset"}
!ll&&!rl&&r
400
not exists $opts->{"content_type_charset"} or $opts->{"content_type_charset"}
l!l&&r!l&&!r
000
MC/DC0%0/3
my $charset = $headers->content_type_charset and !exists $opts->{"content_type_charset"} || $opts->{"content_type_charset"}
my $charset = $headers->content_type_charset not exists $opts->{"content_type_charset"} $opts->{"content_type_charset"}
Truth table25%1/4
A and not(B) or C
A= my $charset = $headers->content_type_charsetB= not exists $opts->{"content_type_charset"}C= $opts->{"content_type_charset"}
ABCresult
0XX0
1000
11X1
1011
451 ( !exists( $opts->{content_type_charset} ) || $opts->{content_type_charset} ) )
452 {
453 0 return( $charset );
454 }
455  
456 4 ▶ $self->_load_class( 'Encode' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Encode") or return $self->pass_error 4 0 4
457 4 ▶ unless( defined( $BOM2ENC ) && scalar( %$BOM2ENC ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $HTTP::Promise::Entity::BOM2ENC and scalar %$HTTP::Promise::Entity::BOM2ENC) 1 3 4
Condition100%2/2
defined $HTTP::Promise::Entity::BOM2ENC and scalar %$HTTP::Promise::Entity::BOM2ENC
!ll
13
MC/DC100%1/1
defined $HTTP::Promise::Entity::BOM2ENC and scalar %$HTTP::Promise::Entity::BOM2ENC
defined $HTTP::Promise::Entity::BOM2ENC
Truth table100%2/2
A
A= defined $HTTP::Promise::Entity::BOM2ENC
Aresult
00
11
458 {
459 # Credits: Matthew Lawrence (File::BOM)
460 our $BOM2ENC = +{
461 1 map{ Encode::encode( $_, "\x{feff}" ) => $_ } qw(
462 UTF-8
463 UTF-16BE
464 UTF-16LE
465 UTF-32BE
466 UTF-32LE
467 )
468 };
469  
470 our $ENC2BOM = +{
471 reverse( %$BOM2ENC ),
472 1 map{ $_ => Encode::encode( $_, "\x{feff}" ) } qw(
473 UCS-2
474 iso-10646-1
475 utf8
476 )
477 };
478 1 my @boms = sort{ length( $b ) <=> length( $a ) } keys( %$BOM2ENC );
479 1 our $BOM_MAX_LENGTH = length( $boms[0] );
480 {
481 1 local $" = '|';
482 1 our $BOM_RE = qr/@boms/;
483 }
484 }
485
486 # time to start guessing
487 # If called from decoded_content, kind of pointless to call decoded_content again
488 4 my $cref;
489 4 ▶ if( exists( $opts->{content} ) && length( $opts->{content} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{'content'} and length $opts->{'content'}) { } 4 0 4
Condition33%1/3
exists $opts->{"content"} and length $opts->{"content"}
!ll&&!rl&&r
004
MC/DC0%0/2
exists $opts->{"content"} and length $opts->{"content"}
exists $opts->{"content"} length $opts->{"content"}
Truth table33%1/3
A and B
A= exists $opts->{"content"}B= length $opts->{"content"}
ABresult
0X0
100
111
490 {
491 4 ▶ return( $self->error( "Unsupported data type (", ref( $opts->{content} ), ")." ) ) if( ref( $opts->{content} ) && !$self->_is_scalar( $opts->{content} ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts->{"content"} and not $self->_is_scalar($opts->{"content"}) 0 4 4
Condition33%1/3
ref $opts->{"content"} and not $self->_is_scalar($opts->{"content"})
!ll&&!rl&&r
040
MC/DC0%0/2
ref $opts->{"content"} and not $self->_is_scalar($opts->{"content"})
ref $opts->{"content"} not $self->_is_scalar($opts->{"content"})
Truth table33%1/3
A and not(B)
A= ref $opts->{"content"}B= not $self->_is_scalar($opts->{"content"})
ABresult
0X0
100
111
492 4 ▶ $cref = $self->_is_scalar( $opts->{content} ) ? $opts->{content} : \$opts->{content};
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_scalar($opts->{'content'}) ? : 4 0 4
493 }
494 else
495 {
496 0 ▶ my $body = $self->body || return( '' );
Condition0%0/2
$self->body || (return '')
l!l
00
MC/DC0%0/1
$self->body || (return '')
$self->body
Truth table0%0/2
A
A= $self->body
Aresult
11
00
497 0 ▶ my $io = $body->open( '<', { binmode => 'raw' } ) ||
Condition0%0/2
$body->open('<', {'binmode', 'raw'}) || (return $self->pass_error($body->error))
l!l
00
MC/DC0%0/1
$body->open('<', {'binmode', 'raw'}) || (return $self->pass_error($body->error))
$body->open('<', {'binmode', 'raw'})
Truth table0%0/2
A
A= $body->open('<', {'binmode', 'raw'})
Aresult
11
00
498 return( $self->pass_error( $body->error ) );
499 0 my $buff;
500 0 my $bytes = $io->read( $buff, 4096 );
501 0 ▶ return( $self->pass_error( $io->error ) ) if( !defined( $bytes ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 0 0
502 0 ▶ return( '' ) if( !$bytes );
Branch: 1 branch
BranchTrueFalseTotal
unless $bytes 0 0 0
503 0 $cref = \$buff;
504 }
505
506 # Is there a Byte Order Mark?
507 4 ▶ if( $$cref =~ /^($BOM_RE)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($$cref =~ /^($HTTP::Promise::Entity::BOM_RE)/) 0 4 4
508 {
509 0 my $bom = $1;
510 0 return( $BOM2ENC->{ $bom } );
511 }
512  
513 # Unicode BOM
514 4 ▶ return( 'UTF-8' ) if( $$cref =~ /^\xEF\xBB\xBF/ );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^\xEF\xBB\xBF/ 0 4 4
515 4 ▶ return( 'UTF-32LE' ) if( $$cref =~ /^\xFF\xFE\x00\x00/ );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^\xFF\xFE\x00\x00/ 0 4 4
516 4 ▶ return( 'UTF-32BE' ) if( $$cref =~ /^\x00\x00\xFE\xFF/ );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^\x00\x00\xFE\xFF/ 0 4 4
517 4 ▶ return( 'UTF-16LE' ) if( $$cref =~ /^\xFF\xFE/ );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^\xFF\xFE/ 0 4 4
518 4 ▶ return( 'UTF-16BE' ) if( $$cref =~ /^\xFE\xFF/ );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^\xFE\xFF/ 0 4 4
519  
520 4 ▶ if( $headers->content_is_xml )
Branch: 3 branches
BranchTrueFalseTotal
if ($headers->content_is_xml) { } 0 4 4
elsif ($headers->content_is_text) { } 4 0 4
elsif ($headers->content_type eq 'application/json') { } 0 0 0
521 {
522 # http://www.w3.org/TR/2006/REC-xml-20060816/#sec-guessing
523 # XML entity not accompanied by external encoding information and not
524 # in UTF-8 or UTF-16 encoding must begin with an XML encoding declaration,
525 # in which the first characters must be '<?xml'
526 0 ▶ return( 'UTF-32BE' ) if( $$cref =~ /^\x00\x00\x00</ );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^\x00\x00\x00</ 0 0 0
527 0 ▶ return( 'UTF-32LE' ) if( $$cref =~ /^<\x00\x00\x00/ );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^<\x00\x00\x00/ 0 0 0
528 0 ▶ return( 'UTF-16BE' ) if( $$cref =~ /^(?:\x00\s)*\x00</ );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^(?:\x00\s)*\x00</ 0 0 0
529 0 ▶ return( 'UTF-16LE' ) if( $$cref =~ /^(?:\s\x00)*<\x00/ );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^(?:\s\x00)*<\x00/ 0 0 0
530 0 ▶ if( $$cref =~ /^[[:blank:]\h]*(<\?xml[^\x00]*?\?>)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($$cref =~ /^[[:blank:]\h]*(<\?xml[^\x00]*?\?>)/) 0 0 0
531 {
532 0 ▶ if( $1 =~ /[[:blank:]\h\v]encoding[[:blank:]\h\v]*=[[:blank:]\h\v]*(["'])(.*?)\1/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($1 =~ /[[:blank:]\h\v]encoding[[:blank:]\h\v]*=[[:blank:]\h\v]*(["'])(.*?)\1/) 0 0 0
533 {
534 0 my $enc = $2;
535 0 $enc =~ s/^[[:blank:]\h]+//;
536 0 $enc =~ s/[[:blank:]\h]+\z//;
537 0 ▶ return( $enc ) if( $enc );
Branch: 1 branch
BranchTrueFalseTotal
if $enc 0 0 0
538 }
539 }
540 0 return( 'UTF-8' );
541 }
542 elsif( $headers->content_is_text )
543 {
544 4 my $encoding = $self->guess_character_encoding( content => $cref, object => 1 );
545 4 ▶ return( ref( $encoding ) ? $encoding->mime_name : $encoding ) if( $encoding );
Branch: 2 branches
BranchTrueFalseTotal
if $encoding 0 4 4
ref $encoding ? : 0 0 0
546 }
547 elsif( $headers->content_type eq 'application/json' )
548 {
549 # RFC 4627, ch 3
550 0 ▶ return( 'UTF-32BE' ) if( $$cref =~ /^\x00\x00\x00./s );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^\x00\x00\x00./s 0 0 0
551 0 ▶ return( 'UTF-32LE' ) if( $$cref =~ /^.\x00\x00\x00/s );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^.\x00\x00\x00/s 0 0 0
552 0 ▶ return( 'UTF-16BE' ) if( $$cref =~ /^\x00.\x00./s );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^\x00.\x00./s 0 0 0
553 0 ▶ return( 'UTF-16LE' ) if( $$cref =~ /^.\x00.\x00/s );
Branch: 1 branch
BranchTrueFalseTotal
if $$cref =~ /^.\x00.\x00/s 0 0 0
554 0 return( 'UTF-8' );
555 }
556 # if( $headers->content_type =~ /^text\// && $self->_load_class( 'Encode' ) )
557 4 ▶ if( $headers->content_type =~ /^text\// )
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->content_type =~ m[^text/]) 4 0 4
558 {
559 4 ▶ if( length( $$cref ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $$cref) 0 4 4
560 {
561 0 ▶ return( 'US-ASCII' ) unless( $$cref =~ /[\x80-\xFF]/ );
Branch: 1 branch
BranchTrueFalseTotal
unless $$cref =~ /[\x80-\xFF]/ 0 0 0
562 0 my $encoding;
563 # try-catch
564 0 local $@;
565 eval
566 0 {
567 0 Encode::decode_utf8( $$cref, ( Encode::FB_CROAK | Encode::LEAVE_SRC ) );
568 0 $encoding = 'UTF-8';
569 };
570 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
571 {
572 0 return( $self->error( "Failed to decode utf8 content: $@" ) );
573 }
574 # return( 'ISO-8859-1' );
575 0 return( $encoding );
576 }
577 }
578 4 return( '' );
579 }
580  
581 sub decode_body
582 4 ▶{
Subroutine decode_body: called CC=52 SCAR=69.8
Pod documented
583 4 my $self = shift( @_ );
584 4 my $this = shift( @_ );
585 4 my $opts = $self->_get_args_as_hash( @_ );
586 4 ▶ return( $self->error( "No decoding string or array has been provided." ) ) if( !defined( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this 0 4 4
587 4 ▶ return( $self->error( "Bad argument provided. decode_body() accepts only either an array of encodings or a string or something that stringifies." ) ) if( !$self->_is_array( $this ) && ( ref( $this ) && !overload::Method( $this => '""' ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if not $self->_is_array($this) and (ref $this and not overload::Method($this, "\"\"")) 0 4 4
Condition33%2/6
not $self->_is_array($this) and (ref $this and not overload::Method($this, "\"\""))
!ll&&!rl&&r
040
ref $this and not overload::Method($this, "\"\"")
!ll&&!rl&&r
040
MC/DC0%0/3
not $self->_is_array($this) and (ref $this and not overload::Method($this, "\"\""))
not $self->_is_array($this) ref $this not overload::Method($this, "\"\"")
Truth table25%1/4
not(A) and B and not(C)
A= not $self->_is_array($this)B= ref $thisC= not overload::Method($this, "\"\"")
ABCresult
0XX0
10X0
1100
1111
588 4 ▶ my $encodings = $self->_is_array( $this ) ? $this : [split( /[[:blank:]\h]*,[[:blank:]\h]*/, "${this}" )];
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_array($this) ? : 0 4 4
589 4 ▶ $opts->{replace} //= 1;
Condition50%1/2
$opts->{"replace"} //= 1
l!l
04
MC/DC0%0/1
$opts->{"replace"} //= 1
$opts->{"replace"}
Truth table50%1/2
A
A= $opts->{"replace"}
Aresult
11
00
590 4 ▶ $opts->{raise_error} //= 0;
Condition50%1/2
$opts->{"raise_error"} //= 0
l!l
04
MC/DC0%0/1
$opts->{"raise_error"} //= 0
$opts->{"raise_error"}
Truth table50%1/2
A
A= $opts->{"raise_error"}
Aresult
11
00
591 4 ▶ $self->_load_class( 'HTTP::Promise::Stream' ) || return( $self->error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Stream") or return $self->error 4 0 4
592 4 my $body = $self->body;
593 4 ▶ warn( "No encoding were provided to decode the HTTP body.\n" ) if( !scalar( @$encodings ) && warnings::enabled( 'HTTP::Promise' ) );
Branch: 1 branch
BranchTrueFalseTotal
if not scalar @$encodings and warnings::enabled("HTTP::Promise") 0 4 4
Condition33%1/3
not scalar @$encodings and warnings::enabled("HTTP::Promise")
!ll&&!rl&&r
400
MC/DC0%0/2
not scalar @$encodings and warnings::enabled("HTTP::Promise")
not scalar @$encodings warnings::enabled("HTTP::Promise")
Truth table33%1/3
not(A) and B
A= not scalar @$encodingsB= warnings::enabled("HTTP::Promise")
ABresult
0X0
100
111
594 # Nothing to do
595 4 ▶ return( $self ) if( !$body || !scalar( @$encodings ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $body and scalar @$encodings 0 4 4
Condition33%1/3
$body and scalar @$encodings
!ll&&!rl&&r
004
MC/DC0%0/2
$body and scalar @$encodings
$body scalar @$encodings
Truth table33%1/3
A and B
A= $bodyB= scalar @$encodings
ABresult
0X0
100
111
596 # Parameters to be passed. Transparent set to 0 allow for failure
597 4 my $enc2params =
598 {
599 bzip2 => { Transparent => 0 },
600 deflate => { Transparent => 0 },
601 inflate => { Transparent => 0 },
602 gzip => { Transparent => 0 },
603 lzf => { Transparent => 0 },
604 lzip => { Transparent => 0 },
605 lzma => { Transparent => 0 },
606 lzop => { Transparent => 0 },
607 rawdeflate => { Transparent => 0 },
608 rawinflate => { Transparent => 0 },
609 xz => { Transparent => 0 },
610 zstd => { Transparent => 0 },
611 };
612
613 4 ▶ if( $body->isa( 'HTTP::Promise::Body::File' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($body->isa('HTTP::Promise::Body::File')) { } 0 4 4
elsif ($body->isa('HTTP::Promise::Body::Scalar')) { } 4 0 4
614 {
615 0 my $f = $body;
616 0 ▶ if( $f->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
if ($f->is_empty) 0 0 0
617 {
618 0 ▶ warn( "HTTP Body file '$f' is empty, so there is nothing to decode\n" ) if( warnings::enabled( 'HTTP::Promise' ) );
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled("HTTP::Promise") 0 0 0
619 0 return( $self );
620 }
621 0 my $ext = $f->extension;
622 0 my $ext_vary = $self->ext_vary;
623 0 my $ext_parts;
624 0 ▶ if( $ext_vary )
Branch: 1 branch
BranchTrueFalseTotal
if ($ext_vary) 0 0 0
625 {
626 0 $ext_parts = $f->extensions;
627 }
628
629 0 foreach my $enc ( @$encodings )
630 {
631 0 ▶ next if( $enc eq 'identity' || $enc eq 'none' );
Branch: 1 branch
BranchTrueFalseTotal
if $enc eq "identity" or $enc eq "none" 0 0 0
Condition0%0/3
$enc eq "identity" or $enc eq "none"
l!l&&r!l&&!r
000
MC/DC0%0/2
$enc eq "identity" or $enc eq "none"
$enc eq "identity" $enc eq "none"
Truth table0%0/3
A or B
A= $enc eq "identity"B= $enc eq "none"
ABresult
1X1
011
000
632 0 my $params = {};
633 0 ▶ $params = $enc2params->{ $enc } if( exists( $enc2params->{ $enc } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $enc2params->{$enc} 0 0 0
634 my $s = HTTP::Promise::Stream->new( $f,
635 decoding => $enc,
636 fatal => $opts->{raise_error},
637 0 ▶ debug => $self->debug,
Condition0%0/2
'HTTP::Promise::Stream'->new($f, 'decoding', $enc, 'fatal', $opts->{'raise_error'}, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::Stream'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Stream'->new($f, 'decoding', $enc, 'fatal', $opts->{'raise_error'}, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::Stream'->error))
'HTTP::Promise::Stream'->new($f, 'decoding', $enc, 'fatal', $opts->{'raise_error'}, 'debug', $self->debug)
Truth table0%0/2
A
A= 'HTTP::Promise::Stream'->new($f, 'decoding', $enc, 'fatal', $opts->{'raise_error'}, 'debug', $self->debug)
Aresult
11
00
638 ) || return( $self->pass_error( HTTP::Promise::Stream->error ) );
639 0 my $ext_deb = $s->encoding2suffix( $enc )->first;
640 0 my $ext_enc;
641 0 ▶ if( $ext_vary &&
Branch: 1 branch
BranchTrueFalseTotal
if ($ext_vary and $ext_enc = $s->encoding2suffix($enc)->first and $ext_parts->[-1] eq $ext_enc) 0 0 0
Condition0%0/6
$ext_vary and $ext_enc = $s->encoding2suffix($enc)->first and $ext_parts->[-1] eq $ext_enc
!ll&&!rl&&r
000
$ext_vary and $ext_enc = $s->encoding2suffix($enc)->first
!ll&&!rl&&r
000
MC/DC0%0/3
$ext_vary and $ext_enc = $s->encoding2suffix($enc)->first and $ext_parts->[-1] eq $ext_enc
$ext_vary $ext_enc = $s->encoding2suffix($enc)->first $ext_parts->[-1] eq $ext_enc
Truth table0%0/4
A and B and C
A= $ext_varyB= $ext_enc = $s->encoding2suffix($enc)->firstC= $ext_parts->[-1] eq $ext_enc
ABCresult
0XX0
10X0
1100
1111
642 ( $ext_enc = $s->encoding2suffix( $enc )->first ) &&
643 $ext_parts->[-1] eq $ext_enc )
644 {
645 0 pop( @$ext_parts );
646 0 $ext = join( '.', @$ext_parts );
647 }
648 0 my $tempfile = $self->new_tempfile( extension => $ext );
649 # my $len = $s->read( $tempfile, ( exists( $params->{ $enc } ) ? %{$params->{ $enc }} : () ) );
650 0 my $len = $s->read( $tempfile, $params );
651 0 ▶ if( !defined( $len ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $len) 0 0 0
652 {
653 0 ▶ if( $enc eq 'deflate' || $enc eq 'inflate' )
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq 'deflate' or $enc eq 'inflate') { } 0 0 0
Condition0%0/3
$enc eq "deflate" or $enc eq "inflate"
l!l&&r!l&&!r
000
MC/DC0%0/2
$enc eq "deflate" or $enc eq "inflate"
$enc eq "deflate" $enc eq "inflate"
Truth table0%0/3
A or B
A= $enc eq "deflate"B= $enc eq "inflate"
ABresult
1X1
011
000
654 {
655 # Try again, but using rawinflate this time
656 0 ▶ if( $s->error->message =~ /Header Error: CRC mismatch/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($s->error->message =~ /Header Error: CRC mismatch/) { } 0 0 0
657 {
658 0 $enc = "raw${enc}";
659 0 $params = {};
660 0 ▶ $params = $enc2params->{ $enc } if( exists( $enc2params->{ $enc } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $enc2params->{$enc} 0 0 0
661 my $s = HTTP::Promise::Stream->new( $f,
662 decoding => $enc,
663 fatal => $opts->{raise_error}
664 0 ▶ ) || return( $self->pass_error( HTTP::Promise::Stream->error ) );
Condition0%0/2
'HTTP::Promise::Stream'->new($f, 'decoding', $enc, 'fatal', $opts->{'raise_error'}) || (return $self->pass_error('HTTP::Promise::Stream'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Stream'->new($f, 'decoding', $enc, 'fatal', $opts->{'raise_error'}) || (return $self->pass_error('HTTP::Promise::Stream'->error))
'HTTP::Promise::Stream'->new($f, 'decoding', $enc, 'fatal', $opts->{'raise_error'})
Truth table0%0/2
A
A= 'HTTP::Promise::Stream'->new($f, 'decoding', $enc, 'fatal', $opts->{'raise_error'})
Aresult
11
00
665 # $len = $s->read( $tempfile, ( exists( $params->{ $enc } ) ? ( $params->{ $enc } ) : () ) );
666 0 $len = $s->read( $tempfile, $params );
667 0 ▶ return( $self->pass_error( $s->error ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
668 }
669 else
670 {
671 0 return( $self->pass_error( $s->error ) )
672 }
673 }
674 else
675 {
676 0 return( $self->pass_error( $s->error ) );
677 }
678 }
679 0 ▶ return( $self->error( "The decoding pass on the HTTP body file source '$f' to target '$tempfile' with encoding '$enc' resulted in 0 byte decoded!" ) ) if( !$len );
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 0 0
680 0 $f = $tempfile;
681 }
682 0 ▶ $body = HTTP::Promise::Body::File->new( $f ) ||
Condition0%0/2
'HTTP::Promise::Body::File'->new($f) || (return $self->pass_error('HTTP::Promise::Body::File'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Body::File'->new($f) || (return $self->pass_error('HTTP::Promise::Body::File'->error))
'HTTP::Promise::Body::File'->new($f)
Truth table0%0/2
A
A= 'HTTP::Promise::Body::File'->new($f)
Aresult
11
00
683 return( $self->pass_error( HTTP::Promise::Body::File->error ) );
684 0 ▶ if( $opts->{replace} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"replace"}) 0 0 0
685 {
686 0 $self->body( $body );
687 0 $self->is_decoded(1);
688 }
689 }
690 elsif( $body->isa( 'HTTP::Promise::Body::Scalar' ) )
691 {
692 4 my $temp = $body;
693 4 ▶ if( $body->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
if ($body->is_empty) 0 4 4
694 {
695 0 ▶ warn( "HTTP Body in memory is empty, so there is nothing to decode\n" ) if( warnings::enabled( 'HTTP::Promise' ) );
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled("HTTP::Promise") 0 0 0
696 0 return( $self );
697 }
698
699 4 foreach my $enc ( @$encodings )
700 {
701 4 ▶ next if( $enc eq 'identity' || $enc eq 'none' );
Branch: 1 branch
BranchTrueFalseTotal
if $enc eq "identity" or $enc eq "none" 0 4 4
Condition33%1/3
$enc eq "identity" or $enc eq "none"
l!l&&r!l&&!r
004
MC/DC0%0/2
$enc eq "identity" or $enc eq "none"
$enc eq "identity" $enc eq "none"
Truth table33%1/3
A or B
A= $enc eq "identity"B= $enc eq "none"
ABresult
1X1
011
000
702 4 my $params = {};
703 4 ▶ $params = $enc2params->{ $enc } if( exists( $enc2params->{ $enc } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $enc2params->{$enc} 0 4 4
704 my $s = HTTP::Promise::Stream->new( $temp,
705 decoding => $enc,
706 fatal => $opts->{raise_error},
707 4 ▶ debug => $self->debug
Condition50%1/2
'HTTP::Promise::Stream'->new($temp, 'decoding', $enc, 'fatal', $opts->{'raise_error'}, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::Stream'->error))
l!l
40
MC/DC0%0/1
'HTTP::Promise::Stream'->new($temp, 'decoding', $enc, 'fatal', $opts->{'raise_error'}, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::Stream'->error))
'HTTP::Promise::Stream'->new($temp, 'decoding', $enc, 'fatal', $opts->{'raise_error'}, 'debug', $self->debug)
Truth table50%1/2
A
A= 'HTTP::Promise::Stream'->new($temp, 'decoding', $enc, 'fatal', $opts->{'raise_error'}, 'debug', $self->debug)
Aresult
11
00
708 ) || return( $self->pass_error( HTTP::Promise::Stream->error ) );
709 4 my $decoded = $self->new_scalar;
710 # my $len = $s->read( $decoded, ( exists( $params->{ $enc } ) ? ( $params->{ $enc } ) : () ) );
711 4 my $len = $s->read( $decoded, $params );
712 # my $len = $s->read( $decoded );
713 # return( $self->pass_error( $s->error ) ) if( !defined( $len ) );
714 4 ▶ if( !defined( $len ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $len) 0 4 4
715 {
716 0 ▶ if( $enc eq 'deflate' || $enc eq 'inflate' )
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq 'deflate' or $enc eq 'inflate') { } 0 0 0
Condition0%0/3
$enc eq "deflate" or $enc eq "inflate"
l!l&&r!l&&!r
000
MC/DC0%0/2
$enc eq "deflate" or $enc eq "inflate"
$enc eq "deflate" $enc eq "inflate"
Truth table0%0/3
A or B
A= $enc eq "deflate"B= $enc eq "inflate"
ABresult
1X1
011
000
717 {
718 # Try again, but using rawinflate this time
719 0 ▶ if( $s->error->message =~ /Header Error: CRC mismatch/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($s->error->message =~ /Header Error: CRC mismatch/) { } 0 0 0
720 {
721 0 $enc = "raw${enc}";
722 0 $params = {};
723 0 ▶ $params = $enc2params->{ $enc } if( exists( $enc2params->{ $enc } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $enc2params->{$enc} 0 0 0
724 my $s = HTTP::Promise::Stream->new( $temp,
725 decoding => $enc,
726 fatal => $opts->{raise_error},
727 0 ▶ debug => $self->debug
Condition0%0/2
'HTTP::Promise::Stream'->new($temp, 'decoding', $enc, 'fatal', $opts->{'raise_error'}, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::Stream'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Stream'->new($temp, 'decoding', $enc, 'fatal', $opts->{'raise_error'}, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::Stream'->error))
'HTTP::Promise::Stream'->new($temp, 'decoding', $enc, 'fatal', $opts->{'raise_error'}, 'debug', $self->debug)
Truth table0%0/2
A
A= 'HTTP::Promise::Stream'->new($temp, 'decoding', $enc, 'fatal', $opts->{'raise_error'}, 'debug', $self->debug)
Aresult
11
00
728 ) || return( $self->pass_error( HTTP::Promise::Stream->error ) );
729 # $len = $s->read( $decoded, ( exists( $params->{ $enc } ) ? $params->{ $enc } : () ) );
730 0 $len = $s->read( $decoded, $params );
731 0 ▶ return( $self->pass_error( $s->error ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
732 }
733 else
734 {
735 0 return( $self->pass_error( $s->error ) )
736 }
737 }
738 else
739 {
740 0 return( $self->pass_error( $s->error ) );
741 }
742 }
743 4 ▶ return( $self->error( "The decoding pass on the HTTP body in memory with encoding '$enc' resulted in 0 byte decoded!" ) ) if( !$len );
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 4 4
744 4 $temp = $decoded;
745 }
746 # Replace content (default)
747 4 ▶ if( $opts->{replace} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'replace'}) { } 4 0 4
748 {
749 4 $body->set( $temp );
750 4 $self->body( $body );
751 4 $self->is_decoded(1);
752 }
753 # Make a copy to return it
754 else
755 {
756 0 $body = $body->new( $temp );
757 }
758 }
759 else
760 {
761 0 return( $self->error( "I do not know how to handle HTTP body object of class ", ref( $body ) ) );
762 }
763 4 return( $body );
764 }
765  
766 sub dump
767 0 ▶{
Subroutine dump: not called CC=40 SCAR=74.0
Pod documented
768 0 my $self = shift( @_ );
769 0 my $opts = $self->_get_args_as_hash( @_ );
770 0 my $content = '';
771 0 my $maxlen = $opts->{maxlength};
772 0 ▶ $maxlen = 512 unless( defined( $maxlen ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxlen 0 0 0
773 0 my $no_content = $opts->{no_content};
774 0 ▶ $no_content = "(no content)" unless( defined( $no_content ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $no_content 0 0 0
775 0 my $body = $self->body;
776 0 my $chopped = 0;
777 0 my $mime_type = $self->mime_type;
778 0 my $toptype;
779 0 ▶ $toptype = [split( '/', lc( $mime_type ), 2 )]->[0] if( defined( $mime_type ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $mime_type 0 0 0
780 0 ▶ my $crlf = $HTTP::Promise::Entity::BOUNDARY_DELIMITER || CRLF;
Condition0%0/2
$HTTP::Promise::Entity::BOUNDARY_DELIMITER || "\r\n"
l!l
00
MC/DC0%0/1
$HTTP::Promise::Entity::BOUNDARY_DELIMITER || "\r\n"
$HTTP::Promise::Entity::BOUNDARY_DELIMITER
Truth table0%0/2
A
A= $HTTP::Promise::Entity::BOUNDARY_DELIMITER
Aresult
11
00
781  
782 0 ▶ if( defined( $body ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $body) { } 0 0 0
elsif (not $self->part->is_empty) { } 0 0 0
783 {
784 0 ▶ my $io = $body->open( '<', { binmode => 'raw' } ) ||
Condition0%0/2
$body->open('<', {'binmode', 'raw'}) || (return $self->pass_error($body->error))
l!l
00
MC/DC0%0/1
$body->open('<', {'binmode', 'raw'}) || (return $self->pass_error($body->error))
$body->open('<', {'binmode', 'raw'})
Truth table0%0/2
A
A= $body->open('<', {'binmode', 'raw'})
Aresult
11
00
785 return( $self->pass_error( $body->error ) );
786 0 ▶ my $bytes = $io->read( $content, ( $maxlen || $body->length ) );
Condition0%0/3
$maxlen || $body->length
l!l&&r!l&&!r
000
MC/DC0%0/2
$maxlen || $body->length
$maxlen $body->length
Truth table0%0/3
A || B
A= $maxlenB= $body->length
ABresult
1X1
011
000
787 0 ▶ return( $self->pass_error( $io->error ) ) if( !defined( $bytes ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 0 0
788 0 $io->close;
789 0 my $encoding = $self->headers->mime_encoding;
790 0 my $encodings = [];
791 0 ▶ $encodings = [split( /[[:blank:]\h]*,[[:blank:]\h]*/, $encoding )] if( defined( $encoding ) && length( $encoding ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $encoding and length $encoding 0 0 0
Condition0%0/3
defined $encoding and length $encoding
!ll&&!rl&&r
000
MC/DC0%0/2
defined $encoding and length $encoding
defined $encoding length $encoding
Truth table0%0/3
A and B
A= defined $encodingB= length $encoding
ABresult
0X0
100
111
792 0 ▶ $self->_load_class( 'HTTP::Promise::Stream' ) || return( $self->error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Stream") or return $self->error 0 0 0
793 # Process encoding
794 0 ▶ if( scalar( @$encodings ) && !$self->is_encoded )
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$encodings and not $self->is_encoded) 0 0 0
Condition0%0/3
scalar @$encodings and not $self->is_encoded
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @$encodings and not $self->is_encoded
scalar @$encodings not $self->is_encoded
Truth table0%0/3
A and not(B)
A= scalar @$encodingsB= not $self->is_encoded
ABresult
0X0
100
111
795 {
796 0 my $temp = $content;
797 0 my $has_error = 0;
798 0 foreach my $enc ( @$encodings )
799 {
800 0 ▶ my $s = HTTP::Promise::Stream->new( $temp, encoding => $enc ) ||
Condition0%0/2
'HTTP::Promise::Stream'->new($temp, 'encoding', $enc) || (return $self->pass_error('HTTP::Promise::Stream'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Stream'->new($temp, 'encoding', $enc) || (return $self->pass_error('HTTP::Promise::Stream'->error))
'HTTP::Promise::Stream'->new($temp, 'encoding', $enc)
Truth table0%0/2
A
A= 'HTTP::Promise::Stream'->new($temp, 'encoding', $enc)
Aresult
11
00
801 return( $self->pass_error( HTTP::Promise::Stream->error ) );
802 0 my $encoded = $self->new_scalar;
803 0 my $len = $s->read( $encoded );
804 0 ▶ return( $self->pass_error( $s->error ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
805 0 ▶ if( !$len )
Branch: 1 branch
BranchTrueFalseTotal
unless ($len) 0 0 0
806 {
807 0 warn( "The encoding pass on the HTTP body in memory with encoding '$enc' resulted in 0 byte encoded!\n" );
808 0 $has_error++;
809 0 last;
810 }
811 0 $temp = $encoded;
812 }
813 0 ▶ $content = $temp unless( $has_error );
Branch: 1 branch
BranchTrueFalseTotal
unless $has_error 0 0 0
814 }
815
816 0 ▶ if( length( $content ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $content) { } 0 0 0
817 {
818 0 ▶ if( $self->is_binary( \$content ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_binary(\$content)) { } 0 0 0
819 {
820 0 $content = '(content is ' . length( $content ) . ' bytes of binary data)';
821 }
822 else
823 {
824 0 ▶ if( $maxlen && $body->length > $maxlen )
Branch: 1 branch
BranchTrueFalseTotal
if ($maxlen and $body->length > $maxlen) 0 0 0
Condition0%0/3
$maxlen and $body->length > $maxlen
!ll&&!rl&&r
000
MC/DC0%0/2
$maxlen and $body->length > $maxlen
$maxlen $body->length > $maxlen
Truth table0%0/3
A and B
A= $maxlenB= $body->length > $maxlen
ABresult
0X0
100
111
825 {
826 0 $content .= '...';
827 0 $chopped = $body->length - $maxlen;
828 }
829 0 $content =~ s/\\/\\\\/g;
830 0 $content =~ s/\t/\\t/g;
831 0 $content =~ s/\r/\\r/g;
832  
833 # no need for 3 digits in escape for these
834 0 $content =~ s/([\0-\11\13-\037])(?!\d)/sprintf('\\%o',ord($1))/eg;
835  
836 0 $content =~ s/([\0-\11\13-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg;
837 0 $content =~ s/([^\12\040-\176])/sprintf('\\x{%X}',ord($1))/eg;
838  
839 # remaining whitespace
840 0 $content =~ s/( +)\n/("\\40" x length($1)) . "\n"/eg;
841 0 $content =~ s/(\n+)\n/("\\n" x length($1)) . "\n"/eg;
842 0 $content =~ s/\n\z/\\n/;
843 0 ▶ if( $content eq $no_content )
Branch: 1 branch
BranchTrueFalseTotal
if ($content eq $no_content) 0 0 0
844 {
845 # escape our $no_content marker
846 0 $content =~ s/^(.)/sprintf('\\x%02X',ord($1))/eg;
847 }
848 }
849 }
850 else
851 {
852 0 $content = $no_content;
853 }
854 0 ▶ $content .= "\n(+ $chopped more bytes not shown)" if( $chopped );
Branch: 1 branch
BranchTrueFalseTotal
if $chopped 0 0 0
855 }
856 elsif( !$self->part->is_empty )
857 {
858 0 my $boundary = $self->_prepare_multipart_headers;
859 # Multipart... form-data or mixed
860 0 ▶ if( defined( $toptype ) && $toptype eq 'multipart' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $toptype and $toptype eq 'multipart') { } 0 0 0
Condition0%0/3
defined $toptype and $toptype eq "multipart"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $toptype and $toptype eq "multipart"
defined $toptype $toptype eq "multipart"
Truth table0%0/3
A and B
A= defined $toptypeB= $toptype eq "multipart"
ABresult
0X0
100
111
861 {
862 0 my $boundary = $self->_prepare_multipart_headers();
863  
864 # Preamble. I do not think there should be any anyway for HTTP multipart
865 0 my $plines = $self->preamble;
866 0 ▶ if( defined( $plines ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $plines) 0 0 0
867 {
868 # Defined, so output the preamble if it exists (avoiding additional
869 # newline as per ticket 60931)
870 0 ▶ $content .= join( '', @$plines ) . $crlf if( @$plines > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if @$plines > 0 0 0 0
871 }
872 # Otherwise, no preamble.
873  
874 # Parts
875 0 foreach my $part ( $self->parts->list )
876 {
877 0 $content .= "--${boundary}${crlf}";
878 0 $content .= $part->dump( $opts );
879 # Trailing CRLF
880 0 $content .= $crlf;
881 }
882 0 $content .= "--${boundary}--${crlf}";
883  
884 # Epilogue
885 0 my $epilogue = $self->epilogue;
886 0 ▶ if( defined( $epilogue ) && !$epilogue->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $epilogue and not $epilogue->is_empty) 0 0 0
Condition0%0/3
defined $epilogue and not $epilogue->is_empty
!ll&&!rl&&r
000
MC/DC0%0/2
defined $epilogue and not $epilogue->is_empty
defined $epilogue not $epilogue->is_empty
Truth table0%0/3
A and not(B)
A= defined $epilogueB= not $epilogue->is_empty
ABresult
0X0
100
111
887 {
888 0 $content .= $epilogue->join( '' )->scalar;
889 0 ▶ if( $epilogue !~ /(?:\015?\012)\Z/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($epilogue =~ /(?:\015?\012)\Z/) 0 0 0
890 {
891 0 $content .= $crlf;
892 }
893 }
894 }
895 # Singlepart type with parts...
896 # This makes $ent->print handle message/rfc822 bodies
897 # when parse_nested_messages('NEST') is on [idea by Marc Rouleau].
898 else
899 {
900 0 my $need_sep = 0;
901 0 my $part;
902 0 foreach $part ( $self->parts->list )
903 {
904 0 ▶ if( $need_sep++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($need_sep++) 0 0 0
905 {
906 0 $content .= "${crlf}${crlf}";
907 }
908 0 $content .= $part->dump( $opts );
909 }
910 }
911 }
912  
913 0 my @dump;
914 0 ▶ push( @dump, $opts->{preheader} ) if( $opts->{preheader} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"preheader"} 0 0 0
915 0 my $start_line;
916 0 ▶ if( $self->http_message && ( $start_line = $self->http_message->start_line ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->http_message and $start_line = $self->http_message->start_line) 0 0 0
Condition0%0/3
$self->http_message and $start_line = $self->http_message->start_line
!ll&&!rl&&r
000
MC/DC0%0/2
$self->http_message and $start_line = $self->http_message->start_line
$self->http_message $start_line = $self->http_message->start_line
Truth table0%0/3
A and B
A= $self->http_messageB= $start_line = $self->http_message->start_line
ABresult
0X0
100
111
917 {
918 0 push( @dump, $start_line );
919 }
920 0 push( @dump, $self->headers->as_string, $content );
921  
922 0 my $dump = join( "\n", @dump, '' );
923 0 ▶ $dump =~ s/^/$opts->{prefix}/gm if( $opts->{prefix} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"prefix"} 0 0 0
924 0 return( $dump );
925 }
926  
927 sub dump_skeleton
928 0 ▶{
Subroutine dump_skeleton: not called CC=17 SCAR=55.9
Pod documented
929 0 my $self = shift( @_ );
930 0 my( $fh, $indent ) = @_;
931 0 ▶ $fh = select if( !$fh );
Branch: 1 branch
BranchTrueFalseTotal
unless $fh 0 0 0
932 0 ▶ $indent = 0 if( !defined( $indent ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $indent 0 0 0
933 0 my $ind = ' ' x $indent;
934 0 my $part;
935 10 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
936 0 my $crlf = CRLF;
937 0 my @first_line = ();
938 0 ▶ if( my $msg = $self->http_message )
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $self->http_message) 0 0 0
939 {
940 0 ▶ if( $msg->isa( 'HTTP::Promise::Request' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($msg->isa('HTTP::Promise::Request')) { } 0 0 0
941 {
942 0 push( @first_line, $msg->method, $msg->uri, $msg->protocol );
943 }
944 else
945 {
946 0 push( @first_line, $msg->protocol, $msg->code, $msg->status );
947 }
948 0 ▶ print( $fh join( ' ', @first_line ), $crlf ) if( @first_line );
Branch: 1 branch
BranchTrueFalseTotal
if @first_line 0 0 0
949 }
950 0 my $headers = $self->headers;
951 0 ▶ print( $fh $headers->as_string, $crlf ) || return( $self->error( $! ) );
Branch: 1 branch
BranchTrueFalseTotal
print $fh $headers->as_string, $crlf or return $self->error($!) 0 0 0
952 0 my $body = $self->body;
953 0 ▶ if( $body )
Branch: 1 branch
BranchTrueFalseTotal
if ($body) 0 0 0
954 {
955 0 ▶ if( $body->isa( 'HTTP::Promise::Body::File' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($body->isa('HTTP::Promise::Body::File')) { } 0 0 0
elsif ($body->isa('HTTP::Promise::Body::Form')) { } 0 0 0
956 {
957 0 ▶ print( $fh "${ind}Body is stored in a temporary file at '", $body->filename, "' and is ", $body->length, " bytes big.${crlf}" ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh "${ind}Body is stored in a temporary file at '", $body->filename, "' and is ", $body->length, " bytes big.$crlf" or return $self->error($!) 0 0 0
958 return( $self->error( $! ) );
959 }
960 elsif( $body->isa( 'HTTP::Promise::Body::Form' ) )
961 {
962 0 ▶ print( $fh "${ind}Body is a x-www-form-urlencoded data with ", $body->length, " elements:\n", $body->dump ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh "${ind}Body is a x-www-form-urlencoded data with ", $body->length, " elements:\n", $body->dump or return $self->error($!) 0 0 0
963 return( $self->error( $! ) );
964 }
965 else
966 {
967 0 ▶ print( $fh "${ind}Body is stored in memory and is ", $body->length, " bytes big.${crlf}" ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $fh "${ind}Body is stored in memory and is ", $body->length, " bytes big.$crlf" or return $self->error($!) 0 0 0
968 return( $self->error( $! ) );
969 }
970 }
971 0 ▶ if( my $cd = $headers->content_disposition )
Branch: 1 branch
BranchTrueFalseTotal
if (my $cd = $headers->content_disposition) 0 0 0
972 {
973 0 ▶ print( $fh "${ind}Body is encoded using $cd\n" ) || return( $self->error( $! ) );
Branch: 1 branch
BranchTrueFalseTotal
print $fh "${ind}Body is encoded using $cd\n" or return $self->error($!) 0 0 0
974 }
975 0 my $filename = $self->headers->recommended_filename;
976 0 ▶ print( $fh $ind, "${ind}Recommended filename is: '${filename}'$crlf" ) if( $filename );
Branch: 1 branch
BranchTrueFalseTotal
if $filename 0 0 0
977  
978 # The parts
979 0 my $parts = $self->parts;
980 0 printf( $fh "${ind}This HTTP message has %d parts.${crlf}", $parts->length );
981 0 print( $fh $ind, "--\n" );
982 0 foreach $part ( @$parts )
983 {
984 0 $part->dump_skeleton( $fh, $indent + 1 );
985 }
986 0 return( $self );
987 }
988  
989 sub effective_type
990 3 ▶{
Subroutine effective_type: called CC=3 SCAR=13.3
Pod documented
991 3 my $self = shift( @_ );
992 3 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 3 3
993 {
994 0 $self->_set_get_scalar_as_object( 'effective_type', @_ );
995 }
996 3 ▶ return( $self->_set_get_scalar_as_object( 'effective_type' ) || $self->mime_type );
Condition33%1/3
$self->_set_get_scalar_as_object('effective_type') || $self->mime_type
l!l&&r!l&&!r
030
MC/DC0%0/2
$self->_set_get_scalar_as_object('effective_type') || $self->mime_type
$self->_set_get_scalar_as_object('effective_type') $self->mime_type
Truth table33%1/3
A || B
A= $self->_set_get_scalar_as_object('effective_type')B= $self->mime_type
ABresult
1X1
011
000
997 }
998  
999 sub encode_body
1000 6 ▶{
Subroutine encode_body: called CC=34 SCAR=47.7
Pod documented
1001 6 my $self = shift( @_ );
1002 6 my $this = shift( @_ );
1003 6 ▶ return( $self->error( "Bad argument provided. encode_body() accepts only either an array of encodings or a string or something that stringifies." ) ) if( !defined( $this ) || ( !$self->_is_array( $this ) && ( ref( $this ) && !overload::Method( $this => '""' ) ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $this or not $self->_is_array($this) and (ref $this and not overload::Method($this, "\"\"")) 0 6 6
Condition33%3/9
not defined $this or not $self->_is_array($this) and (ref $this and not overload::Method($this, "\"\""))
l!l&&r!l&&!r
006
not $self->_is_array($this) and (ref $this and not overload::Method($this, "\"\""))
!ll&&!rl&&r
060
ref $this and not overload::Method($this, "\"\"")
!ll&&!rl&&r
060
MC/DC0%0/4
not defined $this or not $self->_is_array($this) and (ref $this and not overload::Method($this, "\"\""))
not defined $this not $self->_is_array($this) ref $this not overload::Method($this, "\"\"")
Truth table20%1/5
not(A) or not(B) and C and not(D)
A= not defined $thisB= not $self->_is_array($this)C= ref $thisD= not overload::Method($this, "\"\"")
ABCDresult
1XXX1
01111
00XX0
010X0
01100
1004 6 ▶ my $encodings = $self->new_array( $self->_is_array( $this ) ? $this : [split( /[[:blank:]\h]*,[[:blank:]\h]*/, "${this}" )] );
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_array($this) ? : 0 6 6
1005 6 ▶ $self->_load_class( 'HTTP::Promise::Stream' ) || return( $self->error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Stream") or return $self->error 6 0 6
1006 6 my $body = $self->body;
1007 6 ▶ warn( "No encodings were provided to encode the HTTP body.\n" ) if( !scalar( @$encodings ) && warnings::enabled( 'HTTP::Promise' ) );
Branch: 1 branch
BranchTrueFalseTotal
if not scalar @$encodings and warnings::enabled("HTTP::Promise") 0 6 6
Condition33%1/3
not scalar @$encodings and warnings::enabled("HTTP::Promise")
!ll&&!rl&&r
600
MC/DC0%0/2
not scalar @$encodings and warnings::enabled("HTTP::Promise")
not scalar @$encodings warnings::enabled("HTTP::Promise")
Truth table33%1/3
not(A) and B
A= not scalar @$encodingsB= warnings::enabled("HTTP::Promise")
ABresult
0X0
100
111
1008 # Nothing to do
1009 6 ▶ return( $self ) if( !$body );
Branch: 1 branch
BranchTrueFalseTotal
unless $body 0 6 6
1010 6 my $seen = {};
1011 6 ▶ if( $body->isa( 'HTTP::Promise::Body::File' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($body->isa('HTTP::Promise::Body::File')) { } 1 5 6
elsif ($body->isa('HTTP::Promise::Body::Scalar')) { } 5 0 5
1012 {
1013 1 my $f = $body;
1014 1 ▶ if( $f->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
if ($f->is_empty) 0 1 1
1015 {
1016 0 ▶ warn( "HTTP Body file '$f' is empty, so there is nothing to encode\n" ) if( warnings::enabled( 'HTTP::Promise' ) );
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled("HTTP::Promise") 0 0 0
1017 0 return( $self );
1018 }
1019 1 my $ext = $f->extension;
1020 1 foreach my $enc ( @$encodings )
1021 {
1022 2 ▶ next if( $enc eq 'identity' || $enc eq 'none' );
Branch: 1 branch
BranchTrueFalseTotal
if $enc eq "identity" or $enc eq "none" 0 2 2
Condition33%1/3
$enc eq "identity" or $enc eq "none"
l!l&&r!l&&!r
002
MC/DC0%0/2
$enc eq "identity" or $enc eq "none"
$enc eq "identity" $enc eq "none"
Truth table33%1/3
A or B
A= $enc eq "identity"B= $enc eq "none"
ABresult
1X1
011
000
1023 2 ▶ next if( ++$seen->{ $enc } > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if ++$seen->{$enc} > 1 0 2 2
1024 2 ▶ my $s = HTTP::Promise::Stream->new( $f, encoding => $enc ) ||
Condition50%1/2
'HTTP::Promise::Stream'->new($f, 'encoding', $enc) || (return $self->pass_error('HTTP::Promise::Stream'->error))
l!l
20
MC/DC0%0/1
'HTTP::Promise::Stream'->new($f, 'encoding', $enc) || (return $self->pass_error('HTTP::Promise::Stream'->error))
'HTTP::Promise::Stream'->new($f, 'encoding', $enc)
Truth table50%1/2
A
A= 'HTTP::Promise::Stream'->new($f, 'encoding', $enc)
Aresult
11
00
1025 return( $self->pass_error( HTTP::Promise::Stream->error ) );
1026 2 ▶ if( $self->ext_vary )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ext_vary) 0 2 2
1027 {
1028 0 ▶ my $enc_ext = HTTP::Promise::Stream->encoding2suffix( $enc ) ||
Condition0%0/2
'HTTP::Promise::Stream'->encoding2suffix($enc) || (return $self->pass_error('HTTP::Promise::Stream'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Stream'->encoding2suffix($enc) || (return $self->pass_error('HTTP::Promise::Stream'->error))
'HTTP::Promise::Stream'->encoding2suffix($enc)
Truth table0%0/2
A
A= 'HTTP::Promise::Stream'->encoding2suffix($enc)
Aresult
11
00
1029 return( $self->pass_error( HTTP::Promise::Stream->error ) );
1030 0 ▶ if( !$enc_ext->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
unless ($enc_ext->is_empty) 0 0 0
1031 {
1032 0 $ext .= '.' . $enc_ext->join( '.' )->scalar;
1033 }
1034 }
1035 2 my $tempfile = $self->new_tempfile( extension => $ext );
1036 2 my $len = $s->read( $tempfile );
1037 2 ▶ return( $self->pass_error( $s->error ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 2 2
1038 2 ▶ return( $self->error( "The encoding pass on the HTTP body file source '$f' to target '$tempfile' with encoding '$enc' resulted in 0 byte encoded!" ) ) if( !$len );
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 2 2
1039 2 $f = $tempfile;
1040 }
1041 1 ▶ $body = HTTP::Promise::Body::File->new( $f ) ||
Condition50%1/2
'HTTP::Promise::Body::File'->new($f) || (return $self->pass_error('HTTP::Promise::Body::File'->error))
l!l
10
MC/DC0%0/1
'HTTP::Promise::Body::File'->new($f) || (return $self->pass_error('HTTP::Promise::Body::File'->error))
'HTTP::Promise::Body::File'->new($f)
Truth table50%1/2
A
A= 'HTTP::Promise::Body::File'->new($f)
Aresult
11
00
1042 return( $self->pass_error( HTTP::Promise::Body::File->error ) );
1043 1 $self->body( $body );
1044 }
1045 elsif( $body->isa( 'HTTP::Promise::Body::Scalar' ) )
1046 {
1047 5 my $temp = $body;
1048 5 ▶ if( $body->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
if ($body->is_empty) 0 5 5
1049 {
1050 0 ▶ warn( "HTTP Body in memory is empty, so there is nothing to encode\n" ) if( warnings::enabled( 'HTTP::Promise' ) );
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled("HTTP::Promise") 0 0 0
1051 0 return( $self );
1052 }
1053
1054 5 foreach my $enc ( @$encodings )
1055 {
1056 5 ▶ next if( $enc eq 'identity' || $enc eq 'none' );
Branch: 1 branch
BranchTrueFalseTotal
if $enc eq "identity" or $enc eq "none" 0 5 5
Condition33%1/3
$enc eq "identity" or $enc eq "none"
l!l&&r!l&&!r
005
MC/DC0%0/2
$enc eq "identity" or $enc eq "none"
$enc eq "identity" $enc eq "none"
Truth table33%1/3
A or B
A= $enc eq "identity"B= $enc eq "none"
ABresult
1X1
011
000
1057 5 ▶ next if( ++$seen->{ $enc } > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if ++$seen->{$enc} > 1 0 5 5
1058 5 ▶ my $s = HTTP::Promise::Stream->new( $temp, encoding => $enc ) ||
Condition50%1/2
'HTTP::Promise::Stream'->new($temp, 'encoding', $enc) || (return $self->pass_error('HTTP::Promise::Stream'->error))
l!l
50
MC/DC0%0/1
'HTTP::Promise::Stream'->new($temp, 'encoding', $enc) || (return $self->pass_error('HTTP::Promise::Stream'->error))
'HTTP::Promise::Stream'->new($temp, 'encoding', $enc)
Truth table50%1/2
A
A= 'HTTP::Promise::Stream'->new($temp, 'encoding', $enc)
Aresult
11
00
1059 return( $self->pass_error( HTTP::Promise::Stream->error ) );
1060 5 my $encoded = $self->new_scalar;
1061 5 my $len = $s->read( $encoded );
1062 5 ▶ return( $self->pass_error( $s->error ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 5 5
1063 5 ▶ return( $self->error( "The encoding pass on the HTTP body in memory with encoding '$enc' resulted in 0 byte encoded!" ) ) if( !$len );
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 5 5
1064 5 $temp = $encoded;
1065 }
1066 5 $body->set( $temp );
1067 5 $self->body( $body );
1068 }
1069 else
1070 {
1071 0 return( $self->error( "I do not know how to handle HTTP body object of class ", ref( $body ) ) );
1072 }
1073 6 return( $body );
1074 }
1075  
1076 6 ▶sub epilogue { return( shift->_set_get_array_as_object( 'epilogue', @_ ) ); }
Subroutine epilogue: called CC=1 SCAR=0.0
Pod documented
1077  
1078 2 ▶sub ext_vary { return( shift->_set_get_boolean( 'ext_vary', @_ ) ); }
Subroutine ext_vary: called CC=1 SCAR=0.0
Pod documented
1079  
1080 # Credits: Christopher J. Madsen (IO::HTML)
1081 # Extract here, because I do not want to load all the modules
1082 sub guess_character_encoding
1083 4 ▶{
Subroutine guess_character_encoding: called CC=23 SCAR=49.0
Pod documented
1084 4 my $self = shift( @_ );
1085 4 my $opts = $self->_get_args_as_hash( @_ );
1086 4 my $data;
1087 4 ▶ if( exists( $opts->{content} ) && length( $opts->{content} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{'content'} and length $opts->{'content'}) { } 4 0 4
Condition33%1/3
exists $opts->{"content"} and length $opts->{"content"}
!ll&&!rl&&r
004
MC/DC0%0/2
exists $opts->{"content"} and length $opts->{"content"}
exists $opts->{"content"} length $opts->{"content"}
Truth table33%1/3
A and B
A= exists $opts->{"content"}B= length $opts->{"content"}
ABresult
0X0
100
111
1088 {
1089 4 ▶ return( $self->error( "Unsupported data type (", ref( $opts->{content} ), ")." ) ) if( ref( $opts->{content} ) && !$self->_is_scalar( $opts->{content} ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts->{"content"} and not $self->_is_scalar($opts->{"content"}) 0 4 4
Condition33%1/3
ref $opts->{"content"} and not $self->_is_scalar($opts->{"content"})
!ll&&!rl&&r
040
MC/DC0%0/2
ref $opts->{"content"} and not $self->_is_scalar($opts->{"content"})
ref $opts->{"content"} not $self->_is_scalar($opts->{"content"})
Truth table33%1/3
A and not(B)
A= ref $opts->{"content"}B= not $self->_is_scalar($opts->{"content"})
ABresult
0X0
100
111
1090 4 ▶ $data = $self->_is_scalar( $opts->{content} ) ? $opts->{content} : \$opts->{content};
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_scalar($opts->{'content'}) ? : 4 0 4
1091 }
1092 else
1093 {
1094 0 my $body = $self->body;
1095 0 ▶ return( '' ) if( !$body || $body->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if not $body or $body->is_empty 0 0 0
Condition0%0/3
not $body or $body->is_empty
l!l&&r!l&&!r
000
MC/DC0%0/2
not $body or $body->is_empty
not $body $body->is_empty
Truth table0%0/3
not(A) or B
A= not $bodyB= $body->is_empty
ABresult
1X1
011
000
1096 0 my $buff;
1097 0 ▶ my $io = $body->open( '<', { binmode => 'raw' } ) ||
Condition0%0/2
$body->open('<', {'binmode', 'raw'}) || (return $self->pass_error($body->error))
l!l
00
MC/DC0%0/1
$body->open('<', {'binmode', 'raw'}) || (return $self->pass_error($body->error))
$body->open('<', {'binmode', 'raw'})
Truth table0%0/2
A
A= $body->open('<', {'binmode', 'raw'})
Aresult
11
00
1098 return( $self->pass_error( $body->error ) );
1099 0 my $bytes = $io->read( $buff, 4096 );
1100 0 $io->close;
1101 0 ▶ return( $self->pass_error( $io->error ) ) if( !defined( $bytes ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 0 0
1102 0 $data = \$buff;
1103 }
1104 4 ▶ return( '' ) if( $self->is_binary( $data ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_binary($data) 0 4 4
1105  
1106 4 my $encoding;
1107 4 ▶ if( $$data =~ /^\xFe\xFF/ )
Branch: 3 branches
BranchTrueFalseTotal
if ($$data =~ /^\xFe\xFF/) { } 0 4 4
elsif ($$data =~ /^\xFF\xFe/) { } 0 4 4
elsif ($$data =~ /^\xEF\xBB\xBF/) { } 0 4 4
1108 {
1109 0 $encoding = 'UTF-16BE';
1110 }
1111 elsif( $$data =~ /^\xFF\xFe/ )
1112 {
1113 0 $encoding = 'UTF-16LE';
1114 }
1115 elsif( $$data =~ /^\xEF\xBB\xBF/ )
1116 {
1117 0 $encoding = 'utf-8-strict';
1118 }
1119  
1120 # try decoding as UTF-8
1121 4 ▶ if( !defined( $encoding ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $encoding) 4 0 4
1122 {
1123 4 ▶ $self->_load_class( 'Encode' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Encode") or return $self->pass_error 4 0 4
1124 4 my $test = Encode::decode( 'utf-8-strict', $$data, Encode::FB_QUIET );
1125 # end if valid UTF-8 with at least one multi-byte character:
1126 4 ▶ if( $$data =~ /^(?: # nothing left over
Branch: 1 branch
BranchTrueFalseTotal
if ($$data =~ /^(?: # nothing left over | [\xC2-\xDF] # incomplete 2-byte char | [\xE0-\xEF] [\x80-\xBF]? # incomplete 3-byte char | [\xF0-\xF4] [\x80-\xBF]{0,2} # incomplete 4-byte char )\z/x and $test =~ /[^\x00-\x7F]/) 0 4 4
Condition33%1/3
$$data =~ /^(?: # nothing left over | [\xC2-\xDF] # incomplete 2-byte char | [\xE0-\xEF] [\x80-\xBF]? # incomplete 3-byte char | [\xF0-\xF4] [\x80-\xBF]{0,2} # incomplete 4-byte char )\z/x and $test =~ /[^\x00-\x7F]/
!ll&&!rl&&r
040
MC/DC0%0/2
$$data =~ /^(?: # nothing left over | [\xC2-\xDF] # incomplete 2-byte char | [\xE0-\xEF] [\x80-\xBF]? # incomplete 3-byte char | [\xF0-\xF4] [\x80-\xBF]{0,2} # incomplete 4-byte char )\z/x and $test =~ /[^\x00-\x7F]/
$$data =~ /^(?: # nothing left over | [\xC2-\xDF] # incomplete 2-byte char | [\xE0-\xEF] [\x80-\xBF]? # incomplete 3-byte char | [\xF0-\xF4] [\x80-\xBF]{0,2} # incomplete 4-byte char )\z/x $test =~ /[^\x00-\x7F]/
Truth table33%1/3
A and B
A= $$data =~ /^(?: # nothing left over | [\xC2-\xDF] # incomplete 2-byte char | [\xE0-\xEF] [\x80-\xBF]? # incomplete 3-byte char | [\xF0-\xF4] [\x80-\xBF]{0,2} # incomplete 4-byte char )\z/xB= $test =~ /[^\x00-\x7F]/
ABresult
0X0
100
111
1127 | [\xC2-\xDF] # incomplete 2-byte char
1128 | [\xE0-\xEF] [\x80-\xBF]? # incomplete 3-byte char
1129 | [\xF0-\xF4] [\x80-\xBF]{0,2} # incomplete 4-byte char
1130 )\z/x and $test =~ /[^\x00-\x7F]/ )
1131 {
1132 0 $encoding = 'utf-8-strict';
1133 }
1134 }
1135 # end if testing for UTF-8
1136 4 ▶ if( defined( $encoding ) and
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding and $opts->{"object"} and not ref $encoding) 0 4 4
Condition33%2/6
defined $encoding and $opts->{"object"} and not ref $encoding
!ll&&!rl&&r
400
defined $encoding and $opts->{"object"}
!ll&&!rl&&r
400
MC/DC0%0/3
defined $encoding and $opts->{"object"} and not ref $encoding
defined $encoding $opts->{"object"} not ref $encoding
Truth table25%1/4
A and B and not(C)
A= defined $encodingB= $opts->{"object"}C= not ref $encoding
ABCresult
0XX0
10X0
1100
1111
1137 $opts->{object} and
1138 !ref( $encoding ) )
1139 {
1140 0 ▶ $self->_load_class( 'Encode' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Encode") or return $self->pass_error 0 0 0
1141 0 $encoding = Encode::find_encoding( $encoding );
1142 }
1143 4 ▶ return( defined( $encoding ) ? $encoding : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $encoding ? : 0 4 4
1144 }
1145  
1146 0 ▶sub header { return( shift->headers->header( @_ ) ); }
Subroutine header: not called CC=1 SCAR=6.9
Pod documented
1147  
1148 257 ▶sub headers { return( shift->_set_get_object_without_init( 'headers','HTTP::Promise::Headers', @_ ) ); }
Subroutine headers: called CC=1 SCAR=0.0
Pod documented
1149  
1150 0 ▶sub header_as_string { return( shift->headers->as_string( @_ ) ); }
Subroutine header_as_string: not called CC=1 SCAR=6.9
Pod documented
1151  
1152 58 ▶sub http_message { return( shift->_set_get_object_without_init( 'http_message', 'HTTP::Promise::Message', @_ ) ); }
Subroutine http_message: called CC=1 SCAR=0.0
Pod documented
1153  
1154 # Ref: <https://stackoverflow.com/questions/9956198/in-perl-how-can-i-can-check-if-an-encoding-specified-in-a-string-is-valid>
1155 sub io_encoding
1156 4 ▶{
Subroutine io_encoding: called CC=41 SCAR=59.6
Pod documented
1157 4 my $self = shift( @_ );
1158 4 my $opts = $self->_get_args_as_hash( @_ );
1159 # body argument is necessary when content has been decoded, but not replaced with decode_body()
1160 # and then HTTP::Promise::Message::decoded_content calls io_encoding() to get the character encoding
1161 4 ▶ my $body = $opts->{body} // $self->body;
Condition33%1/3
$opts->{"body"} // $self->body
l!l&&r!l&&!r
400
MC/DC0%0/2
$opts->{"body"} // $self->body
$opts->{"body"} $self->body
Truth table33%1/3
A // B
A= $opts->{"body"}B= $self->body
ABresult
1X1
011
000
1162 4 my $headers = $self->headers;
1163 # Use cache if it exists
1164 4 ▶ if( !exists( $opts->{content} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $opts->{"content"} and $opts->{"charset_strict"} && $self->{"_io_encoding_strict_cached"} || !$opts->{"charset_strict"} && $self->{"_io_encoding_cached"} and $body and $self->{"_checksum_md5"} eq $body->checksum_md5) 0 4 4
Condition33%6/18
not exists $opts->{"content"} and $opts->{"charset_strict"} && $self->{"_io_encoding_strict_cached"} || !$opts->{"charset_strict"} && $self->{"_io_encoding_cached"} and $body and $self->{"_checksum_md5"} eq $body->checksum_md5
!ll&&!rl&&r
400
not exists $opts->{"content"} and $opts->{"charset_strict"} && $self->{"_io_encoding_strict_cached"} || !$opts->{"charset_strict"} && $self->{"_io_encoding_cached"} and $body
!ll&&!rl&&r
400
not exists $opts->{"content"} and $opts->{"charset_strict"} && $self->{"_io_encoding_strict_cached"} || !$opts->{"charset_strict"} && $self->{"_io_encoding_cached"}
!ll&&!rl&&r
040
$opts->{"charset_strict"} and $self->{"_io_encoding_strict_cached"} or not $opts->{"charset_strict"} and $self->{"_io_encoding_cached"}
l!l&&r!l&&!r
004
$opts->{"charset_strict"} and $self->{"_io_encoding_strict_cached"}
!ll&&!rl&&r
400
not $opts->{"charset_strict"} and $self->{"_io_encoding_cached"}
!ll&&!rl&&r
040
MC/DC0%0/7
not exists $opts->{"content"} and $opts->{"charset_strict"} && $self->{"_io_encoding_strict_cached"} || !$opts->{"charset_strict"} && $self->{"_io_encoding_cached"} and $body and $self->{"_checksum_md5"} eq $body->checksum_md5
not exists $opts->{"content"} $opts->{"charset_strict"} $self->{"_io_encoding_strict_cached"} not $opts->{"charset_strict"} $self->{"_io_encoding_cached"} $body $self->{"_checksum_md5"} eq $body->checksum_md5
Truth table7%1/14
not(A) and B and C or not(D) and E and F and G
A= not exists $opts->{"content"}B= $opts->{"charset_strict"}C= $self->{"_io_encoding_strict_cached"}D= not $opts->{"charset_strict"}E= $self->{"_io_encoding_cached"}F= $bodyG= $self->{"_checksum_md5"} eq $body->checksum_md5
ABCDEFGresult
0XXXXXX0
10X0XXX0
10X10XX0
1100XXX0
11010XX0
111XX0X0
10X110X0
110110X0
111XX100
10X11100
11011100
111XX111
10X11111
11011111
1165 ( ( $opts->{charset_strict} && $self->{_io_encoding_strict_cached} ) ||
1166 ( !$opts->{charset_strict} && $self->{_io_encoding_cached} )
1167 ) &&
1168 $body &&
1169 $self->{_checksum_md5} eq $body->checksum_md5 )
1170 {
1171 0 ▶ return( $opts->{charset_strict} ? $self->{_io_encoding_strict_cached} : $self->{_io_encoding_cached} );
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'charset_strict'} ? : 0 0 0
1172 }
1173 4 my $data;
1174 4 ▶ if( exists( $opts->{content} ) && length( $opts->{content} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $opts->{'content'} and length $opts->{'content'}) { } 0 4 4
Condition33%1/3
exists $opts->{"content"} and length $opts->{"content"}
!ll&&!rl&&r
400
MC/DC0%0/2
exists $opts->{"content"} and length $opts->{"content"}
exists $opts->{"content"} length $opts->{"content"}
Truth table33%1/3
A and B
A= exists $opts->{"content"}B= length $opts->{"content"}
ABresult
0X0
100
111
1175 {
1176 0 ▶ return( $self->error( "Unsupported data type (", ref( $opts->{content} ), ")." ) ) if( ref( $opts->{content} ) && !$self->_is_scalar( $opts->{content} ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $opts->{"content"} and not $self->_is_scalar($opts->{"content"}) 0 0 0
Condition0%0/3
ref $opts->{"content"} and not $self->_is_scalar($opts->{"content"})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $opts->{"content"} and not $self->_is_scalar($opts->{"content"})
ref $opts->{"content"} not $self->_is_scalar($opts->{"content"})
Truth table0%0/3
A and not(B)
A= ref $opts->{"content"}B= not $self->_is_scalar($opts->{"content"})
ABresult
0X0
100
111
1177 0 ▶ $data = $self->_is_scalar( $opts->{content} ) ? $opts->{content} : \$opts->{content};
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_scalar($opts->{'content'}) ? : 0 0 0
1178 }
1179 else
1180 {
1181 # my $body = $self->body || return( '' );
1182 4 ▶ return( '' ) if( !$body );
Branch: 1 branch
BranchTrueFalseTotal
unless $body 0 4 4
1183 4 $self->{_checksum_md5} = $body->checksum_md5;
1184 4 ▶ my $io = $body->open( '<', { binmode => 'raw' } ) ||
Condition50%1/2
$body->open('<', {'binmode', 'raw'}) || (return $self->pass_error($body->error))
l!l
40
MC/DC0%0/1
$body->open('<', {'binmode', 'raw'}) || (return $self->pass_error($body->error))
$body->open('<', {'binmode', 'raw'})
Truth table50%1/2
A
A= $body->open('<', {'binmode', 'raw'})
Aresult
11
00
1185 return( $self->pass_error( $body->error ) );
1186 4 my $buff;
1187 4 my $bytes = $io->read( $buff, 4096 );
1188 4 ▶ return( $self->pass_error( $io->error ) ) if( !defined( $bytes ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 4 4
1189 4 ▶ return( '' ) if( !$bytes );
Branch: 1 branch
BranchTrueFalseTotal
unless $bytes 0 4 4
1190 4 $data = \$buff;
1191 }
1192 # return( '' ) if( $self->is_binary( $data ) );
1193
1194 4 my $enc;
1195 4 ▶ if( $headers->content_is_text || ( my $is_xml = $headers->content_is_xml ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->content_is_text or my $is_xml = $headers->content_is_xml) 4 0 4
Condition33%1/3
$headers->content_is_text or my $is_xml = $headers->content_is_xml
l!l&&r!l&&!r
400
MC/DC0%0/2
$headers->content_is_text or my $is_xml = $headers->content_is_xml
$headers->content_is_text my $is_xml = $headers->content_is_xml
Truth table33%1/3
A or B
A= $headers->content_is_textB= my $is_xml = $headers->content_is_xml
ABresult
1X1
011
000
1196 {
1197 my $charset = lc(
1198 $opts->{charset} ||
1199 $headers->content_type_charset ||
1200 $opts->{default_charset} ||
1201 # content_type_charset to tell content_charset to not try to call this method since we just called it.
1202 4 ▶ $self->content_charset( content => $data, content_type_charset => 0 ) ||
Condition36%4/11
$opts->{'charset'} || $headers->content_type_charset || $opts->{'default_charset'} || $self->content_charset('content', $data, 'content_type_charset', 0) || 'UTF-8'
l!l
04
$opts->{"charset"} or $headers->content_type_charset or $opts->{"default_charset"} or $self->content_charset("content", $data, "content_type_charset", 0)
l!l&&r!l&&!r
004
$opts->{"charset"} or $headers->content_type_charset or $opts->{"default_charset"}
l!l&&r!l&&!r
004
$opts->{"charset"} or $headers->content_type_charset
l!l&&r!l&&!r
004
MC/DC0%0/4
$opts->{'charset'} || $headers->content_type_charset || $opts->{'default_charset'} || $self->content_charset('content', $data, 'content_type_charset', 0) || 'UTF-8'
$opts->{"charset"} $headers->content_type_charset $opts->{"default_charset"} $self->content_charset("content", $data, "content_type_charset", 0)
Truth table20%1/5
A or B or C or D
A= $opts->{"charset"}B= $headers->content_type_charsetC= $opts->{"default_charset"}D= $self->content_charset("content", $data, "content_type_charset", 0)
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1203 'UTF-8'
1204 );
1205 4 ▶ if( $charset eq 'none' )
Branch: 2 branches
BranchTrueFalseTotal
if ($charset eq 'none') { } 0 4 4
elsif ($charset eq 'us-ascii' or $charset eq 'iso-8859-1') { } 0 4 4
1206 {
1207 # leave it as is
1208 }
1209 ▶ elsif( $charset eq 'us-ascii' || $charset eq 'iso-8859-1' )
Condition33%1/3
$charset eq "us-ascii" or $charset eq "iso-8859-1"
l!l&&r!l&&!r
004
MC/DC0%0/2
$charset eq "us-ascii" or $charset eq "iso-8859-1"
$charset eq "us-ascii" $charset eq "iso-8859-1"
Truth table33%1/3
A or B
A= $charset eq "us-ascii"B= $charset eq "iso-8859-1"
ABresult
1X1
011
000
1210 {
1211 # if( $$content_ref =~ /[^\x00-\x7F]/ && defined &utf8::upgrade )
1212 0 ▶ if( $$data =~ /[^\x00-\x7F]/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($$data =~ /[^\x00-\x7F]/) 0 0 0
1213 {
1214 0 $enc = 'utf-8';
1215 }
1216 }
1217 else
1218 {
1219 4 ▶ $self->_load_class( 'Encode' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Encode") or return $self->pass_error 4 0 4
1220 # try-catch
1221 4 local $@;
1222 eval
1223 4 {
1224 4 ▶ my $test = Encode::decode( $charset, $$data, ( ( $opts->{charset_strict} ? Encode::FB_CROAK : 0 ) | Encode::LEAVE_SRC ) );
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'charset_strict'} ? : 0 4 4
1225 4 $enc = $charset;
1226 };
1227 4 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
1228 {
1229 0 my $retried = 0;
1230 0 ▶ if( $@ =~ /^Unknown encoding/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /^Unknown encoding/) 0 0 0
1231 {
1232 0 ▶ my $alt_charset = lc( $opts->{alt_charset} || '' );
Condition0%0/2
$opts->{'alt_charset'} || ''
l!l
00
MC/DC0%0/1
$opts->{'alt_charset'} || ''
$opts->{'alt_charset'}
Truth table0%0/2
A
A= $opts->{'alt_charset'}
Aresult
11
00
1233 0 ▶ if( $alt_charset && $charset ne $alt_charset )
Branch: 1 branch
BranchTrueFalseTotal
if ($alt_charset and $charset ne $alt_charset) 0 0 0
Condition0%0/3
$alt_charset and $charset ne $alt_charset
!ll&&!rl&&r
000
MC/DC0%0/2
$alt_charset and $charset ne $alt_charset
$alt_charset $charset ne $alt_charset
Truth table0%0/3
A and B
A= $alt_charsetB= $charset ne $alt_charset
ABresult
0X0
100
111
1234 {
1235 # Retry decoding with the alternative charset
1236 0 ▶ my $test = Encode::decode( $alt_charset, $$data, ( ( $opts->{charset_strict} ? Encode::FB_CROAK : 0 ) | Encode::LEAVE_SRC ) ) unless( $alt_charset eq 'none' );
Branch: 2 branches
BranchTrueFalseTotal
unless $alt_charset eq "none" 0 0 0
$opts->{'charset_strict'} ? : 0 0 0
1237 0 $retried++;
1238 0 $enc = $alt_charset;
1239 }
1240 }
1241 0 ▶ return( $self->error( $@ ) ) unless( $retried );
Branch: 1 branch
BranchTrueFalseTotal
unless $retried 0 0 0
1242 }
1243 }
1244 }
1245 4 ▶ if( $opts->{charset_strict} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'charset_strict'}) { } 0 4 4
1246 {
1247 0 $self->{_io_encoding_strict_cached} = $enc;
1248 }
1249 else
1250 {
1251 4 $self->{_io_encoding_cached} = $enc;
1252 }
1253 4 ▶ return( defined( $enc ) ? $enc : '' );
Branch: 1 branch
BranchTrueFalseTotal
defined $enc ? : 4 0 4
1254 }
1255  
1256 # <https://stackoverflow.com/questions/899206/how-does-perl-know-a-file-is-binary>
1257 # <https://github.com/morungos/perl-Data-Binary/blob/master/lib/Data/Binary.pm>
1258 # "The "-T" and "-B" tests work as follows. The first block or so of the file is examined to see if it is valid UTF-8 that includes non-ASCII characters. If so, it's a "-T" file.
1259 # Otherwise, that same portion of the file is examined for odd characters such as strange control codes or characters with the high bit set. If more than a third of the characters are strange, it's a "-B" file; otherwise it's a "-T" file.
1260 # Also, any file containing a zero byte in the examined portion is considered a binary file. (If executed within the scope of a use locale which includes "LC_CTYPE", odd characters are anything that isn't a printable nor space in the current locale.) If "-T" or "-B" is used on a filehandle, the current IO buffer is examined rather than the first block. Both "-T" and "-B" return true on an empty file, or a file at EOF when testing a filehandle. Because you have to read a file to do the "-T" test, on most occasions you want to use a "-f" against the file first, as in "next unless -f $file && -T $file"."
1261 sub is_binary
1262 4 ▶{
Subroutine is_binary: called CC=20 SCAR=48.1
Pod documented
1263 4 my $self = shift( @_ );
1264 4 ▶ $self->_load_class( 'Encode' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Encode") or return $self->pass_error 4 0 4
1265 4 my $data;
1266 4 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 4 0 4
1267 {
1268 # We need to make a copy
1269 4 my $this = shift( @_ );
1270 4 ▶ return(0) if( !defined( $this ) || !length( "$this" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this and length "$this" 0 4 4
Condition33%1/3
defined $this and length "$this"
!ll&&!rl&&r
004
MC/DC0%0/2
defined $this and length "$this"
defined $this length "$this"
Truth table33%1/3
A and B
A= defined $thisB= length "$this"
ABresult
0X0
100
111
1271 4 ▶ return( $self->error( "Bad argument. You can only provide 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 4 4
Condition33%1/3
ref $this and not $self->_is_scalar($this)
!ll&&!rl&&r
040
MC/DC0%0/2
ref $this and not $self->_is_scalar($this)
ref $this not $self->_is_scalar($this)
Truth table33%1/3
A and not(B)
A= ref $thisB= not $self->_is_scalar($this)
ABresult
0X0
100
111
1272 4 ▶ $data = ref( $this ) ? $this : \$this;
Branch: 1 branch
BranchTrueFalseTotal
ref $this ? : 4 0 4
1273 }
1274 else
1275 {
1276 0 my $body = $self->body;
1277 0 ▶ return(0) if( !$body || $body->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if not $body or $body->is_empty 0 0 0
Condition0%0/3
not $body or $body->is_empty
l!l&&r!l&&!r
000
MC/DC0%0/2
not $body or $body->is_empty
not $body $body->is_empty
Truth table0%0/3
not(A) or B
A= not $bodyB= $body->is_empty
ABresult
1X1
011
000
1278 0 my $buff;
1279 0 ▶ my $io = $body->open( '<', { binmode => 'raw' } ) ||
Condition0%0/2
$body->open('<', {'binmode', 'raw'}) || (return $self->pass_error($body->error))
l!l
00
MC/DC0%0/1
$body->open('<', {'binmode', 'raw'}) || (return $self->pass_error($body->error))
$body->open('<', {'binmode', 'raw'})
Truth table0%0/2
A
A= $body->open('<', {'binmode', 'raw'})
Aresult
11
00
1280 return( $self->pass_error( $body->error ) );
1281 0 my $bytes = $io->read( $buff, 4096 );
1282 0 $io->close;
1283 0 ▶ return( $self->pass_error( $io->error ) ) if( !defined( $bytes ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 0 0
1284 0 ▶ warn( "Body is ", $body->length, " bytes big, but somehow I could not read ny bytes out of it.\n" ) if( !$bytes && warnings::enabled( 'HTTP::Promise' ) );
Branch: 1 branch
BranchTrueFalseTotal
if not $bytes and warnings::enabled("HTTP::Promise") 0 0 0
Condition0%0/3
not $bytes and warnings::enabled("HTTP::Promise")
!ll&&!rl&&r
000
MC/DC0%0/2
not $bytes and warnings::enabled("HTTP::Promise")
not $bytes warnings::enabled("HTTP::Promise")
Truth table0%0/3
not(A) and B
A= not $bytesB= warnings::enabled("HTTP::Promise")
ABresult
0X0
100
111
1285 0 ▶ return(0) if( !$bytes );
Branch: 1 branch
BranchTrueFalseTotal
unless $bytes 0 0 0
1286 0 $data = \$buff;
1287 }
1288
1289 # There are various method to check if the data is or contains binary data
1290 # perl's -B function is very cautious and will lean on the false positive.
1291 # Data::Binary implements the perl algorithm, but still yield false positive if, for example,
1292 # there is even 1 \0 in the data
1293 # The most reliable yet is to use module Encode with the die flag on upon error and catch it.
1294
1295 # Has the utf8 bit been set?
1296 # Then, let's try to encode it into utf-8
1297 4 ▶ if( utf8::is_utf8( $$data ) )
Branch: 1 branch
BranchTrueFalseTotal
if (utf8::is_utf8($$data)) 0 4 4
1298 {
1299 eval
1300 0 {
1301 0 Encode::encode( 'utf-8', $$data, ( Encode::FB_CROAK | Encode::LEAVE_SRC ) );
1302 };
1303 0 ▶ return( $@ ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
1304 }
1305 # otherwise, let's try to decode this into perl's internal utf8 representation
1306 # else
1307 # {
1308 # eval
1309 # {
1310 # Encode::decode( 'utf8', $$data, ( Encode::FB_CROAK | Encode::LEAVE_SRC ) );
1311 # };
1312 # }
1313 # return( $@ ? 1 : 0 );
1314  
1315 4 ▶ return(1) if( index( $$data, "\c@" ) != -1 );
Branch: 1 branch
BranchTrueFalseTotal
if (index($$data, "\0") != -1) 0 4 4
1316 4 my $length = length( $$data );
1317 4 my $odd = ( $$data =~ tr/\x01\x02\x03\x04\x05\x06\x07\x09\x0b\x0c\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f//d );
1318 # Detecting >=128 and non-UTF-8 is interesting. Note that all UTF-8 >=128 has several bytes with
1319 # >=128 set, so a quick test is possible by simply checking if any are >=128. However, the count
1320 # from that is typically wrong, if this is binary data, it'll not have been decoded. So we do this
1321 # in two steps.
1322  
1323 4 my $copy = $$data;
1324 4 ▶ if( ( $copy =~ tr[\x80-\xff][]d ) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ($copy =~ tr/\200-\377//d > 0) 0 4 4
1325 {
1326 0 my $modified = Encode::decode_utf8( $$data, Encode::FB_DEFAULT );
1327 0 my $substitions = ( $modified =~ tr/\x{fffd}//d );
1328 0 $odd += $substitions;
1329 }
1330 4 ▶ return(1) if( ( $odd / $length ) > 0.34 );
Branch: 1 branch
BranchTrueFalseTotal
if $odd / $length > "0.34" 0 4 4
1331 4 return(0);
1332 }
1333  
1334 sub is_body_on_file
1335 0 ▶{
Subroutine is_body_on_file: not called CC=3 SCAR=24.8
Pod documented
1336 0 my $self = shift( @_ );
1337 0 my $body = $self->body;
1338 0 ▶ return(0) if( !$body || $body->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if not $body or $body->is_empty 0 0 0
Condition0%0/3
not $body or $body->is_empty
l!l&&r!l&&!r
000
MC/DC0%0/2
not $body or $body->is_empty
not $body $body->is_empty
Truth table0%0/3
not(A) or B
A= not $bodyB= $body->is_empty
ABresult
1X1
011
000
1339 0 return( $self->_is_a( $body => 'HTTP::Promise::Body::File' ) );
1340 }
1341  
1342 sub is_body_in_memory
1343 0 ▶{
Subroutine is_body_in_memory: not called CC=3 SCAR=24.8
Pod documented
1344 0 my $self = shift( @_ );
1345 0 my $body = $self->body;
1346 0 ▶ return(0) if( !$body || $body->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if not $body or $body->is_empty 0 0 0
Condition0%0/3
not $body or $body->is_empty
l!l&&r!l&&!r
000
MC/DC0%0/2
not $body or $body->is_empty
not $body $body->is_empty
Truth table0%0/3
not(A) or B
A= not $bodyB= $body->is_empty
ABresult
1X1
011
000
1347 0 return( $self->_is_a( $body => 'HTTP::Promise::Body::Scalar' ) );
1348 }
1349  
1350 # Convenience
1351 sub is_decoded
1352 4 ▶{
Subroutine is_decoded: called CC=2 SCAR=7.4
Pod documented
1353 4 my $self = shift( @_ );
1354 4 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 4 0 4
1355 {
1356 4 my $bool = shift( @_ );
1357 4 return( !$self->is_encoded( !$bool ) );
1358 }
1359 else
1360 {
1361 0 return( !$self->is_encoded );
1362 }
1363 }
1364  
1365 32 ▶sub is_encoded { return( shift->_set_get_boolean( 'is_encoded', @_ ) ); }
Subroutine is_encoded: called CC=1 SCAR=0.0
Pod documented
1366  
1367 sub is_multipart
1368 3 ▶{
Subroutine is_multipart: called CC=5 SCAR=18.1
Pod documented
1369 3 my $self = shift( @_ );
1370 # no head, so no MIME type!
1371 3 ▶ $self->headers or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->headers or return 3 0 3
1372 3 my $mime_type = $self->headers->type;
1373 3 ▶ return(0) if( !defined( $mime_type ) || !length( $mime_type ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mime_type and length $mime_type 0 3 3
Condition33%1/3
defined $mime_type and length $mime_type
!ll&&!rl&&r
003
MC/DC0%0/2
defined $mime_type and length $mime_type
defined $mime_type length $mime_type
Truth table33%1/3
A and B
A= defined $mime_typeB= length $mime_type
ABresult
0X0
100
111
1374 3 ▶ return( substr( lc( $mime_type ), 0, 9 ) eq 'multipart' ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
substr(lc $mime_type, 0, 9) eq 'multipart' ? : 3 0 3
1375 }
1376  
1377 0 ▶sub is_text { return( !shift->is_binary( @_ ) ); }
Subroutine is_text: not called CC=1 SCAR=6.9
Pod documented
1378  
1379 2 ▶sub make_boundary { return( Crypt::Misc::random_v4uuid() ); }
Subroutine make_boundary: called CC=1 SCAR=0.0
Pod documented
1380  
1381 sub make_multipart
1382 3 ▶{
Subroutine make_multipart: called CC=8 SCAR=34.7
Pod documented
1383 3 my $self = shift( @_ );
1384 3 my $subtype = shift( @_ );
1385 3 my $opts = $self->_get_args_as_hash( @_ );
1386 3 my $tag;
1387 3 ▶ $subtype ||= 'form-data';
Condition50%1/2
$subtype ||= "form-data"
l!l
03
MC/DC0%0/1
$subtype ||= "form-data"
$subtype
Truth table50%1/2
A
A= $subtype
Aresult
11
00
1388 3 my $force = $opts->{force};
1389  
1390 # Trap for simple case: already a multipart?
1391 3 ▶ return( $self ) if( $self->is_multipart and !$force );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_multipart and not $force 3 0 3
Condition33%1/3
$self->is_multipart and not $force
!ll&&!rl&&r
003
MC/DC0%0/2
$self->is_multipart and not $force
$self->is_multipart not $force
Truth table33%1/3
A and not(B)
A= $self->is_multipartB= not $force
ABresult
0X0
100
111
1392 0 my $headers = $self->headers;
1393
1394  
1395 # Rip out our guts, and spew them into our future part.
1396 # part is a shallow copy
1397 # my $part = bless( {%$self} => ref( $self ) );
1398 # my $part = $self->new(
1399 # headers => $headers->clone,
1400 # ( $self->body ? ( body => $self->body ) : () ),
1401 # debug => $self->debug,
1402 # );
1403 #
1404 # if( my $msg = $self->http_message )
1405 # {
1406 # my $clone = $msg->clone( clone_entity => 0 );
1407 # $clone->entity( $part );
1408 # $part->http_message( $clone );
1409 # }
1410 # $part->parts( $self->parts );
1411
1412 0 my $part = $self->clone;
1413
1414 # my $part = $self->clone;
1415 # lobotomize ourselves!
1416 # %$self = ();
1417 # clone the headers
1418  
1419 # Remove content headers from top-level, and set it up as a multipart
1420 0 my $removed = $headers->remove_content_headers;
1421 0 ▶ my $ct = $headers->new_field( 'Content-Type' => "multipart/${subtype}" ) ||
Condition0%0/2
$headers->new_field('Content-Type', "multipart/$subtype") || (return $self->pass_error($headers->error))
l!l
00
MC/DC0%0/1
$headers->new_field('Content-Type', "multipart/$subtype") || (return $self->pass_error($headers->error))
$headers->new_field('Content-Type', "multipart/$subtype")
Truth table0%0/2
A
A= $headers->new_field('Content-Type', "multipart/$subtype")
Aresult
11
00
1422 return( $self->pass_error( $headers->error ) );
1423 0 $ct->boundary( $self->make_boundary );
1424 0 my $ct_string = $ct->as_string;
1425 0 $headers->header( 'Content-Type' => "${ct_string}" );
1426  
1427 # Remove non-content headers from the part
1428 0 $removed = $self->new_array;
1429 0 foreach $tag ( grep{ !/^content-/i } $part->headers->header_field_names )
1430 {
1431 0 $part->headers->delete( $tag );
1432 0 $removed->push( $tag );
1433 }
1434 0 $self->parts->reset;
1435 0 ▶ $self->add_part( $part ) if( $part->body || $part->parts->length );
Branch: 1 branch
BranchTrueFalseTotal
if $part->body or $part->parts->length 0 0 0
Condition0%0/3
$part->body or $part->parts->length
l!l&&r!l&&!r
000
MC/DC0%0/2
$part->body or $part->parts->length
$part->body $part->parts->length
Truth table0%0/3
A or B
A= $part->bodyB= $part->parts->length
ABresult
1X1
011
000
1436 0 return( $self );
1437 }
1438  
1439 sub make_singlepart
1440 0 ▶{
Subroutine make_singlepart: not called CC=8 SCAR=42.8
Pod documented
1441 0 my $self = shift( @_ );
1442 # Trap for simple cases:
1443 # already a singlepart?
1444 0 ▶ return( $self ) if( !$self->is_multipart );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_multipart 0 0 0
1445 # can this even be done?
1446 0 ▶ return(0) if( $self->parts > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->parts > 1 0 0 0
1447  
1448 # Get rid of all our existing content info
1449 0 my $tag;
1450 0 foreach $tag ( grep{ /^content-/i } $self->headers->header_field_names )
1451 {
1452 0 $self->headers->delete( $tag );
1453 }
1454  
1455 # one part
1456 0 ▶ if( $self->parts->length == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parts->length == 1) { } 0 0 0
1457 {
1458 0 my $part = $self->parts->index(0);
1459 # Populate ourselves with any content info from the part:
1460 0 foreach $tag ( grep{ /^content-/i } $part->headers->header_field_names )
1461 {
1462 0 $self->headers->add( $tag => $_ ) for( $part->headers->get( $tag ) );
1463 }
1464  
1465 # Save reconstructed headers, replace our guts, and restore header:
1466 0 my $new_head = $self->headers;
1467 # shallow copy is ok!
1468 0 %$self = %$part;
1469 0 $self->headers( $new_head );
1470  
1471 # One more thing: the part *may* have been a multi with 0 or 1 parts!
1472 0 ▶ return( $self->make_singlepart( @_ ) ) if( $self->is_multipart );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_multipart 0 0 0
1473 }
1474 # no parts!
1475 else
1476 {
1477 0 $self->headers->mime_attr( 'Content-type' => 'text/plain' ); ### simple
1478 }
1479 0 return( $self );
1480 }
1481  
1482 sub mime_type
1483 41 ▶{
Subroutine mime_type: called CC=2 SCAR=7.0
Pod documented
1484 41 my $self = shift( @_ );
1485 41 my $headers = $self->headers;
1486 41 ▶ return if( !defined( $headers ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $headers 0 41 41
1487 41 return( $headers->mime_type( @_ ) );
1488 }
1489  
1490 # NOTE name() is to associate a name for this entity for multipart/form-data
1491 24 ▶sub name { return( shift->_set_get_scalar_as_object( 'name', @_ ) ); }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
1492  
1493 sub new_body
1494 26 ▶{
Subroutine new_body: called CC=6 SCAR=18.5
Pod documented
1495 26 my $self = shift( @_ );
1496 26 ▶ my $type = shift( @_ ) || 'scalar';
Condition50%1/2
shift @_ || 'scalar'
l!l
260
MC/DC0%0/1
shift @_ || 'scalar'
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
1497 26 my $map =
1498 {
1499 file => 'HTTP::Promise::Body::File',
1500 form => 'HTTP::Promise::Body::Form',
1501 scalar => 'HTTP::Promise::Body::Scalar',
1502 string => 'HTTP::Promise::Body::Scalar',
1503 };
1504 26 ▶ my $class = $map->{ $type } || return( $self->error( "Unsupported body type '$type'" ) );
Condition50%1/2
$map->{$type} || (return $self->error("Unsupported body type '${type}'"))
l!l
260
MC/DC0%0/1
$map->{$type} || (return $self->error("Unsupported body type '${type}'"))
$map->{$type}
Truth table50%1/2
A
A= $map->{$type}
Aresult
11
00
1505 26 ▶ if( $type eq 'form' )
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "form") 1 25 26
1506 {
1507 1 ▶ $self->_load_class( $class ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class($class) or return $self->pass_error 1 0 1
1508 }
1509 26 my $body = $class->new( @_ );
1510 26 ▶ return( $self->pass_error( $class->error ) ) if( !defined( $body ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 26 26
1511 26 return( $body );
1512 }
1513  
1514 sub open
1515 17 ▶{
Subroutine open: called CC=3 SCAR=11.3
Pod documented
1516 17 my $self = shift( @_ );
1517 17 my $body = $self->body;
1518 17 ▶ return( $self->error( "Unable to open the entity body, because none is currently set." ) ) if( !$body );
Branch: 1 branch
BranchTrueFalseTotal
unless $body 0 17 17
1519 17 ▶ my $io = $body->open( @_ ) ||
Condition50%1/2
$body->open(@_) || (return $self->pass_error($body->error))
l!l
170
MC/DC0%0/1
$body->open(@_) || (return $self->pass_error($body->error))
$body->open(@_)
Truth table50%1/2
A
A= $body->open(@_)
Aresult
11
00
1520 return( $self->pass_error( $body->error ) );
1521 17 return( $io );
1522 }
1523  
1524 0 ▶sub output_dir { return( shift->_set_get_file( 'output_dir', @_ ) ); }
Subroutine output_dir: not called CC=1 SCAR=6.9
Pod documented
1525  
1526 # An array of HTTP::Promise::Entity objects
1527 52 ▶sub parts { return( shift->_set_get_array_as_object( '_parts', @_ ) ); }
Subroutine parts: called CC=1 SCAR=0.0
Pod documented
1528  
1529 7 ▶sub preamble { return( shift->_set_get_array_as_object( 'preamble', @_ ) ); }
Subroutine preamble: called CC=1 SCAR=0.0
Pod documented
1530  
1531 sub print
1532 22 ▶{
Subroutine print: called CC=13 SCAR=27.5
Pod documented
1533 22 my $self = shift( @_ );
1534 22 my $out = shift( @_ );
1535 22 my $opts = $self->_get_args_as_hash( @_ );
1536 22 ▶ my $eol = $opts->{eol} || $HTTP::Promise::Entity::BOUNDARY_DELIMITER || CRLF;
Condition60%3/5
$opts->{'eol'} || $HTTP::Promise::Entity::BOUNDARY_DELIMITER || "\r\n"
l!l
220
$opts->{"eol"} or $HTTP::Promise::Entity::BOUNDARY_DELIMITER
l!l&&r!l&&!r
7150
MC/DC0%0/2
$opts->{'eol'} || $HTTP::Promise::Entity::BOUNDARY_DELIMITER || "\r\n"
$opts->{"eol"} $HTTP::Promise::Entity::BOUNDARY_DELIMITER
Truth table66%2/3
A or B
A= $opts->{"eol"}B= $HTTP::Promise::Entity::BOUNDARY_DELIMITER
ABresult
1X1
011
000
1537 22 ▶ $out = select if( !defined( $out ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 1 21 22
1538 22 ▶ $out = Symbol::qualify( $out, scalar( caller ) ) unless( ref( $out ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $out 0 22 22
1539 22 ▶ $self->_load_class( 'HTTP::Promise::IO' ) || return( $self->error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::IO") or return $self->error 22 0 22
1540 22 ▶ my $io = $self->_is_a( $out => 'HTTP::Promise::IO' )
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_a($out, 'HTTP::Promise::IO') ? : 8 14 22
1541 ? $out
1542 : HTTP::Promise::IO->new( $out, debug => $self->debug );
1543 22 ▶ return( $self->pass_error( HTTP::Promise::IO->error ) ) if( !defined( $io ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $io 0 22 22
1544 22 $opts->{eol} = $eol;
1545 # The start-line
1546 22 ▶ $self->print_start_line( $io, $opts ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->print_start_line($io, $opts) or return $self->pass_error 22 0 22
1547 # The headers
1548 22 ▶ $self->print_header( $io, $opts ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->print_header($io, $opts) or return $self->pass_error 22 0 22
1549 22 ▶ $io->print( $eol ) ||
Branch: 1 branch
BranchTrueFalseTotal
$io->print($eol) or return $self->error("Unable to print to filehandle provided '${io}': $!") 22 0 22
1550 return( $self->error( "Unable to print to filehandle provided '$io': $!" ) );
1551 # The body
1552 22 ▶ $self->print_body( $io, ( scalar( keys( %$opts ) ) ? $opts : () ) ) || return( $self->pass_error );
Branch: 2 branches
BranchTrueFalseTotal
$self->print_body($io, scalar keys %$opts ? $opts : ()) or return $self->pass_error 22 0 22
scalar keys %$opts ? : 22 0 22
1553 22 return( $self );
1554 }
1555  
1556 sub print_body
1557 23 ▶{
Subroutine print_body: called CC=29 SCAR=46.6
Pod documented
1558 23 my $self = shift( @_ );
1559 23 my $out = shift( @_ );
1560 23 my $opts = $self->_get_args_as_hash( @_ );
1561 23 ▶ return( $self->error( "Filehandle provided ($out) is not a proper filehandle and its not a HTTP::Promise::IO object." ) ) if( !$self->_is_glob( $out ) && !$self->_is_a( $out => 'HTTP::Promise::IO' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_glob($out) or $self->_is_a($out, "HTTP::Promise::IO") 0 23 23
Condition66%2/3
$self->_is_glob($out) or $self->_is_a($out, "HTTP::Promise::IO")
l!l&&r!l&&!r
1220
MC/DC0%0/2
$self->_is_glob($out) or $self->_is_a($out, "HTTP::Promise::IO")
$self->_is_glob($out) $self->_is_a($out, "HTTP::Promise::IO")
Truth table66%2/3
A or B
A= $self->_is_glob($out)B= $self->_is_a($out, "HTTP::Promise::IO")
ABresult
1X1
011
000
1562 23 ▶ $out ||= select;
Condition33%1/3
$out ||= select
l!l&&r!l&&!r
2300
MC/DC0%0/2
$out ||= select
$out select
Truth table33%1/3
A ||= B
A= $outB= select
ABresult
1X1
011
000
1563 23 my $mime_type = $self->mime_type;
1564 23 my $toptype;
1565 23 ▶ $toptype = [split( '/', lc( $mime_type ), 2 )]->[0] if( defined( $mime_type ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $mime_type 23 0 23
1566 # my $crlf = $HTTP::Promise::Entity::BOUNDARY_DELIMITER || CRLF;
1567 23 ▶ my $crlf = $opts->{eol} || $HTTP::Promise::Entity::BOUNDARY_DELIMITER || CRLF;
Condition60%3/5
$opts->{'eol'} || $HTTP::Promise::Entity::BOUNDARY_DELIMITER || "\r\n"
l!l
230
$opts->{"eol"} or $HTTP::Promise::Entity::BOUNDARY_DELIMITER
l!l&&r!l&&!r
2210
MC/DC0%0/2
$opts->{'eol'} || $HTTP::Promise::Entity::BOUNDARY_DELIMITER || "\r\n"
$opts->{"eol"} $HTTP::Promise::Entity::BOUNDARY_DELIMITER
Truth table66%2/3
A or B
A= $opts->{"eol"}B= $HTTP::Promise::Entity::BOUNDARY_DELIMITER
ABresult
1X1
011
000
1568  
1569 # Multipart... form-data or mixed
1570 23 ▶ if( defined( $toptype ) && $toptype eq 'multipart' )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $toptype and $toptype eq 'multipart') { } 3 20 23
elsif (not $self->parts->is_empty) { } 0 20 20
Condition100%3/3
defined $toptype and $toptype eq "multipart"
!ll&&!rl&&r
7133
MC/DC100%2/2
defined $toptype and $toptype eq "multipart"
defined $toptype $toptype eq "multipart"
Truth table100%3/3
A and B
A= defined $toptypeB= $toptype eq "multipart"
ABresult
0X0
100
111
1571 {
1572 3 my $boundary = $self->_prepare_multipart_headers();
1573  
1574 # Preamble. I do not think there should be any anyway for HTTP multipart
1575 3 my $plines = $self->preamble;
1576 3 ▶ if( defined( $plines ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $plines) 3 0 3
1577 {
1578 # Defined, so output the preamble if it exists (avoiding additional
1579 # newline as per ticket 60931)
1580 3 ▶ $out->print( join( $crlf, @$plines ) . $crlf ) if( @$plines > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if @$plines > 0 0 3 3
1581 }
1582 # Otherwise, no preamble.
1583  
1584 # Parts
1585 3 foreach my $part ( $self->parts->list )
1586 {
1587 12 ▶ $out->print( "--${boundary}${crlf}" ) ||
Branch: 1 branch
BranchTrueFalseTotal
$out->print("--$boundary$crlf") or return $self->error("Unable to print request body to filehandle provided '${out}': $!") 12 0 12
1588 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1589 12 ▶ $part->print( $out ) ||
Branch: 1 branch
BranchTrueFalseTotal
$part->print($out) or return $self->error("Unable to print request body to filehandle provided '${out}': $!") 12 0 12
1590 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1591 # Trailing CRLF
1592 12 ▶ $out->print( $crlf ) ||
Branch: 1 branch
BranchTrueFalseTotal
$out->print($crlf) or return $self->error("Unable to print request body to filehandle provided '${out}': $!") 12 0 12
1593 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1594 }
1595 3 ▶ $out->print( "--${boundary}--${crlf}" ) ||
Branch: 1 branch
BranchTrueFalseTotal
$out->print("--$boundary--$crlf") or return $self->error("Unable to print request body to filehandle provided '${out}': $!") 3 0 3
1596 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1597  
1598 # Epilogue
1599 3 my $epilogue = $self->epilogue;
1600 3 ▶ if( defined( $epilogue ) && !$epilogue->is_empty )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $epilogue and not $epilogue->is_empty) 0 3 3
Condition33%1/3
defined $epilogue and not $epilogue->is_empty
!ll&&!rl&&r
030
MC/DC0%0/2
defined $epilogue and not $epilogue->is_empty
defined $epilogue not $epilogue->is_empty
Truth table33%1/3
A and not(B)
A= defined $epilogueB= not $epilogue->is_empty
ABresult
0X0
100
111
1601 {
1602 0 ▶ $out->print( $epilogue->join( $crlf )->scalar ) ||
Branch: 1 branch
BranchTrueFalseTotal
$out->print($epilogue->join($crlf)->scalar) or return $self->error("Unable to print request body to filehandle provided '${out}': $!") 0 0 0
1603 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1604 0 ▶ if( $epilogue !~ /(?:\015?\012)\Z/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($epilogue =~ /(?:\015?\012)\Z/) 0 0 0
1605 {
1606 0 ▶ $out->print( $crlf ) ||
Branch: 1 branch
BranchTrueFalseTotal
$out->print($crlf) or return $self->error("Unable to print request body to filehandle provided '${out}': $!") 0 0 0
1607 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1608 }
1609 }
1610 }
1611 # Singlepart type with parts...
1612 # This makes $ent->print handle message/rfc822 bodies
1613 # when parse_nested_messages('NEST') is on [idea by Marc Rouleau].
1614 elsif( !$self->parts->is_empty )
1615 {
1616 0 my $need_sep = 0;
1617 0 my $part;
1618 0 my $parts = $self->parts;
1619 # foreach $part ( $self->parts->list )
1620 0 foreach $part ( @$parts )
1621 {
1622 0 ▶ if( $need_sep++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($need_sep++) 0 0 0
1623 {
1624 0 ▶ $out->print( "${crlf}${crlf}" ) ||
Branch: 1 branch
BranchTrueFalseTotal
$out->print("$crlf$crlf") or return $self->error("Unable to print request body to filehandle provided '${out}': $!") 0 0 0
1625 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1626 }
1627 0 ▶ $part->print( $out ) ||
Branch: 1 branch
BranchTrueFalseTotal
$part->print($out) or return $self->error("Unable to print request body to filehandle provided '${out}': $!") 0 0 0
1628 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1629 }
1630 }
1631 # Singlepart type, or no parts: output body...
1632 else
1633 {
1634 20 ▶ if( $self->body )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->body) 17 3 20
1635 {
1636 17 ▶ $self->print_bodyhandle( $out, ( scalar( keys( %$opts ) ) ? $opts : () ) ) ||
Branch: 2 branches
BranchTrueFalseTotal
$self->print_bodyhandle($out, scalar keys %$opts ? $opts : ()) or return $self->pass_error 17 0 17
scalar keys %$opts ? : 17 0 17
1637 return( $self->pass_error );
1638 }
1639 }
1640 23 return( $self );
1641 }
1642  
1643 sub print_bodyhandle
1644 17 ▶{
Subroutine print_bodyhandle: called CC=16 SCAR=32.4
Pod documented
1645 17 my $self = shift( @_ );
1646 17 my $out = shift( @_ );
1647 17 my $opts = $self->_get_args_as_hash( @_ );
1648 17 ▶ $out ||= select;
Condition33%1/3
$out ||= select
l!l&&r!l&&!r
1700
MC/DC0%0/2
$out ||= select
$out select
Truth table33%1/3
A ||= B
A= $outB= select
ABresult
1X1
011
000
1649 17 ▶ return( $self->error( "Filehandle provided ($out) is not a proper filehandle and its not a HTTP::Promise::IO object." ) ) if( !$self->_is_glob( $out ) && !$self->_is_a( $out => 'HTTP::Promise::IO' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_glob($out) or $self->_is_a($out, "HTTP::Promise::IO") 0 17 17
Condition33%1/3
$self->_is_glob($out) or $self->_is_a($out, "HTTP::Promise::IO")
l!l&&r!l&&!r
0170
MC/DC0%0/2
$self->_is_glob($out) or $self->_is_a($out, "HTTP::Promise::IO")
$self->_is_glob($out) $self->_is_a($out, "HTTP::Promise::IO")
Truth table33%1/3
A or B
A= $self->_is_glob($out)B= $self->_is_a($out, "HTTP::Promise::IO")
ABresult
1X1
011
000
1650  
1651 17 my $encoding = $self->headers->content_encoding;
1652 17 ▶ if( $encoding &&
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding and not $self->is_encoded and !exists $opts->{"no_encode"} || exists $opts->{"no_encode"} && !$opts->{"no_encode"}) 6 11 17
Condition50%6/12
$encoding and not $self->is_encoded and !exists $opts->{"no_encode"} || exists $opts->{"no_encode"} && !$opts->{"no_encode"}
!ll&&!rl&&r
1106
$encoding and not $self->is_encoded
!ll&&!rl&&r
926
not exists $opts->{"no_encode"} or exists $opts->{"no_encode"} and not $opts->{"no_encode"}
l!l&&r!l&&!r
600
exists $opts->{"no_encode"} and not $opts->{"no_encode"}
!ll&&!rl&&r
000
MC/DC40%2/5
$encoding and not $self->is_encoded and !exists $opts->{"no_encode"} || exists $opts->{"no_encode"} && !$opts->{"no_encode"}
$encoding not $self->is_encoded not exists $opts->{"no_encode"} exists $opts->{"no_encode"} not $opts->{"no_encode"}
Truth table50%3/6
A and not(B) and not(C) or D and not(E)
A= $encodingB= not $self->is_encodedC= not exists $opts->{"no_encode"}D= exists $opts->{"no_encode"}E= not $opts->{"no_encode"}
ABCDEresult
0XXXX0
10XXX0
1100X0
110100
111XX1
110111
1653 !$self->is_encoded &&
1654 ( !exists( $opts->{no_encode} ) ||
1655 ( exists( $opts->{no_encode} ) && !$opts->{no_encode} )
1656 ) )
1657 {
1658 6 ▶ $self->encode_body( $encoding ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->encode_body($encoding) or return $self->pass_error 6 0 6
1659 6 $self->is_encoded(1);
1660 }
1661 17 my $params = {};
1662 17 ▶ $params->{binmode} = $opts->{binmode} if( exists( $opts->{binmode} ) && $opts->{binmode} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"binmode"} and $opts->{"binmode"} 0 17 17
Condition33%1/3
exists $opts->{"binmode"} and $opts->{"binmode"}
!ll&&!rl&&r
1700
MC/DC0%0/2
exists $opts->{"binmode"} and $opts->{"binmode"}
exists $opts->{"binmode"} $opts->{"binmode"}
Truth table33%1/3
A and B
A= exists $opts->{"binmode"}B= $opts->{"binmode"}
ABresult
0X0
100
111
1663 # An opportunity here to specify the io layer, such as utf-8
1664 17 ▶ my $io = $self->open( 'r', ( scalar( keys( %$params ) ) ? $params : () ) ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$params ? : 0 17 17
Condition50%1/2
$self->open('r', scalar keys %$params ? $params : ()) || (return $self->pass_error)
l!l
170
MC/DC0%0/1
$self->open('r', scalar keys %$params ? $params : ()) || (return $self->pass_error)
$self->open('r', scalar keys %$params ? $params : ())
Truth table50%1/2
A
A= $self->open('r', scalar keys %$params ? $params : ())
Aresult
11
00
1665 17 my $buff;
1666 17 ▶ while( $io->read( $buff, 8192 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($io->read($buff, 8192)) 45 17 62
1667 {
1668 45 ▶ $out->print( $buff ) ||
Branch: 1 branch
BranchTrueFalseTotal
$out->print($buff) or return $self->error("Unable to print request body to filehandle provided '${out}': $!") 45 0 45
1669 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1670 }
1671 17 $io->close;
1672 17 return( $self );
1673 }
1674  
1675 22 ▶sub print_header { shift->headers->print( @_ ); }
Subroutine print_header: called CC=1 SCAR=0.0
Pod documented
1676  
1677 # NOTE: An entity is encapsulated inside either a request or a response.
1678 # See rfc7230, section 3.1 <https://tools.ietf.org/html/rfc7230#section-3.1>
1679 sub print_start_line
1680 22 ▶{
Subroutine print_start_line: called CC=7 SCAR=20.9
Pod documented
1681 22 my $self = shift( @_ );
1682 22 my $out = shift( @_ );
1683 22 ▶ $out ||= select;
Condition33%1/3
$out ||= select
l!l&&r!l&&!r
2200
MC/DC0%0/2
$out ||= select
$out select
Truth table33%1/3
A ||= B
A= $outB= select
ABresult
1X1
011
000
1684 22 ▶ return( $self->error( "Filehandle provided ($out) is not a proper filehandle and its not a HTTP::Promise::IO object." ) ) if( !$self->_is_glob( $out ) && !$self->_is_a( $out => 'HTTP::Promise::IO' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_glob($out) or $self->_is_a($out, "HTTP::Promise::IO") 0 22 22
Condition33%1/3
$self->_is_glob($out) or $self->_is_a($out, "HTTP::Promise::IO")
l!l&&r!l&&!r
0220
MC/DC0%0/2
$self->_is_glob($out) or $self->_is_a($out, "HTTP::Promise::IO")
$self->_is_glob($out) $self->_is_a($out, "HTTP::Promise::IO")
Truth table33%1/3
A or B
A= $self->_is_glob($out)B= $self->_is_a($out, "HTTP::Promise::IO")
ABresult
1X1
011
000
1685 22 my $opts = $self->_get_args_as_hash( @_ );
1686 22 ▶ my $eol = $opts->{eol} || CRLF;
Condition50%1/2
$opts->{'eol'} || "\r\n"
l!l
220
MC/DC0%0/1
$opts->{'eol'} || "\r\n"
$opts->{'eol'}
Truth table50%1/2
A
A= $opts->{'eol'}
Aresult
11
00
1687 22 ▶ if( my $msg = $self->http_message )
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $self->http_message) 3 19 22
1688 {
1689 3 my $sl = $msg->start_line;
1690 3 ▶ return( $self ) unless( length( $sl ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $sl 0 3 3
1691 3 $out->print( $sl . $eol );
1692 }
1693 22 return( $self );
1694 }
1695  
1696 sub purge
1697 0 ▶{
Subroutine purge: not called CC=3 SCAR=24.8
Pod documented
1698 0 my $self = shift( @_ );
1699 # purge me
1700 0 ▶ $self->body->purge if( $self->body );
Branch: 1 branch
BranchTrueFalseTotal
if $self->body 0 0 0
1701 # recurse
1702 0 $_->purge for( $self->parts->list );
1703 0 return( $self );
1704 }
1705  
1706 sub save_file
1707 0 ▶{
Subroutine save_file: not called CC=27 SCAR=66.3
Pod documented
1708 0 my $self = shift( @_ );
1709 0 my $fname = shift( @_ );
1710 0 my $type = $self->headers->type;
1711 0 ▶ return( '' ) if( lc( substr( $type, 0, 10 ) ) eq 'multipart/' );
Branch: 1 branch
BranchTrueFalseTotal
if lc substr($type, 0, 10) eq "multipart/" 0 0 0
1712 0 ▶ unless( defined( $fname ) && length( "$fname" ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fname and length "$fname") 0 0 0
Condition0%0/3
defined $fname and length "$fname"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fname and length "$fname"
defined $fname length "$fname"
Truth table0%0/3
A and B
A= defined $fnameB= length "$fname"
ABresult
0X0
100
111
1713 {
1714 0 my $headers = $self->headers;
1715 0 ▶ if( my $val = $headers->content_disposition )
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $headers->content_disposition) 0 0 0
1716 {
1717 0 my $cd = $headers->new_field( 'Content-Disposition' => "$val" );
1718 0 ▶ return( $self->pass_error( $headers->error ) ) if( !defined( $cd ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cd 0 0 0
1719 0 ▶ if( my $orig_name = $cd->filename )
Branch: 1 branch
BranchTrueFalseTotal
if (my $orig_name = $cd->filename) 0 0 0
1720 {
1721 0 my $f = $self->new_file( $orig_name );
1722 0 my $ext = $f->extension;
1723 0 ▶ my $base = $f->basename( ( defined( $ext ) && length( $ext ) ) ? $ext : () );
Branch: 1 branch
BranchTrueFalseTotal
defined $ext && length $ext ? : 0 0 0
Condition0%0/3
defined $ext and length $ext
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ext and length $ext
defined $ext length $ext
Truth table0%0/3
A and B
A= defined $extB= length $ext
ABresult
0X0
100
111
1724
1725 0 my @unsafe = map( quotemeta( $_ ), qw/ < > “ ‘ % ; ) ( & + $ [ ] : ./ );
1726 0 push( @unsafe, "\r", "\n", ' ', '/' );
1727 0 $base =~ s/(?<!\\)\.\.(?!\.)//g;
1728 0 local $" = '|';
1729 0 $base =~ s/(@unsafe)//g;
1730 0 ▶ unless( $ext )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ext) 0 0 0
1731 {
1732 # Guessing extension
1733 0 ▶ my $mime_type = $headers->mime_type( $DEFAULT_MIME_TYPE || 'application/octet-stream' );
Condition0%0/2
$HTTP::Promise::Entity::DEFAULT_MIME_TYPE || 'application/octet-stream'
l!l
00
MC/DC0%0/1
$HTTP::Promise::Entity::DEFAULT_MIME_TYPE || 'application/octet-stream'
$HTTP::Promise::Entity::DEFAULT_MIME_TYPE
Truth table0%0/2
A
A= $HTTP::Promise::Entity::DEFAULT_MIME_TYPE
Aresult
11
00
1734 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
1735 0 my $mime = HTTP::Promise::MIME->new;
1736 0 $ext = $mime->suffix( $mime_type );
1737 0 ▶ return( $self->pass_error( $mime->error ) ) if( !defined( $ext ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext 0 0 0
1738 }
1739 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
1740 0 ▶ $self->_load_class( 'Module::Generic::File' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Module::Generic::File") or return $self->pass_error 0 0 0
1741 0 ▶ my $output_dir = $self->outputdir || Module::Generic::File->sys_tmpdir;
Condition0%0/3
$self->outputdir || 'Module::Generic::File'->sys_tmpdir
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->outputdir || 'Module::Generic::File'->sys_tmpdir
$self->outputdir 'Module::Generic::File'->sys_tmpdir
Truth table0%0/3
A || B
A= $self->outputdirB= 'Module::Generic::File'->sys_tmpdir
ABresult
1X1
011
000
1742 0 $fname = $output_dir->child( join( '.', $base, $ext ) );
1743 }
1744 }
1745
1746 0 ▶ if( !defined( $fname ) || !length( $fname ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fname and length $fname) 0 0 0
Condition0%0/3
defined $fname and length $fname
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fname and length $fname
defined $fname length $fname
Truth table0%0/3
A and B
A= defined $fnameB= length $fname
ABresult
0X0
100
111
1747 {
1748 # Guessing extension
1749 0 ▶ my $mime_type = $headers->mime_type( $DEFAULT_MIME_TYPE || 'application/octet-stream' );
Condition0%0/2
$HTTP::Promise::Entity::DEFAULT_MIME_TYPE || 'application/octet-stream'
l!l
00
MC/DC0%0/1
$HTTP::Promise::Entity::DEFAULT_MIME_TYPE || 'application/octet-stream'
$HTTP::Promise::Entity::DEFAULT_MIME_TYPE
Truth table0%0/2
A
A= $HTTP::Promise::Entity::DEFAULT_MIME_TYPE
Aresult
11
00
1750 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
1751 0 my $mime = HTTP::Promise::MIME->new;
1752 0 my $ext = $mime->suffix( $mime_type );
1753 0 ▶ return( $self->pass_error( $mime->error ) ) if( !defined( $ext ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext 0 0 0
1754 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
1755 0 $fname = $self->new_tempfile( extension => $ext );
1756 }
1757 }
1758 0 ▶ if( my $enc = $self->headers->content_encoding )
Branch: 1 branch
BranchTrueFalseTotal
if (my $enc = $self->headers->content_encoding) 0 0 0
1759 {
1760 0 ▶ $self->decode_body( $enc ) if( $self->is_encoded );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_encoded 0 0 0
1761 }
1762 0 ▶ my $f = $self->_is_a( $fname => 'Module::Generic::File' ) ? $fname : $self->new_file( "$fname" );
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_a($fname, 'Module::Generic::File') ? : 0 0 0
1763 0 ▶ my $io = $f->open( '+>', { binmode => 'raw', autoflush => 1 } ) ||
Condition0%0/2
$f->open('+>', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($f->error))
l!l
00
MC/DC0%0/1
$f->open('+>', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($f->error))
$f->open('+>', {'binmode', 'raw', 'autoflush', 1})
Truth table0%0/2
A
A= $f->open('+>', {'binmode', 'raw', 'autoflush', 1})
Aresult
11
00
1764 return( $self->pass_error( $f->error ) );
1765 # Pass no_encode to ensure the file does not get automatically encoded
1766 0 ▶ $self->print_body( $io, no_encode => 1 ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->print_body($io, "no_encode", 1) or return $self->pass_error 0 0 0
1767 0 $io->close;
1768 0 return( $f );
1769 }
1770  
1771 0 ▶sub stringify { return( shift->as_string( @_ ) ); }
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
1772  
1773 0 ▶sub stringify_body { return( shift->body_as_string( @_ ) ); }
Subroutine stringify_body: not called CC=1 SCAR=6.9
Pod documented
1774  
1775 0 ▶sub stringify_header { return( shift->headers->as_string( @_ ) ); }
Subroutine stringify_header: not called CC=1 SCAR=6.9
Pod documented
1776  
1777 sub suggest_encoding
1778 3 ▶{
Subroutine suggest_encoding: called CC=14 SCAR=41.7
Pod documented
1779 3 my $self = shift( @_ );
1780 3 my $mime_type = $self->effective_type;
1781 3 my $toptype;
1782 3 ▶ $toptype = [split( '/', $mime_type, 2 )]->[0] if( defined( $mime_type ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $mime_type 3 0 3
1783 # Defaults to 200Kb
1784 3 my $threshold = $self->compression_min;
1785 3 my $rule = {qw(
1786 text/css gzip
1787 text/html gzip
1788 text/plain gzip
1789 text/x-component gzip
1790 application/atom+xml gzip
1791 application/javascript gzip
1792 application/json gzip
1793 application/pdf none
1794 application/rss+xml gzip
1795 application/vnd.ms-fontobject gzip
1796 application/x-font-opentype gzip
1797 application/x-font-ttf gzip
1798 application/x-javascript gzip
1799 application/x-web-app-manifest+json gzip
1800 application/xhtml+xml gzip
1801 application/xml gzip
1802 application/gzip none
1803 font/opentype gzip
1804 image/gif none
1805 image/jpeg none
1806 image/png none
1807 image/svg+xml gzip
1808 image/webp none
1809 image/x-icon none
1810 audio/mpeg none
1811 video/mp4 none
1812 audio/webm none
1813 video/webm none
1814 font/otf gzip
1815 font/ttf gzip
1816 font/woff2 none
1817
1818 )};
1819 # Already usually quite compressed, not much benefit compared to CPU penalty; we are
1820 # not in 1998 anymore :)
1821 # <http://web.archive.org/web/20190708231140/http://www.ibm.com/developerworks/web/library/wa-httpcomp/>
1822 # Also small files, like less than 1,500 bytes are a waste o time due to MTU max size
1823 # (https://en.wikipedia.org/wiki/Maximum_transmission_unit)
1824 # See also <https://httpd.apache.org/docs/2.4/mod/mod_deflate.html>
1825 # <https://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits>
1826 3 ▶ if( exists( $rule->{ $mime_type } ) )
Branch: 3 branches
BranchTrueFalseTotal
if (exists $rule->{$mime_type}) { } 3 0 3
elsif ($toptype eq 'image' or $toptype eq 'video' or $toptype eq 'audio' or $toptype eq 'multipart') { } 0 0 0
elsif ($toptype eq 'text' or $self->is_binary) { } 0 0 0
1827 {
1828 3 ▶ return( '' ) if( $rule->{ $mime_type } eq 'none' );
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{$mime_type} eq "none" 1 2 3
1829 2 ▶ return( $rule->{ $mime_type } ) if( !$threshold || $self->body->length >= $threshold );
Branch: 1 branch
BranchTrueFalseTotal
if not $threshold or $self->body->length >= $threshold 0 2 2
Condition33%1/3
not $threshold or $self->body->length >= $threshold
l!l&&r!l&&!r
002
MC/DC0%0/2
not $threshold or $self->body->length >= $threshold
not $threshold $self->body->length >= $threshold
Truth table33%1/3
not(A) or B
A= not $thresholdB= $self->body->length >= $threshold
ABresult
1X1
011
000
1830 }
1831 ▶ elsif( $toptype eq 'image' ||
Condition0%0/9
$toptype eq "image" or $toptype eq "video" or $toptype eq "audio" or $toptype eq "multipart"
l!l&&r!l&&!r
000
$toptype eq "image" or $toptype eq "video" or $toptype eq "audio"
l!l&&r!l&&!r
000
$toptype eq "image" or $toptype eq "video"
l!l&&r!l&&!r
000
MC/DC0%0/4
$toptype eq "image" or $toptype eq "video" or $toptype eq "audio" or $toptype eq "multipart"
$toptype eq "image" $toptype eq "video" $toptype eq "audio" $toptype eq "multipart"
Truth table0%0/5
A or B or C or D
A= $toptype eq "image"B= $toptype eq "video"C= $toptype eq "audio"D= $toptype eq "multipart"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
1832 $toptype eq 'video' ||
1833 $toptype eq 'audio' ||
1834 $toptype eq 'multipart' )
1835 {
1836 0 return( '' );
1837 }
1838 ▶ elsif( $toptype eq 'text' || $self->is_binary )
Condition0%0/3
$toptype eq "text" or $self->is_binary
l!l&&r!l&&!r
000
MC/DC0%0/2
$toptype eq "text" or $self->is_binary
$toptype eq "text" $self->is_binary
Truth table0%0/3
A or B
A= $toptype eq "text"B= $self->is_binary
ABresult
1X1
011
000
1839 {
1840 # Suggest gzip compression if it exceeds 200Kb
1841 0 ▶ return( 'gzip' ) if( !$threshold || $self->body->length >= $threshold );
Branch: 1 branch
BranchTrueFalseTotal
if not $threshold or $self->body->length >= $threshold 0 0 0
Condition0%0/3
not $threshold or $self->body->length >= $threshold
l!l&&r!l&&!r
000
MC/DC0%0/2
not $threshold or $self->body->length >= $threshold
not $threshold $self->body->length >= $threshold
Truth table0%0/3
not(A) or B
A= not $thresholdB= $self->body->length >= $threshold
ABresult
1X1
011
000
1842 }
1843 2 return( '' );
1844 }
1845  
1846 sub textual_type
1847 0 ▶{
Subroutine textual_type: not called CC=2 SCAR=17.9
Pod documented
1848 0 my $self = shift( @_ );
1849 0 ▶ return( $_[0] =~ m{^(text|message)(/|\Z)}i ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ m[^(text|message)(/|\Z)]i ? : 0 0 0
1850 }
1851  
1852 4 ▶sub _parts { return( shift->_set_get_array_as_object( '_parts', @_ ) ); }
Subroutine _parts: called CC=1 SCAR=0.0
1853  
1854 # NOTE: Used in both print_body() and dump()
1855 sub _prepare_multipart_headers
1856 3 ▶{
Subroutine _prepare_multipart_headers: called CC=14 SCAR=34.2
1857 3 my $self = shift( @_ );
1858 3 my $mime_type = $self->mime_type;
1859 3 my $toptype;
1860 3 ▶ $toptype = [split( '/', lc( $mime_type ), 2 )]->[0] if( defined( $mime_type ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $mime_type 3 0 3
1861 3 my $boundary = $self->headers->multipart_boundary;
1862 # Ensure we have a boundary set.
1863 # This is the same code as in HTTP::Promise::Headers::as_string, but since
1864 # print_body() may be called separately, we need to check here too if a boundary
1865 # has been set.
1866 3 ▶ unless( $boundary )
Branch: 1 branch
BranchTrueFalseTotal
unless ($boundary) 0 3 3
1867 {
1868 0 $boundary = $self->make_boundary;
1869 0 my $ct = $self->headers->new_field( 'Content-Type' => $self->headers->content_type );
1870 0 $ct->boundary( $boundary );
1871 0 $self->headers->content_type( "$ct" );
1872 }
1873 # Parts
1874 # For reporting to the caller only when there are some issues.
1875 3 my $n = 0;
1876 # for generated part name, by default
1877 3 my $auto_name = 'part0';
1878 3 foreach my $part ( $self->parts->list )
1879 {
1880 12 ++$n;
1881 # If this is a multipart/form-data, ensure we have a part name, or isse a warning
1882 12 my $name;
1883 12 ▶ if( $mime_type eq 'multipart/form-data' )
Branch: 2 branches
BranchTrueFalseTotal
if ($mime_type eq 'multipart/form-data') { } 4 8 12
elsif ($mime_type eq 'multipart/mixed') { } 8 0 8
1884 {
1885 4 $name = $part->name;
1886 4 ▶ if( !$name )
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 4 4
1887 {
1888 0 ▶ warn( "Warning: no part name set for this part No. ${n}\n" ) if( warnings::enabled( 'HTTP::Promise' ) );
Branch: 1 branch
BranchTrueFalseTotal
if warnings::enabled("HTTP::Promise") 0 0 0
1889 0 $name = ++$auto_name;
1890 0 $part->name( $name );
1891 }
1892 }
1893 elsif( $mime_type eq 'multipart/mixed' )
1894 {
1895 # remove any Content-Disposition used for multipart/form-data
1896 8 $part->headers->remove( 'Content-Disposition' );
1897 }
1898
1899 12 ▶ if( defined( $name ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 4 8 12
1900 {
1901 4 my $content_disposition = $part->headers->content_disposition;
1902 4 ▶ if( defined( $content_disposition ) && $content_disposition->length )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content_disposition and $content_disposition->length) { } 4 0 4
Condition33%1/3
defined $content_disposition and $content_disposition->length
!ll&&!rl&&r
040
MC/DC0%0/2
defined $content_disposition and $content_disposition->length
defined $content_disposition $content_disposition->length
Truth table33%1/3
A and B
A= defined $content_dispositionB= $content_disposition->length
ABresult
0X0
100
111
1903 {
1904 # A simple check to save time from generating the Content-Disposition object
1905 4 ▶ if( $content_disposition->index( 'name=' ) == -1 ||
Branch: 1 branch
BranchTrueFalseTotal
if ($content_disposition->index("name=") == -1 or $content_disposition->index("form-data") == -1) 0 4 4
Condition33%1/3
$content_disposition->index("name=") == -1 or $content_disposition->index("form-data") == -1
l!l&&r!l&&!r
004
MC/DC0%0/2
$content_disposition->index("name=") == -1 or $content_disposition->index("form-data") == -1
$content_disposition->index("name=") == -1 $content_disposition->index("form-data") == -1
Truth table33%1/3
A or B
A= $content_disposition->index("name=") == -1B= $content_disposition->index("form-data") == -1
ABresult
1X1
011
000
1906 $content_disposition->index( 'form-data' ) == -1 )
1907 {
1908 0 my $cd = $part->headers->new_field( 'Content-Disposition' => $part->headers->content_disposition );
1909 0 ▶ $cd->name( $name ) if( !length( $cd->name ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $cd->name 0 0 0
1910 0 $cd->disposition( 'form-data' );
1911 }
1912 }
1913 else
1914 {
1915 0 $part->headers->content_disposition( qq{form-data; name="${name}"} );
1916 }
1917 }
1918 }
1919 3 return( $boundary );
1920 }
1921  
1922 # NOTE: sub FREEZE is inherited
1923 sub FREEZE
1924 8 ▶{
Subroutine FREEZE: called CC=5 SCAR=16.7
Pod undocumented
1925 8 my $self = CORE::shift( @_ );
1926 8 ▶ my $serialiser = CORE::shift( @_ ) // '';
Condition50%1/2
shift @_ // ""
l!l
80
MC/DC0%0/1
shift @_ // ""
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
1927 8 my $class = CORE::ref( $self );
1928 8 my $ref = $self->_obj2h;
1929 8 my %hash = %$ref;
1930 # We remove this to prevent a circular reference that CBOR::XS does not seem to be managing
1931 # This relation is re-created in HTTP::Promise::Message::THAW
1932 # It is safe to remove it, because 1) if it is a standalone HTTP::Promise::Entity object,
1933 # then it would not be set anyway, and 2) if it is part of an HTTP::Promise::Message, it
1934 # is going to be recreated.
1935 8 ▶ CORE::delete( @hash{ qw( http_message ) } ) unless( $serialiser ne 'CBOR' );
Branch: 1 branch
BranchTrueFalseTotal
unless $serialiser ne "CBOR" 0 8 8
1936 # Return an array reference rather than a list so this works with Sereal and CBOR
1937 8 ▶ CORE::return( [$class, \%hash] ) if( $serialiser eq 'Sereal' && Sereal::Encoder->VERSION <= version->parse( '4.023' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $serialiser eq "Sereal" and "Sereal::Encoder"->VERSION <= "version"->parse("4.023") 0 8 8
Condition66%2/3
$serialiser eq "Sereal" and "Sereal::Encoder"->VERSION <= "version"->parse("4.023")
!ll&&!rl&&r
440
MC/DC0%0/2
$serialiser eq "Sereal" and "Sereal::Encoder"->VERSION <= "version"->parse("4.023")
$serialiser eq "Sereal" "Sereal::Encoder"->VERSION <= "version"->parse("4.023")
Truth table66%2/3
A and B
A= $serialiser eq "Sereal"B= "Sereal::Encoder"->VERSION <= "version"->parse("4.023")
ABresult
0X0
100
111
1938 # But Storable want a list with the first element being the serialised element
1939 8 CORE::return( $class, \%hash );
1940 }
1941  
1942 4 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
1943  
1944 4 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
1945  
1946 # NOTE: sub THAW is inherited
1947  
1948 10 1;
1949 # NOTE: POD
1950 __END__
« Promise/Headers.pm ↑ Summary Promise/IO.pm »