| 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 // ""
MC/DC100%1/1
$content_encoding // ""
$content_encoding
Truth table100%2/2
A
A= $content_encoding
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 32 | 40 | ▶ | my $kind = $INFLATE{$encoding} or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 33 | 40 | ▶ | $body = $kind eq 'bzip2' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 59 | 22 | ▶ | return $out if $status == Compress::Raw::Zlib::Z_STREAM_END(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 3 | ▶ | last unless $status == Compress::Raw::Zlib::Z_OK() | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$status == Compress::Raw::Zlib::Z_OK() or $status == Compress::Raw::Zlib::Z_BUF_ERROR()
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()
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
length $input == $before[0] and length $out == $before[1]
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]
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | return $out if $status == Compress::Raw::Bzip2::BZ_STREAM_END(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | 0 | ▶ | $handlers->{undecodable}->($encoding) unless $status == Compress::Raw::Bzip2::BZ_OK(); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | $handlers->{undecodable}->($encoding) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
length $input == $before[0] and length $out == $before[1]
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]
| |||||||||||||||||||||||||||||
| 85 | if length $input == $before[0] && length $out == $before[1]; | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 33 | 1; | |||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | __END__ | ||||||||||||||||||||||||||||