lib/HTTP/Promise/Stream/Base64.pm

filter: Statementstmt 84.0% 74 / 88 Branchbran 57.1% 16 / 28 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 77.7% 14 / 18 Podpod 100% 6 / 6 totaltotal 74.6% 112 / 150 CC 17 SCAR 14.8
CC
17
Cov
75%
encode
8.2
decode
6.3
is_decoder_installed
6.0
CRAP 4.4
« blib/lib/HTTP/Promise/Headers/CacheControl.pm ↑ Summary lib/HTTP/Promise/Headers/ServerTiming.pm »
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 6 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶ warnings::register_categories( 'HTTP::Promise' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶ use HTTP::Promise::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶ use parent -norequire, qw( HTTP::Promise::Stream::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶ use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $Base64Error );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶ use Crypt::Misc ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 use constant {
25 6 ENCODE_BUFFER_SIZE => 300,
26 DECODE_BUFFER_SIZE => ( 32 * 1024 ),
27 6 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
28 6 our @EXPORT_OK = qw( decode_b64 encode_b64 );
29 6 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
30 6 our $VERSION = 'v0.2.0';
31 };
32  
33 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 sub decode
37 {
38 5 ▶ my $self = shift( @_ );
Subroutine decode: called CC=6 SCAR=18.4
Pod documented
39 5 my $from = shift( @_ );
40 5 my $to = shift( @_ );
41 5 my $opts = $self->_get_args_as_hash( @_ );
42 5 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
43 5 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
44 5 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 5 5
Condition33%1/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
005
MC/DC0%0/2
defined $from_fh and defined $to_fh
defined $from_fh defined $to_fh
Truth table33%1/3
A and B
A= defined $from_fhB= defined $to_fh
ABresult
0X0
100
111
45 5 my( $n, $buff );
46
47 5 ▶ while( $n = $reader->( $buff, DECODE_BUFFER_SIZE ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($n = &$reader($buff, 32768)) 5 5 10
48 {
49 5 my $decoded = Crypt::Misc::decode_b64( $buff );
50 5 my $rv = $writer->( $decoded );
51 5 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 5 5
52 }
53 5 ▶ return( $self->pass_error ) if( !defined( $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 5 5
54 5 return( $self );
55 }
56  
57 sub decode_b64
58 {
59 4 ▶ my $s = __PACKAGE__->new;
Subroutine decode_b64: called CC=2 SCAR=7.6
Pod documented
60 4 my $rv = $s->decode( @_ );
61 4 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 4 4
62 {
63 0 $Base64Error = $s->error;
64 0 return( $s->pass_error );
65 }
66 else
67 {
68 4 undef( $Base64Error );
69 4 return( $rv );
70 }
71 }
72  
73 sub encode
74 {
75 6 ▶ my $self = shift( @_ );
Subroutine encode: called CC=8 SCAR=21.1
Pod documented
76 6 my $from = shift( @_ );
77 6 my $to = shift( @_ );
78 6 my $opts = $self->_get_args_as_hash( @_ );
79 6 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
80 6 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
81 6 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 6 6
Condition33%1/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
006
MC/DC0%0/2
defined $from_fh and defined $to_fh
defined $from_fh defined $to_fh
Truth table33%1/3
A and B
A= defined $from_fhB= defined $to_fh
ABresult
0X0
100
111
82 6 ▶ my $eol = exists( $opts->{eol} ) ? $opts->{eol} : $/;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'eol'} ? : 1 5 6
83 6 my $has_eol = length( $eol );
84 6 my( $n, $buff );
85
86 6 ▶ while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($n = &$reader($buff, 300)) 306 6 312
87 {
88 306 my $encoded = Crypt::Misc::encode_b64( $buff );
89 306 ▶ if( $has_eol )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_eol) 305 1 306
90 {
91 305 $encoded =~ s/(.{76})/$1$eol/g;
92 }
93 306 my $rv = $writer->( $encoded );
94 306 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 306 306
95 }
96 6 ▶ return( $self->pass_error ) if( !defined( $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 6 6
97 6 return( $self );
98 }
99  
100 sub encode_b64
101 {
102 5 ▶ my $s = __PACKAGE__->new;
Subroutine encode_b64: called CC=2 SCAR=7.6
Pod documented
103 5 my $rv = $s->encode( @_ );
104 5 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 5 5
105 {
106 0 $Base64Error = $s->error;
107 0 return( $s->pass_error );
108 }
109 else
110 {
111 5 undef( $Base64Error );
112 5 return( $rv );
113 }
114 }
115  
116 sub is_decoder_installed
117 {
118 0 ▶ local $@;
Subroutine is_decoder_installed: not called CC=2 SCAR=17.9
Pod documented
119 eval
120 0 {
121 0 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 require Crypt::Misc;
123 };
124 0 ▶ return( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
125 }
126  
127 sub is_encoder_installed
128 {
129 0 ▶ local $@;
Subroutine is_encoder_installed: not called CC=2 SCAR=17.9
Pod documented
130 eval
131 0 {
132 0 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 require Crypt::Misc;
134 };
135 0 ▶ return( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
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 6 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
« blib/lib/HTTP/Promise/Headers/CacheControl.pm ↑ Summary lib/HTTP/Promise/Headers/ServerTiming.pm »