blib/lib/Langertha/HTTP/BoundedDecode.pm

filter: Statementstmt 89.3% 42 / 47 Branchbran 53.3% 16 / 30 Conditioncond 27.2% 3 / 11 MC/DCmcdc 14.2% 1 / 7 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 67.3% 68 / 101 CC 22 SCAR 24.9
CC
22
Cov
69%
_bunzip_capped
15.6
_inflate_capped
15.1
decode_within
6.0
CRAP 12.1
« blib/lib/Langertha/Role/PromptCache.pm ↑ Summary blib/lib/Langertha/Virtual/Server.pm »
1 package Langertha::HTTP::BoundedDecode;
2 # ABSTRACT: Bounded Content-Encoding inflate shared by the response-body decoders
3 33 our $VERSION = '0.505';
4 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 # The Content-Encodings this decoder can undo, mapped to the inflate kind.
9 # identity is a no-op; gzip/deflate/bzip2 (plus the x- aliases HTTP::Message
10 # accepts) inflate; anything else is refused by the caller's
11 # unbounded_encoding handler, because it cannot be bounded here.
12 33 my %INFLATE = (
13 identity => undef,
14 gzip => 'gzip', 'x-gzip' => 'gzip',
15 deflate => 'zlib', 'x-deflate' => 'zlib',
16 bzip2 => 'bzip2', 'x-bzip2' => 'bzip2',
17 );
18  
19 # Undo $content_encoding on $body within $max decoded bytes. $content_encoding
20 # is the raw Content-Encoding header value (a comma list is applied in order,
21 # so undone in reverse). $handlers holds the three croak points; each is
22 # expected to croak (its return value is ignored):
23 # too_big => sub { my ($max) = @_; ... } # decoded past $max
24 # undecodable => sub { my ($encoding) = @_; ... } # corrupt / not that encoding
25 # unbounded_encoding => sub { my ($encoding) = @_; ... } # an encoding we cannot bound
26 236 ▶sub decode_within {
Subroutine decode_within: called CC=6 SCAR=17.9
Pod documented
27 236 my ( $body, $content_encoding, $max, $handlers ) = @_;
28 236 ▶ my @encodings = grep { length } map { s/\A\s+|\s+\z//gr }
Condition100%2/2
$content_encoding // ""
l!l
40196
MC/DC100%1/1
$content_encoding // ""
$content_encoding
Truth table100%2/2
A
A= $content_encoding
Aresult
11
00
29 split /,/, lc( $content_encoding // '' );
30 236 for my $encoding ( reverse @encodings ) { # listed in the order applied
31 44 ▶ $handlers->{unbounded_encoding}->($encoding) unless exists $INFLATE{$encoding};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $INFLATE{$encoding} 4 40 44
32 40 ▶ my $kind = $INFLATE{$encoding} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $kind = $INFLATE{$encoding} or next 40 0 40
33 40 ▶ $body = $kind eq 'bzip2'
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'bzip2' ? : 4 36 40
34 ? _bunzip_capped( $body, $max, $encoding, $handlers )
35 : _inflate_capped( $body, $max, $encoding, $kind, $handlers );
36 }
37 211 return $body;
38 }
39  
40  
41 # zlib inflate in output blocks of at most 64 KiB. deflate is the zlib format,
42 # with raw deflate as the fallback that HTTP::Message allows too.
43 36 ▶sub _inflate_capped {
Subroutine _inflate_capped: called CC=11 SCAR=27.2
44 36 my ( $in, $max, $encoding, $kind, $handlers ) = @_;
45 36 require Compress::Raw::Zlib;
46 36 ▶ my @bits = $kind eq 'gzip' ? ( Compress::Raw::Zlib::WANT_GZIP() )
Branch: 1 branch
BranchTrueFalseTotal
$kind eq 'gzip' ? : 26 10 36
47 : ( Compress::Raw::Zlib::MAX_WBITS(), -Compress::Raw::Zlib::MAX_WBITS() );
48 36 for my $bits (@bits) {
49 39 my $input = $in;
50 39 my ( $inflater, $status ) = Compress::Raw::Zlib::Inflate->new(
51 -WindowBits => $bits, -Bufsize => 65_536, -LimitOutput => 1,
52 -ConsumeInput => 1, -AppendOutput => 1 );
53 39 ▶ $handlers->{undecodable}->($encoding) unless $status == Compress::Raw::Zlib::Z_OK();
Branch: 1 branch
BranchTrueFalseTotal
unless $status == Compress::Raw::Zlib::Z_OK() 0 39 39
54 39 my $out = '';
55 39 while (1) {
56 39 my @before = ( length $input, length $out );
57 39 $status = $inflater->inflate( $input, $out );
58 39 ▶ $handlers->{too_big}->($max) if length $out > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $out > $max 17 22 39
59 22 ▶ return $out if $status == Compress::Raw::Zlib::Z_STREAM_END();
Branch: 1 branch
BranchTrueFalseTotal
if $status == Compress::Raw::Zlib::Z_STREAM_END() 19 3 22
60 3 ▶ last unless $status == Compress::Raw::Zlib::Z_OK()
Branch: 1 branch
BranchTrueFalseTotal
unless $status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_BUF_ERROR() 3 0 3
Condition33%1/3
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_BUF_ERROR()
l!l&&r!l&&!r
003
MC/DC0%0/2
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_BUF_ERROR()
$status == Compress::Raw::Zlib::Z_OK() $status == Compress::Raw::Zlib::Z_BUF_ERROR()
Truth table33%1/3
A or B
A= $status == Compress::Raw::Zlib::Z_OK()B= $status == Compress::Raw::Zlib::Z_BUF_ERROR()
ABresult
1X1
011
000
61 || $status == Compress::Raw::Zlib::Z_BUF_ERROR();
62 # No progress: the stream ends before its end marker.
63 0 ▶ $handlers->{undecodable}->($encoding)
Branch: 1 branch
BranchTrueFalseTotal
if length $input == $before[0] and length $out == $before[1] 0 0 0
Condition0%0/3
length $input == $before[0] and length $out == $before[1]
!ll&&!rl&&r
000
MC/DC0%0/2
length $input == $before[0] and length $out == $before[1]
length $input == $before[0] length $out == $before[1]
Truth table0%0/3
A and B
A= length $input == $before[0]B= length $out == $before[1]
ABresult
0X0
100
111
64 if length $input == $before[0] && length $out == $before[1];
65 }
66 3 ▶ $handlers->{undecodable}->($encoding) unless $status == Compress::Raw::Zlib::Z_DATA_ERROR();
Branch: 1 branch
BranchTrueFalseTotal
unless $status == Compress::Raw::Zlib::Z_DATA_ERROR() 0 3 3
67 }
68 0 return $handlers->{undecodable}->($encoding);
69 }
70  
71 4 ▶sub _bunzip_capped {
Subroutine _bunzip_capped: called CC=7 SCAR=27.5
72 4 my ( $input, $max, $encoding, $handlers ) = @_;
73 4 require Compress::Raw::Bzip2;
74 # appendOutput, consumeInput, small, verbosity, limitOutput
75 4 my ( $bunzip, $status ) = Compress::Raw::Bunzip2->new( 1, 1, 0, 0, 1 );
76 4 ▶ $handlers->{undecodable}->($encoding) unless $status == Compress::Raw::Bzip2::BZ_OK();
Branch: 1 branch
BranchTrueFalseTotal
unless $status == Compress::Raw::Bzip2::BZ_OK() 0 4 4
77 4 my $out = '';
78 4 while (1) {
79 4 my @before = ( length $input, length $out );
80 4 $status = $bunzip->bzinflate( $input, $out );
81 4 ▶ $handlers->{too_big}->($max) if length $out > $max;
Branch: 1 branch
BranchTrueFalseTotal
if length $out > $max 4 0 4
82 0 ▶ return $out if $status == Compress::Raw::Bzip2::BZ_STREAM_END();
Branch: 1 branch
BranchTrueFalseTotal
if $status == Compress::Raw::Bzip2::BZ_STREAM_END() 0 0 0
83 0 ▶ $handlers->{undecodable}->($encoding) unless $status == Compress::Raw::Bzip2::BZ_OK();
Branch: 1 branch
BranchTrueFalseTotal
unless $status == Compress::Raw::Bzip2::BZ_OK() 0 0 0
84 0 ▶ $handlers->{undecodable}->($encoding)
Branch: 1 branch
BranchTrueFalseTotal
if length $input == $before[0] and length $out == $before[1] 0 0 0
Condition0%0/3
length $input == $before[0] and length $out == $before[1]
!ll&&!rl&&r
000
MC/DC0%0/2
length $input == $before[0] and length $out == $before[1]
length $input == $before[0] length $out == $before[1]
Truth table0%0/3
A and B
A= length $input == $before[0]B= length $out == $before[1]
ABresult
0X0
100
111
85 if length $input == $before[0] && length $out == $before[1];
86 }
87 }
88  
89  
90 33 1;
91  
92 __END__
« blib/lib/Langertha/Role/PromptCache.pm ↑ Summary blib/lib/Langertha/Virtual/Server.pm »