lib/HTTP/Promise/Message.pm

filter: Statementstmt 34.8% 199 / 571 Branchbran 21.0% 79 / 376 Conditioncond 16.7% 42 / 251 MC/DCmcdc 2.3% 3 / 130 Subroutinesub 58.1% 32 / 55 Podpod 84.6% 33 / 39 totaltotal 27.2% 388 / 1422 CC 288 SCAR 55.5
CC
288
Cov
27%
_make_parts
992.0
content
650.0
parts
650.0
CRAP 256.6
« lib/HTTP/Promise/IO.pm ↑ Summary blib/lib/HTTP/Promise/Headers.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Message.pm
3 ## Version v0.3.3
4 ## Copyright(c) 2025 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/03/21
7 ## Modified 2026/03/08
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::Message;
15 BEGIN
16 {
17 6 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶ warnings::register_categories( 'HTTP::Promise' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶ use vars qw( $DEBUG $AUTOLOAD $CRLF $HTTP_VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶ use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 require HTTP::Promise::Headers;
24 # use Nice::Try;
25 6 ▶ use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 6 our $CRLF = "\015\012";
27 # HTTP/1.0, HTTP/1.1, HTTP/2
28 6 our $HTTP_VERSION = qr/(?<http_protocol>HTTP\/(?<http_version>(?<http_vers_major>[0-9])(?:\.(?<http_vers_minor>[0-9]))?))/;
29 6 our $VERSION = 'v0.3.3';
30 };
31  
32 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 sub init
36 {
37 31 ▶ my $self = shift( @_ );
Subroutine init: called CC=40 SCAR=46.5
Pod documented
38 31 my( $headers, $content );
39 31 ▶ if( @_ == 1 && ref( $_[0] ) eq 'HASH' )
Branch: 3 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 12 19 31
elsif (@_ >= 1 and $self->_is_array($_[0]) || $self->_is_a($_[0], 'HTTP::Promise::Headers') || $self->_is_a($_[0], 'HTTP::Headers') || !defined($_[0])) { } 11 8 19
elsif (@_ and ref $_[0] ne 'HASH') { } 0 16 16
Condition66%2/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
19012
MC/DC50%1/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table66%2/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
40 {
41 12 my $opts = shift( @_ );
42 12 ( $headers, $content ) = CORE::delete( @$opts{qw( headers content )} );
43 12 @_ = %$opts;
44 }
45 ▶ elsif( @_ >= 1 &&
Condition50%6/12
@_ >= 1 and $self->_is_array($_[0]) || $self->_is_a($_[0], "HTTP::Promise::Headers") || $self->_is_a($_[0], "HTTP::Headers") || !defined($_[0])
!ll&&!rl&&r
8011
$self->_is_array($_[0]) or $self->_is_a($_[0], "HTTP::Promise::Headers") or $self->_is_a($_[0], "HTTP::Headers") or not defined $_[0]
l!l&&r!l&&!r
1100
$self->_is_array($_[0]) or $self->_is_a($_[0], "HTTP::Promise::Headers") or $self->_is_a($_[0], "HTTP::Headers")
l!l&&r!l&&!r
1100
$self->_is_array($_[0]) or $self->_is_a($_[0], "HTTP::Promise::Headers")
l!l&&r!l&&!r
650
MC/DC20%1/5
@_ >= 1 and $self->_is_array($_[0]) || $self->_is_a($_[0], "HTTP::Promise::Headers") || $self->_is_a($_[0], "HTTP::Headers") || !defined($_[0])
@_ >= 1 $self->_is_array($_[0]) $self->_is_a($_[0], "HTTP::Promise::Headers") $self->_is_a($_[0], "HTTP::Headers") not defined $_[0]
Truth table50%3/6
A and B or C or D or not(E)
A= @_ >= 1B= $self->_is_array($_[0])C= $self->_is_a($_[0], "HTTP::Promise::Headers")D= $self->_is_a($_[0], "HTTP::Headers")E= not defined $_[0]
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
46 ( $self->_is_array( $_[0] ) ||
47 $self->_is_a( $_[0], 'HTTP::Promise::Headers' ) ||
48 $self->_is_a( $_[0], 'HTTP::Headers' ) ||
49 # HTTP::Promise::Message->new( undef, "some\ncontent" );
50 !defined( $_[0] )
51 ) )
52 {
53 11 $headers = shift( @_ );
54 # Odd number of arguments and following argument is not an hash; or
55 # next argument is an hash
56 # this means the next parameter is the content
57 # $r->init( $headers, $content, name1 => value1, name2 => value2 );
58 # $r->init( $headers, $content, { name1 => value1, name2 => value2 } );
59 # First value must be either not a ref or a ref that stringifies
60 11 ▶ if( ( !ref( $_[0] ) ||
Branch: 1 branch
BranchTrueFalseTotal
if (!ref($_[0]) || ref $_[0] && overload::Method($_[0], "\"\"") || $self->_is_a($_[0], "HTTP::Promise::Body") || $self->_is_a($_[0], "HTTP::Promise::Body::Form") and @_ == 1 || @_ > 2 && @_ % 2 && ref $_[1] ne "HASH" || @_ == 2 && ref $_[1] eq "HASH") 0 5 5
Condition36%11/30
!ref($_[0]) || ref $_[0] && overload::Method($_[0], "\"\"") || $self->_is_a($_[0], "HTTP::Promise::Body") || $self->_is_a($_[0], "HTTP::Promise::Body::Form") and @_ == 1 || @_ > 2 && @_ % 2 && ref $_[1] ne "HASH" || @_ == 2 && ref $_[1] eq "HASH"
!ll&&!rl&&r
560
not ref $_[0] or ref $_[0] and overload::Method($_[0], "\"\"") or $self->_is_a($_[0], "HTTP::Promise::Body") or $self->_is_a($_[0], "HTTP::Promise::Body::Form")
l!l&&r!l&&!r
605
not ref $_[0] or ref $_[0] and overload::Method($_[0], "\"\"") or $self->_is_a($_[0], "HTTP::Promise::Body")
l!l&&r!l&&!r
605
not ref $_[0] or ref $_[0] and overload::Method($_[0], "\"\"")
l!l&&r!l&&!r
605
ref $_[0] and overload::Method($_[0], "\"\"")
!ll&&!rl&&r
050
@_ == 1 or @_ > 2 and @_ % 2 and ref $_[1] ne "HASH" or @_ == 2 and ref $_[1] eq "HASH"
l!l&&r!l&&!r
600
@_ == 1 or @_ > 2 and @_ % 2 and ref $_[1] ne "HASH"
l!l&&r!l&&!r
600
@_ > 2 and @_ % 2 and ref $_[1] ne "HASH"
!ll&&!rl&&r
000
@_ > 2 and @_ % 2
!ll&&!rl&&r
000
@_ == 2 and ref $_[1] eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/11
!ref($_[0]) || ref $_[0] && overload::Method($_[0], "\"\"") || $self->_is_a($_[0], "HTTP::Promise::Body") || $self->_is_a($_[0], "HTTP::Promise::Body::Form") and @_ == 1 || @_ > 2 && @_ % 2 && ref $_[1] ne "HASH" || @_ == 2 && ref $_[1] eq "HASH"
not ref $_[0] ref $_[0] overload::Method($_[0], "\"\"") $self->_is_a($_[0], "HTTP::Promise::Body") $self->_is_a($_[0], "HTTP::Promise::Body::Form") @_ == 1 @_ > 2 @_ % 2 ref $_[1] ne "HASH" @_ == 2 ref $_[1] eq "HASH"
Truth table1%1/68
not(A) or B and C or D or E and F or G and H and I or J and K
A= not ref $_[0]B= ref $_[0]C= overload::Method($_[0], "\"\"")D= $self->_is_a($_[0], "HTTP::Promise::Body")E= $self->_is_a($_[0], "HTTP::Promise::Body::Form")F= @_ == 1G= @_ > 2H= @_ % 2I= ref $_[1] ne "HASH"J= @_ == 2K= ref $_[1] eq "HASH"
ABCDEFGHIJKresult
00X00XXXXXX0
01000XXXXXX0
1XXXX00XX0X0
1XXXX00XX100
1XXXX010X0X0
1XXXX010X100
1XXXX01100X0
1XXXX0110100
011XX00XX0X0
011XX00XX100
011XX010X0X0
011XX010X100
011XX01100X0
011XX0110100
00X1X00XX0X0
00X1X00XX100
00X1X010X0X0
00X1X010X100
00X1X01100X0
00X1X0110100
0101X00XX0X0
0101X00XX100
0101X010X0X0
0101X010X100
0101X01100X0
0101X0110100
00X0100XX0X0
00X0100XX100
00X01010X0X0
00X01010X100
00X0101100X0
00X010110100
0100100XX0X0
0100100XX100
01001010X0X0
01001010X100
0100101100X0
010010110100
1XXXX1XXXXX1
1XXXX0111XX1
1XXXX00XX111
1XXXX010X111
1XXXX0110111
011XX1XXXXX1
011XX0111XX1
011XX00XX111
011XX010X111
011XX0110111
00X1X1XXXXX1
00X1X0111XX1
00X1X00XX111
00X1X010X111
00X1X0110111
0101X1XXXXX1
0101X0111XX1
0101X00XX111
0101X010X111
0101X0110111
00X011XXXXX1
00X010111XX1
00X0100XX111
00X01010X111
00X010110111
010011XXXXX1
010010111XX1
0100100XX111
01001010X111
010010110111
61 ( ref( $_[0] ) && overload::Method( $_[0] => '""' ) ) ||
62 ( $self->_is_a( $_[0] => 'HTTP::Promise::Body' ) ) ||
63 ( $self->_is_a( $_[0] => 'HTTP::Promise::Body::Form' ) )
64 ) &&
65 (
66 @_ == 1 ||
67 # Odd number of parameters and the second one is not an hash ref:
68 # e.g.: $content, name1 => value1, name2 => value2
69 ( @_ > 2 && ( @_ % 2 ) && ref( $_[1] ) ne 'HASH' ) ||
70 # 2 params left and the second one is an hash reference:
71 # e.g.: $content, { name1 => value1, name2 => value2 }
72 ( @_ == 2 && ref( $_[1] ) eq 'HASH' )
73 )
74 )
75 {
76 6 $content = shift( @_ );
77 }
78 }
79 ▶ elsif( @_ && ref( $_[0] ) ne 'HASH' )
Condition33%1/3
@_ and ref $_[0] ne "HASH"
!ll&&!rl&&r
800
MC/DC0%0/2
@_ and ref $_[0] ne "HASH"
@_ ref $_[0] ne "HASH"
Truth table33%1/3
A and B
A= @_B= ref $_[0] ne "HASH"
ABresult
0X0
100
111
80 {
81 0 return( $self->error( "Bad header argument. I was expecting an headers object, or an hash reference, but got: ", $_[0] ) );
82 }
83
84 31 ▶ if( defined( $headers ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $headers) { } 11 20 31
85 {
86 11 ▶ if( $self->_is_a( $headers, 'HTTP::Promise::Headers' ) || $self->_is_a( $headers, 'HTTP::Headers' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_a($headers, 'HTTP::Promise::Headers') or $self->_is_a($headers, 'HTTP::Headers')) { } 5 6 11
elsif ($self->_is_array($headers)) { } 6 0 6
Condition66%2/3
$self->_is_a($headers, "HTTP::Promise::Headers") or $self->_is_a($headers, "HTTP::Headers")
l!l&&r!l&&!r
506
MC/DC50%1/2
$self->_is_a($headers, "HTTP::Promise::Headers") or $self->_is_a($headers, "HTTP::Headers")
$self->_is_a($headers, "HTTP::Promise::Headers") $self->_is_a($headers, "HTTP::Headers")
Truth table66%2/3
A or B
A= $self->_is_a($headers, "HTTP::Promise::Headers")B= $self->_is_a($headers, "HTTP::Headers")
ABresult
1X1
011
000
87 {
88 5 $headers = $headers->clone;
89 5 $headers = bless( $headers => 'HTTP::Promise::Headers' );
90 }
91 elsif( $self->_is_array( $headers ) )
92 {
93 6 $headers = HTTP::Promise::Headers->new( @$headers );
94 }
95 else
96 {
97 0 return( $self->error( "Unknown headers value passed. I was expecting an HTTP::Promise::Headers, or HTTP::Headers object or an array reference, but instead I got '$headers' (", overload::StrVal( $headers ), ")." ) );
98 }
99 }
100 else
101 {
102 20 $headers = HTTP::Promise::Headers->new;
103 }
104
105 31 ▶ if( !defined( $content ) && $headers->exists( 'Content' ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $content and $headers->exists("Content")) 0 31 31
Condition66%2/3
not defined $content and $headers->exists("Content")
!ll&&!rl&&r
6250
MC/DC0%0/2
not defined $content and $headers->exists("Content")
not defined $content $headers->exists("Content")
Truth table66%2/3
not(A) and B
A= not defined $contentB= $headers->exists("Content")
ABresult
0X0
100
111
106 {
107 0 ( $content ) = $headers->remove_header( 'Content' );
108 # $content = $headers->header( 'Content' );
109 # $headers->remove_header( 'Content' );
110 }
111
112 31 ▶ if( defined( $content ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) 6 25 31
113 {
114 6 ▶ $self->_utf8_downgrade( $content ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_utf8_downgrade($content) or return $self->pass_error 0 6 6
115 }
116
117 # $self->{content} = $content;
118 31 ▶ $self->{entity} = undef unless( CORE::exists( $self->{entity} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"entity"} 31 0 31
119 31 $self->{headers} = $headers;
120 31 ▶ $self->{protocol} = undef unless( exists( $self->{protocol} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"protocol"} 31 0 31
121 31 ▶ $self->{version} = '' unless( exists( $self->{version} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"version"} 27 4 31
122 31 $self->{_init_strict_use_sub} = 1;
123 31 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 31 31
124 31 my $ent = $self->entity;
125 31 ▶ unless( $ent )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ent) 31 0 31
126 {
127 31 ▶ $self->_load_class( 'HTTP::Promise::Entity' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Entity") or return $self->pass_error 0 31 31
128 31 $ent = HTTP::Promise::Entity->new( headers => $headers, debug => $self->debug );
129 31 $self->entity( $ent );
130 }
131 31 $ent->http_message( $self );
132 31 $ent->debug( $self->debug );
133 31 $headers->debug( $self->debug );
134
135 # Even if it is zero bytes big, we still create the body
136 # If a $content was provided, we store it in a in-memory body
137 # If the user
138 31 ▶ if( defined( $content ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) 6 25 31
139 {
140 6 ▶ if( $self->_is_a( $content => [qw( HTTP::Promise::Body HTTP::Promise::Body::Form )] ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($content, ['HTTP::Promise::Body', 'HTTP::Promise::Body::Form'])) { } 0 6 6
141 {
142 0 ▶ $ent->body( $content ) || return( $self->pass_error( $ent->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$ent->body($content) or return $self->pass_error($ent->error) 0 0 0
143 }
144 else
145 {
146 6 my $body = $ent->new_body( string => \$content );
147 6 ▶ return( $self->pass_error( $ent->error ) ) if( !defined( $body ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 6 6
148 6 $ent->body( $body );
149 }
150 # If Content-Encoding is set, then set is_encoded to true
151 6 ▶ if( $headers->content_encoding->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->content_encoding->length) 2 4 6
152 {
153 2 $ent->is_encoded(1);
154 }
155 }
156 # There is no parts in this object. Everything is held in HTTP::Promise::Entity
157 # $self->{_parts} = [];
158 31 return( $self );
159 }
160  
161 sub add_content
162 {
163 0 ▶ my $self = shift( @_ );
Subroutine add_content: not called CC=9 SCAR=45.0
Pod documented
164 0 ▶ if( defined( $_[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[0]) 0 0 0
165 {
166 0 ▶ $self->_utf8_downgrade( $self->_is_scalar( $_[0] ) ? ${$_[0]} : $_[0] ) ||
Branch: 2 branches
BranchTrueFalseTotal
$self->_utf8_downgrade($self->_is_scalar($_[0]) ? ${$_[0];} : $_[0]) or return $self->pass_error 0 0 0
$self->_is_scalar($_[0]) ? : 0 0 0
167 return( $self->pass_error );
168 }
169 0 my( $ent, $body );
170 0 ▶ unless( $ent = $self->entity )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ent = $self->entity) 0 0 0
171 {
172 0 ▶ $self->_load_class( 'HTTP::Promise::Entity' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Entity") or return $self->pass_error 0 0 0
173 0 $ent = HTTP::Promise::Entity->new( debug => $self->debug );
174 }
175
176 0 $body = $ent->body;
177 0 ▶ if( $body )
Branch: 1 branch
BranchTrueFalseTotal
if ($body) { } 0 0 0
178 {
179 0 ▶ return( $self->error( "Unable to append to an entity body other than a HTTP::Promise::Body::Scalar" ) ) if( !$self->_is_a( $body => 'HTTP::Promise::Body::Scalar' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($body, "HTTP::Promise::Body::Scalar") 0 0 0
180 0 $body->append( $_[0] );
181 }
182 else
183 {
184 0 ▶ $body = $ent->new_body( string => $_[0] ) ||
Condition0%0/2
$ent->new_body('string', $_[0]) || (return $self->pass_error($ent->error))
l!l
00
MC/DC0%0/1
$ent->new_body('string', $_[0]) || (return $self->pass_error($ent->error))
$ent->new_body('string', $_[0])
Truth table0%0/2
A
A= $ent->new_body('string', $_[0])
Aresult
11
00
185 return( $self->pass_error( $ent->error ) );
186 0 $ent->body( $body );
187 }
188 0 return( $body );
189 }
190  
191 sub add_content_utf8
192 {
193 0 ▶ my( $self, $buff ) = @_;
Subroutine add_content_utf8: not called CC=1 SCAR=6.9
Pod documented
194 0 utf8::upgrade( $buff );
195 0 utf8::encode( $buff );
196 0 return( $self->add_content( $buff ) );
197 }
198  
199 # Adding part will automatically makes it a multipart/form-data if not set already
200 # There is no such thing as multipart/mixed in HTTP
201 sub add_part
202 {
203 0 ▶ my $self = shift( @_ );
Subroutine add_part: not called CC=21 SCAR=61.4
Pod documented
204 0 my $ent = $self->entity;
205 0 my $headers = $self->headers;
206 0 ▶ unless( $ent )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ent) 0 0 0
207 {
208 0 ▶ $self->_load_class( 'HTTP::Promise::Entity' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Entity") or return $self->pass_error 0 0 0
209 0 $ent = HTTP::Promise::Entity->new( headers => $headers, debug => $self->debug );
210 0 $self->entity( $ent );
211 }
212 0 ▶ if( ( $self->content_type || '' ) !~ m,^multipart/, )
Branch: 3 branches
BranchTrueFalseTotal
if (not +($self->content_type || '') =~ m[^multipart/]) { } 0 0 0
elsif ($ent->parts->is_empty and ($ent->body and not $ent->body->is_empty)) { } 0 0 0
elsif ($self->content_type->index('boundary') == -1) { } 0 0 0
Condition0%0/2
$self->content_type || ''
l!l
00
MC/DC0%0/1
$self->content_type || ''
$self->content_type
Truth table0%0/2
A
A= $self->content_type
Aresult
11
00
213 {
214 0 ▶ $ent->make_multipart( 'form-data' ) || return( $self->pass_error( $ent->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$ent->make_multipart("form-data") or return $self->pass_error($ent->error) 0 0 0
215 }
216 # elsif( $self->_parts->is_empty && ( $self->entity && $self->entity->body && !$self->entity->body->is_empty ) )
217 ▶ elsif( $ent->parts->is_empty && ( $ent->body && !$ent->body->is_empty ) )
Condition0%0/6
$ent->parts->is_empty and ($ent->body and not $ent->body->is_empty)
!ll&&!rl&&r
000
$ent->body and not $ent->body->is_empty
!ll&&!rl&&r
000
MC/DC0%0/3
$ent->parts->is_empty and ($ent->body and not $ent->body->is_empty)
$ent->parts->is_empty $ent->body not $ent->body->is_empty
Truth table0%0/4
A and B and not(C)
A= $ent->parts->is_emptyB= $ent->bodyC= not $ent->body->is_empty
ABCresult
0XX0
10X0
1100
1111
218 {
219 # Should really use HTTP::Promise::Entity->make_multipart
220 0 $self->_make_parts;
221 }
222 elsif( $self->content_type->index( 'boundary' ) == -1 )
223 {
224 0 my $ct = $headers->new_field( 'Content-Type' => $self->content_type );
225 0 $ct->boundary( $self->make_boundary );
226 0 $self->content_type( $ct );
227 }
228
229 0 my @new = ();
230 0 my $name;
231 0 ▶ for( my $i = 0; $i < scalar( @_ ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @_) 0 0 0
232 {
233 0 my $this = $_[$i];
234 # If this is a string or a scalar reference
235 0 ▶ if( defined( $this ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (defined $this and !ref($this) || $self->_is_scalar($this) && overload::Method($this, "\"\"")) 0 0 0
Condition0%0/9
defined $this and !ref($this) || $self->_is_scalar($this) && overload::Method($this, "\"\"")
!ll&&!rl&&r
000
not ref $this or $self->_is_scalar($this) and overload::Method($this, "\"\"")
l!l&&r!l&&!r
000
$self->_is_scalar($this) and overload::Method($this, "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/4
defined $this and !ref($this) || $self->_is_scalar($this) && overload::Method($this, "\"\"")
defined $this not ref $this $self->_is_scalar($this) overload::Method($this, "\"\"")
Truth table0%0/5
A and not(B) or C and D
A= defined $thisB= not ref $thisC= $self->_is_scalar($this)D= overload::Method($this, "\"\"")
ABCDresult
0XXX0
100X0
10100
11XX1
10111
236 ( !ref( $this ) || ( $self->_is_scalar( $this ) && overload::Method( $this => '""' ) ) ) )
237 {
238 0 $name = $this;
239 0 next;
240 }
241
242 # Either a HTTP::Promise::Request, or a HTTP::Promise::Response, or even a HTTP::Promise::Message
243 0 ▶ unless( $self->_is_a( $this => 'HTTP::Promise::Entity' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($this, "HTTP::Promise::Entity")) 0 0 0
244 {
245 0 ▶ return( $self->error( "Part object provided (", overload::StrVal( $this ), ") is neither a HTTP::Promise::Entity or a HTTP::Promise::Message object." ) ) if( !$self->_is_a( $this => 'HTTP::Promise::Message' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($this, "HTTP::Promise::Message") 0 0 0
246 0 my $part_ent = $this->entity;
247 0 ▶ unless( $part_ent )
Branch: 1 branch
BranchTrueFalseTotal
unless ($part_ent) 0 0 0
248 {
249 0 ▶ $part_ent = HTTP::Promise::Entity->new( headers => $this->headers, debug => $self->debug ) ||
Condition0%0/2
'HTTP::Promise::Entity'->new('headers', $this->headers, 'debug', $self->debug) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
'HTTP::Promise::Entity'->new('headers', $this->headers, 'debug', $self->debug) || (return $self->pass_error)
'HTTP::Promise::Entity'->new('headers', $this->headers, 'debug', $self->debug)
Truth table0%0/2
A
A= 'HTTP::Promise::Entity'->new('headers', $this->headers, 'debug', $self->debug)
Aresult
11
00
250 return( $self->pass_error );
251 0 $this->entity( $part_ent );
252 }
253 0 ▶ $part_ent->name( $name ) if( defined( $name ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
254 0 push( @new, $part_ent );
255 0 undef( $name );
256 0 next;
257 }
258 0 ▶ $this->name( $name ) if( defined( $name ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $name 0 0 0
259 0 undef( $name );
260 0 push( @new, $this );
261 }
262
263 0 $ent->parts->push( @new );
264 0 return( $self );
265 }
266  
267 sub as_form_data
268 {
269 0 ▶ my( $self, $eol ) = @_;
Subroutine as_form_data: not called CC=14 SCAR=53.5
Pod documented
270 0 my $type = $self->headers->type;
271 0 ▶ $type = lc( $type // '' );
Condition0%0/2
$type // ""
l!l
00
MC/DC0%0/1
$type // ""
$type
Truth table0%0/2
A
A= $type
Aresult
11
00
272 0 ▶ $self->_load_class( 'HTTP::Promise::Body::Form' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Body::Form") or return $self->pass_error 0 0 0
273 0 my $payload = $self->decoded_content_utf8->scalar;
274 0 my $uri;
275 0 ▶ if( $type eq 'multipart/form-data' )
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'multipart/form-data') { } 0 0 0
elsif ($type eq 'application/json') { } 0 0 0
elsif ($self->can('uri') and $uri = $self->uri and $uri->query and not defined $payload && length $payload) { } 0 0 0
276 {
277 0 return( $self->entity->as_form_data );
278 }
279 elsif( $type eq 'application/json' )
280 {
281 0 ▶ return( HTTP::Promise::Body::Form->new ) unless( length( $payload ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $payload 0 0 0
282 0 my $form;
283 # try-catch
284 0 local $@;
285 eval
286 0 {
287 0 my $hash = $self->new_json->decode( $payload );
288 0 $form = HTTP::Promise::Body::Form->new( $hash );
289 };
290 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
291 {
292 0 return( $self->error( "Error trying to decode the JSON payload: $@" ) );
293 }
294 0 return( $form );
295 }
296 ▶ elsif( $self->can( 'uri' ) &&
Condition0%0/12
$self->can("uri") and $uri = $self->uri and $uri->query and not defined $payload && length $payload
!ll&&!rl&&r
000
$self->can("uri") and $uri = $self->uri and $uri->query
!ll&&!rl&&r
000
$self->can("uri") and $uri = $self->uri
!ll&&!rl&&r
000
defined $payload && length $payload
!ll&&!rl&&r
000
MC/DC0%0/5
$self->can("uri") and $uri = $self->uri and $uri->query and not defined $payload && length $payload
$self->can("uri") $uri = $self->uri $uri->query defined $payload length $payload
Truth table0%0/6
A and B and C and not(D && E)
A= $self->can("uri")B= $uri = $self->uriC= $uri->queryD= defined $payloadE= length $payload
ABCDEresult
0XXXX0
10XXX0
110XX0
111110
1110X1
111101
297 ( $uri = $self->uri ) &&
298 $uri->query &&
299 ( !defined( $payload ) || !length( $payload ) ) )
300 {
301 0 my $hash = $uri->query_form_hash;
302 0 return( HTTP::Promise::Body::Form->new( $hash ) );
303 }
304 # elsif( $type eq 'application/x-www-form-urlencoded' )
305 else
306 {
307 0 ▶ return( length( $payload // '' ) ? HTTP::Promise::Body::Form->new( $payload ) : HTTP::Promise::Body::Form->new );
Branch: 1 branch
BranchTrueFalseTotal
length($payload // '') ? : 0 0 0
Condition0%0/2
$payload // ""
l!l
00
MC/DC0%0/1
$payload // ""
$payload
Truth table0%0/2
A
A= $payload
Aresult
11
00
308 }
309 }
310  
311 sub as_string
312 {
313 3 ▶ my( $self, $eol ) = @_;
Subroutine as_string: called CC=3 SCAR=11.4
Pod documented
314 3 ▶ $eol = $CRLF unless( defined( $eol ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $eol 3 0 3
315 3 my $ent = $self->entity;
316 # If there is no entity, we just print the headers and that's it.
317 3 ▶ return( $ent ? $ent->as_string( $eol ) : join( $eol, $self->start_line( $eol ), $self->headers->as_string( $eol ) ) . $eol );
Branch: 1 branch
BranchTrueFalseTotal
$ent ? : 3 0 3
318 }
319  
320 0 ▶sub boundary { return( shift->headers->boundary ); }
Subroutine boundary: not called CC=1 SCAR=6.9
Pod documented
321  
322 sub can
323 {
324 52 ▶ my( $self, $method ) = @_;
Subroutine can: called CC=6 SCAR=18.0
Pod documented
325  
326 52 ▶ if( my $own_method = $self->SUPER::can( $method ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $own_method = $self->SUPER::can($method)) 46 6 52
327 {
328 46 return( $own_method );
329 }
330  
331 6 ▶ my $headers = ref( $self ) ? $self->headers : 'HTTP::Promise::Headers';
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 6 0 6
332 6 my $trace = '';
333 6 ▶ my $debug = $self->debug // 0;
Condition50%1/2
$self->debug // 0
l!l
06
MC/DC0%0/1
$self->debug // 0
$self->debug
Truth table50%1/2
A
A= $self->debug
Aresult
11
00
334 6 ▶ if( $headers->can( $method ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($headers->can($method)) { } 3 3 6
335 {
336 # We create the function here so that it will not need to be
337 # autoloaded or recreated the next time.
338 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
339 3 ▶ eval( <<EOT );
Subroutine client_date: called CC=1 SCAR=0.0
Subroutine date: called CC=1 SCAR=0.0
Subroutine last_modified: called CC=1 SCAR=0.0
Pod undocumented
Pod documented
Pod undocumented
340 sub $method { return( shift->headers->$method( \@_ ) ); }
341 EOT
342 3 ▶ my $ref = $self->UNIVERSAL::can( $method ) || die( "AUTOLOAD inconsistency error for dynamic sub \"$method\"." );
Condition50%1/2
$self->UNIVERSAL::can($method) || die(qq[AUTOLOAD inconsistency error for dynamic sub "$method".])
l!l
30
MC/DC0%0/1
$self->UNIVERSAL::can($method) || die(qq[AUTOLOAD inconsistency error for dynamic sub "$method".])
$self->UNIVERSAL::can($method)
Truth table50%1/2
A
A= $self->UNIVERSAL::can($method)
Aresult
11
00
343 3 return( $ref );
344 }
345 else
346 {
347 }
348 3 return;
349 }
350  
351 sub clear
352 {
353 0 ▶ my $self = shift( @_ );
Subroutine clear: not called CC=4 SCAR=30.0
Pod documented
354 0 $self->headers->clear;
355 0 ▶ $self->entity->body->empty if( $self->entity && $self->entity->body );
Branch: 1 branch
BranchTrueFalseTotal
if $self->entity and $self->entity->body 0 0 0
Condition0%0/3
$self->entity and $self->entity->body
!ll&&!rl&&r
000
MC/DC0%0/2
$self->entity and $self->entity->body
$self->entity $self->entity->body
Truth table0%0/3
A and B
A= $self->entityB= $self->entity->body
ABresult
0X0
100
111
356 # $self->_parts->reset;
357 0 ▶ $self->entity->parts->reset if( $self->entity );
Branch: 1 branch
BranchTrueFalseTotal
if $self->entity 0 0 0
358 0 return;
359 }
360  
361 sub clone
362 {
363 0 ▶ my $self = shift( @_ );
Subroutine clone: not called CC=8 SCAR=42.8
Pod documented
364 0 my $opts = $self->_get_args_as_hash( @_ );
365 0 ▶ $opts->{clone_entity} //= 1;
Condition0%0/2
$opts->{"clone_entity"} //= 1
l!l
00
MC/DC0%0/1
$opts->{"clone_entity"} //= 1
$opts->{"clone_entity"}
Truth table0%0/2
A
A= $opts->{"clone_entity"}
Aresult
11
00
366 # my $new = $self->new( [], undef );
367 0 my $new = $self->SUPER::clone;
368 0 my $new_headers;
369 my $ent;
370 0 ▶ if( ( $ent = $self->entity ) && $opts->{clone_entity} )
Branch: 1 branch
BranchTrueFalseTotal
if ($ent = $self->entity and $opts->{'clone_entity'}) { } 0 0 0
Condition0%0/3
$ent = $self->entity and $opts->{"clone_entity"}
!ll&&!rl&&r
000
MC/DC0%0/2
$ent = $self->entity and $opts->{"clone_entity"}
$ent = $self->entity $opts->{"clone_entity"}
Truth table0%0/3
A and B
A= $ent = $self->entityB= $opts->{"clone_entity"}
ABresult
0X0
100
111
371 {
372 0 my $new_ent = $ent->clone( clone_message => 0 );
373 0 $new_headers = $new_ent->headers;
374 0 $new_ent->http_message( $new );
375 0 $new->entity( $new_ent );
376 }
377 else
378 {
379 0 $new_headers = $self->headers->clone;
380 }
381 0 $new->headers( $new_headers );
382 0 my $proto = $self->protocol;
383 0 my $vers = $self->version;
384 0 ▶ $new->protocol( "$proto" ) if( defined( $proto ) && length( $proto ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $proto and length $proto 0 0 0
Condition0%0/3
defined $proto and length $proto
!ll&&!rl&&r
000
MC/DC0%0/2
defined $proto and length $proto
defined $proto length $proto
Truth table0%0/3
A and B
A= defined $protoB= length $proto
ABresult
0X0
100
111
385 0 ▶ $new->version( "$vers" ) if( defined( $vers ) && length( $vers ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $vers and length $vers 0 0 0
Condition0%0/3
defined $vers and length $vers
!ll&&!rl&&r
000
MC/DC0%0/2
defined $vers and length $vers
defined $vers length $vers
Truth table0%0/3
A and B
A= defined $versB= length $vers
ABresult
0X0
100
111
386 0 $new->debug( $self->debug );
387 0 return( $new );
388 }
389  
390 sub content
391 {
392 0 ▶ my $self = shift( @_ );
Subroutine content: not called CC=25 SCAR=64.8
Pod documented
393 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
394 {
395 0 my $has_ref = 0;
396 0 for( @_ )
397 {
398 0 ▶ next unless( defined( $_ ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_ 0 0 0
399 0 ▶ return( $self->error( "I was expecting a string or a scalar reference, but instead got ", ref( $_ ) ) ) if( ref( $_ ) && !$self->_is_scalar( $_ ) );
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ and not $self->_is_scalar($_) 0 0 0
Condition0%0/3
ref $_ and not $self->_is_scalar($_)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_ and not $self->_is_scalar($_)
ref $_ not $self->_is_scalar($_)
Truth table0%0/3
A and not(B)
A= ref $_B= not $self->_is_scalar($_)
ABresult
0X0
100
111
400 # This affects how we set the content
401 0 ▶ $has_ref++ if( $self->_is_scalar( $_ ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_scalar($_) 0 0 0
402 0 ▶ $self->_utf8_downgrade( $self->_is_scalar( $_ ) ? $$_ : $_ ) ||
Branch: 2 branches
BranchTrueFalseTotal
$self->_utf8_downgrade($self->_is_scalar($_) ? $$_ : $_) or return $self->pass_error 0 0 0
$self->_is_scalar($_) ? : 0 0 0
403 return( $self->pass_error );
404 }
405 # $self->_parts->reset;
406 0 my $ent = $self->entity;
407 0 ▶ unless( $ent )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ent) 0 0 0
408 {
409 0 my $headers = $self->headers;
410 0 ▶ $self->_load_class( 'HTTP::Promise::Entity' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Entity") or return $self->pass_error 0 0 0
411 0 $ent = HTTP::Promise::Entity->new( headers => $headers, debug => $self->debug );
412 0 $self->entity( $ent );
413 }
414 0 my $body = $ent->body;
415 0 ▶ unless( $body )
Branch: 1 branch
BranchTrueFalseTotal
unless ($body) 0 0 0
416 {
417 0 $body = $ent->new_body( string => '' );
418 0 ▶ return( $self->pass_error( $ent->error ) ) if( !defined( $body ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 0 0
419 # $ent->body( $body ) || return( $self->pass_error( $ent->error ) );
420 0 my $rv = $ent->body( $body );
421 0 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 0 0
422 {
423 }
424 0 ▶ return( $self->pass_error( $ent->error ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
425 }
426
427 0 ▶ my $io = $body->open( '+>', { binmode => 'raw', autoflush => 1 } ) ||
Condition0%0/2
$body->open('+>', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($body->error))
l!l
00
MC/DC0%0/1
$body->open('+>', {'binmode', 'raw', 'autoflush', 1}) || (return $self->pass_error($body->error))
$body->open('+>', {'binmode', 'raw', 'autoflush', 1})
Truth table0%0/2
A
A= $body->open('+>', {'binmode', 'raw', 'autoflush', 1})
Aresult
11
00
428 return( $self->pass_error( $body->error ) );
429 0 ▶ if( $has_ref > 1 )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_ref > 1) { } 0 0 0
430 {
431 0 for( @_ )
432 {
433 0 ▶ $io->print( $self->_is_scalar( $_ ) ? $$_ : $_ ) || return( $self->pass_error( $io->error ) );
Branch: 2 branches
BranchTrueFalseTotal
$io->print($self->_is_scalar($_) ? $$_ : $_) or return $self->pass_error($io->error) 0 0 0
$self->_is_scalar($_) ? : 0 0 0
434 }
435 }
436 else
437 {
438 0 ▶ $io->print( ( @_ == 1 && $self->_is_scalar( $_[0] ) ) ? ${$_[0]} : @_ ) || return( $self->pass_error( $io->error ) );
Branch: 2 branches
BranchTrueFalseTotal
$io->print(@_ == 1 && $self->_is_scalar($_[0]) ? ${$_[0];} : @_) or return $self->pass_error($io->error) 0 0 0
@_ == 1 && $self->_is_scalar($_[0]) ? : 0 0 0
Condition0%0/3
@_ == 1 and $self->_is_scalar($_[0])
!ll&&!rl&&r
000
MC/DC0%0/2
@_ == 1 and $self->_is_scalar($_[0])
@_ == 1 $self->_is_scalar($_[0])
Truth table0%0/3
A and B
A= @_ == 1B= $self->_is_scalar($_[0])
ABresult
0X0
100
111
439 }
440 0 $io->close;
441 0 $ent->parts->reset;
442 0 return( $body );
443 }
444 else
445 {
446 0 my $ent = $self->entity;
447 0 ▶ return( '' ) if( !$ent );
Branch: 1 branch
BranchTrueFalseTotal
unless $ent 0 0 0
448 0 my $body = $ent->body;
449 0 ▶ return( '' ) if( !$body );
Branch: 1 branch
BranchTrueFalseTotal
unless $body 0 0 0
450 # This is a real bad idea if the body is huge...
451 # NOTE: content() returns a scalar object (Module::Generic::Scalar)
452 0 return( $body->as_string );
453 }
454 }
455  
456 # NOTE: an outdated method since nowadays everyone use UTF-8
457 sub content_charset
458 {
459 0 ▶ my $self = shift( @_ );
Subroutine content_charset: not called CC=2 SCAR=17.9
Pod documented
460 0 my $ent = $self->entity;
461 0 ▶ return( '' ) unless( $ent );
Branch: 1 branch
BranchTrueFalseTotal
unless $ent 0 0 0
462 0 return( $ent->content_charset );
463 }
464  
465 sub content_ref
466 {
467 0 ▶ my $self = shift( @_ );
Subroutine content_ref: not called CC=3 SCAR=24.8
Pod documented
468 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
469 {
470 0 ▶ return( $self->error( "Value provided is not a scalar reference." ) ) unless( $self->_is_scalar( $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_scalar($_[0]) 0 0 0
471 0 return( $self->content( @_ ) );
472 }
473 else
474 {
475 0 my $content = $self->content;
476 0 return( $content );
477 }
478 }
479  
480 sub decodable
481 {
482 1 ▶ my $self = shift( @_ );
Subroutine decodable: called CC=3 SCAR=11.3
Pod documented
483 1 local $@;
484 1 ▶ $self->_load_class( 'HTTP::Promise::Stream' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Stream") or return $self->pass_error 0 1 1
485 1 ▶ my $all = HTTP::Promise::Stream->decodable( 'browser' ) ||
Condition50%1/2
'HTTP::Promise::Stream'->decodable('browser') || (return $self->error('HTTP::Promise::Stream'->pass_error))
l!l
10
MC/DC0%0/1
'HTTP::Promise::Stream'->decodable('browser') || (return $self->error('HTTP::Promise::Stream'->pass_error))
'HTTP::Promise::Stream'->decodable('browser')
Truth table50%1/2
A
A= 'HTTP::Promise::Stream'->decodable('browser')
Aresult
11
00
486 return( $self->error( HTTP::Promise::Stream->pass_error ) );
487 1 return( $all );
488 }
489  
490 sub decode
491 {
492 0 ▶ my $self = shift( @_ );
Subroutine decode: not called CC=6 SCAR=37.4
Pod documented
493 0 my $headers = $self->headers;
494 0 my $ce = $headers->content_encoding;
495 0 ▶ return(1) if( !$ce || $ce->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if not $ce or $ce->is_empty 0 0 0
Condition0%0/3
not $ce or $ce->is_empty
l!l&&r!l&&!r
000
MC/DC0%0/2
not $ce or $ce->is_empty
not $ce $ce->is_empty
Truth table0%0/3
not(A) or B
A= not $ceB= $ce->is_empty
ABresult
1X1
011
000
496 0 ▶ my $ent = $self->entity || return(1);
Condition0%0/2
$self->entity || (return 1)
l!l
00
MC/DC0%0/1
$self->entity || (return 1)
$self->entity
Truth table0%0/2
A
A= $self->entity
Aresult
11
00
497 0 my $encodings = $ce->split( qr/[[:blank:]]*,[[:blank:]]*/ )->reverse->object;
498 0 ▶ return(1) if( $encodings->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if $encodings->is_empty 0 0 0
499 0 ▶ my $body = $ent->decode_body( $encodings ) || return( $self->pass_error( $ent->error ) );
Condition0%0/2
$ent->decode_body($encodings) || (return $self->pass_error($ent->error))
l!l
00
MC/DC0%0/1
$ent->decode_body($encodings) || (return $self->pass_error($ent->error))
$ent->decode_body($encodings)
Truth table0%0/2
A
A= $ent->decode_body($encodings)
Aresult
11
00
500 # Altering existing headers value is really really bad. This is done in HTTP::Message,
501 # but not in our class
502 # $self->remove_header( qw( Content-Encoding Content-Length Content-MD5 ) );
503 0 return(1);
504 }
505  
506 sub decode_content
507 {
508 4 ▶ my $self = shift( @_ );
Subroutine decode_content: called CC=7 SCAR=29.2
Pod documented
509 4 ▶ my $ent = $self->entity || return(0);
Condition50%1/2
$self->entity || (return 0)
l!l
40
MC/DC0%0/1
$self->entity || (return 0)
$self->entity
Truth table50%1/2
A
A= $self->entity
Aresult
11
00
510 4 my $opts = $self->_get_args_as_hash( @_ );
511 4 ▶ my $body = $ent->body || return(0);
Condition50%1/2
$ent->body || (return 0)
l!l
40
MC/DC0%0/1
$ent->body || (return 0)
$ent->body
Truth table50%1/2
A
A= $ent->body
Aresult
11
00
512 4 ▶ return( $body ) if( !$ent->is_encoded );
Branch: 1 branch
BranchTrueFalseTotal
unless $ent->is_encoded 4 0 4
513 0 my $ce = $self->headers->content_encoding;
514 0 ▶ if( $ce )
Branch: 1 branch
BranchTrueFalseTotal
if ($ce) 0 0 0
515 {
516 # object(9 is a noop to ensure an object is returned and not a list
517 0 ▶ $body = $ent->decode_body( $ce->split( qr/[[:blank:]\h]*\,[[:blank:]\h]*/ )->reverse->object, ( scalar( keys( %$opts ) ) ? $opts : () ) ) || return( $self->pass_error( $ent->error ) );
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$opts ? : 0 0 0
Condition0%0/2
$ent->decode_body($ce->split(qr/[[:blank:]\h]*\,[[:blank:]\h]*/)->reverse->object, scalar keys %$opts ? $opts : ()) || (return $self->pass_error($ent->error))
l!l
00
MC/DC0%0/1
$ent->decode_body($ce->split(qr/[[:blank:]\h]*\,[[:blank:]\h]*/)->reverse->object, scalar keys %$opts ? $opts : ()) || (return $self->pass_error($ent->error))
$ent->decode_body($ce->split(qr/[[:blank:]\h]*\,[[:blank:]\h]*/)->reverse->object, scalar keys %$opts ? $opts : ())
Truth table0%0/2
A
A= $ent->decode_body($ce->split(qr/[[:blank:]\h]*\,[[:blank:]\h]*/)->reverse->object, scalar keys %$opts ? $opts : ())
Aresult
11
00
518 # $ent->is_decoded(1);
519 }
520 0 return( $body );
521 }
522  
523 sub decoded_content
524 {
525 4 ▶ my $self = shift( @_ );
Subroutine decoded_content: called CC=20 SCAR=37.2
Pod documented
526 4 my $opts = $self->_get_args_as_hash( @_ );
527 4 ▶ $opts->{charset_strict} //= 0;
Condition50%1/2
$opts->{"charset_strict"} //= 0
l!l
04
MC/DC0%0/1
$opts->{"charset_strict"} //= 0
$opts->{"charset_strict"}
Truth table50%1/2
A
A= $opts->{"charset_strict"}
Aresult
11
00
528 4 my $old_fatal = $self->fatal;
529 4 ▶ $self->fatal( $opts->{raise_error} ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'raise_error'} ? : 0 4 4
530 4 ▶ my $body = $self->decode_content( ( scalar( keys( %$opts ) ) ? $opts : () ) );
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$opts ? : 4 0 4
531 4 ▶ return( $self->pass_error ) if( !defined( $body ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $body 0 4 4
532 # There is no entity or no body
533 4 ▶ if( !$body )
Branch: 1 branch
BranchTrueFalseTotal
unless ($body) 0 4 4
534 {
535 0 return( $self->new_scalar );
536 }
537 4 $self->fatal( $old_fatal );
538 4 my $dummy = '';
539 4 ▶ return( $opts->{ref} ? \$dummy : $dummy ) if( $body->is_empty );
Branch: 2 branches
BranchTrueFalseTotal
if $body->is_empty 0 4 4
$opts->{'ref'} ? : 0 0 0
540 4 ▶ unless( $opts->{binmode} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts->{"binmode"}) 4 0 4
541 {
542 # Need to explicitly provide the body to get the encoding from, otherwise, io_encoding()
543 # would get the default one, which might not yet be replaced with its decoded version.
544 4 my $enc = $self->entity->io_encoding( body => $body, charset_strict => $opts->{charset_strict} );
545 4 ▶ $opts->{binmode} = $enc if( $enc );
Branch: 1 branch
BranchTrueFalseTotal
if $enc 4 0 4
546 }
547
548 # Because of an edge case where open with :binmode(utf-8) layer does not decode properly \x{FF}
549 # but Encode::decode( 'utf-8', $buff ) does, and since the body is loaded into a string
550 # anyway, we first read the data as raw and then decode it with Encode
551 4 my $binmode;
552 4 ▶ 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") 4 0 4
Condition33%2/6
exists $opts->{"binmode"} and length $opts->{"binmode"} and lc substr($opts->{"binmode"}, 0, 3) eq "utf"
!ll&&!rl&&r
004
exists $opts->{"binmode"} and length $opts->{"binmode"}
!ll&&!rl&&r
004
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
553 length( $opts->{binmode} ) &&
554 lc( substr( $opts->{binmode}, 0, 3 ) ) eq 'utf' )
555 {
556 4 $binmode = delete( $opts->{binmode} );
557 4 $opts->{binmode} = 'raw';
558 }
559 4 ▶ my $content = $body->as_string( ( scalar( keys( %$opts ) ) ? $opts : () ) );
Branch: 1 branch
BranchTrueFalseTotal
scalar keys %$opts ? : 4 0 4
560 4 ▶ if( !defined( $content ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $content) 0 4 4
561 {
562 0 return( $self->pass_error( $body->error ) );
563 }
564 4 ▶ if( defined( $binmode ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $binmode) 4 0 4
565 {
566 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
567 # try-catch
568 4 local $@;
569 eval
570 4 {
571 4 $$content = Encode::decode( $binmode, $$content, ( Encode::FB_DEFAULT | Encode::LEAVE_SRC ) );
572 };
573 4 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
574 {
575 0 return( $self->error( "Error decoding body content with character encoding '$binmode': $@" ) );
576 }
577 }
578
579 # $content is a scalar object that stringifies
580 4 ▶ if( $self->headers->content_is_xml )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->headers->content_is_xml) 0 4 4
581 {
582 # Get rid of the XML encoding declaration if present (\x{FEFF})
583 6 ▶ $$content =~ s/^\N{BOM}//;
Subroutine BEGIN: called CC=1 SCAR=0.2
584 0 ▶ if( $$content =~ m/^(?<decl>[[:blank:]\h\v]*<\?xml(.*?)\?>)/is )
Branch: 1 branch
BranchTrueFalseTotal
if ($$content =~ /^(?<decl>[[:blank:]\h\v]*<\?xml(.*?)\?>)/is) 0 0 0
585 {
586 0 substr( $$content, 0, length( $+{decl} ) ) =~ s{
587 [[:blank:]\h\v]+
588 encoding[[:blank:]\h\v]*=[[:blank:]\h\v]*
589 (?<quote>["'])
590 (?<encoding>(?>\\\g{quote}|(?!\g{quote}).)*+)
591 \g{quote}
592 }
593 {}xmis;
594 }
595 }
596 4 return( $content );
597 }
598  
599 sub decoded_content_utf8
600 {
601 0 ▶ my $self = shift( @_ );
Subroutine decoded_content_utf8: not called CC=5 SCAR=34.0
Pod documented
602 0 my $opts = $self->_get_args_as_hash( @_ );
603 0 $opts->{binmode} = 'utf-8';
604 0 my $data = $self->decoded_content( $opts );
605 0 ▶ if( !defined( $data ) && $self->error )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $data and $self->error) 0 0 0
Condition0%0/3
not defined $data and $self->error
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $data and $self->error
not defined $data $self->error
Truth table0%0/3
not(A) and B
A= not defined $dataB= $self->error
ABresult
0X0
100
111
606 {
607 0 return( $self->pass_error );
608 }
609  
610 0 ▶ if( $self->headers->content_is_xml )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->headers->content_is_xml) 0 0 0
611 {
612 # Get rid of the XML encoding declaration if present
613 0 $$data =~ s/^\x{FEFF}//;
614
615 0 ▶ if( $$data =~ /^(\s*<\?xml[^\x00]*?\?>)/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($$data =~ /^(\s*<\?xml[^\x00]*?\?>)/) 0 0 0
616 {
617 0 substr( $$data, 0, length($1)) =~ s/\sencoding\s*=\s*(["']).*?\1//;
618 }
619 }
620 0 return( $data );
621 }
622  
623 sub decoded_json
624 {
625 0 ▶ my $self = shift( @_ );
Subroutine decoded_json: not called CC=7 SCAR=40.3
Pod documented
626 0 my $opts = $self->_get_args_as_hash( @_ );
627 0 $opts->{binmode} = 'utf-8';
628 0 my $data = $self->decoded_content_utf8( $opts );
629 0 ▶ return( $self->pass_error ) if( !defined( $data ) && $self->error );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $data and $self->error 0 0 0
Condition0%0/3
not defined $data and $self->error
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $data and $self->error
not defined $data $self->error
Truth table0%0/3
not(A) and B
A= not defined $dataB= $self->error
ABresult
0X0
100
111
630 0 ▶ return if( !defined( $data ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $data 0 0 0
631 0 ▶ if( $self->headers->content_is_json )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->headers->content_is_json) 0 0 0
632 {
633 0 local $@;
634 # try-catch
635 0 my $ref = $self->new_json_safe->relaxed->decode( $$data );
636 0 ▶ if( !defined( $ref ) && $self->error )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ref and $self->error) 0 0 0
Condition0%0/3
not defined $ref and $self->error
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $ref and $self->error
not defined $ref $self->error
Truth table0%0/3
not(A) and B
A= not defined $refB= $self->error
ABresult
0X0
100
111
637 {
638 0 my $err = $self->error->message;
639 0 return( $self->error( "Error decoding JSON payload: $err\nPayload was: $$data" ) );
640 }
641 0 return( $ref );
642 }
643 0 return( $$data );
644 }
645  
646 sub dump
647 {
648 3 ▶ my $self = shift( @_ );
Subroutine dump: called CC=14 SCAR=39.5
Pod documented
649 3 my $opts = $self->_get_args_as_hash( @_ );
650 3 my $content = '';
651 3 my $ent = $self->entity;
652 3 my $maxlen = $opts->{maxlength};
653 3 ▶ $maxlen = 512 unless( defined( $maxlen ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $maxlen 3 0 3
654 3 my $no_content = $opts->{no_content};
655 3 ▶ $no_content = "(no content)" unless( defined( $no_content ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $no_content 3 0 3
656 3 ▶ if( $ent && $ent->body )
Branch: 1 branch
BranchTrueFalseTotal
if ($ent and $ent->body) 0 3 3
Condition33%1/3
$ent and $ent->body
!ll&&!rl&&r
030
MC/DC0%0/2
$ent and $ent->body
$ent $ent->body
Truth table33%1/3
A and B
A= $entB= $ent->body
ABresult
0X0
100
111
657 {
658 0 ▶ my $io = $ent->body->open( '<', { binmode => 'raw' } ) ||
Condition0%0/2
$ent->body->open('<', {'binmode', 'raw'}) || (return $self->pass_error($ent->error))
l!l
00
MC/DC0%0/1
$ent->body->open('<', {'binmode', 'raw'}) || (return $self->pass_error($ent->error))
$ent->body->open('<', {'binmode', 'raw'})
Truth table0%0/2
A
A= $ent->body->open('<', {'binmode', 'raw'})
Aresult
11
00
659 return( $self->pass_error( $ent->error ) );
660 0 my $bytes = $io->read( $content, $maxlen );
661 0 ▶ return( $self->pass_error( $io->error ) ) if( !defined( $bytes ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 0 0
662 0 $io->close;
663 }
664 3 my $chopped = 0;
665 3 ▶ if( length( $content ) )
Branch: 1 branch
BranchTrueFalseTotal
if (length $content) { } 0 3 3
666 {
667 0 ▶ if( $ent->body->length > $maxlen )
Branch: 1 branch
BranchTrueFalseTotal
if ($ent->body->length > $maxlen) 0 0 0
668 {
669 0 $content .= '...';
670 0 $chopped = $ent->body->length - $maxlen;
671 }
672 0 $content =~ s/\\/\\\\/g;
673 0 $content =~ s/\t/\\t/g;
674 0 $content =~ s/\r/\\r/g;
675  
676 # no need for 3 digits in escape for these
677 0 $content =~ s/([\0-\11\13-\037])(?!\d)/sprintf('\\%o',ord($1))/eg;
678  
679 0 $content =~ s/([\0-\11\13-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg;
680 0 $content =~ s/([^\12\040-\176])/sprintf('\\x{%X}',ord($1))/eg;
681  
682 # remaining whitespace
683 0 $content =~ s/( +)\n/("\\40" x length($1)) . "\n"/eg;
684 0 $content =~ s/(\n+)\n/("\\n" x length($1)) . "\n"/eg;
685 0 $content =~ s/\n\z/\\n/;
686 0 ▶ if( $content eq $no_content )
Branch: 1 branch
BranchTrueFalseTotal
if ($content eq $no_content) 0 0 0
687 {
688 # escape our $no_content marker
689 0 $content =~ s/^(.)/sprintf('\\x%02X',ord($1))/eg;
690 }
691 }
692 else
693 {
694 3 $content = $no_content;
695 }
696
697 3 my @dump;
698 3 ▶ push( @dump, $opts->{preheader} ) if( $opts->{preheader} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"preheader"} 3 0 3
699 3 push( @dump, $self->headers->as_string, $content );
700 3 ▶ push( @dump, "(+ $chopped more bytes not shown)" ) if( $chopped );
Branch: 1 branch
BranchTrueFalseTotal
if $chopped 0 3 3
701  
702 3 my $dump = join( "\n", @dump, '' );
703 3 ▶ $dump =~ s/^/$opts->{prefix}/gm if( $opts->{prefix} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"prefix"} 0 3 3
704  
705 3 ▶ print( $dump ) unless( defined( wantarray() ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined wantarray 1 2 3
706 3 return( $dump );
707 }
708  
709 sub encode
710 {
711 0 ▶ my $self = shift( @_ );
Subroutine encode: not called CC=12 SCAR=50.5
Pod documented
712 0 my $opts = $self->_get_args_as_hash( \@_, args_list => [qw( update_header )] );
713 0 my( @enc ) = @_;
714 0 ▶ $opts->{update_header} //= 1;
Condition0%0/2
$opts->{"update_header"} //= 1
l!l
00
MC/DC0%0/1
$opts->{"update_header"} //= 1
$opts->{"update_header"}
Truth table0%0/2
A
A= $opts->{"update_header"}
Aresult
11
00
715  
716 0 ▶ return( $self->error( "Cannot encode multipart/* messages" ) ) if( $self->content_type =~ m,^multipart/, );
Branch: 1 branch
BranchTrueFalseTotal
if $self->content_type =~ m[^multipart/] 0 0 0
717 0 ▶ return( $self->error( "Cannot encode message/* messages" ) ) if( $self->content_type =~ m,^message/, );
Branch: 1 branch
BranchTrueFalseTotal
if $self->content_type =~ m[^message/] 0 0 0
718 0 my $headers = $self->headers;
719 0 my $e = $headers->content_encoding->split( qr/[[:blank:]\h]*,[[:blank:]\h]*/ );
720 0 my $source = 'argv';
721 0 my $encodings;
722 0 ▶ if( @enc )
Branch: 1 branch
BranchTrueFalseTotal
if (@enc) { } 0 0 0
723 {
724 0 $encodings = $self->new_array( \@enc );
725 }
726 else
727 {
728 0 $source = 'header';
729 0 $encodings = $e;
730 }
731 # nothing to do
732 0 ▶ return(1) if( !$encodings || $encodings->is_empty );
Branch: 1 branch
BranchTrueFalseTotal
if not $encodings or $encodings->is_empty 0 0 0
Condition0%0/3
not $encodings or $encodings->is_empty
l!l&&r!l&&!r
000
MC/DC0%0/2
not $encodings or $encodings->is_empty
not $encodings $encodings->is_empty
Truth table0%0/3
not(A) or B
A= not $encodingsB= $encodings->is_empty
ABresult
1X1
011
000
733 0 ▶ my $ent = $self->entity || return(1);
Condition0%0/2
$self->entity || (return 1)
l!l
00
MC/DC0%0/1
$self->entity || (return 1)
$self->entity
Truth table0%0/2
A
A= $self->entity
Aresult
11
00
734 0 $encodings->unique(1);
735 0 ▶ my $body = $ent->encode_body( $encodings ) || return( $self->pass_error( $ent->error ) );
Condition0%0/2
$ent->encode_body($encodings) || (return $self->pass_error($ent->error))
l!l
00
MC/DC0%0/1
$ent->encode_body($encodings) || (return $self->pass_error($ent->error))
$ent->encode_body($encodings)
Truth table0%0/2
A
A= $ent->encode_body($encodings)
Aresult
11
00
736 0 $ent->is_encoded(1);
737 0 ▶ if( $opts->{update_header} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"update_header"}) 0 0 0
738 {
739 0 ▶ if( $source eq 'argv' )
Branch: 1 branch
BranchTrueFalseTotal
if ($source eq "argv") 0 0 0
740 {
741 0 ▶ if( $e )
Branch: 1 branch
BranchTrueFalseTotal
if ($e) { } 0 0 0
742 {
743 0 $e->push( $encodings->list );
744 }
745 else
746 {
747 0 $e = $encodings;
748 }
749 0 $e->unique(1);
750 }
751 0 $headers->content_encoding( $e->join( ', ' )->scalar );
752 0 $headers->remove_header( qw( Content-Length Content-MD5 ) );
753 }
754 0 return(1);
755 }
756  
757 # sub entity { return( shift->_set_get_object_without_init( 'entity', 'HTTP::Promise::Entity', @_ ) ); }
758 sub entity
759 {
760 81 ▶ my $self = shift( @_ );
Subroutine entity: called CC=4 SCAR=14.3
Pod documented
761 81 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 36 45 81
762 {
763 36 return( $self->_set_get_object_without_init( 'entity', 'HTTP::Promise::Entity', @_ ) );
764 }
765 45 ▶ if( $self->_is_a( $self->{entity} => 'HTTP::Promise::Entity' ) &&
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($self->{"entity"}, "HTTP::Promise::Entity") and not $self->{"entity"}{"http_message"}) 0 45 45
Condition66%2/3
$self->_is_a($self->{"entity"}, "HTTP::Promise::Entity") and not $self->{"entity"}{"http_message"}
!ll&&!rl&&r
31140
MC/DC0%0/2
$self->_is_a($self->{"entity"}, "HTTP::Promise::Entity") and not $self->{"entity"}{"http_message"}
$self->_is_a($self->{"entity"}, "HTTP::Promise::Entity") not $self->{"entity"}{"http_message"}
Truth table66%2/3
A and not(B)
A= $self->_is_a($self->{"entity"}, "HTTP::Promise::Entity")B= not $self->{"entity"}{"http_message"}
ABresult
0X0
100
111
766 !$self->{entity}->{http_message} )
767 {
768 0 $self->{entity}->{http_message} = $self;
769 }
770 45 return( $self->_set_get_object_without_init( 'entity', 'HTTP::Promise::Entity' ) );
771 }
772  
773 8 ▶sub header { return( shift->headers->header( @_ ) ); }
Subroutine header: called CC=1 SCAR=0.0
Pod documented
774  
775 sub headers
776 {
777 42 ▶ my $self = shift( @_ );
Subroutine headers: called CC=6 SCAR=30.9
Pod documented
778 42 ▶ if( @_ )
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 0 42 42
elsif (not $self->{'headers'}) { } 0 42 42
779 {
780 0 my $v = shift( @_ );
781 0 ▶ if( $self->_is_a( $v, 'HTTP::Promise::Headers' ) )
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_is_a($v, 'HTTP::Promise::Headers')) { } 0 0 0
elsif ($self->_is_a($v, 'HTTP::Headers')) { } 0 0 0
elsif ($self->_is_array($v)) { } 0 0 0
782 {
783 0 $self->{headers} = $v;
784 }
785 elsif( $self->_is_a( $v, 'HTTP::Headers' ) )
786 {
787 0 my $h = $v->clone;
788 0 $self->{headers} = bless( $h => 'HTTP::Promise::Headers' );
789 }
790 elsif( $self->_is_array( $v ) )
791 {
792 0 $self->{headers} = HTTP::Promise::Headers->new( @$v );
793 }
794 else
795 {
796 0 return( $self->error( "Bad value for headers. I was expecting either an array reference or a HTTP::Promise::Headers or a HTTP::Headers object and I got instead '", overload::StrVal( $v ), "'." ) );
797 }
798 }
799 elsif( !$self->{headers} )
800 {
801 0 $self->{headers} = HTTP::Promise::Headers->new;
802 }
803 42 return( $self->{headers} );
804 }
805  
806 0 ▶sub headers_as_string { return( shift->headers->as_string( @_ ) ); }
Subroutine headers_as_string: not called CC=1 SCAR=6.9
Pod documented
807  
808 sub is_encoding_supported
809 {
810 0 ▶ my $self = shift( @_ );
Subroutine is_encoding_supported: not called CC=4 SCAR=30.0
Pod documented
811 0 my $enc = shift( @_ );
812 0 ▶ return( $self->error( "No encoding provided." ) ) if( !defined( $enc ) || !length( $enc ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $enc and length $enc 0 0 0
Condition0%0/3
defined $enc and length $enc
!ll&&!rl&&r
000
MC/DC0%0/2
defined $enc and length $enc
defined $enc length $enc
Truth table0%0/3
A and B
A= defined $encB= length $enc
ABresult
0X0
100
111
813 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
814 0 return( HTTP::Promise::Stream->supported( lc( $enc ) ) );
815 }
816  
817 sub make_boundary
818 {
819 0 ▶ my $self = shift( @_ );
Subroutine make_boundary: not called CC=1 SCAR=6.9
Pod documented
820 0 my $uuid = Data::UUID->new;
821 0 my $boundary = $uuid->create_str;
822 0 return( "$boundary" );
823 }
824  
825 sub parse
826 {
827 0 ▶ my $self = shift( @_ );
Subroutine parse: not called CC=9 SCAR=45.0
Pod documented
828 0 my $str = shift( @_ );
829 0 ▶ return( $self->error( "No http headers string was provided to parse." ) ) if( !defined( $str ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
830 0 my $opts = $self->_get_args_as_hash( @_ );
831 0 ▶ $opts->{debug} = $self->debug if( !CORE::exists( $opts->{debug} ) && ref( $self ) );
Branch: 1 branch
BranchTrueFalseTotal
if not exists $opts->{"debug"} and ref $self 0 0 0
Condition0%0/3
not exists $opts->{"debug"} and ref $self
!ll&&!rl&&r
000
MC/DC0%0/2
not exists $opts->{"debug"} and ref $self
not exists $opts->{"debug"} ref $self
Truth table0%0/3
not(A) and B
A= not exists $opts->{"debug"}B= ref $self
ABresult
0X0
100
111
832 # Nothing to parse, we return a dummy object in line with legacy api of HTTP::Message
833 0 ▶ if( !length( "${str}" ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (length "$str") 0 0 0
834 {
835 0 return( HTTP::Promise::Message->new( { debug => $opts->{debug} } ) );
836 }
837 0 ▶ $self = HTTP::Promise::Message->new( { debug => $opts->{debug} } ) if( !ref( $self ) );
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
838 0 ▶ $self->_load_class( 'HTTP::Promise::Parser' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Parser") or return $self->pass_error 0 0 0
839 0 my $p = HTTP::Promise::Parser->new( debug => $opts->{debug} );
840 0 my $copy = $str;
841 0 $copy =~ s/\r/\\r/g;
842 0 $copy =~ s/\n/\\n/g;
843 0 my $ent = $p->parse( \$str );
844 0 ▶ if( !defined( $ent ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ent) 0 0 0
845 {
846 # We do not support the legacy way of accepting an HTTP message that has no header
847 0 return( $self->pass_error( $p->error ) );
848 }
849 0 my $msg = $ent->http_message;
850 0 ▶ unless( $msg )
Branch: 1 branch
BranchTrueFalseTotal
unless ($msg) 0 0 0
851 {
852 0 my $headers = $ent->headers;
853 0 $msg = HTTP::Promise::Message->new( $headers );
854 0 $msg->entity( $ent );
855 }
856 0 return( $msg );
857 }
858  
859 # NOTE: parts() will parse the current content and break it down into parts if applicable
860 # otherwise, it will simply return the array object $parts, which would be empty.
861 # It would be nice to come up with some efficient caching mechanism to avoid the if..elsif
862 # at the beginning of the subroutine.
863 sub parts
864 {
865 0 ▶ my $self = shift( @_ );
Subroutine parts: not called CC=25 SCAR=64.8
Pod documented
866 0 my $ent = $self->entity;
867 0 ▶ if( $ent &&
Branch: 2 branches
BranchTrueFalseTotal
if ($ent and $ent->parts->is_empty and $ent->body and not $ent->body->is_empty) { } 0 0 0
elsif ($ent and $ent->parts->is_empty and $ent->body and $ent->body->is_empty) { } 0 0 0
Condition0%0/9
$ent and $ent->parts->is_empty and $ent->body and not $ent->body->is_empty
!ll&&!rl&&r
000
$ent and $ent->parts->is_empty and $ent->body
!ll&&!rl&&r
000
$ent and $ent->parts->is_empty
!ll&&!rl&&r
000
MC/DC0%0/4
$ent and $ent->parts->is_empty and $ent->body and not $ent->body->is_empty
$ent $ent->parts->is_empty $ent->body not $ent->body->is_empty
Truth table0%0/5
A and B and C and not(D)
A= $entB= $ent->parts->is_emptyC= $ent->bodyD= not $ent->body->is_empty
ABCDresult
0XXX0
10XX0
110X0
11100
11111
868 $ent->parts->is_empty &&
869 $ent->body &&
870 !$ent->body->is_empty )
871 {
872 0 ▶ $self->_make_parts || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_make_parts or return $self->pass_error 0 0 0
873 }
874 ▶ elsif( $ent &&
Condition0%0/9
$ent and $ent->parts->is_empty and $ent->body and $ent->body->is_empty
!ll&&!rl&&r
000
$ent and $ent->parts->is_empty and $ent->body
!ll&&!rl&&r
000
$ent and $ent->parts->is_empty
!ll&&!rl&&r
000
MC/DC0%0/4
$ent and $ent->parts->is_empty and $ent->body and $ent->body->is_empty
$ent $ent->parts->is_empty $ent->body $ent->body->is_empty
Truth table0%0/5
A and B and C and D
A= $entB= $ent->parts->is_emptyC= $ent->bodyD= $ent->body->is_empty
ABCDresult
0XXX0
10XX0
110X0
11100
11111
875 $ent->parts->is_empty &&
876 $ent->body &&
877 $ent->body->is_empty )
878 {
879 0 $ent->body( undef );
880 }
881
882 0 ▶ unless( $ent )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ent) 0 0 0
883 {
884 0 ▶ $self->_load_class( 'HTTP::Promise::Entity' ) || return( $self->_pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Entity") or return $self->_pass_error 0 0 0
885 0 ▶ $ent = HTTP::Promise::Entity->new( headers => $self->headers, debug => $self->debug ) ||
Condition0%0/2
'HTTP::Promise::Entity'->new('headers', $self->headers, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::Entity'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Entity'->new('headers', $self->headers, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::Entity'->error))
'HTTP::Promise::Entity'->new('headers', $self->headers, 'debug', $self->debug)
Truth table0%0/2
A
A= 'HTTP::Promise::Entity'->new('headers', $self->headers, 'debug', $self->debug)
Aresult
11
00
886 return( $self->pass_error( HTTP::Promise::Entity->error ) );
887 0 $self->entity( $ent );
888 }
889
890 # Parts have been provided, add them if suitable
891 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
892 {
893 0 ▶ my @parts = map{ $self->_is_array( $_ ) ? @$_ : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_array($_) ? : 0 0 0
894 0 ▶ my $ct = $self->content_type || '';
Condition0%0/2
$self->content_type || ''
l!l
00
MC/DC0%0/1
$self->content_type || ''
$self->content_type
Truth table0%0/2
A
A= $self->content_type
Aresult
11
00
895 0 ▶ if( $ct =~ m,^message/, )
Branch: 2 branches
BranchTrueFalseTotal
if ($ct =~ m[^message/]) { } 0 0 0
elsif (not $ct =~ m[^multipart/]) { } 0 0 0
896 {
897 0 ▶ return( $self->error( "Only one part allowed for $ct content" ) ) if( @parts > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if @parts > 1 0 0 0
898 }
899 elsif( $ct !~ m,^multipart/, )
900 {
901 0 $self->remove_content_headers;
902 0 $self->content_type( 'multipart/mixed' );
903 }
904 # $self->_parts( \@parts );
905 0 ▶ $self->_load_class( 'HTTP::Promise::Entity' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Entity") or return $self->pass_error 0 0 0
906 0 my @new = ();
907 0 for( @parts )
908 {
909 # Either a HTTP::Promise::Request, or a HTTP::Promise::Response, or even a HTTP::Promise::Message
910 0 ▶ unless( $self->_is_a( $_ => 'HTTP::Promise::Entity' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_a($_, "HTTP::Promise::Entity")) 0 0 0
911 {
912 0 ▶ return( $self->error( "Part object provided (", overload::StrVal( $_ ), ") is neither a HTTP::Promise::Entity or a HTTP::Promise::Message object." ) ) if( !$self->_is_a( $_ => 'HTTP::Promise::Message' ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_a($_, "HTTP::Promise::Message") 0 0 0
913 0 my $ent = $_->entity;
914 0 ▶ unless( $ent )
Branch: 1 branch
BranchTrueFalseTotal
unless ($ent) 0 0 0
915 {
916 0 ▶ $ent = HTTP::Promise::Entity->new( headers => $_->headers, debug => $self->debug ) ||
Condition0%0/2
'HTTP::Promise::Entity'->new('headers', $_->headers, 'debug', $self->debug) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
'HTTP::Promise::Entity'->new('headers', $_->headers, 'debug', $self->debug) || (return $self->pass_error)
'HTTP::Promise::Entity'->new('headers', $_->headers, 'debug', $self->debug)
Truth table0%0/2
A
A= 'HTTP::Promise::Entity'->new('headers', $_->headers, 'debug', $self->debug)
Aresult
11
00
917 return( $self->pass_error );
918 0 $_->entity( $ent );
919 }
920 0 push( @new, $ent );
921 0 next;
922 }
923 0 push( @new, $_ );
924 }
925 0 $ent->parts( \@new );
926 }
927 0 my $parts = $ent->parts;
928 0 return( $parts );
929 }
930  
931 sub protocol
932 {
933 16 ▶ my $self = shift( @_ );
Subroutine protocol: called CC=3 SCAR=11.1
Pod documented
934 16 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 9 16
935 {
936 7 my $v = shift( @_ );
937 7 $v =~ s/^[[:blank:]\h]+|[[:blank:]\h]+$//g;
938 7 ▶ if( $v =~ m,^${HTTP_VERSION}$, )
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ /^$HTTP::Promise::Message::HTTP_VERSION$/) { } 7 0 7
939 {
940 7 $self->version( $+{http_version} );
941 }
942 else
943 {
944 0 return( $self->error( "Bad protocol value \"$v\". It should be something like HTTP/1.1" ) );
945 }
946 7 return( $self->_set_get_scalar_as_object( protocol => $v ) );
947 }
948 9 return( $self->_set_get_scalar_as_object( 'protocol' ) );
949 }
950  
951 # NOTE: This method is superseded by the one in HTTP::Promise::Request or HTTP::Promise::Response
952 0 ▶sub start_line { return( '' ) }
Subroutine start_line: not called CC=1 SCAR=6.9
Pod documented
953  
954 12 ▶sub version { return( shift->_set_get_number( 'version', @_ ) ); }
Subroutine version: called CC=1 SCAR=0.0
Pod documented
955  
956 # NOTE: _make_parts() is different from HTTP::Promise::Entity::make_multipart()
957 # This creates private parts attribute from current content (current entity body)
958 # whereas HTTP::Promise::Entity::make_multipart keeps and transforms the current content
959 # into multipart representation.
960 sub _make_parts
961 {
962 0 ▶ my $self = shift( @_ );
Subroutine _make_parts: not called CC=31 SCAR=69.0
963 0 my $type = $self->headers->type;
964 # my $parts = $self->_parts;
965 0 ▶ my $ent = $self->entity ||
Condition0%0/2
$self->entity || (return $self->error('No entity object is set.'))
l!l
00
MC/DC0%0/1
$self->entity || (return $self->error('No entity object is set.'))
$self->entity
Truth table0%0/2
A
A= $self->entity
Aresult
11
00
966 return( $self->error( "No entity object is set." ) );
967 0 my $body = $ent->body;
968 0 my $parts = $ent->parts;
969 0 ▶ return( $parts ) unless( defined( $type ) && length( $type ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $type and length $type 0 0 0
Condition0%0/3
defined $type and length $type
!ll&&!rl&&r
000
MC/DC0%0/2
defined $type and length $type
defined $type length $type
Truth table0%0/3
A and B
A= defined $typeB= length $type
ABresult
0X0
100
111
970 0 my $toptype = lc( [split( '/', $type, 2 )]->[0] );
971 # Nothing to do
972 0 ▶ return( $parts ) if( !$body );
Branch: 1 branch
BranchTrueFalseTotal
unless $body 0 0 0
973 0 ▶ if( $toptype eq 'multipart' )
Branch: 3 branches
BranchTrueFalseTotal
if ($toptype eq 'multipart') { } 0 0 0
elsif ($type eq 'message/http') { } 0 0 0
elsif ($toptype eq 'message') { } 0 0 0
974 {
975 # Now parse the raw data saved earlier
976 0 ▶ my $fh = $body->open( '+<', { binmode => 'raw' } ) ||
Condition0%0/2
$body->open('+<', {'binmode', 'raw'}) || (return $self->pass_error($ent->body->error))
l!l
00
MC/DC0%0/1
$body->open('+<', {'binmode', 'raw'}) || (return $self->pass_error($ent->body->error))
$body->open('+<', {'binmode', 'raw'})
Truth table0%0/2
A
A= $body->open('+<', {'binmode', 'raw'})
Aresult
11
00
977 return( $self->pass_error( $ent->body->error ) );
978 0 ▶ $self->_load_class( 'HTTP::Promise::IO' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::IO") or return $self->pass_error 0 0 0
979 0 ▶ my $reader = HTTP::Promise::IO->new( $fh, max_read_buffer => 4096, debug => $self->debug ) ||
Condition0%0/2
'HTTP::Promise::IO'->new($fh, 'max_read_buffer', 4096, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::IO'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::IO'->new($fh, 'max_read_buffer', 4096, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::IO'->error))
'HTTP::Promise::IO'->new($fh, 'max_read_buffer', 4096, 'debug', $self->debug)
Truth table0%0/2
A
A= 'HTTP::Promise::IO'->new($fh, 'max_read_buffer', 4096, 'debug', $self->debug)
Aresult
11
00
980 return( $self->pass_error( HTTP::Promise::IO->error ) );
981 0 ▶ $self->_load_class( 'HTTP::Promise::Parser' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Parser") or return $self->pass_error 0 0 0
982 0 my $parser = HTTP::Promise::Parser->new( debug => $self->debug );
983
984 # Request body can be one of 3 types:
985 # application/x-www-form-urlencoded
986 # multipart/form-data
987 # text/plain or other mime types
988 # <https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST>
989 0 ▶ my $part_ent = $parser->parse_multi_part( entity => $ent, reader => $reader ) ||
Condition0%0/2
$parser->parse_multi_part('entity', $ent, 'reader', $reader) || (return $parser->pass_error)
l!l
00
MC/DC0%0/1
$parser->parse_multi_part('entity', $ent, 'reader', $reader) || (return $parser->pass_error)
$parser->parse_multi_part('entity', $ent, 'reader', $reader)
Truth table0%0/2
A
A= $parser->parse_multi_part('entity', $ent, 'reader', $reader)
Aresult
11
00
990 return( $parser->pass_error );
991 0 $ent->body( undef );
992 }
993 # See rfc7230, section 8.3.1
994 # <https://tools.ietf.org/html/rfc7230#section-8.3.1>
995 elsif( $type eq 'message/http' )
996 {
997 0 ▶ my $fh = $body->open( '+<', { binmode => 'raw' } ) ||
Condition0%0/2
$body->open('+<', {'binmode', 'raw'}) || (return $self->pass_error($ent->body->error))
l!l
00
MC/DC0%0/1
$body->open('+<', {'binmode', 'raw'}) || (return $self->pass_error($ent->body->error))
$body->open('+<', {'binmode', 'raw'})
Truth table0%0/2
A
A= $body->open('+<', {'binmode', 'raw'})
Aresult
11
00
998 return( $self->pass_error( $ent->body->error ) );
999 0 ▶ $self->_load_class( 'HTTP::Promise::IO' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::IO") or return $self->pass_error 0 0 0
1000 0 ▶ my $reader = HTTP::Promise::IO->new( $fh, max_read_buffer => 4096, debug => $self->debug ) ||
Condition0%0/2
'HTTP::Promise::IO'->new($fh, 'max_read_buffer', 4096, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::IO'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::IO'->new($fh, 'max_read_buffer', 4096, 'debug', $self->debug) || (return $self->pass_error('HTTP::Promise::IO'->error))
'HTTP::Promise::IO'->new($fh, 'max_read_buffer', 4096, 'debug', $self->debug)
Truth table0%0/2
A
A= 'HTTP::Promise::IO'->new($fh, 'max_read_buffer', 4096, 'debug', $self->debug)
Aresult
11
00
1001 return( $self->pass_error( HTTP::Promise::IO->error ) );
1002 # "It is RECOMMENDED that all HTTP senders and recipients support, at a minimum, request-line lengths of 8000 octets."
1003 # Ref: <https://tools.ietf.org/html/rfc7230#section-3.1.1>
1004 # getline() returns a scalar object
1005 0 my $buff = $reader->getline( max_read_buffer => 8192 );
1006 0 ▶ return( $self->pass_error( $reader->error ) ) if( !defined( $buff ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buff 0 0 0
1007 0 ▶ $self->_load_class( 'HTTP::Promise::Parser' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Parser") or return $self->pass_error 0 0 0
1008 0 my $parser = HTTP::Promise::Parser->new( debug => $self->debug );
1009 0 my $def = $parser->looks_like_what( $buff );
1010 0 ▶ warn( "Part found of type message/http, but its content does not match a HTTP request or response.\n" ) if( !$def && warnings::enabled( 'HTTP::Promise' ) );
Branch: 1 branch
BranchTrueFalseTotal
if not $def and warnings::enabled("HTTP::Promise") 0 0 0
Condition0%0/3
not $def and warnings::enabled("HTTP::Promise")
!ll&&!rl&&r
000
MC/DC0%0/2
not $def and warnings::enabled("HTTP::Promise")
not $def warnings::enabled("HTTP::Promise")
Truth table0%0/3
not(A) and B
A= not $defB= warnings::enabled("HTTP::Promise")
ABresult
0X0
100
111
1011 0 ▶ return( $self->pass_error( $parser->error ) ) if( !defined( $def ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $def 0 0 0
1012 # Give back what we just read to the reader for later use
1013 0 $reader->unread( $buff );
1014 # We parse it even if it may be a defective message/http part
1015 0 ▶ my $sub_ent = $parser->parse( $fh, reader => $reader ) || return( $self->pass_error( $parser->error ) );
Condition0%0/2
$parser->parse($fh, 'reader', $reader) || (return $self->pass_error($parser->error))
l!l
00
MC/DC0%0/1
$parser->parse($fh, 'reader', $reader) || (return $self->pass_error($parser->error))
$parser->parse($fh, 'reader', $reader)
Truth table0%0/2
A
A= $parser->parse($fh, 'reader', $reader)
Aresult
11
00
1016 0 ▶ if( $def )
Branch: 1 branch
BranchTrueFalseTotal
if ($def) 0 0 0
1017 {
1018 0 my $headers = $sub_ent->headers;
1019 0 my $msg;
1020 0 ▶ if( $def->{type} eq 'request' )
Branch: 2 branches
BranchTrueFalseTotal
if ($def->{'type'} eq 'request') { } 0 0 0
elsif ($def->{'type'} eq 'response') { } 0 0 0
1021 {
1022 0 ▶ $self->_load_class( 'HTTP::Promise::Request' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Request") or return $self->pass_error 0 0 0
1023 0 ▶ $msg = HTTP::Promise::Request->new( @$def{qw( method uri )}, $headers, { protocol => $def->{protocol}, version => $def->{http_version} } ) || return( $self->pass_error( HTTP::Promise::Request->error ) );
Condition0%0/2
'HTTP::Promise::Request'->new(@$def{'method', 'uri'}, $headers, {'protocol', $def->{'protocol'}, 'version', $def->{'http_version'}}) || (return $self->pass_error('HTTP::Promise::Request'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Request'->new(@$def{'method', 'uri'}, $headers, {'protocol', $def->{'protocol'}, 'version', $def->{'http_version'}}) || (return $self->pass_error('HTTP::Promise::Request'->error))
'HTTP::Promise::Request'->new(@$def{'method', 'uri'}, $headers, {'protocol', $def->{'protocol'}, 'version', $def->{'http_version'}})
Truth table0%0/2
A
A= 'HTTP::Promise::Request'->new(@$def{'method', 'uri'}, $headers, {'protocol', $def->{'protocol'}, 'version', $def->{'http_version'}})
Aresult
11
00
1024 }
1025 elsif( $def->{type} eq 'response' )
1026 {
1027 0 ▶ $self->_load_class( 'HTTP::Promise::Response' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Response") or return $self->pass_error 0 0 0
1028 0 ▶ $msg = HTTP::Promise::Response->new( @$def{qw( code status )}, $headers, { protocol => $def->{protocol}, version => $def->{http_version} } ) || return( $self->pass_error( HTTP::Promise::Response->error ) );
Condition0%0/2
'HTTP::Promise::Response'->new(@$def{'code', 'status'}, $headers, {'protocol', $def->{'protocol'}, 'version', $def->{'http_version'}}) || (return $self->pass_error('HTTP::Promise::Response'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Response'->new(@$def{'code', 'status'}, $headers, {'protocol', $def->{'protocol'}, 'version', $def->{'http_version'}}) || (return $self->pass_error('HTTP::Promise::Response'->error))
'HTTP::Promise::Response'->new(@$def{'code', 'status'}, $headers, {'protocol', $def->{'protocol'}, 'version', $def->{'http_version'}})
Truth table0%0/2
A
A= 'HTTP::Promise::Response'->new(@$def{'code', 'status'}, $headers, {'protocol', $def->{'protocol'}, 'version', $def->{'http_version'}})
Aresult
11
00
1029 }
1030 else
1031 {
1032 0 return( $self->error( "Something is wrong with the parser who returned HTTP message type '$def->{type}', which I do not recognise." ) );
1033 }
1034
1035 0 $msg->entity( $sub_ent );
1036 0 $sub_ent->http_message( $msg );
1037 }
1038 0 $parts->set( $sub_ent );
1039 0 $ent->body( undef );
1040 }
1041 elsif( $toptype eq 'message' )
1042 {
1043 0 ▶ my $fh = $body->open( '+<', { binmode => 'raw' } ) ||
Condition0%0/2
$body->open('+<', {'binmode', 'raw'}) || (return $self->pass_error($ent->body->error))
l!l
00
MC/DC0%0/1
$body->open('+<', {'binmode', 'raw'}) || (return $self->pass_error($ent->body->error))
$body->open('+<', {'binmode', 'raw'})
Truth table0%0/2
A
A= $body->open('+<', {'binmode', 'raw'})
Aresult
11
00
1044 return( $self->pass_error( $ent->body->error ) );
1045 0 my $parser = HTTP::Promise::Parser->new( debug => $self->debug );
1046 0 ▶ my $ent = $parser->parse( $fh ) || return( $self->pass_error( $parser->error ) );
Condition0%0/2
$parser->parse($fh) || (return $self->pass_error($parser->error))
l!l
00
MC/DC0%0/1
$parser->parse($fh) || (return $self->pass_error($parser->error))
$parser->parse($fh)
Truth table0%0/2
A
A= $parser->parse($fh)
Aresult
11
00
1047 0 $parts->set( $ent );
1048 0 $ent->body( undef );
1049 }
1050 # Any other is not a multipart as per HTTP protocol
1051 0 return( $parts );
1052 }
1053  
1054 sub _set_content
1055 {
1056 0 ▶ my $self = shift( @_ );
Subroutine _set_content: not called CC=4 SCAR=30.0
1057 0 ▶ $self->_utf8_downgrade( $_[0] ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_utf8_downgrade($_[0]) or return $self->pass_error 0 0 0
1058 0 $self->content( $_[0] );
1059 0 ▶ $self->entity->parts->reset unless( !$self->entity || $_[1] );
Branch: 1 branch
BranchTrueFalseTotal
unless not $self->entity or $_[1] 0 0 0
Condition0%0/3
not $self->entity or $_[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
not $self->entity or $_[1]
not $self->entity $_[1]
Truth table0%0/3
not(A) or B
A= not $self->entityB= $_[1]
ABresult
1X1
011
000
1060 }
1061  
1062 sub _utf8_downgrade
1063 {
1064 6 ▶ my $self = shift( @_ );
Subroutine _utf8_downgrade: called CC=4 SCAR=14.6
1065 6 my $rv;
1066 # try-catch
1067 6 local $@;
1068 eval
1069 6 {
1070 6 ▶ if( defined( &utf8::downgrade ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined &utf8::downgrade) 6 0 6
1071 {
1072 6 $rv = utf8::downgrade( $_[0], 1 );
1073 }
1074 };
1075 6 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 6 6
1076 {
1077 0 return( $self->error( "Error downgrading utf8 data: $@" ) );
1078 }
1079 6 ▶ $rv || return( $self->error( 'HTTP::Promise::Message content must be bytes' ) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return $self->error("HTTP::Promise::Message content must be bytes") 0 6 6
1080 }
1081  
1082 sub AUTOLOAD
1083 {
1084 3 ▶ my( $package, $method ) = $AUTOLOAD =~ m/\A(.+)::([^:]*)\z/;
Subroutine AUTOLOAD: called CC=2 SCAR=8.4
1085 3 my $code = $_[0]->can( $method );
1086 3 ▶ goto( &$code ) if( $code );
Branch: 1 branch
BranchTrueFalseTotal
if $code 3 0 3
1087 # Give a chance to our parent AUTOLOAD to kick in
1088 0 $Module::Generic::AUTOLOAD = $AUTOLOAD;
1089 0 goto( &Module::Generic::AUTOLOAD );
1090 }
1091  
1092 # sub CARP_TRACE { return( shift->_get_stack_trace ); }
1093  
1094 # avoid AUTOLOADing it
1095 ▶sub DESTROY { }
Subroutine DESTROY: not called CC=1 SCAR=0.0
1096  
1097 # NOTE: sub FREEZE is inherited
1098  
1099 3 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
1100  
1101 3 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
1102  
1103 # NOTE: sub THAW is inherited
1104 sub THAW
1105 {
1106 6 ▶ my( $self, $serialiser, @args ) = @_;
Subroutine THAW: called CC=16 SCAR=36.0
Pod undocumented
1107 6 ▶ my $ref = ( CORE::scalar( @args ) == 1 && CORE::ref( $args[0] ) eq 'ARRAY' ) ? CORE::shift( @args ) : \@args;
Branch: 1 branch
BranchTrueFalseTotal
scalar @args == 1 && ref $args[0] eq 'ARRAY' ? : 0 6 6
Condition33%1/3
scalar @args == 1 and ref $args[0] eq "ARRAY"
!ll&&!rl&&r
600
MC/DC0%0/2
scalar @args == 1 and ref $args[0] eq "ARRAY"
scalar @args == 1 ref $args[0] eq "ARRAY"
Truth table33%1/3
A and B
A= scalar @args == 1B= ref $args[0] eq "ARRAY"
ABresult
0X0
100
111
1108 6 ▶ my $class = ( CORE::defined( $ref ) && CORE::ref( $ref ) eq 'ARRAY' && CORE::scalar( @$ref ) > 1 ) ? CORE::shift( @$ref ) : ( CORE::ref( $self ) || $self );
Branch: 1 branch
BranchTrueFalseTotal
defined $ref && ref $ref eq 'ARRAY' && scalar @$ref > 1 ? : 6 0 6
Condition22%2/9
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
!ll&&!rl&&r
006
defined $ref and ref $ref eq "ARRAY"
!ll&&!rl&&r
006
ref $self or $self
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $ref and ref $ref eq "ARRAY" and scalar @$ref > 1
defined $ref ref $ref eq "ARRAY" scalar @$ref > 1
MC/DC0%0/2
ref $self or $self
ref $self $self
Truth table25%1/4
A and B and C
A= defined $refB= ref $ref eq "ARRAY"C= scalar @$ref > 1
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/3
A or B
A= ref $selfB= $self
ABresult
1X1
011
000
1109 6 ▶ my $hash = CORE::ref( $ref ) eq 'ARRAY' ? CORE::shift( @$ref ) : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'ARRAY' ? : 6 0 6
1110 6 my $new;
1111 # Storable pattern requires to modify the object it created rather than returning a new one
1112 6 ▶ if( CORE::ref( $self ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self) { } 3 3 6
1113 {
1114 3 foreach( CORE::keys( %$hash ) )
1115 {
1116 63 $self->{ $_ } = CORE::delete( $hash->{ $_ } );
1117 }
1118
1119 # Need to make sure the headers object, which is an XS one is properly post processed, because Storable does not handle well XS objects, as of version 3.26
1120 3 ▶ if( CORE::exists( $self->{headers} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"headers"} and defined $self->{"headers"} and ref $self->{"headers"} and $self->{"headers"}->isa("HTTP::Promise::Headers")) 3 0 3
Condition33%3/9
exists $self->{"headers"} and defined $self->{"headers"} and ref $self->{"headers"} and $self->{"headers"}->isa("HTTP::Promise::Headers")
!ll&&!rl&&r
003
exists $self->{"headers"} and defined $self->{"headers"} and ref $self->{"headers"}
!ll&&!rl&&r
003
exists $self->{"headers"} and defined $self->{"headers"}
!ll&&!rl&&r
003
MC/DC0%0/4
exists $self->{"headers"} and defined $self->{"headers"} and ref $self->{"headers"} and $self->{"headers"}->isa("HTTP::Promise::Headers")
exists $self->{"headers"} defined $self->{"headers"} ref $self->{"headers"} $self->{"headers"}->isa("HTTP::Promise::Headers")
Truth table20%1/5
A and B and C and D
A= exists $self->{"headers"}B= defined $self->{"headers"}C= ref $self->{"headers"}D= $self->{"headers"}->isa("HTTP::Promise::Headers")
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1121 CORE::defined( $self->{headers} ) &&
1122 CORE::ref( $self->{headers} ) &&
1123 $self->{headers}->isa( 'HTTP::Promise::Headers' ) )
1124 {
1125 3 $self->{headers} = $self->{headers}->STORABLE_thaw_post_processing;
1126 }
1127 # The headers object in HTTP::Promise::Message must be the same shared on in HTTP::Promise::Entity
1128 3 ▶ if( CORE::exists( $self->{entity} ) &&
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"entity"} and exists $self->{"entity"}{"headers"}) 3 0 3
Condition33%1/3
exists $self->{"entity"} and exists $self->{"entity"}{"headers"}
!ll&&!rl&&r
003
MC/DC0%0/2
exists $self->{"entity"} and exists $self->{"entity"}{"headers"}
exists $self->{"entity"} exists $self->{"entity"}{"headers"}
Truth table33%1/3
A and B
A= exists $self->{"entity"}B= exists $self->{"entity"}{"headers"}
ABresult
0X0
100
111
1129 CORE::exists( $self->{entity}->{headers} ) )
1130 {
1131 3 $self->{entity}->{headers} = $self->{headers};
1132 }
1133 3 $new = $self;
1134 }
1135 else
1136 {
1137 3 $new = bless( $hash => $class );
1138 }
1139 6 CORE::return( $new );
1140 }
1141  
1142 # NOTE: only here to avoid triggering HTTP::Promise::Headers::STORABLE_thaw_post_processing which we inherit when we did 'require HTTP::Promise::Headers'
1143 3 ▶sub STORABLE_thaw_post_processing { CORE::return( $_[0] ); }
Subroutine STORABLE_thaw_post_processing: called CC=1 SCAR=0.0
Pod undocumented
1144  
1145 6 1;
1146 # NOTE: POD
1147 __END__
1148  
1149 =encoding utf-8
1150  
1151 =head1 NAME
1152  
1153 HTTP::Promise::Message - HTTP Message Class
1154  
1155 =head1 SYNOPSIS
1156  
1157 use HTTP::Promise::Message;
1158 my $this = HTTP::Promise::Message->new(
1159 [ 'Content-Type' => 'text/plain' ],
1160 'Hello world'
1161 ) || die( HTTP::Promise::Message->error, "\n" );
1162  
1163 =head1 VERSION
1164  
1165 v0.3.3
1166  
1167 =head1 DESCRIPTION
1168  
1169 This class represents an HTTP message, and implements methods that are common to either a request or a response. This class is inherited by L<HTTP::Promise::Request> and L<HTTP::Promise::Response>. It difffers from L<HTTP::Promise::Entity> in that L<HTTP::Promise::Entity> represents en HTTP entity which is composed of headers and a body, and this can be embedded within another entity.
1170  
1171 Here is how it fits in overall relation with other classes.
1172
1173 +-------------------------+ +--------------------------+
1174 | | | |
1175 | HTTP::Promise::Request | | HTTP::Promise::Response |
1176 | | | |
1177 +------------|------------+ +-------------|------------+
1178 | |
1179 | |
1180 | |
1181 | +------------------------+ |
1182 | | | |
1183 +--- HTTP::Promise::Message |---+
1184 | |
1185 +------------|-----------+
1186 |
1187 |
1188 +------------|-----------+
1189 | |
1190 | HTTP::Promise::Entity |
1191 | |
1192 +------------|-----------+
1193 |
1194 |
1195 +------------|-----------+
1196 | |
1197 | HTTP::Promise::Body |
1198 | |
1199 +------------------------+
1200  
1201 =head1 CONSTRUCTOR
1202  
1203 =head2 new
1204  
1205 This takes some parameters and instantiates a new L<HTTP::Promise::Message>.
1206  
1207 Accepted parameters can be one of the followings:
1208  
1209 =over 4
1210  
1211 =item 1. an L<headers object|HTTP::Promise::Headers> and some content as a string or scalar reference.
1212  
1213 my $msg = HTTP::Promise::Message->new( HTTP::Promise::Headers->new(
1214 Content_Type => 'text/plain',
1215 Content_Encoding => 'gzip',
1216 Host: 'www.example.org',
1217 ),
1218 "Some content",
1219 );
1220  
1221 my $str = "Some content";
1222 my $hdr = HTTP::Promise::Headers->new(
1223 Content_Type => 'text/plain',
1224 Content_Encoding => 'gzip',
1225 Host: 'www.example.org',
1226 );
1227 my $msg = HTTP::Promise::Message->new( $hdr, \$str );
1228  
1229 =item 2. an L<headers object|HTTP::Promise::Headers> and and L<HTTP::Promise::Body> or L<HTTP::Promise::Body::Form> object
1230  
1231 my $body = HTTP::Promise::Body::Scalar->new( "Some content" );
1232 my $hdr = HTTP::Promise::Headers->new(
1233 Content_Type => 'text/plain',
1234 Content_Encoding => 'gzip',
1235 Host: 'www.example.org',
1236 );
1237 my $msg = HTTP::Promise::Message->new( $hdr, $body );
1238  
1239 Using the x-www-form-urlencoded class:
1240  
1241 my $body = HTTP::Promise::Body::Form->new({ name => '嘉納 治五郎', age => 22, city => 'Tokyo' });
1242 my $hdr = HTTP::Promise::Headers->new(
1243 Content_Type => 'text/plain',
1244 Content_Encoding => 'gzip',
1245 Host: 'www.example.org',
1246 );
1247 my $msg = HTTP::Promise::Message->new( $hdr, $body );
1248  
1249 =item 3. an array reference of headers field-value pairs and some content as a string or scalar reference.
1250  
1251 my $msg = HTTP::Promise::Message->new([
1252 Content_Type => 'text/plain',
1253 Content_Encoding => 'gzip',
1254 Host: 'www.example.org',
1255 ],
1256 "Some content",
1257 );
1258  
1259 =item 4. an hash reference of parameters
1260  
1261 my $hdr = HTTP::Promise::Headers->new(
1262 Content_Type => 'text/plain',
1263 Content_Encoding => 'gzip',
1264 Host: 'www.example.org',
1265 );
1266 my $msg = HTTP::Promise::Message->new({
1267 headers => $hdr,
1268 content => \$str,
1269 # HTP::Promise::Entity
1270 entity => $entity_object,
1271 debug => 4,
1272 });
1273  
1274 =back
1275  
1276 In any case, you can provide additional object options by providing an hash reference as the last argument, such as:
1277  
1278 my $msg = HTTP::Promise::Message->new([
1279 Content_Type => 'text/plain',
1280 Content_Encoding => 'gzip',
1281 Host: 'www.example.org',
1282 ],
1283 "Some content",
1284 {
1285 debug => 4,
1286 entity => $entity_object
1287 },
1288 );
1289  
1290 If some content is provided, a new L<entity in-memory body object|HTTP::Promise::Body::Scalar> will be initiated
1291  
1292 It returns the new http message object, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
1293  
1294 =head1 METHODS
1295  
1296 =head2 add_content
1297  
1298 This takes a string or a scalar reference and append it to the current body if the body object is an L<HTTP::Promise::Body::File> or L<HTTP::Promise::Body::Scalar> object. This does not work for L<HTTP::Promise::Body::Form>. You would have to call yourself the class methods to add your key-value pairs.
1299  
1300 The content thus provided is downgraded, which means it is flagged as being in perl's internal utf-8 representation. So you cannot use this method to add binary data. If you want to do so, you would need to use directly the body object methods. For example:
1301  
1302 my $io = $msg->entity->body->open( '>', { binmode => 'utf-8', autoflush => 1 }) ||
1303 die( $msg->entity->body->error );
1304 $io->print( $some_data ) || die( $io->error );
1305 $io->close;
1306  
1307 This code works for either L<HTTP::Promise::Body::File> or L<HTTP::Promise::Body::Scalar>
1308  
1309 If no entity, or body is set yet, it will create one automatically, and defaults to L<HTTP::Promise::Body::Scalar> for the body class.
1310  
1311 It returns the entity body object, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
1312  
1313 =head2 add_content_utf8
1314  
1315 This is the same thing as L</add_content>, except it will encode in utf-8 the data provided, i.e. not perl's internal representation.
1316  
1317 =head2 add_part
1318  
1319 By default, this will check if the HTTP message C<Content-Type> is a multipart one, and if not, it will automatically set it to C<multipart/form-data> and transform the current HTTP message into the first part of a C<multipart/form-data>, and add after all the parts provided.
1320  
1321 If the C<Content-Type> is already a multipart one, but has no part yet and has a body content, it will parse that content to build one or more parts from it.
1322  
1323 When used for an HTTP request, C<multipart/form-data> is the only valid 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."
1324  
1325 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>
1326  
1327 When used for an HTTP response, one can return either a C<multipart/form-data> or a C<multipart-mixed> HTTP message.
1328  
1329 If you want to make an HTTP request, then you need to provide pairs of form-name-and part object (either a L<HTTP::Promise::Entity> or a L<HTTP::Promise::Message> object with an L<HTTP::Promise::Entity> set with L</entity>) OR a list of parts whose L<name attribute|HTTP::Promise::Entity/name> is set.
1330  
1331 If you want to make an HTTP response, you can either return a C<multipart/form-data> by providing pairs of form-name-and part object as mentioned above, or a C<multipart/mixed> by providing a list of part object (either a L<HTTP::Promise::Entity> or a L<HTTP::Promise::Message> object with an L<HTTP::Promise::Entity> set with L</entity>).
1332  
1333 For example:
1334  
1335 $m->add_part(
1336 file1 => $ent1,
1337 file2 => $ent2,
1338 first_name => $ent3,
1339 last_name => $ent4,
1340 # etc...
1341 );
1342  
1343 or, using the L<name attribute|HTTP::Promise::Entity/name>:
1344  
1345 $ent1->name( 'file1' );
1346 $ent2->name( 'file2' );
1347 $ent3->name( 'first_name' );
1348 $ent4->name( 'last_name' );
1349 $m->add_part( $ent1, $ent2, $ent3, $ent4 );
1350  
1351 Note that you can always set an L<entity name|HTTP::Promise::Entity/name>, and it will only be used if the HTTP message Content-Type is of type C<multipart/form-data>, unless you set yourself the C<Content-Disposition> header value.
1352  
1353 It returns the current object, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
1354  
1355 =head2 as_form_data
1356  
1357 This will read the body of the HTTP entity and return it as an object of key-value pairs with the module L<HTTP::Promise::Body::Form>
1358  
1359 This supports HTTP C<Content-Type> C<multipart/form-data>, C<application/json>, C<application/x-www-form-urlencoded>, or in the case of HTTP method C<GET>, C<HEAD>, or C<DELETE>, it will use any query string parameters, and return a new L<HTTP::Promise::Body::Form> object.
1360  
1361 It defaults to C<application/x-www-form-urlencoded>. Upon error, it will set an L<HTTP::Promise::Exception> and return C<undef> in scalar context, or an empty list in list context.
1362  
1363 The way this works is it checks first for C<multipart/form-data>, then C<application/json>, and for query strings only if there is no HTTP body content, and else it fallbacks to C<application/x-www-form-urlencoded>.
1364  
1365 This means you must be careful if you send or receive C<JSON> data to properly set the C<Content-Type> to C<application/json>
1366  
1367 =head2 as_string
1368  
1369 This takes an optional end-of-line terminator and returns a L<scalar object|Module::Generic::Scalar> representing the entire HTTP message.
1370  
1371 The end-of-line terminator defaults to C<$CRLF>, which is a global variable of L<HTTP::Promise::Message>
1372  
1373 =head2 boundary
1374  
1375 This is a shortcut.
1376  
1377 It returns the result returned by L<HTTP::Promise::Headers/boundary>
1378  
1379 =head2 can
1380  
1381 This behaves like L<UNIVERSAL/can>, with a twist.
1382  
1383 Provided with a method name and this check if this is supported by L<HTTP::Promise::Message>, or in last resort by L<HTTP::Promise::Headers> and if the latter is true, it will alias the headers method to this namespace.
1384  
1385 It returns the code reference of the requested method, or C<undef> if none could be found.
1386  
1387 =head2 clear
1388  
1389 Clears out the headers object by calling L<HTTP::Promise::Headers/clear>, empty the entity body, if any, and remove any part if any.
1390  
1391 It does not return anything. This should be called in void context.
1392  
1393 =head2 clone
1394  
1395 This clones the current HTTP message and returns a new object.
1396  
1397 =head2 content
1398  
1399 Get or set the HTTP message body.
1400  
1401 If one or more values are provided, they will be added to the newly created L<HTTP::Promise::Body> object.
1402  
1403 You can provide as values one or more instance of either a string or a scalar reference.
1404  
1405 For example:
1406  
1407 $m->content( \$string, 'Hello world', \$another_string );
1408  
1409 It returns the newly set L<HTTP::Promise::Body> object upon success or, upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
1410  
1411 When no argument is provided, this returns the L<HTTP::Promise::Body> object as a L<scalar object|Module::Generic::Scalar>.
1412  
1413 Beware that the content returned might not be decoded if compression has been applied previously, or if compressed content was provided upon instantiation of the C<HTTP::Promise::Message> object, such as:
1414  
1415 my $m = HTTP::Promise::Message->new([
1416 'Content-Type' => 'text/plain',
1417 'Content-Encoding' => 'deflate, base64',
1418 ],
1419 '80jNyclXCM8vyklRBAA='
1420 );
1421 my $content = $m->content; # 80jNyclXCM8vyklRBAA=
1422  
1423 But even with utf-8 content, such as:
1424  
1425 my $m = HTTP::Promise::Message->new([
1426 'Content-Type' => 'text/plain; charset=utf-8',
1427 ],
1428 "\x{E3}\x{81}\x{8A}\x{E6}\x{97}\x{A9}\x{E3}\x{81}\x{86}\x{EF}\x{BC}\x{81}\x{A}",
1429 );
1430 my $content = $m->content;
1431  
1432 C<$content> would contain undecoded utf-8 bytes, i.e. not in perl's internal representation. Indeed, charset is never decoded. If you want the charset decoded content, use L</decoded_content>, which will guess the content charset to decode it into perl's internal representation. If you are sure this is utf-8, you can either call:
1433  
1434 my $decoded_content = $m->decoded_content( binmode => 'utf-8' );
1435  
1436 or
1437  
1438 my $decoded_content = $m->decoded_content_utf8;
1439  
1440 See L</decoded_content> for more information.
1441  
1442 =head2 content_charset
1443  
1444 This is a convenient method that calls L<HTTP::Promise::Entity/content_charset> and returns the result.
1445  
1446 This method attempts at guessing the content charset of the entity body.
1447  
1448 It returns a string representing the content charset, possibly empty if nothing was found, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
1449  
1450 =head2 content_ref
1451  
1452 This sets or gets the content as a scalar reference.
1453  
1454 In assignment mode, this takes a scalar reference and pass it to L</content> and returns the L<body object|HTTP::Promise::Body>
1455  
1456 Otherwise, this returns the content as L<scalar object|Module::Generic::Scalar>.
1457  
1458 If an error occurs, this sets an L<error|Module::Generic/error> and returns C<undef>.
1459  
1460 =head2 decodable
1461  
1462 This gets an L<array object|Module::Generic::Array> of all supported and installed decodings on the system, by calling L<HTTP::Promise::Stream/decodable>
1463  
1464 =head2 decode
1465  
1466 This decodes the HTTP message body and return true.
1467  
1468 If there is no C<Content-Encoding> set, or the entity body is empty, or the entity body already has been decoded, this does nothing obviously. Otherwise, this calls L<HTTP::Promise::Entity/decode_body> passing it the encodings as an array reference.
1469  
1470 If an error occurs, this sets an L<error|Module::Generic/error> and returns C<undef>.
1471  
1472 =head2 decode_content
1473  
1474 This is similar to </decode>, except that it takes an hash or hash reference of options passed to L<HTTP::Promise::Entity/decode_body>, notably C<replace>, which if true will replace the body by its decoded version and if false will return a new body version representing the decoded body.
1475  
1476 This returns the entity body object upon success, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
1477  
1478 =head2 decoded_content
1479  
1480 This takes an hash or hash reference of options and returns the decoded representation of the body, including charset.
1481  
1482 This calls L</decode_content>, passing it the options provided, to decompress the entity body if necessary. Then, unless the C<binmode> option was provided, this calls L<HTTP::Promise::Entity/io_encoding> to guess the charset encoding, and set the C<binmode> option to it, if anything was found.
1483  
1484 If the entity body is an xml file, any C<BOM> (Byte Order Mark) will be removed.
1485  
1486 This returns the content as a L<scalar object|Module::Generic::Scalar>, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
1487  
1488 Supported options are:
1489  
1490 =over 4
1491  
1492 =item * C<binmode>
1493  
1494 The L<PerlIO> encoding to apply to decode the data.
1495  
1496 If not provided, this will be guessed by calling L<HTTP::Promise::Entity/io_encoding>
1497  
1498 =item * C<charset_strict>
1499  
1500 If true, this will returns an error if there is some issues with the content charset. By default, this is false, making it lenient, especially with malformed utf-8.
1501  
1502 =item * C<raise_error>
1503  
1504 When set to true, this will cause this method to die upon error. Default is false.
1505  
1506 =back
1507  
1508 =head2 decoded_content_utf8
1509  
1510 This calls L</decoded_content>, but this sets the C<binmode> option to C<utf-8>.
1511  
1512 It returns whatever L</decode_content> returns.
1513  
1514 =head2 decoded_json
1515  
1516 If the content type of the response is C<application/json>, this will call L</decoded_content_utf8> and decode the JSON payload and return the hash reference. If an error occurred, it will set L<an error|Module::Generic/error> and return C<undef> in scalar context, or an empty list in list context.
1517  
1518 =head2 dump
1519  
1520 This takes an hash or hash reference of options and either print the resulting dump on the C<STDOUT> in void content, or returns a string representation of the HTTP message, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
1521  
1522 Supported options are:
1523  
1524 =over 4
1525  
1526 =item * C<maxlength>
1527  
1528 The maximum amount of body data in bytes to display.
1529  
1530 =item * C<no_content>
1531  
1532 The string to use when there is no entity body data.
1533  
1534 =item * C<prefix>
1535  
1536 A string to be added at the beginning of each line of the data returned.
1537  
1538 =item * C<preheader>
1539  
1540 An arbitrary string to add before the HTTP headers, typically the HTTP C<start line>
1541  
1542 =back
1543  
1544 # Returns a string
1545 my $dump = $msg->dump;
1546 # Prints on the STDOUT the result
1547 $msg->dump;
1548  
1549 =head2 encode
1550  
1551 This takes an optional list of encoding and an optional hash or hash reference of options and encode the entity body and returns true, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
1552  
1553 This will return an error if it is used on a multipart message or an C<message/*> such as C<message/http>.
1554  
1555 Obviously this is a no-op if no encoding was found, or if the body is empty, or if the body is already marked L<as encoded|HTTP::Promise::Entity/is_encoded>
1556  
1557 Supported options are:
1558  
1559 =over 4
1560  
1561 =item * C<update_header>
1562  
1563 When true, this will set the C<Content-Encoding> with the encoding used to encode the entity body and remove the headers C<Content-Length> and C<Content-MD5>. Defaults to true.
1564  
1565 =back
1566  
1567 =head2 entity
1568  
1569 Sets or gets the HTTP L<entity object|HTTP::Promise::Entity>
1570  
1571 =head2 header
1572  
1573 This is a shortcut by calling L<HTTP::Promise::Headers/header>
1574  
1575 =head2 headers
1576  
1577 Sets or gets the L<HTTP::Promise::Headers> object.
1578  
1579 =head2 headers_as_string
1580  
1581 This is a shortcut to call L<HTTP::Promise::Headers/as_string>
1582  
1583 =head2 is_encoding_supported
1584  
1585 Provided with an encoding and this returns true if the encoding is supported by L<HTTP::Promise::Stream>
1586  
1587 =head2 make_boundary
1588  
1589 Returns a newly generated boundary, which is basically a uuid generated by the XS module L<Data::UUID>
1590  
1591 =head2 parse
1592  
1593 Provided with a string and this will try to parse this HTTP message and returns the current message object if it was called with an HTTP message, or a new HTTP message if it was called as a class function, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
1594  
1595 my $msg = HTTP::Promise::Message->parse( $some_http_message ) ||
1596 die( HTTP::Promise::Message->error );
1597
1598 $msg->parse( $some_http_message ) ||
1599 die( HTTP::Promise::Message->error );
1600  
1601 =head2 parts
1602  
1603 This returns the HTTP message entity parts as an L<array object|Module::Generic::Array> and returns it, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
1604  
1605 If the HTTP message has a body with content and there is no part and the mime-type top type is C<multipart> or C<message>, this will first parse the body into parts. Thus you could do:
1606  
1607 my $msg = HTTP::Promise::Message->new([
1608 Content_Type => 'multipart/form-data; boundary="abcd"',
1609 Content_Encoding => 'gzip',
1610 Host => 'example.org',
1611 ], <<EOT );
1612 --abcd
1613 Content-Disposition: form-data; name="name"
1614  
1615 Jigoro Kano
1616  
1617 --abcd
1618 Content-Disposition: form-data; name="birthdate"
1619  
1620 1860-12-10
1621 --abcd--
1622 EOT
1623  
1624 my $parts = $msg->parts;
1625  
1626 =head2 protocol
1627  
1628 Sets or gets the HTTP protocol. This is typically something like C<HTTP/1.0>, C<HTTP/1.1>, C<HTTP/2>
1629  
1630 Returns the HTTP protocol, if any was set, as a L<scalar object|Module::Generic::Scalar>, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
1631  
1632 Note that it may return C<undef> if no protocol was set. Errors are likely to occur when assigning an improper value.
1633  
1634 =head2 start_line
1635  
1636 This is a no-op since it is superseded by its inheriting classes L<HTTP::Promise::Request> and L<HTTP::Promise::Response>
1637  
1638 =head2 version
1639  
1640 Sets or gets the HTTP protocol version, something like C<1.0>, or C<1.1>, or maybe C<2>
1641  
1642 This returns a L<number object|Module::Generic::Number>
1643  
1644 =for Pod::Coverage STORABLE_thaw_post_processing
1645  
1646 =head1 THREAD-SAFETY
1647  
1648 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
1649  
1650 =head1 AUTHOR
1651  
1652 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
1653  
1654 =head1 SEE ALSO
1655  
1656 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>
1657  
1658 =head1 COPYRIGHT & LICENSE
1659  
1660 Copyright(c) 2022 DEGUEST Pte. Ltd.
1661  
1662 All rights reserved
1663 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
1664  
1665 =cut
« lib/HTTP/Promise/IO.pm ↑ Summary blib/lib/HTTP/Promise/Headers.pm »