lib/HTTP/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
« blib/lib/HTTP/Promise.pm ↑ Summary blib/lib/HTTP/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 {
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( HTTP::Promise::Message );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 use vars qw( $VERSION $EXCEPTION_CLASS $KNOWN_METHODS $KNOWN_METHODS_I $TIMEOUT
22 6 ▶ $DEFAULT_MIME_TYPE $DEFAULT_METHOD $DEFAULT_PROTOCOL $SCHEME_RE $INTL_URI_RE );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶ use Cookie::Jar;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶ use Crypt::Misc 0.076;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 # use HTTP::Parser2::XS 0.01 ();
26 6 ▶ use HTTP::Promise::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 6 ▶ use HTTP::Promise::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 ▶ use HTTP::Promise::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 # use Nice::Try v1.2.0;
30 6 ▶ 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 6 ▶ use URI 5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 6 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 6 local $" = '|';
42 6 my @methods = qw( CONNECT DELETE GET HEAD OPTIONS PATCH POST PUT TRACE );
43 6 our $KNOWN_METHODS = qr/(?:@methods)/;
44 6 our $KNOWN_METHODS_I = qr/(?:@methods)/i;
45 6 our $TIMEOUT = 10;
46 6 our $DEFAULT_MIME_TYPE = 'application/octet-stream';
47 6 our $DEFAULT_METHOD = 'GET';
48 6 our $DEFAULT_PROTOCOL = 'HTTP/1.1';
49 # Borrowed from URI::_server
50 6 our $GROSS_URI_RE = qr{(?<scheme>(?:https?:)?)//(?<host>[^/?\#]*)(?<rest>.*)};
51 # [\x00-\x7f]
52 6 our $INTL_URI_RE = qr{(?<scheme>(?:https?:)?)//(?<host>[^\x00-\x7f]+\.[^/?\#]*)(?<rest>.*)};
53 6 our $VERSION = 'v0.2.0';
54 };
55  
56 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 6 ▶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 {
68 35 ▶ my $self = shift( @_ );
Subroutine init: called CC=13 SCAR=27.6
Pod documented
69 35 my( $method, $uri );
70 35 my $iKNOWN_METHODS = qr/$KNOWN_METHODS/i;
71 35 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 31 4 35
72 {
73 31 ▶ if( @_ == 1 && ref( $_[0] ) eq 'HASH' )
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'HASH') { } 0 31 31
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])) { } 21 10 31
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
3100
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
10021
@_ >= 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
01021
@_ >= 2 and defined $_[0]
!ll&&!rl&&r
0031
$_[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
17410
$_[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
2010
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
1020
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 21 ( $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 25 $self->{default_protocol} = $DEFAULT_PROTOCOL;
95 # properties headers and content are set by our parent class HTTP::Promise::Message
96 25 $self->{method} = $method;
97 25 $self->{timeout} = $TIMEOUT;
98 25 $self->{uri} = $uri;
99 # Should as_string return an absolute uri or just the absolute path?
100 25 $self->{uri_absolute} = 0;
101 25 $self->{_init_strict_use_sub} = 1;
102 25 $self->{_init_params_order} = [qw( content headers )];
103 25 $self->{_exception_class} = $EXCEPTION_CLASS;
104 # $self->SUPER::init( ( defined( $headers ) ? $headers : () ), @_ ) || return( $self->pass_error );
105 25 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 25 25
106 25 return( $self );
107 }
108  
109 sub accept_decodable
110 {
111 1 ▶ my $self = shift( @_ );
Subroutine accept_decodable: called CC=1 SCAR=0.0
Pod documented
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 {
119 0 ▶ my $self = shift( @_ );
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
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 {
134 3 ▶ my $self = shift( @_ );
Subroutine dump: called CC=1 SCAR=0.0
Pod documented
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 {
143 0 ▶ my $self = shift( @_ );
Subroutine host: not called CC=3 SCAR=24.8
Pod documented
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 {
150 0 ▶ my $self = shift( @_ );
Subroutine make_form_data: not called CC=57 SCAR=81.0
Pod documented
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 {
324 7 ▶ my $self = shift( @_ );
Subroutine parse: called CC=10 SCAR=23.9
Pod documented
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 {
342 6 ▶ my $self = shift( @_ );
Subroutine start_line: called CC=10 SCAR=24.0
Pod documented
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 {
389 0 ▶ my $self = shift( @_ );
Subroutine url_encode: not called CC=3 SCAR=24.8
Pod documented
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 {
404 0 ▶ my $self = shift( @_ );
Subroutine url_encode_utf8: not called CC=2 SCAR=17.9
Pod documented
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 6 1;
419 # NOTE: POD
420 __END__
421  
422 =encoding utf-8
423  
424 =head1 NAME
425  
426 HTTP::Promise::Request - HTTP Request Class
427  
428 =head1 SYNOPSIS
429  
430 use HTTP::Promise::Request;
431 my $r = HTTP::Promise::Request->new( $method, $uri, $headers, $content, k1 => v1, k2 => v2);
432 my $r = HTTP::Promise::Request->new( $method, $uri, $headers, $content, { k1 => v1, k2 => v2 });
433 my $r = HTTP::Promise::Request->new( $method, $uri, $headers, k1 => v1, k2 => v2);
434 my $r = HTTP::Promise::Request->new( $method, $uri, $headers, { k1 => v1, k2 => v2 });
435 my $r = HTTP::Promise::Request->new( $method, $uri, k1 => v1, k2 => v2);
436 my $r = HTTP::Promise::Request->new( $method, k1 => v1, k2 => v2);
437 my $r = HTTP::Promise::Request->new( k1 => v1, k2 => v2 );
438 die( HTTP::Promise::Request->error ) if( !defined( $r ) );
439  
440 =head1 VERSION
441  
442 v0.2.0
443  
444 =head1 DESCRIPTION
445  
446 L<HTTP::Promise::Request> implements a similar interface to L<HTTP::Request>, but does not inherit from it. It uses a different API internally and relies on XS modules for speed while offering more features.
447  
448 L<HTTP::Promise::Request> inherits from L<HTTP::Promise::Message>
449  
450 One major difference with C<HTTP::Request> is that the HTTP request content is not necessarily stored in memory, but it relies on L<HTTP::Promise::Body> as you can see below, and this class has 2 subclasses: 1 storing data in memory when the size is reasonable (threshold set by you) and 1 storing data in a file on the filesystem for larger content.
451  
452 Here is how it fits in overall relation with other classes.
453
454 +-------------------------+ +--------------------------+
455 | | | |
456 | HTTP::Promise::Request | | HTTP::Promise::Response |
457 | | | |
458 +------------|------------+ +-------------|------------+
459 | |
460 | |
461 | |
462 | +------------------------+ |
463 | | | |
464 +--- HTTP::Promise::Message |---+
465 | |
466 +------------|-----------+
467 |
468 |
469 +------------|-----------+
470 | |
471 | HTTP::Promise::Entity |
472 | |
473 +------------|-----------+
474 |
475 |
476 +------------|-----------+
477 | |
478 | HTTP::Promise::Body |
479 | |
480 +------------------------+
481  
482 =head1 CONSTRUCTOR
483  
484 =head2 new
485  
486 my $r = HTTP::Promise::Request->new( $method, $uri, $headers, $content, k1 => v1, k2 => v2);
487 my $r = HTTP::Promise::Request->new( $method, $uri, $headers, $content, { k1 => v1, k2 => v2 });
488 my $r = HTTP::Promise::Request->new( $method, $uri, $headers, k1 => v1, k2 => v2);
489 my $r = HTTP::Promise::Request->new( $method, $uri, $headers, { k1 => v1, k2 => v2 });
490 my $r = HTTP::Promise::Request->new( $method, $uri, k1 => v1, k2 => v2);
491 my $r = HTTP::Promise::Request->new( $method, k1 => v1, k2 => v2);
492 my $r = HTTP::Promise::Request->new( k1 => v1, k2 => v2 );
493 die( HTTP::Promise::Request->error ) if( !defined( $r ) );
494  
495 my $decodable = $r->accept_decodable;
496 my $r2 = $r->clone;
497 $r->cookie_jar( Cookie::Jar->new );
498 my $jar = $r->cookie_jar;
499 my $proto = $r->default_protocol; # HTTP/1.1
500 say $r->dump;
501 my $headers = $r->headers; # Returns an HTTP::Promise::Headers object
502 $r->make_form_data;
503 $r->method( 'GET' );
504 my $method = $r->method;
505 my $r3 = $r->parse( $data );
506 say $r->start_line;
507 say $r->start_line( "\015\012" );
508 $r->timeout(5);
509 $r->uri( 'https://example.org/some/where' );
510 my $uri = $r->uri; # Returns an URI object
511 my $bool = $r->uri_absolute;
512 my $canonical_uri = $r->uri_canonical;
513  
514 Provided with an HTTP method, URI, an optional set of headers, as either an array reference or a L<HTTP::Promise::Headers> or a L<HTTP::Headers> object, some optional content and an optional hash reference of options (as the last or only parameter), and this instantiates a new L<HTTP::Promise::Request> object. The supported arguments are as follow. Each arguments can be set or changed later using the method with the same name.
515  
516 It returns the newly created object upon success, and upon error, such as bad argument provided, this sets an L<error|Module::Generic/error> and returns C<undef>
517  
518 It takes the following arguments:
519  
520 =over 4
521  
522 =item 1. C<$method>
523  
524 This is a proper HTTP method in upper case. Note that you can also provide non-standard method in any case you want.
525  
526 =item 2. C<$uri>
527  
528 The request uri such as C</> or an absolute uri, typical for making request to proxy, such as C<https://example.org/some/where>
529  
530 =item 3. C<$headers>
531  
532 An L<HTTP::Promise::Headers> object or an array reference of header field-value pairs, such as:
533  
534 my $r = HTTP::Promise::Request->new( $method, $uri, [
535 'Content-Type' => 'text/html; charset=utf-8',
536 Content_Encoding => 'gzip',
537 ]);
538  
539 =item 4. C<$content>
540  
541 C<$content> can either be a string, a scalar reference, or an L<HTTP::Promise::Body> object (L<HTTP::Promise::Body::File> and L<HTTP::Promise::Body::Scalar>)
542  
543 =back
544  
545 Each supported option below can also be set using its corresponding method.
546  
547 Supported options are:
548  
549 =over 4
550  
551 =item * C<content>
552  
553 Same as C<$content> above.
554  
555 =item * C<headers>
556  
557 Same as C<$headers> above.
558  
559 =item * C<method>
560  
561 Same as C<$method> above.
562  
563 =item * C<protocol>
564  
565 The HTTP protocol, such as C<HTTP/1.1> or C<HTTP/2>
566  
567 =item * C<uri>
568  
569 The request uri, such as C</chat> or it could also be a fully qualified uri such as C<wss://example.com/chat>
570  
571 =item * C<version>
572  
573 The HTTP protocol version. Defaults to C<1.17>
574  
575 =back
576  
577 =head1 METHODS
578  
579 =head2 accept_decodable
580  
581 This sets and returns the header C<Accept-Encoding> after having set it the value of L</decodable>
582  
583 =head2 add_content
584  
585 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/add_content>
586  
587 =head2 add_content_utf8
588  
589 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/add_content_utf8>
590  
591 =head2 add_part
592  
593 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/add_part>
594  
595 =head2 as_string
596  
597 Depending on whether L<uri_absolute> is true, this returns a HTTP request with an URI including the HTTP host (a.k.a C<absolute-form>) or only the absolute path (a.k.a C<origin-form>). The former is used when issuing requests to proxies.
598  
599 C<origin-form>:
600  
601 GET /where?q=now HTTP/1.1
602 Host: www.example.org
603  
604 C<absolute-form>:
605  
606 GET http://www.example.org/pub/WWW/TheProject.html HTTP/1.1
607  
608 See L<rfc7230, section 5.3|https://tools.ietf.org/html/rfc7230#section-5.3>
609  
610 =head2 boundary
611  
612 This is inherited from L<HTTP::Promise::Message> and returns the multipart boundary currently set in the C<Content-Type> header.
613  
614 =head2 can
615  
616 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/can>
617  
618 =head2 clear
619  
620 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/clear>
621  
622 =head2 clone
623  
624 This clones the current object and returns the clone version.
625  
626 =head2 content
627  
628 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/content>
629  
630 Use this method with care, because it will stringify the request body, thus loading it into memory, which could potentially be important if the body size is large. Maybe you can check the body size first? Something like:
631  
632 my $content;
633 $content = $r->content if( $r->body->length < 102400 );
634  
635 =head2 content_charset
636  
637 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/content_charset>
638  
639 =head2 content_ref
640  
641 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/content_ref>
642  
643 =head2 cookie_jar
644  
645 Sets or gets the L<Cookie::Jar> object. This is used to read and store cookies.
646  
647 =head2 decodable
648  
649 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/decodable>
650  
651 =head2 decode
652  
653 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/decode>
654  
655 =head2 decode_content
656  
657 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/decode_content>
658  
659 =head2 decoded_content
660  
661 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/decoded_content>
662  
663 =head2 decoded_content_utf8
664  
665 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/decoded_content_utf8>
666  
667 =head2 default_protocol
668  
669 Sets or gets the default HTTP protocol to use. This defaults to C<HTTP/1.1>
670  
671 =head2 dump
672  
673 This dumps the HTTP request and prints it on the C<STDOUT> in void context, or returns a string of it.
674  
675 =head2 encode
676  
677 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/encode>
678  
679 =head2 entity
680  
681 Sets or gets an L<HTTP::Promise::Entity> object.
682  
683 This object is automatically created upon instantiation of the HTTP request, and if you also provide some content when creating a new object, an L<HTTP::Promise::Body> object will also be created.
684  
685 =head2 header
686  
687 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/header>
688  
689 =head2 headers
690  
691 Sets or gets a L<HTTP::Promise::Headers> object.
692  
693 A header object is always created upon instantiation, whether you provided headers fields or not.
694  
695 =head2 headers_as_string
696  
697 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/headers_as_string>
698  
699 =head2 host
700  
701 my $req = HTTP::Promise::Request->new( GET => 'https://example.com' );
702 say $req->host; # example.com
703 my $req = HTTP::Promise::Request->new( GET => 'https://example.com:8080' );
704 say $req->host; # example.com:8080
705 my $req = HTTP::Promise::Request->new( GET => 'http://example.com' );
706 say $req->host; # example.com
707 my $req = HTTP::Promise::Request->new( GET => 'http://example.com:8383' );
708 say $req->host; # example.com:8383
709  
710 Read-only. Returns the request URI host part.
711 The port number is added and separated by a colon if the port number is not standard.
712  
713 So, if the protocol is C<https> and the port number used is something other than 443, it will be suffixed.
714 If the protocol is C<http> and the port number used is something other than 80, it will be suffixed.
715  
716 =head2 is_encoding_supported
717  
718 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/is_encoding_supported>
719  
720 =head2 make_boundary
721  
722 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/make_boundary>
723  
724 =head2 make_form_data
725  
726 This takes either an L<HTTP::Promise::Body::Form> object, an L<HTTP::Promise::Body::Form::Data> object, an array reference or an hash reference of form name-value pairs and builds a C<multipart/form-data>
727  
728 If a C<boundary> is already set in the C<Content-Type> header field, it will be used, otherwise a new one will be generated.
729  
730 Each name provided will be the C<form-data> name for each part.
731  
732 It returns the current entity object, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
733  
734 Each value provided can be either one of the following:
735  
736 =over 4
737  
738 =item 1. string
739  
740 =item 2. a L<Module::Generic::File> object
741  
742 In this case, the file-mime type will try to be guessed. If you prefer to be specific about the file mime-type, use the alternate C<hash reference> below.
743  
744 =item 3. an hash reference
745  
746 For more granular control, you can provide an hash reference with the following supported properties:
747  
748 =over 8
749  
750 =item * C<encoding>
751  
752 The encoding to be applied to the content. This will also set the C<Content-Encoding> for this C<form-data> part.
753  
754 Note that when provided, the encodings will be applied immediately on the C<form-data> content, whether it is a string or a file.
755  
756 =item * C<file>
757  
758 A filepath to content for this part. The file content will not be loaded into memory, but instead will be used as-is. When it will need to be sent, it will be read from in chunks.
759  
760 If this provided, the L<body object|HTTP::Promise::Body> will be a L<HTTP::Promise::Body::File>
761  
762 =item * C<filename>
763  
764 The C<filename> attribute of the C<Content-Disposition> or this C<form-data> part.
765  
766 If this is not provided and C<headers> property is not provided, or C<headers> is specified, but the C<Content-Disposition> C<filename> attribute is not set, then the C<file> basename will be used instead.
767  
768 =item * C<headers>
769  
770 An L<HTTP::Promise::Headers> object or an array reference of header field-value pairs.
771  
772 =item * C<type>
773  
774 The mime-type to be used for this C<form-data> part.
775  
776 If a C<file> is provided and this is not specified, it will try to guess the mime-type using L<HTTP::Promise::MIME>
777  
778 Note that even if this provided, and if a C<headers> has been specified, it will not override an existing C<Content-Type> header that would have been set.
779  
780 =item * C<value>
781  
782 The C<form-data> value. This is an alternative to providing the C<form-data> content as a C<file>
783  
784 Obviously you should not use both and if you do, C<file> will take priority.
785  
786 If this provided, the L<body object|HTTP::Promise::Body> will be a L<HTTP::Promise::Body::Scalar>
787  
788 =back
789  
790 =back
791  
792 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."
793  
794 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>
795  
796 See also L<HTTP::Promise::Body::Form::Data> for an alternate easy way to create and manipulate C<form-data>, and see also L<HTTP::Promise::Entity/as_form_data>, which will create and return a L<HTTP::Promise::Body::Form::Data> object.
797  
798 =head2 method
799  
800 Sets or gets the HTTP C<method>, such as C<CONNECT>, C<DELETE>, C<GET>, C<HEAD>, C<OPTIONS>, C<PATCH>, C<POST>, C<PUT>, C<TRACE> which are the standard ones as defined by L<rfc7231, section 4.1|https://tools.ietf.org/html/rfc7231#section-4.1>
801  
802 Note that casing must be uppercase for standard methods, but non-standard ones can be whatever you want as long as it complies with the rfc7231.
803  
804 This returns the current method set, if any, as an L<scalar object|Module::Generic::Scalar>
805  
806 =head2 parse
807  
808 Provided with a scalar reference of data, a glob or a file path, and an hash or hash reference of options and this will parse the data provided using L<HTTP::Promise::Parser/parse>, passing it whatever options has been provided. See L<HTTP::Promise::Parser/parse_fh> for the supported options.
809  
810 This returns the resulting L<HTTP::Promise::Message> object from the parsing, or, upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
811  
812 Note that the resulting L<HTTP::Promise::Message> object can be a L<HTTP::Promise::Request> or L<HTTP::Promise::Response> object (both of which inherits from L<HTTP::Promise::Message>) if a start-line was found, or else just an L<HTTP::Promise::Message> object.
813  
814 =head2 parts
815  
816 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/parts>
817  
818 =head2 protocol
819  
820 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/protocol>
821  
822 =head2 start_line
823  
824 Read-only.
825  
826 Returns a regular string representing the start-line containing the L<method|/method>, the L<uri|/uri> and the L<protocol|/protocol> of the request.
827  
828 For example:
829  
830 GET / HTTP/1.1
831  
832 See L<rfc7230, section 3.1|https://tools.ietf.org/html/rfc7230#section-3.1>
833  
834 =head2 timeout
835  
836 Sets or gets the C<timeout> as an integer. This returns the value as an L<number object|Module::Generic::Number>
837  
838 =head2 uri
839  
840 Sets or gets the C<uri>. Returns the current value, if any, as an L<URI> object.
841  
842 =head2 uri_absolute
843  
844 Boolean. Sets or gets whether L</as_string> will return a request including an uri in C<absolute-form> (with the host included) or in C<origin-form> (only with the absolute path).
845  
846 If true, it sets the former otherwise the latter. Default to false.
847  
848 =head2 uri_canonical
849  
850 Returns the current L</uri> in its canonical form by calling L<URI/canonical>
851  
852 =head2 url_encode
853  
854 my $v = $req->url_encode( "=encoding utf-8\n\n=head1 Hello World" );
855 # %3Dencoding+utf-8%0A%0A%3Dhead1+Hello+World
856  
857 or
858  
859 use utf8;
860 my $v = $req->url_encode( "文字化けかな" );
861 # %C3%A6%C2%96%C2%87%C3%A5%C2%AD%C2%97%C3%A5%C2%8C%C2%96%C3%A3%C2%81%C2%91%C3%A3%C2%81%C2%8B%C3%A3%C2%81%C2%AA
862  
863 This returns the string provided properly URL-encoded.
864  
865 This is actually a convenient wrapper around C<URL::Encode::XS::url_encode> or C<URL::Encode::XS::url_encode_utf8> if this is regarded as a Perl internal UTF-8 string.
866  
867 =head2 url_encode_utf8
868  
869 Returns the string provided URL-encoded.
870  
871 This is to be used on Perl internal UTF-8 strings.
872  
873 =head2 version
874  
875 This is inherited from L<HTTP::Promise::Message>. See L<HTTP::Promise::Message/version>
876  
877 =head1 THREAD-SAFETY
878  
879 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
880  
881 =head1 AUTHOR
882  
883 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
884  
885 =head1 SEE ALSO
886  
887 L<rfc7230|https://tools.ietf.org/html/rfc7230>, and L<rfc7231|https://tools.ietf.org/html/rfc7231>
888  
889 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>
890  
891 =head1 COPYRIGHT & LICENSE
892  
893 Copyright(c) 2022 DEGUEST Pte. Ltd.
894  
895 All rights reserved.
896  
897 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
898  
899 =cut
« blib/lib/HTTP/Promise.pm ↑ Summary blib/lib/HTTP/Promise/Stream.pm »