| 1 | package HTTP::Entity::Parser; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use 5.008001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Stream::Buffered; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use Module::Load; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 4 | our $VERSION = "0.23"; | |||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | our $BUFFER_LENGTH = 65536; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | our %LOADED; | |||||||||||||||||||||||||||
| 14 | 4 | our @DEFAULT_PARSER = qw/ | |||||||||||||||||||||||||||
| 15 | OctetStream | ||||||||||||||||||||||||||||
| 16 | UrlEncoded | ||||||||||||||||||||||||||||
| 17 | MultiPart | ||||||||||||||||||||||||||||
| 18 | JSON | ||||||||||||||||||||||||||||
| 19 | /; | ||||||||||||||||||||||||||||
| 20 | 4 | for my $parser ( @DEFAULT_PARSER ) { | |||||||||||||||||||||||||||
| 21 | 16 | load "HTTP::Entity::Parser::".$parser; | |||||||||||||||||||||||||||
| 22 | 16 | $LOADED{"HTTP::Entity::Parser::".$parser} = 1; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | sub new { | ||||||||||||||||||||||||||||
| 26 | 27 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 27 | my %args = ( | |||||||||||||||||||||||||||
| 28 | buffer_length => $BUFFER_LENGTH, | ||||||||||||||||||||||||||||
| 29 | @_, | ||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||
| 31 | 27 | bless [ [], $args{buffer_length} ], $class; | |||||||||||||||||||||||||||
| 32 | } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | sub register { | ||||||||||||||||||||||||||||
| 35 | 24 | ▶ | my ($self,$content_type, $klass, $opts) = @_; | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=2 SCAR=7.9
Pod
documented
| |||||||||||||||||||||||||||||
| 36 | 24 | ▶ | if ( !$LOADED{$klass} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 0 | load $klass; | |||||||||||||||||||||||||||
| 38 | 0 | $LOADED{$klass} = 1; | |||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||
| 40 | 24 | push @{$self->[0]}, [$content_type, $klass, $opts]; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | sub parse { | ||||||||||||||||||||||||||||
| 44 | 26 | ▶ | my ($self, $env) = @_; | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=21 SCAR=32.3
Pod
documented
| |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 26 | my $buffer_length = $self->[1]; | |||||||||||||||||||||||||||
| 47 | 26 | my $ct = $env->{CONTENT_TYPE}; | |||||||||||||||||||||||||||
| 48 | 26 | ▶ | if (!$ct) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 49 | # No Content-Type | ||||||||||||||||||||||||||||
| 50 | 0 | return ([], []); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 26 | my $parser; | |||||||||||||||||||||||||||
| 54 | 26 | for my $handler (@{$self->[0]}) { | |||||||||||||||||||||||||||
| 55 | 24 | ▶ | if ( $ct eq $handler->[0] || index($ct, $handler->[0]) == 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ct eq $handler->[0] or index($ct, $handler->[0]) == 0
MC/DC100%2/2
$ct eq $handler->[0] or index($ct, $handler->[0]) == 0
$ct eq $handler->[0]
index($ct, $handler->[0]) == 0
Truth table100%3/3
A or B
A= $ct eq $handler->[0]B= index($ct, $handler->[0]) == 0
| |||||||||||||||||||||||||||||
| 56 | 22 | $parser = $handler->[1]->new($env, $handler->[2]); | |||||||||||||||||||||||||||
| 57 | 22 | last; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 26 | ▶ | if ( !$parser ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | 4 | $parser = HTTP::Entity::Parser::OctetStream->new(); | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 26 | my $input = $env->{'psgi.input'}; | |||||||||||||||||||||||||||
| 67 | 26 | ▶ | if (!$input) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | # no input | ||||||||||||||||||||||||||||
| 69 | 2 | return ([], []); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 24 | my $buffer; | |||||||||||||||||||||||||||
| 73 | 24 | ▶ | if ($env->{'psgix.input.buffered'}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | # Just in case if input is read by middleware/apps beforehand | ||||||||||||||||||||||||||||
| 75 | 0 | $input->seek(0, 0); | |||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | 24 | $buffer = Stream::Buffered->new(); | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 4 | ▶ | my $chunked = do { no warnings; lc delete $env->{HTTP_TRANSFER_ENCODING} eq 'chunked' }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 81 | 24 | ▶ | if ( my $cl = $env->{CONTENT_LENGTH} ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 82 | 21 | my $spin = 0; | |||||||||||||||||||||||||||
| 83 | 21 | ▶ | while ($cl > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 21 | ▶ | $input->read(my $chunk, $cl < $buffer_length ? $cl : $buffer_length); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 21 | my $read = length $chunk; | |||||||||||||||||||||||||||
| 86 | 21 | $cl -= $read; | |||||||||||||||||||||||||||
| 87 | 21 | $parser->add($chunk); | |||||||||||||||||||||||||||
| 88 | 21 | ▶ | $buffer->print($chunk) if $buffer; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 21 | ▶ | if ($read == 0 && $spin++ > 2000) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$read == 0 and $spin++ > 2000
MC/DC0%0/2
$read == 0 and $spin++ > 2000
$read == 0
$spin++ > 2000
Truth table33%1/3
A and B
A= $read == 0B= $spin++ > 2000
| |||||||||||||||||||||||||||||
| 90 | 0 | Carp::croak "Bad Content-Length: maybe client disconnect? ($cl bytes remaining)"; | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | elsif ($chunked) { | ||||||||||||||||||||||||||||
| 95 | 3 | my $chunk_buffer = ''; | |||||||||||||||||||||||||||
| 96 | 3 | my $length; | |||||||||||||||||||||||||||
| 97 | 3 | my $spin = 0; | |||||||||||||||||||||||||||
| 98 | 3 | DECHUNK: while(1) { | |||||||||||||||||||||||||||
| 99 | 3 | $input->read(my $chunk, $buffer_length); | |||||||||||||||||||||||||||
| 100 | 3 | my $read = length $chunk; | |||||||||||||||||||||||||||
| 101 | 3 | ▶ | if ($read == 0 ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 0 | ▶ | Carp::croak "Malformed chunked request" if $spin++ > 2000; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | 0 | next; | |||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | 3 | $chunk_buffer .= $chunk; | |||||||||||||||||||||||||||
| 106 | 3 | ▶ | while ( $chunk_buffer =~ s/^(([0-9a-fA-F]+).*\015\012)// ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 21 | my $trailer = $1; | |||||||||||||||||||||||||||
| 108 | 21 | my $chunk_len = hex $2; | |||||||||||||||||||||||||||
| 109 | 21 | ▶ | if ($chunk_len == 0) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 110 | 3 | last DECHUNK; | |||||||||||||||||||||||||||
| 111 | } elsif (length $chunk_buffer < $chunk_len + 2) { | ||||||||||||||||||||||||||||
| 112 | 0 | $chunk_buffer = $trailer . $chunk_buffer; | |||||||||||||||||||||||||||
| 113 | 0 | last; | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 18 | my $loaded = substr $chunk_buffer, 0, $chunk_len, ''; | |||||||||||||||||||||||||||
| 116 | 18 | $parser->add($loaded); | |||||||||||||||||||||||||||
| 117 | 18 | $buffer->print($loaded); | |||||||||||||||||||||||||||
| 118 | 18 | $chunk_buffer =~ s/^\015\012//; | |||||||||||||||||||||||||||
| 119 | 18 | $length += $chunk_len; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | 3 | $env->{CONTENT_LENGTH} = $length; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 24 | ▶ | if ($buffer) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 24 | $env->{'psgix.input.buffered'} = 1; | |||||||||||||||||||||||||||
| 127 | 24 | $env->{'psgi.input'} = $buffer->rewind; | |||||||||||||||||||||||||||
| 128 | } else { | ||||||||||||||||||||||||||||
| 129 | 0 | $input->seek(0, 0); | |||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 24 | $parser->finalize(); | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 4 | 1; | |||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||