Promise/Request.pm

filter: Statementstmt 51.2% 106 / 207 Branchbran 20.2% 19 / 94 Conditioncond 25.0% 30 / 120 MC/DCmcdc 11.8% 7 / 59 Subroutinesub 71.8% 23 / 32 Podpod 94.4% 17 / 18 totaltotal 38.1% 202 / 530 CC 92 SCAR 73.3
CC
92
Cov
37%
make_form_data
3306.0
init
15.8
host
12.0
CRAP 1524.0
« Promise.pm ↑ Summary Promise/Stream.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Request.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2023 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/03/21
7 ## Modified 2023/09/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::Request;
15 BEGIN
16 5 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
17 5 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 5 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 5 warnings::register_categories( 'HTTP::Promise' );
20 5 ▶ use parent qw( HTTP::Promise::Message );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶ use vars qw( $VERSION $EXCEPTION_CLASS $KNOWN_METHODS $KNOWN_METHODS_I $TIMEOUT
Subroutine BEGIN: called CC=1 SCAR=0.0
22 5 $DEFAULT_MIME_TYPE $DEFAULT_METHOD $DEFAULT_PROTOCOL $SCHEME_RE $INTL_URI_RE );
23 5 ▶ use Cookie::Jar;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶ use Crypt::Misc 0.076;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 # use HTTP::Parser2::XS 0.01 ();
26 5 ▶ use HTTP::Promise::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶ use HTTP::Promise::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶ use HTTP::Promise::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 # use Nice::Try v1.2.0;
30 5 ▶ use Regexp::Common qw( URI net );
Subroutine BEGIN: called CC=1 SCAR=0.0
31 # URI::Fast is great, but only supports simple protocols
32 # use URI::Fast 0.55;
33 5 ▶ use URI 5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 5 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
35 # rc7231, section 4.1
36 # Ref: <https://datatracker.ietf.org/doc/html/rfc7231#section-4.1>
37 # + PATCH
38 # HTTP "method token is case-sensitive"
39 # rfc7231, section 4.1 <https://tools.ietf.org/html/rfc7231#section-4.1>
40 # our $KNOWN_METHODS = qr/^(?:CONNECT|DELETE|GET|HEAD|OPTIONS|PATCH|POST|PUT|TRACE)$/;
41 5 local $" = '|';
42 5 my @methods = qw( CONNECT DELETE GET HEAD OPTIONS PATCH POST PUT TRACE );
43 5 our $KNOWN_METHODS = qr/(?:@methods)/;
44 5 our $KNOWN_METHODS_I = qr/(?:@methods)/i;
45 5 our $TIMEOUT = 10;
46 5 our $DEFAULT_MIME_TYPE = 'application/octet-stream';
47 5 our $DEFAULT_METHOD = 'GET';
48 5 our $DEFAULT_PROTOCOL = 'HTTP/1.1';
49 # Borrowed from URI::_server
50 5 our $GROSS_URI_RE = qr{(?<scheme>(?:https?:)?)//(?<host>[^/?\#]*)(?<rest>.*)};
51 # [\x00-\x7f]
52 5 our $INTL_URI_RE = qr{(?<scheme>(?:https?:)?)//(?<host>[^\x00-\x7f]+\.[^/?\#]*)(?<rest>.*)};
53 5 our $VERSION = 'v0.2.0';
54 };
55  
56 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
58  
59 # $req->new( $method, $uri, $headers, $content, k1 => v1, k2 => v2);
60 # $req->new( $method, $uri, $headers, $content, { k1 => v1, k2 => v2 });
61 # $req->new( $method, $uri, $headers, k1 => v1, k2 => v2);
62 # $req->new( $method, $uri, $headers, { k1 => v1, k2 => v2 });
63 # $req->new( $method, $uri, k1 => v1, k2 => v2);
64 # $req->new( $method, k1 => v1, k2 => v2);
65 # $req->new( k1 => v1, k2 => v2 );
66 sub init
67 34 ▶{
Subroutine init: called CC=13 SCAR=27.6
Pod documented
68 34 my $self = shift( @_ );
69 34 my( $method, $uri );
70 34 my $iKNOWN_METHODS = qr/$KNOWN_METHODS/i;
71 34 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 30 4 34
72 {
73 30 ▶ if( @_ == 1 && ref( $_[0] ) eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 30 30
elsif (@_ >= 2 and defined $_[0] and $_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS)$/ || $_[0] =~ /^[A-Za-z]{3,12}$/ && !($_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS_I)$/i) and defined $_[1] && $_[1] =~ m(^(?:$HTTP::Promise::Request::RE{'URI'}{'HTTP'}|$HTTP::Promise::Request::RE{'URI'}{'HTTP'}{join $;, '-scheme', 'https'}|(?:https?\:\/{2}\[?(?:$HTTP::Promise::Request::RE{'net'}{'IPv4'}|$HTTP::Promise::Request::RE{'net'}{'IPv6'}|\:{2}1)\]?(?:\:\d+)?)|$HTTP::Promise::Request::INTL_URI_RE|/)) || !defined($_[1])) { } 20 10 30
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
3000
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
74 {
75 0 my $opts = $_[0];
76 0 ( $method, $uri ) = CORE::delete( @$opts{qw( method uri )} );
77 }
78 ▶ elsif( @_ >= 2 &&
Condition66%14/21
@_ >= 2 and defined $_[0] and $_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS)$/ || $_[0] =~ /^[A-Za-z]{3,12}$/ && !($_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS_I)$/i) and defined $_[1] && $_[1] =~ m(^(?:$HTTP::Promise::Request::RE{"URI"}{"HTTP"}|$HTTP::Promise::Request::RE{"URI"}{"HTTP"}{join $;, "-scheme", "https"}|(?:https?\:\/{2}\[?(?:$HTTP::Promise::Request::RE{"net"}{"IPv4"}|$HTTP::Promise::Request::RE{"net"}{"IPv6"}|\:{2}1)\]?(?:\:\d+)?)|$HTTP::Promise::Request::INTL_URI_RE|/)) || !defined($_[1])
!ll&&!rl&&r
10020
@_ >= 2 and defined $_[0] and $_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS)$/ || $_[0] =~ /^[A-Za-z]{3,12}$/ && !($_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS_I)$/i)
!ll&&!rl&&r
01020
@_ >= 2 and defined $_[0]
!ll&&!rl&&r
0030
$_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS)$/ or $_[0] =~ /^[A-Za-z]{3,12}$/ and not $_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS_I)$/i
l!l&&r!l&&!r
16410
$_[0] =~ /^[A-Za-z]{3,12}$/ and not $_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS_I)$/i
!ll&&!rl&&r
0104
defined $_[1] and $_[1] =~ m(^(?:$HTTP::Promise::Request::RE{"URI"}{"HTTP"}|$HTTP::Promise::Request::RE{"URI"}{"HTTP"}{join $;, "-scheme", "https"}|(?:https?\:\/{2}\[?(?:$HTTP::Promise::Request::RE{"net"}{"IPv4"}|$HTTP::Promise::Request::RE{"net"}{"IPv6"}|\:{2}1)\]?(?:\:\d+)?)|$HTTP::Promise::Request::INTL_URI_RE|/)) or not defined $_[1]
l!l&&r!l&&!r
1910
defined $_[1] and $_[1] =~ m(^(?:$HTTP::Promise::Request::RE{"URI"}{"HTTP"}|$HTTP::Promise::Request::RE{"URI"}{"HTTP"}{join $;, "-scheme", "https"}|(?:https?\:\/{2}\[?(?:$HTTP::Promise::Request::RE{"net"}{"IPv4"}|$HTTP::Promise::Request::RE{"net"}{"IPv6"}|\:{2}1)\]?(?:\:\d+)?)|$HTTP::Promise::Request::INTL_URI_RE|/))
!ll&&!rl&&r
1019
MC/DC25%2/8
@_ >= 2 and defined $_[0] and $_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS)$/ || $_[0] =~ /^[A-Za-z]{3,12}$/ && !($_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS_I)$/i) and defined $_[1] && $_[1] =~ m(^(?:$HTTP::Promise::Request::RE{"URI"}{"HTTP"}|$HTTP::Promise::Request::RE{"URI"}{"HTTP"}{join $;, "-scheme", "https"}|(?:https?\:\/{2}\[?(?:$HTTP::Promise::Request::RE{"net"}{"IPv4"}|$HTTP::Promise::Request::RE{"net"}{"IPv6"}|\:{2}1)\]?(?:\:\d+)?)|$HTTP::Promise::Request::INTL_URI_RE|/)) || !defined($_[1])
@_ >= 2 defined $_[0] $_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS)$/ $_[0] =~ /^[A-Za-z]{3,12}$/ not $_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS_I)$/i defined $_[1] $_[1] =~ m(^(?:$HTTP::Promise::Request::RE{"URI"}{"HTTP"}|$HTTP::Promise::Request::RE{"URI"}{"HTTP"}{join $;, "-scheme", "https"}|(?:https?\:\/{2}\[?(?:$HTTP::Promise::Request::RE{"net"}{"IPv4"}|$HTTP::Promise::Request::RE{"net"}{"IPv6"}|\:{2}1)\]?(?:\:\d+)?)|$HTTP::Promise::Request::INTL_URI_RE|/)) not defined $_[1]
Truth table28%4/14
A and B and C or D and not(E) and F and G or not(H)
A= @_ >= 2B= defined $_[0]C= $_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS)$/D= $_[0] =~ /^[A-Za-z]{3,12}$/E= not $_[0] =~ /^(?:$HTTP::Promise::Request::KNOWN_METHODS_I)$/iF= defined $_[1]G= $_[1] =~ m(^(?:$HTTP::Promise::Request::RE{"URI"}{"HTTP"}|$HTTP::Promise::Request::RE{"URI"}{"HTTP"}{join $;, "-scheme", "https"}|(?:https?\:\/{2}\[?(?:$HTTP::Promise::Request::RE{"net"}{"IPv4"}|$HTTP::Promise::Request::RE{"net"}{"IPv6"}|\:{2}1)\]?(?:\:\d+)?)|$HTTP::Promise::Request::INTL_URI_RE|/))H= not defined $_[1]
ABCDEFGHresult
0XXXXXXX0
10XXXXXX0
1100XXXX0
11010XXX0
111XX0X00
111XX1000
110110X00
110111000
111XX11X1
111XX0X11
111XX1011
1101111X1
110110X11
110111011
79 defined( $_[0] ) &&
80 # rfc7230 says methods are case sensitive, so unless this is an unknown method we care about the case
81 ( $_[0] =~ /^(?:$KNOWN_METHODS)$/ || ( $_[0] =~ /^[A-Za-z]{3,12}$/ && $_[0] !~ /^(?:$KNOWN_METHODS_I)$/i ) ) &&
82 (
83 ( defined( $_[1] ) && $_[1] =~ m,^(?:$RE{URI}{HTTP}|$RE{URI}{HTTP}{-scheme => 'https'}|(?:https?\:\/{2}\[?(?:$RE{net}{IPv4}|$RE{net}{IPv6}|\:{2}1)\]?(?:\:\d+)?)|$INTL_URI_RE|/), ) ||
84 !defined( $_[1] )
85 ) )
86 {
87 20 ( $method, $uri ) = splice( @_, 0, 2 );
88 }
89 else
90 {
91 10 return( $self->error( "Invalid parameters received. I was expecting either an hash reference or at least a method and a valid uri, but instead got: '", join( "', '", @_ ), "'" ) );
92 }
93 }
94 24 $self->{default_protocol} = $DEFAULT_PROTOCOL;
95 # properties headers and content are set by our parent class HTTP::Promise::Message
96 24 $self->{method} = $method;
97 24 $self->{timeout} = $TIMEOUT;
98 24 $self->{uri} = $uri;
99 # Should as_string return an absolute uri or just the absolute path?
100 24 $self->{uri_absolute} = 0;
101 24 $self->{_init_strict_use_sub} = 1;
102 24 $self->{_init_params_order} = [qw( content headers )];
103 24 $self->{_exception_class} = $EXCEPTION_CLASS;
104 # $self->SUPER::init( ( defined( $headers ) ? $headers : () ), @_ ) || return( $self->pass_error );
105 24 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 24 0 24
106 24 return( $self );
107 }
108  
109 sub accept_decodable
110 1 ▶{
Subroutine accept_decodable: called CC=1 SCAR=0.0
Pod documented
111 1 my $self = shift( @_ );
112 1 return( $self->header( 'Accept-Encoding', $self->decodable->join( ', ' )->scalar ) );
113 }
114  
115 0 ▶sub authorization_basic { return( shift->headers->authorization_basic( @_ ) ); }
Subroutine authorization_basic: not called CC=1 SCAR=6.9
Pod undocumented
116  
117 sub clone
118 0 ▶{
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
119 0 my $self = shift( @_ );
120 0 my $new = $self->SUPER::clone;
121 0 $new->method( $self->method );
122 0 $new->uri( $self->uri );
123 0 return( $new );
124 }
125  
126 # NOTE: method content is inherited from HTTP::Promise::Message
127  
128 0 ▶sub cookie_jar { return( shift->_set_get_object( 'cookie_jar', 'Cookie::Jar', @_ ) ); }
Subroutine cookie_jar: not called CC=1 SCAR=6.9
Pod documented
129  
130 3 ▶sub default_protocol { return( shift->_set_get_scalar_as_object( 'default_protocol', @_ ) ); }
Subroutine default_protocol: called CC=1 SCAR=0.0
Pod documented
131  
132 sub dump
133 3 ▶{
Subroutine dump: called CC=1 SCAR=0.0
Pod documented
134 3 my $self = shift( @_ );
135 3 my $start_line = $self->start_line;
136 3 return( $self->SUPER::dump( preheader => $start_line ) );
137 }
138  
139 # sub headers { return( shift->_set_get_object_without_init( 'headers', 'HTTP::Promise::Headers', @_ ) ); }
140  
141 sub host
142 0 ▶{
Subroutine host: not called CC=3 SCAR=24.8
Pod documented
143 0 my $self = shift( @_ );
144 0 my $uri = $self->uri;
145 0 ▶ return( ( $uri->port && $uri->port != $uri->default_port ) ? join( ':', $uri->host, $uri->port ) : $uri->host );
Branch: 1 branch
BranchTrueFalseTotal
$uri->port && $uri->port != $uri->default_port ? : 0 0 0
Condition0%0/3
$uri->port and $uri->port != $uri->default_port
!ll&&!rl&&r
000
MC/DC0%0/2
$uri->port and $uri->port != $uri->default_port
$uri->port $uri->port != $uri->default_port
Truth table0%0/3
A and B
A= $uri->portB= $uri->port != $uri->default_port
ABresult
0X0
100
111
146 }
147  
148 sub make_form_data
149 0 ▶{
Subroutine make_form_data: not called CC=57 SCAR=81.0
Pod documented
150 0 my $self = shift( @_ );
151 0 my $this = shift( @_ );
152 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
153 0 ▶ $self->_load_class( 'HTTP::Promise::Headers' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Headers") or return $self->pass_error 0 0 0
154 0 my $ent = HTTP::Promise::Entity->new( debug => $self->debug );
155 0 my( $headers, $ct );
156 0 ▶ if( $headers = $self->headers )
Branch: 1 branch
BranchTrueFalseTotal
if ($headers = $self->headers) { } 0 0 0
157 {
158 0 $ct = $headers->new_field( 'Content-Type' => $headers->content_type );
159 0 ▶ $ct->boundary( $ent->make_boundary ) unless( $ct->boundary );
Branch: 1 branch
BranchTrueFalseTotal
unless $ct->boundary 0 0 0
160 }
161 else
162 {
163 0 $headers = HTTP::Promise::Headers->new;
164 0 $ct = $headers->new_field( 'Content-Type' );
165 0 my $boundary = $ent->make_boundary;
166 0 $ct->boundary( $boundary );
167 }
168 0 $ent->headers( $headers );
169 0 $ct->type( 'multipart/form-data' );
170 0 $headers->content_type( "$ct" );
171 0 $self->entity( $ent );
172
173 0 ▶ if( $self->_is_a( $this => 'HTTP::Promise::Body::Form' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_a($this, 'HTTP::Promise::Body::Form')) { } 0 0 0
elsif ($self->_is_a($this, 'HTTP::Promise::Body::Form::Data')) { } 0 0 0
174 {
175 0 ▶ my $form_data = $this->as_form_data ||
Condition0%0/2
$this->as_form_data || (return $self->pass_error($this->error))
l!l
00
MC/DC0%0/1
$this->as_form_data || (return $self->pass_error($this->error))
$this->as_form_data
Truth table0%0/2
A
A= $this->as_form_data
Aresult
11
00
176 return( $self->pass_error( $this->error ) );
177 0 ▶ my $parts = $form_data->make_parts ||
Condition0%0/2
$form_data->make_parts || (return $self->pass_error($form_data->error))
l!l
00
MC/DC0%0/1
$form_data->make_parts || (return $self->pass_error($form_data->error))
$form_data->make_parts
Truth table0%0/2
A
A= $form_data->make_parts
Aresult
11
00
178 return( $self->pass_error( $form_data->error ) );
179 0 $ent->parts( $parts );
180 0 return( $ent );
181 }
182 elsif( $self->_is_a( $this => 'HTTP::Promise::Body::Form::Data' ) )
183 {
184 0 ▶ my $parts = $this->make_parts ||
Condition0%0/2
$this->make_parts || (return $self->pass_error($this->error))
l!l
00
MC/DC0%0/1
$this->make_parts || (return $self->pass_error($this->error))
$this->make_parts
Truth table0%0/2
A
A= $this->make_parts
Aresult
11
00
185 return( $self->pass_error( $this->error ) );
186 0 $ent->parts( $parts );
187 0 return( $ent );
188 }
189
190 0 ▶ my $args = $self->_is_array( $this ) ? $this : [%$this];
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_array($this) ? : 0 0 0
191 0 ▶ for( my $i = 0; $i < scalar( @$args ); $i += 2 )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$args) 0 0 0
192 {
193 0 my $k = $args->[$i];
194 0 my $v = $args->[$i+1];
195 # Content-Type: image/gif
196 # Content-Transfer-Encoding: base64
197 # Content-Disposition: inline; filename="3d-vise.gif"
198
199 # Content-Disposition: form-data; name="file_upload"; filename="&#22825;&#29399;.png"
200 # Content-Type: image/png
201
202 0 my $def;
203 # This will be true also for HTTP::Promise::Body::File, because it inherits from Module::Generic::File
204 0 ▶ if( $self->is_a( $v => 'Module::Generic::File' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->is_a($v, 'Module::Generic::File')) { } 0 0 0
elsif (ref $v eq 'HASH') { } 0 0 0
205 {
206 0 ▶ $def =
Condition0%0/3
$v->finfo->mime_type || $HTTP::Promise::Request::DEFAULT_MIME_TYPE
l!l&&r!l&&!r
000
MC/DC0%0/2
$v->finfo->mime_type || $HTTP::Promise::Request::DEFAULT_MIME_TYPE
$v->finfo->mime_type $HTTP::Promise::Request::DEFAULT_MIME_TYPE
Truth table0%0/3
A || B
A= $v->finfo->mime_typeB= $HTTP::Promise::Request::DEFAULT_MIME_TYPE
ABresult
1X1
011
000
207 {
208 filename => $v->basename,
209 type => ( $v->finfo->mime_type || $DEFAULT_MIME_TYPE ),
210 };
211 }
212 # An hash referenece can be passed as the value to provide granularity.
213 # It can be used for regular scalar or file upload
214 elsif( ref( $v ) eq 'HASH' )
215 {
216 0 $def = $v;
217 0 my $f;
218 0 ▶ if( $def->{file} )
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"file"}) 0 0 0
219 {
220 $f = $self->_is_a( $def->{file} => 'Module::Generic::File' )
221 ? $def->{file}
222 0 ▶ : $self->new_file( $def->{file} );
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_a($def->{'file'}, 'Module::Generic::File') ? : 0 0 0
223 0 ▶ return( $self->pass_error ) if( !defined( $f ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $f 0 0 0
224 0 $def->{file} = $f;
225 }
226
227 0 ▶ if( $f &&
Branch: 1 branch
BranchTrueFalseTotal
if ($f and not $def->{"filename"} and !$def->{"headers"} || ref $def->{"headers"} ne "ARRAY" && !$self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") || $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") && $def->{"headers"}->content_disposition->index("filename") == -1) 0 0 0
Condition0%0/18
$f and not $def->{"filename"} and !$def->{"headers"} || ref $def->{"headers"} ne "ARRAY" && !$self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") || $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") && $def->{"headers"}->content_disposition->index("filename") == -1
!ll&&!rl&&r
000
$f and not $def->{"filename"}
!ll&&!rl&&r
000
not $def->{"headers"} or ref $def->{"headers"} ne "ARRAY" and not $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") or $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") and $def->{"headers"}->content_disposition->index("filename") == -1
l!l&&r!l&&!r
000
not $def->{"headers"} or ref $def->{"headers"} ne "ARRAY" and not $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers")
l!l&&r!l&&!r
000
ref $def->{"headers"} ne "ARRAY" and not $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers")
!ll&&!rl&&r
000
$self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") and $def->{"headers"}->content_disposition->index("filename") == -1
!ll&&!rl&&r
000
MC/DC0%0/7
$f and not $def->{"filename"} and !$def->{"headers"} || ref $def->{"headers"} ne "ARRAY" && !$self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") || $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") && $def->{"headers"}->content_disposition->index("filename") == -1
$f not $def->{"filename"} not $def->{"headers"} ref $def->{"headers"} ne "ARRAY" not $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") $def->{"headers"}->content_disposition->index("filename") == -1
Truth table0%0/10
A and not(B) and not(C) or D and not(E) or F and G
A= $fB= not $def->{"filename"}C= not $def->{"headers"}D= ref $def->{"headers"} ne "ARRAY"E= not $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers")F= $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers")G= $def->{"headers"}->content_disposition->index("filename") == -1
ABCDEFGresult
0XXXXXX0
10XXXXX0
1100X0X0
1100X100
110100X0
11010100
111XXXX1
11011XX1
1100X111
11010111
228 !$def->{filename} &&
229 ( !$def->{headers} ||
230 ( ref( $def->{headers} ) ne 'ARRAY' &&
231 !$self->_is_a( $def->{headers} => 'HTTP::Promise::Headers' )
232 ) ||
233 (
234 $self->_is_a( $def->{headers} => 'HTTP::Promise::Headers' ) &&
235 $def->{headers}->content_disposition->index( 'filename' ) == -1
236 )
237 ) )
238 {
239 0 $def->{filename} = $f->basename;
240 }
241 0 ▶ if( $f &&
Branch: 1 branch
BranchTrueFalseTotal
if ($f and not $def->{"type"} and !$def->{"headers"} || ref $def->{"headers"} ne "ARRAY" && !$self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") || $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") && !$def->{"headers"}->content_type) 0 0 0
Condition0%0/18
$f and not $def->{"type"} and !$def->{"headers"} || ref $def->{"headers"} ne "ARRAY" && !$self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") || $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") && !$def->{"headers"}->content_type
!ll&&!rl&&r
000
$f and not $def->{"type"}
!ll&&!rl&&r
000
not $def->{"headers"} or ref $def->{"headers"} ne "ARRAY" and not $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") or $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") and not $def->{"headers"}->content_type
l!l&&r!l&&!r
000
not $def->{"headers"} or ref $def->{"headers"} ne "ARRAY" and not $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers")
l!l&&r!l&&!r
000
ref $def->{"headers"} ne "ARRAY" and not $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers")
!ll&&!rl&&r
000
$self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") and not $def->{"headers"}->content_type
!ll&&!rl&&r
000
MC/DC0%0/7
$f and not $def->{"type"} and !$def->{"headers"} || ref $def->{"headers"} ne "ARRAY" && !$self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") || $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") && !$def->{"headers"}->content_type
$f not $def->{"type"} not $def->{"headers"} ref $def->{"headers"} ne "ARRAY" not $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers") not $def->{"headers"}->content_type
Truth table0%0/10
A and not(B) and not(C) or D and not(E) or F and not(G)
A= $fB= not $def->{"type"}C= not $def->{"headers"}D= ref $def->{"headers"} ne "ARRAY"E= not $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers")F= $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers")G= not $def->{"headers"}->content_type
ABCDEFGresult
0XXXXXX0
10XXXXX0
1100X0X0
1100X100
110100X0
11010100
111XXXX1
11011XX1
1100X111
11010111
242 !$def->{type} &&
243 ( !$def->{headers} ||
244 ( ref( $def->{headers} ) ne 'ARRAY' &&
245 !$self->_is_a( $def->{headers} => 'HTTP::Promise::Headers' )
246 ) ||
247 (
248 $self->_is_a( $def->{headers} => 'HTTP::Promise::Headers' ) &&
249 !$def->{headers}->content_type
250 )
251 ) )
252 {
253 0 $def->{type} = $f->finfo->mime_type;
254 }
255 }
256
257 0 my $part = HTTP::Promise::Entity->new;
258 0 my $disp = $headers->new_field( 'Content-Disposition' => 'form-data' );
259 0 $disp->name( "$k" );
260 0 my( $ph, $body );
261 0 ▶ if( defined( $def ) && ref( $def ) eq 'HASH' )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $def and ref $def eq 'HASH') { } 0 0 0
Condition0%0/3
defined $def and ref $def eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $def and ref $def eq "HASH"
defined $def ref $def eq "HASH"
Truth table0%0/3
A and B
A= defined $defB= ref $def eq "HASH"
ABresult
0X0
100
111
262 {
263 0 ▶ if( exists( $def->{headers} ) && ref( $def->{headers} ) eq 'ARRAY' )
Branch: 2 branches
BranchTrueFalseTotal
if (exists $def->{'headers'} and ref $def->{'headers'} eq 'ARRAY') { } 0 0 0
elsif (exists $def->{'headers'} and $self->_is_a($def->{'headers'}, 'HTTP::Promise::Headers')) { } 0 0 0
Condition0%0/3
exists $def->{"headers"} and ref $def->{"headers"} eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
exists $def->{"headers"} and ref $def->{"headers"} eq "ARRAY"
exists $def->{"headers"} ref $def->{"headers"} eq "ARRAY"
Truth table0%0/3
A and B
A= exists $def->{"headers"}B= ref $def->{"headers"} eq "ARRAY"
ABresult
0X0
100
111
264 {
265 0 ▶ $ph = HTTP::Promise::Headers->new( @{$def->{headers}} ) ||
Condition0%0/2
'HTTP::Promise::Headers'->new(@{$$def{'headers'};}) || (return $self->pass_error('HTTP::Promise::Headers'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Headers'->new(@{$$def{'headers'};}) || (return $self->pass_error('HTTP::Promise::Headers'->error))
'HTTP::Promise::Headers'->new(@{$$def{'headers'};})
Truth table0%0/2
A
A= 'HTTP::Promise::Headers'->new(@{$$def{'headers'};})
Aresult
11
00
266 return( $self->pass_error( HTTP::Promise::Headers->error ) );
267 }
268 ▶ elsif( exists( $def->{headers} ) && $self->_is_a( $def->{headers} => 'HTTP::Promise::Headers' ) )
Condition0%0/3
exists $def->{"headers"} and $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers")
!ll&&!rl&&r
000
MC/DC0%0/2
exists $def->{"headers"} and $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers")
exists $def->{"headers"} $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers")
Truth table0%0/3
A and B
A= exists $def->{"headers"}B= $self->_is_a($def->{"headers"}, "HTTP::Promise::Headers")
ABresult
0X0
100
111
269 {
270 0 $ph = $def->{headers};
271 }
272 0 ▶ $disp->filename( $def->{filename} ) if( $def->{filename} );
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"filename"} 0 0 0
273 0 ▶ $ph->content_type( "$def->{type}" ) if( $def->{type} && !$ph->exists( 'Content-Type' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $def->{"type"} and not $ph->exists("Content-Type") 0 0 0
Condition0%0/3
$def->{"type"} and not $ph->exists("Content-Type")
!ll&&!rl&&r
000
MC/DC0%0/2
$def->{"type"} and not $ph->exists("Content-Type")
$def->{"type"} not $ph->exists("Content-Type")
Truth table0%0/3
A and not(B)
A= $def->{"type"}B= not $ph->exists("Content-Type")
ABresult
0X0
100
111
274
275 # If the user asks to encode the file and the file is not zero-byte big
276 0 ▶ if( $def->{file} && $def->{encoding} && $def->{file}->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($def->{"file"} and $def->{"encoding"} and $def->{"file"}->length) 0 0 0
Condition0%0/6
$def->{"file"} and $def->{"encoding"} and $def->{"file"}->length
!ll&&!rl&&r
000
$def->{"file"} and $def->{"encoding"}
!ll&&!rl&&r
000
MC/DC0%0/3
$def->{"file"} and $def->{"encoding"} and $def->{"file"}->length
$def->{"file"} $def->{"encoding"} $def->{"file"}->length
Truth table0%0/4
A and B and C
A= $def->{"file"}B= $def->{"encoding"}C= $def->{"file"}->length
ABCresult
0XX0
10X0
1100
1111
277 {
278 0 ▶ my $encodings = $self->_is_array( $def->{encoding} ) ? $def->{encoding} : [$def->{encoding}];
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_array($def->{'encoding'}) ? : 0 0 0
279 0 ▶ $self->_load_class( 'HTTP::Promise::Stream' ) ||
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Stream") or return $self->pass_error 0 0 0
280 return( $self->pass_error );
281 0 my $source = $def->{file};
282 0 foreach my $enc ( @$encodings )
283 {
284 0 ▶ my $s = HTTP::Promise::Stream->new( $source, encoding => $def->{encoding} ) ||
Condition0%0/2
'HTTP::Promise::Stream'->new($source, 'encoding', $def->{'encoding'}) || (return $self->pass_error('HTTP::Promise::Stream'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Stream'->new($source, 'encoding', $def->{'encoding'}) || (return $self->pass_error('HTTP::Promise::Stream'->error))
'HTTP::Promise::Stream'->new($source, 'encoding', $def->{'encoding'})
Truth table0%0/2
A
A= 'HTTP::Promise::Stream'->new($source, 'encoding', $def->{'encoding'})
Aresult
11
00
285 return( $self->pass_error( HTTP::Promise::Stream->error ) );
286 0 my $file = $self->new_file;
287 0 my $bytes = $s->read( $file );
288 0 ▶ return( $self->pass_error( $s->error ) ) if( !defined( $bytes ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $bytes 0 0 0
289 0 ▶ return( $self->error( "No encoded byte could be writen to file '$file' for encoding '$enc' with source file '$source'." ) ) if( !$bytes );
Branch: 1 branch
BranchTrueFalseTotal
unless $bytes 0 0 0
290 0 $source = $file;
291 }
292 0 ▶ $ph->content_transfer_encoding( join( ' ', @$encodings ) ) if( scalar( @$encodings ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$encodings 0 0 0
293 0 $part->is_encoded(1);
294 }
295
296 0 ▶ if( $def->{file} )
Branch: 2 branches
BranchTrueFalseTotal
if ($def->{'file'}) { } 0 0 0
elsif ($def->{'value'}) { } 0 0 0
297 {
298 0 $body = $part->new_body( file => $def->{file} );
299 }
300 elsif( $def->{value} )
301 {
302 0 $body = $part->new_body( string => $def->{value} );
303 }
304 }
305 else
306 {
307 0 ▶ $ph = HTTP::Promise::Headers->new ||
Condition0%0/2
'HTTP::Promise::Headers'->new || (return $self->pass_error('HTTP::Promise::Headers'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Headers'->new || (return $self->pass_error('HTTP::Promise::Headers'->error))
'HTTP::Promise::Headers'->new
Truth table0%0/2
A
A= 'HTTP::Promise::Headers'->new
Aresult
11
00
308 return( $self->pass_error( HTTP::Promise::Headers->error ) );
309 0 $body = $part->new_body( string => $v );
310 }
311
312 0 $ph->content_disposition( "${disp}" );
313 0 ▶ $part->headers( $ph ) if( defined( $ph ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $ph 0 0 0
314 0 ▶ $part->body( $body ) if( defined( $body ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $body 0 0 0
315 0 $ent->parts->push( $part );
316 }
317 0 return( $self );
318 }
319  
320 28 ▶sub method { return( shift->_set_get_scalar_as_object( 'method', @_ ) ); }
Subroutine method: called CC=1 SCAR=0.0
Pod documented
321  
322 sub parse
323 7 ▶{
Subroutine parse: called CC=10 SCAR=23.9
Pod documented
324 7 my $self = shift( @_ );
325 7 my $str = shift( @_ );
326 7 ▶ warn( 'Undefined argument to ' . ( ref( $self ) || $self ) . '->parse()' ) if( !defined( $str ) && warnings::enabled( 'HTTP::Promise' ) );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $str and warnings::enabled("HTTP::Promise") 1 6 7
Condition66%4/6
not defined $str and warnings::enabled("HTTP::Promise")
!ll&&!rl&&r
511
ref $self || $self
l!l&&r!l&&!r
010
MC/DC100%2/2
not defined $str and warnings::enabled("HTTP::Promise")
not defined $str warnings::enabled("HTTP::Promise")
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table100%3/3
not(A) and B
A= not defined $strB= warnings::enabled("HTTP::Promise")
ABresult
0X0
100
111
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
327 7 $self->clear_error;
328 7 ▶ if( !defined( $str ) || !length( $str ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $str and length $str) 2 5 7
Condition66%2/3
defined $str and length $str
!ll&&!rl&&r
205
MC/DC50%1/2
defined $str and length $str
defined $str length $str
Truth table66%2/3
A and B
A= defined $strB= length $str
ABresult
0X0
100
111
329 {
330 2 ▶ return( ref( $self ) ? $self : $self->new );
Branch: 1 branch
BranchTrueFalseTotal
ref $self ? : 0 2 2
331 }
332 5 my $opts = $self->_get_args_as_hash( @_ );
333 5 ▶ my $p = HTTP::Promise::Parser->new( debug => ( delete( $opts->{debug} ) || $self->debug ) );
Condition33%1/3
delete $opts->{'debug'} || $self->debug
l!l&&r!l&&!r
005
MC/DC0%0/2
delete $opts->{'debug'} || $self->debug
delete $opts->{'debug'} $self->debug
Truth table33%1/3
A || B
A= delete $opts->{'debug'}B= $self->debug
ABresult
1X1
011
000
334 5 $opts->{request} = 1;
335 5 ▶ my $ent = $p->parse( $str, ( %$opts ? ( $opts ) : () ) ) || return( $self->pass_error( $p->error ) );
Branch: 1 branch
BranchTrueFalseTotal
%$opts ? : 5 0 5
Condition100%2/2
$p->parse($str, %$opts ? $opts : ()) || (return $self->pass_error($p->error))
l!l
32
MC/DC100%1/1
$p->parse($str, %$opts ? $opts : ()) || (return $self->pass_error($p->error))
$p->parse($str, %$opts ? $opts : ())
Truth table100%2/2
A
A= $p->parse($str, %$opts ? $opts : ())
Aresult
11
00
336 3 return( $ent->http_message );
337 }
338  
339 # See rfc7230, section 3.1 <https://tools.ietf.org/html/rfc7230#section-3.1>
340 sub start_line
341 6 ▶{
Subroutine start_line: called CC=10 SCAR=24.0
Pod documented
342 6 my $self = shift( @_ );
343 6 my $eol = shift( @_ );
344 6 ▶ $eol = "\n" if( !defined( $eol ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $eol 6 0 6
345 6 ▶ my $req_line = $self->method || $DEFAULT_METHOD || 'GET';
Condition60%3/5
$self->method || $HTTP::Promise::Request::DEFAULT_METHOD || 'GET'
l!l
60
$self->method or $HTTP::Promise::Request::DEFAULT_METHOD
l!l&&r!l&&!r
330
MC/DC0%0/2
$self->method || $HTTP::Promise::Request::DEFAULT_METHOD || 'GET'
$self->method $HTTP::Promise::Request::DEFAULT_METHOD
Truth table66%2/3
A or B
A= $self->methodB= $HTTP::Promise::Request::DEFAULT_METHOD
ABresult
1X1
011
000
346 6 my $uri = $self->uri;
347 6 ▶ if( defined( $uri ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $uri) { } 3 3 6
348 {
349 3 ▶ if( $self->uri_absolute )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->uri_absolute) { } 0 3 3
350 {
351 0 $uri = "$uri";
352 }
353 else
354 {
355 3 $uri = $uri->path_query;
356 }
357 3 ▶ $uri = '/' if( !length( $uri ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $uri 3 0 3
358 }
359 else
360 {
361 # $uri = '-';
362 3 $uri = '/';
363 }
364 6 $req_line .= " $uri";
365 6 my $proto = $self->protocol;
366 # $req_line .= " $proto" if( defined( $proto ) && length( $proto ) );
367 6 ▶ if( defined( $proto ) && length( $proto ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $proto and length $proto) { } 3 3 6
Condition66%2/3
defined $proto and length $proto
!ll&&!rl&&r
303
MC/DC50%1/2
defined $proto and length $proto
defined $proto length $proto
Truth table66%2/3
A and B
A= defined $protoB= length $proto
ABresult
0X0
100
111
368 {
369 3 $req_line .= " $proto";
370 }
371 else
372 {
373 3 ▶ $req_line .= ' ' . ( $self->default_protocol || 'HTTP/1.1' );
Condition50%1/2
$self->default_protocol || 'HTTP/1.1'
l!l
30
MC/DC0%0/1
$self->default_protocol || 'HTTP/1.1'
$self->default_protocol
Truth table50%1/2
A
A= $self->default_protocol
Aresult
11
00
374 }
375 6 return( $req_line );
376 }
377  
378 # NOTE: method protocol is inherited from HTTP::Promise::Message
379 0 ▶sub timeout { return( shift->_set_get_number( 'timeout', @_ ) ); }
Subroutine timeout: not called CC=1 SCAR=6.9
Pod documented
380  
381 20 ▶sub uri { return( shift->_set_get_uri( { field => 'uri', class => 'URI' }, @_ ) ); }
Subroutine uri: called CC=1 SCAR=0.0
Pod documented
382  
383 3 ▶sub uri_absolute { return( shift->_set_get_boolean( 'uri_absolute', @_ ) ); }
Subroutine uri_absolute: called CC=1 SCAR=0.0
Pod documented
384  
385 0 ▶sub uri_canonical { return( shift->uri->canonical ); }
Subroutine uri_canonical: not called CC=1 SCAR=6.9
Pod documented
386  
387 sub url_encode
388 0 ▶{
Subroutine url_encode: not called CC=3 SCAR=24.8
Pod documented
389 0 my $self = shift( @_ );
390 0 my $str = shift( @_ );
391 0 ▶ $self->_load_class( 'URL::Encode::XS' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("URL::Encode::XS") or return $self->pass_error 0 0 0
392 0 ▶ if( Encode::is_utf8( $str ) )
Branch: 1 branch
BranchTrueFalseTotal
if (&Encode::is_utf8($str)) { } 0 0 0
393 {
394 0 return( URL::Encode::XS::url_encode_utf8( "$str" ) );
395 }
396 else
397 {
398 0 return( URL::Encode::XS::url_encode( "$str" ) );
399 }
400 }
401  
402 sub url_encode_utf8
403 0 ▶{
Subroutine url_encode_utf8: not called CC=2 SCAR=17.9
Pod documented
404 0 my $self = shift( @_ );
405 0 my $str = shift( @_ );
406 0 ▶ $self->_load_class( 'URL::Encode::XS' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("URL::Encode::XS") or return $self->pass_error 0 0 0
407 0 return( URL::Encode::XS::url_encode_utf8( "$str" ) );
408 }
409  
410 # NOTE: sub FREEZE is inherited
411  
412 # NOTE: sub STORABLE_freeze is inherited
413  
414 # NOTE: sub STORABLE_thaw is inherited
415  
416 # NOTE: sub THAW is inherited
417  
418 5 1;
419 # NOTE: POD
420 __END__
« Promise.pm ↑ Summary Promise/Stream.pm »