lib/HTTP/Promise/Stream/UU.pm

filter: Statementstmt 82.3% 84 / 102 Branchbran 52.2% 23 / 44 Conditioncond 40.0% 6 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 73.6% 14 / 19 Podpod 100% 8 / 8 totaltotal 68.6% 136 / 198 CC 28 SCAR 21.4
CC
28
Cov
70%
encode
14.3
decode
13.7
decode_uu
6.0
CRAP 8.5
« lib/HTTP/Promise/MIME.pm ↑ Summary blib/lib/HTTP/Promise/Headers/Cookie.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Stream/UU.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/04/29
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::UU;
15 BEGIN
16 {
17 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶ warnings::register_categories( 'HTTP::Promise' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶ use HTTP::Promise::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ use parent -norequire, qw( HTTP::Promise::Stream::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶ use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $UUError );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 use constant {
24 3 DECODE_BUFFER_SIZE => 1024,
25 ENCODE_BUFFER_SIZE => 45,
26 3 ▶ };
Subroutine BEGIN: called CC=1 SCAR=0.0
27 3 our @EXPORT_OK = qw( decode_uu encode_uu );
28 3 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
29 3 our $VERSION = 'v0.2.0';
30 };
31  
32 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: not called CC=1 SCAR=0.0
34  
35 sub decode
36 {
37 1 ▶ my $self = shift( @_ );
Subroutine decode: called CC=12 SCAR=26.2
Pod documented
38 1 my $from = shift( @_ );
39 1 my $to = shift( @_ );
40 1 my $opts = $self->_get_args_as_hash( @_ );
41 1 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
42 1 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
43 1 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 1 1
Condition33%1/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
001
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
44 1 my( $n, $buff );
45 1 $buff = '';
46
47 my $getline = sub
48 {
49 8 ▶ if( length( $buff ) > 0 && $buff =~ s/^((?:[^\015\012]+)\015?\012)// )
Subroutine __ANON__: called CC=5 SCAR=17.5
Branch: 1 branch
BranchTrueFalseTotal
if (length $buff > 0 and $buff =~ s/^((?:[^\015\012]+)\015?\012)//) { } 7 1 8
Condition66%2/3
length $buff > 0 and $buff =~ s/^((?:[^\015\012]+)\015?\012)//
!ll&&!rl&&r
107
MC/DC50%1/2
length $buff > 0 and $buff =~ s/^((?:[^\015\012]+)\015?\012)//
length $buff > 0 $buff =~ s/^((?:[^\015\012]+)\015?\012)//
Truth table66%2/3
A and B
A= length $buff > 0B= $buff =~ s/^((?:[^\015\012]+)\015?\012)//
ABresult
0X0
100
111
50 {
51 7 return( $1 );
52 }
53 else
54 {
55 1 ▶ while( $n = $reader->( $buff, DECODE_BUFFER_SIZE, length( $buff ) ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($n = &$reader($buff, 1024, length $buff)) 1 0 1
56 {
57 1 ▶ if( $buff =~ s/^((?:[^\015\012]+)\015?\012)// )
Branch: 1 branch
BranchTrueFalseTotal
if ($buff =~ s/^((?:[^\015\012]+)\015?\012)//) { } 1 0 1
58 {
59 1 return( $1 );
60 }
61 else
62 {
63 0 next;
64 }
65 }
66 0 return;
67 }
68 1 };
69
70 1 my( $mode, $fname );
71 1 my $done;
72 1 local $_;
73 # Credit: brian d foy <https://metacpan.org/dist/PerlPowerTools/view/bin/uudecode>
74 1 ▶ READ: while( defined( $_ = $getline->() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = &$getline())) 1 0 1
75 {
76 1 ▶ next unless( ( $mode, $fname ) = /^begin[[:blank:]\h]+(\d+)[[:blank:]\h]+(\S+)/ );
Branch: 1 branch
BranchTrueFalseTotal
unless () = /^begin[[:blank:]\h]+(\d+)[[:blank:]\h]+(\S+)/ 0 1 1
77 1 $self->filename( $fname );
78 1 $self->mode( $mode );
79 1 $opts->{filename} = $fname;
80 1 $opts->{mode} = $mode;
81 1 $done = 0;
82 1 ▶ LINE: while( defined( $_ = $getline->() ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = &$getline())) 7 0 7
83 {
84 7 ▶ if( /^end$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (/^end$/) 1 6 7
85 {
86 1 $done = 1;
87 1 last READ;
88 }
89 6 ▶ next LINE if( /[a-z]/ );
Branch: 1 branch
BranchTrueFalseTotal
if /[a-z]/ 0 6 6
90 6 ▶ next LINE unless int( ( ( ( ord( $_ ) - 32 ) & 077 ) + 2 ) / 3 ) == int( length( $_ ) / 4 );
Branch: 1 branch
BranchTrueFalseTotal
unless int(((ord($_) - 32 & 63) + 2) / 3) == int length($_) / 4 0 6 6
91 6 my $rv = $writer->( unpack( 'u', $_ ) );
92 6 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 6 6
93 }
94 }
95 1 ▶ return( $self->error( "No UU encoded data found." ) ) if( !defined( $done ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $done 0 1 1
96 1 ▶ return( $self->error( "Missing end. input data stream may be truncated." ) ) if( !$done );
Branch: 1 branch
BranchTrueFalseTotal
unless $done 0 1 1
97 1 return( $self );
98 }
99  
100 sub decode_uu
101 {
102 0 ▶ my $s = __PACKAGE__->new;
Subroutine decode_uu: not called CC=2 SCAR=17.9
Pod documented
103 0 my $rv = $s->decode( @_ );
104 0 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 0 0
105 {
106 0 $UUError = $s->error;
107 0 return( $s->pass_error );
108 }
109 else
110 {
111 0 undef( $UUError );
112 0 return( $rv );
113 }
114 }
115  
116 sub encode
117 {
118 1 ▶ my $self = shift( @_ );
Subroutine encode: called CC=11 SCAR=26.6
Pod documented
119 1 my $from = shift( @_ );
120 1 my $to = shift( @_ );
121 1 my $opts = $self->_get_args_as_hash( @_ );
122 1 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
123 1 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
124 1 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 1 1
Condition33%1/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
001
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
125 1 ▶ my $fname = ( exists( $opts->{filename} ) && length( $opts->{filename} ) ) ? $opts->{filename} : 'unknown.bin';
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'filename'} && length $opts->{'filename'} ? : 1 0 1
Condition33%1/3
exists $opts->{"filename"} and length $opts->{"filename"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $opts->{"filename"} and length $opts->{"filename"}
exists $opts->{"filename"} length $opts->{"filename"}
Truth table33%1/3
A and B
A= exists $opts->{"filename"}B= length $opts->{"filename"}
ABresult
0X0
100
111
126 1 ▶ my $mode = ( exists( $opts->{mode} ) && length( $opts->{mode} ) ) ? $opts->{mode} : 0644;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'mode'} && length $opts->{'mode'} ? : 1 0 1
Condition33%1/3
exists $opts->{"mode"} and length $opts->{"mode"}
!ll&&!rl&&r
001
MC/DC0%0/2
exists $opts->{"mode"} and length $opts->{"mode"}
exists $opts->{"mode"} length $opts->{"mode"}
Truth table33%1/3
A and B
A= exists $opts->{"mode"}B= length $opts->{"mode"}
ABresult
0X0
100
111
127 1 my( $n, $buff );
128
129 1 my $rv = $writer->( sprintf( "begin %03o $fname\n", $mode ) );
130 1 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 1 1
131 1 ▶ while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($n = &$reader($buff, 45)) 5 1 6
132 {
133 5 $writer->( pack( 'u', $buff ) );
134 }
135 1 ▶ defined( $writer->( "`\n" ) ) or return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
defined &$writer("`\n") or return $self->pass_error 0 1 1
136 1 ▶ defined( $writer->( "end\n" ) ) or return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
defined &$writer("end\n") or return $self->pass_error 0 1 1
137 1 return( $self );
138 }
139  
140 sub encode_uu
141 {
142 0 ▶ my $s = __PACKAGE__->new;
Subroutine encode_uu: not called CC=2 SCAR=17.9
Pod documented
143 0 my $rv = $s->encode( @_ );
144 0 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 0 0
145 {
146 0 $UUError = $s->error;
147 0 return( $s->pass_error );
148 }
149 else
150 {
151 0 undef( $UUError );
152 0 return( $rv );
153 }
154 }
155  
156 1 ▶sub filename { return( shift->_set_get_scalar( 'filename', @_ ) ); }
Subroutine filename: called CC=1 SCAR=0.0
Pod documented
157  
158 0 ▶sub is_decoder_installed { return(1); }
Subroutine is_decoder_installed: not called CC=1 SCAR=6.9
Pod documented
159  
160 0 ▶sub is_encoder_installed { return(1); }
Subroutine is_encoder_installed: not called CC=1 SCAR=6.9
Pod documented
161  
162 1 ▶sub mode { return( shift->_set_get_number( 'mode', @_ ) ); }
Subroutine mode: called CC=1 SCAR=0.0
Pod documented
163  
164 # NOTE: sub FREEZE is inherited
165  
166 # NOTE: sub STORABLE_freeze is inherited
167  
168 # NOTE: sub STORABLE_thaw is inherited
169  
170 # NOTE: sub THAW is inherited
171  
172 3 1;
173 # NOTE: POD
174 __END__
175  
176 =encoding utf-8
177  
178 =head1 NAME
179  
180 HTTP::Promise::Stream::UU - Stream Encoder for UU Encoding
181  
182 =head1 SYNOPSIS
183  
184 use HTTP::Promise::Stream::UU;
185 my $s = HTTP::Promise::Stream::UU->new ||
186 die( HTTP::Promise::Stream::UU->error, "\n" );
187 $s->encode( $input => $output, eol => "\n" ) ||
188 die( $s->error );
189 $s->decode( $input => $output ) || die( $s->error );
190 HTTP::Promise::Stream::UU::encode_uu( $input => $output, eol => "\n" ) ||
191 die( $HTTP::Promise::Stream::UU::UUError );
192 HTTP::Promise::Stream::UU::decode_uu( $input => $output, eol => "\n" ) ||
193 die( $HTTP::Promise::Stream::UU::UUError );
194  
195 =head1 VERSION
196  
197 v0.2.0
198  
199 =head1 DESCRIPTION
200  
201 This implements an encoding and decoding mechanism for UU encoding using either of the following on input and output:
202  
203 =over 4
204  
205 =item C<filepath>
206  
207 If the parameter is neither a scalar reference nor a file handle, it will be assumed to be a file path.
208  
209 =item C<file handle>
210  
211 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.
212  
213 Alternatively, those methods can die and those exceptions wil be caught.
214  
215 =item C<scalar reference>
216  
217 This can be a simple scalar reference, or an object scalar reference.
218  
219 =back
220  
221 =head1 CONSTRUCTOR
222  
223 =head2 new
224  
225 Creates a new L<HTTP::Promise::Stream::UU> 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 UU 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 UU encoded data and write the result into the output.
242  
243 Possible options are:
244  
245 =over 4
246  
247 =item I<filename>
248  
249 The file name (not the file path) to be used for UU encoding.
250  
251 =item I<mode>
252  
253 The file octal permisions, like C<0644>. It defaults to C<0644> if nothing is provided.
254  
255 =back
256  
257 It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error.
258  
259 =head1 CLASS FUNCTIONS
260  
261 The following class functions are available and can also be exported, such as:
262  
263 use HTTP::Promise::Stream::Brotli qw( decode_uu encode_uu );
264  
265 =head2 decode_uu
266  
267 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.
268  
269 It will decode the UU encoded data and write the result into the output.
270  
271 It returns true upon success, and upon error, it will set the error in the global variable C<$UUError> and return C<undef>
272  
273 my $decoded = HTTP::Promise::Stream::UU::decode_uu( $encoded );
274 die( "Something went wrong: $HTTP::Promise::Stream::UU::UUError\n" if( !defined( $decoded ) );
275 print( "Decoded data is: $decoded\n" );
276  
277 =head2 encode_uu
278  
279 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.
280  
281 It will encode the data into UU encoded data and write the result into the output.
282  
283 It returns true upon success, and upon error, it will set the error in the global variable C<$UUError> and return C<undef>
284  
285 my $encoded = HTTP::Promise::Stream::UU::encode_uu( $data );
286 die( "Something went wrong: $HTTP::Promise::Stream::UU::UUError\n" if( !defined( $encoded ) );
287 print( "Encoded data is: $encoded\n" );
288  
289 =head2 is_decoder_installed
290  
291 Always returns true, because decoding is done internally.
292  
293 =head2 is_encoder_installed
294  
295 Always returns true, because encoding is done internally.
296  
297 =head1 THREAD-SAFETY
298  
299 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
300  
301 =head1 AUTHOR
302  
303 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
304  
305 =head1 SEE ALSO
306  
307 L<Wikipedia page|https://en.wikipedia.org/wiki/Uuencoding>
308  
309 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>
310  
311 =head1 COPYRIGHT & LICENSE
312  
313 Copyright(c) 2022 DEGUEST Pte. Ltd.
314  
315 All rights reserved.
316  
317 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
318  
319 =cut
« lib/HTTP/Promise/MIME.pm ↑ Summary blib/lib/HTTP/Promise/Headers/Cookie.pm »