Promise/Stream/Base64.pm

filter: Statementstmt 84.0% 74 / 88 Branchbran 57.1% 16 / 28 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 77.7% 14 / 18 Podpod 100% 6 / 6 totaltotal 74.6% 112 / 150 CC 17 SCAR 14.8
CC
17
Cov
75%
encode
8.2
decode
6.3
is_decoder_installed
6.0
CRAP 4.4
« Promise/Headers/ContentType.pm ↑ Summary Promise/Headers/ServerTiming.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Stream/Base64.pm
3 ## Version v0.2.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/04/28
7 ## Modified 2023/09/08
8 ## All rights reserved.
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTTP::Promise::Stream::Base64;
15 BEGIN
16 6 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 warnings::register_categories( 'HTTP::Promise' );
20 6 ▶ use HTTP::Promise::Stream;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 ▶ use parent -norequire, qw( HTTP::Promise::Stream::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 6 ▶ use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $Base64Error );
Subroutine BEGIN: called CC=1 SCAR=0.0
23 6 ▶ use Crypt::Misc ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 ▶ use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ENCODE_BUFFER_SIZE => 300,
26 DECODE_BUFFER_SIZE => ( 32 * 1024 ),
27 6 };
28 6 our @EXPORT_OK = qw( decode_b64 encode_b64 );
29 6 our $EXCEPTION_CLASS = 'HTTP::Promise::Exception';
30 6 our $VERSION = 'v0.2.0';
31 };
32  
33 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 sub decode
37 5 ▶{
Subroutine decode: called CC=6 SCAR=18.4
Pod documented
38 5 my $self = shift( @_ );
39 5 my $from = shift( @_ );
40 5 my $to = shift( @_ );
41 5 my $opts = $self->_get_args_as_hash( @_ );
42 5 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
43 5 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
44 5 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 5 5
Condition33%1/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
005
MC/DC0%0/2
defined $from_fh and defined $to_fh
defined $from_fh defined $to_fh
Truth table33%1/3
A and B
A= defined $from_fhB= defined $to_fh
ABresult
0X0
100
111
45 5 my( $n, $buff );
46
47 5 ▶ while( $n = $reader->( $buff, DECODE_BUFFER_SIZE ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($n = &$reader($buff, 32768)) 5 5 10
48 {
49 5 my $decoded = Crypt::Misc::decode_b64( $buff );
50 5 my $rv = $writer->( $decoded );
51 5 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 5 5
52 }
53 5 ▶ return( $self->pass_error ) if( !defined( $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 5 5
54 5 return( $self );
55 }
56  
57 sub decode_b64
58 4 ▶{
Subroutine decode_b64: called CC=2 SCAR=7.6
Pod documented
59 4 my $s = __PACKAGE__->new;
60 4 my $rv = $s->decode( @_ );
61 4 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 4 4
62 {
63 0 $Base64Error = $s->error;
64 0 return( $s->pass_error );
65 }
66 else
67 {
68 4 undef( $Base64Error );
69 4 return( $rv );
70 }
71 }
72  
73 sub encode
74 6 ▶{
Subroutine encode: called CC=8 SCAR=21.1
Pod documented
75 6 my $self = shift( @_ );
76 6 my $from = shift( @_ );
77 6 my $to = shift( @_ );
78 6 my $opts = $self->_get_args_as_hash( @_ );
79 6 my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from );
80 6 my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 );
81 6 ▶ return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $from_fh and defined $to_fh 0 6 6
Condition33%1/3
defined $from_fh and defined $to_fh
!ll&&!rl&&r
006
MC/DC0%0/2
defined $from_fh and defined $to_fh
defined $from_fh defined $to_fh
Truth table33%1/3
A and B
A= defined $from_fhB= defined $to_fh
ABresult
0X0
100
111
82 6 ▶ my $eol = exists( $opts->{eol} ) ? $opts->{eol} : $/;
Branch: 1 branch
BranchTrueFalseTotal
exists $opts->{'eol'} ? : 1 5 6
83 6 my $has_eol = length( $eol );
84 6 my( $n, $buff );
85
86 6 ▶ while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($n = &$reader($buff, 300)) 306 6 312
87 {
88 306 my $encoded = Crypt::Misc::encode_b64( $buff );
89 306 ▶ if( $has_eol )
Branch: 1 branch
BranchTrueFalseTotal
if ($has_eol) 305 1 306
90 {
91 305 $encoded =~ s/(.{76})/$1$eol/g;
92 }
93 306 my $rv = $writer->( $encoded );
94 306 ▶ return( $self->pass_error ) if( !defined( $rv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rv 0 306 306
95 }
96 6 ▶ return( $self->pass_error ) if( !defined( $n ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 6 6
97 6 return( $self );
98 }
99  
100 sub encode_b64
101 5 ▶{
Subroutine encode_b64: called CC=2 SCAR=7.6
Pod documented
102 5 my $s = __PACKAGE__->new;
103 5 my $rv = $s->encode( @_ );
104 5 ▶ if( !defined( $rv ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $rv) { } 0 5 5
105 {
106 0 $Base64Error = $s->error;
107 0 return( $s->pass_error );
108 }
109 else
110 {
111 5 undef( $Base64Error );
112 5 return( $rv );
113 }
114 }
115  
116 sub is_decoder_installed
117 0 ▶{
Subroutine is_decoder_installed: not called CC=2 SCAR=17.9
Pod documented
118 0 local $@;
119 eval
120 0 {
121 0 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
122 0 require Crypt::Misc;
123 };
124 0 ▶ return( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
125 }
126  
127 sub is_encoder_installed
128 0 ▶{
Subroutine is_encoder_installed: not called CC=2 SCAR=17.9
Pod documented
129 0 local $@;
130 eval
131 0 {
132 0 ▶ local $SIG{__DIE__} = sub{};
Subroutine __ANON__: not called CC=1 SCAR=6.9
133 0 require Crypt::Misc;
134 };
135 0 ▶ return( $@ ? 0 : 1 );
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
136 }
137  
138 # NOTE: sub FREEZE is inherited
139  
140 # NOTE: sub STORABLE_freeze is inherited
141  
142 # NOTE: sub STORABLE_thaw is inherited
143  
144 # NOTE: sub THAW is inherited
145  
146 6 1;
147 # NOTE: POD
148 __END__
« Promise/Headers/ContentType.pm ↑ Summary Promise/Headers/ServerTiming.pm »