lib/HTTP/Promise/Stream.pm

filter: Statementstmt 54.3% 486 / 895 Branchbran 34.3% 196 / 570 Conditioncond 35.8% 76 / 212 MC/DCmcdc 11.4% 13 / 114 Subroutinesub 68.9% 60 / 87 Podpod 84.2% 16 / 19 totaltotal 44.6% 847 / 1897 CC 371 SCAR 61.6
CC
371
Cov
45%
write
2407.8
read
419.8
as_string
240.0
CRAP 474.1
« blib/lib/HTTP/Promise/Entity.pm ↑ Summary lib/HTTP/Promise/Entity.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Stream.pm
3 ## Version v0.3.0
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/03/28
7 ## Modified 2026/03/09
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;
15 BEGIN
16 {
17 13 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 13 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 13 ▶ warnings::register_categories( 'HTTP::Promise' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 13 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 13 use vars qw(
22 $FILTER_MAP $CLASSES $ENCODING_SUFFIX $SUFFIX_ENCODING $ENCODING_ALIAS
23 $MIME_ENCODING
24 13 ▶ );
Subroutine BEGIN: called CC=1 SCAR=0.0
25 13 ▶ use HTTP::Promise::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 13 ▶ use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 13 ▶ use constant HAS_BROWSER_SUPPORT => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 13 our $VERSION = 'v0.3.0';
29 };
30  
31 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 13 ▶no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 {
36 13 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
37 13 $CLASSES =
38 {
39 base64 => [qw( HTTP::Promise::Stream::Base64 HTTP::Promise::Stream::Base64 )],
40 brotli => [qw( HTTP::Promise::Stream::Brotli HTTP::Promise::Stream::Brotli ), HAS_BROWSER_SUPPORT],
41 bzip2 => [qw( IO::Compress::Bzip2 IO::Uncompress::Bunzip2 ), HAS_BROWSER_SUPPORT],
42 deflate => [qw( IO::Compress::Deflate IO::Uncompress::Inflate ), HAS_BROWSER_SUPPORT],
43 gzip => [qw( IO::Compress::Gzip IO::Uncompress::Gunzip ), HAS_BROWSER_SUPPORT],
44 lzf => [qw( IO::Compress::Lzf IO::Uncompress::UnLzf )],
45 lzip => [qw( IO::Compress::Lzip IO::Uncompress::UnLzip )],
46 lzma => [qw( IO::Compress::Lzma IO::Uncompress::UnLzma )],
47 lzop => [qw( IO::Compress::Lzop IO::Uncompress::UnLzop )],
48 lzw => [qw( HTTP::Promise::Stream::LZW HTTP::Promise::Stream::LZW )],
49 qp => [qw( HTTP::Promise::Stream::QuotedPrint HTTP::Promise::Stream::QuotedPrint )],
50 rawdeflate => [qw( IO::Compress::RawDeflate IO::Uncompress::RawInflate ), HAS_BROWSER_SUPPORT],
51 uu => [qw( HTTP::Promise::Stream::UU HTTP::Promise::Stream::UU )],
52 xz => [qw( IO::Compress::Xz IO::Uncompress::UnXz )],
53 zip => [qw( IO::Compress::Zip IO::Uncompress::Unzip )],
54 zstd => [qw( IO::Compress::Zstd IO::Uncompress::UnZstd )],
55 };
56 13 $CLASSES->{br} = $CLASSES->{brotli};
57 13 $CLASSES->{inflate} = $CLASSES->{deflate};
58 13 $CLASSES->{rawinflate} = $CLASSES->{rawdeflate};
59 13 $CLASSES->{compress} = $CLASSES->{lzw};
60 13 $CLASSES->{'quoted-printable'} = $CLASSES->{qp};
61 13 $CLASSES->{'x-compress'} = $CLASSES->{lzw};
62 13 $CLASSES->{uuencode} = $CLASSES->{uu};
63 # Permit non-standard call with prefix x-
64 13 for( qw( bzip2 deflate gzip zip ) )
65 {
66 52 $CLASSES->{'x-' . $_} = $CLASSES->{ $_ };
67 }
68
69 $FILTER_MAP =
70 {
71 encode =>
72 {
73 # NOTE: encode -> base64
74 base64 => sub
75 {
76 # try-catch
77 5 ▶ local $@;
Subroutine __ANON__: called CC=3 SCAR=11.4
78 my $rv = eval
79 5 {
80 5 require HTTP::Promise::Stream::Base64;
81 5 HTTP::Promise::Stream::Base64::encode_b64( $_[0] => $_[1], @_[2..$#_] );
82 };
83 5 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
84 {
85 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
86 }
87 # defined( $rv ) or return( undef, $HTTP::Promise::Stream::Base64::Base64Error );
88 5 ▶ defined( $rv ) or return( undef, HTTP::Promise::Stream::Base64->error );
Branch: 1 branch
BranchTrueFalseTotal
defined $rv or return undef, "HTTP::Promise::Stream::Base64"->error 0 5 5
89 5 return( $rv );
90 },
91 # NOTE: encode -> brotli
92 brotli => sub
93 {
94 # try-catch
95 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
96 my $rv = eval
97 0 {
98 0 require HTTP::Promise::Stream::Brotli;
99 0 HTTP::Promise::Stream::Brotli::encode_bro( $_[0] => $_[1], @_[2..$#_] );
100 };
101 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
102 {
103 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
104 }
105 # $rv or return( undef, $HTTP::Promise::Stream::Brotli::BrotliError );
106 0 ▶ defined( $rv ) or return( undef, HTTP::Promise::Stream::Brotli->error );
Branch: 1 branch
BranchTrueFalseTotal
defined $rv or return undef, "HTTP::Promise::Stream::Brotli"->error 0 0 0
107 0 return( $rv );
108 },
109 # NOTE: encode -> bzip2
110 bzip2 => sub
111 {
112 # try-catch
113 8 ▶ local $@;
Subroutine __ANON__: called CC=3 SCAR=11.4
114 my $rv = eval
115 8 {
116 8 require IO::Compress::Bzip2;
117 8 IO::Compress::Bzip2::bzip2( $_[0] => $_[1], @_[2..$#_] );
118 };
119 8 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
120 {
121 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
122 }
123 8 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Bzip2::Bzip2Error }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Compress::Bzip2::Bzip2Error}) 0 8 8
124 8 return( $rv );
125 },
126 # NOTE: encode -> deflate
127 deflate => sub
128 {
129 # try-catch
130 8 ▶ local $@;
Subroutine __ANON__: called CC=3 SCAR=11.4
131 my $rv = eval
132 8 {
133 8 require IO::Compress::Deflate;
134 8 IO::Compress::Deflate::deflate( $_[0] => $_[1], @_[2..$#_] );
135 };
136 8 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
137 {
138 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
139 }
140 # $rv or return( undef, $IO::Compress::Deflate::DeflateError );
141 8 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Deflate::DeflateError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Compress::Deflate::DeflateError}) 0 8 8
142 8 return( $rv );
143 },
144 # NOTE: encode -> gzip
145 gzip => sub
146 {
147 # try-catch
148 9 ▶ local $@;
Subroutine __ANON__: called CC=3 SCAR=11.4
149 my $rv = eval
150 9 {
151 9 require IO::Compress::Gzip;
152 9 IO::Compress::Gzip::gzip( $_[0] => $_[1], @_[2..$#_] );
153 };
154 9 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 9 9
155 {
156 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
157 }
158 # $rv or return( undef, $IO::Compress::Gzip::GzipError );
159 9 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Gzip::GzipError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Compress::Gzip::GzipError}) 0 9 9
160 9 return( $rv );
161 },
162 # NOTE: encode -> lzf
163 lzf => sub
164 {
165 # try-catch
166 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
167 my $rv = eval
168 0 {
169 0 require IO::Compress::Lzf;
170 0 IO::Compress::Lzf::lzf( $_[0] => $_[1], @_[2..$#_] );
171 };
172 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
173 {
174 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
175 }
176 # $rv or return( undef, $IO::Compress::Lzf::LzfError );
177 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Lzf::LzfError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Compress::Lzf::LzfError}) 0 0 0
178 0 return( $rv );
179 },
180 # NOTE: encode -> lzip
181 lzip => sub
182 {
183 # try-catch
184 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
185 my $rv = eval
186 0 {
187 0 require IO::Compress::Lzip;
188 0 IO::Compress::Lzip::lzip( $_[0] => $_[1], @_[2..$#_] );
189 };
190 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
191 {
192 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
193 }
194 # $rv or return( undef, $IO::Compress::Lzip::LzipError );
195 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Lzip::LzipError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Compress::Lzip::LzipError}) 0 0 0
196 0 return( $rv );
197 },
198 # NOTE: encode -> lzma
199 lzma => sub
200 {
201 # try-catch
202 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
203 my $rv = eval
204 0 {
205 0 require IO::Compress::Lzma;
206 0 IO::Compress::Lzma::lzma( $_[0] => $_[1], @_[2..$#_] );
207 };
208 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
209 {
210 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
211 }
212 # $rv or return( undef, $IO::Compress::Lzma::LzmaError );
213 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Lzma::LzmaError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Compress::Lzma::LzmaError}) 0 0 0
214 0 return( $rv );
215 },
216 # NOTE: encode -> lzop
217 lzop => sub
218 {
219 # try-catch
220 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
221 my $rv = eval
222 0 {
223 0 require IO::Compress::Lzop;
224 0 IO::Compress::Lzop::lzop( $_[0] => $_[1], @_[2..$#_] );
225 };
226 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
227 {
228 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
229 }
230 # $rv or return( undef, $IO::Compress::Lzop::LzopError );
231 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Lzop::LzopError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Compress::Lzop::LzopError}) 0 0 0
232 0 return( $rv );
233 },
234 # NOTE: encode -> lzw
235 lzw => sub
236 {
237 # try-catch
238 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
239 my $rv = eval
240 0 {
241 0 require HTTP::Promise::Streem::LZW;
242 0 HTTP::Promise::Streem::LZW::encode_lzw( $_[0] => $_[1], @_[2..$#_] );
243 };
244 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
245 {
246 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
247 }
248 # $rv or return( undef, $HTTP::Promise::Streem::LZW::LZWError );
249 0 ▶ defined( $rv ) or return( undef, HTTP::Promise::Streem::LZW->error );
Branch: 1 branch
BranchTrueFalseTotal
defined $rv or return undef, "HTTP::Promise::Streem::LZW"->error 0 0 0
250 0 return( $rv );
251 },
252 # NOTE: encode -> qp
253 qp => sub
254 {
255 # try-catch
256 1 ▶ local $@;
Subroutine __ANON__: called CC=3 SCAR=11.4
257 my $rv = eval
258 1 {
259 1 require HTTP::Promise::Stream::QuotedPrint;
260 1 HTTP::Promise::Stream::QuotedPrint::encode_qp( $_[0] => $_[1], @_[2..$#_] );
261 };
262 1 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
263 {
264 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
265 }
266 # $rv or return( undef, $HTTP::Promise::Stream::QuotedPrint::QuotedPrintError );
267 1 ▶ defined( $rv ) or return( undef, HTTP::Promise::Stream::QuotedPrint->error );
Branch: 1 branch
BranchTrueFalseTotal
defined $rv or return undef, "HTTP::Promise::Stream::QuotedPrint"->error 0 1 1
268 1 return( $rv );
269 },
270 # NOTE: encode -> rawdeflate
271 rawdeflate => sub
272 {
273 # try-catch
274 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
275 my $rv = eval
276 0 {
277 0 require IO::Compress::RawDeflate;
278 0 IO::Compress::RawDeflate::rawdeflate( $_[0] => $_[1], @_[2..$#_] );
279 };
280 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
281 {
282 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
283 }
284 # $rv or return( undef, $IO::Compress::RawDeflate::RawDeflateError );
285 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::RawDeflate::RawDeflateError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Compress::RawDeflate::RawDeflateError}) 0 0 0
286 0 return( $rv );
287 },
288 # NOTE: encode -> uu
289 uu => sub
290 {
291 # try-catch
292 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
293 my $rv = eval
294 0 {
295 0 require HTTP::Promise::Stream::UU;
296 0 HTTP::Promise::Stream::UU::encode_uu( $_[0] => $_[1], @_[2..$#_] );
297 };
298 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
299 {
300 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
301 }
302 # $rv or return( undef, $HTTP::Promise::Stream::UU::UUError );
303 0 ▶ defined( $rv ) or return( undef, HTTP::Promise::Stream::UU->error );
Branch: 1 branch
BranchTrueFalseTotal
defined $rv or return undef, "HTTP::Promise::Stream::UU"->error 0 0 0
304 0 return( $rv );
305 },
306 # NOTE: encode -> xz
307 xz => sub
308 {
309 # try-catch
310 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
311 my $rv = eval
312 0 {
313 0 require IO::Compress::Xz;
314 0 IO::Compress::Xz::xz( $_[0] => $_[1], @_[2..$#_] );
315 };
316 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
317 {
318 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
319 }
320 # $rv or return( undef, $IO::Compress::Xz::XzError );
321 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Xz::XzError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Compress::Xz::XzError}) 0 0 0
322 0 return( $rv );
323 },
324 # NOTE: encode -> zip
325 zip => sub
326 {
327 # try-catch
328 8 ▶ local $@;
Subroutine __ANON__: called CC=3 SCAR=11.4
329 my $rv = eval
330 8 {
331 8 require IO::Compress::Zip;
332 8 IO::Compress::Zip::zip( $_[0] => $_[1], @_[2..$#_] );
333 };
334 8 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
335 {
336 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
337 }
338 # $rv or return( undef, $IO::Compress::Zip::ZipError );
339 8 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Zip::ZipError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Compress::Zip::ZipError}) 0 8 8
340 8 return( $rv );
341 },
342 # NOTE: encode -> zstd
343 zstd => sub
344 {
345 # try-catch
346 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
347 my $rv = eval
348 0 {
349 0 require IO::Compress::Zstd;
350 0 IO::Compress::Zstd::zstd( $_[0] => $_[1], @_[2..$#_] );
351 };
352 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
353 {
354 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
355 }
356 # $rv or return( undef, $IO::Compress::Zstd::ZstdError );
357 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Zstd::ZstdError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Compress::Zstd::ZstdError}) 0 0 0
358 0 return( $rv );
359 },
360 },
361 decode =>
362 {
363 # NOTE: decode -> base64
364 base64 => sub
365 {
366 # try-catch
367 4 ▶ local $@;
Subroutine __ANON__: called CC=3 SCAR=11.4
368 my $rv = eval
369 4 {
370 4 require HTTP::Promise::Stream::Base64;
371 4 HTTP::Promise::Stream::Base64::decode_b64( $_[0] => $_[1], @_[2..$#_] );
372 };
373 4 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
374 {
375 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
376 }
377 # $rv or return( undef, $HTTP::Promise::Stream::Base64::Base64Error );
378 4 ▶ defined( $rv ) or return( undef, HTTP::Promise::Stream::Base64->error );
Branch: 1 branch
BranchTrueFalseTotal
defined $rv or return undef, "HTTP::Promise::Stream::Base64"->error 0 4 4
379 4 return( $rv );
380 },
381 # NOTE: decode -> brotli
382 brotli => sub
383 {
384 # try-catch
385 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
386 my $rv = eval
387 0 {
388 0 require HTTP::Promise::Stream::Brotli;
389 0 HTTP::Promise::Stream::Brotli::decode_bro( $_[0] => $_[1], @_[2..$#_] );
390 };
391 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
392 {
393 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
394 }
395 # defined( $rv ) or return( undef, $HTTP::Promise::Stream::Brotli::BrotliError );
396 0 ▶ defined( $rv ) or return( undef, HTTP::Promise::Stream::Brotli->error );
Branch: 1 branch
BranchTrueFalseTotal
defined $rv or return undef, "HTTP::Promise::Stream::Brotli"->error 0 0 0
397 0 return( $rv );
398 },
399 # NOTE: decode -> bzip2
400 bzip2 => sub
401 {
402 # try-catch
403 13 ▶ local $@;
Subroutine __ANON__: called CC=3 SCAR=11.4
404 my $rv = eval
405 13 {
406 13 require IO::Uncompress::Bunzip2;
407 13 IO::Uncompress::Bunzip2::bunzip2( $_[0] => $_[1], @_[2..$#_] );
408 };
409 13 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 13 13
410 {
411 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
412 }
413 # $rv or return( undef, $IO::Uncompress::Bunzip2::Bunzip2Error );
414 13 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::Bunzip2::Bunzip2Error }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Uncompress::Bunzip2::Bunzip2Error}) 0 13 13
415 13 return( $rv );
416 },
417 # NOTE: decode -> gzip
418 gzip => sub
419 {
420 # try-catch
421 13 ▶ local $@;
Subroutine __ANON__: called CC=3 SCAR=11.4
422 my $rv = eval
423 13 {
424 13 require IO::Uncompress::Gunzip;
425 13 IO::Uncompress::Gunzip::gunzip( $_[0] => $_[1], @_[2..$#_] );
426 };
427 13 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 13 13
428 {
429 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
430 }
431 # $rv or return( undef, $IO::Uncompress::Gunzip::GunzipError );
432 13 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::Gunzip::GunzipError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Uncompress::Gunzip::GunzipError}) 0 13 13
433 13 return( $rv );
434 },
435 # NOTE: decode -> inflate
436 inflate => sub
437 {
438 # try-catch
439 13 ▶ local $@;
Subroutine __ANON__: called CC=3 SCAR=11.4
440 my $rv = eval
441 13 {
442 13 require IO::Uncompress::Inflate;
443 13 IO::Uncompress::Inflate::inflate( $_[0] => $_[1], @_[2..$#_] );
444 };
445 13 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 13 13
446 {
447 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
448 }
449 # $rv or return( undef, $IO::Uncompress::Inflate::InflateError );
450 13 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::Inflate::InflateError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Uncompress::Inflate::InflateError}) 0 13 13
451 13 return( $rv );
452 },
453 # NOTE: decode -> lzf
454 lzf => sub
455 {
456 # try-catch
457 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
458 my $rv = eval
459 0 {
460 0 require IO::Uncompress::UnLzf;
461 0 IO::Uncompress::UnLzf::unlzf( $_[0] => $_[1], @_[2..$#_] );
462 };
463 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
464 {
465 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
466 }
467 # $rv or return( undef, $IO::Uncompress::UnLzf::UnLzfError );
468 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::UnLzf::UnLzfError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Uncompress::UnLzf::UnLzfError}) 0 0 0
469 0 return( $rv );
470 },
471 # NOTE: decode -> lzip
472 lzip => sub
473 {
474 # try-catch
475 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
476 my $rv = eval
477 0 {
478 0 require IO::Uncompress::UnLzip;
479 0 IO::Uncompress::UnLzip::unlzip( $_[0] => $_[1], @_[2..$#_] );
480 };
481 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
482 {
483 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
484 }
485 # $rv or return( undef, $IO::Uncompress::UnLzip::UnLzipError );
486 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::UnLzip::UnLzipError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Uncompress::UnLzip::UnLzipError}) 0 0 0
487 0 return( $rv );
488 },
489 # NOTE: decode -> lzma
490 lzma => sub
491 {
492 # try-catch
493 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
494 my $rv = eval
495 0 {
496 0 require IO::Uncompress::UnLzma;
497 0 IO::Uncompress::UnLzma::unlzma( $_[0] => $_[1], @_[2..$#_] );
498 };
499 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
500 {
501 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
502 }
503 # $rv or return( undef, $IO::Uncompress::UnLzma::UnLzmaError );
504 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::UnLzma::UnLzmaError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Uncompress::UnLzma::UnLzmaError}) 0 0 0
505 0 return( $rv );
506 },
507 # NOTE: decode -> lzop
508 lzop => sub
509 {
510 # try-catch
511 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
512 my $rv = eval
513 0 {
514 0 require IO::Uncompress::UnLzop;
515 0 IO::Uncompress::UnLzop::unlzop( $_[0] => $_[1], @_[2..$#_] );
516 };
517 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
518 {
519 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
520 }
521 # $rv or return( undef, $IO::Uncompress::UnLzop::UnLzopError );
522 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::UnLzop::UnLzopError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Uncompress::UnLzop::UnLzopError}) 0 0 0
523 0 return( $rv );
524 },
525 # NOTE: decode -> lzw
526 lzw => sub
527 {
528 # try-catch
529 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
530 my $rv = eval
531 0 {
532 0 require HTTP::Promise::Streem::LZW;
533 0 HTTP::Promise::Streem::LZW::decode_lzw( $_[0] => $_[1], @_[2..$#_] );
534 };
535 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
536 {
537 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
538 }
539 # $rv or return( undef, $HTTP::Promise::Streem::LZW::LZWError );
540 0 ▶ defined( $rv ) or return( undef, HTTP::Promise::Streem::LZW->error );
Branch: 1 branch
BranchTrueFalseTotal
defined $rv or return undef, "HTTP::Promise::Streem::LZW"->error 0 0 0
541 0 return( $rv );
542 },
543 # NOTE: decode -> qp
544 qp => sub
545 {
546 # try-catch
547 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
548 my $rv = eval
549 0 {
550 0 require HTTP::Promise::Stream::QuotedPrint;
551 0 HTTP::Promise::Stream::QuotedPrint::decode_qp( $_[0] => $_[1], @_[2..$#_] );
552 };
553 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
554 {
555 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
556 }
557 # $rv or return( undef, $HTTP::Promise::Stream::QuotedPrint::QuotedPrintError );
558 0 ▶ defined( $rv ) or return( undef, HTTP::Promise::Stream::QuotedPrint->error );
Branch: 1 branch
BranchTrueFalseTotal
defined $rv or return undef, "HTTP::Promise::Stream::QuotedPrint"->error 0 0 0
559 0 return( $rv );
560 },
561 # NOTE: decode -> rawinflate
562 rawinflate => sub
563 {
564 # try-catch
565 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
566 my $rv = eval
567 0 {
568 0 require IO::Uncompress::RawInflate;
569 0 IO::Uncompress::RawInflate::rawinflate( $_[0] => $_[1], @_[2..$#_] );
570 };
571 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
572 {
573 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
574 }
575 # $rv or return( undef, $IO::Uncompress::RawInflate::RawInflateError );
576 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::RawInflate::RawInflateError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Uncompress::RawInflate::RawInflateError}) 0 0 0
577 0 return( $rv );
578 },
579 # NOTE: decode -> uu
580 uu => sub
581 {
582 # try-catch
583 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
584 my $rv = eval
585 0 {
586 0 require HTTP::Promise::Stream::UU;
587 0 HTTP::Promise::Stream::UU::decode_uu( $_[0] => $_[1], @_[2..$#_] );
588 };
589 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
590 {
591 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
592 }
593 # $rv or return( undef, $HTTP::Promise::Stream::UU::UUError );
594 0 ▶ defined( $rv ) or return( undef, HTTP::Promise::Stream::UU->error );
Branch: 1 branch
BranchTrueFalseTotal
defined $rv or return undef, "HTTP::Promise::Stream::UU"->error 0 0 0
595 0 return( $rv );
596 },
597 # NOTE: decode -> xz
598 xz => sub
599 {
600 # try-catch
601 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
602 my $rv = eval
603 0 {
604 0 require IO::Uncompress::UnXz;
605 0 IO::Uncompress::UnXz::unxz( $_[0] => $_[1], @_[2..$#_] );
606 };
607 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
608 {
609 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
610 }
611 # $rv or return( undef, $IO::Uncompress::UnXz::UnXzError );
612 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::UnXz::UnXzError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Uncompress::UnXz::UnXzError}) 0 0 0
613 0 return( $rv );
614 },
615 # NOTE: decode -> zip
616 zip => sub
617 {
618 # try-catch
619 13 ▶ local $@;
Subroutine __ANON__: called CC=3 SCAR=11.4
620 my $rv = eval
621 13 {
622 13 require IO::Uncompress::Unzip;
623 13 IO::Uncompress::Unzip::unzip( $_[0] => $_[1], @_[2..$#_] );
624 };
625 13 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 13 13
626 {
627 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
628 }
629 # $rv or return( undef, $IO::Uncompress::Unzip::UnzipError );
630 13 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::Unzip::UnzipError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Uncompress::Unzip::UnzipError}) 0 13 13
631 13 return( $rv );
632 },
633 # NOTE: decode -> zstd
634 zstd => sub
635 {
636 # try-catch
637 0 ▶ local $@;
Subroutine __ANON__: not called CC=3 SCAR=24.8
638 my $rv = eval
639 0 {
640 0 require IO::Uncompress::UnZstd;
641 0 IO::Uncompress::UnZstd::unzstd( $_[0] => $_[1], @_[2..$#_] );
642 };
643 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
644 {
645 0 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
646 }
647 # $rv or return( undef, $IO::Uncompress::UnZstd::UnZstdError );
648 0 ▶ $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::UnZstd::UnZstdError }) );
Branch: 1 branch
BranchTrueFalseTotal
$rv or return undef, "HTTP::Promise::Exception"->new({"message", $IO::Uncompress::UnZstd::UnZstdError}) 0 0 0
649 0 return( $rv );
650 },
651 }
652 13 };
653 # rfc1945, section 3.5
654 # Ref: <https://tools.ietf.org/html/rfc1945#section-3.5>
655 13 $FILTER_MAP->{encode}->{ 'x-gzip' } = $FILTER_MAP->{encode}->{gzip};
656 13 $FILTER_MAP->{decode}->{ 'x-gzip' } = $FILTER_MAP->{decode}->{gzip};
657 13 $FILTER_MAP->{encode}->{ 'x-bzip2' } = $FILTER_MAP->{encode}->{bzip2};
658 13 $FILTER_MAP->{decode}->{ 'x-bzip2' } = $FILTER_MAP->{decode}->{bzip2};
659 13 $FILTER_MAP->{encode}->{ 'x-compress' } = $FILTER_MAP->{encode}->{lzw};
660 13 $FILTER_MAP->{decode}->{ 'x-compress' } = $FILTER_MAP->{decode}->{lzw};
661 13 $FILTER_MAP->{encode}->{br} = $FILTER_MAP->{encode}->{brotli};
662 13 $FILTER_MAP->{decode}->{br} = $FILTER_MAP->{decode}->{brotli};
663 13 $FILTER_MAP->{encode}->{compress} = $FILTER_MAP->{encode}->{lzw};
664 13 $FILTER_MAP->{decode}->{compress} = $FILTER_MAP->{decode}->{lzw};
665 13 $FILTER_MAP->{encode}->{uuencode} = $FILTER_MAP->{encode}->{uu};
666 13 $FILTER_MAP->{decode}->{uuencode} = $FILTER_MAP->{decode}->{uu};
667 # deflate <-> inflate, make the choice of word irrelevant
668 13 $FILTER_MAP->{decode}->{deflate} = $FILTER_MAP->{decode}->{inflate};
669 13 $FILTER_MAP->{encode}->{inflate} = $FILTER_MAP->{encode}->{deflate};
670 13 $FILTER_MAP->{decode}->{rawdeflate} = $FILTER_MAP->{decode}->{rawinflate};
671 13 $FILTER_MAP->{encode}->{rawinflate} = $FILTER_MAP->{encode}->{rawdeflate};
672 13 $FILTER_MAP->{encode}->{ 'x-zip' } = $FILTER_MAP->{encode}->{zip};
673 13 $FILTER_MAP->{decode}->{ 'x-zip' } = $FILTER_MAP->{decode}->{zip};
674 # x-compress was used for LZW compression (the algorithm used in GIF),
675 # but is not actually used. There is a module Compress::LZW, but what is the point?
676 13 $FILTER_MAP->{encode}->{ 'quoted-printable' } = $FILTER_MAP->{encode}->{qp};
677 13 $FILTER_MAP->{decode}->{ 'quoted-printable' } = $FILTER_MAP->{decode}->{qp};
678  
679 13 $ENCODING_SUFFIX =
680 {
681 base64 => 'b64',
682 brotli => 'br',
683 bzip2 => 'bz2',
684 # See rfc1950
685 # <https://fileinfo.com/extension/zz#pigz_zlib_compressed_file>
686 deflate => 'zz',
687 gzip => 'gz',
688 lzf => 'lzf',
689 # <https://fileinfo.com/extension/lz>
690 lzip => 'lz',
691 # <https://fileinfo.com/extension/lzma>
692 lzma => 'lzma',
693 lzop => 'lzop',
694 lzw => 'lzw',
695 qp => 'qp',
696 rawdeflate => 'rzz',
697 uu => 'uu',
698 xz => 'xz',
699 zip => 'zip',
700 zstd => 'zst',
701 };
702  
703 13 $ENCODING_ALIAS =
704 {
705 br => 'brotli',
706 'x-gzip' => 'gzip',
707 'x-bzip2' => 'bzip2',
708 'x-compress' => 'lzw',
709 'x-uuencode' => 'uu',
710 'x-zip' => 'zip',
711 'compress' => 'lzw',
712 'inflate' => 'deflate',
713 'quoted-printable' => 'qp',
714 'rawinflate' => 'rawdeflate',
715 'uuencode' => 'uu',
716 };
717  
718 13 $MIME_ENCODING =
719 {
720 'application/gzip' => 'gzip',
721 'application/x-gzip' => 'gzip',
722 'application/x-bzip2' => 'bzip2',
723 'application/x-xz' => 'xz',
724 'application/zstd' => 'zstd',
725 'application/x-compress' => 'lzw',
726 'application/x-lzma' => 'lzma',
727 'application/x-lzip' => 'lzip',
728 'application/x-lzop' => 'lzop',
729 'application/x-lzf' => 'lzf',
730 };
731 }
732  
733 sub init
734 {
735 95 ▶ my $self = shift( @_ );
Subroutine init: called CC=17 SCAR=30.4
Pod documented
736 95 my $src = shift( @_ );
737 13 ▶ no warnings 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
738 95 ▶ return( $self->error( "No stream was provided." ) ) if( !defined( $src ) || !length( $src ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $src and length $src 0 95 95
Condition33%1/3
defined $src and length $src
!ll&&!rl&&r
0095
MC/DC0%0/2
defined $src and length $src
defined $src length $src
Truth table33%1/3
A and B
A= defined $srcB= length $src
ABresult
0X0
100
111
739 95 ▶ my $type = ref( $src ) ? lc( Scalar::Util::reftype( $src ) ) : '';
Branch: 1 branch
BranchTrueFalseTotal
ref $src ? : 93 2 95
740 95 ▶ if( ref( $src ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $src) { } 93 2 95
741 {
742 93 ▶ if( $self->_is_a( $src => 'Module::Generic::File' ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_a($src, 'Module::Generic::File')) { } 47 46 93
elsif ($type ne 'scalar' and $type ne 'glob' and $type ne 'code') { } 0 92 92
743 {
744 47 $src = "$src";
745 }
746 ▶ elsif( $type ne 'scalar' && $type ne 'glob' && $type ne 'code' )
Condition50%3/6
$type ne "scalar" and $type ne "glob" and $type ne "code"
!ll&&!rl&&r
4600
$type ne "scalar" and $type ne "glob"
!ll&&!rl&&r
4150
MC/DC0%0/3
$type ne "scalar" and $type ne "glob" and $type ne "code"
$type ne "scalar" $type ne "glob" $type ne "code"
Truth table50%2/4
A and B and C
A= $type ne "scalar"B= $type ne "glob"C= $type ne "code"
ABCresult
0XX0
10X0
1100
1111
747 {
748 0 return( $self->error( "You can only provide a scalar reference, array reference, code reference or a glob as a reference element for the filter." ) );
749 }
750 }
751 else
752 {
753 2 ▶ if( $src =~ /\n/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($src =~ /\n/) 0 2 2
754 {
755 0 return( $self->error( "You cannot provide a text to set the filter. It can only be a scalar reference, array reference, a glob or a file path." ) );
756 }
757 }
758 95 $self->{compress_params} = {};
759 95 $self->{encoding} = undef;
760 95 $self->{decoding} = undef;
761 95 $self->{_init_strict_use_sub} = 1;
762 95 $self->{_exception_class} = 'HTTP::Promise::Exception';
763 95 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 95 95
764 95 $self->source( $src );
765 95 $self->{read_tmp_file} = undef;
766 95 $self->{src_file_handle} = undef;
767 95 ▶ if( defined( $self->{encoding} ) && length( $self->{encoding} ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $self->{'encoding'} and length $self->{'encoding'}) { } 27 68 95
elsif (defined $self->{'decoding'} and length $self->{'decoding'}) { } 44 48 92
Condition66%2/3
defined $self->{"encoding"} and length $self->{"encoding"}
!ll&&!rl&&r
68027
MC/DC50%1/2
defined $self->{"encoding"} and length $self->{"encoding"}
defined $self->{"encoding"} length $self->{"encoding"}
Truth table66%2/3
A and B
A= defined $self->{"encoding"}B= length $self->{"encoding"}
ABresult
0X0
100
111
768 {
769 27 ▶ return( $self->error( "Encoding provided \"$self->{encoding}\" is unsupported." ) ) if( !exists( $FILTER_MAP->{encode}->{ $self->{encoding} } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $HTTP::Promise::Stream::FILTER_MAP->{"encode"}{$self->{"encoding"}} 0 27 27
770 }
771 ▶ elsif( defined( $self->{decoding} ) && length( $self->{decoding} ) )
Condition66%2/3
defined $self->{"decoding"} and length $self->{"decoding"}
!ll&&!rl&&r
24044
MC/DC50%1/2
defined $self->{"decoding"} and length $self->{"decoding"}
defined $self->{"decoding"} length $self->{"decoding"}
Truth table66%2/3
A and B
A= defined $self->{"decoding"}B= length $self->{"decoding"}
ABresult
0X0
100
111
772 {
773 44 ▶ return( $self->error( "Decoding provided \"$self->{decoding}\" is unsupported." ) ) if( !exists( $FILTER_MAP->{decode}->{ $self->{decoding} } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $HTTP::Promise::Stream::FILTER_MAP->{"decode"}{$self->{"decoding"}} 0 44 44
774 }
775 95 return( $self );
776 }
777  
778 sub as_string
779 {
780 0 ▶ my $self = shift( @_ );
Subroutine as_string: not called CC=15 SCAR=54.8
Pod documented
781 0 my $src = $self->source;
782 0 ▶ if( ref( $src ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $src) { } 0 0 0
783 {
784 0 my $type = lc( Scalar::Util::reftype( $src ) );
785 0 ▶ if( $type eq 'scalar' )
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'scalar') { } 0 0 0
elsif ($type eq 'glob') { } 0 0 0
elsif ($self->_is_a($src, 'Module::Generic::File')) { } 0 0 0
786 {
787 0 return( ${$src} );
788 }
789 elsif( $type eq 'glob' )
790 {
791 0 ▶ if( $self->_is_a( $src => 'Module::Generic::Scalar::IO' ) )
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_is_a($src, 'Module::Generic::Scalar::IO')) { } 0 0 0
elsif ($self->_is_object($src) and $self->_can($src, 'seek') and $self->_can($src, 'read')) { } 0 0 0
elsif (fileno $src) { } 0 0 0
792 {
793 0 return( join( '', $src->getlines ) );
794 }
795 ▶ elsif( $self->_is_object( $src ) && $self->_can( $src => 'seek' ) && $self->_can( $src => 'read' ) )
Condition0%0/6
$self->_is_object($src) and $self->_can($src, "seek") and $self->_can($src, "read")
!ll&&!rl&&r
000
$self->_is_object($src) and $self->_can($src, "seek")
!ll&&!rl&&r
000
MC/DC0%0/3
$self->_is_object($src) and $self->_can($src, "seek") and $self->_can($src, "read")
$self->_is_object($src) $self->_can($src, "seek") $self->_can($src, "read")
Truth table0%0/4
A and B and C
A= $self->_is_object($src)B= $self->_can($src, "seek")C= $self->_can($src, "read")
ABCresult
0XX0
10X0
1100
1111
796 {
797 0 my $data = '';
798 0 ▶ $src->seek(0,0) || return( $self->error( "Unable to seek source stream glob: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
$src->seek(0, 0) or return $self->error("Unable to seek source stream glob: $!") 0 0 0
799 0 ▶ while( $src->read( my $buff, 10240 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($src->read(my $buff, 10240)) 0 0 0
800 {
801 0 $data .= $buff;
802 }
803 0 return( $data );
804 }
805 elsif( fileno( $src ) )
806 {
807 0 my $data = '';
808 0 ▶ CORE::seek( $src, 0, 0 ) || return( $self->error( "Unable to seek source stream glob: $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
seek $src, 0, 0 or return $self->error("Unable to seek source stream glob: $!") 0 0 0
809 0 ▶ while( CORE::read( $src, my $buff, 10240 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::read $src, my $buff, 10240) 0 0 0
810 {
811 0 $data .= $buff;
812 }
813 0 return( $data );
814 }
815 }
816 elsif( $self->_is_a( $src => 'Module::Generic::File' ) )
817 {
818 0 return( $src->content );
819 }
820 0 return;
821 }
822 else
823 {
824 0 ▶ my $f = $self->new_file( $src ) || return( $self->pass_error );
Condition0%0/2
$self->new_file($src) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_file($src) || (return $self->pass_error)
$self->new_file($src)
Truth table0%0/2
A
A= $self->new_file($src)
Aresult
11
00
825 0 return( $f->content );
826 }
827 }
828  
829 95 ▶sub compress_params { return( shift->_set_get_hash_as_mix_object( 'compress_params', @_ ) ); }
Subroutine compress_params: called CC=1 SCAR=0.0
Pod documented
830  
831 1 ▶sub decodable { return( shift->_decodable_encodable( 0, @_ ) ); }
Subroutine decodable: called CC=1 SCAR=0.0
Pod documented
832  
833 # Decoding $data and writing to stream:
834 # $stream->decode( $data );
835 # Decoding stream and returning decoded data:
836 # my $decoded = $stream->decode;
837 sub decode
838 {
839 12 ▶ my( $self ) = @_;
Subroutine decode: called CC=16 SCAR=37.0
Pod documented
840 12 my $opts = {};
841 12 ▶ $opts = pop( @_ ) if( ref( $_[-1] ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "HASH" 12 0 12
842 12 ▶ $opts->{encoding} //= '';
Condition50%1/2
$opts->{"encoding"} //= ""
l!l
120
MC/DC0%0/1
$opts->{"encoding"} //= ""
$opts->{"encoding"}
Truth table50%1/2
A
A= $opts->{"encoding"}
Aresult
11
00
843 12 ▶ $opts->{decoding} //= '';
Condition50%1/2
$opts->{"decoding"} //= ""
l!l
012
MC/DC0%0/1
$opts->{"decoding"} //= ""
$opts->{"decoding"}
Truth table50%1/2
A
A= $opts->{"decoding"}
Aresult
11
00
844 12 ▶ my $dec = $opts->{decoding} || $opts->{encoding} || $self->decoding->lower;
Condition33%2/6
$opts->{'decoding'} || $opts->{'encoding'} || $self->decoding->lower
l!l&&r!l&&!r
1200
$opts->{"decoding"} or $opts->{"encoding"}
l!l&&r!l&&!r
0120
MC/DC0%0/3
$opts->{'decoding'} || $opts->{'encoding'} || $self->decoding->lower
$opts->{"decoding"} $opts->{"encoding"} $self->decoding->lower
Truth table25%1/4
A or B || C
A= $opts->{"decoding"}B= $opts->{"encoding"}C= $self->decoding->lower
ABCresult
1XX1
01X1
0011
0000
845 12 my $src = $self->source;
846 # Scalar reference or glob
847 12 ▶ my $this = @_ >= 2 ? $_[1] : $src;
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 ? : 0 12 12
848 12 my $size = $self->_get_size( $this );
849
850 # No need to bother going further
851 12 ▶ if( !defined( $dec ) || !length( $dec ) || !$size )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $dec and length $dec and $size) 0 12 12
Condition33%2/6
defined $dec and length $dec and $size
!ll&&!rl&&r
0012
defined $dec and length $dec
!ll&&!rl&&r
0012
MC/DC0%0/3
defined $dec and length $dec and $size
defined $dec length $dec $size
Truth table25%1/4
A and B and C
A= defined $decB= length $decC= $size
ABCresult
0XX0
10X0
1100
1111
852 {
853 # $stream->decode( $data );
854 0 ▶ return( $self ) if( @_ >= 2 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ >= 2 0 0 0
855 # my $decoded = $stream->decode;
856 0 return( '' );
857 }
858 12 my $filters = $FILTER_MAP->{decode};
859 12 ▶ return( $self->error( "Unknown decoding \"$dec\"." ) ) if( !exists( $filters->{ $dec } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $filters->{$dec} 0 12 12
860 12 my $params = $self->_io_compress_params( $opts );
861 12 my $rv;
862 # Decode some data provided and into the stream
863 12 ▶ if( @_ >= 2 )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2) { } 0 12 12
864 {
865 0 ( $rv, my $err ) = $filters->{ $dec }->( $_[0] => $src, %$params );
866 0 ▶ return( $self->error( "Unable to decode $size bytes of data into the stream with $dec: $err" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
867 0 return( $rv );
868 }
869 # Decode the stream and return the decoded data
870 else
871 {
872 12 my $buf;
873 12 ( $rv, my $err ) = $filters->{ $dec }->( $src => \$buf, %$params );
874 12 ▶ return( $self->error( "Unable to decode $size bytes of data from the stream with $dec: $err" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 12 12
875 12 ▶ return( $buf ) if( defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rv 12 0 12
876 0 return( $rv );
877 }
878 }
879  
880 115 ▶sub decoding { return( shift->_set_get_scalar_as_object( 'decoding', @_ ) ); }
Subroutine decoding: called CC=1 SCAR=0.0
Pod documented
881  
882 0 ▶sub encodable { return( shift->_decodable_encodable( 1, @_ ) ); }
Subroutine encodable: not called CC=1 SCAR=6.9
Pod documented
883  
884 # Encoding $data and writing to stream:
885 # $stream->encode( $data );
886 # Encoding stream and returning decoded data:
887 # my $encoded = $stream->encode;
888 sub encode
889 {
890 12 ▶ my( $self ) = @_;
Subroutine encode: called CC=14 SCAR=34.4
Pod documented
891 12 my $opts = {};
892 12 ▶ $opts = pop( @_ ) if( ref( $_[-1] ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "HASH" 12 0 12
893 12 ▶ $opts->{encoding} //= '';
Condition50%1/2
$opts->{"encoding"} //= ""
l!l
120
MC/DC0%0/1
$opts->{"encoding"} //= ""
$opts->{"encoding"}
Truth table50%1/2
A
A= $opts->{"encoding"}
Aresult
11
00
894 12 ▶ my $enc = $opts->{encoding} || $self->encoding->lower;
Condition33%1/3
$opts->{'encoding'} || $self->encoding->lower
l!l&&r!l&&!r
1200
MC/DC0%0/2
$opts->{'encoding'} || $self->encoding->lower
$opts->{'encoding'} $self->encoding->lower
Truth table33%1/3
A || B
A= $opts->{'encoding'}B= $self->encoding->lower
ABresult
1X1
011
000
895 12 my $src = $self->source;
896 # Scalar reference or glob
897 12 ▶ my $this = @_ >= 2 ? $_[1] : $src;
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 2 ? : 0 12 12
898 12 my $size = $self->_get_size( $this );
899
900 # No need to bother going further
901 12 ▶ if( !defined( $enc ) || !length( $enc ) || !$size )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $enc and length $enc and $size) 0 12 12
Condition33%2/6
defined $enc and length $enc and $size
!ll&&!rl&&r
0012
defined $enc and length $enc
!ll&&!rl&&r
0012
MC/DC0%0/3
defined $enc and length $enc and $size
defined $enc length $enc $size
Truth table25%1/4
A and B and C
A= defined $encB= length $encC= $size
ABCresult
0XX0
10X0
1100
1111
902 {
903 # $stream->encode( $data );
904 0 ▶ return( $self ) if( @_ >= 2 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ >= 2 0 0 0
905 # my $encoded = $stream->encode;
906 0 return( '' );
907 }
908 12 my $filters = $FILTER_MAP->{encode};
909 12 ▶ return( $self->error( "Unknown encoding \"$enc\". Supported encodings are: ", join( ', ', sort( keys( %$filters ) ) ) ) ) if( !exists( $filters->{ $enc } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $filters->{$enc} 0 12 12
910 12 my $params = $self->_io_compress_params( $opts );
911 12 my $rv;
912 # Encode some data provided and into the stream
913 12 ▶ if( @_ >= 2 )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ >= 2) { } 0 12 12
914 {
915 0 ( $rv, my $err ) = $filters->{ $enc }->( $_[0] => $src, %$params );
916 0 ▶ return( $self->error( "Unable to encode $size bytes of data into the stream with $enc: $err" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
917 0 return( $rv );
918 }
919 # Encode the stream and return the decoded data
920 else
921 {
922 12 my $buf;
923 12 my $ref = \$buf;
924 12 ( $rv, my $err ) = $filters->{ $enc }->( $src => \$buf, %$params );
925 12 ▶ return( $self->error( "Unable to encode $size bytes of data from the stream with $enc: $err" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 12 12
926 12 ▶ return( $buf ) if( defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $rv 12 0 12
927 0 return( $rv );
928 }
929 }
930  
931 98 ▶sub encoding { return( shift->_set_get_scalar_as_object( 'encoding', @_ ) ); }
Subroutine encoding: called CC=1 SCAR=0.0
Pod documented
932  
933 sub encoding2suffix
934 {
935 16 ▶ my $self = shift( @_ );
Subroutine encoding2suffix: called CC=9 SCAR=25.4
Pod documented
936 16 my $this = shift( @_ );
937 16 ▶ return( $self->error( "Bad argument provided. encoding2suffix() takes either an array of encodings or a string or something that stringifies." ) ) if( !defined( $this ) || ( !$self->_is_array( $this ) && ( ref( $this ) && !overload::Method( $this => '""' ) ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if not defined $this or not $self->_is_array($this) and (ref $this and not overload::Method($this, "\"\"")) 0 16 16
Condition22%2/9
not defined $this or not $self->_is_array($this) and (ref $this and not overload::Method($this, "\"\""))
l!l&&r!l&&!r
0016
not $self->_is_array($this) and (ref $this and not overload::Method($this, "\"\""))
!ll&&!rl&&r
1600
ref $this and not overload::Method($this, "\"\"")
!ll&&!rl&&r
000
MC/DC0%0/4
not defined $this or not $self->_is_array($this) and (ref $this and not overload::Method($this, "\"\""))
not defined $this not $self->_is_array($this) ref $this not overload::Method($this, "\"\"")
Truth table0%0/5
not(A) or not(B) and C and not(D)
A= not defined $thisB= not $self->_is_array($this)C= ref $thisD= not overload::Method($this, "\"\"")
ABCDresult
1XXX1
01111
00XX0
010X0
01100
938 16 ▶ my $encodings = $self->new_array( $self->_is_array( $this ) ? $this : [split( /[[:blank:]\h]*,[[:blank:]\h]*/, lc( "${this}" ) )] );
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_array($this) ? : 16 0 16
939 16 my $ext = $self->new_array;
940 16 foreach( @$encodings )
941 {
942 17 my $enc;
943 17 ▶ $enc = exists( $ENCODING_ALIAS->{ $_ } ) ? $ENCODING_ALIAS->{ $_ } : $_;
Branch: 1 branch
BranchTrueFalseTotal
exists $HTTP::Promise::Stream::ENCODING_ALIAS->{$_} ? : 0 17 17
944 17 ▶ return( $self->error( "Unknown encoding provided \"${enc}\"." ) ) if( !exists( $ENCODING_SUFFIX->{ $enc } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $HTTP::Promise::Stream::ENCODING_SUFFIX->{$enc} 1 16 17
945 16 $ext->push( $ENCODING_SUFFIX->{ $enc } );
946 }
947 15 return( $ext );
948 }
949  
950 sub load
951 {
952 0 ▶ my $self = shift( @_ );
Subroutine load: not called CC=10 SCAR=47.0
Pod documented
953 0 ▶ my $enc = shift( @_ ) || return( $self->error( "No encoding was provided." ) );
Condition0%0/2
shift @_ || (return $self->error('No encoding was provided.'))
l!l
00
MC/DC0%0/1
shift @_ || (return $self->error('No encoding was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
Aresult
11
00
954 0 $enc = lc( $enc );
955 0 ▶ return(0) if( !exists( $CLASSES->{ $enc } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $HTTP::Promise::Stream::CLASSES->{$enc} 0 0 0
956 0 my $opts = $self->_get_args_as_hash( @_ );
957 0 my $p = {};
958 0 ▶ $p->{version} = $opts->{version} if( exists( $opts->{version} ) && length( $opts->{version} ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $opts->{"version"} and length $opts->{"version"} 0 0 0
Condition0%0/3
exists $opts->{"version"} and length $opts->{"version"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $opts->{"version"} and length $opts->{"version"}
exists $opts->{"version"} length $opts->{"version"}
Truth table0%0/3
A and B
A= exists $opts->{"version"}B= length $opts->{"version"}
ABresult
0X0
100
111
959 0 my( $encoder, $decoder ) = @{$CLASSES->{ $enc }};
960 0 my $ok = 0;
961 0 for( $encoder, $decoder )
962 {
963 0 ▶ $ok++, next if( $_ eq $decoder && $decoder eq $encoder );
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq $decoder and $decoder eq $encoder 0 0 0
Condition0%0/3
$_ eq $decoder and $decoder eq $encoder
!ll&&!rl&&r
000
MC/DC0%0/2
$_ eq $decoder and $decoder eq $encoder
$_ eq $decoder $decoder eq $encoder
Truth table0%0/3
A and B
A= $_ eq $decoderB= $decoder eq $encoder
ABresult
0X0
100
111
964 0 ▶ $self->_load_class( $_, $p ) || next;
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class($_, $p) or next 0 0 0
965 0 $ok++;
966 }
967 0 ▶ return(1) if( $ok == 2 );
Branch: 1 branch
BranchTrueFalseTotal
if $ok == 2 0 0 0
968 0 return(0);
969 }
970  
971 sub mime2encoding
972 {
973 11 ▶ my $self = shift( @_ );
Subroutine mime2encoding: called CC=3 SCAR=11.0
Pod undocumented
974 11 ▶ my $mime = shift( @_ ) || return( $self->error( "No MIME type was provided." ) );
Condition100%2/2
shift @_ || (return $self->error('No MIME type was provided.'))
l!l
101
MC/DC100%1/1
shift @_ || (return $self->error('No MIME type was provided.'))
shift @_
Truth table100%2/2
A
A= shift @_
Aresult
11
00
975 10 $mime = lc( "${mime}" );
976 # Strip parameters like '; charset=binary'
977 10 ▶ $mime =~ s/[[:blank:]]*;.*$// if( index( $mime, ';' ) != -1 );
Branch: 1 branch
BranchTrueFalseTotal
if (index($mime, ";") != -1) 1 9 10
978 10 return( $MIME_ENCODING->{ $mime } );
979 }
980  
981 # $stream->read( $buffer, $len, $offset );
982 # $stream->read( $buffer, $len );
983 # $stream->read( $buffer );
984 # $stream->read( *buffer );
985 # $stream->read( sub{} );
986 # $stream->read( \$buffer );
987 # $stream->read( '/some/where/file.txt' );
988 sub read
989 {
990 51 ▶ my( $self ) = @_;
Subroutine read: called CC=63 SCAR=60.4
Pod documented
991 51 my $opts = {};
992 51 ▶ $opts = pop( @_ ) if( ref( $_[-1] ) eq 'HASH' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] eq "HASH" 4 47 51
993 51 ▶ $opts->{binmode} = 'raw' if( !exists( $opts->{binmode} ) || !length( $opts->{binmode} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"binmode"} and length $opts->{"binmode"} 51 0 51
Condition33%1/3
exists $opts->{"binmode"} and length $opts->{"binmode"}
!ll&&!rl&&r
5100
MC/DC0%0/2
exists $opts->{"binmode"} and length $opts->{"binmode"}
exists $opts->{"binmode"} length $opts->{"binmode"}
Truth table33%1/3
A and B
A= exists $opts->{"binmode"}B= length $opts->{"binmode"}
ABresult
0X0
100
111
994 51 my $src = $self->source;
995 51 ▶ my $enc = $self->encoding->lower || lc( $opts->{encoding} );
Condition66%2/3
$self->encoding->lower || lc $opts->{'encoding'}
l!l&&r!l&&!r
7044
MC/DC50%1/2
$self->encoding->lower || lc $opts->{'encoding'}
$self->encoding->lower lc $opts->{'encoding'}
Truth table66%2/3
A || B
A= $self->encoding->lowerB= lc $opts->{'encoding'}
ABresult
1X1
011
000
996 51 ▶ my $dec = $self->decoding->lower || lc( $opts->{decoding} );
Condition66%2/3
$self->decoding->lower || lc $opts->{'decoding'}
l!l&&r!l&&!r
4407
MC/DC50%1/2
$self->decoding->lower || lc $opts->{'decoding'}
$self->decoding->lower lc $opts->{'decoding'}
Truth table66%2/3
A || B
A= $self->decoding->lowerB= lc $opts->{'decoding'}
ABresult
1X1
011
000
997 51 my $io = $self->{src_file_handle};
998 51 my $tempfile = $self->{read_tmp_file};
999 51 ▶ unless( $io )
Branch: 1 branch
BranchTrueFalseTotal
unless ($io) 51 0 51
1000 {
1001 51 ▶ $tempfile = $self->{read_tmp_file} = $self->new_tempfile ||
Condition50%1/2
$self->new_tempfile || (return $self->error('Unable to get a new tempfile: ', $self->error))
l!l
510
MC/DC0%0/1
$self->new_tempfile || (return $self->error('Unable to get a new tempfile: ', $self->error))
$self->new_tempfile
Truth table50%1/2
A
A= $self->new_tempfile
Aresult
11
00
1002 return( $self->error( "Unable to get a new tempfile: ", $self->error ) );
1003 51 ▶ if( $enc )
Branch: 2 branches
BranchTrueFalseTotal
if ($enc) { } 7 44 51
elsif ($dec) { } 44 0 44
1004 {
1005 7 my $params = $self->_io_compress_params( $opts );
1006 7 my $filters = $FILTER_MAP->{encode};
1007 7 ▶ return( $self->error( "Unknown encoding \"$enc\"." ) ) if( !exists( $filters->{ $enc } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $filters->{$enc} 0 7 7
1008 7 my( $rv, $err ) = $filters->{ $enc }->( $self->_normalise( $src ) => "$tempfile", %$params );
1009 7 my $size = $self->_get_size( $src );
1010 7 ▶ return( $self->error( "Unable to encode $size bytes of data into the stream with $enc: $err" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 7 7
1011 7 ▶ $io = $self->{src_file_handle} = $tempfile->open( '<', { binmode => $opts->{binmode} }) ||
Condition50%1/2
$tempfile->open('<', {'binmode', $opts->{'binmode'}}) || (return $self->pass_error($tempfile))
l!l
70
MC/DC0%0/1
$tempfile->open('<', {'binmode', $opts->{'binmode'}}) || (return $self->pass_error($tempfile))
$tempfile->open('<', {'binmode', $opts->{'binmode'}})
Truth table50%1/2
A
A= $tempfile->open('<', {'binmode', $opts->{'binmode'}})
Aresult
11
00
1012 return( $self->pass_error( $tempfile ) );
1013 }
1014 elsif( $dec )
1015 {
1016 44 my $params = $self->_io_compress_params( $opts );
1017 44 my $filters = $FILTER_MAP->{decode};
1018 44 ▶ return( $self->error( "Unknown decoding \"$dec\"." ) ) if( !exists( $filters->{ $dec } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $filters->{$dec} 0 44 44
1019 # $err, if defined, would be a HTTP::Promise::Exception object
1020 44 my( $rv, $err ) = $filters->{ $dec }->( $self->_normalise( $src ) => "$tempfile", %$params );
1021 44 my $size = $self->_get_size( $src );
1022 44 ▶ return( $self->error( "Unable to decode $size bytes of data into the stream with $dec and input '", $self->_normalise( $src ), "' and output '", $tempfile, "': $err" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 44 44
1023 44 ▶ $io = $self->{src_file_handle} = $tempfile->open( '<', { binmode => $opts->{binmode} }) ||
Condition50%1/2
$tempfile->open('<', {'binmode', $opts->{'binmode'}}) || (return $self->pass_error($tempfile))
l!l
440
MC/DC0%0/1
$tempfile->open('<', {'binmode', $opts->{'binmode'}}) || (return $self->pass_error($tempfile))
$tempfile->open('<', {'binmode', $opts->{'binmode'}})
Truth table50%1/2
A
A= $tempfile->open('<', {'binmode', $opts->{'binmode'}})
Aresult
11
00
1024 return( $self->pass_error( $tempfile ) );
1025 }
1026 else
1027 {
1028 0 my $type = lc( Scalar::Util::reftype( $src ) );
1029 0 ▶ if( $type eq 'scalar' )
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'scalar') { } 0 0 0
elsif ($type eq 'glob') { } 0 0 0
elsif (not ref $src) { } 0 0 0
1030 {
1031 0 my $s = $self->new_scalar( $src );
1032 0 ▶ $io = $self->{src_file_handle} = $s->open( '<' ) ||
Condition0%0/2
$s->open('<') || (return $self->pass_error($s->error))
l!l
00
MC/DC0%0/1
$s->open('<') || (return $self->pass_error($s->error))
$s->open('<')
Truth table0%0/2
A
A= $s->open('<')
Aresult
11
00
1033 return( $self->pass_error( $s->error ) );
1034 }
1035 elsif( $type eq 'glob' )
1036 {
1037 0 $io = $self->{src_file_handle} = $src;
1038 }
1039 elsif( !ref( $src ) )
1040 {
1041 0 my $f = $self->new_file( $src );
1042 0 ▶ $io = $self->{src_file_handle} = $f->open( '<', { $opts->{binmode} ? ( binmode => $opts->{binmode} ) : () }) ||
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'binmode'} ? : 0 0 0
Condition0%0/2
$f->open('<', {$opts->{'binmode'} ? ('binmode', $opts->{'binmode'}) : ()}) || (return $self->pass_error($f->error))
l!l
00
MC/DC0%0/1
$f->open('<', {$opts->{'binmode'} ? ('binmode', $opts->{'binmode'}) : ()}) || (return $self->pass_error($f->error))
$f->open('<', {$opts->{'binmode'} ? ('binmode', $opts->{'binmode'}) : ()})
Truth table0%0/2
A
A= $f->open('<', {$opts->{'binmode'} ? ('binmode', $opts->{'binmode'}) : ()})
Aresult
11
00
1043 return( $self->pass_error( $f->error ) );
1044 }
1045 else
1046 {
1047 0 return( $self->error( "I do not know how to handle source '$src'." ) );
1048 }
1049 }
1050 }
1051
1052 51 my $len;
1053 51 ▶ if( ref( $_[1] ) eq 'CODE' )
Branch: 4 branches
BranchTrueFalseTotal
if (ref $_[1] eq 'CODE') { } 4 47 51
elsif (builtin::reftype($_[1]) eq 'SCALAR') { } 33 14 47
elsif (builtin::reftype($_[1]) eq 'GLOB') { } 4 10 14
elsif ($self->_is_a($_[1], 'Module::Generic::File') or not ref $_[1] and length $_[1] and index($_[1], "\n") == -1) { } 6 4 10
1054 {
1055 4 my $buf;
1056 # Because there is no buffer provided and we send the data chunk to a callback, the
1057 # offset option of the read() function is useless
1058 4 ▶ if( @_ >= 3 )
Branch: 2 branches
BranchTrueFalseTotal
if (@_ >= 3) { } 0 4 4
elsif (@_ >= 2) { } 4 0 4
1059 {
1060 0 $len = $io->read( $buf, $_[2] );
1061 0 ▶ return( $self->error( "Unable to read ", $_[2], " bytes from source: $!" ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
1062 }
1063 elsif( @_ >= 2 )
1064 {
1065 4 $len = $io->read( $buf, $tempfile->length );
1066 4 ▶ return( $self->error( "Unable to read bytes from source: $!" ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 4 4
1067 }
1068
1069 # try-catch
1070 4 local $@;
1071 eval
1072 4 {
1073 4 $_[1]->( $buf );
1074 };
1075 4 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
1076 {
1077 0 return( $self->error( "Callback raised an exception when sending it the ", length( $buf ), " bytes of data read from source: $@" ) );
1078 }
1079 }
1080 elsif( Scalar::Util::reftype( $_[1] ) eq 'SCALAR' )
1081 {
1082 33 ▶ if( @_ >= 4 )
Branch: 3 branches
BranchTrueFalseTotal
if (@_ >= 4) { } 0 33 33
elsif (@_ >= 3) { } 0 33 33
elsif (@_ >= 2) { } 33 0 33
1083 {
1084 0 $len = $io->read( ${$_[1]}, $_[2], $_[3] );
1085 0 ▶ return( $self->error( "Unable to read ", $_[2], " bytes at offset ", $_[3], " from source: $!" ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
1086 }
1087 elsif( @_ >= 3 )
1088 {
1089 0 $len = $io->read( ${$_[1]}, $_[2] );
1090 0 ▶ return( $self->error( "Unable to read ", $_[2], " bytes from source: $!" ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
1091 }
1092 elsif( @_ >= 2 )
1093 {
1094 33 $len = $io->read( ${$_[1]}, $tempfile->length );
1095 33 ▶ return( $self->error( "Unable to read bytes from source: $!" ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 33 33
1096 }
1097 }
1098 elsif( Scalar::Util::reftype( $_[1] ) eq 'GLOB' )
1099 {
1100 4 my $buf;
1101 # Because there is no buffer provided and we send the data chunk to a glob, the
1102 # offset option of the read() function is useless
1103 4 ▶ if( @_ >= 3 )
Branch: 2 branches
BranchTrueFalseTotal
if (@_ >= 3) { } 0 4 4
elsif (@_ >= 2) { } 4 0 4
1104 {
1105 0 $len = $io->read( $buf, $_[2] );
1106 0 ▶ return( $self->error( "Unable to read ", $_[2], " bytes from source: $!" ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
1107 0 my $rv = CORE::print( $_[1], $buf );
1108 0 ▶ return( $self->error( "Unable to print ", CORE::length( $buf ), " bytes of data to provided file handle '", $_[1], "': $!" ) ) if( !$rv );
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 0 0
1109 }
1110 elsif( @_ >= 2 )
1111 {
1112 4 my $chunklen;
1113 4 ▶ while( $chunklen = $io->read( $buf, 10240 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($chunklen = $io->read($buf, 10240)) 4 4 8
1114 {
1115 4 $len += $chunklen;
1116 #my $rv = CORE::print( $_[1], $buf );
1117 4 my $rv = $_[1]->print( $buf );
1118 4 ▶ return( $self->error( "Unable to print ", CORE::length( $buf ), " bytes of data to provided file handle '", $_[1], "': $!" ) ) if( !$rv );
Branch: 1 branch
BranchTrueFalseTotal
unless $rv 0 4 4
1119 }
1120 4 ▶ return( $self->error( "Unable to read bytes from source: $!" ) ) if( !defined( $chunklen ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chunklen 0 4 4
1121 }
1122 }
1123 # A file
1124 ▶ elsif( $self->_is_a( $_[1] => 'Module::Generic::File' ) ||
Condition44%4/9
$self->_is_a($_[1], "Module::Generic::File") or not ref $_[1] and length $_[1] and index($_[1], "\n") == -1
l!l&&r!l&&!r
604
not ref $_[1] and length $_[1] and index($_[1], "\n") == -1
!ll&&!rl&&r
400
not ref $_[1] and length $_[1]
!ll&&!rl&&r
040
MC/DC25%1/4
$self->_is_a($_[1], "Module::Generic::File") or not ref $_[1] and length $_[1] and index($_[1], "\n") == -1
$self->_is_a($_[1], "Module::Generic::File") not ref $_[1] length $_[1] index($_[1], "\n") == -1
Truth table40%2/5
A or not(B) and C and D
A= $self->_is_a($_[1], "Module::Generic::File")B= not ref $_[1]C= length $_[1]D= index($_[1], "\n") == -1
ABCDresult
1XXX1
01111
00XX0
010X0
01100
1125 ( !ref( $_[1] ) &&
1126 CORE::length( $_[1] ) &&
1127 CORE::index( $_[1], "\n" ) == -1
1128 ) )
1129 {
1130 6 ▶ my $f = $self->new_file( $_[1] ) || return( $self->pass_error );
Condition50%1/2
$self->new_file($_[1]) || (return $self->pass_error)
l!l
60
MC/DC0%0/1
$self->new_file($_[1]) || (return $self->pass_error)
$self->new_file($_[1])
Truth table50%1/2
A
A= $self->new_file($_[1])
Aresult
11
00
1131 6 my $buf;
1132 # Because there is no buffer provided and we send the data chunk to a file, the
1133 # offset option of the read() function is useless
1134 6 ▶ if( @_ >= 3 )
Branch: 2 branches
BranchTrueFalseTotal
if (@_ >= 3) { } 0 6 6
elsif (@_ >= 2) { } 6 0 6
1135 {
1136 0 $len = $io->read( $buf, $_[2] );
1137 0 ▶ return( $self->error( "Unable to read ", $_[2], " bytes from source: $!" ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
1138 }
1139 elsif( @_ >= 2 )
1140 {
1141 6 $len = $io->read( $buf, $tempfile->length );
1142 6 ▶ return( $self->error( "Unable to read bytes from source: $!" ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 6 6
1143 }
1144 6 ▶ my $mode = $opts->{mode} ? $opts->{mode} : '>';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'mode'} ? : 0 6 6
1145 6 my $params = {};
1146 6 ▶ $params->{binmode} = $opts->{binmode} if( $opts->{binmode} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"binmode"} 6 0 6
1147 6 ▶ $params->{autoflush} = $opts->{autoflush} if( $opts->{autoflush} );
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"autoflush"} 0 6 6
1148 6 ▶ $f->open( $mode, $params ) ||
Branch: 1 branch
BranchTrueFalseTotal
$f->open($mode, $params) or return $self->pass_error($f->error) 0 6 6
1149 return( $self->pass_error( $f->error ) );
1150 6 ▶ $f->print( $buf ) || return( $self->pass_error( $f->error ) );
Branch: 1 branch
BranchTrueFalseTotal
$f->print($buf) or return $self->pass_error($f->error) 0 6 6
1151 6 $f->close;
1152 }
1153 # A regular string
1154 else
1155 {
1156 4 ▶ if( @_ >= 4 )
Branch: 3 branches
BranchTrueFalseTotal
if (@_ >= 4) { } 0 4 4
elsif (@_ >= 3) { } 0 4 4
elsif (@_ >= 2) { } 4 0 4
1157 {
1158 0 $len = $io->read( $_[1], $_[2], $_[3] );
1159 0 ▶ return( $self->error( "Unable to read ", $_[2], " bytes at offset ", $_[3], " from source: $!" ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
1160 }
1161 elsif( @_ >= 3 )
1162 {
1163 0 $len = $io->read( $_[1], $_[2] );
1164 0 ▶ return( $self->error( "Unable to read ", $_[2], " bytes from source: $!" ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 0 0
1165 }
1166 elsif( @_ >= 2 )
1167 {
1168 4 $len = $io->read( $_[1], $tempfile->length );
1169 4 ▶ return( $self->error( "Unable to read bytes from source: $!" ) ) if( !defined( $len ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 0 4 4
1170 }
1171 }
1172 51 return( $len );
1173 }
1174  
1175 190 ▶sub source { return( shift->_set_get_scalar( 'source', @_ ) ); }
Subroutine source: called CC=1 SCAR=0.0
Pod documented
1176  
1177 sub suffix2encoding
1178 {
1179 27 ▶ my $self = shift( @_ );
Subroutine suffix2encoding: called CC=7 SCAR=19.6
Pod documented
1180 27 ▶ my $file = shift( @_ ) || return( $self->pass_error( "No file was provided to guess encoding." ) );
Condition50%1/2
shift @_ || (return $self->pass_error('No file was provided to guess encoding.'))
l!l
270
MC/DC0%0/1
shift @_ || (return $self->pass_error('No file was provided to guess encoding.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
1181 27 my @parts = reverse( split( /\./, $file ) );
1182 27 ▶ unless( defined( $SUFFIX_ENCODING ) && %$SUFFIX_ENCODING )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $HTTP::Promise::Stream::SUFFIX_ENCODING and %$HTTP::Promise::Stream::SUFFIX_ENCODING) 2 25 27
Condition66%2/3
defined $HTTP::Promise::Stream::SUFFIX_ENCODING and %$HTTP::Promise::Stream::SUFFIX_ENCODING
!ll&&!rl&&r
2025
MC/DC50%1/2
defined $HTTP::Promise::Stream::SUFFIX_ENCODING and %$HTTP::Promise::Stream::SUFFIX_ENCODING
defined $HTTP::Promise::Stream::SUFFIX_ENCODING %$HTTP::Promise::Stream::SUFFIX_ENCODING
Truth table66%2/3
A and B
A= defined $HTTP::Promise::Stream::SUFFIX_ENCODINGB= %$HTTP::Promise::Stream::SUFFIX_ENCODING
ABresult
0X0
100
111
1183 {
1184 2 my @keys = keys( %$ENCODING_SUFFIX );
1185 2 my @vals = @$ENCODING_SUFFIX{ @keys };
1186 2 $SUFFIX_ENCODING = {};
1187 2 @$SUFFIX_ENCODING{ @vals } = @keys;
1188 2 ▶ $SUFFIX_ENCODING->{Z} = 'lzw' unless( CORE::exists( $SUFFIX_ENCODING->{Z} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $HTTP::Promise::Stream::SUFFIX_ENCODING->{"Z"} 2 0 2
1189 }
1190 27 my $encs = $self->new_array;
1191 27 foreach( @parts )
1192 {
1193 54 ▶ if( exists( $SUFFIX_ENCODING->{ $_ } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $HTTP::Promise::Stream::SUFFIX_ENCODING->{$_}) { } 27 27 54
1194 {
1195 27 $encs->push( $SUFFIX_ENCODING->{ $_ } );
1196 }
1197 else
1198 {
1199 27 last;
1200 }
1201 }
1202 27 return( $encs->reverse );
1203 }
1204  
1205 sub supported
1206 {
1207 0 ▶ my $self = shift( @_ );
Subroutine supported: not called CC=6 SCAR=37.4
Pod documented
1208 0 ▶ return( $self->error( "No encoding was provided to check if it exists." ) ) if( !@_ || !defined( $_[0] ) || !length( $_[0] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless @_ and defined $_[0] and length $_[0] 0 0 0
Condition0%0/6
@_ and defined $_[0] and length $_[0]
!ll&&!rl&&r
000
@_ and defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
@_ and defined $_[0] and length $_[0]
@_ defined $_[0] length $_[0]
Truth table0%0/4
A and B and C
A= @_B= defined $_[0]C= length $_[0]
ABCresult
0XX0
10X0
1100
1111
1209 0 my $this = lc( shift( @_ ) );
1210 0 ▶ return(1) if( exists( $FILTER_MAP->{encode}->{ $this } ) || exists( $FILTER_MAP->{decode}->{ $this } ) );
Branch: 1 branch
BranchTrueFalseTotal
if exists $HTTP::Promise::Stream::FILTER_MAP->{"encode"}{$this} or exists $HTTP::Promise::Stream::FILTER_MAP->{"decode"}{$this} 0 0 0
Condition0%0/3
exists $HTTP::Promise::Stream::FILTER_MAP->{"encode"}{$this} or exists $HTTP::Promise::Stream::FILTER_MAP->{"decode"}{$this}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $HTTP::Promise::Stream::FILTER_MAP->{"encode"}{$this} or exists $HTTP::Promise::Stream::FILTER_MAP->{"decode"}{$this}
exists $HTTP::Promise::Stream::FILTER_MAP->{"encode"}{$this} exists $HTTP::Promise::Stream::FILTER_MAP->{"decode"}{$this}
Truth table0%0/3
A or B
A= exists $HTTP::Promise::Stream::FILTER_MAP->{"encode"}{$this}B= exists $HTTP::Promise::Stream::FILTER_MAP->{"decode"}{$this}
ABresult
1X1
011
000
1211 0 return(0);
1212 }
1213  
1214 # $stream->write( $data );
1215 # $stream->write( \$data );
1216 # $stream->write( *$data );
1217 # $stream->write( '/some/where/file.txt' );
1218 # $stream->write( sub{} );
1219 sub write
1220 {
1221 20 ▶ my( $self ) = @_;
Subroutine write: called CC=74 SCAR=77.9
Pod documented
1222 # No data was provided
1223 20 ▶ return(0) if( !defined( $_[1] ) || !length( $_[1] ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $_[1] and length $_[1] 0 20 20
Condition33%1/3
defined $_[1] and length $_[1]
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $_[1] and length $_[1]
defined $_[1] length $_[1]
Truth table33%1/3
A and B
A= defined $_[1]B= length $_[1]
ABresult
0X0
100
111
1224 20 my $src = $self->source;
1225 20 my $enc = $self->encoding->lower;
1226 20 my $dec = $self->decoding->lower;
1227 20 my $type = lc( Scalar::Util::reftype( $_[1] ) );
1228 20 my $data;
1229 my $size;
1230 20 my $len;
1231 20 ▶ if( $type eq 'code' )
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'code') { } 4 16 20
1232 {
1233 # try-catch
1234 4 local $@;
1235 my $buf = eval
1236 4 {
1237 4 $_[1]->()
1238 };
1239 4 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 4 4
1240 {
1241 0 return( $self->error( "Error getting data from callback: $@" ) );
1242 }
1243 4 $data = \$buf;
1244 4 $size = length( $$data );
1245 }
1246 else
1247 {
1248 16 $size = $self->_get_size( $_[1] );
1249 # If the data provided is not a reference i.e. a string and it does not have any
1250 # CRLF sequence and it is not a file that exists, OR it has multiple CRLF
1251 # sequences, then we treat it as a string, and to remove ambiguity, we make it a
1252 # scalar reference
1253 16 ▶ if( !ref( $_[1] ) &&
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $_[1] and index($_[1], "\n") == -1 && !(-e $_[1]) || (index($_[1], "\n") != -1)) { } 4 12 16
elsif ($type eq 'scalar') { } 4 8 12
elsif ($self->_is_a($_[1], 'Module::Generic::File') or $self->_can($_[1], 'filename')) { } 4 4 8
Condition44%4/9
not ref $_[1] and index($_[1], "\n") == -1 && !(-e $_[1]) || (index($_[1], "\n") != -1)
!ll&&!rl&&r
1204
index($_[1], "\n") == -1 and not -e $_[1] or (index($_[1], "\n") != -1)
l!l&&r!l&&!r
400
index($_[1], "\n") == -1 and not -e $_[1]
!ll&&!rl&&r
004
MC/DC25%1/4
not ref $_[1] and index($_[1], "\n") == -1 && !(-e $_[1]) || (index($_[1], "\n") != -1)
not ref $_[1] index($_[1], "\n") == -1 not -e $_[1] (index($_[1], "\n") != -1)
Truth table33%2/6
not(A) and B and not(C) or D
A= not ref $_[1]B= index($_[1], "\n") == -1C= not -e $_[1]D= (index($_[1], "\n") != -1)
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1254 (
1255 ( index( $_[1], "\n" ) == -1 && !-e( $_[1] ) ) ||
1256 ( index( $_[1], "\n" ) != -1 )
1257 ) )
1258 {
1259 4 $data = \$_[1];
1260 }
1261 elsif( $type eq 'scalar' )
1262 {
1263 4 $data = $_[1];
1264 }
1265 ▶ elsif( $self->_is_a( $_[1] => 'Module::Generic::File' ) ||
Condition66%2/3
$self->_is_a($_[1], "Module::Generic::File") or $self->_can($_[1], "filename")
l!l&&r!l&&!r
404
MC/DC50%1/2
$self->_is_a($_[1], "Module::Generic::File") or $self->_can($_[1], "filename")
$self->_is_a($_[1], "Module::Generic::File") $self->_can($_[1], "filename")
Truth table66%2/3
A or B
A= $self->_is_a($_[1], "Module::Generic::File")B= $self->_can($_[1], "filename")
ABresult
1X1
011
000
1266 $self->_can( $_[1] => 'filename' ) )
1267 {
1268 4 $data = $_[1]->filename;
1269 }
1270 # otherwise, it is either a scalar reference, a glob or a file, and if it is none
1271 # of those, we return an error
1272 else
1273 {
1274 4 $data = $_[1];
1275 4 ▶ return( $self->error( "Unsupported data type '", overload::StrVal( $data ), "'. You can only provide a string, a scalar reference, a code reference, a glob or a file path." ) ) if( ref( $data ) && $type ne 'scalar' && $type ne 'glob' && $type ne 'code' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $data and $type ne "scalar" and $type ne "glob" and $type ne "code" 0 4 4
Condition33%3/9
ref $data and $type ne "scalar" and $type ne "glob" and $type ne "code"
!ll&&!rl&&r
400
ref $data and $type ne "scalar" and $type ne "glob"
!ll&&!rl&&r
040
ref $data and $type ne "scalar"
!ll&&!rl&&r
004
MC/DC0%0/4
ref $data and $type ne "scalar" and $type ne "glob" and $type ne "code"
ref $data $type ne "scalar" $type ne "glob" $type ne "code"
Truth table20%1/5
A and B and C and D
A= ref $dataB= $type ne "scalar"C= $type ne "glob"D= $type ne "code"
ABCDresult
0XXX0
10XX0
110X0
11100
11111
1276 }
1277
1278 # If we are dealing with a file, open it and use its file glob instead,
1279 # because some encoder like IO::Compress::Zip actually creates and archive of the file with the file path included, rather than just the file content as advertised.
1280 # See Bug #38
1281 # <https://github.com/pmqs/IO-Compress/issues/38>
1282 16 ▶ if( !ref( $data ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $data) 4 12 16
1283 {
1284 4 my $f = $self->new_file( $data );
1285 4 ▶ $data = $f->open( '<', { binmode => 'raw' } ) ||
Condition50%1/2
$f->open('<', {'binmode', 'raw'}) || (return $self->pass_error($f->error))
l!l
40
MC/DC0%0/1
$f->open('<', {'binmode', 'raw'}) || (return $self->pass_error($f->error))
$f->open('<', {'binmode', 'raw'})
Truth table50%1/2
A
A= $f->open('<', {'binmode', 'raw'})
Aresult
11
00
1286 return( $self->pass_error( $f->error ) );
1287 }
1288 }
1289  
1290 20 my $stype = lc( Scalar::Util::reftype( $src ) );
1291 20 ▶ if( $stype eq 'code' )
Branch: 1 branch
BranchTrueFalseTotal
if ($stype eq 'code') { } 0 20 20
1292 {
1293 0 ▶ if( $enc )
Branch: 2 branches
BranchTrueFalseTotal
if ($enc) { } 0 0 0
elsif ($dec) { } 0 0 0
1294 {
1295 0 my $params = $self->_io_compress_params;
1296 # try-catch
1297 0 local $@;
1298 eval
1299 0 {
1300 0 $src->( $self->encode( $data, $params ) );
1301 };
1302 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1303 {
1304 0 return( $self->error( "Error executing calback to write $size bytes of data: $@" ) );
1305 }
1306 0 $len = $size;
1307 }
1308 elsif( $dec )
1309 {
1310 0 my $params = $self->_io_compress_params;
1311 # try-catch
1312 0 local $@;
1313 eval
1314 0 {
1315 0 $src->( $self->decode( $data, $params ) );
1316 };
1317 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1318 {
1319 0 return( $self->error( "Error executing calback to write $size bytes of data: $@" ) );
1320 }
1321 0 $len = $size;
1322 }
1323 else
1324 {
1325 0 ▶ if( $type eq 'scalar' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'scalar') { } 0 0 0
elsif ($type eq 'glob') { } 0 0 0
1326 {
1327 0 $len = length( $$data );
1328 # try-catch
1329 0 local $@;
1330 eval
1331 0 {
1332 0 $src->( $$data );
1333 };
1334 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1335 {
1336 0 return( $self->error( "Error executing calback to write $size bytes of data: $@" ) );
1337 }
1338 }
1339 elsif( $type eq 'glob' )
1340 {
1341 0 my( $rv, $buf );
1342 0 ▶ while( $rv = CORE::read( $data, $buf, 10240 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($rv = CORE::read($data, $buf, 10240)) 0 0 0
1343 {
1344 # try-catch
1345 0 local $@;
1346 eval
1347 0 {
1348 0 $src->( $buf );
1349 };
1350 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1351 {
1352 0 return( $self->error( "Error executing calback to write $size bytes of data: $@" ) );
1353 }
1354 0 $len += length( $buf );
1355 }
1356 0 ▶ return( $self->error( "Unable to read data from glob provided: $!" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
1357 }
1358 else
1359 {
1360 0 ▶ my $f = $self->new_file( $data ) || return( $self->pass_error );
Condition0%0/2
$self->new_file($data) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_file($data) || (return $self->pass_error)
$self->new_file($data)
Truth table0%0/2
A
A= $self->new_file($data)
Aresult
11
00
1361 0 ▶ my $fh = $f->open( '<' ) || return( $self->pass_error( $f->error ) );
Condition0%0/2
$f->open('<') || (return $self->pass_error($f->error))
l!l
00
MC/DC0%0/1
$f->open('<') || (return $self->pass_error($f->error))
$f->open('<')
Truth table0%0/2
A
A= $f->open('<')
Aresult
11
00
1362 0 my $buf;
1363 0 my $rv = $fh->read( $buf );
1364 0 ▶ return( $self->error( "Unable to read data from file \"$f\" provided: $!" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
1365 # try-catch
1366 0 local $@;
1367 eval
1368 0 {
1369 0 $src->( $buf );
1370 };
1371 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1372 {
1373 0 return( $self->error( "Error executing calback to write $size bytes of data: $@" ) );
1374 }
1375 0 $fh->close;
1376 0 $len = length( $buf );
1377 }
1378 }
1379 }
1380 else
1381 {
1382 20 my $filters;
1383 20 ▶ if( $dec )
Branch: 2 branches
BranchTrueFalseTotal
if ($dec) { } 0 20 20
elsif ($enc) { } 20 0 20
1384 {
1385 0 $filters = $FILTER_MAP->{decode};
1386 }
1387 elsif( $enc )
1388 {
1389 20 $filters = $FILTER_MAP->{encode};
1390 }
1391
1392 20 my $rv;
1393 20 ▶ if( $dec )
Branch: 4 branches
BranchTrueFalseTotal
if ($dec) { } 0 20 20
elsif ($enc) { } 20 0 20
elsif ($stype eq 'scalar') { } 0 0 0
elsif ($stype eq 'glob') { } 0 0 0
1394 {
1395 0 my $params = $self->_io_compress_params;
1396 0 ▶ return( $self->error( "No encoding found for \"$dec\"." ) ) if( !exists( $filters->{ $dec } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $filters->{$dec} 0 0 0
1397 # try-catch
1398 0 local $@;
1399 ( $rv, my $err ) = eval
1400 0 {
1401 0 $filters->{ $dec }->( $data => $src, %$params );
1402 };
1403 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1404 {
1405 0 ▶ return( $self->error( "Error ", ( $self->encode ? 'encoding' : 'decoding' ), " $size bytes of data: $@" ) );
Branch: 1 branch
BranchTrueFalseTotal
$self->encode ? : 0 0 0
1406 }
1407 0 ▶ return( $self->error( "Unable to decode data to write to source: $err" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
1408 0 $len = $size;
1409 }
1410 elsif( $enc )
1411 {
1412 20 my $params = $self->_io_compress_params;
1413 20 ▶ return( $self->error( "No encoding found for \"$enc\"." ) ) if( !exists( $filters->{ $enc } ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $filters->{$enc} 0 20 20
1414 # try-catch
1415 20 local $@;
1416 ( $rv, my $err ) = eval
1417 20 {
1418 20 $filters->{ $enc }->( $data => $src, %$params );
1419 };
1420 20 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 20 20
1421 {
1422 0 ▶ return( $self->error( "Error ", ( $self->encode ? 'encoding' : 'decoding' ), " $size bytes of data: $@" ) );
Branch: 1 branch
BranchTrueFalseTotal
$self->encode ? : 0 0 0
1423 }
1424 20 ▶ return( $self->error( "Unable to encode data to write to source: $err" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 20 20
1425 20 $len = $size;
1426 }
1427 elsif( $stype eq 'scalar' )
1428 {
1429 0 ▶ if( $type eq 'scalar' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'scalar') { } 0 0 0
elsif ($type eq 'glob') { } 0 0 0
1430 {
1431 0 $$src .= $$data;
1432 0 $len = length( $$data );
1433 }
1434 elsif( $type eq 'glob' )
1435 {
1436 0 my( $rv, $buf );
1437 0 ▶ while( $rv = CORE::read( $data, $buf, 10240 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($rv = CORE::read($data, $buf, 10240)) 0 0 0
1438 {
1439 0 $$src .= $buf;
1440 0 $len += length( $buf );
1441 }
1442 0 ▶ return( $self->error( "Unable to read data from glob provided: $!" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
1443 }
1444 else
1445 {
1446 0 ▶ my $f = $self->new_file( $data ) || return( $self->pass_error );
Condition0%0/2
$self->new_file($data) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_file($data) || (return $self->pass_error)
$self->new_file($data)
Truth table0%0/2
A
A= $self->new_file($data)
Aresult
11
00
1447 0 ▶ my $fh = $f->open( '<' ) ||
Condition0%0/2
$f->open('<') || (return $self->pass_error($f->error))
l!l
00
MC/DC0%0/1
$f->open('<') || (return $self->pass_error($f->error))
$f->open('<')
Truth table0%0/2
A
A= $f->open('<')
Aresult
11
00
1448 return( $self->pass_error( $f->error ) );
1449 0 my $buf;
1450 0 my $rv = $fh->read( $buf );
1451 0 ▶ return( $self->error( "Unable to read data from file \"$f\" provided: $!" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 0 0
1452 0 $$src .= $buf;
1453 0 $len = length( $buf );
1454 }
1455 }
1456 elsif( $stype eq 'glob' )
1457 {
1458 0 ▶ if( $type eq 'scalar' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'scalar') { } 0 0 0
elsif ($type eq 'glob') { } 0 0 0
1459 {
1460 0 ▶ print( $src, $$data ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $src, $$data or return $self->error("Unable to write ", length $$data, " bytes of data to source glob: $!") 0 0 0
1461 return( $self->error( "Unable to write ", length( $$data ), " bytes of data to source glob: $!" ) );
1462 0 $len = length( $$data );
1463 }
1464 elsif( $type eq 'glob' )
1465 {
1466 0 my $buf;
1467 0 ▶ while( CORE::read( $data, $buf, 10240 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::read $data, $buf, 10240) 0 0 0
1468 {
1469 0 ▶ print( $src, $buf ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $src, $buf or return $self->error("Unable to write ", length $buf, " bytes of data to source glob: $!") 0 0 0
1470 return( $self->error( "Unable to write ", length( $buf ), " bytes of data to source glob: $!" ) );
1471 0 $len += length( $buf );
1472 }
1473 }
1474 else
1475 {
1476 0 ▶ my $f = $self->new_file( $data ) || return( $self->pass_error );
Condition0%0/2
$self->new_file($data) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_file($data) || (return $self->pass_error)
$self->new_file($data)
Truth table0%0/2
A
A= $self->new_file($data)
Aresult
11
00
1477 0 ▶ my $fh = $f->open( '<' ) ||
Condition0%0/2
$f->open('<') || (return $self->pass_error($f->error))
l!l
00
MC/DC0%0/1
$f->open('<') || (return $self->pass_error($f->error))
$f->open('<')
Truth table0%0/2
A
A= $f->open('<')
Aresult
11
00
1478 return( $self->pass_error( $f->error ) );
1479 0 my $buf;
1480 0 ▶ while( $fh->read( $buf, 10240 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($fh->read($buf, 10240)) 0 0 0
1481 {
1482 0 ▶ print( $src, $buf ) ||
Branch: 1 branch
BranchTrueFalseTotal
print $src, $buf or return $self->error("Unable to write ", length $buf, " bytes of data to source glob: $!") 0 0 0
1483 return( $self->error( "Unable to write ", length( $buf ), " bytes of data to source glob: $!" ) );
1484 0 $len += length( $buf );
1485 }
1486 }
1487 }
1488 else
1489 {
1490 0 ▶ my $f = $self->new_file( $src ) || return( $self->pass_error );
Condition0%0/2
$self->new_file($src) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_file($src) || (return $self->pass_error)
$self->new_file($src)
Truth table0%0/2
A
A= $self->new_file($src)
Aresult
11
00
1491 0 ▶ my $fh = $f->open( '>', { autoflush => 1 } ) || return( $self->pass_error( $f->error ) );
Condition0%0/2
$f->open('>', {'autoflush', 1}) || (return $self->pass_error($f->error))
l!l
00
MC/DC0%0/1
$f->open('>', {'autoflush', 1}) || (return $self->pass_error($f->error))
$f->open('>', {'autoflush', 1})
Truth table0%0/2
A
A= $f->open('>', {'autoflush', 1})
Aresult
11
00
1492 0 ▶ if( $type eq 'scalar' )
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'scalar') { } 0 0 0
elsif ($type eq 'glob') { } 0 0 0
1493 {
1494 0 ▶ $fh->print( $$data ) ||
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($$data) or return $self->error("Unable to write ", length $$data, qq[ bytes of data to file "$f": $!]) 0 0 0
1495 return( $self->error( "Unable to write ", length( $$data ), " bytes of data to file \"$f\": $!" ) );
1496 0 $len = length( $$data );
1497 }
1498 elsif( $type eq 'glob' )
1499 {
1500 0 my $buf;
1501 0 ▶ while( CORE::read( $data, $buf, 10240 ) )
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::read $data, $buf, 10240) 0 0 0
1502 {
1503 0 ▶ $fh->print( $buf ) ||
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($buf) or return $self->error("Unable to write ", length $buf, qq[ bytes of data to file "$f": $!]) 0 0 0
1504 return( $self->error( "Unable to write ", length( $buf ), " bytes of data to file \"$f\": $!" ) );
1505 0 $len += length( $buf );
1506 }
1507 }
1508 else
1509 {
1510 0 ▶ my $f2 = $self->new_file( $data ) || return( $self->pass_error );
Condition0%0/2
$self->new_file($data) || (return $self->pass_error)
l!l
00
MC/DC0%0/1
$self->new_file($data) || (return $self->pass_error)
$self->new_file($data)
Truth table0%0/2
A
A= $self->new_file($data)
Aresult
11
00
1511 0 ▶ my $fh2 = $f2->open( '<' ) ||
Condition0%0/2
$f2->open('<') || (return $self->pass_error($f2->error))
l!l
00
MC/DC0%0/1
$f2->open('<') || (return $self->pass_error($f2->error))
$f2->open('<')
Truth table0%0/2
A
A= $f2->open('<')
Aresult
11
00
1512 return( $self->pass_error( $f2->error ) );
1513 0 my $buf;
1514 0 ▶ while( $fh2->read( $buf, 10240 ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($fh2->read($buf, 10240)) 0 0 0
1515 {
1516 0 ▶ $fh->print( $buf ) ||
Branch: 1 branch
BranchTrueFalseTotal
$fh->print($buf) or return $self->error("Unable to write ", length $buf, qq[ bytes of data to source file "$f": $!]) 0 0 0
1517 return( $self->error( "Unable to write ", length( $buf ), " bytes of data to source file \"$f\": $!" ) );
1518 0 $len += length( $buf );
1519 }
1520 0 $fh2->close;
1521 }
1522 0 $fh->close;
1523 }
1524 }
1525 20 return( $len );
1526 }
1527  
1528 sub _decodable_encodable
1529 {
1530 1 ▶ my $self = shift( @_ );
Subroutine _decodable_encodable: called CC=21 SCAR=34.7
1531 # 1 for encodable, 0 for decodable
1532 1 my $enc_or_dec = shift( @_ );
1533 1 ▶ my $what = shift( @_ ) || 'all';
Condition50%1/2
shift @_ || 'all'
l!l
10
MC/DC0%0/1
shift @_ || 'all'
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
1534 1 my $list = $self->new_array;
1535 1 ▶ my $offset = $enc_or_dec ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$enc_or_dec ? : 0 1 1
1536 1 ▶ if( $self->_is_array( $what ) )
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_is_array($what)) { } 0 1 1
elsif ($what eq 'all' or $what eq 'auto') { } 0 1 1
elsif ($what eq 'browser') { } 1 0 1
1537 {
1538 0 $list = $what;
1539 }
1540 ▶ elsif( $what eq 'all' || $what eq 'auto' )
Condition33%1/3
$what eq "all" or $what eq "auto"
l!l&&r!l&&!r
001
MC/DC0%0/2
$what eq "all" or $what eq "auto"
$what eq "all" $what eq "auto"
Truth table33%1/3
A or B
A= $what eq "all"B= $what eq "auto"
ABresult
1X1
011
000
1541 {
1542 0 $list = [sort( keys( %$CLASSES ) )];
1543 }
1544 elsif( $what eq 'browser' )
1545 {
1546 1 foreach( keys( %$CLASSES ) )
1547 {
1548 27 ▶ $list->push( $_ ) if( $CLASSES->{ $_ }->[2] );
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Promise::Stream::CLASSES->{$_}[2] 11 16 27
1549 }
1550 }
1551 else
1552 {
1553 0 return( $self->error( "Unsupported keyword '$what' used." ) );
1554 }
1555
1556 1 my $res = $self->new_array;
1557 1 foreach my $enc ( @$list )
1558 {
1559 # inflate is just an alias for deflate
1560 11 ▶ next if( $enc eq 'inflate' || $enc eq 'rawinflate' || substr( $enc, 0, 2 ) eq 'x-' );
Branch: 1 branch
BranchTrueFalseTotal
if $enc eq "inflate" or $enc eq "rawinflate" or substr($enc, 0, 2) eq "x-" 4 6 10
Condition100%6/6
$enc eq "inflate" or $enc eq "rawinflate" or substr($enc, 0, 2) eq "x-"
l!l&&r!l&&!r
236
$enc eq "inflate" or $enc eq "rawinflate"
l!l&&r!l&&!r
119
MC/DC100%3/3
$enc eq "inflate" or $enc eq "rawinflate" or substr($enc, 0, 2) eq "x-"
$enc eq "inflate" $enc eq "rawinflate" substr($enc, 0, 2) eq "x-"
Truth table100%4/4
A or B or C
A= $enc eq "inflate"B= $enc eq "rawinflate"C= substr($enc, 0, 2) eq "x-"
ABCresult
1XX1
01X1
0011
0000
1561 6 ▶ if( !exists( $CLASSES->{ $enc } ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $HTTP::Promise::Stream::CLASSES->{$enc}) 0 6 6
1562 {
1563 0 ▶ warn( "Unsupported content encoding \"$enc\"." ) if( $self->_is_warnings_enabled( 'HTTP::Promise' ) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_warnings_enabled("HTTP::Promise") 0 0 0
1564 0 next;
1565 }
1566 6 my $encoder_class = $CLASSES->{ $enc }->[$offset];
1567 6 ▶ my $is_installed_method = ( $enc_or_dec ? 'is_encoder_installed' : 'is_decoder_installed' );
Branch: 1 branch
BranchTrueFalseTotal
$enc_or_dec ? : 0 6 6
1568 # Load the module if it is one of ours, so we can then use the method 'is_encoder_installed' or 'is_decoder_installed'
1569 6 ▶ if( substr( $encoder_class, 0, length( 'HTTP::Promise::Stream' ) ) eq 'HTTP::Promise::Stream' )
Branch: 1 branch
BranchTrueFalseTotal
if (substr($encoder_class, 0, 21) eq "HTTP::Promise::Stream") 2 4 6
1570 {
1571 2 ▶ $self->_load_class( $encoder_class ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class($encoder_class) or return $self->pass_error 0 2 2
1572 }
1573  
1574 6 ▶ if( my $coderef = $encoder_class->can( $is_installed_method ) )
Branch: 2 branches
BranchTrueFalseTotal
if (my $coderef = $encoder_class->can($is_installed_method)) { } 2 4 6
elsif ($self->_is_class_loadable($encoder_class)) { } 4 0 4
1575 {
1576 2 my $rv = $coderef->();
1577 2 ▶ $res->push( $enc ) if( $coderef->() );
Branch: 1 branch
BranchTrueFalseTotal
if &$coderef() 2 0 2
1578 }
1579 elsif( $self->_is_class_loadable( $encoder_class ) )
1580 {
1581 4 $res->push( $enc );
1582 }
1583 }
1584 1 return( $res );
1585 }
1586  
1587 sub _encoding2suffix_map
1588 {
1589 5 ▶ my $self = shift( @_ );
Subroutine _encoding2suffix_map: called CC=3 SCAR=12.3
1590 5 ▶ unless( defined( $ENCODING_SUFFIX ) && %$ENCODING_SUFFIX )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $HTTP::Promise::Stream::ENCODING_SUFFIX and %$HTTP::Promise::Stream::ENCODING_SUFFIX) 0 5 5
Condition33%1/3
defined $HTTP::Promise::Stream::ENCODING_SUFFIX and %$HTTP::Promise::Stream::ENCODING_SUFFIX
!ll&&!rl&&r
005
MC/DC0%0/2
defined $HTTP::Promise::Stream::ENCODING_SUFFIX and %$HTTP::Promise::Stream::ENCODING_SUFFIX
defined $HTTP::Promise::Stream::ENCODING_SUFFIX %$HTTP::Promise::Stream::ENCODING_SUFFIX
Truth table33%1/3
A and B
A= defined $HTTP::Promise::Stream::ENCODING_SUFFIXB= %$HTTP::Promise::Stream::ENCODING_SUFFIX
ABresult
0X0
100
111
1591 {
1592 # Initialisation défensive - normalement déjà fait dans le bloc BEGIN
1593 0 $ENCODING_SUFFIX = {};
1594 }
1595 5 my $copy = { %{$ENCODING_SUFFIX} };
1596 5 return( $copy );
1597 }
1598  
1599 sub _get_size
1600 {
1601 91 ▶ my $self = shift( @_ );
Subroutine _get_size: called CC=13 SCAR=27.3
1602 91 ▶ if( ref( $_[0] ) )
Branch: 2 branches
BranchTrueFalseTotal
if (ref $_[0]) { } 57 34 91
elsif (not ref $_[0] and index($_[0], "\n") == -1 && !(-e $_[0]) || (index($_[0], "\n") != -1)) { } 4 30 34
1603 {
1604 57 my $type = lc( Scalar::Util::reftype( $_[0] ) );
1605 57 ▶ if( $type eq 'scalar' )
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'scalar') { } 41 16 57
elsif ($type eq 'glob') { } 12 4 16
elsif ($self->_is_a($_[0], 'Module::Generic::File')) { } 4 0 4
1606 {
1607 41 return( length( ${$_[0]} ) );
1608 }
1609 elsif( $type eq 'glob' )
1610 {
1611 12 ▶ if( $self->_is_a( $_[0] => 'Module::Generic::Scalar::IO' ) )
Branch: 3 branches
BranchTrueFalseTotal
if ($self->_is_a($_[0], 'Module::Generic::Scalar::IO')) { } 8 4 12
elsif ($self->_is_object($_[0]) and $self->_can($_[0], 'size')) { } 0 4 4
elsif (fileno $_[0]) { } 4 0 4
1612 {
1613 8 return( $_[0]->size );
1614 }
1615 ▶ elsif( $self->_is_object( $_[0] ) && $self->_can( $_[0] => 'size' ) )
Condition33%1/3
$self->_is_object($_[0]) and $self->_can($_[0], "size")
!ll&&!rl&&r
040
MC/DC0%0/2
$self->_is_object($_[0]) and $self->_can($_[0], "size")
$self->_is_object($_[0]) $self->_can($_[0], "size")
Truth table33%1/3
A and B
A= $self->_is_object($_[0])B= $self->_can($_[0], "size")
ABresult
0X0
100
111
1616 {
1617 0 return( $_[0]->size );
1618 }
1619 elsif( fileno( $_[0] ) )
1620 {
1621 4 return( -s( $_[0] ) );
1622 }
1623 }
1624 elsif( $self->_is_a( $_[0] => 'Module::Generic::File' ) )
1625 {
1626 4 return( $_[0]->size );
1627 }
1628 0 return;
1629 }
1630 # If the data provided is not a reference i.e. a string and it does not have any
1631 # CRLF sequence and it is not a file that exists, OR it has multiple CRLF
1632 # sequences, then we treat it as a string, and to remove ambiguity, we make it a
1633 # scalar reference
1634 ▶ elsif( !ref( $_[0] ) &&
Condition66%6/9
not ref $_[0] and index($_[0], "\n") == -1 && !(-e $_[0]) || (index($_[0], "\n") != -1)
!ll&&!rl&&r
0304
index($_[0], "\n") == -1 and not -e $_[0] or (index($_[0], "\n") != -1)
l!l&&r!l&&!r
4030
index($_[0], "\n") == -1 and not -e $_[0]
!ll&&!rl&&r
0304
MC/DC25%1/4
not ref $_[0] and index($_[0], "\n") == -1 && !(-e $_[0]) || (index($_[0], "\n") != -1)
not ref $_[0] index($_[0], "\n") == -1 not -e $_[0] (index($_[0], "\n") != -1)
Truth table33%2/6
not(A) and B and not(C) or D
A= not ref $_[0]B= index($_[0], "\n") == -1C= not -e $_[0]D= (index($_[0], "\n") != -1)
ABCDresult
0XXX0
10X00
11000
111X1
10X11
11011
1635 (
1636 ( index( $_[0], "\n" ) == -1 && !-e( $_[0] ) ) ||
1637 ( index( $_[0], "\n" ) != -1 )
1638 ) )
1639 {
1640 4 return( length( $_[0] ) );
1641 }
1642 else
1643 {
1644 30 return( -s( $_[0] ) );
1645 }
1646 }
1647  
1648 sub _io_compress_params
1649 {
1650 95 ▶ my $self = shift( @_ );
Subroutine _io_compress_params: called CC=3 SCAR=11.0
1651 95 my $opts = {};
1652 95 my $ref = $self->compress_params;
1653 95 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 75 20 95
1654 {
1655 75 $opts = shift( @_ );
1656 75 my @keys = grep( /^[A-Z]\w+$/, keys( %$opts ) );
1657 75 ▶ @$ref{ @keys } = @$opts{ @keys } if( scalar( @keys ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @keys 0 75 75
1658 }
1659 95 return( $ref );
1660 }
1661  
1662 # Because the IO::Compress and IO::Uncompress family does not recognise a scalar object
1663 # as a valid scalar reference, we have to normalise it, before we can pass it to the filters
1664 # Remove this once IO::Compress has accepted my pull request to change
1665 # IO::Compress::Base::Common->whatIs made on 2022-04-11
1666 # <https://github.com/pmqs/IO-Compress/pull/40>
1667 sub _normalise
1668 {
1669 51 ▶ my $self = shift( @_ );
Subroutine _normalise: called CC=7 SCAR=23.7
1670 51 ▶ if( ref( $_[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 29 22 51
1671 {
1672 29 my $type = lc( Scalar::Util::reftype( $_[0] ) );
1673 29 ▶ if( $type eq 'scalar' )
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'scalar') { } 29 0 29
elsif ($type eq 'glob') { } 0 0 0
elsif ($self->_is_a($_[0], 'Module::Generic::File') or $self->_can($_[0], 'filename')) { } 0 0 0
1674 {
1675 # if it is a regular scalar reference, we return it
1676 # return( $self->_is_object( $_[0] ) ? \${$_[0]} : $_[0] );
1677 29 ▶ if( $self->_is_object( $_[0] ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_object($_[0])) { } 9 20 29
1678 {
1679 9 my $tmp = ${$_[0]};
1680 9 return( \$tmp );
1681 }
1682 else
1683 {
1684 20 return( $_[0] );
1685 }
1686 }
1687 elsif( $type eq 'glob' )
1688 {
1689 0 return( $_[0] );
1690 }
1691 ▶ elsif( $self->_is_a( $_[0] => 'Module::Generic::File' ) || $self->_can( $_[0] => 'filename' ) )
Condition0%0/3
$self->_is_a($_[0], "Module::Generic::File") or $self->_can($_[0], "filename")
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_is_a($_[0], "Module::Generic::File") or $self->_can($_[0], "filename")
$self->_is_a($_[0], "Module::Generic::File") $self->_can($_[0], "filename")
Truth table0%0/3
A or B
A= $self->_is_a($_[0], "Module::Generic::File")B= $self->_can($_[0], "filename")
ABresult
1X1
011
000
1692 {
1693 0 return( $_[0]->filename );
1694 }
1695 else
1696 {
1697 0 return( $_[0] );
1698 }
1699 }
1700 else
1701 {
1702 22 return( $_[0] );
1703 }
1704 }
1705  
1706 # NOTE: sub FREEZE is inherited
1707  
1708 1 ▶sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
Pod undocumented
1709  
1710 1 ▶sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
Pod undocumented
1711  
1712 # NOTE: sub THAW is inherited
1713  
1714 # NOTE: HTTP::Promise::Stream::Generic class
1715 {
1716 package
1717 HTTP::Promise::Stream::Generic;
1718 BEGIN
1719 {
1720 13 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1721 13 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1722 13 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
1723 13 ▶ use vars qw( $VERSION $EXCEPTION_CLASS );
Subroutine BEGIN: called CC=1 SCAR=0.0
1724 13 ▶ use Module::Generic::File::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
1725 13 ▶ use Module::Generic::Scalar::IO;
Subroutine BEGIN: called CC=1 SCAR=0.0
1726 # use Nice::Try;
1727 13 ▶ our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
Subroutine BEGIN: called CC=1 SCAR=0.0
1728 13 our $VERSION = $HTTP::Promise::Stream::VERSION;
1729 };
1730  
1731 13 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
1732 13 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
1733  
1734 sub init
1735 {
1736 21 ▶ my $self = shift( @_ );
Subroutine init: called CC=4 SCAR=14.3
1737 21 ▶ my $class = ( ref( $self ) || $self );
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
2100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
1738 21 $self->{_init_strict_use_sub} = 1;
1739 13 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
1740 21 ▶ $self->{_exception_class} = defined( ${"${class}\::EXCEPTION_CLASS"} ) ? ${"${class}\::EXCEPTION_CLASS"} : $EXCEPTION_CLASS;
Branch: 1 branch
BranchTrueFalseTotal
defined ${"${class}::EXCEPTION_CLASS";} ? : 21 0 21
1741 21 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 21 21
1742 21 return( $self );
1743 }
1744  
1745 sub _get_glob_from_arg
1746 {
1747 32 ▶ my $self = shift( @_ );
Subroutine _get_glob_from_arg: called CC=27 SCAR=48.8
1748 32 my $this = shift( @_ );
1749 32 ▶ return( $self->error( "No argument was provided." ) ) if( !defined( $this ) || ( !ref( $this ) && !length( $this ) ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $this and ref $this || length $this 0 32 32
Condition66%4/6
defined $this and ref $this || length $this
!ll&&!rl&&r
01220
ref $this or length $this
l!l&&r!l&&!r
21110
MC/DC0%0/3
defined $this and ref $this || length $this
defined $this ref $this length $this
Truth table25%1/4
A and B or C
A= defined $thisB= ref $thisC= length $this
ABCresult
0XX0
1000
11X1
1011
1750 32 my $opts = $self->_get_args_as_hash( @_ );
1751 32 ▶ $opts->{write} = 0 if( !exists( $opts->{write} ) );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $opts->{"write"} 16 16 32
1752 32 ▶ my $mode = $opts->{write} ? '+>' : '<';
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'write'} ? : 16 16 32
1753 32 my $fh;
1754 32 my $is_native_glob = 0;
1755 32 ▶ if( $self->_is_glob( $this ) )
Branch: 2 branches
BranchTrueFalseTotal
if ($self->_is_glob($this)) { } 3 29 32
elsif ($self->_is_scalar($this)) { } 15 14 29
1756 {
1757 3 $fh = $this;
1758 # even if this is a scalar reference opened in memory, perl will return -1, which is true
1759 3 ▶ $is_native_glob++ if( fileno( $this ) );
Branch: 1 branch
BranchTrueFalseTotal
if fileno $this 3 0 3
1760 }
1761 elsif( $self->_is_scalar( $this ) )
1762 {
1763 15 ▶ $fh = Module::Generic::Scalar::IO->new( $this, $mode ) ||
Condition50%1/2
'Module::Generic::Scalar::IO'->new($this, $mode) || (return $self->pass_error('Module::Generic::Scalar::IO'->error))
l!l
150
MC/DC0%0/1
'Module::Generic::Scalar::IO'->new($this, $mode) || (return $self->pass_error('Module::Generic::Scalar::IO'->error))
'Module::Generic::Scalar::IO'->new($this, $mode)
Truth table50%1/2
A
A= 'Module::Generic::Scalar::IO'->new($this, $mode)
Aresult
11
00
1764 return( $self->pass_error( Module::Generic::Scalar::IO->error ) );
1765 15 $is_native_glob++;
1766 }
1767 else
1768 {
1769 14 ▶ my $f = $self->new_file( "$this" ) || return( $self->pass_error );
Condition50%1/2
$self->new_file("$this") || (return $self->pass_error)
l!l
140
MC/DC0%0/1
$self->new_file("$this") || (return $self->pass_error)
$self->new_file("$this")
Truth table50%1/2
A
A= $self->new_file("$this")
Aresult
11
00
1770 14 ▶ return( $self->error( "File '$this' does not exist." ) ) if( !$f->exists && !$opts->{write} );
Branch: 1 branch
BranchTrueFalseTotal
unless $f->exists or $opts->{"write"} 0 14 14
Condition66%2/3
$f->exists or $opts->{"write"}
l!l&&r!l&&!r
4100
MC/DC0%0/2
$f->exists or $opts->{"write"}
$f->exists $opts->{"write"}
Truth table66%2/3
A or B
A= $f->existsB= $opts->{"write"}
ABresult
1X1
011
000
1771 14 ▶ $fh = $f->open( $mode, { binmode => 'raw', ( $opts->{write} ? ( autoflush => 1 ) : () ) } ) ||
Branch: 1 branch
BranchTrueFalseTotal
$opts->{'write'} ? : 10 4 14
Condition50%1/2
$f->open($mode, {'binmode', 'raw', $opts->{'write'} ? ('autoflush', 1) : ()}) || (return $self->pass_error($f->error))
l!l
140
MC/DC0%0/1
$f->open($mode, {'binmode', 'raw', $opts->{'write'} ? ('autoflush', 1) : ()}) || (return $self->pass_error($f->error))
$f->open($mode, {'binmode', 'raw', $opts->{'write'} ? ('autoflush', 1) : ()})
Truth table50%1/2
A
A= $f->open($mode, {'binmode', 'raw', $opts->{'write'} ? ('autoflush', 1) : ()})
Aresult
11
00
1772 return( $self->pass_error( $f->error ) );
1773 14 $is_native_glob++;
1774 }
1775 32 my $flags;
1776 32 ▶ if( $self->_can( $fh => 'fcntl' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_can($fh, 'fcntl')) { } 32 0 32
1777 {
1778 32 $flags = $fh->fcntl( F_GETFL, 0 );
1779 }
1780 else
1781 {
1782 0 $flags = fcntl( $fh, F_GETFL, 0 );
1783 }
1784
1785 32 ▶ if( defined( $flags ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $flags) 32 0 32
1786 {
1787 32 ▶ if( $opts->{write} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'write'}) { } 16 16 32
1788 {
1789 16 ▶ unless( $flags & ( O_RDWR | O_WRONLY | O_APPEND ) )
Branch: 1 branch
BranchTrueFalseTotal
unless ($flags & 1027) 0 16 16
1790 {
1791 0 return( $self->error( "Filehandle provided does not have write permission enabled." ) );
1792 }
1793 }
1794 # read mode then
1795 else
1796 {
1797 16 ▶ unless( ( ( $flags & O_RDONLY ) == O_RDONLY ) || ( $flags & O_RDWR ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (($flags & 0) == 0 or $flags & 2) 0 16 16
Condition33%1/3
($flags & 0) == 0 or $flags & 2
l!l&&r!l&&!r
1600
MC/DC0%0/2
($flags & 0) == 0 or $flags & 2
($flags & 0) == 0 $flags & 2
Truth table33%1/3
A or B
A= ($flags & 0) == 0B= $flags & 2
ABresult
1X1
011
000
1798 {
1799 0 return( $self->error( "Filehandle provided does not have read permission enabled. File handle flags value is '$flags'" ) );
1800 }
1801 }
1802 }
1803
1804 # We check if the file handle is an object, in which case we use its method, because
1805 # it may not be a true glob and calling perl's core read() or print() on it would not
1806 # work unless that glob object has implemented a tie. See perltie manual page.
1807 32 my $op;
1808 my $meth;
1809 32 ▶ if( $opts->{write} )
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{'write'}) { } 16 16 32
1810 {
1811 16 ▶ if( $is_native_glob )
Branch: 2 branches
BranchTrueFalseTotal
if ($is_native_glob) { } 16 0 16
elsif ($meth = $self->_can($fh, 'print') || $self->_can($fh, 'write')) { } 0 0 0
1812 {
1813 $op = sub
1814 {
1815 344 ▶ my $rv = print( $fh @_ );
Subroutine __ANON__: called CC=2 SCAR=7.1
1816 344 ▶ return( $self->error( "Error writing ", CORE::length( $_[0] ), " bytes of data to output: $!" ) ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 344 344
1817 344 return( $rv );
1818 16 };
1819 }
1820 ▶ elsif( ( $meth = ( $self->_can( $fh => 'print' ) || $self->_can( $fh => 'write' ) ) ) )
Condition0%0/3
$self->_can($fh, 'print') || $self->_can($fh, 'write')
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->_can($fh, 'print') || $self->_can($fh, 'write')
$self->_can($fh, 'print') $self->_can($fh, 'write')
Truth table0%0/3
A || B
A= $self->_can($fh, 'print')B= $self->_can($fh, 'write')
ABresult
1X1
011
000
1821 {
1822 $op = sub
1823 {
1824 # try-catch
1825 0 ▶ local $@;
Subroutine __ANON__: not called CC=6 SCAR=37.4
1826 my $rv = eval
1827 0 {
1828 0 $fh->$meth( @_ );
1829 };
1830 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1831 {
1832 0 return( $self->error( "Error writing ", CORE::length( $_[0] ), " bytes of data to output: $@" ) );
1833 }
1834 0 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $rv) 0 0 0
1835 {
1836 0 my $err;
1837 0 ▶ if( defined( $! ) )
Branch: 3 branches
BranchTrueFalseTotal
if (defined $!) { } 0 0 0
elsif ($self->_can($fh, 'error')) { } 0 0 0
elsif ($self->_can($fh, 'errstr')) { } 0 0 0
1838 {
1839 0 $err = $!;
1840 }
1841 elsif( $self->_can( $fh => 'error' ) )
1842 {
1843 0 $err = $fh->error;
1844 }
1845 elsif( $self->_can( $fh => 'errstr' ) )
1846 {
1847 0 $err = $fh->errstr;
1848 }
1849 0 return( $self->error( "Error writing ", CORE::length( $_[0] ), " bytes of data to output: $err" ) );
1850 }
1851 0 return( $rv );
1852 0 };
1853 }
1854 else
1855 {
1856 0 return( $self->error( "The file handle provided is not a native opened one and does not support the print() or write() methods." ) );
1857 }
1858 }
1859 else
1860 {
1861 16 ▶ if( $is_native_glob )
Branch: 2 branches
BranchTrueFalseTotal
if ($is_native_glob) { } 16 0 16
elsif ($self->_can($fh, 'read')) { } 0 0 0
1862 {
1863 $op = sub
1864 {
1865 329 ▶ my $n = read( $fh, $_[0], $_[1] );
Subroutine __ANON__: called CC=2 SCAR=7.1
1866 329 ▶ return( $self->error( "Error reading ", $_[1], " bytes of data from input: $!" ) ) if( !defined( $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 329 329
1867 329 return( $n );
1868 16 };
1869 }
1870 elsif( $self->_can( $fh => 'read' ) )
1871 {
1872 $op = sub
1873 {
1874 # try-catch
1875 0 ▶ local $@;
Subroutine __ANON__: not called CC=6 SCAR=37.4
1876 my $n = eval
1877 0 {
1878 0 $fh->read( @_ );
1879 };
1880 0 ▶ if( $@ )
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
1881 {
1882 0 return( $self->error( "Error reading ", $_[1], " bytes of data from input: $@" ) );
1883 }
1884 0 ▶ if( !defined( $n ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $n) 0 0 0
1885 {
1886 0 my $err;
1887 0 ▶ if( defined( $! ) )
Branch: 3 branches
BranchTrueFalseTotal
if (defined $!) { } 0 0 0
elsif ($self->_can($fh, 'error')) { } 0 0 0
elsif ($self->_can($fh, 'errstr')) { } 0 0 0
1888 {
1889 0 $err = $!;
1890 }
1891 elsif( $self->_can( $fh => 'error' ) )
1892 {
1893 0 $err = $fh->error;
1894 }
1895 elsif( $self->_can( $fh => 'errstr' ) )
1896 {
1897 0 $err = $fh->errstr;
1898 }
1899 0 return( $self->error( "Error reading ", $_[1], " bytes of data from intput: $err" ) );
1900 }
1901 0 return( $n );
1902 0 };
1903 }
1904 else
1905 {
1906 0 return( $self->error( "The file handle provided is not a native opened one and does not support the read() method." ) );
1907 }
1908 }
1909 32 return( $fh, $op );
1910 }
1911  
1912 # NOTE: sub FREEZE is inherited
1913  
1914 4 ▶ sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
Subroutine STORABLE_freeze: called CC=1 SCAR=0.0
1915  
1916 4 ▶ sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
Subroutine STORABLE_thaw: called CC=1 SCAR=0.0
1917  
1918 # NOTE: sub THAW is inherited
1919 }
1920  
1921 13 1;
1922 # NOTE: POD
1923 __END__
1924  
1925 =encoding utf-8
1926  
1927 =head1 NAME
1928  
1929 HTTP::Promise::Stream - Data Stream Encoding and Decoding
1930  
1931 =head1 SYNOPSIS
1932  
1933 use HTTP::Promise::Stream;
1934 my $this = HTTP::Promise::Stream->new ||
1935 die( HTTP::Promise::Stream->error, "\n" );
1936  
1937 =head1 VERSION
1938  
1939 v0.3.0
1940  
1941 =head1 DESCRIPTION
1942  
1943 L<HTTP::Promise::Stream> serves to set a stream of data tha that optionally may need to be encoding or decoding, and read or write data from or to it that may also need to be compressed or decompressed.
1944  
1945 Once those versatile parameters are set, one can use the class method to access or write the data and the necessary encoding or decoding is done automatically.
1946  
1947 =head1 CONSTRUCTOR
1948  
1949 =head2 new
1950  
1951 Provided with a stream source, and some optional parameters and this will return a new L<HTTP::Promise::Stream> object.
1952  
1953 Currently supported stream sources are: scalar reference, glob and file path.
1954  
1955 If an error occurred, this sets an L<error|Module::Generic/error> and returns C<undef>
1956  
1957 Supported parameters are:
1958  
1959 =over 4
1960  
1961 =item * C<decoding>
1962  
1963 A string representing the encoding to use for decoding data. Currently supported encodings are: gzip, bzip2, deflate/inflate and zip
1964  
1965 =item * C<encoding>
1966  
1967 A string representing the encoding to use for encoding data. Currently supported encodings are: gzip, bzip2, deflate/inflate and zip
1968  
1969 =back
1970  
1971 =head1 METHODS
1972  
1973 =head2 as_string
1974  
1975 Returns the source stream as a string, or C<undef> and an L<error|Module::Generic/error> occurred.
1976  
1977 =head2 compress_params
1978  
1979 Sets or gets an hash of parameters-value pairs to be used for the compression algorithm used.
1980  
1981 =head2 decodable
1982  
1983 Provided with a C<target> and this returns an L<array object|Module::Generic::Array> of decoders that are installed.
1984  
1985 The C<target> can be a string or an array reference of decoder names. If the target string C<all> is specified, then, this will check all supported encodings. See L</supported>. If the target string C<browser> is specified, then ths will check only the supported encodings that are also supported by web browsers. If no target is specified, it defaults to C<all>.
1986  
1987 If the target is an array reference, it will return the list of supported decoders in the order provided.
1988  
1989 my $all = $stream->decodable;
1990 # Same as above
1991 my $all = $stream->decodable( 'all' );
1992 my $all = $stream->decodable( 'browser' );
1993 my $all = $stream->decodable( [qw( gzip br deflate )] );
1994 # $all could contain gzip and br for example
1995  
1996 Note that for most encoding, encoding and decoding is done by different classes.
1997  
1998 =head2 decode
1999  
2000 $stream->decode( $data );
2001 $stream->decode( $data, { encoding => bzip2 } );
2002 $stream->decode( $data, { decoding => bzip2 } );
2003 my $decoded = $stream->decode;
2004 my $decoded = $stream->decode( { encoding => bzip2 } );
2005 my $decoded = $stream->decode( { decoding => bzip2 } );
2006  
2007 This behaves in two different ways depending on the parameters provided:
2008  
2009 =over 4
2010  
2011 =item 1. with C<data> provided
2012  
2013 This will decode the C<data> provided using the encoding specified and write the decoded data to the source stream.
2014  
2015 =item 2. without C<data> provided
2016  
2017 This will decode the source stream directly and return the data thus decoded.
2018  
2019 =back
2020  
2021 This method will take the required encoding in the following order: from the C<decoding> parameter, from the C<encoding> parameter, or from L</decoding> as set during object instantiation.
2022  
2023 If the encoding specified is not supported this will return an error.
2024  
2025 It returns true upon success, or sets an L<error|Module::Generic/error> and returns C<undef>
2026  
2027 =head2 decoding
2028  
2029 This is a string. Sets or gets the encoding used for decoding. Supported encodings are: gzip, bzip2, inflate/deflate and zip
2030  
2031 =head2 encodable
2032  
2033 Provided with a C<target> and this returns an L<array object|Module::Generic::Array> of encoders that are installed.
2034  
2035 The C<target> can be a string or an array reference of decoder names. If the target string C<all> is specified, then, this will check all supported encodings. See L</supported>. If the target string C<browser> is specified, then ths will check only the supported encodings that are also supported by web browsers. If no target is specified, it defaults to C<all>.
2036  
2037 If the target is an array reference, it will return the list of supported encoders in the order provided.
2038  
2039 my $all = $stream->encodable;
2040 # Same as above
2041 my $all = $stream->encodable( 'all' );
2042 my $all = $stream->encodable( 'browser' );
2043 my $all = $stream->encodable( [qw( gzip br deflate )] );
2044 # $all could contain gzip and br for example
2045  
2046 Note that for most encoding, encoding and decoding is done by different classes.
2047  
2048 =head2 encode
2049  
2050 $stream->encode( $data );
2051 $stream->encode( $data, { encoding => bzip2 } );
2052 $stream->encode( $data, { decoding => bzip2 } );
2053 my $encoded = $stream->encode;
2054 my $encoded = $stream->encode( { encoding => bzip2 } );
2055 my $encoded = $stream->encode( { decoding => bzip2 } );
2056  
2057 This is the alter ego of L</decode>
2058  
2059 This behaves in two different ways depending on the parameters provided:
2060  
2061 =over 4
2062  
2063 =item 1. with C<data> provided
2064  
2065 This will encode the C<data> provided using the encoding specified and write the encoded data to the source stream.
2066  
2067 =item 2. without C<data> provided
2068  
2069 This will encode the source stream directly and return the data thus encoded.
2070  
2071 =back
2072  
2073 This method will take the required encoding in the following order: from the C<encoding> parameter, or from L</encoding> as set during object instantiation.
2074  
2075 If the encoding specified is not supported this will return an error.
2076  
2077 It returns true upon success, or sets an L<error|Module::Generic/error> and returns C<undef>
2078  
2079 =head2 encoding
2080  
2081 This is a string. Sets or gets the encoding used for encoding. Supported encodings are: gzip, bzip2, inflate/deflate and zip
2082  
2083 =head2 encoding2suffix
2084  
2085 Provided with a string of comma-separated encodings, or an array reference of encodings and this will return an L<array object|Module::Generic::Array> of associated file extensions.
2086  
2087 For example:
2088  
2089 my $a = HTTP::Promise::Stream->encoding2suffix( [qw( base64 gzip )] );
2090 # $a contains: b64 and gz
2091  
2092 my $a = HTTP::Promise::Stream->encoding2suffix( 'gzip' );
2093 # $a contains: gz
2094  
2095 =head2 load
2096  
2097 This attempts the load the specified encoding related class and returns true upon success or false otherwise.
2098  
2099 It sets an L<error|Module::Generic/error> and returns C<undef> upon error.
2100  
2101 For example:
2102  
2103 if( HTTP::Promise::Stream->load( 'bzip2' ) )
2104 {
2105 my $s = HTTP::Promise::Stream->new( \$data, encoding => 'bzip2' );
2106 my $output = Module::Generic::Scalar->new;
2107 my $len = $s->read( $output, { Transparent => 0 } );
2108 die( $s->error ) if( !defined( $len ) );
2109 say "Ok, $len bytes were encoded.";
2110 }
2111 else
2112 {
2113 say "Encoder/decoder bzip2 related modules are not installed on this system.";
2114 }
2115  
2116 See also L</supported>, which will tell you if L<HTTP::Promise::Stream> even supports the specified encoding.
2117  
2118 =head2 read
2119  
2120 $stream->read( $buffer );
2121 $stream->read( $buffer, $len );
2122 $stream->read( $buffer, $len, $offset );
2123 $stream->read( *buffer );
2124 $stream->read( *buffer, $len );
2125 $stream->read( sub{} );
2126 $stream->read( sub{}, $len );
2127 $stream->read( \$buffer );
2128 $stream->read( \$buffer, $len );
2129 $stream->read( \$buffer, $len, $offset );
2130 $stream->read( '/some/where/file.txt' );
2131 $stream->read( '/some/where/file.txt', $len );
2132  
2133 Provided with some parameters, as detailed below, and this will either encode or decode the stream if any encoding was provided at all and into the read buffer specified.
2134  
2135 Possible read buffers are:
2136  
2137 =over 4
2138  
2139 =item * scalar
2140  
2141 =item * scalar reference
2142  
2143 =item * file handle (glob)
2144  
2145 =item * subroutine reference or anonymous subroutine
2146  
2147 =item * file path
2148  
2149 =back
2150  
2151 It takes as optional parameters the C<length> of data, possibly encoded or decoded if any encoding was provided, and an optional C<offset>. However, note that the C<offset> argument is not used and ignored if the data buffer is not a string or a scalar reference.
2152  
2153 Also you can specify an hash reference of options as the last parameter. Recognised options are:
2154  
2155 =over 4
2156  
2157 =item * autoflush
2158  
2159 Boolean value. If true, this will set the auto flush.
2160  
2161 =item * binmode
2162  
2163 The encoding to be used when opening the file specified, if one is specified. See L</binmode>
2164  
2165 =item * mode
2166  
2167 The mode in which to open the file specified, if one is specified.
2168  
2169 Possible modes can be >, +>, >>, +<, w, w+, r+, a, a+, < and r or an integer representing a bitwise value such as O_APPEND, O_ASYNC, O_CREAT, O_DEFER, O_EXCL, O_NDELAY, O_NONBLOCK, O_SYNC, O_TRUNC, O_RDONLY, O_WRONLY, O_RDWR. For example: C<O_WRONLY|O_APPEND> For that see L<Fcntl>
2170  
2171 =item * other parameters starting with an uppercase letter
2172  
2173 Those parameters will be passed directly to the encoder/decoder.
2174  
2175 my $s = HTTP::Promise::Stream->new( \$data, decoding => 'inflate' );
2176 # Transparent and its value are passed directly to IO::Uncompress::Inflate
2177 $s->read( \$output, { Transparent => 0 } );
2178  
2179 =back
2180  
2181 A typical recommended parameter used for the C<IO::Compress> and C<IO::Uncompress> families is C<Transparent> set to C<0>, otherwise, the default is C<1> and it would be lenient and any encoding/decoding issues with the data would be ignored.
2182  
2183 For example, when using C<inflate> to uncompress data compressed with C<deflate>, some encoder do not format the data correctly, or declare it as C<deflate> when they really meant C<rawdeflate>, i.e. without the zlib headers and trailers. By default with C<Transparent> set to C<1>, L<IO::Uncompress::Inflate> will simply pass through the data. However, you are better of catching the error and resort to using C<rawinflate> instead.
2184  
2185 For example:
2186  
2187 use v5.17;
2188 use HTTP::Promise::Stream;
2189 my $data = '80jNyclXCM8vyklRBAA=';
2190 my $buff = '';
2191 my $s = HTTP::Promise::Stream->new( \$data, decoding => 'base64' ) ||
2192 die( HTTP::Promise::Stream->error );
2193 my $len = $s->read( \$buff );
2194 die( $s->error ) if( !defined( $len ) );
2195
2196 say "Now inflating data.";
2197 $data = $buff;
2198 $buff = '';
2199 my $s = HTTP::Promise::Stream->new( \$data, decoding => 'deflate' ) ||
2200 die( HTTP::Promise::Stream->error );
2201 $len = $s->read( \$buff, { Transparent => 0 } );
2202 if( !defined( $len ) )
2203 {
2204 # Trying with rawinflate instead
2205 if( $s->error->message =~ /Header Error: CRC mismatch/ )
2206 {
2207 say "Found deflate encoding error (", $s->error->message, "), trying with rawinflate instead.";
2208 my $s = HTTP::Promise::Stream->new( \$data, decoding => 'rawdeflate' ) ||
2209 die( HTTP::Promise::Stream->error );
2210 $len = $s->read( \$buff, { Transparent => 0 } );
2211 die( $s->error ) if( !defined( $len ) );
2212 }
2213 else
2214 {
2215 die( $s->error );
2216 }
2217 }
2218 say $buff; # Hello world
2219  
2220 =head2 source
2221  
2222 Set or get the source stream.
2223  
2224 =head2 suffix2encoding
2225  
2226 Provided with a filename, and this will return an L<array object|Module::Generic::Array> containing the encoding naes associated with the extensions found.
2227  
2228 For example:
2229  
2230 my $a = HTTP::Promise::Stream->suffix2encoding( 'file.html.gz' );
2231 # $a contains: gzip
2232  
2233 my $a = HTTP::Promise::Stream->suffix2encoding( 'file.html' );
2234 # $a contains nothing
2235  
2236 =head2 supported
2237  
2238 Provided with an encoding name and this returns true if it is supported, or false otherwise.
2239  
2240 Currently supported encodings are:
2241  
2242 =over 4
2243  
2244 =item Base64
2245  
2246 Supported natively. See L<HTTP::Promise::Stream::Base64>
2247  
2248 =item Brotli
2249  
2250 Requires L<IO::Compress::Brotli> for encoding and L<IO::Uncompress::Brotli> for decoding.
2251  
2252 See also L<caniuse|https://caniuse.com/brotli>
2253  
2254 =item Bzip2
2255  
2256 Requires L<IO::Compress::Bzip2> for encoding and L<IO::Uncompress::Bunzip2> for decoding.
2257  
2258 =item Deflate and Inflate
2259  
2260 Requires L<IO::Compress::Deflate> for encoding and L<IO::Uncompress::Inflate> for decoding.
2261  
2262 This is the same as C<rawdeflate> and C<rawinflate>, except it has zlib headers and trailers.
2263  
2264 See also its L<rfc1950|https://tools.ietf.org/html/rfc1950>, L<the Wikipedia page|https://en.wikipedia.org/wiki/Deflate> and L<Mozilla documentation about Content-Encoding|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding#directives>
2265  
2266 Note that some web server announce data encoded with C<deflate> whereas they really mean C<rawdeflate>, so you might want to use the C<Transparent> parameter set to C<0> when using L</read>
2267  
2268 =item Gzip
2269  
2270 Requires L<IO::Compress::Gzip> for encoding and L<IO::Uncompress::Gunzip> for decoding.
2271  
2272 See also L<caniuse|https://caniuse.com/sr_content-encoding-gzip>
2273  
2274 =item Lzf
2275  
2276 This is Lempel-Ziv-Free compression.
2277  
2278 Requires L<IO::Compress::Lzf> for encoding and L<IO::Uncompress::UnLzf> for decoding.
2279  
2280 See L<Stackoverflow discussion|https://stackoverflow.com/questions/5089112/whatre-lzo-and-lzf-and-the-differences>
2281  
2282 =item Lzip
2283  
2284 Requires L<IO::Compress::Lzip> for encoding and L<IO::Uncompress::UnLzip> for decoding.
2285  
2286 =item Lzma
2287  
2288 Requires L<IO::Compress::Lzma> for encoding and L<IO::Uncompress::UnLzma> for decoding.
2289  
2290 See L<Wikipedia page|https://fr.wikipedia.org/wiki/LZMA>
2291  
2292 =item Lzop
2293  
2294 Requires L<IO::Compress::Lzop> for encoding and L<IO::Uncompress::UnLzop> for decoding.
2295  
2296 "lzop is a file compressor which is very similar to L<gzip|http://www.gzip.org/>. lzop uses the L<LZO data compression library|http://www.oberhumer.com/opensource/lzo/> for compression services, and its main advantages over gzip are much higher compression and decompression speed (at the cost of some compression ratio)."
2297  
2298 See the L<compressor home page|https://www.lzop.org/> and L<Wikipedia page|https://en.wikipedia.org/wiki/Lzop>
2299  
2300 =item Lzw
2301  
2302 This is Lempel-Ziv-Welch compression.
2303  
2304 Requires L<Compress::LZW> for encoding and for decoding.
2305  
2306 A.k.a C<compress>, this was used commonly until some corporation purchased the patent and started asking everyone for royalties. The patent expired in 2003. Gzip took over since then.
2307  
2308 =item QuotedPrint
2309  
2310 Requires the XS module L<MIME::QuotedPrint> for encoding and decoding.
2311  
2312 This encodes and decodes the quoted-printable data according to L<rfc2045, section 6.7|https://tools.ietf.org/html/rfc2045#section-6.7>
2313  
2314 See also the L<Wikipedia page|https://en.wikipedia.org/wiki/Quoted-printable>
2315  
2316 =item Raw deflate
2317  
2318 Requires L<IO::Compress::RawDeflate> for encoding and L<IO::Uncompress::RawInflate> for decoding.
2319  
2320 This is the same as C<deflate> and C<inflate>, but without the zlib headers and trailers.
2321  
2322 See also its L<rfc1951|https://tools.ietf.org/html/rfc1951> and L<Mozilla documentation about Content-Encoding|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding#directives>
2323  
2324 =item UU encoding and decoding
2325  
2326 Supported natively. See L<HTTP::Promise::Stream::UU>
2327  
2328 =item Xz
2329  
2330 Requires L<IO::Compress::Xz> for encoding and L<IO::Uncompress::UnXz> for decoding.
2331  
2332 Reportedly, "xz achieves higher compression rates than alternatives like gzip and bzip2. Decompression speed is higher than bzip2, but lower than gzip. Compression can be much slower than gzip, and is slower than bzip2 for high levels of compression, and is most useful when a compressed file will be used many times."
2333  
2334 See L<compressor home page|https://tukaani.org/xz/> and L<Wikipedia page|https://en.wikipedia.org/wiki/XZ_Utils>
2335  
2336 =item Zip
2337  
2338 Requires L<IO::Compress::Zip> for encoding and L<IO::Uncompress::Unzip> for decoding.
2339  
2340 =item Zstd
2341  
2342 Requires L<IO::Compress::Zstd> for encoding and L<IO::Uncompress::UnZstd> for decoding.
2343  
2344 See L<rfc8878|https://tools.ietf.org/html/rfc8878> and L<Wikipedia page|https://en.wikipedia.org/wiki/Zstd>
2345  
2346 =back
2347  
2348 See also L</load>, which will tell you if the specified encoding related modules are installed on the system or not.
2349  
2350 =head2 write
2351  
2352 $stream->write( $data );
2353 $stream->write( \$data );
2354 $stream->write( *$data );
2355 $stream->write( '/some/where/file.txt' );
2356 $stream->write( sub{} );
2357  
2358 Provided with some data, and this will read the data provided, and write it, possibly encoded or decoded, depending on whether a decoding or encoding was provided, to the stream source.
2359  
2360 It returns the amount of bytes written to the source stream, but before any possible encoding or decoding.
2361  
2362 The data that can be provided are:
2363  
2364 =over 4
2365  
2366 =item * string
2367  
2368 Note that the difference between a file and a string is slim. To distinguish the two, this method will treat as a string any value that is not a reference and that either contains a CRLF sequence, or that does not contain a CRLF sequence and is not an existing file.
2369  
2370 =item * scalar reference
2371  
2372 =item * file handle (glob)
2373  
2374 =item * file path
2375  
2376 Note that the difference between a file and a string is slim. So to distinguish the two, this method will treat as a file a value that has no CRLR sequence
2377  
2378 =item * code reference (anonymous subroutine or subroutine reference)
2379  
2380 It will be called once and expect data in return. If the code executed dies, the exception will be trapped using try-catch block from L<Nice::Try>
2381  
2382 =back
2383  
2384 The behaviour is different depending on the source type and the data type being provided. Below is an in-depth explanation:
2385  
2386 =over 4
2387  
2388 =item 1. Source stream is a code reference
2389  
2390 =over 8
2391  
2392 =item 1.1 Data is to be encoded
2393  
2394 Data is encoded with L</encode>, then the source code reference is executed, passing it the encoded data
2395  
2396 =item 1.2 Data is to be decoded
2397  
2398 Data is decoded with L</decode>, then the source code reference is executed, passing it the decoded data
2399  
2400 =item 1.3 Data is scalar reference
2401  
2402 The source code reference is executed, passing it the content of the scalar reference
2403  
2404 =item 1.4 Data is a glob
2405  
2406 The file handle is read by chunks of 10Kb (10240 bytes) and each time the source code reference is called passing it the data chunk read.
2407  
2408 =item 1.5 Data is a file path
2409  
2410 The file is opened in read mode, and all its content is provided in one pass to the source code reference.
2411  
2412 =back
2413  
2414 =item 2. Data is the be encoded
2415  
2416 The appropriate encoder is called to encode the data and write to the source stream.
2417  
2418 =item 3. Data is to be decoded
2419  
2420 The appropriate decoder is called to decode the data and write to the source stream.
2421  
2422 =item 4. Source stream is a scalar reference
2423  
2424 =over 8
2425  
2426 =item 4.1 Data is a scalar reference
2427  
2428 The provided data is simply appended to the source stream.
2429  
2430 =item 4.2 Data is a glob
2431  
2432 The file handle is read by chunks of 10Kb (10240 bytes) and appended to the source stream.
2433  
2434 =item 4.3 Data is a file path
2435  
2436 The file is opened in read mode and its content appended to the source stream.
2437  
2438 =back
2439  
2440 =item 5. Source stream is a glob
2441  
2442 =over 8
2443  
2444 =item 5.1 Data is a scalar reference
2445  
2446 The file handle of the source stream is called with L</print> and the data is printed to it.
2447  
2448 =item 5.2 Data is a glob
2449  
2450 The data file handle is read by chunks of 10Kb (10240 bytes) and each one printed to the source stream file handle.
2451  
2452 =item 5.3 Data is a file path
2453  
2454 The given file path is read in read mode and each chunk of 10Kb (10240 bytes) read is printed to the source stream file handle.
2455  
2456 =back
2457  
2458 =item 6. Source stream is a file path
2459  
2460 The source file is opened in write clobbering mode.
2461  
2462 =over 8
2463  
2464 =item 6.1 Data is a scalar reference
2465  
2466 The data is printed to the source stream
2467  
2468 =item 6.2 Data is a glob
2469  
2470 Data from the glob is read by chunks of 10Kb (10240 bytes) and each one printed to the source stream
2471  
2472 =item 6.3 Data is a file path.
2473  
2474 The file is opened in read mode and its content is read by chunks o 10Kb (10240 bytes) and each chunk printed to the source stream.
2475  
2476 =back
2477  
2478 =back
2479  
2480 =for Pod::Coverage _get_size
2481  
2482 =head1 THREAD-SAFETY
2483  
2484 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
2485  
2486 =head1 AUTHOR
2487  
2488 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
2489  
2490 =head1 SEE ALSO
2491  
2492 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Compression>, L<Content-Encoding documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Encoding>
2493  
2494 L<Wikipedia page|https://en.wikipedia.org/wiki/HTTP_compression>
2495  
2496 L<PerlIO::via::gzip>, L<PerlIO::via::Bzip2>, L<PerlIO::via::Base64>, L<PerlIO::via::QuotedPrint>, L<PerlIO::via::xz>
2497  
2498 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>
2499  
2500 =head1 COPYRIGHT & LICENSE
2501  
2502 Copyright(c) 2022 DEGUEST Pte. Ltd.
2503  
2504 All rights reserved
2505 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
2506  
2507 =cut
« blib/lib/HTTP/Promise/Entity.pm ↑ Summary lib/HTTP/Promise/Entity.pm »