| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Entity/Body.pm | ||
| 3 | ## Version v0.1.0 | ||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2022/04/19 | ||
| 7 | ## Modified 2022/04/19 | ||
| 8 | ## All rights reserved | ||
| 9 | ## | ||
| 10 | ## This program is free software; you can redistribute it and/or modify it | ||
| 11 | ## under the same terms as Perl itself. | ||
| 12 | ##---------------------------------------------------------------------------- | ||
| 13 | package HTTP::Promise::Body; | ||
| 14 | BEGIN | ||
| 15 | { | ||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | warnings::register_categories( 'HTTP::Promise' ); | ||
| 19 | use parent qw( Module::Generic ); | ||
| 20 | use vars qw( $VERSION $EXCEPTION_CLASS ); | ||
| 21 | use HTTP::Promise::Exception; | ||
| 22 | our $EXCEPTION_CLASS = 'HTTP::Promise::Exception'; | ||
| 23 | our $VERSION = 'v0.1.0'; | ||
| 24 | }; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | use warnings; | ||
| 28 | |||
| 29 | sub as_lines | ||
| 30 | { | ||
| 31 | my $self = shift( @_ ); | ||
| 32 | my $io = $self->open( 'r' ) || return( $self->pass_error ); | ||
| 33 | my $lines = $self->new_array; | ||
| 34 | local $_; | ||
| 35 | while( defined( $_ = $io->getline ) ) | ||
| 36 | { | ||
| 37 | $lines->push( $_ ); | ||
| 38 | } | ||
| 39 | $io->close; | ||
| 40 | return( $lines ); | ||
| 41 | } | ||
| 42 | |||
| 43 | # Very dangerous to use indiscriminately when dealing with large data stored on file | ||
| 44 | sub as_string | ||
| 45 | { | ||
| 46 | my $self = shift( @_ ); | ||
| 47 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 48 | my $str = $self->new_scalar; | ||
| 49 | my $params = {}; | ||
| 50 | for( qw( binmode debug ) ) | ||
| 51 | { | ||
| 52 | $params->{ $_ } = $opts->{ $_ } if( exists( $opts->{ $_ } ) && $opts->{ $_ } ); | ||
| 53 | } | ||
| 54 | |||
| 55 | my( $pos, $io ); | ||
| 56 | if( $self->can( 'opened' ) && ( $io = $self->opened ) ) | ||
| 57 | { | ||
| 58 | $pos = $self->tell; | ||
| 59 | # Rewind | ||
| 60 | $self->seek(0,0); | ||
| 61 | } | ||
| 62 | else | ||
| 63 | { | ||
| 64 | $io = $self->open( 'r', ( scalar( keys( %$params ) ) ? $params : () ) ) || return( $self->pass_error ); | ||
| 65 | } | ||
| 66 | my( $buff, $nread ); | ||
| 67 | while( $nread = $io->read( $buff, 8192 ) ) | ||
| 68 | { | ||
| 69 | $$str .= $buff; | ||
| 70 | } | ||
| 71 | if( defined( $pos ) ) | ||
| 72 | { | ||
| 73 | $self->seek( $pos, 0 ); | ||
| 74 | } | ||
| 75 | return( $str ); | ||
| 76 | } | ||
| 77 | |||
| 78 | # sub binmode { return( shift->_set_get_boolean( 'binmode', @_ ) ); } | ||
| 79 | |||
| 80 | sub data { return( shift->as_string( @_ ) ); } | ||
| 81 | |||
| 82 | sub dup { return( shift->clone( @_ ) ); } | ||
| 83 | |||
| 84 | # sub open { return; } | ||
| 85 | |||
| 86 | sub path { return; } | ||
| 87 | |||
| 88 | sub print | ||
| 89 | { | ||
| 90 | my $self = shift( @_ ); | ||
| 91 | my $fh = shift( @_ ); | ||
| 92 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 93 | my $nread; | ||
| 94 | |||
| 95 | # Get output filehandle, and ensure that it's a printable object | ||
| 96 | return( $self->error( "Filehandle provided ($fh) is not a proper filehandle and its not a HTTP::Promise::IO object." ) ) if( !$self->_is_glob( $fh ) && !$self->_is_a( $fh => 'HTTP::Promise::IO' ) ); | ||
| 97 | |||
| 98 | my $params = {}; | ||
| 99 | $params->{binmode} = $opts->{binmode} if( exists( $opts->{binmode} ) && $opts->{binmode} ); | ||
| 100 | # Write it | ||
| 101 | my $buff = ''; | ||
| 102 | my $io = $self->open( 'r', ( scalar( keys( %$params ) ) ? $params : () ) ) || return( $self->pass_error ); | ||
| 103 | while( $nread = $io->read( $buff, 8192 ) ) | ||
| 104 | { | ||
| 105 | print( $fh $buff ) || return( $self->error( "Unable to write to filehandle '$fh': $!" ) ); | ||
| 106 | } | ||
| 107 | $io->close; | ||
| 108 | return( defined( $nread ) ); | ||
| 109 | } | ||
| 110 | |||
| 111 | sub purge { return; } | ||
| 112 | |||
| 113 | # NOTE: sub FREEZE is inherited | ||
| 114 | |||
| 115 | sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); } | ||
| 116 | |||
| 117 | sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); } | ||
| 118 | |||
| 119 | # NOTE: sub THAW is inherited | ||
| 120 | |||
| 121 | |||
| 122 | # NOTE: HTTP::Promise::Body::File package | ||
| 123 | package HTTP::Promise::Body::File; | ||
| 124 | BEGIN | ||
| 125 | { | ||
| 126 | use strict; | ||
| 127 | use warnings; | ||
| 128 | use Module::Generic::File; | ||
| 129 | use parent -norequire, qw( HTTP::Promise::Body Module::Generic::File ); | ||
| 130 | use vars qw( $EXCEPTION_CLASS ); | ||
| 131 | our $EXCEPTION_CLASS = 'HTTP::Promise::Exception'; | ||
| 132 | }; | ||
| 133 | |||
| 134 | use strict; | ||
| 135 | use warnings; | ||
| 136 | |||
| 137 | sub new { return( shift->Module::Generic::File::new( @_ ) ); } | ||
| 138 | |||
| 139 | sub init | ||
| 140 | { | ||
| 141 | my $self = shift( @_ ); | ||
| 142 | $self->{_init_strict_use_sub} = 1; | ||
| 143 | $self->{_exception_class} = $EXCEPTION_CLASS; | ||
| 144 | $self->Module::Generic::File::init( @_ ) || return( $self->pass_error ); | ||
| 145 | return( $self ); | ||
| 146 | } | ||
| 147 | |||
| 148 | sub path { return( shift->filename( @_ ) ); } | ||
| 149 | |||
| 150 | sub purge { return( shift->unlink ); } | ||
| 151 | |||
| 152 | sub FREEZE { CORE::return( CORE::shift->Module::Generic::File::FREEZE( @_ ) ); } | ||
| 153 | |||
| 154 | sub STORABLE_freeze { CORE::return( CORE::shift->Module::Generic::File::STORABLE_freeze( @_ ) ); } | ||
| 155 | |||
| 156 | # NOTE: sub STORABLE_thaw is inherited | ||
| 157 | |||
| 158 | # NOTE: sub THAW is inherited | ||
| 159 | |||
| 160 | |||
| 161 | # NOTE: HTTP::Promise::Body::Scalar package | ||
| 162 | package HTTP::Promise::Body::Scalar; | ||
| 163 | BEGIN | ||
| 164 | { | ||
| 165 | use strict; | ||
| 166 | use warnings; | ||
| 167 | use Module::Generic::Scalar; | ||
| 168 | use parent -norequire, qw( HTTP::Promise::Body Module::Generic::Scalar ); | ||
| 169 | use vars qw( $EXCEPTION_CLASS ); | ||
| 170 | use overload ( | ||
| 171 | '""' => sub{ $_[0] }, | ||
| 172 | bool => sub{1}, | ||
| 173 | fallback => 1, | ||
| 174 | ); | ||
| 175 | our $EXCEPTION_CLASS = 'HTTP::Promise::Exception'; | ||
| 176 | }; | ||
| 177 | |||
| 178 | use strict; | ||
| 179 | use warnings; | ||
| 180 | |||
| 181 | # sub new { return( shift->Module::Generic::Scalar::new( @_ ) ); } | ||
| 182 | sub new | ||
| 183 | { | ||
| 184 | my $this = shift( @_ ); | ||
| 185 | my $new = $this->Module::Generic::Scalar::new( @_ ); | ||
| 186 | return( $this->pass_error( $this->Module::Generic::Scalar::error ) ) if( !defined( $new ) ); | ||
| 187 | return( $new ); | ||
| 188 | } | ||
| 189 | |||
| 190 | sub as_string { return( @_ > 1 ? shift->SUPER::as_string( @_ ) : $_[0]->new_scalar( $_[0] ) ); } | ||
| 191 | |||
| 192 | sub checksum_md5 | ||
| 193 | { | ||
| 194 | my $self = shift( @_ ); | ||
| 195 | $self->_load_class( 'Crypt::Digest::MD5' ) || return( $self->pass_error ); | ||
| 196 | return( Crypt::Digest::MD5::md5_hex( $$self ) ); | ||
| 197 | } | ||
| 198 | |||
| 199 | sub error { return( shift->Module::Generic::Scalar::error( @_ ) ); } | ||
| 200 | |||
| 201 | sub pass_error { return( shift->Module::Generic::Scalar::pass_error( @_ ) ); } | ||
| 202 | |||
| 203 | sub purge { return( shift->Module::Generic::Scalar::reset( @_ ) ); } | ||
| 204 | |||
| 205 | sub set { return( shift->Module::Generic::Scalar::set( @_ ) ); } | ||
| 206 | |||
| 207 | sub FREEZE { return( shift->Module::Generic::Scalar::FREEZE( @_ ) ); } | ||
| 208 | |||
| 209 | # NOTE: sub STORABLE_freeze is inherited | ||
| 210 | |||
| 211 | # NOTE: sub STORABLE_thaw is inherited | ||
| 212 | |||
| 213 | sub THAW { return( shift->Module::Generic::Scalar::THAW( @_ ) ); } | ||
| 214 | |||
| 215 | |||
| 216 | # NOTE: HTTP::Promise::Body::InCore package | ||
| 217 | package HTTP::Promise::Body::InCore; | ||
| 218 | BEGIN | ||
| 219 | { | ||
| 220 | use strict; | ||
| 221 | use warnings; | ||
| 222 | use parent -norequire, qw( HTTP::Promise::Body::Scalar ); | ||
| 223 | use vars qw( $EXCEPTION_CLASS ); | ||
| 224 | our $EXCEPTION_CLASS = 'HTTP::Promise::Exception'; | ||
| 225 | }; | ||
| 226 | |||
| 227 | use strict; | ||
| 228 | use warnings; | ||
| 229 | |||
| 230 | sub init | ||
| 231 | { | ||
| 232 | my $self = shift( @_ ); | ||
| 233 | my $data = shift( @_ ); | ||
| 234 | # nothing | ||
| 235 | if( !defined( $data ) || | ||
| 236 | # simple scalar or a scalar object | ||
| 237 | ( !ref( $data ) || ( $self->_is_scalar( $data ) && overload::Method( $data => '""' ) ) ) || | ||
| 238 | # or a scalar reference | ||
| 239 | ref( $data ) eq 'SCALAR' ) | ||
| 240 | { | ||
| 241 | # pass through | ||
| 242 | } | ||
| 243 | elsif( $self->_is_array( $data ) ) | ||
| 244 | { | ||
| 245 | $data = join( '', @$data ); | ||
| 246 | } | ||
| 247 | else | ||
| 248 | { | ||
| 249 | return( $self->error( "Data of type '", ref( $data ), "' is unsupported." ) ); | ||
| 250 | } | ||
| 251 | $self->SUPER::init( $data, @_ ) || return( $self->pass_error ); | ||
| 252 | return( $self ); | ||
| 253 | } | ||
| 254 | |||
| 255 | 1; | ||
| 256 | # NOTE: POD | ||
| 257 | __END__ | ||
| 258 | |||
| 259 | =encoding utf-8 | ||
| 260 | |||
| 261 | =head1 NAME | ||
| 262 | |||
| 263 | HTTP::Promise::Body - HTTP Message Body Class | ||
| 264 | |||
| 265 | =head1 SYNOPSIS | ||
| 266 | |||
| 267 | use HTTP::Promise::Body; | ||
| 268 | my $body = HTTP::Promise::Body->new || | ||
| 269 | die( HTTP::Promise::Body->error, "\n" ); | ||
| 270 | |||
| 271 | =head1 VERSION | ||
| 272 | |||
| 273 | v0.1.0 | ||
| 274 | |||
| 275 | =head1 DESCRIPTION | ||
| 276 | |||
| 277 | This class represents an entity body. | ||
| 278 | |||
| 279 | Here is how it fits in overall relation with other classes. | ||
| 280 | |||
| 281 | +-------------------------+ +--------------------------+ | ||
| 282 | | | | | | ||
| 283 | | HTTP::Promise::Request | | HTTP::Promise::Response | | ||
| 284 | | | | | | ||
| 285 | +------------|------------+ +-------------|------------+ | ||
| 286 | | | | ||
| 287 | | | | ||
| 288 | | | | ||
| 289 | | +------------------------+ | | ||
| 290 | | | | | | ||
| 291 | +--- HTTP::Promise::Message |---+ | ||
| 292 | | | | ||
| 293 | +------------|-----------+ | ||
| 294 | | | ||
| 295 | | | ||
| 296 | +------------|-----------+ | ||
| 297 | | | | ||
| 298 | | HTTP::Promise::Entity | | ||
| 299 | | | | ||
| 300 | +------------|-----------+ | ||
| 301 | | | ||
| 302 | | | ||
| 303 | +------------|-----------+ | ||
| 304 | | | | ||
| 305 | | HTTP::Promise::Body | | ||
| 306 | | | | ||
| 307 | +------------------------+ | ||
| 308 | |||
| 309 | =head1 METHODS | ||
| 310 | |||
| 311 | =head2 as_lines | ||
| 312 | |||
| 313 | Returns a new L<array object|Module::Generic::Array> containing the body lines. | ||
| 314 | |||
| 315 | =head2 as_string | ||
| 316 | |||
| 317 | Returns the body data as a L<scalar object|Module::Generic::Scalar>. | ||
| 318 | |||
| 319 | Be mindful about the size of the body before you load it all in memory. You can get the size of the body with C<< $body->length >> | ||
| 320 | |||
| 321 | =head2 data | ||
| 322 | |||
| 323 | This is just an alias for L</as_string> | ||
| 324 | |||
| 325 | =head2 dup | ||
| 326 | |||
| 327 | This is an alias for L<Module::Generic/clone>, which is inherited by this class. | ||
| 328 | |||
| 329 | =head2 path | ||
| 330 | |||
| 331 | This is a no-op and is superseded by inheriting classes. | ||
| 332 | |||
| 333 | =head2 print | ||
| 334 | |||
| 335 | Provided with a filehandle, or an L<HTTP::Promise::IO> object and an hash or hash reference of options and this will print the body data to and returns true if it was successful, or sets an L<error|Module::Generic/error> and returns C<undef> | ||
| 336 | |||
| 337 | =head2 purge | ||
| 338 | |||
| 339 | This is a no-op and is superseded by inheriting classes. | ||
| 340 | |||
| 341 | =head1 THREAD-SAFETY | ||
| 342 | |||
| 343 | L<HTTP::Promise::Body> is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries. | ||
| 344 | |||
| 345 | =head1 AUTHOR | ||
| 346 | |||
| 347 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 348 | |||
| 349 | =head1 SEE ALSO | ||
| 350 | |||
| 351 | 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> | ||
| 352 | |||
| 353 | L<Module::Generic::File>, L<Module::Generic::Scalar>, L<Module::Generic::File::IO>, L<Module::Generic::Scalar::IO> | ||
| 354 | |||
| 355 | L<PerlIO::scalar> | ||
| 356 | |||
| 357 | =head1 COPYRIGHT & LICENSE | ||
| 358 | |||
| 359 | Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 360 | |||
| 361 | All rights reserved. | ||
| 362 | |||
| 363 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 364 | |||
| 365 | =cut |