lib/HTTP/Promise/Stream/QuotedPrint.pm

filter: Statementstmt 73.7% 73 / 99 Branchbran 41.3% 19 / 46 Conditioncond 38.1% 8 / 21 MC/DCmcdc 8.3% 1 / 12 Subroutinesub 68.4% 13 / 19 Podpod 100% 7 / 7 totaltotal 59.3% 121 / 204 CC 31 SCAR 27.8
CC
31
Cov
60%
encode
29.5
decode
21.9
decode_qp
6.0
CRAP 16.1
« lib/HTTP/Promise/Body.pm ↑ Summary blib/lib/HTTP/Promise/Stream/Base64.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Stream/QuotedPrint.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/05/30
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::QuotedPrint;
15 BEGIN
16 {
17 4 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 4 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶ warnings::register_categories( 'HTTP::Promise' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 4 ▶ use HTTP::Promise::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 4 ▶ use parent -norequire, qw( HTTP::Promise::Stream::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 4 ▶ use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $QuotedPrintError $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 4 ▶ use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶ use Module::Generic::File::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 our @EXPORT_OK = qw( decode_qp encode_qp );
26 4 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
27 4 our $VERSION = 'v0.2.0';
28 4 our $DEBUG = 0;
29 };
30  
31 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 sub decode
35 {
36 1 ▶ my $self = shift( @_ );
Subroutine decode: called CC=12 SCAR=30.8
Pod documented
37 1 my $from = shift( @_ );
38 1 my $to = shift( @_ );
39 1 my $opts = $self->_get_args_as_hash( @_ );
40 1 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
41 1 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
42 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
43 1 ▶ $self->_load_class( 'MIME::QuotedPrint', { no_import => 1 } ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("MIME::QuotedPrint", {"no_import", 1}) or return $self->pass_error 0 1 1
44 # Wrap the filehandle into an object-oriented one that support the getline() method
45 1 ▶ unless( $self->_can( $from_fh => 'getline' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_can($from_fh, "getline")) 0 1 1
46 {
47 0 my $io = Module::Generic::File::IO->new;
48 0 ▶ $io->fdopen( ( $self->_can( $from_fh => 'fileno' ) ? $from_fh->fileno : fileno( $from_fh ) ), 'r' ) ||
Branch: 2 branches
BranchTrueFalseTotal
$io->fdopen($self->_can($from_fh, "fileno") ? $from_fh->fileno : fileno $from_fh, "r") or return $self->pass_error($io->error) 0 0 0
$self->_can($from_fh, 'fileno') ? : 0 0 0
49 return( $self->pass_error( $io->error ) );
50 0 $from_fh = $io;
51 }
52
53 1 my $buff;
54 1 ▶ while( defined( $buff = $from_fh->getline ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($buff = $from_fh->getline)) 8 1 9
55 {
56 8 my $decoded = MIME::QuotedPrint::decode_qp( $buff );
57 # MIME::QuotedPrint::decode_qp() will decode the data into an utf-8 bytes (not the perl's internal representation)
58 # This is fine and we save it as it is in the output
59 8 my $rv = $writer->( $decoded );
60 8 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 8 8
61 }
62 1 ▶ return( $self->pass_error( $from_fh->error ) ) if( !defined( $buff ) && $self->_can( $from_fh => 'error' ) && $self->error );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $buff and $self->_can($from_fh, "error") and $self->error 0 1 1
Condition33%2/6
not defined $buff and $self->_can($from_fh, "error") and $self->error
!ll&&!rl&&r
010
not defined $buff and $self->_can($from_fh, "error")
!ll&&!rl&&r
001
MC/DC0%0/3
not defined $buff and $self->_can($from_fh, "error") and $self->error
not defined $buff $self->_can($from_fh, "error") $self->error
Truth table25%1/4
not(A) and B and C
A= not defined $buffB= $self->_can($from_fh, "error")C= $self->error
ABCresult
0XX0
10X0
1100
1111
63 1 return( $self );
64 }
65  
66 sub decode_qp
67 {
68 0 ▶ my $s = __PACKAGE__->new( debug => $DEBUG );
Subroutine decode_qp: not called CC=2 SCAR=17.9
Pod documented
69 0 my $rv = $s->decode( @_ );
70 0 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 0 0
71 {
72 0 $QuotedPrintError = $s->error;
73 0 return( $s->pass_error );
74 }
75 else
76 {
77 0 undef( $QuotedPrintError );
78 0 return( $rv );
79 }
80 }
81  
82 sub encode
83 {
84 2 ▶ my $self = shift( @_ );
Subroutine encode: called CC=16 SCAR=33.8
Pod documented
85 2 my $from = shift( @_ );
86 2 my $to = shift( @_ );
87 2 my $opts = $self->_get_args_as_hash( @_ );
88 2 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
89 2 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
90 2 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 2 2
Condition33%1/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
002
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
91 2 ▶ $self->_load_class( 'MIME::QuotedPrint', { no_import => 1 } ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("MIME::QuotedPrint", {"no_import", 1}) or return $self->pass_error 0 2 2
92 # Wrap the filehandle into an object-oriented one that support the getline() method
93 2 ▶ unless( $self->_can( $from_fh => 'getline' ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_can($from_fh, "getline")) 0 2 2
94 {
95 0 my $io = Module::Generic::File::IO->new;
96 0 ▶ $io->fdopen( ( $self->_can( $from_fh => 'fileno' ) ? $from_fh->fileno : fileno( $from_fh ) ), 'r' ) ||
Branch: 2 branches
BranchTrueFalseTotal
$io->fdopen($self->_can($from_fh, "fileno") ? $from_fh->fileno : fileno $from_fh, "r") or return $self->pass_error($io->error) 0 0 0
$self->_can($from_fh, 'fileno') ? : 0 0 0
97 return( $self->pass_error( $io->error ) );
98 0 $from_fh = $io;
99 }
100 2 ▶ my $eol = ( exists( $opts->{eol} ) && defined( $opts->{eol} ) ) ? $opts->{eol} : $/;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'eol'} && defined $opts->{'eol'} ? : 1 1 2
Condition66%2/3
exists $opts->{"eol"} and defined $opts->{"eol"}
!ll&&!rl&&r
101
MC/DC50%1/2
exists $opts->{"eol"} and defined $opts->{"eol"}
exists $opts->{"eol"} defined $opts->{"eol"}
Truth table66%2/3
A and B
A= exists $opts->{"eol"}B= defined $opts->{"eol"}
ABresult
0X0
100
111
101 2 my $has_eol = length( $eol );
102
103 2 my $buff;
104 2 ▶ while( defined( $buff = $from_fh->getline ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined($buff = $from_fh->getline)) 11 2 13
105 {
106 # Make sure the chunk of data is in formal utf-8 encoding, i.e. not perl's internal representation
107 # Should probably use Encode::encode( 'utf-8', $buff ) instead though
108 11 ▶ $buff = Encode::encode_utf8( $buff ) if( Encode::is_utf8( $buff ) );
Branch: 1 branch
BranchTrueFalseTotal
if &Encode::is_utf8($buff) 0 11 11
109 11 ▶ my $encoded = MIME::QuotedPrint::encode_qp( $buff, ( $has_eol ? ( $eol ) : () ) );
Branch: 1 branch
BranchTrueFalseTotal
$has_eol ? : 11 0 11
110 # MIME::QuotedPrint::decode_qp() will decode the data into an utf-8 bytes (not the perl's internal representation)
111 # This is fine and we save it as it is in the output
112 11 my $rv = $writer->( $encoded );
113 11 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 11 11
114 }
115 2 ▶ return( $self->pass_error( $from_fh->error ) ) if( !defined( $buff ) && $self->_can( $from_fh => 'error' ) && $self->error );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $buff and $self->_can($from_fh, "error") and $self->error 0 2 2
Condition33%2/6
not defined $buff and $self->_can($from_fh, "error") and $self->error
!ll&&!rl&&r
020
not defined $buff and $self->_can($from_fh, "error")
!ll&&!rl&&r
002
MC/DC0%0/3
not defined $buff and $self->_can($from_fh, "error") and $self->error
not defined $buff $self->_can($from_fh, "error") $self->error
Truth table25%1/4
not(A) and B and C
A= not defined $buffB= $self->_can($from_fh, "error")C= $self->error
ABCresult
0XX0
10X0
1100
1111
116 2 return( $self );
117 }
118  
119 sub encode_qp
120 {
121 1 ▶ my $s = __PACKAGE__->new;
Subroutine encode_qp: called CC=2 SCAR=7.6
Pod documented
122 1 my $rv = $s->encode( @_ );
123 1 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 1 1
124 {
125 0 $QuotedPrintError = $s->error;
126 0 return( $s->pass_error );
127 }
128 else
129 {
130 1 undef( $QuotedPrintError );
131 1 return( $rv );
132 }
133 }
134  
135 0 ▶sub encode_qp_utf8 { return( shift->encode_qp( Encode::encode_utf8( shift( @_ ) ) ) ); }
Subroutine encode_qp_utf8: not called CC=1 SCAR=6.9
Pod documented
136  
137 sub is_decoder_installed
138 {
139 0 ▶ local $@;
Subroutine is_decoder_installed: not called CC=2 SCAR=17.9
Pod documented
140 eval
141 0 {
142 0 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 require MIME::QuotedPrint;
144 };
145 0 ▶ return( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
146 }
147  
148 sub is_encoder_installed
149 {
150 0 ▶ local $@;
Subroutine is_encoder_installed: not called CC=2 SCAR=17.9
Pod documented
151 eval
152 0 {
153 0 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 0 require MIME::QuotedPrint;
155 };
156 0 ▶ return( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
157 }
158  
159 # NOTE: sub FREEZE is inherited
160  
161 # NOTE: sub STORABLE_freeze is inherited
162  
163 # NOTE: sub STORABLE_thaw is inherited
164  
165 # NOTE: sub THAW is inherited
166  
167 4 1;
168 # NOTE: POD
169 __END__
170  
171 =encoding utf-8
172  
173 =head1 NAME
174  
175 HTTP::Promise::Stream::QuotedPrint - Stream Encoder for QuotedPrint Encoding
176  
177 =head1 SYNOPSIS
178  
179 use HTTP::Promise::Stream::QuotedPrint;
180 my $s = HTTP::Promise::Stream::QuotedPrint->new ||
181 die( HTTP::Promise::Stream::QuotedPrint->error, "\n" );
182 $s->encode( $input => $output, eol => "\n" ) ||
183 die( $s->error );
184 $s->decode( $input => $output ) || die( $s->error );
185 HTTP::Promise::Stream::QuotedPrint::encode_qp( $input => $output, eol => "\n" ) ||
186 die( $HTTP::Promise::Stream::QuotedPrint::QuotedPrintError );
187 HTTP::Promise::Stream::QuotedPrint::decode_qp( $input => $output, eol => "\n" ) ||
188 die( $HTTP::Promise::Stream::QuotedPrint::QuotedPrintError );
189  
190 =head1 VERSION
191  
192 v0.2.0
193  
194 =head1 DESCRIPTION
195  
196 This implements an encoding and decoding mechanism for quoted-printable encoding using either of the following on input and output:
197  
198 =over 4
199  
200 =item C<filepath>
201  
202 If the parameter is neither a scalar reference nor a file handle, it will be assumed to be a file path.
203  
204 =item C<file handle>
205  
206 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.
207  
208 Alternatively, those methods can die and those exceptions wil be caught.
209  
210 =item C<scalar reference>
211  
212 This can be a simple scalar reference, or an object scalar reference.
213  
214 =back
215  
216 Requires the XS module L<MIME::QuotedPrint> for encoding and decoding.
217  
218 This encodes and decodes the quoted-printable data according to L<rfc2045, section 6.7|https://tools.ietf.org/html/rfc2045#section-6.7>
219  
220 =head1 CONSTRUCTOR
221  
222 =head2 new
223  
224 Creates a new L<HTTP::Promise::Stream::QuotedPrint> object and returns it.
225  
226 =head1 METHODS
227  
228 =head2 decode
229  
230 This takes 2 arguments: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
231  
232 It will decode the quoted-printable encoded data and write the result into the output.
233  
234 It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error.
235  
236 =head2 encode
237  
238 This takes 2 arguments: an input and an output. Each one can be either a file path, a file handle, or a scalar reference.
239  
240 It will encode the data into quoted-printable encoded data and write the result into the output.
241  
242 If the option I<eol> (standing for "End of line") is provided, it will be used at the end of each line of 76 characters. If I<eol> is not provided, it will default to C<$/>, which usually is C<\n>.
243  
244 It returns true upon success and sets an L<error|Module::Generic/error> and return C<undef> upon error.
245  
246 =head1 CLASS FUNCTIONS
247  
248 The following class functions are available and can also be exported, such as:
249  
250 use HTTP::Promise::Stream::QuotedPrint qw( decode_qp encode_qp );
251  
252 =head2 decode_qp
253  
254 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.
255  
256 It will decode the quoted-printable encoded data and write the result into the output.
257  
258 It returns true upon success, and upon error, it will set the error in the global variable C<$QuotedPrintError> and return C<undef>
259  
260 my $decoded = HTTP::Promise::Stream::QuotedPrint::decode_qp( $encoded );
261 die( "Something went wrong: $HTTP::Promise::Stream::QuotedPrint::QuotedPrintError\n" if( !defined( $decoded ) );
262 print( "Decoded data is: $decoded\n" );
263  
264 =head2 encode_qp
265  
266 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.
267  
268 It will encode the data into quoted-printable encoded data and write the result into the output.
269  
270 It returns true upon success, and upon error, it will set the error in the global variable C<$QuotedPrintError> and return C<undef>
271  
272 my $encoded = HTTP::Promise::Stream::QuotedPrint::encode_qp( $data );
273 die( "Something went wrong: $HTTP::Promise::Stream::QuotedPrint::QuotedPrintError\n" if( !defined( $encoded ) );
274 print( "Encoded data is: $encoded\n" );
275  
276 =head2 encode_qp_utf8
277  
278 This takes a string, encode it into an UTF-8 string using L<Encode/encode_utf8> and then encode the resulting string into quoted-printable and returns the result.
279  
280 =head2 is_decoder_installed
281  
282 Returns true if the module L<MIME::QuotedPrint> is installed, false otherwise.
283  
284 =head2 is_encoder_installed
285  
286 Returns true if the module L<MIME::QuotedPrint> is installed, false otherwise.
287  
288 =head1 THREAD-SAFETY
289  
290 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
291  
292 =head1 AUTHOR
293  
294 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
295  
296 =head1 SEE ALSO
297  
298 This encodes and decodes the quoted-printable data according to L<rfc2045, section 6.7|https://tools.ietf.org/html/rfc2045#section-6.7>
299  
300 See also the L<Wikipedia page|https://en.wikipedia.org/wiki/Quoted-printable>
301  
302 L<PerlIO::via::QuotedPrint>
303  
304 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>
305  
306 =head1 COPYRIGHT & LICENSE
307  
308 Copyright(c) 2022 DEGUEST Pte. Ltd.
309  
310 All rights reserved.
311  
312 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
313  
314 =cut
« lib/HTTP/Promise/Body.pm ↑ Summary blib/lib/HTTP/Promise/Stream/Base64.pm »