| 1 | ##---------------------------------------------------------------------------- | ||
| 2 | ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Stream/Brotli.pm | ||
| 3 | ## Version v0.2.0 | ||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||
| 6 | ## Created 2022/05/04 | ||
| 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::Stream::Brotli; | ||
| 15 | BEGIN | ||
| 16 | { | ||
| 17 | use strict; | ||
| 18 | use warnings; | ||
| 19 | warnings::register_categories( 'HTTP::Promise' ); | ||
| 20 | use HTTP::Promise::Stream; | ||
| 21 | use parent -norequire, qw( HTTP::Promise::Stream::Generic ); | ||
| 22 | use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $BrotliError ); | ||
| 23 | # use Nice::Try; | ||
| 24 | use constant { | ||
| 25 | ENCODE_BUFFER_SIZE => ( 32 * 1024 ), | ||
| 26 | DECODE_BUFFER_SIZE => ( 32 * 1024 ), | ||
| 27 | }; | ||
| 28 | our @EXPORT_OK = qw( decode_bro encode_bro ); | ||
| 29 | our $EXCEPTION_CLASS = 'HTTP::Promise::Exception'; | ||
| 30 | our $VERSION = 'v0.2.0'; | ||
| 31 | }; | ||
| 32 | |||
| 33 | use strict; | ||
| 34 | use warnings; | ||
| 35 | |||
| 36 | sub decode | ||
| 37 | { | ||
| 38 | my $self = shift( @_ ); | ||
| 39 | my $from = shift( @_ ); | ||
| 40 | my $to = shift( @_ ); | ||
| 41 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 42 | my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from ); | ||
| 43 | my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 ); | ||
| 44 | return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) ); | ||
| 45 | my( $n, $buff ); | ||
| 46 | $self->_load_class( 'IO::Uncompress::Brotli', { no_import => 1 } ) || return( $self->pass_error ); | ||
| 47 | my $c = IO::Uncompress::Brotli->create; | ||
| 48 | |||
| 49 | while( $n = $reader->( $buff, DECODE_BUFFER_SIZE ) ) | ||
| 50 | { | ||
| 51 | my $decoded = $c->decompress( $buff ); | ||
| 52 | # try-catch | ||
| 53 | local $@; | ||
| 54 | my $rv = eval | ||
| 55 | { | ||
| 56 | $writer->( $decoded ); | ||
| 57 | }; | ||
| 58 | if( $@ ) | ||
| 59 | { | ||
| 60 | return( $self->error( "Error decompressing with Brotli: $@" ) ); | ||
| 61 | } | ||
| 62 | return( $self->pass_error ) if( !defined( $rv ) ); | ||
| 63 | } | ||
| 64 | return( $self->pass_error ) if( !defined( $n ) ); | ||
| 65 | return( $self ); | ||
| 66 | } | ||
| 67 | |||
| 68 | sub decode_bro | ||
| 69 | { | ||
| 70 | my $s = __PACKAGE__->new; | ||
| 71 | my $rv = $s->decode( @_ ); | ||
| 72 | if( !defined( $rv ) ) | ||
| 73 | { | ||
| 74 | $BrotliError = $s->error; | ||
| 75 | return( $s->pass_error ); | ||
| 76 | } | ||
| 77 | else | ||
| 78 | { | ||
| 79 | undef( $BrotliError ); | ||
| 80 | return( $rv ); | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | sub encode | ||
| 85 | { | ||
| 86 | my $self = shift( @_ ); | ||
| 87 | my $from = shift( @_ ); | ||
| 88 | my $to = shift( @_ ); | ||
| 89 | my $opts = $self->_get_args_as_hash( @_ ); | ||
| 90 | my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from ); | ||
| 91 | my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 ); | ||
| 92 | return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) ); | ||
| 93 | my( $n, $buff ); | ||
| 94 | $self->_load_class( 'IO::Compress::Brotli', { no_import => 1 } ) || return( $self->pass_error ); | ||
| 95 | my $c = IO::Compress::Brotli->create; | ||
| 96 | |||
| 97 | while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) ) | ||
| 98 | { | ||
| 99 | my $encoded = $c->compress( $buff ); | ||
| 100 | # try-catch | ||
| 101 | local $@; | ||
| 102 | my $rv = eval | ||
| 103 | { | ||
| 104 | $writer->( $encoded ); | ||
| 105 | }; | ||
| 106 | if( $@ ) | ||
| 107 | { | ||
| 108 | return( $self->error( "Error compressing with Brotli: $@" ) ); | ||
| 109 | } | ||
| 110 | return( $self->pass_error ) if( !defined( $rv ) ); | ||
| 111 | } | ||
| 112 | $c->finish; | ||
| 113 | |||
| 114 | return( $self->pass_error ) if( !defined( $n ) ); | ||
| 115 | return( $self ); | ||
| 116 | } | ||
| 117 | |||
| 118 | sub encode_bro | ||
| 119 | { | ||
| 120 | my $s = __PACKAGE__->new; | ||
| 121 | my $rv = $s->encode( @_ ); | ||
| 122 | if( !defined( $rv ) ) | ||
| 123 | { | ||
| 124 | $BrotliError = $s->error; | ||
| 125 | return( $s->pass_error ); | ||
| 126 | } | ||
| 127 | else | ||
| 128 | { | ||
| 129 | undef( $BrotliError ); | ||
| 130 | return( $rv ); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | sub is_decoder_installed | ||
| 135 | { | ||
| 136 | local $@; | ||
| 137 | eval | ||
| 138 | { | ||
| 139 | local $SIG{__DIE__} = sub{}; | ||
| 140 | require IO::Uncompress::Brotli; | ||
| 141 | }; | ||
| 142 | # eval( 'use IO::Uncompress::Brotli ();' ); | ||
| 143 | return( $@ ? 0 : 1 ); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub is_encoder_installed | ||
| 147 | { | ||
| 148 | local $@; | ||
| 149 | # eval( 'use IO::Compress::Brotli ();' ); | ||
| 150 | eval | ||
| 151 | { | ||
| 152 | local $SIG{__DIE__} = sub{}; | ||
| 153 | require IO::Uncompress::Brotli; | ||
| 154 | }; | ||
| 155 | return( $@ ? 0 : 1 ); | ||
| 156 | } | ||
| 157 | |||
| 158 | # NOTE: sub FREEZE is inherited | ||
| 159 | |||
| 160 | # NOTE: sub STORABLE_freeze is inherited | ||
| 161 | |||
| 162 | # NOTE: sub STORABLE_thaw is inherited | ||
| 163 | |||
| 164 | # NOTE: sub THAW is inherited | ||
| 165 | |||
| 166 | 1; | ||
| 167 | # NOTE: POD | ||
| 168 | __END__ | ||
| 169 | |||
| 170 | =encoding utf-8 | ||
| 171 | |||
| 172 | =head1 NAME | ||
| 173 | |||
| 174 | HTTP::Promise::Stream::Brotli - Stream Encoder for Brotli Encoding | ||
| 175 | |||
| 176 | =head1 SYNOPSIS | ||
| 177 | |||
| 178 | use HTTP::Promise::Stream::Brotli; | ||
| 179 | my $s = HTTP::Promise::Stream::Brotli->new || | ||
| 180 | die( HTTP::Promise::Stream::Brotli->error, "\n" ); | ||
| 181 | $s->encode( $input => $output ) || | ||
| 182 | die( $s->error ); | ||
| 183 | $s->decode( $input => $output ) || die( $s->error ); | ||
| 184 | HTTP::Promise::Stream::Brotli::encode_bro( $input => $output ) || | ||
| 185 | die( $HTTP::Promise::Stream::Brotli::BrotliError ); | ||
| 186 | HTTP::Promise::Stream::Brotli::decode_bro( $input => $output ) || | ||
| 187 | die( $HTTP::Promise::Stream::Brotli::BrotliError ); | ||
| 188 | |||
| 189 | =head1 VERSION | ||
| 190 | |||
| 191 | v0.2.0 | ||
| 192 | |||
| 193 | =head1 DESCRIPTION | ||
| 194 | |||
| 195 | This implements an encoding and decoding mechanism for Brotli compression using either of the following on input and output: | ||
| 196 | |||
| 197 | =over 4 | ||
| 198 | |||
| 199 | =item C<filepath> | ||
| 200 | |||
| 201 | If the parameter is neither a scalar reference nor a file handle, it will be assumed to be a file path. | ||
| 202 | |||
| 203 | =item C<file handle> | ||
| 204 | |||
| 205 | This can be a native file handle, or an object oriented one as long as it implements the C<print> or C<write>, and C<read> methods. The C<read> method is expected to return the number of bytes read or C<undef> upon error. The C<print> and C<write> methods are expected to simply return true upon success and C<undef> upon error. | ||
| 206 | |||
| 207 | Alternatively, those methods can die and those exceptions wil be caught. | ||
| 208 | |||
| 209 | =item C<scalar reference> | ||
| 210 | |||
| 211 | This can be a simple scalar reference, or an object scalar reference. | ||
| 212 | |||
| 213 | =back | ||
| 214 | |||
| 215 | This module requires L<IO::Compress::Brotli> and L<IO::Uncompress::Brotli> to be installed or it will return an error. | ||
| 216 | |||
| 217 | Brotli L<is described|https://www.brotli.org/> as "Brotli is a generic-purpose lossless compression algorithm that compresses data using a combination of a modern variant of the LZ77 algorithm, Huffman coding and 2nd order context modeling" that is "similar in speed with deflate" and "supported by L<most web browsers|http://caniuse.com/#search=brotli>, major web servers, and some CDNs". The specification of its Compressed Data Format is defined in L<rfc7932|https://tools.ietf.org/html/rfc7932>. | ||
| 218 | |||
| 219 | According to L<Mozilla|https://developer.mozilla.org/en-US/docs/Glossary/brotli_compression>, "Brotli provides better compression ratios than gzip and deflate speeds are comparable, but brotli compressing is a slower process than Gzip" | ||
| 220 | |||
| 221 | =head1 CONSTRUCTOR | ||
| 222 | |||
| 223 | =head2 new | ||
| 224 | |||
| 225 | Creates a new L<HTTP::Promise::Stream::Brotli> object and returns it. | ||
| 226 | |||
| 227 | =head1 METHODS | ||
| 228 | |||
| 229 | =head2 decode | ||
| 230 | |||
| 231 | This takes 2 arguments: an input and an output. Each one can be either a file path, a file handle, or a scalar reference. | ||
| 232 | |||
| 233 | It will decode the Brotli encoded data and write the result into the output. | ||
| 234 | |||
| 235 | It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error. | ||
| 236 | |||
| 237 | =head2 encode | ||
| 238 | |||
| 239 | This takes 2 arguments: an input and an output. Each one can be either a file path, a file handle, or a scalar reference. | ||
| 240 | |||
| 241 | It will encode the data into Brotli encoded data and write the result into the output. | ||
| 242 | |||
| 243 | It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error. | ||
| 244 | |||
| 245 | =head1 CLASS FUNCTIONS | ||
| 246 | |||
| 247 | The following class functions are available and can also be exported, such as: | ||
| 248 | |||
| 249 | use HTTP::Promise::Stream::Brotli qw( decode_bro encode_bro ); | ||
| 250 | |||
| 251 | =head2 decode_bro | ||
| 252 | |||
| 253 | This takes the same 2 arguments used in L</decode>: an input and an output. Each one can be either a file path, a file handle, or a scalar reference. | ||
| 254 | |||
| 255 | It will decode the Brotli encoded data and write the result into the output. | ||
| 256 | |||
| 257 | It returns true upon success, and upon error, it will set the error in the global variable C<$BrotliError> and return C<undef> | ||
| 258 | |||
| 259 | my $decoded = HTTP::Promise::Stream::Brotli::decode_bro( $encoded ); | ||
| 260 | die( "Something went wrong: $HTTP::Promise::Stream::Brotli::BrotliError\n" if( !defined( $decoded ) ); | ||
| 261 | print( "Decoded data is: $decoded\n" ); | ||
| 262 | |||
| 263 | =head2 encode_bro | ||
| 264 | |||
| 265 | This takes the same 2 arguments used in L</encode>: an input and an output. Each one can be either a file path, a file handle, or a scalar reference. | ||
| 266 | |||
| 267 | It will encode the data into Brotli encoded data and write the result into the output. | ||
| 268 | |||
| 269 | It returns true upon success, and upon error, it will set the error in the global variable C<$BrotliError> and return C<undef> | ||
| 270 | |||
| 271 | my $encoded = HTTP::Promise::Stream::Brotli::encode_bro( $data ); | ||
| 272 | die( "Something went wrong: $HTTP::Promise::Stream::Brotli::BrotliError\n" if( !defined( $encoded ) ); | ||
| 273 | print( "Encoded data is: $encoded\n" ); | ||
| 274 | |||
| 275 | =head2 is_decoder_installed | ||
| 276 | |||
| 277 | Returns true if the module L<IO::Uncompress::Brotli> is installed, false otherwise. | ||
| 278 | |||
| 279 | =head2 is_encoder_installed | ||
| 280 | |||
| 281 | Returns true if the module L<IO::Compress::Brotli> is installed, false otherwise. | ||
| 282 | |||
| 283 | =head1 THREAD-SAFETY | ||
| 284 | |||
| 285 | This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries. | ||
| 286 | |||
| 287 | =head1 AUTHOR | ||
| 288 | |||
| 289 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||
| 290 | |||
| 291 | =head1 SEE ALSO | ||
| 292 | |||
| 293 | L<IO::Compress::Brotli>, L<IO::Uncompress::Brotli> | ||
| 294 | |||
| 295 | L<Brotli web page|https://www.brotli.org/>, L<Brotli Github page|https://github.com/google/brotli> | ||
| 296 | |||
| 297 | L<rfc7932|https://tools.ietf.org/html/rfc7932> | ||
| 298 | |||
| 299 | L<Wikipedia page|https://en.wikipedia.org/wiki/Brotli> | ||
| 300 | |||
| 301 | L<caniuse|http://caniuse.com/#search=brotli> | ||
| 302 | |||
| 303 | 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> | ||
| 304 | |||
| 305 | =head1 COPYRIGHT & LICENSE | ||
| 306 | |||
| 307 | Copyright(c) 2022 DEGUEST Pte. Ltd. | ||
| 308 | |||
| 309 | All rights reserved. | ||
| 310 | |||
| 311 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||
| 312 | |||
| 313 | =cut |