| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Stream/UU.pm | ||||||||||||||||||||||||||||
| 3 | ## Version v0.2.0 | ||||||||||||||||||||||||||||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||||||||||||||||||
| 6 | ## Created 2022/04/29 | ||||||||||||||||||||||||||||
| 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::UU; | ||||||||||||||||||||||||||||
| 15 | BEGIN | ||||||||||||||||||||||||||||
| 16 | 3 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 3 | warnings::register_categories( 'HTTP::Promise' ); | |||||||||||||||||||||||||||
| 20 | 3 | ▶ | use HTTP::Promise::Stream; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 3 | ▶ | use parent -norequire, qw( HTTP::Promise::Stream::Generic ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 3 | ▶ | use vars qw( @EXPORT_OK $VERSION $EXCEPTION_CLASS $UUError ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 3 | ▶ | use constant { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | 3 | DECODE_BUFFER_SIZE => 1024, | |||||||||||||||||||||||||||
| 25 | ENCODE_BUFFER_SIZE => 45, | ||||||||||||||||||||||||||||
| 26 | 3 | }; | |||||||||||||||||||||||||||
| 27 | 3 | our @EXPORT_OK = qw( decode_uu encode_uu ); | |||||||||||||||||||||||||||
| 28 | 3 | our $EXCEPTION_CLASS = 'HTTP::Promise::Exception'; | |||||||||||||||||||||||||||
| 29 | 3 | our $VERSION = 'v0.2.0'; | |||||||||||||||||||||||||||
| 30 | }; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 33 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | sub decode | ||||||||||||||||||||||||||||
| 36 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
decode: called CC=12 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||
| 37 | 1 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 38 | 1 | my $from = shift( @_ ); | |||||||||||||||||||||||||||
| 39 | 1 | my $to = shift( @_ ); | |||||||||||||||||||||||||||
| 40 | 1 | my $opts = $self->_get_args_as_hash( @_ ); | |||||||||||||||||||||||||||
| 41 | 1 | my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from ); | |||||||||||||||||||||||||||
| 42 | 1 | my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 ); | |||||||||||||||||||||||||||
| 43 | 1 | ▶ | return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $from_fh and defined $to_fh
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
| |||||||||||||||||||||||||||||
| 44 | 1 | my( $n, $buff ); | |||||||||||||||||||||||||||
| 45 | 1 | $buff = ''; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | my $getline = sub | ||||||||||||||||||||||||||||
| 48 | 8 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=17.5
| |||||||||||||||||||||||||||||
| 49 | 8 | ▶ | if( length( $buff ) > 0 && $buff =~ s/^((?:[^\015\012]+)\015?\012)// ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
length $buff > 0 and $buff =~ s/^((?:[^\015\012]+)\015?\012)//
MC/DC50%1/2
length $buff > 0 and $buff =~ s/^((?:[^\015\012]+)\015?\012)//
length $buff > 0
$buff =~ s/^((?:[^\015\012]+)\015?\012)//
Truth table66%2/3
A and B
A= length $buff > 0B= $buff =~ s/^((?:[^\015\012]+)\015?\012)//
| |||||||||||||||||||||||||||||
| 50 | { | ||||||||||||||||||||||||||||
| 51 | 7 | return( $1 ); | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | else | ||||||||||||||||||||||||||||
| 54 | { | ||||||||||||||||||||||||||||
| 55 | 1 | ▶ | while( $n = $reader->( $buff, DECODE_BUFFER_SIZE, length( $buff ) ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | { | ||||||||||||||||||||||||||||
| 57 | 1 | ▶ | if( $buff =~ s/^((?:[^\015\012]+)\015?\012)// ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 58 | { | ||||||||||||||||||||||||||||
| 59 | 1 | return( $1 ); | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | else | ||||||||||||||||||||||||||||
| 62 | { | ||||||||||||||||||||||||||||
| 63 | 0 | next; | |||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | 0 | return; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 1 | }; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 1 | my( $mode, $fname ); | |||||||||||||||||||||||||||
| 71 | 1 | my $done; | |||||||||||||||||||||||||||
| 72 | 1 | local $_; | |||||||||||||||||||||||||||
| 73 | # Credit: brian d foy <https://metacpan.org/dist/PerlPowerTools/view/bin/uudecode> | ||||||||||||||||||||||||||||
| 74 | 1 | ▶ | READ: while( defined( $_ = $getline->() ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | { | ||||||||||||||||||||||||||||
| 76 | 1 | ▶ | next unless( ( $mode, $fname ) = /^begin[[:blank:]\h]+(\d+)[[:blank:]\h]+(\S+)/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | 1 | $self->filename( $fname ); | |||||||||||||||||||||||||||
| 78 | 1 | $self->mode( $mode ); | |||||||||||||||||||||||||||
| 79 | 1 | $opts->{filename} = $fname; | |||||||||||||||||||||||||||
| 80 | 1 | $opts->{mode} = $mode; | |||||||||||||||||||||||||||
| 81 | 1 | $done = 0; | |||||||||||||||||||||||||||
| 82 | 1 | ▶ | LINE: while( defined( $_ = $getline->() ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||
| 84 | 7 | ▶ | if( /^end$/ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||
| 86 | 1 | $done = 1; | |||||||||||||||||||||||||||
| 87 | 1 | last READ; | |||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||
| 89 | 6 | ▶ | next LINE if( /[a-z]/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 6 | ▶ | next LINE unless int( ( ( ( ord( $_ ) - 32 ) & 077 ) + 2 ) / 3 ) == int( length( $_ ) / 4 ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 6 | my $rv = $writer->( unpack( 'u', $_ ) ); | |||||||||||||||||||||||||||
| 92 | 6 | ▶ | return( $self->pass_error ) if( !defined( $rv ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | 1 | ▶ | return( $self->error( "No UU encoded data found." ) ) if( !defined( $done ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 1 | ▶ | return( $self->error( "Missing end. input data stream may be truncated." ) ) if( !$done ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | 1 | return( $self ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | sub decode_uu | ||||||||||||||||||||||||||||
| 101 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
decode_uu: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 0 | my $s = __PACKAGE__->new; | |||||||||||||||||||||||||||
| 103 | 0 | my $rv = $s->decode( @_ ); | |||||||||||||||||||||||||||
| 104 | 0 | ▶ | if( !defined( $rv ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | { | ||||||||||||||||||||||||||||
| 106 | 0 | $UUError = $s->error; | |||||||||||||||||||||||||||
| 107 | 0 | return( $s->pass_error ); | |||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | else | ||||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||
| 111 | 0 | undef( $UUError ); | |||||||||||||||||||||||||||
| 112 | 0 | return( $rv ); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub encode | ||||||||||||||||||||||||||||
| 117 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
encode: called CC=11 SCAR=26.6
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 1 | my $self = shift( @_ ); | |||||||||||||||||||||||||||
| 119 | 1 | my $from = shift( @_ ); | |||||||||||||||||||||||||||
| 120 | 1 | my $to = shift( @_ ); | |||||||||||||||||||||||||||
| 121 | 1 | my $opts = $self->_get_args_as_hash( @_ ); | |||||||||||||||||||||||||||
| 122 | 1 | my( $from_fh, $reader ) = $self->_get_glob_from_arg( $from ); | |||||||||||||||||||||||||||
| 123 | 1 | my( $to_fh, $writer ) = $self->_get_glob_from_arg( $to, write => 1 ); | |||||||||||||||||||||||||||
| 124 | 1 | ▶ | return( $self->pass_error ) if( !defined( $from_fh ) || !defined( $to_fh ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $from_fh and defined $to_fh
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
| |||||||||||||||||||||||||||||
| 125 | 1 | ▶ | my $fname = ( exists( $opts->{filename} ) && length( $opts->{filename} ) ) ? $opts->{filename} : 'unknown.bin'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $opts->{"filename"} and length $opts->{"filename"}
MC/DC0%0/2
exists $opts->{"filename"} and length $opts->{"filename"}
exists $opts->{"filename"}
length $opts->{"filename"}
Truth table33%1/3
A and B
A= exists $opts->{"filename"}B= length $opts->{"filename"}
| |||||||||||||||||||||||||||||
| 126 | 1 | ▶ | my $mode = ( exists( $opts->{mode} ) && length( $opts->{mode} ) ) ? $opts->{mode} : 0644; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $opts->{"mode"} and length $opts->{"mode"}
MC/DC0%0/2
exists $opts->{"mode"} and length $opts->{"mode"}
exists $opts->{"mode"}
length $opts->{"mode"}
Truth table33%1/3
A and B
A= exists $opts->{"mode"}B= length $opts->{"mode"}
| |||||||||||||||||||||||||||||
| 127 | 1 | my( $n, $buff ); | |||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | my $rv = $writer->( sprintf( "begin %03o $fname\n", $mode ) ); | |||||||||||||||||||||||||||
| 130 | 1 | ▶ | return( $self->pass_error ) if( !defined( $rv ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 1 | ▶ | while( $n = $reader->( $buff, ENCODE_BUFFER_SIZE ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | { | ||||||||||||||||||||||||||||
| 133 | 5 | $writer->( pack( 'u', $buff ) ); | |||||||||||||||||||||||||||
| 134 | } | ||||||||||||||||||||||||||||
| 135 | 1 | ▶ | defined( $writer->( "`\n" ) ) or return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | 1 | ▶ | defined( $writer->( "end\n" ) ) or return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | 1 | return( $self ); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | sub encode_uu | ||||||||||||||||||||||||||||
| 141 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
encode_uu: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 142 | 0 | my $s = __PACKAGE__->new; | |||||||||||||||||||||||||||
| 143 | 0 | my $rv = $s->encode( @_ ); | |||||||||||||||||||||||||||
| 144 | 0 | ▶ | if( !defined( $rv ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||
| 146 | 0 | $UUError = $s->error; | |||||||||||||||||||||||||||
| 147 | 0 | return( $s->pass_error ); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | else | ||||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||
| 151 | 0 | undef( $UUError ); | |||||||||||||||||||||||||||
| 152 | 0 | return( $rv ); | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1 | ▶ | sub filename { return( shift->_set_get_scalar( 'filename', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
filename: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | ▶ | sub is_decoder_installed { return(1); } | ||||||||||||||||||||||||||
|
Subroutine
is_decoder_installed: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub is_encoder_installed { return(1); } | ||||||||||||||||||||||||||
|
Subroutine
is_encoder_installed: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | ▶ | sub mode { return( shift->_set_get_number( 'mode', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
mode: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # NOTE: sub FREEZE is inherited | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | # NOTE: sub STORABLE_freeze is inherited | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | # NOTE: sub STORABLE_thaw is inherited | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | # NOTE: sub THAW is inherited | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | 3 | 1; | |||||||||||||||||||||||||||
| 173 | # NOTE: POD | ||||||||||||||||||||||||||||
| 174 | __END__ | ||||||||||||||||||||||||||||