lib/HTTP/Promise/Entity.pm

filter: Statementstmt 51.8% 502 / 968 Branchbran 32.0% 225 / 702 Conditioncond 31.2% 131 / 419 MC/DCmcdc 7.1% 17 / 237 Subroutinesub 77.3% 58 / 75 Podpod 94.7% 54 / 57 totaltotal 40.1% 987 / 2458 CC 530 SCAR 58.6
CC
530
Cov
41%
dump
1640.0
decode_body
1071.9
save_file
756.0
CRAP 349.6
« lib/HTTP/Promise/Stream.pm ↑ Summary blib/lib/HTTP/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 {
17 11 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 11 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 11 ▶ warnings::register_categories( 'HTTP::Promise' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 11 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 11 use vars qw( $VERSION $EXCEPTION_CLASS $BOUNDARY_DELIMITER $BOM2ENC $ENC2BOM $BOM_RE
22 11 ▶ $BOM_MAX_LENGTH $DEFAULT_MIME_TYPE );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 11 ▶ use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 11 ▶ use HTTP::Promise::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 11 ▶ use HTTP::Promise::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 11 ▶ use HTTP::Promise::Body;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 11 ▶ use Module::Generic::HeaderValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 # use Nice::Try;
29 11 ▶ use Symbol;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 11 ▶ use URI::Escape::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
31 11 ▶ use constant CRLF => "\015\012";
Subroutine BEGIN: called CC=1 SCAR=0.0
32 11 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
33 11 our $BOUNDARY_DELIMITER = "\015\012";
34 11 our $DEFAULT_MIME_TYPE = 'application/octet-stream';
35 11 our $VERSION = 'v0.2.2';
36 };
37  
38 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
40  
41 sub init
42 {
43 80 ▶ my $self = shift( @_ );
Subroutine init: called CC=2 SCAR=6.9
Pod documented
44 80 $self->{body} = undef;
45 # Sie minimum from which compression is enabled, if mime type is suitable.
46 # Defaults to 200Kb
47 80 $self->{compression_min}= 204800;
48 80 $self->{effective_type} = undef;
49 80 $self->{epilogue} = undef;
50 80 $self->{ext_vary} = undef;
51 80 $self->{headers} = undef;
52 80 $self->{is_encoded} = 0;
53 80 $self->{output_dir} = undef;
54 80 $self->{preamble} = undef;
55 80 $self->{_init_strict_use_sub} = 1;
56 80 $self->{_exception_class} = $EXCEPTION_CLASS;
57 80 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 80 80
58 80 $self->{_parts} = [];
59 80 return( $self );
60 }
61  
62 sub add_part
63 {
64 4 ▶ my $self = shift( @_ );
Subroutine add_part: called CC=4 SCAR=14.2
Pod documented
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 {
76 1 ▶ my $self = shift( @_ );
Subroutine as_form_data: called CC=15 SCAR=32.1
Pod documented
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 0 1 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 {
125 8 ▶ my $self = shift( @_ );
Subroutine as_string: called CC=11 SCAR=31.4
Pod documented
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 0 8 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 {
165 3 ▶ my $self = shift( @_ );
Subroutine attach: called CC=4 SCAR=15.2
Pod documented
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 0 3 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 {
177 0 ▶ my $self = shift( @_ );
Subroutine body_as_array: not called CC=6 SCAR=37.4
Pod documented
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 {
195 1 ▶ my $self = shift( @_ );
Subroutine body_as_string: called CC=10 SCAR=32.9
Pod documented
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 0 1 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 {
232 17 ▶ my $self = shift( @_ );
Subroutine build: called CC=60 SCAR=50.0
Pod documented
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 0 12 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 0 3 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
2510
defined $disposition or defined $filename
l!l&&r!l&&!r
1104
MC/DC66%2/3
not $is_multipart and defined $disposition || defined $filename
not $is_multipart defined $disposition defined $filename
Truth table75%3/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 {
388 0 ▶ my $self = shift( @_ );
Subroutine clone: not called CC=12 SCAR=50.5
Pod documented
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 {
445 4 ▶ my $self = shift( @_ );
Subroutine content_charset: called CC=42 SCAR=62.3
Pod documented
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 0 4 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 {
583 4 ▶ my $self = shift( @_ );
Subroutine decode_body: called CC=52 SCAR=69.8
Pod documented
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 0 4 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 {
768 0 ▶ my $self = shift( @_ );
Subroutine dump: not called CC=40 SCAR=74.0
Pod documented
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 {
929 0 ▶ my $self = shift( @_ );
Subroutine dump_skeleton: not called CC=17 SCAR=55.9
Pod documented
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 11 ▶ 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 {
991 3 ▶ my $self = shift( @_ );
Subroutine effective_type: called CC=3 SCAR=13.3
Pod documented
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 {
1001 6 ▶ my $self = shift( @_ );
Subroutine encode_body: called CC=34 SCAR=47.7
Pod documented
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 0 6 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 {
1084 4 ▶ my $self = shift( @_ );
Subroutine guess_character_encoding: called CC=23 SCAR=49.0
Pod documented
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 0 4 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 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 260 ▶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 61 ▶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 {
1157 4 ▶ my $self = shift( @_ );
Subroutine io_encoding: called CC=41 SCAR=59.6
Pod documented
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 0 4 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 {
1263 4 ▶ my $self = shift( @_ );
Subroutine is_binary: called CC=20 SCAR=48.1
Pod documented
1264 4 ▶ $self->_load_class( 'Encode' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("Encode") or return $self->pass_error 0 4 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 {
1336 0 ▶ my $self = shift( @_ );
Subroutine is_body_on_file: not called CC=3 SCAR=24.8
Pod documented
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 {
1344 0 ▶ my $self = shift( @_ );
Subroutine is_body_in_memory: not called CC=3 SCAR=24.8
Pod documented
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 {
1353 4 ▶ my $self = shift( @_ );
Subroutine is_decoded: called CC=2 SCAR=7.4
Pod documented
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 {
1369 3 ▶ my $self = shift( @_ );
Subroutine is_multipart: called CC=5 SCAR=18.1
Pod documented
1370 # no head, so no MIME type!
1371 3 ▶ $self->headers or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->headers or return 0 3 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( Data::UUID->new->create_str ); }
Subroutine make_boundary: called CC=1 SCAR=0.0
Pod documented
1380 # sub make_boundary
1381 # {
1382 # my $self = shift( @_ );
1383 # # my $uuid = $self->_uuid;
1384 # my $uuid = Data::UUID->new;
1385 # my $boundary = $uuid->create_str;
1386 # return( $boundary );
1387 # }
1388  
1389 sub make_multipart
1390 {
1391 3 ▶ my $self = shift( @_ );
Subroutine make_multipart: called CC=8 SCAR=34.7
Pod documented
1392 3 my $subtype = shift( @_ );
1393 3 my $opts = $self->_get_args_as_hash( @_ );
1394 3 my $tag;
1395 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
1396 3 my $force = $opts->{force};
1397  
1398 # Trap for simple case: already a multipart?
1399 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
1400 0 my $headers = $self->headers;
1401
1402  
1403 # Rip out our guts, and spew them into our future part.
1404 # part is a shallow copy
1405 # my $part = bless( {%$self} => ref( $self ) );
1406 # my $part = $self->new(
1407 # headers => $headers->clone,
1408 # ( $self->body ? ( body => $self->body ) : () ),
1409 # debug => $self->debug,
1410 # );
1411 #
1412 # if( my $msg = $self->http_message )
1413 # {
1414 # my $clone = $msg->clone( clone_entity => 0 );
1415 # $clone->entity( $part );
1416 # $part->http_message( $clone );
1417 # }
1418 # $part->parts( $self->parts );
1419
1420 0 my $part = $self->clone;
1421
1422 # my $part = $self->clone;
1423 # lobotomize ourselves!
1424 # %$self = ();
1425 # clone the headers
1426  
1427 # Remove content headers from top-level, and set it up as a multipart
1428 0 my $removed = $headers->remove_content_headers;
1429 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
1430 return( $self->pass_error( $headers->error ) );
1431 0 $ct->boundary( $self->make_boundary );
1432 0 my $ct_string = $ct->as_string;
1433 0 $headers->header( 'Content-Type' => "${ct_string}" );
1434  
1435 # Remove non-content headers from the part
1436 0 $removed = $self->new_array;
1437 0 foreach $tag ( grep{ !/^content-/i } $part->headers->header_field_names )
1438 {
1439 0 $part->headers->delete( $tag );
1440 0 $removed->push( $tag );
1441 }
1442 0 $self->parts->reset;
1443 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
1444 0 return( $self );
1445 }
1446  
1447 sub make_singlepart
1448 {
1449 0 ▶ my $self = shift( @_ );
Subroutine make_singlepart: not called CC=8 SCAR=42.8
Pod documented
1450 # Trap for simple cases:
1451 # already a singlepart?
1452 0 ▶ return( $self ) if( !$self->is_multipart );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->is_multipart 0 0 0
1453 # can this even be done?
1454 0 ▶ return(0) if( $self->parts > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->parts > 1 0 0 0
1455  
1456 # Get rid of all our existing content info
1457 0 my $tag;
1458 0 foreach $tag ( grep{ /^content-/i } $self->headers->header_field_names )
1459 {
1460 0 $self->headers->delete( $tag );
1461 }
1462  
1463 # one part
1464 0 ▶ if( $self->parts->length == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->parts->length == 1) { } 0 0 0
1465 {
1466 0 my $part = $self->parts->index(0);
1467 # Populate ourselves with any content info from the part:
1468 0 foreach $tag ( grep{ /^content-/i } $part->headers->header_field_names )
1469 {
1470 0 $self->headers->add( $tag => $_ ) for( $part->headers->get( $tag ) );
1471 }
1472  
1473 # Save reconstructed headers, replace our guts, and restore header:
1474 0 my $new_head = $self->headers;
1475 # shallow copy is ok!
1476 0 %$self = %$part;
1477 0 $self->headers( $new_head );
1478  
1479 # One more thing: the part *may* have been a multi with 0 or 1 parts!
1480 0 ▶ return( $self->make_singlepart( @_ ) ) if( $self->is_multipart );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_multipart 0 0 0
1481 }
1482 # no parts!
1483 else
1484 {
1485 0 $self->headers->mime_attr( 'Content-type' => 'text/plain' ); ### simple
1486 }
1487 0 return( $self );
1488 }
1489  
1490 sub mime_type
1491 {
1492 41 ▶ my $self = shift( @_ );
Subroutine mime_type: called CC=2 SCAR=7.0
Pod documented
1493 41 my $headers = $self->headers;
1494 41 ▶ return if( !defined( $headers ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $headers 0 41 41
1495 41 return( $headers->mime_type( @_ ) );
1496 }
1497  
1498 # NOTE name() is to associate a name for this entity for multipart/form-data
1499 24 ▶sub name { return( shift->_set_get_scalar_as_object( 'name', @_ ) ); }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
1500  
1501 sub new_body
1502 {
1503 26 ▶ my $self = shift( @_ );
Subroutine new_body: called CC=6 SCAR=18.5
Pod documented
1504 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
1505 26 my $map =
1506 {
1507 file => 'HTTP::Promise::Body::File',
1508 form => 'HTTP::Promise::Body::Form',
1509 scalar => 'HTTP::Promise::Body::Scalar',
1510 string => 'HTTP::Promise::Body::Scalar',
1511 };
1512 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
1513 26 ▶ if( $type eq 'form' )
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq "form") 1 25 26
1514 {
1515 1 ▶ $self->_load_class( $class ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class($class) or return $self->pass_error 0 1 1
1516 }
1517 26 my $body = $class->new( @_ );
1518 26 ▶ return( $self->pass_error( $class->error ) ) if( !defined( $body ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 26 26
1519 26 return( $body );
1520 }
1521  
1522 sub open
1523 {
1524 17 ▶ my $self = shift( @_ );
Subroutine open: called CC=3 SCAR=11.3
Pod documented
1525 17 my $body = $self->body;
1526 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
1527 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
1528 return( $self->pass_error( $body->error ) );
1529 17 return( $io );
1530 }
1531  
1532 0 ▶sub output_dir { return( shift->_set_get_file( 'output_dir', @_ ) ); }
Subroutine output_dir: not called CC=1 SCAR=6.9
Pod documented
1533  
1534 # An array of HTTP::Promise::Entity objects
1535 52 ▶sub parts { return( shift->_set_get_array_as_object( '_parts', @_ ) ); }
Subroutine parts: called CC=1 SCAR=0.0
Pod documented
1536  
1537 7 ▶sub preamble { return( shift->_set_get_array_as_object( 'preamble', @_ ) ); }
Subroutine preamble: called CC=1 SCAR=0.0
Pod documented
1538  
1539 sub print
1540 {
1541 22 ▶ my $self = shift( @_ );
Subroutine print: called CC=13 SCAR=27.5
Pod documented
1542 22 my $out = shift( @_ );
1543 22 my $opts = $self->_get_args_as_hash( @_ );
1544 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
1545 22 ▶ $out = select if( !defined( $out ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $out 1 21 22
1546 22 ▶ $out = Symbol::qualify( $out, scalar( caller ) ) unless( ref( $out ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $out 0 22 22
1547 22 ▶ $self->_load_class( 'HTTP::Promise::IO' ) || return( $self->error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::IO") or return $self->error 0 22 22
1548 22 ▶ my $io = $self->_is_a( $out => 'HTTP::Promise::IO' )
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_a($out, 'HTTP::Promise::IO') ? : 8 14 22
1549 ? $out
1550 : HTTP::Promise::IO->new( $out, debug => $self->debug );
1551 22 ▶ return( $self->pass_error( HTTP::Promise::IO->error ) ) if( !defined( $io ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $io 0 22 22
1552 22 $opts->{eol} = $eol;
1553 # The start-line
1554 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 0 22 22
1555 # The headers
1556 22 ▶ $self->print_header( $io, $opts ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->print_header($io, $opts) or return $self->pass_error 0 22 22
1557 22 ▶ $io->print( $eol ) ||
Branch: 1 branch
BranchTrueFalseTotal
$io->print($eol) or return $self->error("Unable to print to filehandle provided '${io}': $!") 0 22 22
1558 return( $self->error( "Unable to print to filehandle provided '$io': $!" ) );
1559 # The body
1560 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 0 22 22
scalar keys %$opts ? : 22 0 22
1561 22 return( $self );
1562 }
1563  
1564 sub print_body
1565 {
1566 23 ▶ my $self = shift( @_ );
Subroutine print_body: called CC=29 SCAR=46.6
Pod documented
1567 23 my $out = shift( @_ );
1568 23 my $opts = $self->_get_args_as_hash( @_ );
1569 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
1570 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
1571 23 my $mime_type = $self->mime_type;
1572 23 my $toptype;
1573 23 ▶ $toptype = [split( '/', lc( $mime_type ), 2 )]->[0] if( defined( $mime_type ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $mime_type 23 0 23
1574 # my $crlf = $HTTP::Promise::Entity::BOUNDARY_DELIMITER || CRLF;
1575 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
1576  
1577 # Multipart... form-data or mixed
1578 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
1579 {
1580 3 my $boundary = $self->_prepare_multipart_headers();
1581  
1582 # Preamble. I do not think there should be any anyway for HTTP multipart
1583 3 my $plines = $self->preamble;
1584 3 ▶ if( defined( $plines ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $plines) 3 0 3
1585 {
1586 # Defined, so output the preamble if it exists (avoiding additional
1587 # newline as per ticket 60931)
1588 3 ▶ $out->print( join( $crlf, @$plines ) . $crlf ) if( @$plines > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if @$plines > 0 0 3 3
1589 }
1590 # Otherwise, no preamble.
1591  
1592 # Parts
1593 3 foreach my $part ( $self->parts->list )
1594 {
1595 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}': $!") 0 12 12
1596 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1597 12 ▶ $part->print( $out ) ||
Branch: 1 branch
BranchTrueFalseTotal
$part->print($out) or return $self->error("Unable to print request body to filehandle provided '${out}': $!") 0 12 12
1598 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1599 # Trailing CRLF
1600 12 ▶ $out->print( $crlf ) ||
Branch: 1 branch
BranchTrueFalseTotal
$out->print($crlf) or return $self->error("Unable to print request body to filehandle provided '${out}': $!") 0 12 12
1601 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1602 }
1603 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}': $!") 0 3 3
1604 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1605  
1606 # Epilogue
1607 3 my $epilogue = $self->epilogue;
1608 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
1609 {
1610 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
1611 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1612 0 ▶ if( $epilogue !~ /(?:\015?\012)\Z/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($epilogue =~ /(?:\015?\012)\Z/) 0 0 0
1613 {
1614 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
1615 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1616 }
1617 }
1618 }
1619 # Singlepart type with parts...
1620 # This makes $ent->print handle message/rfc822 bodies
1621 # when parse_nested_messages('NEST') is on [idea by Marc Rouleau].
1622 elsif( !$self->parts->is_empty )
1623 {
1624 0 my $need_sep = 0;
1625 0 my $part;
1626 0 my $parts = $self->parts;
1627 # foreach $part ( $self->parts->list )
1628 0 foreach $part ( @$parts )
1629 {
1630 0 ▶ if( $need_sep++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($need_sep++) 0 0 0
1631 {
1632 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
1633 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1634 }
1635 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
1636 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1637 }
1638 }
1639 # Singlepart type, or no parts: output body...
1640 else
1641 {
1642 20 ▶ if( $self->body )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->body) 17 3 20
1643 {
1644 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 0 17 17
scalar keys %$opts ? : 17 0 17
1645 return( $self->pass_error );
1646 }
1647 }
1648 23 return( $self );
1649 }
1650  
1651 sub print_bodyhandle
1652 {
1653 17 ▶ my $self = shift( @_ );
Subroutine print_bodyhandle: called CC=16 SCAR=32.4
Pod documented
1654 17 my $out = shift( @_ );
1655 17 my $opts = $self->_get_args_as_hash( @_ );
1656 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
1657 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
1658  
1659 17 my $encoding = $self->headers->content_encoding;
1660 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
1160
$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/DC0%0/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 table33%2/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
1661 !$self->is_encoded &&
1662 ( !exists( $opts->{no_encode} ) ||
1663 ( exists( $opts->{no_encode} ) && !$opts->{no_encode} )
1664 ) )
1665 {
1666 6 ▶ $self->encode_body( $encoding ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->encode_body($encoding) or return $self->pass_error 0 6 6
1667 6 $self->is_encoded(1);
1668 }
1669 17 my $params = {};
1670 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
1671 # An opportunity here to specify the io layer, such as utf-8
1672 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
1673 17 my $buff;
1674 17 ▶ while( $io->read( $buff, 8192 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($io->read($buff, 8192)) 45 17 62
1675 {
1676 45 ▶ $out->print( $buff ) ||
Branch: 1 branch
BranchTrueFalseTotal
$out->print($buff) or return $self->error("Unable to print request body to filehandle provided '${out}': $!") 0 45 45
1677 return( $self->error( "Unable to print request body to filehandle provided '$out': $!" ) );
1678 }
1679 17 $io->close;
1680 17 return( $self );
1681 }
1682  
1683 22 ▶sub print_header { shift->headers->print( @_ ); }
Subroutine print_header: called CC=1 SCAR=0.0
Pod documented
1684  
1685 # NOTE: An entity is encapsulated inside either a request or a response.
1686 # See rfc7230, section 3.1 <https://tools.ietf.org/html/rfc7230#section-3.1>
1687 sub print_start_line
1688 {
1689 22 ▶ my $self = shift( @_ );
Subroutine print_start_line: called CC=7 SCAR=20.9
Pod documented
1690 22 my $out = shift( @_ );
1691 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
1692 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
1693 22 my $opts = $self->_get_args_as_hash( @_ );
1694 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
1695 22 ▶ if( my $msg = $self->http_message )
Branch: 1 branch
BranchTrueFalseTotal
if (my $msg = $self->http_message) 3 19 22
1696 {
1697 3 my $sl = $msg->start_line;
1698 3 ▶ return( $self ) unless( length( $sl ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $sl 0 3 3
1699 3 $out->print( $sl . $eol );
1700 }
1701 22 return( $self );
1702 }
1703  
1704 sub purge
1705 {
1706 0 ▶ my $self = shift( @_ );
Subroutine purge: not called CC=3 SCAR=24.8
Pod documented
1707 # purge me
1708 0 ▶ $self->body->purge if( $self->body );
Branch: 1 branch
BranchTrueFalseTotal
if $self->body 0 0 0
1709 # recurse
1710 0 $_->purge for( $self->parts->list );
1711 0 return( $self );
1712 }
1713  
1714 sub save_file
1715 {
1716 0 ▶ my $self = shift( @_ );
Subroutine save_file: not called CC=27 SCAR=66.3
Pod documented
1717 0 my $fname = shift( @_ );
1718 0 my $type = $self->headers->type;
1719 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
1720 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
1721 {
1722 0 my $headers = $self->headers;
1723 0 ▶ if( my $val = $headers->content_disposition )
Branch: 1 branch
BranchTrueFalseTotal
if (my $val = $headers->content_disposition) 0 0 0
1724 {
1725 0 my $cd = $headers->new_field( 'Content-Disposition' => "$val" );
1726 0 ▶ return( $self->pass_error( $headers->error ) ) if( !defined( $cd ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cd 0 0 0
1727 0 ▶ if( my $orig_name = $cd->filename )
Branch: 1 branch
BranchTrueFalseTotal
if (my $orig_name = $cd->filename) 0 0 0
1728 {
1729 0 my $f = $self->new_file( $orig_name );
1730 0 my $ext = $f->extension;
1731 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
1732
1733 0 my @unsafe = map( quotemeta( $_ ), qw/ < > “ ‘ % ; ) ( & + $ [ ] : ./ );
1734 0 push( @unsafe, "\r", "\n", ' ', '/' );
1735 0 $base =~ s/(?<!\\)\.\.(?!\.)//g;
1736 0 local $" = '|';
1737 0 $base =~ s/(@unsafe)//g;
1738 0 ▶ unless( $ext )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ext) 0 0 0
1739 {
1740 # Guessing extension
1741 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
1742 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
1743 0 my $mime = HTTP::Promise::MIME->new;
1744 0 $ext = $mime->suffix( $mime_type );
1745 0 ▶ return( $self->pass_error( $mime->error ) ) if( !defined( $ext ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext 0 0 0
1746 }
1747 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
1748 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
1749 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
1750 0 $fname = $output_dir->child( join( '.', $base, $ext ) );
1751 }
1752 }
1753
1754 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
1755 {
1756 # Guessing extension
1757 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
1758 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
1759 0 my $mime = HTTP::Promise::MIME->new;
1760 0 my $ext = $mime->suffix( $mime_type );
1761 0 ▶ return( $self->pass_error( $mime->error ) ) if( !defined( $ext ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ext 0 0 0
1762 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
1763 0 $fname = $self->new_tempfile( extension => $ext );
1764 }
1765 }
1766 0 ▶ if( my $enc = $self->headers->content_encoding )
Branch: 1 branch
BranchTrueFalseTotal
if (my $enc = $self->headers->content_encoding) 0 0 0
1767 {
1768 0 ▶ $self->decode_body( $enc ) if( $self->is_encoded );
Branch: 1 branch
BranchTrueFalseTotal
if $self->is_encoded 0 0 0
1769 }
1770 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
1771 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
1772 return( $self->pass_error( $f->error ) );
1773 # Pass no_encode to ensure the file does not get automatically encoded
1774 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
1775 0 $io->close;
1776 0 return( $f );
1777 }
1778  
1779 0 ▶sub stringify { return( shift->as_string( @_ ) ); }
Subroutine stringify: not called CC=1 SCAR=6.9
Pod documented
1780  
1781 0 ▶sub stringify_body { return( shift->body_as_string( @_ ) ); }
Subroutine stringify_body: not called CC=1 SCAR=6.9
Pod documented
1782  
1783 0 ▶sub stringify_header { return( shift->headers->as_string( @_ ) ); }
Subroutine stringify_header: not called CC=1 SCAR=6.9
Pod documented
1784  
1785 sub suggest_encoding
1786 {
1787 3 ▶ my $self = shift( @_ );
Subroutine suggest_encoding: called CC=14 SCAR=41.7
Pod documented
1788 3 my $mime_type = $self->effective_type;
1789 3 my $toptype;
1790 3 ▶ $toptype = [split( '/', $mime_type, 2 )]->[0] if( defined( $mime_type ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $mime_type 3 0 3
1791 # Defaults to 200Kb
1792 3 my $threshold = $self->compression_min;
1793 3 my $rule = {qw(
1794 text/css gzip
1795 text/html gzip
1796 text/plain gzip
1797 text/x-component gzip
1798 application/atom+xml gzip
1799 application/javascript gzip
1800 application/json gzip
1801 application/pdf none
1802 application/rss+xml gzip
1803 application/vnd.ms-fontobject gzip
1804 application/x-font-opentype gzip
1805 application/x-font-ttf gzip
1806 application/x-javascript gzip
1807 application/x-web-app-manifest+json gzip
1808 application/xhtml+xml gzip
1809 application/xml gzip
1810 application/gzip none
1811 font/opentype gzip
1812 image/gif none
1813 image/jpeg none
1814 image/png none
1815 image/svg+xml gzip
1816 image/webp none
1817 image/x-icon none
1818 audio/mpeg none
1819 video/mp4 none
1820 audio/webm none
1821 video/webm none
1822 font/otf gzip
1823 font/ttf gzip
1824 font/woff2 none
1825
1826 )};
1827 # Already usually quite compressed, not much benefit compared to CPU penalty; we are
1828 # not in 1998 anymore :)
1829 # <http://web.archive.org/web/20190708231140/http://www.ibm.com/developerworks/web/library/wa-httpcomp/>
1830 # Also small files, like less than 1,500 bytes are a waste o time due to MTU max size
1831 # (https://en.wikipedia.org/wiki/Maximum_transmission_unit)
1832 # See also <https://httpd.apache.org/docs/2.4/mod/mod_deflate.html>
1833 # <https://webmasters.stackexchange.com/questions/31750/what-is-recommended-minimum-object-size-for-gzip-performance-benefits>
1834 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
1835 {
1836 3 ▶ return( '' ) if( $rule->{ $mime_type } eq 'none' );
Branch: 1 branch
BranchTrueFalseTotal
if $rule->{$mime_type} eq "none" 1 2 3
1837 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
1838 }
1839 ▶ 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
1840 $toptype eq 'video' ||
1841 $toptype eq 'audio' ||
1842 $toptype eq 'multipart' )
1843 {
1844 0 return( '' );
1845 }
1846 ▶ 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
1847 {
1848 # Suggest gzip compression if it exceeds 200Kb
1849 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
1850 }
1851 2 return( '' );
1852 }
1853  
1854 sub textual_type
1855 {
1856 0 ▶ my $self = shift( @_ );
Subroutine textual_type: not called CC=2 SCAR=17.9
Pod documented
1857 0 ▶ return( $_[0] =~ m{^(text|message)(/|\Z)}i ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$_[0] =~ m[^(text|message)(/|\Z)]i ? : 0 0 0
1858 }
1859  
1860 4 ▶sub _parts { return( shift->_set_get_array_as_object( '_parts', @_ ) ); }
Subroutine _parts: called CC=1 SCAR=0.0
1861  
1862 # NOTE: Used in both print_body() and dump()
1863 sub _prepare_multipart_headers
1864 {
1865 3 ▶ my $self = shift( @_ );
Subroutine _prepare_multipart_headers: called CC=14 SCAR=34.2
1866 3 my $mime_type = $self->mime_type;
1867 3 my $toptype;
1868 3 ▶ $toptype = [split( '/', lc( $mime_type ), 2 )]->[0] if( defined( $mime_type ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $mime_type 3 0 3
1869 3 my $boundary = $self->headers->multipart_boundary;
1870 # Ensure we have a boundary set.
1871 # This is the same code as in HTTP::Promise::Headers::as_string, but since
1872 # print_body() may be called separately, we need to check here too if a boundary
1873 # has been set.
1874 3 ▶ unless( $boundary )
Branch: 1 branch
BranchTrueFalseTotal
unless ($boundary) 0 3 3
1875 {
1876 0 $boundary = $self->make_boundary;
1877 0 my $ct = $self->headers->new_field( 'Content-Type' => $self->headers->content_type );
1878 0 $ct->boundary( $boundary );
1879 0 $self->headers->content_type( "$ct" );
1880 }
1881 # Parts
1882 # For reporting to the caller only when there are some issues.
1883 3 my $n = 0;
1884 # for generated part name, by default
1885 3 my $auto_name = 'part0';
1886 3 foreach my $part ( $self->parts->list )
1887 {
1888 12 ++$n;
1889 # If this is a multipart/form-data, ensure we have a part name, or isse a warning
1890 12 my $name;
1891 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
1892 {
1893 4 $name = $part->name;
1894 4 ▶ if( !$name )
Branch: 1 branch
BranchTrueFalseTotal
unless ($name) 0 4 4
1895 {
1896 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
1897 0 $name = ++$auto_name;
1898 0 $part->name( $name );
1899 }
1900 }
1901 elsif( $mime_type eq 'multipart/mixed' )
1902 {
1903 # remove any Content-Disposition used for multipart/form-data
1904 8 $part->headers->remove( 'Content-Disposition' );
1905 }
1906
1907 12 ▶ if( defined( $name ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) 4 8 12
1908 {
1909 4 my $content_disposition = $part->headers->content_disposition;
1910 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
004
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
1911 {
1912 # A simple check to save time from generating the Content-Disposition object
1913 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
1914 $content_disposition->index( 'form-data' ) == -1 )
1915 {
1916 0 my $cd = $part->headers->new_field( 'Content-Disposition' => $part->headers->content_disposition );
1917 0 ▶ $cd->name( $name ) if( !length( $cd->name ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $cd->name 0 0 0
1918 0 $cd->disposition( 'form-data' );
1919 }
1920 }
1921 else
1922 {
1923 0 $part->headers->content_disposition( qq{form-data; name="${name}"} );
1924 }
1925 }
1926 }
1927 3 return( $boundary );
1928 }
1929  
1930 # NOTE: sub FREEZE is inherited
1931 sub FREEZE
1932 {
1933 8 ▶ my $self = CORE::shift( @_ );
Subroutine FREEZE: called CC=5 SCAR=16.7
Pod undocumented
1934 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
1935 8 my $class = CORE::ref( $self );
1936 8 my $ref = $self->_obj2h;
1937 8 my %hash = %$ref;
1938 # We remove this to prevent a circular reference that CBOR::XS does not seem to be managing
1939 # This relation is re-created in HTTP::Promise::Message::THAW
1940 # It is safe to remove it, because 1) if it is a standalone HTTP::Promise::Entity object,
1941 # then it would not be set anyway, and 2) if it is part of an HTTP::Promise::Message, it
1942 # is going to be recreated.
1943 8 ▶ CORE::delete( @hash{ qw( http_message ) } ) unless( $serialiser ne 'CBOR' );
Branch: 1 branch
BranchTrueFalseTotal
unless $serialiser ne "CBOR" 0 8 8
1944 # Return an array reference rather than a list so this works with Sereal and CBOR
1945 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
1946 # But Storable want a list with the first element being the serialised element
1947 8 CORE::return( $class, \%hash );
1948 }
1949  
1950 4 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
1951  
1952 4 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
1953  
1954 # NOTE: sub THAW is inherited
1955  
1956 11 1;
1957 # NOTE: POD
1958 __END__
1959  
1960 =encoding utf-8
1961  
1962 =head1 NAME
1963  
1964 HTTP::Promise::Entity - HTTP Entity Class
1965  
1966 =head1 SYNOPSIS
1967  
1968 use HTTP::Promise::Entity;
1969 my $this = HTTP::Promise::Entity->new || die( HTTP::Promise::Entity->error, "\n" );
1970  
1971 =head1 VERSION
1972  
1973 v0.2.2
1974  
1975 =head1 DESCRIPTION
1976  
1977 This class represents an HTTP entity, which is an object class containing an headers object and a body object. It is agnostic to the type of HTTP message (request or response) it is associated with and can be used recurrently, such as to represent a part in a multipart HTTP message. Its purpose is to provide an API to access and manipulate and HTTP message entity.
1978  
1979 Here is how it fits in overall relation with other classes.
1980
1981 +-------------------------+ +--------------------------+
1982 | | | |
1983 | HTTP::Promise::Request | | HTTP::Promise::Response |
1984 | | | |
1985 +------------|------------+ +-------------|------------+
1986 | |
1987 | |
1988 | |
1989 | +------------------------+ |
1990 | | | |
1991 +--- HTTP::Promise::Message |---+
1992 | |
1993 +------------|-----------+
1994 |
1995 |
1996 +------------|-----------+
1997 | |
1998 | HTTP::Promise::Entity |
1999 | |
2000 +------------|-----------+
2001 |
2002 |
2003 +------------|-----------+
2004 | |
2005 | HTTP::Promise::Body |
2006 | |
2007 +------------------------+
2008  
2009 =head1 CONSTRUCTOR
2010  
2011 =head2 new
2012  
2013 This instantiate a new L<HTTP::Promise::Entity> object and returns it. It takes the following options, which can also be set or retrieved with their related method.
2014  
2015 =over 4
2016  
2017 =item * C<compression_min>
2018  
2019 Integer. Size threshold beyond which the associated body can be compressed. This defaults to 204800 (200Kb). Set it to 0 to disable it.
2020  
2021 =item * C<effective_type>
2022  
2023 String. The effective mime-type. Default to C<undef>
2024  
2025 =item * C<epilogue>
2026  
2027 An array reference of strings to be added after the headers and before the parts in a multipart message. Each array reference entry is treated as one line. This defaults to C<undef>
2028  
2029 =item * C<ext_vary>
2030  
2031 Boolean. Setting this to a true value and this will have L</decode_body> and L</encode_body> change the entity body file extension to reflect the encoding or decoding applied.
2032  
2033 See L</ext_vary> for an example.
2034  
2035 =item * C<headers>
2036  
2037 This is an L<HTTP::Promise::Headers> object. This defaults to C<undef>
2038  
2039 =item * C<is_encoded>
2040  
2041 Boolean. This is a flag used to determine whether the related entity body is decoded or not. This defaults to C<undef>
2042  
2043 See also L<HTTP::Promise::Headers/content_encoding>
2044  
2045 =item * C<output_dir>
2046  
2047 This is the path to the directory used when extracting body to files on the filesystem. This defaults to C<undef>
2048  
2049 =item * C<preamble>
2050  
2051 An array reference of strings to be added after all the parts in a multipart message. Each array reference entry is treated as one line. This defaults to C<undef>
2052  
2053 =back
2054  
2055 =head1 METHODS
2056  
2057 =head2 add_part
2058  
2059 Provided with an L<HTTP::Promise::Entity> object, and this will add it to the stack of parts for this entity.
2060  
2061 It returns the part added, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2062  
2063 =head2 as_form_data
2064  
2065 If the entity is of type C<multipart/form-data>, this will transform all of its parts into an L<HTTP::Promise::Body::Form::Data> object.
2066  
2067 It returns the new L<HTTP::Promise::Body::Form::Data> object upon success, or 0 if there was nothing to be done i the entity is not C<multipart/form-data> for example, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2068  
2069 Note that this is memory savvy, because even though it breaks down the parts into an L<HTTP::Promise::Body::Form::Data> object, original entity body that were stored on file remain on file. Each of the L<HTTP::Promise::Body::Form::Data> entry is a field name and its value is an L<HTTP::Promise::Body::Form::Field> object. Thus you could access data such as:
2070  
2071 my $form = $ent->as_form_data;
2072 my $name = $form->{fullname}->value;
2073 if( $form->{picture}->file )
2074 {
2075 say "Picture is stored on file.";
2076 }
2077 elsif( $form->{picture}->value->length )
2078 {
2079 say "Picture is in memory.";
2080 }
2081 else
2082 {
2083 say "There is no data.";
2084 }
2085  
2086 say "Content-Type for this form-data is: ", $form->{picture}->headers->content_type;
2087  
2088 =head2 as_string
2089  
2090 This returns a L<scalar object|Module::Generic::Scalar> containing a string representation of the message entity.
2091  
2092 It takes an optional string parameter containing an end of line separator, which defaults to C<\015\012>.
2093  
2094 Internally, this calls L</print>.
2095  
2096 If an error occurred, it set an L<error|Module::Generic/error> and returns C<undef>.
2097  
2098 Be mindful that because this returns a scalar object, it means the entire HTTP message entity is loaded into memory, which, depending on the content size, can potentially be big and thus take a lot of memory.
2099  
2100 You may want to check the body size first using: C<$ent->body->length> for example if this is not a multipart entity.
2101  
2102 =head2 attach
2103  
2104 Provided with a list of parameters and this add the created part entity to the stack of entity parts.
2105  
2106 This will transform the current entity into a multipart, if necessary, by calling L</make_multipart>
2107  
2108 Since it calls L</build> internally to build the message entity, see L</build> for the list of supported parameters.
2109  
2110 It returns the newly added L<part object|HTTP::Promise::Entity> upon success, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2111  
2112 =head2 body
2113  
2114 Sets or gets this entity L<body object|HTTP::Promise::Body>.
2115  
2116 =head2 body_as_array
2117  
2118 This returns an L<array object|Module::Generic::Array> object containing body lines with each line terminated by an end-of-line sequence, which is optional and defaults to C<\015\012>.
2119  
2120 Upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2121  
2122 =head2 body_as_string
2123  
2124 This returns a L<scalar object|Module::Generic::Scalar> containing a string representation of the message body.
2125  
2126 =head2 build
2127  
2128 my $ent = HTTP::Promise::Entity->new(
2129 encoding => 'gzip',
2130 type => 'text/plain',
2131 data => 'Hello world',
2132 );
2133 my $ent = HTTP::Promise::Entity->new(
2134 encoding => 'guess',
2135 type => 'text/plain',
2136 data => '/some/where/file.txt',
2137 );
2138  
2139 This takes an hash or hash reference of parameters and build a new L<HTTP::Promise::Entity>.
2140  
2141 It returns the newly created L<entity object|HTTP::Promise::Entity> object upon success, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2142  
2143 Supported arguments are:
2144  
2145 =over 4
2146  
2147 =item * C<boundary>
2148  
2149 The part boundary to be used if the entity is of type multipart.
2150  
2151 =item * C<data>
2152  
2153 The entity body content. If this is provided, the entity body will be an L<HTTP::Promise::Body::Scalar> object.
2154  
2155 =item * C<debug>
2156  
2157 An integer representing the level of debugging output. Defaults to 0.
2158  
2159 =item * C<disposition>
2160  
2161 A string representing the C<Content-Disposition>, such as C<form-data>. This defaults to C<inline>.
2162  
2163 =item * C<encoding>
2164  
2165 String. A comma-separated list of content encodings used in order you want the entity body to be encoded.
2166  
2167 For example: C<gzip, base64> or C<brotli>
2168  
2169 See L<HTTP::Promise::Stream> for a list of supported encodings.
2170  
2171 If C<encoding> is C<guess>, this will call L</suggest_encoding> to find a suitable encoding, if any at all.
2172  
2173 =item * C<filename>
2174  
2175 The C<filename> attribute value of a C<Content-Disposition> header value, if any.
2176  
2177 If the filename provided contains 8 bit characters like unicode characters, this will be detected and the filename will be encoded according to L<rfc2231|https://tools.ietf.org/html/rfc2231>
2178  
2179 See also L<HTTP::Promise::Headers/content_disposition> and L<HTTP::Promise::Headers::ContentDisposition>
2180  
2181 =item * C<path>
2182  
2183 The filepath to the content to be used as the entity body. This is useful if the body size is big and you do not want to load it in memory.
2184  
2185 =item * C<type>
2186  
2187 String. The entity mime-type. This defaults to C<text/plain>
2188  
2189 If the type is set to C<multipart/form-data> or C<multipart/mixed>, or any other multipart type, this will automatically create a boundary, which is basically a UUID generated with the XS module L<Data::UUID>
2190  
2191 =back
2192  
2193 =head2 compression_min
2194  
2195 Integer. This is the body size threshold in bytes beyond which this will make the encoding of the entity body possible. You can set this to zero to deactivate it.
2196  
2197 =head2 content_charset
2198  
2199 This will try to guess the character set of the body and returns a string the character encoding found, if any, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>. If nothing was found, it will return an empty string.
2200  
2201 It takes an optional hash or hash reference of options.
2202  
2203 Supported options are;
2204  
2205 =over 4
2206  
2207 =item * C<content>
2208  
2209 A string or scalar reference of some or all of the body data to be checked. If this is not provided, 4Kb of data will be read from the body to guess the character encoding.
2210  
2211 =back
2212  
2213 =head2 decode_body
2214  
2215 This takes a coma-separated list of encoding or an array reference of encodings, and an optional hash or hash reference of options and decodes the entity body.
2216  
2217 It returns the L<body object|HTTP::Promise::Body> upon success, and upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2218  
2219 Supported options are:
2220  
2221 =over 4
2222  
2223 =item * C<raise_error>
2224  
2225 Boolean. When set to true, this will cause this method to die upon error.
2226  
2227 =item * C<replace>
2228  
2229 Boolean. If true, this will replace the body content with the decoded version. Defaults to true.
2230  
2231 =back
2232  
2233 What this method does is instantiate a new L<HTTP::Promise::Stream> object for each encoding and pass it the data whether as a scalar reference if the data are in-memory body, or a file, until all decoding have been applied.
2234  
2235 When C<deflate> is one of the encoding, it will try to use L<IO::Uncompress::Inflate> to decompress data. However, some server encode data with C<deflate> but omit the zlib headers, which makes L<IO::Uncompress::Inflate> fail. This is detected and trapped and C<rawdeflate> is used as a fallback.
2236  
2237 =head2 dump
2238  
2239 This dumps the entity data into a string and returns it. It will encode the body if not yet encoded and will escape control and space characters, and show in hexadecimal representation the body content, so that even binary data is safe to dump.
2240  
2241 It takes some optional arguments, which are:
2242  
2243 =over 4
2244  
2245 =item * C<maxlength>
2246  
2247 Max body length to include in the dump.
2248  
2249 =item * C<no_content>
2250  
2251 The string to use when there is no content, i.e. when the body is empty.
2252  
2253 =back
2254  
2255 =head2 dump_skeleton
2256  
2257 This method is more for debugging, or to get a peek at the entity structure. This takes a filehandle to print the result to.
2258  
2259 This returns the current L<entity object|HTTP::Promise::Entity> on success, and upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2260  
2261 =head2 effective_type
2262  
2263 This set or get the effective mime-type. In assignment mode, this simply stores whatever mie-type you provide and in retrieval mode, this retrieve the value previously set, or by default the value returned from L</mime_type>
2264  
2265 =head2 encode_body
2266  
2267 This encode the entity body according to the encodings provided either as a comma-separated string or an array reference of encodings.
2268  
2269 The way it does this is to instantiate a new L<HTTP::Promise::Stream> object for each encoding and pass it the latest entity body.
2270  
2271 The resulting encoded body replaces the original one.
2272  
2273 It returns the L<entity body|HTTP::Promise::Body> upon success, and upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2274  
2275 =head2 epilogue
2276  
2277 Sets or gets an array of epilogue lines. An C<epilogue> is lines of text added after the last part of a C<multipart> message.
2278  
2279 This returns an L<array object|Module::Generic::Array>
2280  
2281 =head2 ext_vary
2282  
2283 Boolean. Setting this to a true value and this will have L</decode_body> and L</encode_body> change the entity body file extension to reflect the encoding or decoding applied.
2284  
2285 For example, if the entity body is stored in a text file C</tmp/DDAB03F0-F530-11EC-8067-D968FDB3E034.txt>, applying L</encode_body> with C<gzip> will create a new body text file such as C</tmp/DE13000E-F530-11EC-8067-D968FDB3E034.txt.gz>
2286  
2287 =head2 guess_character_encoding
2288  
2289 This will try to guess the entity body character encoding.
2290  
2291 It returns the encoding found as a string, if any otherwise it returns an empty string (not undef), and upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2292  
2293 This method tries to guess variation of unicode character sets, such as C<UTF-16BE>, C<UTF-16LE>, and C<utf-8-strict>
2294  
2295 It takes some optional parameters:
2296  
2297 =over 4
2298  
2299 =item * C<content>
2300  
2301 A string or scalar reference of content data to perform the guessing against.
2302  
2303 If this is not provided, this method will read up to 4096 bytes of data from the body to perform the guessing.
2304  
2305 =back
2306  
2307 See also L</content_charset>
2308  
2309 =head2 header
2310  
2311 Set or get the value returned by calling L<HTTP::Promise::Headers/header>
2312  
2313 This is just a shortcut.
2314  
2315 =head2 headers
2316  
2317 Sets or get the L<entity headers object|HTTP::Promise::Headers>
2318  
2319 =head2 header_as_string
2320  
2321 Returns the entity headers as a string.
2322  
2323 =head2 http_message
2324  
2325 Sets or get the L<HTTP message object|HTTP::Promise::Message>
2326  
2327 =head2 io_encoding
2328  
2329 This tries hard to find out the character set of the entity body to be used with L<perlfunc/open> or L<perlfunc/binmode>
2330  
2331 It returns a string, possibly empty if nothing could be guessed, and upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2332  
2333 It takes the following optional parameters:
2334  
2335 =over 4
2336  
2337 =item * C<alt_charset>
2338  
2339 Alternative character set to be used if none other could be found nor worked.
2340  
2341 =item * C<body>
2342  
2343 The entity L<body object|HTTP::Promise::Body> to use.
2344  
2345 =item * C<charset>
2346  
2347 A string containing the charset you think is used and this will perform checks against it.
2348  
2349 =item * C<charset_strict>
2350  
2351 Boolean. If true, this will enable the guessing in more strict mode (using the C<FB_CROAK> flag on L<Encode>)
2352  
2353 =item * C<content>
2354  
2355 A string or a scalar reference of content data to the guessing against.
2356  
2357 =item * C<default_charset>
2358  
2359 The default charset to use when nothing else was found.
2360  
2361 =back
2362  
2363 =head2 is_binary
2364  
2365 This checks if the data provided, or by default this entity body is binary data or not.
2366  
2367 It returns true (1) if it is, and false (0) otherwise. It returns false if the data is empty.
2368  
2369 This performs the similar checks that perl does (see L<perlfunc/-T>
2370  
2371 It sets and L<error|Module::Generic/error> and return C<undef> upon error
2372  
2373 You can optionally provide some data either as a string or as a scalar reference.
2374  
2375 See also L</is_text>
2376  
2377 For example:
2378  
2379 my $bool = $ent->is_binary;
2380 my $bool = $ent->is_binary( $string_of_data );
2381 my $bool = $ent->is_binary( \$string_of_data );
2382  
2383 =head2 is_body_in_memory
2384  
2385 Returns true if the entity body is an L<HTTP::Promise::Body::Scalar> object, false otherwise.
2386  
2387 =head2 is_body_on_file
2388  
2389 Returns true if the entity body is an L<HTTP::Promise::Body::File> object, false otherwise.
2390  
2391 =head2 is_decoded
2392  
2393 Boolean. Set get the decoded status of the entity body.
2394  
2395 =head2 is_encoded
2396  
2397 Boolean. Set get the encoded status of the entity body.
2398  
2399 =head2 is_multipart
2400  
2401 Returns true if this entity is a multipart message or not.
2402  
2403 =head2 is_text
2404  
2405 This checks if the data provided, or by default this entity body is text data or not.
2406  
2407 It returns true (1) if it is, and false (0) otherwise. It returns true if the data is empty.
2408  
2409 It sets and L<error|Module::Generic/error> and return C<undef> upon error
2410  
2411 You can optionally provide some data either as a string or as a scalar reference.
2412  
2413 See also L</is_binary>
2414  
2415 For example:
2416  
2417 my $bool = $ent->is_text;
2418 my $bool = $ent->is_text( $string_of_data );
2419 my $bool = $ent->is_text( \$string_of_data );
2420  
2421 =head2 make_boundary
2422  
2423 Returns a uniquely generated multipart boundary created using L<Data::UUID>
2424  
2425 =head2 make_multipart
2426  
2427 This transforms the current entity into the first part of a <multipart/form-data> HTTP message.
2428  
2429 For HTTP request, C<multipart/form-data> is the only valid C<Content-Type> for sending multiple data. L<rfc7578 in section 4.3|https://tools.ietf.org/html/rfc7578#section-4.3> states: "[RFC2388] suggested that multiple files for a single form field be transmitted using a nested "multipart/mixed" part. This usage is deprecated."
2430  
2431 See also this L<Stackoverflow discussion|https://stackoverflow.com/questions/36674161/http-multipart-form-data-multiple-files-in-one-input/41204533#41204533> and L<this one too|https://stackoverflow.com/questions/51575746/http-header-content-type-multipart-mixed-causes-400-bad-request>
2432  
2433 Of course, technically, nothing prevents an HTTP message (request or response) from being a C<multipart/mixed> or something else.
2434  
2435 This method takes a multipart subtype, such as C<form-data>, or C<mixed>, etc and creates a multipart entity of which this current entity will become the first part. If no multipart subtype is specified, this defaults to C<form-data>.
2436  
2437 It takes also an optional hash or hash reference of parameters.
2438  
2439 Valid parameters are:
2440  
2441 =over 4
2442  
2443 =item * C<force>
2444  
2445 Boolean. Forces the creation of a multipart even when the current entity is already a multipart.
2446  
2447 This would have the effect of having the current entity become an embedded multipart into a new multipart entity.
2448  
2449 =back
2450  
2451 It returns the current entity object, modified, upon success, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2452  
2453 =head2 make_singlepart
2454  
2455 This transform the current entity into a simple, i.e. no multipart, message entity.
2456  
2457 It returns false, but not C<undef> if this contains more than one part. It returns the current object upon success, or if this is already a simple entity message, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2458  
2459 =head2 mime_type
2460  
2461 Returns this entity mime-type by calling L<HTTP::Promise::Headers/mime_type> and passing it whatever arguments were provided.
2462  
2463 =head2 name
2464  
2465 The name of this entity used for C<multipart/form-data> as defined in L<rfc7578|https://tools.ietf.org/html/rfc7578>
2466  
2467 =head2 new_body
2468  
2469 This is a convenient constructor to instantiate a new entity body. It takes a single argument, one of C<file>, C<form>, C<scalar> or C<string>
2470  
2471 =over 4
2472  
2473 =item * C<file>
2474  
2475 Returns a new L<HTTP::Promise::Body::File> object
2476  
2477 =item * C<form>
2478  
2479 Returns a new L<HTTP::Promise::Body::Form> object
2480  
2481 =item * C<scalar> or C<string>
2482  
2483 Returns a new L<HTTP::Promise::Body::Scalar> object
2484  
2485 =back
2486  
2487 The constructor of each of those classes are passed whatever argument is provided to this method (except, of course, the initial argument).
2488  
2489 For example:
2490  
2491 my $body = $ent->new_body( file => '/some/where/file.txt' );
2492 my $body = $ent->new_body( string => 'Hello world!' );
2493 my $body = $ent->new_body( string => \$scalar );
2494 # Same, but using indistinctly 'scalar'
2495 my $body = $ent->new_body( scalar => \$scalar );
2496  
2497 It returns the newly instantiated object upon success, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2498  
2499 =head2 open
2500  
2501 This calls C<open> on the entity body object, if any, and passing it whatever argument was provided.
2502  
2503 It returns the resulting L<filehandle object|Module::Generic::File::IO>, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2504  
2505 =head2 output_dir
2506  
2507 Sets or gets the path to the directory used to store extracted files, when applicable.
2508  
2509 =head2 parts
2510  
2511 Sets or gets the L<array object|Module::Generic::Array> of entity part objects.
2512  
2513 =head2 preamble
2514  
2515 Sets or gets the L<array object|Module::Generic::Array> of preamble lines. C<preamble> is the lines of text that precedes the first part in a multipart message. Normally, this is never used in HTTP parlance.
2516  
2517 =head2 print
2518  
2519 Provided with a filehandle, or an L<HTTP::Promise::IO> object, and an hash or hash reference of options and this will print the current entity with all its parts, if any.
2520  
2521 What this does internally is:
2522  
2523 =over 4
2524  
2525 =item 1. Call L</print_start_line>
2526  
2527 =item 2. Call L</print_header>
2528  
2529 =item 3. Call L</print_body>
2530  
2531 =back
2532  
2533 The only supported option is C<eol> which is the string to be used as a new line terminator. This is printed out just right after printing the headers. This defaults to C<\015\012>, which is C<\r\n>
2534  
2535 It returns the current entity object upon success, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2536  
2537 =head2 print_body
2538  
2539 Provided with a filehandle, or an L<HTTP::Promise::IO> object, and an hash or hash reference of options and this will print the current entity body. This is possibly is a no-op if there is no entity body.
2540  
2541 If the entity is a multipart message, this will call L</print> on all its L<entity parts|HTTP::Promise::Entity>.
2542  
2543 It returns the current entity object upon success, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2544  
2545 =head2 print_bodyhandle
2546  
2547 Provided with a filehandle, or an L<HTTP::Promise::IO> object, and an hash or hash reference of options and this will print the current entity body.
2548  
2549 This will first encode the body by calling L</encode> if encodings are set and the entity body is not yet marked as being encoded with L</is_encoded>
2550  
2551 Supported options are:
2552  
2553 =over 4
2554  
2555 =item * C<binmode>
2556  
2557 The character encoding to use for PerlIO when calling open.
2558  
2559 =back
2560  
2561 It returns the current entity object upon success, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2562  
2563 =head2 print_header
2564  
2565 This calls L<HTTP::Promise::Headers/print>, passing it whatever arguments were provided, and returns whatever value is returned from this method call. This is basically a convenient shortcut.
2566  
2567 =head2 print_start_line
2568  
2569 Provided with a filehandle, and an hash or hash reference of options and this will print the message C<start line>, if any.
2570  
2571 A message C<start line> in HTTP parlance is the first line of a request or response, so something like:
2572  
2573 GET / HTTP/1.0
2574  
2575 or for a response:
2576  
2577 HTTP/1.0 200 OK
2578  
2579 It returns the current entity object upon success, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2580  
2581 =head2 purge
2582  
2583 This calls C<purge> on the body object, if any, and calls it also on every parts.
2584  
2585 It returns the current entity object upon success, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2586  
2587 =head2 save_file
2588  
2589 Provided with an optional filepath and this will save the body to it unless this is an HTTP multipart message.
2590  
2591 If no explicit filepath is provided, this will try to guess one from the C<Content-Disposition> header value, possibly striping it of any dangerous characters and making it a complete path using L</output_dir>
2592  
2593 If no suitable filename could be found, ultimately, this will use a generated one using L<Module::Generic/new_tempfile> inherited by this class.
2594  
2595 The file extension will be guessed from the entity body mime-type by checking the C<Content-Type> header or by looking directly at the entity body data using L<HTTP::Promise::MIME> that uses the XS module L<File::MMagic::XS> to perform the job.
2596  
2597 If the entity body is encoded, it will decode it before saving it to the resulting filepath.
2598  
2599 It returns the L<file object|Module::Generic::File> upon success, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2600  
2601 =head2 stringify
2602  
2603 This is an alias for L</as_string>
2604  
2605 =head2 stringify_body
2606  
2607 This is an alias for L</body_as_string>
2608  
2609 =head2 stringify_header
2610  
2611 This is an alias for L<HTTP::Promise::Headers/as_string>
2612  
2613 =head2 suggest_encoding
2614  
2615 Based on the entity body mime-type, this will guess what encoding is appropriate.
2616  
2617 It does not provide any encoding for image, audio or video files who are usually already compressed and if the body size is below the threshold set with L</compression_min>.
2618  
2619 This returns the encoding as a string upon success, an empty string if no suitable encoding could be found, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
2620  
2621 =head2 textual_type
2622  
2623 Returns true if this entity mime-type starts with C<text>, such as C<text/plain> or C<text/html> or starts with C<message>, such as C<message/http>
2624  
2625 =head1 THREAD-SAFETY
2626  
2627 L<HTTP::Promise::Entity> is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
2628  
2629 =head1 AUTHOR
2630  
2631 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
2632  
2633 =head1 SEE ALSO
2634  
2635 =over 4
2636
2637 =item L<rfc2616 section 3.7.2 Multipart Types|http://tools.ietf.org/html/rfc2616#section-3.7.2>
2638
2639 =item L<rfc2046 section 5.1.1 Common Syntax|http://tools.ietf.org/html/rfc2046#section-5.1.1>
2640
2641 =item L<rfc2388 multipart/form-data|http://tools.ietf.org/html/rfc2388>
2642
2643 =item L<rfc2045|https://tools.ietf.org/html/rfc2045>
2644
2645 =back
2646  
2647 L<Mozilla documentation on Content-Disposition and international filename|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition> and L<other Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types.>
2648  
2649 L<Wikipedia|https://en.wikipedia.org/wiki/MIME#Multipart_messages>
2650  
2651 L<On Unicode|https://perldoc.perl.org/Encode::Unicode>
2652  
2653 L<HTTP::Promise>, L<HTTP::Promise::Request>, L<HTTP::Promise::Response>, L<HTTP::Promise::Message>, L<HTTP::Promise::Entity>, L<HTTP::Promise::Headers>, L<HTTP::Promise::Body>, L<HTTP::Promise::Body::Form>, L<HTTP::Promise::Body::Form::Data>, L<HTTP::Promise::Body::Form::Field>, L<HTTP::Promise::Status>, L<HTTP::Promise::MIME>, L<HTTP::Promise::Parser>, L<HTTP::Promise::IO>, L<HTTP::Promise::Stream>, L<HTTP::Promise::Exception>
2654  
2655 =head1 COPYRIGHT & LICENSE
2656  
2657 Copyright(c) 2022 DEGUEST Pte. Ltd.
2658  
2659 All rights reserved
2660 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2661  
2662 =cut
« lib/HTTP/Promise/Stream.pm ↑ Summary blib/lib/HTTP/Promise/IO.pm »