blib/lib/HTTP/Promise/Request.pm untested

filter: Statementstmt 0.0% 0 / 216 Branchbran 0.0% 0 / 80 Conditioncond 0.0% 0 / 132 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 32 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 479 CC 41 SCAR 56.7
CC
41
Cov
0%
make_form_data
756.0
start_line
42.0
parse
30.0
CRAP 291.2
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 use strict;
18 use warnings;
19 warnings::register_categories( 'HTTP::Promise' );
20 use parent qw( HTTP::Promise::Message );
21 use vars qw( $VERSION $EXCEPTION_CLASS $KNOWN_METHODS $KNOWN_METHODS_I $TIMEOUT
22 $DEFAULT_MIME_TYPE $DEFAULT_METHOD $DEFAULT_PROTOCOL $SCHEME_RE $INTL_URI_RE );
23 use Cookie::Jar;
24 use Crypt::Misc 0.076;
25 # use HTTP::Parser2::XS 0.01 ();
26 use HTTP::Promise::Exception;
27 use HTTP::Promise::Parser;
28 use HTTP::Promise::Stream;
29 # use Nice::Try v1.2.0;
30 use Regexp::Common qw( URI net );
31 # URI::Fast is great, but only supports simple protocols
32 # use URI::Fast 0.55;
33 use URI 5.10;
34 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 local $" = '|';
42 my @methods = qw( CONNECT DELETE GET HEAD OPTIONS PATCH POST PUT TRACE );
43 our $KNOWN_METHODS = qr/(?:@methods)/;
44 our $KNOWN_METHODS_I = qr/(?:@methods)/i;
45 our $TIMEOUT = 10;
46 our $DEFAULT_MIME_TYPE = 'application/octet-stream';
47 our $DEFAULT_METHOD = 'GET';
48 our $DEFAULT_PROTOCOL = 'HTTP/1.1';
49 # Borrowed from URI::_server
50 our $GROSS_URI_RE = qr{(?<scheme>(?:https?:)?)//(?<host>[^/?\#]*)(?<rest>.*)};
51 # [\x00-\x7f]
52 our $INTL_URI_RE = qr{(?<scheme>(?:https?:)?)//(?<host>[^\x00-\x7f]+\.[^/?\#]*)(?<rest>.*)};
53 our $VERSION = 'v0.2.0';
54 };
55  
56 use strict;
57 use warnings;
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 my $self = shift( @_ );
69 my( $method, $uri );
70 my $iKNOWN_METHODS = qr/$KNOWN_METHODS/i;
71 if( @_ )
72 {
73 if( @_ == 1 && ref( $_[0] ) eq 'HASH' )
74 {
75 my $opts = $_[0];
76 ( $method, $uri ) = CORE::delete( @$opts{qw( method uri )} );
77 }
78 elsif( @_ >= 2 &&
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 ( $method, $uri ) = splice( @_, 0, 2 );
88 }
89 else
90 {
91 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 $self->{default_protocol} = $DEFAULT_PROTOCOL;
95 # properties headers and content are set by our parent class HTTP::Promise::Message
96 $self->{method} = $method;
97 $self->{timeout} = $TIMEOUT;
98 $self->{uri} = $uri;
99 # Should as_string return an absolute uri or just the absolute path?
100 $self->{uri_absolute} = 0;
101 $self->{_init_strict_use_sub} = 1;
102 $self->{_init_params_order} = [qw( content headers )];
103 $self->{_exception_class} = $EXCEPTION_CLASS;
104 # $self->SUPER::init( ( defined( $headers ) ? $headers : () ), @_ ) || return( $self->pass_error );
105 $self->SUPER::init( @_ ) || return( $self->pass_error );
106 return( $self );
107 }
108  
109 sub accept_decodable
110 {
111 my $self = shift( @_ );
112 return( $self->header( 'Accept-Encoding', $self->decodable->join( ', ' )->scalar ) );
113 }
114  
115 sub authorization_basic { return( shift->headers->authorization_basic( @_ ) ); }
116  
117 sub clone
118 {
119 my $self = shift( @_ );
120 my $new = $self->SUPER::clone;
121 $new->method( $self->method );
122 $new->uri( $self->uri );
123 return( $new );
124 }
125  
126 # NOTE: method content is inherited from HTTP::Promise::Message
127  
128 sub cookie_jar { return( shift->_set_get_object( 'cookie_jar', 'Cookie::Jar', @_ ) ); }
129  
130 sub default_protocol { return( shift->_set_get_scalar_as_object( 'default_protocol', @_ ) ); }
131  
132 sub dump
133 {
134 my $self = shift( @_ );
135 my $start_line = $self->start_line;
136 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 my $self = shift( @_ );
144 my $uri = $self->uri;
145 return( ( $uri->port && $uri->port != $uri->default_port ) ? join( ':', $uri->host, $uri->port ) : $uri->host );
146 }
147  
148 sub make_form_data
149 {
150 my $self = shift( @_ );
151 my $this = shift( @_ );
152 $self->_load_class( 'HTTP::Promise::Entity' ) || return( $self->pass_error );
153 $self->_load_class( 'HTTP::Promise::Headers' ) || return( $self->pass_error );
154 my $ent = HTTP::Promise::Entity->new( debug => $self->debug );
155 my( $headers, $ct );
156 if( $headers = $self->headers )
157 {
158 $ct = $headers->new_field( 'Content-Type' => $headers->content_type );
159 $ct->boundary( $ent->make_boundary ) unless( $ct->boundary );
160 }
161 else
162 {
163 $headers = HTTP::Promise::Headers->new;
164 $ct = $headers->new_field( 'Content-Type' );
165 my $boundary = $ent->make_boundary;
166 $ct->boundary( $boundary );
167 }
168 $ent->headers( $headers );
169 $ct->type( 'multipart/form-data' );
170 $headers->content_type( "$ct" );
171 $self->entity( $ent );
172
173 if( $self->_is_a( $this => 'HTTP::Promise::Body::Form' ) )
174 {
175 my $form_data = $this->as_form_data ||
176 return( $self->pass_error( $this->error ) );
177 my $parts = $form_data->make_parts ||
178 return( $self->pass_error( $form_data->error ) );
179 $ent->parts( $parts );
180 return( $ent );
181 }
182 elsif( $self->_is_a( $this => 'HTTP::Promise::Body::Form::Data' ) )
183 {
184 my $parts = $this->make_parts ||
185 return( $self->pass_error( $this->error ) );
186 $ent->parts( $parts );
187 return( $ent );
188 }
189
190 my $args = $self->_is_array( $this ) ? $this : [%$this];
191 for( my $i = 0; $i < scalar( @$args ); $i += 2 )
192 {
193 my $k = $args->[$i];
194 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 my $def;
203 # This will be true also for HTTP::Promise::Body::File, because it inherits from Module::Generic::File
204 if( $self->is_a( $v => 'Module::Generic::File' ) )
205 {
206 $def =
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 $def = $v;
217 my $f;
218 if( $def->{file} )
219 {
220 $f = $self->_is_a( $def->{file} => 'Module::Generic::File' )
221 ? $def->{file}
222 : $self->new_file( $def->{file} );
223 return( $self->pass_error ) if( !defined( $f ) );
224 $def->{file} = $f;
225 }
226
227 if( $f &&
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 $def->{filename} = $f->basename;
240 }
241 if( $f &&
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 $def->{type} = $f->finfo->mime_type;
254 }
255 }
256
257 my $part = HTTP::Promise::Entity->new;
258 my $disp = $headers->new_field( 'Content-Disposition' => 'form-data' );
259 $disp->name( "$k" );
260 my( $ph, $body );
261 if( defined( $def ) && ref( $def ) eq 'HASH' )
262 {
263 if( exists( $def->{headers} ) && ref( $def->{headers} ) eq 'ARRAY' )
264 {
265 $ph = HTTP::Promise::Headers->new( @{$def->{headers}} ) ||
266 return( $self->pass_error( HTTP::Promise::Headers->error ) );
267 }
268 elsif( exists( $def->{headers} ) && $self->_is_a( $def->{headers} => 'HTTP::Promise::Headers' ) )
269 {
270 $ph = $def->{headers};
271 }
272 $disp->filename( $def->{filename} ) if( $def->{filename} );
273 $ph->content_type( "$def->{type}" ) if( $def->{type} && !$ph->exists( 'Content-Type' ) );
274
275 # If the user asks to encode the file and the file is not zero-byte big
276 if( $def->{file} && $def->{encoding} && $def->{file}->length )
277 {
278 my $encodings = $self->_is_array( $def->{encoding} ) ? $def->{encoding} : [$def->{encoding}];
279 $self->_load_class( 'HTTP::Promise::Stream' ) ||
280 return( $self->pass_error );
281 my $source = $def->{file};
282 foreach my $enc ( @$encodings )
283 {
284 my $s = HTTP::Promise::Stream->new( $source, encoding => $def->{encoding} ) ||
285 return( $self->pass_error( HTTP::Promise::Stream->error ) );
286 my $file = $self->new_file;
287 my $bytes = $s->read( $file );
288 return( $self->pass_error( $s->error ) ) if( !defined( $bytes ) );
289 return( $self->error( "No encoded byte could be writen to file '$file' for encoding '$enc' with source file '$source'." ) ) if( !$bytes );
290 $source = $file;
291 }
292 $ph->content_transfer_encoding( join( ' ', @$encodings ) ) if( scalar( @$encodings ) );
293 $part->is_encoded(1);
294 }
295
296 if( $def->{file} )
297 {
298 $body = $part->new_body( file => $def->{file} );
299 }
300 elsif( $def->{value} )
301 {
302 $body = $part->new_body( string => $def->{value} );
303 }
304 }
305 else
306 {
307 $ph = HTTP::Promise::Headers->new ||
308 return( $self->pass_error( HTTP::Promise::Headers->error ) );
309 $body = $part->new_body( string => $v );
310 }
311
312 $ph->content_disposition( "${disp}" );
313 $part->headers( $ph ) if( defined( $ph ) );
314 $part->body( $body ) if( defined( $body ) );
315 $ent->parts->push( $part );
316 }
317 return( $self );
318 }
319  
320 sub method { return( shift->_set_get_scalar_as_object( 'method', @_ ) ); }
321  
322 sub parse
323 {
324 my $self = shift( @_ );
325 my $str = shift( @_ );
326 warn( 'Undefined argument to ' . ( ref( $self ) || $self ) . '->parse()' ) if( !defined( $str ) && warnings::enabled( 'HTTP::Promise' ) );
327 $self->clear_error;
328 if( !defined( $str ) || !length( $str ) )
329 {
330 return( ref( $self ) ? $self : $self->new );
331 }
332 my $opts = $self->_get_args_as_hash( @_ );
333 my $p = HTTP::Promise::Parser->new( debug => ( delete( $opts->{debug} ) || $self->debug ) );
334 $opts->{request} = 1;
335 my $ent = $p->parse( $str, ( %$opts ? ( $opts ) : () ) ) || return( $self->pass_error( $p->error ) );
336 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 my $self = shift( @_ );
343 my $eol = shift( @_ );
344 $eol = "\n" if( !defined( $eol ) );
345 my $req_line = $self->method || $DEFAULT_METHOD || 'GET';
346 my $uri = $self->uri;
347 if( defined( $uri ) )
348 {
349 if( $self->uri_absolute )
350 {
351 $uri = "$uri";
352 }
353 else
354 {
355 $uri = $uri->path_query;
356 }
357 $uri = '/' if( !length( $uri ) );
358 }
359 else
360 {
361 # $uri = '-';
362 $uri = '/';
363 }
364 $req_line .= " $uri";
365 my $proto = $self->protocol;
366 # $req_line .= " $proto" if( defined( $proto ) && length( $proto ) );
367 if( defined( $proto ) && length( $proto ) )
368 {
369 $req_line .= " $proto";
370 }
371 else
372 {
373 $req_line .= ' ' . ( $self->default_protocol || 'HTTP/1.1' );
374 }
375 return( $req_line );
376 }
377  
378 # NOTE: method protocol is inherited from HTTP::Promise::Message
379 sub timeout { return( shift->_set_get_number( 'timeout', @_ ) ); }
380  
381 sub uri { return( shift->_set_get_uri( { field => 'uri', class => 'URI' }, @_ ) ); }
382  
383 sub uri_absolute { return( shift->_set_get_boolean( 'uri_absolute', @_ ) ); }
384  
385 sub uri_canonical { return( shift->uri->canonical ); }
386  
387 sub url_encode
388 {
389 my $self = shift( @_ );
390 my $str = shift( @_ );
391 $self->_load_class( 'URL::Encode::XS' ) || return( $self->pass_error );
392 if( Encode::is_utf8( $str ) )
393 {
394 return( URL::Encode::XS::url_encode_utf8( "$str" ) );
395 }
396 else
397 {
398 return( URL::Encode::XS::url_encode( "$str" ) );
399 }
400 }
401  
402 sub url_encode_utf8
403 {
404 my $self = shift( @_ );
405 my $str = shift( @_ );
406 $self->_load_class( 'URL::Encode::XS' ) || return( $self->pass_error );
407 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 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