blib/lib/HTTP/Promise/Stream/Base64.pm untested

filter: Statementstmt 0.0% 0 / 92 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 6 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 147 CC 13 SCAR 26.0
CC
13
Cov
0%
encode
42.0
decode
20.0
decode_b64
6.0
CRAP 13.5
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Stream/Base64.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/04/28
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::Base64;
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 $Base64Error );
23 use Crypt::Misc ();
24 use constant {
25 ENCODE_BUFFER_SIZE => 300,
26 DECODE_BUFFER_SIZE => ( 32 * 1024 ),
27 };
28 our @EXPORT_OK = qw( decode_b64 encode_b64 );
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
47 while( $n = $reader->( $buff, DECODE_BUFFER_SIZE ) )
48 {
49 my $decoded = Crypt::Misc::decode_b64( $buff );
50 my $rv = $writer->( $decoded );
51 return( $self->pass_error ) if( !defined( $rv ) );
52 }
53 return( $self->pass_error ) if( !defined( $n ) );
54 return( $self );
55 }
56  
57 sub decode_b64
58 {
59 my $s = __PACKAGE__->new;
60 my $rv = $s->decode( @_ );
61 if( !defined( $rv ) )
62 {
63 $Base64Error = $s->error;
64 return( $s->pass_error );
65 }
66 else
67 {
68 undef( $Base64Error );
69 return( $rv );
70 }
71 }
72  
73 sub encode
74 {
75 my $self = shift( @_ );
76 my $from = shift( @_ );
77 my $to = shift( @_ );
78 my $opts = $self->_get_args_as_hash( @_ );
79 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
80 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
81 return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
82 my $eol = exists( $opts->{eol} ) ? $opts->{eol} : $/;
83 my $has_eol = length( $eol );
84 my( $n, $buff );
85
86 while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) )
87 {
88 my $encoded = Crypt::Misc::encode_b64( $buff );
89 if( $has_eol )
90 {
91 $encoded =~ s/(.{76})/$1$eol/g;
92 }
93 my $rv = $writer->( $encoded );
94 return( $self->pass_error ) if( !defined( $rv ) );
95 }
96 return( $self->pass_error ) if( !defined( $n ) );
97 return( $self );
98 }
99  
100 sub encode_b64
101 {
102 my $s = __PACKAGE__->new;
103 my $rv = $s->encode( @_ );
104 if( !defined( $rv ) )
105 {
106 $Base64Error = $s->error;
107 return( $s->pass_error );
108 }
109 else
110 {
111 undef( $Base64Error );
112 return( $rv );
113 }
114 }
115  
116 sub is_decoder_installed
117 {
118 local $@;
119 eval
120 {
121 local $SIG{__DIE__} = sub{};
122 require Crypt::Misc;
123 };
124 return( $@ ? 0 : 1 );
125 }
126  
127 sub is_encoder_installed
128 {
129 local $@;
130 eval
131 {
132 local $SIG{__DIE__} = sub{};
133 require Crypt::Misc;
134 };
135 return( $@ ? 0 : 1 );
136 }
137  
138 # NOTE: sub FREEZE is inherited
139  
140 # NOTE: sub STORABLE_freeze is inherited
141  
142 # NOTE: sub STORABLE_thaw is inherited
143  
144 # NOTE: sub THAW is inherited
145  
146 1;
147 # NOTE: POD
148 __END__
149  
150 =encoding utf-8
151  
152 =head1 NAME
153  
154 HTTP::Promise::Stream::Base64 - Stream Encoder for Base64 Encoding
155  
156 =head1 SYNOPSIS
157  
158 use HTTP::Promise::Stream::Base64;
159 my $s = HTTP::Promise::Stream::Base64->new ||
160 die( HTTP::Promise::Stream::Base64->error, "\n" );
161 $s->encode( $input => $output, eol => "\n" ) ||
162 die( $s->error );
163 $s->decode( $input => $output ) || die( $s->error );
164 HTTP::Promise::Stream::Base64::encode_b64( $input => $output, eol => "\n" ) ||
165 die( $HTTP::Promise::Stream::Base64::Base64Error );
166 HTTP::Promise::Stream::Base64::decode_b64( $input => $output, eol => "\n" ) ||
167 die( $HTTP::Promise::Stream::Base64::Base64Error );
168  
169 =head1 VERSION
170  
171 v0.2.0
172  
173 =head1 DESCRIPTION
174  
175 This implements an encoding and decoding mechanism for base64 encoding using either of the following on input and output:
176  
177 =over 4
178  
179 =item C<filepath>
180  
181 If the parameter is neither a scalar reference nor a file handle, it will be assumed to be a file path.
182  
183 =item C<file handle>
184  
185 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.
186  
187 Alternatively, those methods can die and those exceptions wil be caught.
188  
189 =item C<scalar reference>
190  
191 This can be a simple scalar reference, or an object scalar reference.
192  
193 =back
194  
195 =head1 CONSTRUCTOR
196  
197 =head2 new
198  
199 Creates a new L<HTTP::Promise::Stream::Base64> object and returns it.
200  
201 =head1 METHODS
202  
203 =head2 decode
204  
205 This takes 2 arguments: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
206  
207 It will decode the base64 encoded data and write the result into the output.
208  
209 It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error.
210  
211 =head2 encode
212  
213 This takes 2 arguments: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
214  
215 It will encode the data into base64 encoded data and write the result into the output.
216  
217 If the option I<eol> (standing for "End of line") is provided, it will be used to break down the base64 encoded into lines of 76 characters ending with the I<eol>. If I<eol> is not provided, it will default to C<$/>, which usually is C<\n>. If you want base64 data that are not borken down into 76 characters line, then pass an empty I<eol> parameter, such as:
218  
219 my $s = HTTP::Promise::Stream::Base64->new;
220 $s->encode( $from => $to, eol => undef ); # or eol => ''
221  
222 It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error.
223  
224 =head1 CLASS FUNCTIONS
225  
226 The following class functions are available and can also be exported, such as:
227  
228 use HTTP::Promise::Stream::Base64 qw( decode_b64 encode_b64 );
229  
230 =head2 decode_b64
231  
232 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.
233  
234 It will decode the base64 encoded data and write the result into the output.
235  
236 It returns true upon success, and upon error, it will set the error in the global variable C<$Base64Error> and return C<undef>
237  
238 my $decoded = HTTP::Promise::Stream::Base64::decode_b64( $encoded );
239 die( "Something went wrong: $HTTP::Promise::Stream::Base64::Base64Error\n" if( !defined( $decoded ) );
240 print( "Decoded data is: $decoded\n" );
241  
242 =head2 encode_b64
243  
244 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.
245  
246 It will encode the data into base64 encoded data and write the result into the output.
247  
248 It returns true upon success, and upon error, it will set the error in the global variable C<$Base64Error> and return C<undef>
249  
250 my $encoded = HTTP::Promise::Stream::Base64::encode_b64( $data );
251 die( "Something went wrong: $HTTP::Promise::Stream::Base64::Base64Error\n" if( !defined( $encoded ) );
252 print( "Encoded data is: $encoded\n" );
253  
254 =head2 is_decoder_installed
255  
256 Returns true if the module L<Crypt::Misc> is installed, false otherwise.
257  
258 =head2 is_encoder_installed
259  
260 Returns true if the module L<Crypt::Misc> is installed, false otherwise.
261  
262 =head1 THREAD-SAFETY
263  
264 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
265  
266 =head1 AUTHOR
267  
268 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
269  
270 =head1 SEE ALSO
271  
272 L<W3C|http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2>
273  
274 L<caniuse|https://caniuse.com/atob-btoa>
275  
276 L<PerlIO::via::Base64>
277  
278 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>
279  
280 =head1 COPYRIGHT & LICENSE
281  
282 Copyright(c) 2022 DEGUEST Pte. Ltd.
283  
284 All rights reserved.
285  
286 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
287  
288 =cut