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