blib/lib/HTTP/Parser/XS/PP.pm

filter: Statementstmt 87.6% 113 / 129 Branchbran 62.2% 51 / 82 Conditioncond 58.3% 14 / 24 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 100% 9 / 9 Podpod 100% 2 / 2 totaltotal 74.6% 194 / 260 CC 56 SCAR 30.1
CC
56
Cov
76%
_parse_as_none
70.1
_parse_header
14.4
_parse_as_hashref
14.3
CRAP 20.4
↑ Summary picohttpparser/picohttpparser.c »
1 package HTTP::Parser::XS::PP;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 16 ▶sub HTTP::Parser::XS::parse_http_request {
Subroutine parse_http_request: called CC=5 SCAR=16.5
Pod documented
7 16 my($chunk, $env) = @_;
8 16 ▶ Carp::croak("second param to parse_http_request should be a hashref") unless (ref $env|| '') eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $env || "") eq "HASH" 1 15 16
Condition100%2/2
ref $env || ''
l!l
151
MC/DC100%1/1
ref $env || ''
ref $env
Truth table100%2/2
A
A= ref $env
Aresult
11
00
9  
10 # pre-header blank lines are allowed (RFC 2616 4.1)
11 15 $chunk =~ s/^(\x0d?\x0a)+//;
12 15 ▶ return -2 unless length $chunk;
Branch: 1 branch
BranchTrueFalseTotal
unless length $chunk 0 15 15
13  
14 # double line break indicates end of header; parse it
15 15 ▶ if ($chunk =~ /^(.*?\x0d?\x0a\x0d?\x0a)/s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunk =~ /^(.*?\x0d?\x0a\x0d?\x0a)/s) 15 0 15
16 15 return _parse_header($chunk, length $1, $env);
17 }
18 0 return -2; # still waiting for unknown amount of header lines
19 }
20  
21 15 ▶sub _parse_header {
Subroutine _parse_header: called CC=14 SCAR=26.7
22 15 my($chunk, $eoh, $env) = @_;
23  
24 15 my $header = substr($chunk, 0, $eoh,'');
25 15 $chunk =~ s/^\x0d?\x0a\x0d?\x0a//;
26  
27 # parse into lines
28 15 my @header = split /\x0d?\x0a/,$header;
29 15 my $request = shift @header;
30  
31 # join folded lines
32 15 my @out;
33 15 for(@header) {
34 9 ▶ if(/^[ \t]+/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^[ \t]+/) { } 2 7 9
35 2 ▶ return -1 unless @out;
Branch: 1 branch
BranchTrueFalseTotal
unless @out 0 2 2
36 2 $out[-1] .= $_;
37 } else {
38 7 push @out, $_;
39 }
40 }
41  
42 # parse request or response line
43 15 my $obj;
44 my $minor;
45  
46 15 my ($method,$uri,$http) = split / /,$request;
47 15 ▶ return -1 unless $http and $http =~ /^HTTP\/1\.(\d+)$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $http and $http =~ m[^HTTP/1\.(\d+)$] 0 15 15
Condition33%1/3
$http and $http =~ m[^HTTP/1\.(\d+)$]
!ll&&!rl&&r
0015
MC/DC0%0/2
$http and $http =~ m[^HTTP/1\.(\d+)$]
$http $http =~ m[^HTTP/1\.(\d+)$]
Truth table33%1/3
A and B
A= $httpB= $http =~ m[^HTTP/1\.(\d+)$]
ABresult
0X0
100
111
48 15 $minor = $1;
49  
50 15 my($path, $query) = ( $uri =~ /^([^?#]*)(?:\?([^#]*))?/s );
51 # following validations are just needed to pass t/01simple.t
52 15 ▶ if ($path =~ /%(?:[0-9a-f][^0-9a-f]|[^0-9a-f][0-9a-f])/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /%(?:[0-9a-f][^0-9a-f]|[^0-9a-f][0-9a-f])/i) 1 14 15
53 # invalid char in url-encoded path
54 1 return -1;
55 }
56 14 ▶ if ($path =~ /%(?:[0-9a-f])$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($path =~ /%(?:[0-9a-f])$/i) 1 13 14
57 # partially url-encoded
58 1 return -1;
59 }
60  
61 13 $env->{REQUEST_METHOD} = $method;
62 13 $env->{REQUEST_URI} = $uri;
63 13 $env->{SERVER_PROTOCOL} = "HTTP/1.$minor";
64 13 ($env->{PATH_INFO} = $path) =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
65 13 ▶ $env->{QUERY_STRING} = $query || '';
Condition100%2/2
$query || ''
l!l
310
MC/DC100%1/1
$query || ''
$query
Truth table100%2/2
A
A= $query
Aresult
11
00
66 13 $env->{SCRIPT_NAME} = '';
67  
68 # import headers
69 13 my $token = qr/[^][\x00-\x1f\x7f()<>@,;:\\"\/?={} \t]+/;
70 13 my $k;
71 13 for my $header (@out) {
72 7 ▶ if ( $header =~ s/^($token): ?// ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($header =~ s/^($token): ?//) { } 7 0 7
elsif ($header =~ /^\s+/) { } 0 0 0
73 7 $k = $1;
74 7 $k =~ s/-/_/g;
75 7 $k = uc $k;
76  
77 7 ▶ if ($k !~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($k =~ /^(?:CONTENT_LENGTH|CONTENT_TYPE)$/) 5 2 7
78 5 $k = "HTTP_$k";
79 }
80 } elsif ( $header =~ /^\s+/) {
81 # multiline header
82 } else {
83 0 return -1;
84 }
85  
86 7 ▶ if (exists $env->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $env->{$k}) { } 2 5 7
87 2 $env->{$k} .= ", $header";
88 } else {
89 5 $env->{$k} = $header;
90 }
91 }
92  
93 13 return $eoh;
94 }
95  
96 # ----------------------------------------------------------
97  
98 7 my %PARSER_FUNC = (
99 HTTP::Parser::XS::HEADERS_NONE() => \&_parse_as_none,
100 HTTP::Parser::XS::HEADERS_AS_HASHREF() => \&_parse_as_hashref,
101 HTTP::Parser::XS::HEADERS_AS_ARRAYREF() => \&_parse_as_arrayref,
102 );
103  
104 37 ▶sub HTTP::Parser::XS::parse_http_response {
Subroutine parse_http_response: called CC=10 SCAR=23.5
Pod documented
105 37 my ($str, $header_format, $special_headers) = @_;
106 37 ▶ return -2 unless $str;
Branch: 1 branch
BranchTrueFalseTotal
unless $str 0 37 37
107  
108 37 my $len = length $str;
109
110 37 my ($sl, $remain) = split /\r?\n/, $_[0], 2;
111 37 my ($proto, $rc, $msg) = split(' ', $sl, 3);
112 37 ▶ return -1 unless $proto =~m{^HTTP/1.(\d)};
Branch: 1 branch
BranchTrueFalseTotal
unless $proto =~ m[^HTTP/1.(\d)] 1 36 37
113 36 my $minor_version = $1;
114 36 ▶ return -1 unless $rc =~m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $rc =~ /^\d+$/ 0 36 36
115  
116 36 my ($header_str, $content) = split /\r?\n\r?\n/, $remain, 2;
117  
118 36 my $parser_func = $PARSER_FUNC{$header_format};
119 36 ▶ die 'unknown header format: '. $header_format unless $parser_func;
Branch: 1 branch
BranchTrueFalseTotal
unless $parser_func 0 36 36
120  
121 36 ▶ my $header = $parser_func->($header_str, $special_headers || +{});
Condition50%1/2
$special_headers || {}
l!l
036
MC/DC0%0/1
$special_headers || {}
$special_headers
Truth table50%1/2
A
A= $special_headers
Aresult
11
00
122  
123 36 ▶ return -2 if ($str !~/\r?\n\r?\n/ && $remain !~/\r?\n\r?\n/ && !defined $content);
Branch: 1 branch
BranchTrueFalseTotal
unless $str =~ /\r?\n\r?\n/ or $remain =~ /\r?\n\r?\n/ or defined $content 2 34 36
Condition66%4/6
$str =~ /\r?\n\r?\n/ or $remain =~ /\r?\n\r?\n/ or defined $content
l!l&&r!l&&!r
3402
$str =~ /\r?\n\r?\n/ or $remain =~ /\r?\n\r?\n/
l!l&&r!l&&!r
3402
MC/DC33%1/3
$str =~ /\r?\n\r?\n/ or $remain =~ /\r?\n\r?\n/ or defined $content
$str =~ /\r?\n\r?\n/ $remain =~ /\r?\n\r?\n/ defined $content
Truth table50%2/4
A or B or C
A= $str =~ /\r?\n\r?\n/B= $remain =~ /\r?\n\r?\n/C= defined $content
ABCresult
1XX1
01X1
0011
0000
124 34 ▶ my $parsed = $len - (defined $content ? length $content : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $content ? : 30 4 34
125  
126 34 return ($parsed, $minor_version, $rc, $msg, $header);
127 }
128  
129 # return special headers only
130 8 ▶sub _parse_as_none {
Subroutine _parse_as_none: called CC=10 SCAR=42.5
131 8 my ($str, $special) = @_;
132 8 ▶ return unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 8 8
133 8 ▶ return unless keys %$special;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %$special 8 0 8
134  
135 0 my ($field, $value, $f);
136 0 for ( split /\r?\n/, $str ) {
137 0 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) 0 0 0
138 0 ▶ if ( ord == 9 || ord == 32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $_ == 9 or ord $_ == 32) 0 0 0
Condition0%0/3
ord $_ == 9 or ord $_ == 32
l!l&&r!l&&!r
000
MC/DC0%0/2
ord $_ == 9 or ord $_ == 32
ord $_ == 9 ord $_ == 32
Truth table0%0/3
A or B
A= ord $_ == 9B= ord $_ == 32
ABresult
1X1
011
000
139 0 $value .= "\n$_";
140 0 next;
141 }
142 0 $f = lc($field);
143 0 ▶ exists $special->{$f} and $special->{$f} = $value;
Branch: 1 branch
BranchTrueFalseTotal
exists $special->{$f} and $special->{$f} = $value 0 0 0
144 }
145 0 ( $field, $value ) = split /[ \t]*: ?/, $_, 2;
146 }
147 0 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) 0 0 0
148 0 $f = lc($field);
149 0 ▶ exists $special->{$f} and $special->{$f} = $value;
Branch: 1 branch
BranchTrueFalseTotal
exists $special->{$f} and $special->{$f} = $value 0 0 0
150 }
151 }
152  
153 # return headers as arrayref
154 8 ▶sub _parse_as_arrayref {
Subroutine _parse_as_arrayref: called CC=9 SCAR=22.1
155 8 my ($str, $special) = @_;
156 8 ▶ return [] unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 8 8
157  
158 8 my (@headers, $field, $value, $f );
159 8 for ( split /\r?\n/, $str ) {
160 12 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) 5 7 12
161 5 ▶ if ( ord == 9 || ord == 32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $_ == 9 or ord $_ == 32) 1 4 5
Condition66%2/3
ord $_ == 9 or ord $_ == 32
l!l&&r!l&&!r
014
MC/DC50%1/2
ord $_ == 9 or ord $_ == 32
ord $_ == 9 ord $_ == 32
Truth table66%2/3
A or B
A= ord $_ == 9B= ord $_ == 32
ABresult
1X1
011
000
162 1 $value .= "\n$_";
163 1 next;
164 }
165 4 $f = lc($field);
166 4 push @headers, $f, $value;
167 4 ▶ exists $special->{$f} and $special->{$f} = $value;
Branch: 1 branch
BranchTrueFalseTotal
exists $special->{$f} and $special->{$f} = $value 0 4 4
168 }
169 11 ( $field, $value ) = split /[ \t]*: ?/, $_, 2;
170 }
171 8 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) 7 1 8
172 7 $f = lc($field);
173 7 push @headers, $f, $value;
174 7 ▶ exists $special->{$f} and $special->{$f} = $value;
Branch: 1 branch
BranchTrueFalseTotal
exists $special->{$f} and $special->{$f} = $value 0 7 7
175 }
176 8 return \@headers;
177 }
178  
179 # return headers as HTTP::Header compatible HashRef
180 20 ▶sub _parse_as_hashref {
Subroutine _parse_as_hashref: called CC=13 SCAR=26.6
181 20 my ($str, $special) = @_;
182 20 ▶ return +{} unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 1 19 20
183
184 19 my ( %self, $field, $value, $f );
185 19 for ( split /\r?\n/, $str ) {
186 31 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) 14 17 31
187 14 ▶ if ( ord == 9 || ord == 32 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ord $_ == 9 or ord $_ == 32) 3 11 14
Condition66%2/3
ord $_ == 9 or ord $_ == 32
l!l&&r!l&&!r
0311
MC/DC50%1/2
ord $_ == 9 or ord $_ == 32
ord $_ == 9 ord $_ == 32
Truth table66%2/3
A or B
A= ord $_ == 9B= ord $_ == 32
ABresult
1X1
011
000
188 3 $value .= "\n$_";
189 3 next;
190 }
191 11 $f = lc($field);
192 11 ▶ if ( defined $self{$f} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self{$f}) { } 0 11 11
193 0 my $h = $self{$f};
194 ref($h) eq 'ARRAY'
195 ? push( @$h, $value )
196 0 ▶ : ( $self{$f} = [ $h, $value ] );
Branch: 1 branch
BranchTrueFalseTotal
ref $h eq 'ARRAY' ? : 0 0 0
197 }
198 11 ▶ else { $self{$f} = $value; exists $special->{$f} and $special->{$f} = $value; }
Branch: 1 branch
BranchTrueFalseTotal
exists $special->{$f} and $special->{$f} = $value 0 11 11
199 }
200 28 ( $field, $value ) = split /[ \t]*: ?/, $_, 2;
201 }
202 19 ▶ if ( defined $field ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $field) 17 2 19
203 17 $f = lc($field);
204 17 ▶ if ( defined $self{$f} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self{$f}) { } 3 14 17
205 3 my $h = $self{$f};
206 ref($h) eq 'ARRAY'
207 ? push( @$h, $value )
208 3 ▶ : ( $self{$f} = [ $h, $value ] );
Branch: 1 branch
BranchTrueFalseTotal
ref $h eq 'ARRAY' ? : 0 3 3
209 }
210 14 ▶ else { $self{$f} = $value; exists $special->{$f} and $special->{$f} = $value; }
Branch: 1 branch
BranchTrueFalseTotal
exists $special->{$f} and $special->{$f} = $value 0 14 14
211 }
212 19 return \%self;
213 }
214  
215 7 1;
216  
↑ Summary picohttpparser/picohttpparser.c »