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

filter: Statementstmt 0.0% 0 / 1043 Branchbran 0.0% 0 / 460 Conditioncond 0.0% 0 / 304 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 83 Podpod 0.0% 0 / 30 totaltotal 0.0% 0 / 1920 CC 199 SCAR 67.2
CC
199
Cov
0%
read
2352.0
write
1892.0
_get_glob_from_arg
992.0
CRAP 832.2
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 use strict;
18 use warnings;
19 warnings::register_categories( 'HTTP::Promise' );
20 use parent qw( Module::Generic );
21 use vars qw(
22 $FILTER_MAP $CLASSES $ENCODING_SUFFIX $SUFFIX_ENCODING $ENCODING_ALIAS
23 $MIME_ENCODING
24 );
25 use HTTP::Promise::Exception;
26 use Scalar::Util;
27 use constant HAS_BROWSER_SUPPORT => 1;
28 our $VERSION = 'v0.3.0';
29 };
30  
31 use strict;
32 use warnings;
33 no warnings 'uninitialized';
34  
35 {
36 no warnings 'once';
37 $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 $CLASSES->{br} = $CLASSES->{brotli};
57 $CLASSES->{inflate} = $CLASSES->{deflate};
58 $CLASSES->{rawinflate} = $CLASSES->{rawdeflate};
59 $CLASSES->{compress} = $CLASSES->{lzw};
60 $CLASSES->{'quoted-printable'} = $CLASSES->{qp};
61 $CLASSES->{'x-compress'} = $CLASSES->{lzw};
62 $CLASSES->{uuencode} = $CLASSES->{uu};
63 # Permit non-standard call with prefix x-
64 for( qw( bzip2 deflate gzip zip ) )
65 {
66 $CLASSES->{'x-' . $_} = $CLASSES->{ $_ };
67 }
68
69 $FILTER_MAP =
70 {
71 encode =>
72 {
73 # NOTE: encode -> base64
74 base64 => sub
75 {
76 # try-catch
77 local $@;
78 my $rv = eval
79 {
80 require HTTP::Promise::Stream::Base64;
81 HTTP::Promise::Stream::Base64::encode_b64( $_[0] => $_[1], @_[2..$#_] );
82 };
83 if( $@ )
84 {
85 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
86 }
87 # defined( $rv ) or return( undef, $HTTP::Promise::Stream::Base64::Base64Error );
88 defined( $rv ) or return( undef, HTTP::Promise::Stream::Base64->error );
89 return( $rv );
90 },
91 # NOTE: encode -> brotli
92 brotli => sub
93 {
94 # try-catch
95 local $@;
96 my $rv = eval
97 {
98 require HTTP::Promise::Stream::Brotli;
99 HTTP::Promise::Stream::Brotli::encode_bro( $_[0] => $_[1], @_[2..$#_] );
100 };
101 if( $@ )
102 {
103 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
104 }
105 # $rv or return( undef, $HTTP::Promise::Stream::Brotli::BrotliError );
106 defined( $rv ) or return( undef, HTTP::Promise::Stream::Brotli->error );
107 return( $rv );
108 },
109 # NOTE: encode -> bzip2
110 bzip2 => sub
111 {
112 # try-catch
113 local $@;
114 my $rv = eval
115 {
116 require IO::Compress::Bzip2;
117 IO::Compress::Bzip2::bzip2( $_[0] => $_[1], @_[2..$#_] );
118 };
119 if( $@ )
120 {
121 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
122 }
123 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Bzip2::Bzip2Error }) );
124 return( $rv );
125 },
126 # NOTE: encode -> deflate
127 deflate => sub
128 {
129 # try-catch
130 local $@;
131 my $rv = eval
132 {
133 require IO::Compress::Deflate;
134 IO::Compress::Deflate::deflate( $_[0] => $_[1], @_[2..$#_] );
135 };
136 if( $@ )
137 {
138 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
139 }
140 # $rv or return( undef, $IO::Compress::Deflate::DeflateError );
141 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Deflate::DeflateError }) );
142 return( $rv );
143 },
144 # NOTE: encode -> gzip
145 gzip => sub
146 {
147 # try-catch
148 local $@;
149 my $rv = eval
150 {
151 require IO::Compress::Gzip;
152 IO::Compress::Gzip::gzip( $_[0] => $_[1], @_[2..$#_] );
153 };
154 if( $@ )
155 {
156 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
157 }
158 # $rv or return( undef, $IO::Compress::Gzip::GzipError );
159 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Gzip::GzipError }) );
160 return( $rv );
161 },
162 # NOTE: encode -> lzf
163 lzf => sub
164 {
165 # try-catch
166 local $@;
167 my $rv = eval
168 {
169 require IO::Compress::Lzf;
170 IO::Compress::Lzf::lzf( $_[0] => $_[1], @_[2..$#_] );
171 };
172 if( $@ )
173 {
174 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
175 }
176 # $rv or return( undef, $IO::Compress::Lzf::LzfError );
177 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Lzf::LzfError }) );
178 return( $rv );
179 },
180 # NOTE: encode -> lzip
181 lzip => sub
182 {
183 # try-catch
184 local $@;
185 my $rv = eval
186 {
187 require IO::Compress::Lzip;
188 IO::Compress::Lzip::lzip( $_[0] => $_[1], @_[2..$#_] );
189 };
190 if( $@ )
191 {
192 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
193 }
194 # $rv or return( undef, $IO::Compress::Lzip::LzipError );
195 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Lzip::LzipError }) );
196 return( $rv );
197 },
198 # NOTE: encode -> lzma
199 lzma => sub
200 {
201 # try-catch
202 local $@;
203 my $rv = eval
204 {
205 require IO::Compress::Lzma;
206 IO::Compress::Lzma::lzma( $_[0] => $_[1], @_[2..$#_] );
207 };
208 if( $@ )
209 {
210 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
211 }
212 # $rv or return( undef, $IO::Compress::Lzma::LzmaError );
213 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Lzma::LzmaError }) );
214 return( $rv );
215 },
216 # NOTE: encode -> lzop
217 lzop => sub
218 {
219 # try-catch
220 local $@;
221 my $rv = eval
222 {
223 require IO::Compress::Lzop;
224 IO::Compress::Lzop::lzop( $_[0] => $_[1], @_[2..$#_] );
225 };
226 if( $@ )
227 {
228 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
229 }
230 # $rv or return( undef, $IO::Compress::Lzop::LzopError );
231 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Lzop::LzopError }) );
232 return( $rv );
233 },
234 # NOTE: encode -> lzw
235 lzw => sub
236 {
237 # try-catch
238 local $@;
239 my $rv = eval
240 {
241 require HTTP::Promise::Streem::LZW;
242 HTTP::Promise::Streem::LZW::encode_lzw( $_[0] => $_[1], @_[2..$#_] );
243 };
244 if( $@ )
245 {
246 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
247 }
248 # $rv or return( undef, $HTTP::Promise::Streem::LZW::LZWError );
249 defined( $rv ) or return( undef, HTTP::Promise::Streem::LZW->error );
250 return( $rv );
251 },
252 # NOTE: encode -> qp
253 qp => sub
254 {
255 # try-catch
256 local $@;
257 my $rv = eval
258 {
259 require HTTP::Promise::Stream::QuotedPrint;
260 HTTP::Promise::Stream::QuotedPrint::encode_qp( $_[0] => $_[1], @_[2..$#_] );
261 };
262 if( $@ )
263 {
264 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
265 }
266 # $rv or return( undef, $HTTP::Promise::Stream::QuotedPrint::QuotedPrintError );
267 defined( $rv ) or return( undef, HTTP::Promise::Stream::QuotedPrint->error );
268 return( $rv );
269 },
270 # NOTE: encode -> rawdeflate
271 rawdeflate => sub
272 {
273 # try-catch
274 local $@;
275 my $rv = eval
276 {
277 require IO::Compress::RawDeflate;
278 IO::Compress::RawDeflate::rawdeflate( $_[0] => $_[1], @_[2..$#_] );
279 };
280 if( $@ )
281 {
282 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
283 }
284 # $rv or return( undef, $IO::Compress::RawDeflate::RawDeflateError );
285 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::RawDeflate::RawDeflateError }) );
286 return( $rv );
287 },
288 # NOTE: encode -> uu
289 uu => sub
290 {
291 # try-catch
292 local $@;
293 my $rv = eval
294 {
295 require HTTP::Promise::Stream::UU;
296 HTTP::Promise::Stream::UU::encode_uu( $_[0] => $_[1], @_[2..$#_] );
297 };
298 if( $@ )
299 {
300 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
301 }
302 # $rv or return( undef, $HTTP::Promise::Stream::UU::UUError );
303 defined( $rv ) or return( undef, HTTP::Promise::Stream::UU->error );
304 return( $rv );
305 },
306 # NOTE: encode -> xz
307 xz => sub
308 {
309 # try-catch
310 local $@;
311 my $rv = eval
312 {
313 require IO::Compress::Xz;
314 IO::Compress::Xz::xz( $_[0] => $_[1], @_[2..$#_] );
315 };
316 if( $@ )
317 {
318 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
319 }
320 # $rv or return( undef, $IO::Compress::Xz::XzError );
321 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Xz::XzError }) );
322 return( $rv );
323 },
324 # NOTE: encode -> zip
325 zip => sub
326 {
327 # try-catch
328 local $@;
329 my $rv = eval
330 {
331 require IO::Compress::Zip;
332 IO::Compress::Zip::zip( $_[0] => $_[1], @_[2..$#_] );
333 };
334 if( $@ )
335 {
336 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
337 }
338 # $rv or return( undef, $IO::Compress::Zip::ZipError );
339 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Zip::ZipError }) );
340 return( $rv );
341 },
342 # NOTE: encode -> zstd
343 zstd => sub
344 {
345 # try-catch
346 local $@;
347 my $rv = eval
348 {
349 require IO::Compress::Zstd;
350 IO::Compress::Zstd::zstd( $_[0] => $_[1], @_[2..$#_] );
351 };
352 if( $@ )
353 {
354 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
355 }
356 # $rv or return( undef, $IO::Compress::Zstd::ZstdError );
357 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Compress::Zstd::ZstdError }) );
358 return( $rv );
359 },
360 },
361 decode =>
362 {
363 # NOTE: decode -> base64
364 base64 => sub
365 {
366 # try-catch
367 local $@;
368 my $rv = eval
369 {
370 require HTTP::Promise::Stream::Base64;
371 HTTP::Promise::Stream::Base64::decode_b64( $_[0] => $_[1], @_[2..$#_] );
372 };
373 if( $@ )
374 {
375 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
376 }
377 # $rv or return( undef, $HTTP::Promise::Stream::Base64::Base64Error );
378 defined( $rv ) or return( undef, HTTP::Promise::Stream::Base64->error );
379 return( $rv );
380 },
381 # NOTE: decode -> brotli
382 brotli => sub
383 {
384 # try-catch
385 local $@;
386 my $rv = eval
387 {
388 require HTTP::Promise::Stream::Brotli;
389 HTTP::Promise::Stream::Brotli::decode_bro( $_[0] => $_[1], @_[2..$#_] );
390 };
391 if( $@ )
392 {
393 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
394 }
395 # defined( $rv ) or return( undef, $HTTP::Promise::Stream::Brotli::BrotliError );
396 defined( $rv ) or return( undef, HTTP::Promise::Stream::Brotli->error );
397 return( $rv );
398 },
399 # NOTE: decode -> bzip2
400 bzip2 => sub
401 {
402 # try-catch
403 local $@;
404 my $rv = eval
405 {
406 require IO::Uncompress::Bunzip2;
407 IO::Uncompress::Bunzip2::bunzip2( $_[0] => $_[1], @_[2..$#_] );
408 };
409 if( $@ )
410 {
411 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
412 }
413 # $rv or return( undef, $IO::Uncompress::Bunzip2::Bunzip2Error );
414 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::Bunzip2::Bunzip2Error }) );
415 return( $rv );
416 },
417 # NOTE: decode -> gzip
418 gzip => sub
419 {
420 # try-catch
421 local $@;
422 my $rv = eval
423 {
424 require IO::Uncompress::Gunzip;
425 IO::Uncompress::Gunzip::gunzip( $_[0] => $_[1], @_[2..$#_] );
426 };
427 if( $@ )
428 {
429 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
430 }
431 # $rv or return( undef, $IO::Uncompress::Gunzip::GunzipError );
432 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::Gunzip::GunzipError }) );
433 return( $rv );
434 },
435 # NOTE: decode -> inflate
436 inflate => sub
437 {
438 # try-catch
439 local $@;
440 my $rv = eval
441 {
442 require IO::Uncompress::Inflate;
443 IO::Uncompress::Inflate::inflate( $_[0] => $_[1], @_[2..$#_] );
444 };
445 if( $@ )
446 {
447 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
448 }
449 # $rv or return( undef, $IO::Uncompress::Inflate::InflateError );
450 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::Inflate::InflateError }) );
451 return( $rv );
452 },
453 # NOTE: decode -> lzf
454 lzf => sub
455 {
456 # try-catch
457 local $@;
458 my $rv = eval
459 {
460 require IO::Uncompress::UnLzf;
461 IO::Uncompress::UnLzf::unlzf( $_[0] => $_[1], @_[2..$#_] );
462 };
463 if( $@ )
464 {
465 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
466 }
467 # $rv or return( undef, $IO::Uncompress::UnLzf::UnLzfError );
468 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::UnLzf::UnLzfError }) );
469 return( $rv );
470 },
471 # NOTE: decode -> lzip
472 lzip => sub
473 {
474 # try-catch
475 local $@;
476 my $rv = eval
477 {
478 require IO::Uncompress::UnLzip;
479 IO::Uncompress::UnLzip::unlzip( $_[0] => $_[1], @_[2..$#_] );
480 };
481 if( $@ )
482 {
483 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
484 }
485 # $rv or return( undef, $IO::Uncompress::UnLzip::UnLzipError );
486 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::UnLzip::UnLzipError }) );
487 return( $rv );
488 },
489 # NOTE: decode -> lzma
490 lzma => sub
491 {
492 # try-catch
493 local $@;
494 my $rv = eval
495 {
496 require IO::Uncompress::UnLzma;
497 IO::Uncompress::UnLzma::unlzma( $_[0] => $_[1], @_[2..$#_] );
498 };
499 if( $@ )
500 {
501 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
502 }
503 # $rv or return( undef, $IO::Uncompress::UnLzma::UnLzmaError );
504 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::UnLzma::UnLzmaError }) );
505 return( $rv );
506 },
507 # NOTE: decode -> lzop
508 lzop => sub
509 {
510 # try-catch
511 local $@;
512 my $rv = eval
513 {
514 require IO::Uncompress::UnLzop;
515 IO::Uncompress::UnLzop::unlzop( $_[0] => $_[1], @_[2..$#_] );
516 };
517 if( $@ )
518 {
519 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
520 }
521 # $rv or return( undef, $IO::Uncompress::UnLzop::UnLzopError );
522 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::UnLzop::UnLzopError }) );
523 return( $rv );
524 },
525 # NOTE: decode -> lzw
526 lzw => sub
527 {
528 # try-catch
529 local $@;
530 my $rv = eval
531 {
532 require HTTP::Promise::Streem::LZW;
533 HTTP::Promise::Streem::LZW::decode_lzw( $_[0] => $_[1], @_[2..$#_] );
534 };
535 if( $@ )
536 {
537 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
538 }
539 # $rv or return( undef, $HTTP::Promise::Streem::LZW::LZWError );
540 defined( $rv ) or return( undef, HTTP::Promise::Streem::LZW->error );
541 return( $rv );
542 },
543 # NOTE: decode -> qp
544 qp => sub
545 {
546 # try-catch
547 local $@;
548 my $rv = eval
549 {
550 require HTTP::Promise::Stream::QuotedPrint;
551 HTTP::Promise::Stream::QuotedPrint::decode_qp( $_[0] => $_[1], @_[2..$#_] );
552 };
553 if( $@ )
554 {
555 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
556 }
557 # $rv or return( undef, $HTTP::Promise::Stream::QuotedPrint::QuotedPrintError );
558 defined( $rv ) or return( undef, HTTP::Promise::Stream::QuotedPrint->error );
559 return( $rv );
560 },
561 # NOTE: decode -> rawinflate
562 rawinflate => sub
563 {
564 # try-catch
565 local $@;
566 my $rv = eval
567 {
568 require IO::Uncompress::RawInflate;
569 IO::Uncompress::RawInflate::rawinflate( $_[0] => $_[1], @_[2..$#_] );
570 };
571 if( $@ )
572 {
573 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
574 }
575 # $rv or return( undef, $IO::Uncompress::RawInflate::RawInflateError );
576 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::RawInflate::RawInflateError }) );
577 return( $rv );
578 },
579 # NOTE: decode -> uu
580 uu => sub
581 {
582 # try-catch
583 local $@;
584 my $rv = eval
585 {
586 require HTTP::Promise::Stream::UU;
587 HTTP::Promise::Stream::UU::decode_uu( $_[0] => $_[1], @_[2..$#_] );
588 };
589 if( $@ )
590 {
591 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
592 }
593 # $rv or return( undef, $HTTP::Promise::Stream::UU::UUError );
594 defined( $rv ) or return( undef, HTTP::Promise::Stream::UU->error );
595 return( $rv );
596 },
597 # NOTE: decode -> xz
598 xz => sub
599 {
600 # try-catch
601 local $@;
602 my $rv = eval
603 {
604 require IO::Uncompress::UnXz;
605 IO::Uncompress::UnXz::unxz( $_[0] => $_[1], @_[2..$#_] );
606 };
607 if( $@ )
608 {
609 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
610 }
611 # $rv or return( undef, $IO::Uncompress::UnXz::UnXzError );
612 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::UnXz::UnXzError }) );
613 return( $rv );
614 },
615 # NOTE: decode -> zip
616 zip => sub
617 {
618 # try-catch
619 local $@;
620 my $rv = eval
621 {
622 require IO::Uncompress::Unzip;
623 IO::Uncompress::Unzip::unzip( $_[0] => $_[1], @_[2..$#_] );
624 };
625 if( $@ )
626 {
627 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
628 }
629 # $rv or return( undef, $IO::Uncompress::Unzip::UnzipError );
630 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::Unzip::UnzipError }) );
631 return( $rv );
632 },
633 # NOTE: decode -> zstd
634 zstd => sub
635 {
636 # try-catch
637 local $@;
638 my $rv = eval
639 {
640 require IO::Uncompress::UnZstd;
641 IO::Uncompress::UnZstd::unzstd( $_[0] => $_[1], @_[2..$#_] );
642 };
643 if( $@ )
644 {
645 return( undef, HTTP::Promise::Exception->new({ message => $@ }) );
646 }
647 # $rv or return( undef, $IO::Uncompress::UnZstd::UnZstdError );
648 $rv or return( undef, HTTP::Promise::Exception->new({ message => $IO::Uncompress::UnZstd::UnZstdError }) );
649 return( $rv );
650 },
651 }
652 };
653 # rfc1945, section 3.5
654 # Ref: <https://tools.ietf.org/html/rfc1945#section-3.5>
655 $FILTER_MAP->{encode}->{ 'x-gzip' } = $FILTER_MAP->{encode}->{gzip};
656 $FILTER_MAP->{decode}->{ 'x-gzip' } = $FILTER_MAP->{decode}->{gzip};
657 $FILTER_MAP->{encode}->{ 'x-bzip2' } = $FILTER_MAP->{encode}->{bzip2};
658 $FILTER_MAP->{decode}->{ 'x-bzip2' } = $FILTER_MAP->{decode}->{bzip2};
659 $FILTER_MAP->{encode}->{ 'x-compress' } = $FILTER_MAP->{encode}->{lzw};
660 $FILTER_MAP->{decode}->{ 'x-compress' } = $FILTER_MAP->{decode}->{lzw};
661 $FILTER_MAP->{encode}->{br} = $FILTER_MAP->{encode}->{brotli};
662 $FILTER_MAP->{decode}->{br} = $FILTER_MAP->{decode}->{brotli};
663 $FILTER_MAP->{encode}->{compress} = $FILTER_MAP->{encode}->{lzw};
664 $FILTER_MAP->{decode}->{compress} = $FILTER_MAP->{decode}->{lzw};
665 $FILTER_MAP->{encode}->{uuencode} = $FILTER_MAP->{encode}->{uu};
666 $FILTER_MAP->{decode}->{uuencode} = $FILTER_MAP->{decode}->{uu};
667 # deflate <-> inflate, make the choice of word irrelevant
668 $FILTER_MAP->{decode}->{deflate} = $FILTER_MAP->{decode}->{inflate};
669 $FILTER_MAP->{encode}->{inflate} = $FILTER_MAP->{encode}->{deflate};
670 $FILTER_MAP->{decode}->{rawdeflate} = $FILTER_MAP->{decode}->{rawinflate};
671 $FILTER_MAP->{encode}->{rawinflate} = $FILTER_MAP->{encode}->{rawdeflate};
672 $FILTER_MAP->{encode}->{ 'x-zip' } = $FILTER_MAP->{encode}->{zip};
673 $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 $FILTER_MAP->{encode}->{ 'quoted-printable' } = $FILTER_MAP->{encode}->{qp};
677 $FILTER_MAP->{decode}->{ 'quoted-printable' } = $FILTER_MAP->{decode}->{qp};
678  
679 $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 $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 $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 my $self = shift( @_ );
736 my $src = shift( @_ );
737 no warnings 'uninitialized';
738 return( $self->error( "No stream was provided." ) ) if( !defined( $src ) || !length( $src ) );
739 my $type = ref( $src ) ? lc( Scalar::Util::reftype( $src ) ) : '';
740 if( ref( $src ) )
741 {
742 if( $self->_is_a( $src => 'Module::Generic::File' ) )
743 {
744 $src = "$src";
745 }
746 elsif( $type ne 'scalar' && $type ne 'glob' && $type ne 'code' )
747 {
748 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 if( $src =~ /\n/ )
754 {
755 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 $self->{compress_params} = {};
759 $self->{encoding} = undef;
760 $self->{decoding} = undef;
761 $self->{_init_strict_use_sub} = 1;
762 $self->{_exception_class} = 'HTTP::Promise::Exception';
763 $self->SUPER::init( @_ ) || return( $self->pass_error );
764 $self->source( $src );
765 $self->{read_tmp_file} = undef;
766 $self->{src_file_handle} = undef;
767 if( defined( $self->{encoding} ) && length( $self->{encoding} ) )
768 {
769 return( $self->error( "Encoding provided \"$self->{encoding}\" is unsupported." ) ) if( !exists( $FILTER_MAP->{encode}->{ $self->{encoding} } ) );
770 }
771 elsif( defined( $self->{decoding} ) && length( $self->{decoding} ) )
772 {
773 return( $self->error( "Decoding provided \"$self->{decoding}\" is unsupported." ) ) if( !exists( $FILTER_MAP->{decode}->{ $self->{decoding} } ) );
774 }
775 return( $self );
776 }
777  
778 sub as_string
779 {
780 my $self = shift( @_ );
781 my $src = $self->source;
782 if( ref( $src ) )
783 {
784 my $type = lc( Scalar::Util::reftype( $src ) );
785 if( $type eq 'scalar' )
786 {
787 return( ${$src} );
788 }
789 elsif( $type eq 'glob' )
790 {
791 if( $self->_is_a( $src => 'Module::Generic::Scalar::IO' ) )
792 {
793 return( join( '', $src->getlines ) );
794 }
795 elsif( $self->_is_object( $src ) && $self->_can( $src => 'seek' ) && $self->_can( $src => 'read' ) )
796 {
797 my $data = '';
798 $src->seek(0,0) || return( $self->error( "Unable to seek source stream glob: $!" ) );
799 while( $src->read( my $buff, 10240 ) )
800 {
801 $data .= $buff;
802 }
803 return( $data );
804 }
805 elsif( fileno( $src ) )
806 {
807 my $data = '';
808 CORE::seek( $src, 0, 0 ) || return( $self->error( "Unable to seek source stream glob: $!" ) );
809 while( CORE::read( $src, my $buff, 10240 ) )
810 {
811 $data .= $buff;
812 }
813 return( $data );
814 }
815 }
816 elsif( $self->_is_a( $src => 'Module::Generic::File' ) )
817 {
818 return( $src->content );
819 }
820 return;
821 }
822 else
823 {
824 my $f = $self->new_file( $src ) || return( $self->pass_error );
825 return( $f->content );
826 }
827 }
828  
829 sub compress_params { return( shift->_set_get_hash_as_mix_object( 'compress_params', @_ ) ); }
830  
831 sub decodable { return( shift->_decodable_encodable( 0, @_ ) ); }
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 my( $self ) = @_;
840 my $opts = {};
841 $opts = pop( @_ ) if( ref( $_[-1] ) eq 'HASH' );
842 $opts->{encoding} //= '';
843 $opts->{decoding} //= '';
844 my $dec = $opts->{decoding} || $opts->{encoding} || $self->decoding->lower;
845 my $src = $self->source;
846 # Scalar reference or glob
847 my $this = @_ >= 2 ? $_[1] : $src;
848 my $size = $self->_get_size( $this );
849
850 # No need to bother going further
851 if( !defined( $dec ) || !length( $dec ) || !$size )
852 {
853 # $stream->decode( $data );
854 return( $self ) if( @_ >= 2 );
855 # my $decoded = $stream->decode;
856 return( '' );
857 }
858 my $filters = $FILTER_MAP->{decode};
859 return( $self->error( "Unknown decoding \"$dec\"." ) ) if( !exists( $filters->{ $dec } ) );
860 my $params = $self->_io_compress_params( $opts );
861 my $rv;
862 # Decode some data provided and into the stream
863 if( @_ >= 2 )
864 {
865 ( $rv, my $err ) = $filters->{ $dec }->( $_[0] => $src, %$params );
866 return( $self->error( "Unable to decode $size bytes of data into the stream with $dec: $err" ) ) if( !defined( $rv ) );
867 return( $rv );
868 }
869 # Decode the stream and return the decoded data
870 else
871 {
872 my $buf;
873 ( $rv, my $err ) = $filters->{ $dec }->( $src => \$buf, %$params );
874 return( $self->error( "Unable to decode $size bytes of data from the stream with $dec: $err" ) ) if( !defined( $rv ) );
875 return( $buf ) if( defined( $rv ) );
876 return( $rv );
877 }
878 }
879  
880 sub decoding { return( shift->_set_get_scalar_as_object( 'decoding', @_ ) ); }
881  
882 sub encodable { return( shift->_decodable_encodable( 1, @_ ) ); }
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 my( $self ) = @_;
891 my $opts = {};
892 $opts = pop( @_ ) if( ref( $_[-1] ) eq 'HASH' );
893 $opts->{encoding} //= '';
894 my $enc = $opts->{encoding} || $self->encoding->lower;
895 my $src = $self->source;
896 # Scalar reference or glob
897 my $this = @_ >= 2 ? $_[1] : $src;
898 my $size = $self->_get_size( $this );
899
900 # No need to bother going further
901 if( !defined( $enc ) || !length( $enc ) || !$size )
902 {
903 # $stream->encode( $data );
904 return( $self ) if( @_ >= 2 );
905 # my $encoded = $stream->encode;
906 return( '' );
907 }
908 my $filters = $FILTER_MAP->{encode};
909 return( $self->error( "Unknown encoding \"$enc\". Supported encodings are: ", join( ', ', sort( keys( %$filters ) ) ) ) ) if( !exists( $filters->{ $enc } ) );
910 my $params = $self->_io_compress_params( $opts );
911 my $rv;
912 # Encode some data provided and into the stream
913 if( @_ >= 2 )
914 {
915 ( $rv, my $err ) = $filters->{ $enc }->( $_[0] => $src, %$params );
916 return( $self->error( "Unable to encode $size bytes of data into the stream with $enc: $err" ) ) if( !defined( $rv ) );
917 return( $rv );
918 }
919 # Encode the stream and return the decoded data
920 else
921 {
922 my $buf;
923 my $ref = \$buf;
924 ( $rv, my $err ) = $filters->{ $enc }->( $src => \$buf, %$params );
925 return( $self->error( "Unable to encode $size bytes of data from the stream with $enc: $err" ) ) if( !defined( $rv ) );
926 return( $buf ) if( defined( $rv ) );
927 return( $rv );
928 }
929 }
930  
931 sub encoding { return( shift->_set_get_scalar_as_object( 'encoding', @_ ) ); }
932  
933 sub encoding2suffix
934 {
935 my $self = shift( @_ );
936 my $this = shift( @_ );
937 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 => '""' ) ) ) );
938 my $encodings = $self->new_array( $self->_is_array( $this ) ? $this : [split( /[[:blank:]\h]*,[[:blank:]\h]*/, lc( "${this}" ) )] );
939 my $ext = $self->new_array;
940 foreach( @$encodings )
941 {
942 my $enc;
943 $enc = exists( $ENCODING_ALIAS->{ $_ } ) ? $ENCODING_ALIAS->{ $_ } : $_;
944 return( $self->error( "Unknown encoding provided \"${enc}\"." ) ) if( !exists( $ENCODING_SUFFIX->{ $enc } ) );
945 $ext->push( $ENCODING_SUFFIX->{ $enc } );
946 }
947 return( $ext );
948 }
949  
950 sub load
951 {
952 my $self = shift( @_ );
953 my $enc = shift( @_ ) || return( $self->error( "No encoding was provided." ) );
954 $enc = lc( $enc );
955 return(0) if( !exists( $CLASSES->{ $enc } ) );
956 my $opts = $self->_get_args_as_hash( @_ );
957 my $p = {};
958 $p->{version} = $opts->{version} if( exists( $opts->{version} ) && length( $opts->{version} ) );
959 my( $encoder, $decoder ) = @{$CLASSES->{ $enc }};
960 my $ok = 0;
961 for( $encoder, $decoder )
962 {
963 $ok++, next if( $_ eq $decoder && $decoder eq $encoder );
964 $self->_load_class( $_, $p ) || next;
965 $ok++;
966 }
967 return(1) if( $ok == 2 );
968 return(0);
969 }
970  
971 sub mime2encoding
972 {
973 my $self = shift( @_ );
974 my $mime = shift( @_ ) || return( $self->error( "No MIME type was provided." ) );
975 $mime = lc( "${mime}" );
976 # Strip parameters like '; charset=binary'
977 $mime =~ s/[[:blank:]]*;.*$// if( index( $mime, ';' ) != -1 );
978 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 my( $self ) = @_;
991 my $opts = {};
992 $opts = pop( @_ ) if( ref( $_[-1] ) eq 'HASH' );
993 $opts->{binmode} = 'raw' if( !exists( $opts->{binmode} ) || !length( $opts->{binmode} ) );
994 my $src = $self->source;
995 my $enc = $self->encoding->lower || lc( $opts->{encoding} );
996 my $dec = $self->decoding->lower || lc( $opts->{decoding} );
997 my $io = $self->{src_file_handle};
998 my $tempfile = $self->{read_tmp_file};
999 unless( $io )
1000 {
1001 $tempfile = $self->{read_tmp_file} = $self->new_tempfile ||
1002 return( $self->error( "Unable to get a new tempfile: ", $self->error ) );
1003 if( $enc )
1004 {
1005 my $params = $self->_io_compress_params( $opts );
1006 my $filters = $FILTER_MAP->{encode};
1007 return( $self->error( "Unknown encoding \"$enc\"." ) ) if( !exists( $filters->{ $enc } ) );
1008 my( $rv, $err ) = $filters->{ $enc }->( $self->_normalise( $src ) => "$tempfile", %$params );
1009 my $size = $self->_get_size( $src );
1010 return( $self->error( "Unable to encode $size bytes of data into the stream with $enc: $err" ) ) if( !defined( $rv ) );
1011 $io = $self->{src_file_handle} = $tempfile->open( '<', { binmode => $opts->{binmode} }) ||
1012 return( $self->pass_error( $tempfile ) );
1013 }
1014 elsif( $dec )
1015 {
1016 my $params = $self->_io_compress_params( $opts );
1017 my $filters = $FILTER_MAP->{decode};
1018 return( $self->error( "Unknown decoding \"$dec\"." ) ) if( !exists( $filters->{ $dec } ) );
1019 # $err, if defined, would be a HTTP::Promise::Exception object
1020 my( $rv, $err ) = $filters->{ $dec }->( $self->_normalise( $src ) => "$tempfile", %$params );
1021 my $size = $self->_get_size( $src );
1022 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 ) );
1023 $io = $self->{src_file_handle} = $tempfile->open( '<', { binmode => $opts->{binmode} }) ||
1024 return( $self->pass_error( $tempfile ) );
1025 }
1026 else
1027 {
1028 my $type = lc( Scalar::Util::reftype( $src ) );
1029 if( $type eq 'scalar' )
1030 {
1031 my $s = $self->new_scalar( $src );
1032 $io = $self->{src_file_handle} = $s->open( '<' ) ||
1033 return( $self->pass_error( $s->error ) );
1034 }
1035 elsif( $type eq 'glob' )
1036 {
1037 $io = $self->{src_file_handle} = $src;
1038 }
1039 elsif( !ref( $src ) )
1040 {
1041 my $f = $self->new_file( $src );
1042 $io = $self->{src_file_handle} = $f->open( '<', { $opts->{binmode} ? ( binmode => $opts->{binmode} ) : () }) ||
1043 return( $self->pass_error( $f->error ) );
1044 }
1045 else
1046 {
1047 return( $self->error( "I do not know how to handle source '$src'." ) );
1048 }
1049 }
1050 }
1051
1052 my $len;
1053 if( ref( $_[1] ) eq 'CODE' )
1054 {
1055 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 if( @_ >= 3 )
1059 {
1060 $len = $io->read( $buf, $_[2] );
1061 return( $self->error( "Unable to read ", $_[2], " bytes from source: $!" ) ) if( !defined( $len ) );
1062 }
1063 elsif( @_ >= 2 )
1064 {
1065 $len = $io->read( $buf, $tempfile->length );
1066 return( $self->error( "Unable to read bytes from source: $!" ) ) if( !defined( $len ) );
1067 }
1068
1069 # try-catch
1070 local $@;
1071 eval
1072 {
1073 $_[1]->( $buf );
1074 };
1075 if( $@ )
1076 {
1077 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 if( @_ >= 4 )
1083 {
1084 $len = $io->read( ${$_[1]}, $_[2], $_[3] );
1085 return( $self->error( "Unable to read ", $_[2], " bytes at offset ", $_[3], " from source: $!" ) ) if( !defined( $len ) );
1086 }
1087 elsif( @_ >= 3 )
1088 {
1089 $len = $io->read( ${$_[1]}, $_[2] );
1090 return( $self->error( "Unable to read ", $_[2], " bytes from source: $!" ) ) if( !defined( $len ) );
1091 }
1092 elsif( @_ >= 2 )
1093 {
1094 $len = $io->read( ${$_[1]}, $tempfile->length );
1095 return( $self->error( "Unable to read bytes from source: $!" ) ) if( !defined( $len ) );
1096 }
1097 }
1098 elsif( Scalar::Util::reftype( $_[1] ) eq 'GLOB' )
1099 {
1100 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 if( @_ >= 3 )
1104 {
1105 $len = $io->read( $buf, $_[2] );
1106 return( $self->error( "Unable to read ", $_[2], " bytes from source: $!" ) ) if( !defined( $len ) );
1107 my $rv = CORE::print( $_[1], $buf );
1108 return( $self->error( "Unable to print ", CORE::length( $buf ), " bytes of data to provided file handle '", $_[1], "': $!" ) ) if( !$rv );
1109 }
1110 elsif( @_ >= 2 )
1111 {
1112 my $chunklen;
1113 while( $chunklen = $io->read( $buf, 10240 ) )
1114 {
1115 $len += $chunklen;
1116 #my $rv = CORE::print( $_[1], $buf );
1117 my $rv = $_[1]->print( $buf );
1118 return( $self->error( "Unable to print ", CORE::length( $buf ), " bytes of data to provided file handle '", $_[1], "': $!" ) ) if( !$rv );
1119 }
1120 return( $self->error( "Unable to read bytes from source: $!" ) ) if( !defined( $chunklen ) );
1121 }
1122 }
1123 # A file
1124 elsif( $self->_is_a( $_[1] => 'Module::Generic::File' ) ||
1125 ( !ref( $_[1] ) &&
1126 CORE::length( $_[1] ) &&
1127 CORE::index( $_[1], "\n" ) == -1
1128 ) )
1129 {
1130 my $f = $self->new_file( $_[1] ) || return( $self->pass_error );
1131 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 if( @_ >= 3 )
1135 {
1136 $len = $io->read( $buf, $_[2] );
1137 return( $self->error( "Unable to read ", $_[2], " bytes from source: $!" ) ) if( !defined( $len ) );
1138 }
1139 elsif( @_ >= 2 )
1140 {
1141 $len = $io->read( $buf, $tempfile->length );
1142 return( $self->error( "Unable to read bytes from source: $!" ) ) if( !defined( $len ) );
1143 }
1144 my $mode = $opts->{mode} ? $opts->{mode} : '>';
1145 my $params = {};
1146 $params->{binmode} = $opts->{binmode} if( $opts->{binmode} );
1147 $params->{autoflush} = $opts->{autoflush} if( $opts->{autoflush} );
1148 $f->open( $mode, $params ) ||
1149 return( $self->pass_error( $f->error ) );
1150 $f->print( $buf ) || return( $self->pass_error( $f->error ) );
1151 $f->close;
1152 }
1153 # A regular string
1154 else
1155 {
1156 if( @_ >= 4 )
1157 {
1158 $len = $io->read( $_[1], $_[2], $_[3] );
1159 return( $self->error( "Unable to read ", $_[2], " bytes at offset ", $_[3], " from source: $!" ) ) if( !defined( $len ) );
1160 }
1161 elsif( @_ >= 3 )
1162 {
1163 $len = $io->read( $_[1], $_[2] );
1164 return( $self->error( "Unable to read ", $_[2], " bytes from source: $!" ) ) if( !defined( $len ) );
1165 }
1166 elsif( @_ >= 2 )
1167 {
1168 $len = $io->read( $_[1], $tempfile->length );
1169 return( $self->error( "Unable to read bytes from source: $!" ) ) if( !defined( $len ) );
1170 }
1171 }
1172 return( $len );
1173 }
1174  
1175 sub source { return( shift->_set_get_scalar( 'source', @_ ) ); }
1176  
1177 sub suffix2encoding
1178 {
1179 my $self = shift( @_ );
1180 my $file = shift( @_ ) || return( $self->pass_error( "No file was provided to guess encoding." ) );
1181 my @parts = reverse( split( /\./, $file ) );
1182 unless( defined( $SUFFIX_ENCODING ) && %$SUFFIX_ENCODING )
1183 {
1184 my @keys = keys( %$ENCODING_SUFFIX );
1185 my @vals = @$ENCODING_SUFFIX{ @keys };
1186 $SUFFIX_ENCODING = {};
1187 @$SUFFIX_ENCODING{ @vals } = @keys;
1188 $SUFFIX_ENCODING->{Z} = 'lzw' unless( CORE::exists( $SUFFIX_ENCODING->{Z} ) );
1189 }
1190 my $encs = $self->new_array;
1191 foreach( @parts )
1192 {
1193 if( exists( $SUFFIX_ENCODING->{ $_ } ) )
1194 {
1195 $encs->push( $SUFFIX_ENCODING->{ $_ } );
1196 }
1197 else
1198 {
1199 last;
1200 }
1201 }
1202 return( $encs->reverse );
1203 }
1204  
1205 sub supported
1206 {
1207 my $self = shift( @_ );
1208 return( $self->error( "No encoding was provided to check if it exists." ) ) if( !@_ || !defined( $_[0] ) || !length( $_[0] ) );
1209 my $this = lc( shift( @_ ) );
1210 return(1) if( exists( $FILTER_MAP->{encode}->{ $this } ) || exists( $FILTER_MAP->{decode}->{ $this } ) );
1211 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 my( $self ) = @_;
1222 # No data was provided
1223 return(0) if( !defined( $_[1] ) || !length( $_[1] ) );
1224 my $src = $self->source;
1225 my $enc = $self->encoding->lower;
1226 my $dec = $self->decoding->lower;
1227 my $type = lc( Scalar::Util::reftype( $_[1] ) );
1228 my $data;
1229 my $size;
1230 my $len;
1231 if( $type eq 'code' )
1232 {
1233 # try-catch
1234 local $@;
1235 my $buf = eval
1236 {
1237 $_[1]->()
1238 };
1239 if( $@ )
1240 {
1241 return( $self->error( "Error getting data from callback: $@" ) );
1242 }
1243 $data = \$buf;
1244 $size = length( $$data );
1245 }
1246 else
1247 {
1248 $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 if( !ref( $_[1] ) &&
1254 (
1255 ( index( $_[1], "\n" ) == -1 && !-e( $_[1] ) ) ||
1256 ( index( $_[1], "\n" ) != -1 )
1257 ) )
1258 {
1259 $data = \$_[1];
1260 }
1261 elsif( $type eq 'scalar' )
1262 {
1263 $data = $_[1];
1264 }
1265 elsif( $self->_is_a( $_[1] => 'Module::Generic::File' ) ||
1266 $self->_can( $_[1] => 'filename' ) )
1267 {
1268 $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 $data = $_[1];
1275 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' );
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 if( !ref( $data ) )
1283 {
1284 my $f = $self->new_file( $data );
1285 $data = $f->open( '<', { binmode => 'raw' } ) ||
1286 return( $self->pass_error( $f->error ) );
1287 }
1288 }
1289  
1290 my $stype = lc( Scalar::Util::reftype( $src ) );
1291 if( $stype eq 'code' )
1292 {
1293 if( $enc )
1294 {
1295 my $params = $self->_io_compress_params;
1296 # try-catch
1297 local $@;
1298 eval
1299 {
1300 $src->( $self->encode( $data, $params ) );
1301 };
1302 if( $@ )
1303 {
1304 return( $self->error( "Error executing calback to write $size bytes of data: $@" ) );
1305 }
1306 $len = $size;
1307 }
1308 elsif( $dec )
1309 {
1310 my $params = $self->_io_compress_params;
1311 # try-catch
1312 local $@;
1313 eval
1314 {
1315 $src->( $self->decode( $data, $params ) );
1316 };
1317 if( $@ )
1318 {
1319 return( $self->error( "Error executing calback to write $size bytes of data: $@" ) );
1320 }
1321 $len = $size;
1322 }
1323 else
1324 {
1325 if( $type eq 'scalar' )
1326 {
1327 $len = length( $$data );
1328 # try-catch
1329 local $@;
1330 eval
1331 {
1332 $src->( $$data );
1333 };
1334 if( $@ )
1335 {
1336 return( $self->error( "Error executing calback to write $size bytes of data: $@" ) );
1337 }
1338 }
1339 elsif( $type eq 'glob' )
1340 {
1341 my( $rv, $buf );
1342 while( $rv = CORE::read( $data, $buf, 10240 ) )
1343 {
1344 # try-catch
1345 local $@;
1346 eval
1347 {
1348 $src->( $buf );
1349 };
1350 if( $@ )
1351 {
1352 return( $self->error( "Error executing calback to write $size bytes of data: $@" ) );
1353 }
1354 $len += length( $buf );
1355 }
1356 return( $self->error( "Unable to read data from glob provided: $!" ) ) if( !defined( $rv ) );
1357 }
1358 else
1359 {
1360 my $f = $self->new_file( $data ) || return( $self->pass_error );
1361 my $fh = $f->open( '<' ) || return( $self->pass_error( $f->error ) );
1362 my $buf;
1363 my $rv = $fh->read( $buf );
1364 return( $self->error( "Unable to read data from file \"$f\" provided: $!" ) ) if( !defined( $rv ) );
1365 # try-catch
1366 local $@;
1367 eval
1368 {
1369 $src->( $buf );
1370 };
1371 if( $@ )
1372 {
1373 return( $self->error( "Error executing calback to write $size bytes of data: $@" ) );
1374 }
1375 $fh->close;
1376 $len = length( $buf );
1377 }
1378 }
1379 }
1380 else
1381 {
1382 my $filters;
1383 if( $dec )
1384 {
1385 $filters = $FILTER_MAP->{decode};
1386 }
1387 elsif( $enc )
1388 {
1389 $filters = $FILTER_MAP->{encode};
1390 }
1391
1392 my $rv;
1393 if( $dec )
1394 {
1395 my $params = $self->_io_compress_params;
1396 return( $self->error( "No encoding found for \"$dec\"." ) ) if( !exists( $filters->{ $dec } ) );
1397 # try-catch
1398 local $@;
1399 ( $rv, my $err ) = eval
1400 {
1401 $filters->{ $dec }->( $data => $src, %$params );
1402 };
1403 if( $@ )
1404 {
1405 return( $self->error( "Error ", ( $self->encode ? 'encoding' : 'decoding' ), " $size bytes of data: $@" ) );
1406 }
1407 return( $self->error( "Unable to decode data to write to source: $err" ) ) if( !defined( $rv ) );
1408 $len = $size;
1409 }
1410 elsif( $enc )
1411 {
1412 my $params = $self->_io_compress_params;
1413 return( $self->error( "No encoding found for \"$enc\"." ) ) if( !exists( $filters->{ $enc } ) );
1414 # try-catch
1415 local $@;
1416 ( $rv, my $err ) = eval
1417 {
1418 $filters->{ $enc }->( $data => $src, %$params );
1419 };
1420 if( $@ )
1421 {
1422 return( $self->error( "Error ", ( $self->encode ? 'encoding' : 'decoding' ), " $size bytes of data: $@" ) );
1423 }
1424 return( $self->error( "Unable to encode data to write to source: $err" ) ) if( !defined( $rv ) );
1425 $len = $size;
1426 }
1427 elsif( $stype eq 'scalar' )
1428 {
1429 if( $type eq 'scalar' )
1430 {
1431 $$src .= $$data;
1432 $len = length( $$data );
1433 }
1434 elsif( $type eq 'glob' )
1435 {
1436 my( $rv, $buf );
1437 while( $rv = CORE::read( $data, $buf, 10240 ) )
1438 {
1439 $$src .= $buf;
1440 $len += length( $buf );
1441 }
1442 return( $self->error( "Unable to read data from glob provided: $!" ) ) if( !defined( $rv ) );
1443 }
1444 else
1445 {
1446 my $f = $self->new_file( $data ) || return( $self->pass_error );
1447 my $fh = $f->open( '<' ) ||
1448 return( $self->pass_error( $f->error ) );
1449 my $buf;
1450 my $rv = $fh->read( $buf );
1451 return( $self->error( "Unable to read data from file \"$f\" provided: $!" ) ) if( !defined( $rv ) );
1452 $$src .= $buf;
1453 $len = length( $buf );
1454 }
1455 }
1456 elsif( $stype eq 'glob' )
1457 {
1458 if( $type eq 'scalar' )
1459 {
1460 print( $src, $$data ) ||
1461 return( $self->error( "Unable to write ", length( $$data ), " bytes of data to source glob: $!" ) );
1462 $len = length( $$data );
1463 }
1464 elsif( $type eq 'glob' )
1465 {
1466 my $buf;
1467 while( CORE::read( $data, $buf, 10240 ) )
1468 {
1469 print( $src, $buf ) ||
1470 return( $self->error( "Unable to write ", length( $buf ), " bytes of data to source glob: $!" ) );
1471 $len += length( $buf );
1472 }
1473 }
1474 else
1475 {
1476 my $f = $self->new_file( $data ) || return( $self->pass_error );
1477 my $fh = $f->open( '<' ) ||
1478 return( $self->pass_error( $f->error ) );
1479 my $buf;
1480 while( $fh->read( $buf, 10240 ) )
1481 {
1482 print( $src, $buf ) ||
1483 return( $self->error( "Unable to write ", length( $buf ), " bytes of data to source glob: $!" ) );
1484 $len += length( $buf );
1485 }
1486 }
1487 }
1488 else
1489 {
1490 my $f = $self->new_file( $src ) || return( $self->pass_error );
1491 my $fh = $f->open( '>', { autoflush => 1 } ) || return( $self->pass_error( $f->error ) );
1492 if( $type eq 'scalar' )
1493 {
1494 $fh->print( $$data ) ||
1495 return( $self->error( "Unable to write ", length( $$data ), " bytes of data to file \"$f\": $!" ) );
1496 $len = length( $$data );
1497 }
1498 elsif( $type eq 'glob' )
1499 {
1500 my $buf;
1501 while( CORE::read( $data, $buf, 10240 ) )
1502 {
1503 $fh->print( $buf ) ||
1504 return( $self->error( "Unable to write ", length( $buf ), " bytes of data to file \"$f\": $!" ) );
1505 $len += length( $buf );
1506 }
1507 }
1508 else
1509 {
1510 my $f2 = $self->new_file( $data ) || return( $self->pass_error );
1511 my $fh2 = $f2->open( '<' ) ||
1512 return( $self->pass_error( $f2->error ) );
1513 my $buf;
1514 while( $fh2->read( $buf, 10240 ) )
1515 {
1516 $fh->print( $buf ) ||
1517 return( $self->error( "Unable to write ", length( $buf ), " bytes of data to source file \"$f\": $!" ) );
1518 $len += length( $buf );
1519 }
1520 $fh2->close;
1521 }
1522 $fh->close;
1523 }
1524 }
1525 return( $len );
1526 }
1527  
1528 sub _decodable_encodable
1529 {
1530 my $self = shift( @_ );
1531 # 1 for encodable, 0 for decodable
1532 my $enc_or_dec = shift( @_ );
1533 my $what = shift( @_ ) || 'all';
1534 my $list = $self->new_array;
1535 my $offset = $enc_or_dec ? 0 : 1;
1536 if( $self->_is_array( $what ) )
1537 {
1538 $list = $what;
1539 }
1540 elsif( $what eq 'all' || $what eq 'auto' )
1541 {
1542 $list = [sort( keys( %$CLASSES ) )];
1543 }
1544 elsif( $what eq 'browser' )
1545 {
1546 foreach( keys( %$CLASSES ) )
1547 {
1548 $list->push( $_ ) if( $CLASSES->{ $_ }->[2] );
1549 }
1550 }
1551 else
1552 {
1553 return( $self->error( "Unsupported keyword '$what' used." ) );
1554 }
1555
1556 my $res = $self->new_array;
1557 foreach my $enc ( @$list )
1558 {
1559 # inflate is just an alias for deflate
1560 next if( $enc eq 'inflate' || $enc eq 'rawinflate' || substr( $enc, 0, 2 ) eq 'x-' );
1561 if( !exists( $CLASSES->{ $enc } ) )
1562 {
1563 warn( "Unsupported content encoding \"$enc\"." ) if( $self->_is_warnings_enabled( 'HTTP::Promise' ) );
1564 next;
1565 }
1566 my $encoder_class = $CLASSES->{ $enc }->[$offset];
1567 my $is_installed_method = ( $enc_or_dec ? 'is_encoder_installed' : 'is_decoder_installed' );
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 if( substr( $encoder_class, 0, length( 'HTTP::Promise::Stream' ) ) eq 'HTTP::Promise::Stream' )
1570 {
1571 $self->_load_class( $encoder_class ) || return( $self->pass_error );
1572 }
1573  
1574 if( my $coderef = $encoder_class->can( $is_installed_method ) )
1575 {
1576 my $rv = $coderef->();
1577 $res->push( $enc ) if( $coderef->() );
1578 }
1579 elsif( $self->_is_class_loadable( $encoder_class ) )
1580 {
1581 $res->push( $enc );
1582 }
1583 }
1584 return( $res );
1585 }
1586  
1587 sub _encoding2suffix_map
1588 {
1589 my $self = shift( @_ );
1590 unless( defined( $ENCODING_SUFFIX ) && %$ENCODING_SUFFIX )
1591 {
1592 # Initialisation défensive - normalement déjà fait dans le bloc BEGIN
1593 $ENCODING_SUFFIX = {};
1594 }
1595 my $copy = { %{$ENCODING_SUFFIX} };
1596 return( $copy );
1597 }
1598  
1599 sub _get_size
1600 {
1601 my $self = shift( @_ );
1602 if( ref( $_[0] ) )
1603 {
1604 my $type = lc( Scalar::Util::reftype( $_[0] ) );
1605 if( $type eq 'scalar' )
1606 {
1607 return( length( ${$_[0]} ) );
1608 }
1609 elsif( $type eq 'glob' )
1610 {
1611 if( $self->_is_a( $_[0] => 'Module::Generic::Scalar::IO' ) )
1612 {
1613 return( $_[0]->size );
1614 }
1615 elsif( $self->_is_object( $_[0] ) && $self->_can( $_[0] => 'size' ) )
1616 {
1617 return( $_[0]->size );
1618 }
1619 elsif( fileno( $_[0] ) )
1620 {
1621 return( -s( $_[0] ) );
1622 }
1623 }
1624 elsif( $self->_is_a( $_[0] => 'Module::Generic::File' ) )
1625 {
1626 return( $_[0]->size );
1627 }
1628 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] ) &&
1635 (
1636 ( index( $_[0], "\n" ) == -1 && !-e( $_[0] ) ) ||
1637 ( index( $_[0], "\n" ) != -1 )
1638 ) )
1639 {
1640 return( length( $_[0] ) );
1641 }
1642 else
1643 {
1644 return( -s( $_[0] ) );
1645 }
1646 }
1647  
1648 sub _io_compress_params
1649 {
1650 my $self = shift( @_ );
1651 my $opts = {};
1652 my $ref = $self->compress_params;
1653 if( @_ )
1654 {
1655 $opts = shift( @_ );
1656 my @keys = grep( /^[A-Z]\w+$/, keys( %$opts ) );
1657 @$ref{ @keys } = @$opts{ @keys } if( scalar( @keys ) );
1658 }
1659 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 my $self = shift( @_ );
1670 if( ref( $_[0] ) )
1671 {
1672 my $type = lc( Scalar::Util::reftype( $_[0] ) );
1673 if( $type eq 'scalar' )
1674 {
1675 # if it is a regular scalar reference, we return it
1676 # return( $self->_is_object( $_[0] ) ? \${$_[0]} : $_[0] );
1677 if( $self->_is_object( $_[0] ) )
1678 {
1679 my $tmp = ${$_[0]};
1680 return( \$tmp );
1681 }
1682 else
1683 {
1684 return( $_[0] );
1685 }
1686 }
1687 elsif( $type eq 'glob' )
1688 {
1689 return( $_[0] );
1690 }
1691 elsif( $self->_is_a( $_[0] => 'Module::Generic::File' ) || $self->_can( $_[0] => 'filename' ) )
1692 {
1693 return( $_[0]->filename );
1694 }
1695 else
1696 {
1697 return( $_[0] );
1698 }
1699 }
1700 else
1701 {
1702 return( $_[0] );
1703 }
1704 }
1705  
1706 # NOTE: sub FREEZE is inherited
1707  
1708 sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
1709  
1710 sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
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 use strict;
1721 use warnings;
1722 use parent qw( Module::Generic );
1723 use vars qw( $VERSION $EXCEPTION_CLASS );
1724 use Module::Generic::File::IO;
1725 use Module::Generic::Scalar::IO;
1726 # use Nice::Try;
1727 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
1728 our $VERSION = $HTTP::Promise::Stream::VERSION;
1729 };
1730  
1731 use strict;
1732 use warnings;
1733  
1734 sub init
1735 {
1736 my $self = shift( @_ );
1737 my $class = ( ref( $self ) || $self );
1738 $self->{_init_strict_use_sub} = 1;
1739 no strict 'refs';
1740 $self->{_exception_class} = defined( ${"${class}\::EXCEPTION_CLASS"} ) ? ${"${class}\::EXCEPTION_CLASS"} : $EXCEPTION_CLASS;
1741 $self->SUPER::init( @_ ) || return( $self->pass_error );
1742 return( $self );
1743 }
1744  
1745 sub _get_glob_from_arg
1746 {
1747 my $self = shift( @_ );
1748 my $this = shift( @_ );
1749 return( $self->error( "No argument was provided." ) ) if( !defined( $this ) || ( !ref( $this ) && !length( $this ) ) );
1750 my $opts = $self->_get_args_as_hash( @_ );
1751 $opts->{write} = 0 if( !exists( $opts->{write} ) );
1752 my $mode = $opts->{write} ? '+>' : '<';
1753 my $fh;
1754 my $is_native_glob = 0;
1755 if( $self->_is_glob( $this ) )
1756 {
1757 $fh = $this;
1758 # even if this is a scalar reference opened in memory, perl will return -1, which is true
1759 $is_native_glob++ if( fileno( $this ) );
1760 }
1761 elsif( $self->_is_scalar( $this ) )
1762 {
1763 $fh = Module::Generic::Scalar::IO->new( $this, $mode ) ||
1764 return( $self->pass_error( Module::Generic::Scalar::IO->error ) );
1765 $is_native_glob++;
1766 }
1767 else
1768 {
1769 my $f = $self->new_file( "$this" ) || return( $self->pass_error );
1770 return( $self->error( "File '$this' does not exist." ) ) if( !$f->exists && !$opts->{write} );
1771 $fh = $f->open( $mode, { binmode => 'raw', ( $opts->{write} ? ( autoflush => 1 ) : () ) } ) ||
1772 return( $self->pass_error( $f->error ) );
1773 $is_native_glob++;
1774 }
1775 my $flags;
1776 if( $self->_can( $fh => 'fcntl' ) )
1777 {
1778 $flags = $fh->fcntl( F_GETFL, 0 );
1779 }
1780 else
1781 {
1782 $flags = fcntl( $fh, F_GETFL, 0 );
1783 }
1784
1785 if( defined( $flags ) )
1786 {
1787 if( $opts->{write} )
1788 {
1789 unless( $flags & ( O_RDWR | O_WRONLY | O_APPEND ) )
1790 {
1791 return( $self->error( "Filehandle provided does not have write permission enabled." ) );
1792 }
1793 }
1794 # read mode then
1795 else
1796 {
1797 unless( ( ( $flags & O_RDONLY ) == O_RDONLY ) || ( $flags & O_RDWR ) )
1798 {
1799 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 my $op;
1808 my $meth;
1809 if( $opts->{write} )
1810 {
1811 if( $is_native_glob )
1812 {
1813 $op = sub
1814 {
1815 my $rv = print( $fh @_ );
1816 return( $self->error( "Error writing ", CORE::length( $_[0] ), " bytes of data to output: $!" ) ) if( !defined( $rv ) );
1817 return( $rv );
1818 };
1819 }
1820 elsif( ( $meth = ( $self->_can( $fh => 'print' ) || $self->_can( $fh => 'write' ) ) ) )
1821 {
1822 $op = sub
1823 {
1824 # try-catch
1825 local $@;
1826 my $rv = eval
1827 {
1828 $fh->$meth( @_ );
1829 };
1830 if( $@ )
1831 {
1832 return( $self->error( "Error writing ", CORE::length( $_[0] ), " bytes of data to output: $@" ) );
1833 }
1834 if( !defined( $rv ) )
1835 {
1836 my $err;
1837 if( defined( $! ) )
1838 {
1839 $err = $!;
1840 }
1841 elsif( $self->_can( $fh => 'error' ) )
1842 {
1843 $err = $fh->error;
1844 }
1845 elsif( $self->_can( $fh => 'errstr' ) )
1846 {
1847 $err = $fh->errstr;
1848 }
1849 return( $self->error( "Error writing ", CORE::length( $_[0] ), " bytes of data to output: $err" ) );
1850 }
1851 return( $rv );
1852 };
1853 }
1854 else
1855 {
1856 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 if( $is_native_glob )
1862 {
1863 $op = sub
1864 {
1865 my $n = read( $fh, $_[0], $_[1] );
1866 return( $self->error( "Error reading ", $_[1], " bytes of data from input: $!" ) ) if( !defined( $n ) );
1867 return( $n );
1868 };
1869 }
1870 elsif( $self->_can( $fh => 'read' ) )
1871 {
1872 $op = sub
1873 {
1874 # try-catch
1875 local $@;
1876 my $n = eval
1877 {
1878 $fh->read( @_ );
1879 };
1880 if( $@ )
1881 {
1882 return( $self->error( "Error reading ", $_[1], " bytes of data from input: $@" ) );
1883 }
1884 if( !defined( $n ) )
1885 {
1886 my $err;
1887 if( defined( $! ) )
1888 {
1889 $err = $!;
1890 }
1891 elsif( $self->_can( $fh => 'error' ) )
1892 {
1893 $err = $fh->error;
1894 }
1895 elsif( $self->_can( $fh => 'errstr' ) )
1896 {
1897 $err = $fh->errstr;
1898 }
1899 return( $self->error( "Error reading ", $_[1], " bytes of data from intput: $err" ) );
1900 }
1901 return( $n );
1902 };
1903 }
1904 else
1905 {
1906 return( $self->error( "The file handle provided is not a native opened one and does not support the read() method." ) );
1907 }
1908 }
1909 return( $fh, $op );
1910 }
1911  
1912 # NOTE: sub FREEZE is inherited
1913  
1914 sub STORABLE_freeze { CORE::return( CORE::shift->FREEZE( @_ ) ); }
1915  
1916 sub STORABLE_thaw { CORE::return( CORE::shift->THAW( @_ ) ); }
1917  
1918 # NOTE: sub THAW is inherited
1919 }
1920  
1921 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