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

filter: Statementstmt 0.0% 0 / 105 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 29 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 195 CC 18 SCAR 34.5
CC
18
Cov
0%
encode
90.0
decode
42.0
decode_qp
6.0
CRAP 31.6
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 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 $QuotedPrintError $DEBUG );
23 use Encode ();
24 use Module::Generic::File::IO;
25 our @EXPORT_OK = qw( decode_qp encode_qp );
26 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
27 our $VERSION = 'v0.2.0';
28 our $DEBUG = 0;
29 };
30  
31 use strict;
32 use warnings;
33  
34 sub decode
35 {
36 my $self = shift( @_ );
37 my $from = shift( @_ );
38 my $to = shift( @_ );
39 my $opts = $self->_get_args_as_hash( @_ );
40 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
41 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
42 return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
43 $self->_load_class( 'MIME::QuotedPrint', { no_import => 1 } ) || return( $self->pass_error );
44 # Wrap the filehandle into an object-oriented one that support the getline() method
45 unless( $self->_can( $from_fh => 'getline' ) )
46 {
47 my $io = Module::Generic::File::IO->new;
48 $io->fdopen( ( $self->_can( $from_fh => 'fileno' ) ? $from_fh->fileno : fileno( $from_fh ) ), 'r' ) ||
49 return( $self->pass_error( $io->error ) );
50 $from_fh = $io;
51 }
52
53 my $buff;
54 while( defined( $buff = $from_fh->getline ) )
55 {
56 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 my $rv = $writer->( $decoded );
60 return( $self->pass_error ) if( !defined( $rv ) );
61 }
62 return( $self->pass_error( $from_fh->error ) ) if( !defined( $buff ) && $self->_can( $from_fh => 'error' ) && $self->error );
63 return( $self );
64 }
65  
66 sub decode_qp
67 {
68 my $s = __PACKAGE__->new( debug => $DEBUG );
69 my $rv = $s->decode( @_ );
70 if( !defined( $rv ) )
71 {
72 $QuotedPrintError = $s->error;
73 return( $s->pass_error );
74 }
75 else
76 {
77 undef( $QuotedPrintError );
78 return( $rv );
79 }
80 }
81  
82 sub encode
83 {
84 my $self = shift( @_ );
85 my $from = shift( @_ );
86 my $to = shift( @_ );
87 my $opts = $self->_get_args_as_hash( @_ );
88 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
89 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
90 return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
91 $self->_load_class( 'MIME::QuotedPrint', { no_import => 1 } ) || return( $self->pass_error );
92 # Wrap the filehandle into an object-oriented one that support the getline() method
93 unless( $self->_can( $from_fh => 'getline' ) )
94 {
95 my $io = Module::Generic::File::IO->new;
96 $io->fdopen( ( $self->_can( $from_fh => 'fileno' ) ? $from_fh->fileno : fileno( $from_fh ) ), 'r' ) ||
97 return( $self->pass_error( $io->error ) );
98 $from_fh = $io;
99 }
100 my $eol = ( exists( $opts->{eol} ) && defined( $opts->{eol} ) ) ? $opts->{eol} : $/;
101 my $has_eol = length( $eol );
102
103 my $buff;
104 while( defined( $buff = $from_fh->getline ) )
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 $buff = Encode::encode_utf8( $buff ) if( Encode::is_utf8( $buff ) );
109 my $encoded = MIME::QuotedPrint::encode_qp( $buff, ( $has_eol ? ( $eol ) : () ) );
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 my $rv = $writer->( $encoded );
113 return( $self->pass_error ) if( !defined( $rv ) );
114 }
115 return( $self->pass_error( $from_fh->error ) ) if( !defined( $buff ) && $self->_can( $from_fh => 'error' ) && $self->error );
116 return( $self );
117 }
118  
119 sub encode_qp
120 {
121 my $s = __PACKAGE__->new;
122 my $rv = $s->encode( @_ );
123 if( !defined( $rv ) )
124 {
125 $QuotedPrintError = $s->error;
126 return( $s->pass_error );
127 }
128 else
129 {
130 undef( $QuotedPrintError );
131 return( $rv );
132 }
133 }
134  
135 sub encode_qp_utf8 { return( shift->encode_qp( Encode::encode_utf8( shift( @_ ) ) ) ); }
136  
137 sub is_decoder_installed
138 {
139 local $@;
140 eval
141 {
142 local $SIG{__DIE__} = sub{};
143 require MIME::QuotedPrint;
144 };
145 return( $@ ? 0 : 1 );
146 }
147  
148 sub is_encoder_installed
149 {
150 local $@;
151 eval
152 {
153 local $SIG{__DIE__} = sub{};
154 require MIME::QuotedPrint;
155 };
156 return( $@ ? 0 : 1 );
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 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