| 1 | package Furl::ZlibStream; | ||||||||||||||||||||||||||||
| 2 | # internal class. | ||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use overload '.=' => 'append', fallback => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use Carp (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 1 | ▶ | use Compress::Raw::Zlib qw(Z_OK Z_STREAM_END); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # Compressed input is inflated in slices of this size. DEFLATE expands at | ||||||||||||||||||||||||||||
| 10 | # most ~1032:1, so one inflate() call yields at most a few MB, and the | ||||||||||||||||||||||||||||
| 11 | # size limit is checked before a single call can produce an unbounded | ||||||||||||||||||||||||||||
| 12 | # amount of output (a chunked response can hand us one huge chunk). | ||||||||||||||||||||||||||||
| 13 | 1 | my $SLICE = 4096; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 11 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 16 | 11 | my ( $class, $buffer, $max_size ) = @_; | |||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 11 | my ( $zlib, $status ) = Compress::Raw::Zlib::Inflate->new( | |||||||||||||||||||||||||||
| 19 | -WindowBits => Compress::Raw::Zlib::WANT_GZIP_OR_ZLIB(), ); | ||||||||||||||||||||||||||||
| 20 | 11 | ▶ | $status == Z_OK | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | or Carp::croak("Cannot initialize zlib: $status"); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 11 | bless { | |||||||||||||||||||||||||||
| 24 | buffer => $buffer, | ||||||||||||||||||||||||||||
| 25 | zlib => $zlib, | ||||||||||||||||||||||||||||
| 26 | max_size => $max_size, | ||||||||||||||||||||||||||||
| 27 | size => 0, | ||||||||||||||||||||||||||||
| 28 | }, $class; | ||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 28 | ▶ | sub append { | ||||||||||||||||||||||||||
|
Subroutine
append: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 32 | 28 | my ( $self, $partial ) = @_; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 28 | ▶ | for ( my $offset = 0; $offset < length $partial; $offset += $SLICE ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | # inflate() consumes its input, so hand it a copy of the slice. | ||||||||||||||||||||||||||||
| 36 | 82 | my $input = substr( $partial, $offset, $SLICE ); | |||||||||||||||||||||||||||
| 37 | 82 | my $status = $self->{zlib}->inflate( $input, \my $deflated ); | |||||||||||||||||||||||||||
| 38 | 82 | ▶ | ($status == Z_OK or $status == Z_STREAM_END) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$status == Z_OK() or $status == Z_STREAM_END()
MC/DC0%0/2
$status == Z_OK() or $status == Z_STREAM_END()
$status == Z_OK()
$status == Z_STREAM_END()
Truth table66%2/3
A or B
A= $status == Z_OK()B= $status == Z_STREAM_END()
| |||||||||||||||||||||||||||||
| 39 | or Carp::croak("Uncompress error: $status"); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 82 | $self->{size} += length $deflated; | |||||||||||||||||||||||||||
| 42 | 82 | ▶ | if ( $self->{max_size} && $self->{size} > $self->{max_size} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$self->{"max_size"} and $self->{"size"} > $self->{"max_size"}
MC/DC100%2/2
$self->{"max_size"} and $self->{"size"} > $self->{"max_size"}
$self->{"max_size"}
$self->{"size"} > $self->{"max_size"}
Truth table100%3/3
A and B
A= $self->{"max_size"}B= $self->{"size"} > $self->{"max_size"}
| |||||||||||||||||||||||||||||
| 43 | # Thrown as an object so that Furl::HTTP can tell it apart from | ||||||||||||||||||||||||||||
| 44 | # other exceptions (e.g. one thrown by a write_code callback) | ||||||||||||||||||||||||||||
| 45 | # and turn it into an internal 500 response. | ||||||||||||||||||||||||||||
| 46 | 4 | die Furl::ZlibStream::LimitExceeded->new( $self->{max_size} ); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 78 | $self->{buffer} .= $deflated; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 77 | ▶ | last if $status == Z_STREAM_END; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 23 | return $self; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 6 | ▶ | sub get_response_string { ref $_[0]->{buffer} ? undef : $_[0]->{buffer} } | ||||||||||||||||||||||||||
|
Subroutine
get_response_string: called CC=2 SCAR=6.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | package # hide from PAUSE | ||||||||||||||||||||||||||||
| 59 | Furl::ZlibStream::LimitExceeded; | ||||||||||||||||||||||||||||
| 60 | 1 | ▶ | use overload '""' => 'message', fallback => 1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 63 | 4 | my ( $class, $max_size ) = @_; | |||||||||||||||||||||||||||
| 64 | 4 | bless { max_size => $max_size }, $class; | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | 4 | ▶ | sub message { | ||||||||||||||||||||||||||
|
Subroutine
message: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 69 | 4 | "Decompressed response body exceeds max_decompressed_size" | |||||||||||||||||||||||||||
| 70 | . " ($self->{max_size} bytes)"; | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 1 | 1; | |||||||||||||||||||||||||||