Inspect/L7/HTTP.pm

filter: Statementstmt 72.0% 452 / 627 Branchbran 55.7% 262 / 470 Conditioncond 32.6% 79 / 242 MC/DCmcdc 12.0% 18 / 150 Subroutinesub 68.5% 24 / 35 Podpod 82.3% 14 / 17 totaltotal 55.0% 849 / 1541 CC 324 SCAR 67.4
CC
324
Cov
59%
_in1
1613.9
_in0
881.5
guess_protocol
56.0
CRAP 841.9
« Inspect/L4/TCP.pm ↑ Summary Inspect/L7/HTTP/WebSocket.pm »
1 ############################################################################
2 # finds HTTP requests + responses in tcp connection
3 # chunked HTTP responses are supported
4 ############################################################################
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 package Net::Inspect::L7::HTTP;
8 1 ▶use base 'Net::Inspect::Flow';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Net::Inspect::Debug qw(:DEFAULT $DEBUG %TRACE);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Hash::Util 'lock_ref_keys';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Scalar::Util 'weaken';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 use fields (
14 1 'replay', # collected and replayed in guess_protocol
15 'meta', # meta data from connection
16 'requests', # list of open requests, see _in0 for fields
17 'error', # connection has error like server sending data w/o request
18 'upgrade', # true if got upgrade, CONNECT, WebSockets..
19 'connid', # connection id
20 'lastreqid',# id of last request
21 'offset', # offset in data stream
22 'gap_upto', # up to which offset we could manage a gap, that is where we
23 # only get body data (no header, chunked info..).
24 # [off,off] similar to offset and off is set to -1 if umlimited
25 # (i.e. body ends with end of file)
26 'hdr_maxsz',# maximum header size for request(0), response(1) and
27 # chunk header(2). Defaults to 64k, 16k and 2k.
28 1 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 our (@EXPORT_OK,%EXPORT_TAGS);
32 {
33 1 %EXPORT_TAGS = (
34 need_body => [qw(
35 METHODS_WITHOUT_RQBODY METHODS_WITH_RQBODY METHODS_WITHOUT_RPBODY
36 CODE_WITHOUT_RPBODY
37 )]
38 );
39 1 push @EXPORT_OK,@$_ for (values %EXPORT_TAGS);
40 1 push @EXPORT_OK,'parse_hdrfields','parse_reqhdr','parse_rsphdr';
41 }
42  
43 use constant {
44 1 METHODS_WITHOUT_RQBODY => [qw(GET HEAD DELETE CONNECT)],
45 METHODS_WITH_RQBODY => [qw(POST PUT)],
46 METHODS_WITHOUT_RPBODY => [qw(HEAD CONNECT)],
47 CODE_WITHOUT_RPBODY => [100..199, 204, 205, 304],
48 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
49  
50 use constant {
51 1 RQHDR_DONE => 0b00001,
52 RQBDY_DONE => 0b00010,
53 RQ_ERROR => 0b00100,
54 RPHDR_DONE => 0b01000,
55 RPBDY_DONE_ON_EOF => 0b10000,
56 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 # rfc2616, 2.2
59 # token = 1*<any CHAR except CTLs or separators>
60 # separators = "(" | ")" | "<" | ">" | "@"
61 # | "," | ";" | ":" | "\" | <">
62 # | "/" | "[" | "]" | "?" | "="
63 # | "{" | "}" | SP | HT
64  
65 1 my $separator = qr{[()<>@,;:\\"/\[\]?={} \t]};
66 1 my $token = qr{[^()<>@,;:\\"/\[\]?={}\x00-\x20\x7f]+};
67 1 my $token_value_cont = qr{
68 ($token): # key:
69 [\040\t]*([^\r\n]*?)[\040\t]* # <space>value<space>
70 ((?:\r?\n[\040\t][^\r\n]*)*) # continuation lines
71 \r?\n # (CR)LF
72 }x;
73  
74 # common error: "Last Modified" instead of "Last-Modified"
75 # squid seems to just strip invalid headers, try the same
76 1 my $xtoken = qr{[^()<>@,;:\\"/\[\]?={}\x00-\x20\x7f][^:[:^print:]]*};
77  
78 1 my %METHODS_WITHOUT_RQBODY = map { ($_,1) } @{METHODS_WITHOUT_RQBODY()};
79 1 my %METHODS_WITH_RQBODY = map { ($_,1) } @{METHODS_WITH_RQBODY()};
80 1 my %METHODS_WITHOUT_RPBODY = map { ($_,1) } @{METHODS_WITHOUT_RPBODY()};
81 1 my %CODE_WITHOUT_RPBODY = map { ($_,1) } @{CODE_WITHOUT_RPBODY()};
82  
83 sub guess_protocol {
84 0 ▶ my ($self,$guess,$dir,$data,$eof,$time,$meta) = @_;
Subroutine guess_protocol: not called CC=7 SCAR=40.3
Pod documented
85  
86 0 ▶ if ( $dir == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dir == 0) { } 0 0 0
87 0 ▶ my $rp = $self->{replay} ||= [];
Condition0%0/2
$self->{"replay"} ||= []
l!l
00
MC/DC0%0/1
$self->{"replay"} ||= []
$self->{"replay"}
Truth table0%0/2
A
A= $self->{"replay"}
Aresult
11
00
88 0 push @$rp,[$data,$eof,$time];
89 0 my $buf = join('',map { $_->[0] } @$rp);
90 0 ▶ if ( $buf =~m{
Branch: 2 branches
BranchTrueFalseTotal
if ($buf =~ m[ \A[\r\n]* # initial junk [A-Z]{2,20}[\040\t]{1,3} # method \S+[\040\t]{1,3} # path/URI HTTP/1\.[01][\040\t]{0,3} # version \r?\n # (CR)LF (?:$xtoken:.*\r?\n(?:[\t\040].*\r?\n)* )* # field:..+cont \r?\n # empty line ]xi) { } 0 0 0
elsif ($buf =~ /[^\n]\r?\n\r?\n/ or length $buf > 65536.0) { } 0 0 0
91 \A[\r\n]* # initial junk
92 [A-Z]{2,20}[\040\t]{1,3} # method
93 \S+[\040\t]{1,3} # path/URI
94 HTTP/1\.[01][\040\t]{0,3} # version
95 \r?\n # (CR)LF
96 (?:$xtoken:.*\r?\n(?:[\t\040].*\r?\n)* )* # field:..+cont
97 \r?\n # empty line
98 }xi) {
99 # looks like HTTP request
100 0 my $obj = $self->new_connection($meta);
101 # replay as one piece
102 0 my $n = $obj->in(0,$buf,$rp->[-1][1],$rp->[-1][2]);
103 0 undef $self->{replay};
104 0 $n += -length($buf) + length($data);
105 0 ▶ $n<=0 and die "object $obj did not consume alle replayed bytes";
Branch: 1 branch
BranchTrueFalseTotal
$n <= 0 and die "object $obj did not consume alle replayed bytes" 0 0 0
106 0 debug("consumed $n of ".length($data)." bytes");
107 0 return ($obj,$n);
108  
109 ▶ } elsif ( $buf =~m{[^\n]\r?\n\r?\n}
Condition0%0/3
$buf =~ /[^\n]\r?\n\r?\n/ or length $buf > 65536
l!l&&r!l&&!r
000
MC/DC0%0/2
$buf =~ /[^\n]\r?\n\r?\n/ or length $buf > 65536
$buf =~ /[^\n]\r?\n\r?\n/ length $buf > 65536
Truth table0%0/3
A or B
A= $buf =~ /[^\n]\r?\n\r?\n/B= length $buf > 65536
ABresult
1X1
011
000
110 or length($buf)>2**16 ) {
111 # does not look like a HTTP header for me
112 0 debug("does not look like HTTP header: $buf");
113 0 $guess->detach($self);
114 } else {
115 0 debug("need more data to decide if HTTP");
116 }
117 } else {
118 # data from server but no request header from
119 # client yet - cannot be HTTP
120 0 debug("got data from server before getting request from client -> no HTTP");
121 0 $guess->detach($self);
122 }
123 0 return;
124 }
125  
126  
127 {
128 1 my $connid = 0;
129 0 ▶ sub syn { 1 }; # in case it is attached to Net::Inspect::Tcp
Subroutine syn: not called CC=1 SCAR=6.9
Pod undocumented
130 sub new_connection {
131 14 ▶ my ($self,$meta,%args) = @_;
Subroutine new_connection: called CC=4 SCAR=14.0
Pod documented
132 14 my $obj = $self->new;
133 14 $obj->{meta} = $meta;
134 14 $obj->{requests} = [];
135 14 $obj->{connid} = ++$connid;
136 14 $obj->{lastreqid} = 0;
137 14 $obj->{offset} = [0,0];
138 14 $obj->{gap_upto} = [0,0];
139 14 $obj->{hdr_maxsz} = delete $args{header_maxsize};
140 14 ▶ $obj->{hdr_maxsz}[0] ||= 2**16;
Condition50%1/2
$obj->{"hdr_maxsz"}[0] ||= 65536
l!l
014
MC/DC0%0/1
$obj->{"hdr_maxsz"}[0] ||= 65536
$obj->{"hdr_maxsz"}[0]
Truth table50%1/2
A
A= $obj->{"hdr_maxsz"}[0]
Aresult
11
00
141 14 ▶ $obj->{hdr_maxsz}[1] ||= 2**14;
Condition50%1/2
$obj->{"hdr_maxsz"}[1] ||= 16384
l!l
014
MC/DC0%0/1
$obj->{"hdr_maxsz"}[1] ||= 16384
$obj->{"hdr_maxsz"}[1]
Truth table50%1/2
A
A= $obj->{"hdr_maxsz"}[1]
Aresult
11
00
142 14 ▶ $obj->{hdr_maxsz}[2] ||= 2**11;
Condition50%1/2
$obj->{"hdr_maxsz"}[2] ||= 2048
l!l
014
MC/DC0%0/1
$obj->{"hdr_maxsz"}[2] ||= 2048
$obj->{"hdr_maxsz"}[2]
Truth table50%1/2
A
A= $obj->{"hdr_maxsz"}[2]
Aresult
11
00
143  
144 14 return $obj;
145 }
146 }
147  
148 1 sub in {
149 92 ▶ my ($self,$dir,$data,$eof,$time) = @_;
Subroutine in: called CC=5 SCAR=19.2
Pod documented
150 92 ▶ $DEBUG && $self->xdebug("got %s bytes from %d, eof=%d",
Branch: 2 branches
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $self->xdebug("got %s bytes from %d, eof=%d", ref $data ? join(":", @$data) : length $data, $dir, $eof // 0) 0 92 92
ref $data ? : 0 0 0
Condition0%0/2
$eof // 0
l!l
00
MC/DC0%0/1
$eof // 0
$eof
Truth table0%0/2
A
A= $eof
Aresult
11
00
151 ref($data) ? join(":",@$data): length($data),
152 $dir,$eof//0
153 );
154 92 ▶ my $bytes = $dir == 0
Branch: 1 branch
BranchTrueFalseTotal
$dir == 0 ? : 55 37 92
155 ? _in0($self,$data,$eof,$time)
156 : _in1($self,$data,$eof,$time);
157 #$self->dump_state if $DEBUG;
158 92 return $bytes;
159 }
160  
161 sub offset {
162 0 ▶ my $self = shift;
Subroutine offset: not called CC=2 SCAR=17.9
Pod documented
163 0 ▶ return @{ $self->{offset} }[wantarray ? @_:$_[0]];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
164 }
165  
166 sub gap_diff {
167 29 ▶ my $self = shift;
Subroutine gap_diff: called CC=5 SCAR=16.1
Pod documented
168 29 my @rv;
169 29 for(@_) {
170 58 my $off = $self->{gap_upto}[$_];
171 push @rv,
172 $off == -1 ? -1 :
173 58 ▶ ($off-=$self->{offset}[$_]) > 0 ? $off :
Branch: 2 branches
BranchTrueFalseTotal
$off == -1 ? : 6 52 58
($off -= $self->{'offset'}[$_]) > 0 ? : 21 31 52
174 0;
175 }
176 29 ▶ return wantarray ? @rv : $rv[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 29 0 29
177 }
178  
179 sub set_gap_diff {
180 2 ▶ my ($self,$dir,$diff) = @_;
Subroutine set_gap_diff: called CC=2 SCAR=7.2
Pod documented
181 $self->{gap_upto}[$dir] = defined($diff)
182 2 ▶ ? $self->{offset}[$dir] + $diff # add to offset
Branch: 1 branch
BranchTrueFalseTotal
defined $diff ? : 2 0 2
183 : 0; # reset gap_upto
184 }
185  
186 sub gap_offset {
187 16 ▶ my $self = shift;
Subroutine gap_offset: called CC=5 SCAR=16.1
Pod documented
188 16 my @rv;
189 16 for(@_) {
190 32 my $off = $self->{gap_upto}[$_];
191 push @rv,
192 $off == -1 ? -1 :
193 32 ▶ $off > $self->{offset}[$_] ? $off :
Branch: 2 branches
BranchTrueFalseTotal
$off == -1 ? : 2 30 32
$off > $self->{'offset'}[$_] ? : 14 16 30
194 0
195 }
196 16 ▶ return wantarray ? @rv : $rv[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 16 0 16
197 }
198  
199 # give requests a chance to cleanup before destroying connection
200 sub DESTROY {
201 14 ▶ my $self = shift;
Subroutine DESTROY: called CC=1 SCAR=0.0
202 14 @{$self->{requests}} = ();
203 }
204  
205  
206 # process request data
207 sub _in0 {
208 55 ▶ my ($self,$data,$eof,$time) = @_;
Subroutine _in0: called CC=106 SCAR=67.8
209 55 my $bytes = 0; # processed bytes
210 55 my $rqs = $self->{requests};
211  
212 55 ▶ if ( ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) 7 48 55
213 # process gap in request data
214 7 ▶ croak "unknown type $data->[0]" if $data->[0] ne 'gap';
Branch: 1 branch
BranchTrueFalseTotal
if $data->[0] ne "gap" 0 7 7
215 7 my $len = $data->[1];
216  
217 7 ▶ croak 'existing error in connection' if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 7 7
218  
219 7 my $rqs = $self->{requests};
220 croak 'no open request' if ! @$rqs or
221 7 ▶ $rqs->[0]{state} & RQBDY_DONE && ! $self->{upgrade};
Branch: 1 branch
BranchTrueFalseTotal
if not @$rqs or $rqs->[0]{"state"} & 2 and not $self->{"upgrade"} 0 7 7
Condition50%3/6
not @$rqs or $rqs->[0]{"state"} & 2 and not $self->{"upgrade"}
l!l&&r!l&&!r
007
$rqs->[0]{"state"} & 2 and not $self->{"upgrade"}
!ll&&!rl&&r
250
MC/DC0%0/3
not @$rqs or $rqs->[0]{"state"} & 2 and not $self->{"upgrade"}
not @$rqs $rqs->[0]{"state"} & 2 not $self->{"upgrade"}
Truth table25%1/4
not(A) or B and not(C)
A= not @$rqsB= $rqs->[0]{"state"} & 2C= not $self->{"upgrade"}
ABCresult
1XX1
0111
00X0
0100
222 7 ▶ croak 'existing error in request' if $rqs->[0]{state} & RQ_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $rqs->[0]{"state"} & 4 0 7 7
223 croak "gap too large" if $self->{gap_upto}[0]>=0
224 7 ▶ && $self->{gap_upto}[0] < $self->{offset}[0] + $len;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"gap_upto"}[0] >= 0 and $self->{"gap_upto"}[0] < $self->{"offset"}[0] + $len 0 7 7
Condition33%1/3
$self->{"gap_upto"}[0] >= 0 and $self->{"gap_upto"}[0] < $self->{"offset"}[0] + $len
!ll&&!rl&&r
070
MC/DC0%0/2
$self->{"gap_upto"}[0] >= 0 and $self->{"gap_upto"}[0] < $self->{"offset"}[0] + $len
$self->{"gap_upto"}[0] >= 0 $self->{"gap_upto"}[0] < $self->{"offset"}[0] + $len
Truth table33%1/3
A and B
A= $self->{"gap_upto"}[0] >= 0B= $self->{"gap_upto"}[0] < $self->{"offset"}[0] + $len
ABresult
0X0
100
111
225  
226 7 ▶ if (defined $rqs->[0]{rqclen}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rqs->[0]{"rqclen"}) 2 5 7
227 2 $rqs->[0]{rqclen} -= $len;
228 2 ▶ if ( ! $rqs->[0]{rqclen} && ! $rqs->[0]{rqchunked} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rqs->[0]{"rqclen"} or $rqs->[0]{"rqchunked"}) 2 0 2
Condition33%1/3
$rqs->[0]{"rqclen"} or $rqs->[0]{"rqchunked"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$rqs->[0]{"rqclen"} or $rqs->[0]{"rqchunked"}
$rqs->[0]{"rqclen"} $rqs->[0]{"rqchunked"}
Truth table33%1/3
A or B
A= $rqs->[0]{"rqclen"}B= $rqs->[0]{"rqchunked"}
ABresult
1X1
011
000
229 2 $rqs->[0]{state} |= RQBDY_DONE;
230 }
231 }
232  
233 7 $self->{offset}[0] += $len;
234 7 my $obj = $rqs->[0]{obj};
235 7 ▶ if ($self->{upgrade}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'upgrade'}) { } 5 2 7
elsif ($obj) { } 2 0 2
236 5 $self->{upgrade}(0,[ gap => $len ],$eof,$time);
237 } elsif ($obj) {
238 $obj->in_request_body(
239 [ gap => $len ],
240 2 ▶ $eof || ($rqs->[0]{state} & RQBDY_DONE ? 1:0),
Branch: 1 branch
BranchTrueFalseTotal
$rqs->[0]{'state'} & 2 ? : 2 0 2
Condition33%1/3
$eof || ($rqs->[0]{'state'} & 2 ? 1 : 0)
l!l&&r!l&&!r
020
MC/DC0%0/2
$eof || ($rqs->[0]{'state'} & 2 ? 1 : 0)
$eof ($rqs->[0]{'state'} & 2 ? 1 : 0)
Truth table33%1/3
A || B
A= $eofB= ($rqs->[0]{'state'} & 2 ? 1 : 0)
ABresult
1X1
011
000
241 $time
242 );
243 }
244 7 return $len;
245 }
246  
247 READ_DATA:
248  
249 85 ▶ if ($self->{error}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"error"}) 0 85 85
250 0 ▶ $DEBUG && $self->xdebug("no more data because of server side error");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $self->xdebug("no more data because of server side error") 0 0 0
251 0 return $bytes;
252 }
253  
254 85 ▶ if ($self->{upgrade}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"upgrade"}) 10 75 85
255 10 $self->{offset}[0] += length($data);
256 10 $self->{upgrade}(0,$data,$eof,$time);
257 10 return $bytes + length($data);
258 }
259  
260 75 ▶ if (@$rqs and $rqs->[0]{state} & RQ_ERROR ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rqs and $rqs->[0]{"state"} & 4) 0 75 75
Condition66%2/3
@$rqs and $rqs->[0]{"state"} & 4
!ll&&!rl&&r
16590
MC/DC0%0/2
@$rqs and $rqs->[0]{"state"} & 4
@$rqs $rqs->[0]{"state"} & 4
Truth table66%2/3
A and B
A= @$rqsB= $rqs->[0]{"state"} & 4
ABresult
0X0
100
111
261 # error reading request
262 0 ▶ $DEBUG && $self->xdebug("no more data because of client side error");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $self->xdebug("no more data because of client side error") 0 0 0
263 0 return $bytes;
264 }
265  
266 75 ▶ if ( ( ! @$rqs or $rqs->[0]{state} & RQBDY_DONE )
Branch: 1 branch
BranchTrueFalseTotal
if (!@$rqs || $rqs->[0]{"state"} & 2 and $data =~ /\A[\r\n]+/g) 0 75 75
Condition83%5/6
!@$rqs || $rqs->[0]{"state"} & 2 and $data =~ /\A[\r\n]+/g
!ll&&!rl&&r
44310
not @$rqs or $rqs->[0]{"state"} & 2
l!l&&r!l&&!r
161544
MC/DC0%0/3
!@$rqs || $rqs->[0]{"state"} & 2 and $data =~ /\A[\r\n]+/g
not @$rqs $rqs->[0]{"state"} & 2 $data =~ /\A[\r\n]+/g
Truth table60%3/5
not(A) or B and C
A= not @$rqsB= $rqs->[0]{"state"} & 2C= $data =~ /\A[\r\n]+/g
ABCresult
00X0
1X00
0100
1X11
0111
267 and $data =~m{\A[\r\n]+}g ) {
268 # first request or previous request body done
269 # new request might follow but maybe we only have trailing lines after
270 # the last request: eat empty lines
271 0 my $n = pos($data);
272 0 $bytes += $n;
273 0 $self->{offset}[0] += $n;
274 0 substr($data,0,$n,'');
275 0 ▶ %TRACE && $self->xtrace("eat empty lines before request header");
Branch: 1 branch
BranchTrueFalseTotal
%Net::Inspect::L7::HTTP::TRACE and $self->xtrace("eat empty lines before request header") 0 0 0
276 }
277  
278 75 ▶ if ( $data eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq "") 28 47 75
279 28 ▶ $DEBUG && $self->xdebug("no data, eof=$eof, bytes=$bytes");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $self->xdebug("no data, eof=$eof, bytes=$bytes") 0 28 28
280 28 ▶ return $bytes if ! $eof; # need more data
Branch: 1 branch
BranchTrueFalseTotal
unless $eof 28 0 28
281  
282 # handle EOF
283 # check if we got request body for last request
284 0 ▶ if ( @$rqs and not $rqs->[0]{state} & RQBDY_DONE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$rqs and not $rqs->[0]{"state"} & 2) 0 0 0
Condition0%0/3
@$rqs and not $rqs->[0]{"state"} & 2
!ll&&!rl&&r
000
MC/DC0%0/2
@$rqs and not $rqs->[0]{"state"} & 2
@$rqs not $rqs->[0]{"state"} & 2
Truth table0%0/3
A and not(B)
A= @$rqsB= not $rqs->[0]{"state"} & 2
ABresult
0X0
100
111
285 # request body not done yet
286 0 ▶ %TRACE && ($rqs->[0]{obj}||$self)->xtrace("request body not done but eof");
Branch: 1 branch
BranchTrueFalseTotal
%Net::Inspect::L7::HTTP::TRACE and ($rqs->[0]{"obj"} || $self)->xtrace("request body not done but eof") 0 0 0
Condition0%0/3
$rqs->[0]{'obj'} || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$rqs->[0]{'obj'} || $self
$rqs->[0]{'obj'} $self
Truth table0%0/3
A || B
A= $rqs->[0]{'obj'}B= $self
ABresult
1X1
011
000
287 0 ▶ ($rqs->[0]{obj}||$self)->fatal('eof but request body not done',0,$time);
Condition0%0/3
$rqs->[0]{'obj'} || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$rqs->[0]{'obj'} || $self
$rqs->[0]{'obj'} $self
Truth table0%0/3
A || B
A= $rqs->[0]{'obj'}B= $self
ABresult
1X1
011
000
288 0 $rqs->[0]{state} |= RQ_ERROR;
289 0 return $bytes;
290 }
291  
292 0 return $bytes; # request body done
293 }
294  
295 # create new request if no open request or last open request has the
296 # request body already done (pipelining)
297 47 ▶ if ( ! @$rqs or $rqs->[0]{state} & RQBDY_DONE ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @$rqs or $rqs->[0]{"state"} & 2) 17 30 47
Condition100%3/3
not @$rqs or $rqs->[0]{"state"} & 2
l!l&&r!l&&!r
16130
MC/DC100%2/2
not @$rqs or $rqs->[0]{"state"} & 2
not @$rqs $rqs->[0]{"state"} & 2
Truth table100%3/3
not(A) or B
A= not @$rqsB= $rqs->[0]{"state"} & 2
ABresult
1X1
011
000
298 17 my $reqid = ++$self->{lastreqid};
299 my $obj = $self->new_request({
300 17 %{$self->{meta}},
301 time => $time,
302 reqid => $reqid,
303 });
304 17 my $rq = {
305 obj => $obj,
306 # bitmask what is done: rpbody|rphdr|rqerror|rqbody|rqhdr
307 state => 0,
308 rqclen => undef, # open content-length request
309 rpclen => undef, # open content-length response
310 # chunked mode for request|response:
311 # false - no chunking
312 # 1,r[qp]clen == 0 - next will be chunk size
313 # 1,r[qp]clen > 0 - inside chunk data, need *clen
314 # 2 - next will be chunk
315 # 3 - after last chunk, next will be chunk trailer
316 rqchunked => undef, # chunked mode for request
317 rpchunked => undef, # chunked mode for response
318 request => undef, # result from parse_reqhdr
319 };
320  
321 17 ▶ if ($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Net::Inspect::L7::HTTP::DEBUG) 0 17 17
322 0 $rq->{reqid} = $reqid;
323 0 weaken($rq->{conn} = $self);
324 0 bless $rq, 'Net::Inspect::L7::HTTP::_DebugRequest';
325 0 $rq->xdebug("create new request");
326 }
327 17 lock_ref_keys($rq);
328 17 unshift @$rqs, $rq;
329 }
330  
331 47 my $rq = $rqs->[0];
332 47 my $obj = $rq->{obj};
333  
334 # read request header if not done
335 47 ▶ if ( not $rq->{state} & RQHDR_DONE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rq->{"state"} & 1) 20 27 47
336 # no request header yet, check if data contains it
337  
338 # leading newlines at beginning of request are legally ignored junk
339 20 ▶ if ( $data =~s{\A([\r\n]+)}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ s/\A([\r\n]+)//) 0 20 20
340 0 ▶ ($obj||$self)->in_junk(0,$1,0,$time);
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
341 }
342  
343 20 ▶ $DEBUG && $rq->xdebug("need to read request header");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("need to read request header") 0 20 20
344 20 ▶ if ($data =~s{\A(\A.*?\n\r?\n)}{}s) {
Branch: 4 branches
BranchTrueFalseTotal
if ($data =~ s/\A(\A.*?\n\r?\n)//s) { } 17 3 20
elsif ($data =~ /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/) { } 0 3 3
elsif (length $data > $self->{'hdr_maxsz'}[0]) { } 0 3 3
elsif ($eof) { } 0 3 3
345 17 ▶ $DEBUG && $rq->xdebug("got request header");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("got request header") 0 17 17
346 17 my $hdr = $1;
347 17 my $n = length($hdr);
348 17 $self->{offset}[0] += $n;
349 17 $bytes += $n;
350 17 $rq->{state} |= RQHDR_DONE; # rqhdr done
351  
352 17 my (%hdr,@warn);
353 17 my $err = parse_reqhdr($hdr,\%hdr,0);
354 17 ▶ if ($err and my $sub = $obj->can('fix_reqhdr')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and my $sub = $obj->can("fix_reqhdr")) 0 17 17
Condition66%2/3
$err and my $sub = $obj->can("fix_reqhdr")
!ll&&!rl&&r
1610
MC/DC0%0/2
$err and my $sub = $obj->can("fix_reqhdr")
$err my $sub = $obj->can("fix_reqhdr")
Truth table66%2/3
A and B
A= $errB= my $sub = $obj->can("fix_reqhdr")
ABresult
0X0
100
111
355 0 $hdr = $sub->($obj,$hdr);
356 0 $err = parse_reqhdr($hdr,\%hdr,0);
357 }
358  
359 17 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 1 16 17
360 1 ▶ ($obj||$self)->fatal($err,0,$time);
Condition33%1/3
$obj || $self
l!l&&r!l&&!r
100
MC/DC0%0/2
$obj || $self
$obj $self
Truth table33%1/3
A || B
A= $objB= $self
ABresult
1X1
011
000
361 1 $rq->{state} |= RQ_ERROR;
362 1 return $bytes;
363 }
364  
365 16 my $body_done;
366 16 ▶ if ($hdr{chunked}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hdr{'chunked'}) { } 4 12 16
elsif ($hdr{'content_length'}) { } 3 9 12
367 4 $rq->{rqchunked} = 1;
368 } elsif ($hdr{content_length}) {
369 3 $rq->{rqclen} = $hdr{content_length};
370 3 $self->{gap_upto}[0]= $self->{offset}[0] + $hdr{content_length};
371 } else {
372 9 $body_done = 1;
373 }
374  
375 16 $rq->{request} = \%hdr;
376  
377 16 ▶ %TRACE && $hdr{junk} && ($obj||$self)->xtrace(
Branch: 1 branch
BranchTrueFalseTotal
%Net::Inspect::L7::HTTP::TRACE and $hdr{"junk"} and ($obj || $self)->xtrace(qq[invalid request header data: $hdr{"junk"}]) 0 16 16
Condition16%1/6
%Net::Inspect::L7::HTTP::TRACE and $hdr{"junk"}
!ll&&!rl&&r
1600
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
%Net::Inspect::L7::HTTP::TRACE and $hdr{"junk"}
%Net::Inspect::L7::HTTP::TRACE $hdr{"junk"}
MC/DC0%0/2
$obj || $self
$obj $self
Truth table33%1/3
A and B
A= %Net::Inspect::L7::HTTP::TRACEB= $hdr{"junk"}
ABresult
0X0
100
111
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
378 "invalid request header data: $hdr{junk}");
379  
380 16 ▶ $obj && $obj->in_request_header($hdr,$time,\%hdr);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_request_header($hdr, $time, \%hdr) 16 0 16
381  
382 16 ▶ if ($body_done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body_done) 9 7 16
383 9 ▶ $DEBUG && $rq->xdebug("request done (no body)");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("request done (no body)") 0 9 9
384 9 $rq->{state} |= RQBDY_DONE;
385 9 ▶ if ($hdr{method} eq 'CONNECT' || $hdr{upgrade}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr{'method'} eq 'CONNECT' or $hdr{'upgrade'}) { } 2 7 9
Condition100%3/3
$hdr{"method"} eq "CONNECT" or $hdr{"upgrade"}
l!l&&r!l&&!r
117
MC/DC100%2/2
$hdr{"method"} eq "CONNECT" or $hdr{"upgrade"}
$hdr{"method"} eq "CONNECT" $hdr{"upgrade"}
Truth table100%3/3
A or B
A= $hdr{"method"} eq "CONNECT"B= $hdr{"upgrade"}
ABresult
1X1
011
000
386 # Don't propagate an empty request body:
387 # with CONNECT there will be no body and with Upgrade we
388 # will have no body if the upgrade succeeded. If it failed
389 # we submit the body later.
390 } else {
391 7 ▶ $obj && $obj->in_request_body('',1,$time);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_request_body("", 1, $time) 7 0 7
392 }
393 }
394  
395 } elsif ($data =~m{[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]}) {
396 # junk data, maybe attempt to use SOCKS instead of proxy request
397 0 ▶ ($obj||$self)->fatal( sprintf(
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
398 "junk data instead of request header '%s...'",
399 substr($data,0,10)),0,$time);
400 0 $rq->{state} |= RQ_ERROR;
401 0 return $bytes;
402 } elsif ( length($data) > $self->{hdr_maxsz}[0] ) {
403 0 ▶ ($obj||$self)->fatal('request header too big',0,$time);
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
404 0 $rq->{state} |= RQ_ERROR;
405 0 return $bytes;
406 } elsif ( $eof ) {
407 0 ▶ ($obj||$self)->fatal('eof in request header',0,$time);
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
408 0 $rq->{state} |= RQ_ERROR;
409 0 return $bytes;
410 } else {
411 # will be called on new data from upper flow
412 3 ▶ $DEBUG && $rq->xdebug("need more bytes for request header");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("need more bytes for request header") 0 3 3
413 3 return $bytes;
414 }
415 }
416  
417 # read request body if not done
418 43 ▶ if ( $data ne '' and not $rq->{state} & RQBDY_DONE ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne "" and not $rq->{"state"} & 2) 27 16 43
Condition66%2/3
$data ne "" and not $rq->{"state"} & 2
!ll&&!rl&&r
16027
MC/DC50%1/2
$data ne "" and not $rq->{"state"} & 2
$data ne "" not $rq->{"state"} & 2
Truth table66%2/3
A and not(B)
A= $data ne ""B= not $rq->{"state"} & 2
ABresult
0X0
100
111
419 # request body
420 27 ▶ if ( my $want = $rq->{rqclen} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $want = $rq->{'rqclen'}) { } 10 17 27
421 10 my $l = length($data);
422 10 ▶ if ( $l>=$want) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l >= $want) { } 8 2 10
423 # got all request body
424 8 ▶ $DEBUG && $rq->xdebug("need $want bytes, got all");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("need $want bytes, got all") 0 8 8
425 8 my $body = substr($data,0,$rq->{rqclen},'');
426 8 $self->{offset}[0] += $rq->{rqclen};
427 8 $bytes += $rq->{rqclen};
428 8 $rq->{rqclen} = 0;
429 8 ▶ if ( ! $rq->{rqchunked} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rq->{'rqchunked'}) { } 1 7 8
430 1 ▶ $DEBUG && $rq->xdebug("request done (full clen)");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("request done (full clen)") 0 1 1
431 1 $rq->{state} |= RQBDY_DONE; # req body done
432 1 ▶ $obj && $obj->in_request_body($body,1,$time)
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_request_body($body, 1, $time) 1 0 1
433 } else {
434 7 ▶ $obj && $obj->in_request_body($body,$eof,$time);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_request_body($body, $eof, $time) 7 0 7
435 7 $rq->{rqchunked} = 2; # get CRLF after chunk
436 }
437 } else {
438 # only part
439 2 ▶ $DEBUG && $rq->xdebug("need $want bytes, got only $l");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("need $want bytes, got only $l") 0 2 2
440 2 my $body = substr($data,0,$l,'');
441 2 $self->{offset}[0] += $l;
442 2 $bytes += $l;
443 2 $rq->{rqclen} -= $l;
444 2 ▶ $obj && $obj->in_request_body($body,0,$time);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_request_body($body, 0, $time) 2 0 2
445 }
446  
447 # Chunking: rfc2616, 3.6.1
448 } else {
449 # [2] must get CRLF after chunk
450 17 ▶ if ( $rq->{rqchunked} == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rq->{"rqchunked"} == 2) 7 10 17
451 7 ▶ $DEBUG && $rq->xdebug("want CRLF after chunk");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("want CRLF after chunk") 0 7 7
452 7 ▶ if ( $data =~m{\A\r?\n}g ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /\A\r?\n/g) { } 7 0 7
elsif (length $data >= 2) { } 0 0 0
453 7 my $n = pos($data);
454 7 $self->{offset}[0] += $n;
455 7 $bytes += $n;
456 7 substr($data,0,$n,'');
457 7 $rq->{rqchunked} = 1; # get next chunk header
458 7 ▶ $DEBUG && $rq->xdebug("got CRLF after chunk");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("got CRLF after chunk") 0 7 7
459 } elsif ( length($data)>=2 ) {
460 0 ▶ ($obj||$self)->fatal("no CRLF after chunk",0,$time);
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
461 0 $self->{error} = 1;
462 0 return $bytes;
463 } else {
464 # need more
465 0 return $bytes;
466 }
467 }
468  
469 # [1] must read chunk header
470 17 ▶ if ( $rq->{rqchunked} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rq->{"rqchunked"} == 1) 17 0 17
471 17 ▶ $DEBUG && $rq->xdebug("want chunk header");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("want chunk header") 0 17 17
472 17 ▶ if ( $data =~m{\A([\da-fA-F]+)[ \t]*(?:;.*)?\r?\n}g ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /\A([\da-fA-F]+)[ \t]*(?:;.*)?\r?\n/g) { } 11 6 17
elsif ($data =~ /\n/ or length $data > 8192) { } 0 6 6
473 11 $rq->{rqclen} = hex($1);
474 11 my $chdr = substr($data,0,pos($data),'');
475 11 $self->{offset}[0] += length($chdr);
476 11 $bytes += length($chdr);
477  
478 $self->{gap_upto}[0] = $self->{offset}[0] + $rq->{rqclen}
479 11 ▶ if $rq->{rqclen};
Branch: 1 branch
BranchTrueFalseTotal
if $rq->{"rqclen"} 7 4 11
480  
481 11 ▶ $obj->in_chunk_header(0,$chdr,$time) if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 11 0 11
482 11 ▶ $DEBUG && $rq->xdebug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug(qq[got chunk header - want $rq->{"rqclen"} bytes]) 0 11 11
483 "got chunk header - want $rq->{rqclen} bytes");
484 11 ▶ if ( ! $rq->{rqclen} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rq->{"rqclen"}) 4 7 11
485 # last chunk
486 4 $rq->{rqchunked} = 3;
487 4 ▶ $obj && $obj->in_request_body('',1,$time);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_request_body("", 1, $time) 4 0 4
488 }
489 ▶ } elsif ( $data =~m{\n} or length($data)>8192 ) {
Condition33%1/3
$data =~ /\n/ or length $data > 8192
l!l&&r!l&&!r
006
MC/DC0%0/2
$data =~ /\n/ or length $data > 8192
$data =~ /\n/ length $data > 8192
Truth table33%1/3
A or B
A= $data =~ /\n/B= length $data > 8192
ABresult
1X1
011
000
490 0 ▶ ($obj||$self)->fatal("invalid chunk header",0,$time);
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
491 0 $self->{error} = 1;
492 0 return $bytes;
493 } else {
494 # need more data
495 6 return $bytes;
496 }
497 }
498  
499 # [3] must read chunk trailer
500 11 ▶ if ( $rq->{rqchunked} == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rq->{"rqchunked"} == 3) 4 7 11
501 4 ▶ $DEBUG && $rq->xdebug("want chunk trailer");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("want chunk trailer") 0 4 4
502 4 ▶ if ( $data =~m{\A
Branch: 3 branches
BranchTrueFalseTotal
if ($data =~ /\A (?:\w[\w\-]*:.*\r?\n(?:[\t\040].*\r?\n)* )* # field:..+cont \r?\n /gx) { } 4 0 4
elsif ($data =~ /\n\r?\n/ or length $data > $self->{'hdr_maxsz'}[2]) { } 0 0 0
elsif ($eof) { } 0 0 0
503 (?:\w[\w\-]*:.*\r?\n(?:[\t\040].*\r?\n)* )* # field:..+cont
504 \r?\n
505 }xg) {
506 4 ▶ $DEBUG && $rq->xdebug("request done (chunk trailer)");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("request done (chunk trailer)") 0 4 4
507 4 my $trailer = substr($data,0,pos($data),'');
508 4 $self->{offset}[0] += length($trailer);
509 4 $bytes += length($trailer);
510 4 ▶ $obj->in_chunk_trailer(0,$trailer,$time) if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 4 0 4
511 4 $rq->{state} |= RQBDY_DONE; # request done
512 ▶ } elsif ( $data =~m{\n\r?\n}
Condition0%0/3
$data =~ /\n\r?\n/ or length $data > $self->{"hdr_maxsz"}[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
$data =~ /\n\r?\n/ or length $data > $self->{"hdr_maxsz"}[2]
$data =~ /\n\r?\n/ length $data > $self->{"hdr_maxsz"}[2]
Truth table0%0/3
A or B
A= $data =~ /\n\r?\n/B= length $data > $self->{"hdr_maxsz"}[2]
ABresult
1X1
011
000
513 or length($data) > $self->{hdr_maxsz}[2] ) {
514 0 ▶ ($obj||$self)->fatal("invalid chunk trailer",0,$time);
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
515 0 $self->{error} = 1;
516 0 return $bytes;
517 } elsif ( $eof ) {
518 # not fatal, because we got all data
519 0 ▶ %TRACE && ($obj||$self)->xtrace(
Branch: 1 branch
BranchTrueFalseTotal
%Net::Inspect::L7::HTTP::TRACE and ($obj || $self)->xtrace("eof before end of chunk trailer") 0 0 0
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
520 "eof before end of chunk trailer");
521 0 $self->{error} = 1;
522 0 return $bytes;
523 } else {
524 # need more
525 0 ▶ $DEBUG && $rq->xdebug("need more bytes for chunk trailer");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("need more bytes for chunk trailer") 0 0 0
526 0 return $bytes
527 }
528 }
529 }
530 }
531  
532 37 goto READ_DATA;
533 }
534  
535  
536  
537 # process response data
538 sub _in1 {
539 37 ▶ my ($self,$data,$eof,$time) = @_;
Subroutine _in1: called CC=127 SCAR=73.9
540  
541 37 my $rqs = $self->{requests};
542 37 my $bytes = 0; # processed bytes
543  
544 37 ▶ if ( ref($data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data) 2 35 37
545 # process gap in response data
546 2 ▶ croak "unknown type $data->[0]" if $data->[0] ne 'gap';
Branch: 1 branch
BranchTrueFalseTotal
if $data->[0] ne "gap" 0 2 2
547 2 my $len = $data->[1];
548  
549 2 ▶ croak 'existing error in connection' if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 2 2
550  
551 2 my $rqs = $self->{requests};
552 2 ▶ croak 'no open response' if ! @$rqs;
Branch: 1 branch
BranchTrueFalseTotal
unless @$rqs 0 2 2
553 2 my $rq = $rqs->[-1];
554 2 ▶ croak 'existing error in request' if $rq->{state} & RQ_ERROR;
Branch: 1 branch
BranchTrueFalseTotal
if $rq->{"state"} & 4 0 2 2
555 croak "gap too large" if $self->{gap_upto}[1]>=0
556 2 ▶ && $self->{gap_upto}[1] < $self->{offset}[1] + $len;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"gap_upto"}[1] >= 0 and $self->{"gap_upto"}[1] < $self->{"offset"}[1] + $len 0 2 2
Condition33%1/3
$self->{"gap_upto"}[1] >= 0 and $self->{"gap_upto"}[1] < $self->{"offset"}[1] + $len
!ll&&!rl&&r
020
MC/DC0%0/2
$self->{"gap_upto"}[1] >= 0 and $self->{"gap_upto"}[1] < $self->{"offset"}[1] + $len
$self->{"gap_upto"}[1] >= 0 $self->{"gap_upto"}[1] < $self->{"offset"}[1] + $len
Truth table33%1/3
A and B
A= $self->{"gap_upto"}[1] >= 0B= $self->{"gap_upto"}[1] < $self->{"offset"}[1] + $len
ABresult
0X0
100
111
557  
558 2 ▶ $rq->{rpclen} -= $len if defined $rq->{rpclen};
Branch: 1 branch
BranchTrueFalseTotal
if defined $rq->{"rpclen"} 2 0 2
559 2 $self->{offset}[1] += $len;
560  
561 2 my $obj = $rq->{obj};
562 2 ▶ if ($self->{upgrade}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->{'upgrade'}) { } 0 2 2
elsif ($rq->{'rpclen'} or not defined $rq->{'rpclen'} or $rq->{'rpchunked'}) { } 0 2 2
563 0 $self->{upgrade}(1,[ gap => $len ],$eof,$time);
564 ▶ } elsif ($rq->{rpclen}
Condition33%2/6
$rq->{"rpclen"} or not defined $rq->{"rpclen"} or $rq->{"rpchunked"}
l!l&&r!l&&!r
002
$rq->{"rpclen"} or not defined $rq->{"rpclen"}
l!l&&r!l&&!r
002
MC/DC0%0/3
$rq->{"rpclen"} or not defined $rq->{"rpclen"} or $rq->{"rpchunked"}
$rq->{"rpclen"} not defined $rq->{"rpclen"} $rq->{"rpchunked"}
Truth table25%1/4
A or not(B) or C
A= $rq->{"rpclen"}B= not defined $rq->{"rpclen"}C= $rq->{"rpchunked"}
ABCresult
1XX1
01X1
0011
0000
565 or !defined $rq->{rpclen}
566 or $rq->{rpchunked}) {
567 0 ▶ $obj && $obj->in_response_body([ gap => $len ],$eof,$time);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_response_body(["gap", $len], $eof, $time) 0 0 0
568 } else {
569 # done with request
570 2 ▶ $DEBUG && $rq->xdebug("response done (last gap)");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("response done (last gap)") 0 2 2
571 2 pop(@$rqs);
572 2 ▶ $obj && $obj->in_response_body([ gap => $len ],1,$time);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_response_body(["gap", $len], 1, $time) 2 0 2
573 }
574 2 return $len;
575 }
576  
577  
578 READ_DATA:
579  
580 60 ▶ return $bytes if $self->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"error"} 0 60 60
581 60 ▶ return $bytes if $data eq '' && !$eof;
Branch: 1 branch
BranchTrueFalseTotal
if $data eq "" and not $eof 20 40 60
Condition100%3/3
$data eq "" and not $eof
!ll&&!rl&&r
37320
MC/DC100%2/2
$data eq "" and not $eof
$data eq "" not $eof
Truth table100%3/3
A and not(B)
A= $data eq ""B= not $eof
ABresult
0X0
100
111
582  
583 40 ▶ if ($self->{upgrade}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"upgrade"}) 5 35 40
584 5 $self->{offset}[1] += length($data);
585 5 $self->{upgrade}(1,$data,$eof,$time);
586 5 return $bytes + length($data);
587 }
588  
589 35 ▶ if ( $data eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data eq "") 2 33 35
590 2 ▶ $DEBUG && $self->xdebug("no more data, eof=$eof bytes=$bytes");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $self->xdebug("no more data, eof=$eof bytes=$bytes") 0 2 2
591  
592 # handle EOF
593 # check if we got response body for last request
594 2 ▶ if ( @$rqs && $rqs->[-1]{state} & RPBDY_DONE_ON_EOF ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$rqs and $rqs->[-1]{'state'} & 16) { } 2 0 2
elsif (@$rqs) { } 0 0 0
Condition33%1/3
@$rqs and $rqs->[-1]{"state"} & 16
!ll&&!rl&&r
002
MC/DC0%0/2
@$rqs and $rqs->[-1]{"state"} & 16
@$rqs $rqs->[-1]{"state"} & 16
Truth table33%1/3
A and B
A= @$rqsB= $rqs->[-1]{"state"} & 16
ABresult
0X0
100
111
595 # response body done on eof
596 2 my $rq = pop(@$rqs);
597 2 ▶ $DEBUG && $rq->xdebug("response done (eof)");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("response done (eof)") 0 2 2
598 2 ▶ $rq->{obj}->in_response_body('',1,$time) if $rq->{obj};
Branch: 1 branch
BranchTrueFalseTotal
if $rq->{"obj"} 2 0 2
599  
600 } elsif ( @$rqs ) {
601 # response body not done yet
602 0 my $rq = pop(@$rqs);
603 0 ▶ $DEBUG && $rq->xdebug("response done (unexpected eof)");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("response done (unexpected eof)") 0 0 0
604 0 ▶ if (($rq->{state} & RPHDR_DONE) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (($rq->{'state'} & 8) == 0) { } 0 0 0
605 0 ▶ if ($data eq '' and $self->{lastreqid}>1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data eq '' and $self->{'lastreqid'} > 1) { } 0 0 0
elsif ($data eq '') { } 0 0 0
Condition0%0/3
$data eq "" and $self->{"lastreqid"} > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$data eq "" and $self->{"lastreqid"} > 1
$data eq "" $self->{"lastreqid"} > 1
Truth table0%0/3
A and B
A= $data eq ""B= $self->{"lastreqid"} > 1
ABresult
0X0
100
111
606 # We had already a request and now the server closes while
607 # the client is still sending a new request. This happens
608 # with keep-alive connections and the client needs to handle
609 # this case with retrying the request. Signal this issue by
610 # calling in_request_header with empty header.
611 0 ▶ ($rq->{obj}||$self)->in_request_header('',$time);
Condition0%0/3
$rq->{'obj'} || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$rq->{'obj'} || $self
$rq->{'obj'} $self
Truth table0%0/3
A || B
A= $rq->{'obj'}B= $self
ABresult
1X1
011
000
612 } elsif ($data eq '') {
613 0 ▶ ($rq->{obj}||$self)->fatal(
Condition0%0/3
$rq->{'obj'} || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$rq->{'obj'} || $self
$rq->{'obj'} $self
Truth table0%0/3
A || B
A= $rq->{'obj'}B= $self
ABresult
1X1
011
000
614 'eof before receiving first response', 1,$time);
615 } else {
616 0 ▶ %TRACE && ($rq->{obj}||$self)->xtrace(
Branch: 1 branch
BranchTrueFalseTotal
%Net::Inspect::L7::HTTP::TRACE and ($rq->{"obj"} || $self)->xtrace("eof within response header: '${data}'") 0 0 0
Condition0%0/3
$rq->{'obj'} || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$rq->{'obj'} || $self
$rq->{'obj'} $self
Truth table0%0/3
A || B
A= $rq->{'obj'}B= $self
ABresult
1X1
011
000
617 "eof within response header: '$data'");
618 0 ▶ ($rq->{obj}||$self)->fatal(
Condition0%0/3
$rq->{'obj'} || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$rq->{'obj'} || $self
$rq->{'obj'} $self
Truth table0%0/3
A || B
A= $rq->{'obj'}B= $self
ABresult
1X1
011
000
619 'eof within response header', 1,$time);
620 }
621 } else {
622 # eof inside a response or close for first request already.
623 0 ▶ %TRACE && ($rq->{obj}||$self)->xtrace("eof within response body");
Branch: 1 branch
BranchTrueFalseTotal
%Net::Inspect::L7::HTTP::TRACE and ($rq->{"obj"} || $self)->xtrace("eof within response body") 0 0 0
Condition0%0/3
$rq->{'obj'} || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$rq->{'obj'} || $self
$rq->{'obj'} $self
Truth table0%0/3
A || B
A= $rq->{'obj'}B= $self
ABresult
1X1
011
000
624 0 ▶ ($rq->{obj}||$self)->fatal('eof within response body', 1,$time);
Condition0%0/3
$rq->{'obj'} || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$rq->{'obj'} || $self
$rq->{'obj'} $self
Truth table0%0/3
A || B
A= $rq->{'obj'}B= $self
ABresult
1X1
011
000
625 }
626 }
627  
628 2 return $bytes; # done
629 }
630  
631 33 ▶ if ( ! @$rqs ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$rqs) 0 33 33
632 0 ▶ if ( $data =~s{\A([\r\n]+)}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ s/\A([\r\n]+)//) 0 0 0
633 # skip newlines after request because newlines at beginning of
634 # new request are allowed, stupid
635 0 $bytes += length($1);
636 0 goto READ_DATA;
637 }
638  
639 0 $self->fatal('data from server w/o request',1,$time);
640 0 $self->{error} = 1;
641 0 return $bytes;
642 }
643  
644 33 my $rq = $rqs->[-1];
645 33 my $obj = $rq->{obj};
646  
647 # read response header if not done
648 33 ▶ if ( not $rq->{state} & RPHDR_DONE ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rq->{"state"} & 8) 15 18 33
649 15 ▶ $DEBUG && $rq->xdebug("response header not read yet");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("response header not read yet") 0 15 15
650  
651 # leading newlines at beginning of response are legally ignored junk
652 15 ▶ if ( $data =~s{\A([\r\n]+)}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data =~ s/\A([\r\n]+)//) 0 15 15
653 0 ▶ ($obj||$self)->in_junk(1,$1,0,$time);
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
654 }
655  
656 # no response header yet, check if data contains it
657 15 ▶ if ( $data =~s{\A(.*?\n\r?\n)}{}s ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($data =~ s/\A(.*?\n\r?\n)//s) { } 14 1 15
elsif ($data =~ /[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]/) { } 0 1 1
elsif ($data =~ /[^\n]\r?\n\r?\n/g) { } 0 1 1
elsif (length $data > $self->{'hdr_maxsz'}[1]) { } 0 1 1
elsif ($eof) { } 0 1 1
658 14 my $hdr = $1;
659 14 my $n = length($hdr);
660 14 $bytes += $n;
661 14 $self->{offset}[1] += $n;
662  
663 14 my %hdr;
664 14 my $err = parse_rsphdr($hdr,$rq->{request},\%hdr);
665 14 ▶ if ($err and my $sub = $obj->can('fix_rsphdr')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err and my $sub = $obj->can("fix_rsphdr")) 0 14 14
Condition66%2/3
$err and my $sub = $obj->can("fix_rsphdr")
!ll&&!rl&&r
1310
MC/DC0%0/2
$err and my $sub = $obj->can("fix_rsphdr")
$err my $sub = $obj->can("fix_rsphdr")
Truth table66%2/3
A and B
A= $errB= my $sub = $obj->can("fix_rsphdr")
ABresult
0X0
100
111
666 0 $hdr = $sub->($obj,$hdr);
667 0 $err = parse_rsphdr($hdr,$rq->{request},\%hdr);
668 }
669  
670 14 ▶ goto error if $err;
Branch: 1 branch
BranchTrueFalseTotal
if $err 1 13 14
671 13 ▶ $DEBUG && $rq->xdebug("got response header");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("got response header") 0 13 13
672  
673 13 ▶ %TRACE && $hdr{junk} && ($obj||$self)->xtrace(
Branch: 1 branch
BranchTrueFalseTotal
%Net::Inspect::L7::HTTP::TRACE and $hdr{"junk"} and ($obj || $self)->xtrace(qq[invalid request header data: $hdr{"junk"}]) 0 13 13
Condition16%1/6
%Net::Inspect::L7::HTTP::TRACE and $hdr{"junk"}
!ll&&!rl&&r
1300
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
%Net::Inspect::L7::HTTP::TRACE and $hdr{"junk"}
%Net::Inspect::L7::HTTP::TRACE $hdr{"junk"}
MC/DC0%0/2
$obj || $self
$obj $self
Truth table33%1/3
A and B
A= %Net::Inspect::L7::HTTP::TRACEB= $hdr{"junk"}
ABresult
0X0
100
111
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
674 "invalid request header data: $hdr{junk}");
675  
676 13 ▶ if ($hdr{preliminary}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr{"preliminary"}) 1 12 13
677 # Preliminary response. Wait for read real response.
678 1 ▶ $obj && $obj->in_response_header($hdr,$time,\%hdr);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_response_header($hdr, $time, \%hdr) 1 0 1
679 1 goto READ_DATA;
680 }
681  
682 12 $rq->{state} |= RPHDR_DONE; # response header done
683  
684 12 ▶ if ($hdr{upgrade}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hdr{'upgrade'}) { } 2 10 12
elsif ($rq->{'request'}{'upgrade'}) { } 0 10 10
685 # Reset length to undef since we need to read until eof.
686 2 $rq->{rpclen} = undef;
687  
688 # If no object is given we just use a dummy function which
689 # returns the size of the data.
690 # If object has its own upgrade_XXXX method for the protocol we
691 # use this. Support for CONNECT has been traditionally built in
692 # but can be redefined with upgrade_CONNECT.
693  
694 2 ▶ if (!$obj) {
Branch: 4 branches
BranchTrueFalseTotal
if (not $obj) { } 0 2 2
elsif (my $sub = $obj->can('upgrade_' . $hdr{'upgrade'})) { } 1 1 2
elsif ($sub = $obj->can('upgrade_ANY')) { } 0 1 1
elsif ($hdr{'upgrade'} eq 'CONNECT') { } 1 0 1
695 0 ▶ $self->{upgrade} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
696 0 @{$self->{gap_upto}} = (-1,-1);
697  
698 } elsif (my $sub = $obj->can('upgrade_'.$hdr{upgrade})) {
699 # $sub might throw an error if it is unwilling to upgrade
700 # the connection based on request and response.
701 1 ▶ unless ($self->{upgrade} = eval {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"upgrade"} = eval { do { &$sub($obj, $self, $rq->{"request"}, \%hdr) } }) 0 1 1
702 1 $sub->($obj,$self,$rq->{request},\%hdr)
703 }) {
704 0 $err = "invalid connection upgrade '$hdr{upgrade}': $@";
705 0 goto error;
706 }
707  
708 } elsif ($sub = $obj->can('upgrade_ANY')) {
709 # $sub might throw an error if it is unwilling to upgrade
710 # the connection based on request and response.
711 0 ▶ unless ($self->{upgrade} = eval {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"upgrade"} = eval { do { &$sub($obj, $self, $rq->{"request"}, \%hdr, $hdr{"upgrade"}) } }) 0 0 0
712 $sub->($obj,$self,$rq->{request},\%hdr,$hdr{upgrade})
713 0 }) {
714 0 $err = "invalid connection upgrade '$hdr{upgrade}': $@";
715 0 goto error;
716 }
717  
718 } elsif ($hdr{upgrade} eq 'CONNECT') {
719 # Traditionally just calls in_data. If this is not available
720 # call dummy function.
721 $self->{upgrade} = $obj->can('in_data') && do {
722 1 weaken(my $wobj = $obj);
723 2 ▶ sub { $wobj->in_data(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
724 1 ▶ } || sub {};
Subroutine __ANON__: not called CC=1 SCAR=1.2
Condition33%2/6
$obj->can('in_data') && do { builtin::weaken(my $wobj = $obj); sub { $wobj->in_data(@_); } } || sub { }
l!l&&r!l&&!r
100
$obj->can("in_data") and do { builtin::weaken(my $wobj = $obj); sub { $wobj->in_data(@_); } }
!ll&&!rl&&r
001
MC/DC0%0/3
$obj->can('in_data') && do { builtin::weaken(my $wobj = $obj); sub { $wobj->in_data(@_); } } || sub { }
$obj->can("in_data") do { builtin::weaken(my $wobj = $obj); sub { $wobj->in_data(@_); } } sub { }
Truth table20%1/5
A and B || C
A= $obj->can("in_data")B= do { builtin::weaken(my $wobj = $obj); sub { $wobj->in_data(@_); } }C= sub { }
ABCresult
11X1
0X11
1011
0X00
1000
725 1 @{$self->{gap_upto}} = (-1,-1);
726  
727 } else {
728 0 $err = "unsupported connection upgrade '$hdr{upgrade}'";
729 0 goto error;
730 }
731  
732 2 goto done;
733 } elsif ($rq->{request}{upgrade}) {
734 # The client requested an upgrade which the server did not ack.
735 # Thus propagate the empty request body now.
736 0 ▶ $obj && $obj->in_request_body('',1,$time);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_request_body("", 1, $time) 0 0 0
737 }
738  
739 10 my $body_done;
740 10 ▶ if ($hdr{chunked}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($hdr{'chunked'}) { } 2 8 10
elsif (defined $hdr{'content_length'}) { } 6 2 8
741 2 $rq->{rpchunked} = 1;
742 } elsif (defined $hdr{content_length}) {
743 6 ▶ if (($rq->{rpclen} = $hdr{content_length})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rq->{'rpclen'} = $hdr{'content_length'}) { } 3 3 6
744 # content_length > 0, can do gaps
745 $self->{gap_upto}[1]= $self->{offset}[1]
746 3 + $hdr{content_length};
747 } else {
748 3 $body_done = 1;
749 }
750 } else {
751 # no length given but method supports body -> end with eof
752 2 $rq->{state} |= RPBDY_DONE_ON_EOF; # body done when eof
753 2 $self->{gap_upto}[1] = -1;
754 }
755  
756 12 ▶ done:
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_response_header($hdr, $time, \%hdr) 12 0 12
757 $obj && $obj->in_response_header($hdr,$time,\%hdr);
758 12 ▶ if ($body_done) {
Branch: 1 branch
BranchTrueFalseTotal
if ($body_done) 3 9 12
759 3 ▶ $DEBUG && $rq->xdebug("response done (no body)");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("response done (no body)") 0 3 3
760 3 pop(@$rqs);
761 3 ▶ $obj && $obj->in_response_body('',1,$time);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_response_body("", 1, $time) 3 0 3
762 }
763 12 goto READ_DATA;
764  
765 error:
766 1 $self->{error} = 1;
767 1 ▶ ($obj||$self)->fatal($err,1,$time);
Condition33%1/3
$obj || $self
l!l&&r!l&&!r
100
MC/DC0%0/2
$obj || $self
$obj $self
Truth table33%1/3
A || B
A= $objB= $self
ABresult
1X1
011
000
768 1 return $bytes;
769  
770 } elsif ($data =~m{[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]}) {
771 0 ▶ ($obj||$self)->fatal( sprintf(
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
772 "junk data instead of response header '%s...'",
773 substr($data,0,10)) ,1,$time);
774 0 $self->{error} = 1;
775 0 return $bytes;
776 } elsif ( $data =~m{[^\n]\r?\n\r?\n}g ) {
777 0 ▶ ($obj||$self)->fatal( sprintf("invalid response header syntax '%s'",
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
778 substr($data,0,pos($data))),1,$time);
779 0 $self->{error} = 1;
780 0 return $bytes;
781 } elsif ( length($data) > $self->{hdr_maxsz}[1] ) {
782 0 ▶ ($obj||$self)->fatal('response header too big',1,$time);
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
783 0 $self->{error} = 1;
784 0 return $bytes;
785 } elsif ( $eof ) {
786 0 ▶ ($obj||$self)->fatal('eof in response header',1,$time);
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
787 0 $self->{error} = 1;
788 0 return $bytes;
789 } else {
790 # will be called on new data from upper flow
791 1 ▶ $DEBUG && $rq->xdebug("need more data for response header");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("need more data for response header") 0 1 1
792 1 return $bytes;
793 }
794 }
795  
796 # read response body
797 18 ▶ if ( $data ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data ne "") 18 0 18
798 # response body
799 18 ▶ $DEBUG && $rq->xdebug("response body data");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("response body data") 0 18 18
800  
801 # have content-length or within chunk
802 18 ▶ if ( my $want = $rq->{rpclen} ) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $want = $rq->{'rpclen'}) { } 6 12 18
elsif ($rq->{'state'} & 16) { } 2 10 12
elsif (not $rq->{'rpchunked'}) { } 0 10 10
803 # called for content-length or to read content from chunk
804 # with known length
805 6 my $l = length($data);
806 6 ▶ if ( $l >= $want ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l >= $want) { } 5 1 6
807 5 ▶ $DEBUG && $rq->xdebug("need $want bytes, got all($l)");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("need $want bytes, got all($l)") 0 5 5
808 # got all response body
809 5 my $body = substr($data,0,$want,'');
810 5 $self->{offset}[1] += $want;
811 5 $bytes += $want;
812 5 $rq->{rpclen} = 0;
813 5 ▶ if ( ! $rq->{rpchunked} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rq->{'rpchunked'}) { } 1 4 5
814 # response done
815 1 pop(@$rqs);
816 1 ▶ $DEBUG && $rq->xdebug("response done (full clen received)");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("response done (full clen received)") 0 1 1
817 1 ▶ $obj && $obj->in_response_body($body,1,$time);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_response_body($body, 1, $time) 1 0 1
818 } else {
819 4 ▶ $obj->in_response_body($body,0,$time) if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 4 0 4
820 4 $rq->{rpchunked} = 2; # get CRLF after chunk
821 }
822 } else {
823 # only part
824 1 ▶ $DEBUG && $rq->xdebug("need $want bytes, got only $l");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("need $want bytes, got only $l") 0 1 1
825 1 my $body = substr($data,0,$l,'');
826 1 $self->{offset}[1] += $l;
827 1 $bytes += $l;
828 1 $rq->{rpclen} -= $l;
829 1 ▶ $obj->in_response_body($body,0,$time) if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 1 0 1
830 }
831  
832 # no content-length, no chunk: must read until eof
833 } elsif ( $rq->{state} & RPBDY_DONE_ON_EOF ) {
834 2 ▶ $DEBUG && $rq->xdebug("read until eof");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("read until eof") 0 2 2
835 2 $self->{offset}[1] += length($data);
836 2 $bytes += length($data);
837 2 ▶ if ($eof) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eof) 0 2 2
838 # response done
839 0 pop(@$rqs);
840 0 ▶ $DEBUG && $rq->xdebug("response done (eof)");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("response done (eof)") 0 0 0
841 }
842 2 ▶ $obj->in_response_body($data,$eof,$time) if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 2 0 2
843 2 $data = '';
844 2 return $bytes;
845  
846 # Chunking: rfc2616, 3.6.1
847 } elsif ( ! $rq->{rpchunked} ) {
848 # should not happen
849 0 die "no content-length and no chunked - why we are here?";
850 } else {
851 # [2] must get CRLF after chunk
852 10 ▶ if ( $rq->{rpchunked} == 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rq->{"rpchunked"} == 2) 4 6 10
853 4 ▶ $DEBUG && $rq->xdebug("want CRLF after chunk");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("want CRLF after chunk") 0 4 4
854 4 ▶ if ( $data =~m{\A\r?\n}g ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /\A\r?\n/g) { } 4 0 4
elsif (length $data >= 2) { } 0 0 0
855 4 my $n = pos($data);
856 4 $self->{offset}[1] += $n;
857 4 $bytes += $n;
858 4 substr($data,0,$n,'');
859 4 $rq->{rpchunked} = 1; # get next chunk header
860 4 ▶ $DEBUG && $rq->xdebug("got CRLF after chunk");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("got CRLF after chunk") 0 4 4
861 } elsif ( length($data)>=2 ) {
862 0 ▶ ($obj||$self)->fatal("no CRLF after chunk",1,$time);
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
863 0 $self->{error} = 1;
864 0 return $bytes;
865 } else {
866 # need more
867 0 return $bytes;
868 }
869 }
870  
871 # [1] must read chunk header
872 10 ▶ if ( $rq->{rpchunked} == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rq->{"rpchunked"} == 1) 10 0 10
873 10 ▶ $DEBUG && $rq->xdebug("want chunk header");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("want chunk header") 0 10 10
874 10 ▶ if ( $data =~m{\A([\da-fA-F]+)[ \t]*(?:;.*)?\r?\n}g ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /\A([\da-fA-F]+)[ \t]*(?:;.*)?\r?\n/g) { } 6 4 10
elsif ($data =~ /\n/ or length $data > 8192) { } 0 4 4
875 6 $rq->{rpclen} = hex($1);
876 6 my $chdr = substr($data,0,pos($data),'');
877 6 $self->{offset}[1] += length($chdr);
878 6 $bytes += length($chdr);
879 $self->{gap_upto}[1] = $self->{offset}[1] + $rq->{rpclen}
880 6 ▶ if $rq->{rpclen};
Branch: 1 branch
BranchTrueFalseTotal
if $rq->{"rpclen"} 4 2 6
881  
882 6 ▶ $obj->in_chunk_header(1,$chdr,$time) if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 6 0 6
883 6 ▶ $DEBUG && $rq->xdebug(
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug(qq[got chunk header - want $rq->{"rpclen"} bytes]) 0 6 6
884 "got chunk header - want $rq->{rpclen} bytes");
885 6 ▶ if ( ! $rq->{rpclen} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rq->{"rpclen"}) 2 4 6
886 # last chunk
887 2 $rq->{rpchunked} = 3;
888 2 ▶ $obj && $obj->in_response_body('',1,$time);
Branch: 1 branch
BranchTrueFalseTotal
$obj and $obj->in_response_body("", 1, $time) 2 0 2
889 }
890 ▶ } elsif ( $data =~m{\n} or length($data)>8192 ) {
Condition33%1/3
$data =~ /\n/ or length $data > 8192
l!l&&r!l&&!r
004
MC/DC0%0/2
$data =~ /\n/ or length $data > 8192
$data =~ /\n/ length $data > 8192
Truth table33%1/3
A or B
A= $data =~ /\n/B= length $data > 8192
ABresult
1X1
011
000
891 0 ▶ ($obj||$self)->fatal("invalid chunk header",1,$time);
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
892 0 $self->{error} = 1;
893 0 return $bytes;
894 } else {
895 # need more data
896 4 return $bytes;
897 }
898 }
899  
900 # [3] must read chunk trailer
901 6 ▶ if ( $rq->{rpchunked} == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rq->{"rpchunked"} == 3) 2 4 6
902 2 ▶ $DEBUG && $rq->xdebug("want chunk trailer");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("want chunk trailer") 0 2 2
903 2 ▶ if ( $data =~m{\A
Branch: 2 branches
BranchTrueFalseTotal
if ($data =~ /\A (?:\w[\w\-]*:.*\r?\n(?:[\t\040].*\r?\n)* )* # field:..+cont \r?\n /gx) { } 2 0 2
elsif ($data =~ /\n\r?\n/ or length $data > $self->{'hdr_maxsz'}[2]) { } 0 0 0
904 (?:\w[\w\-]*:.*\r?\n(?:[\t\040].*\r?\n)* )* # field:..+cont
905 \r?\n
906 }xg) {
907 2 ▶ $DEBUG && $rq->xdebug("response done (chunk trailer)");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("response done (chunk trailer)") 0 2 2
908 2 my $trailer = substr($data,0,pos($data),'');
909 2 $self->{offset}[1] += length($trailer);
910 2 $bytes += length($trailer);
911 2 ▶ $obj->in_chunk_trailer(1,$trailer,$time) if $obj;
Branch: 1 branch
BranchTrueFalseTotal
if $obj 2 0 2
912 2 pop(@$rqs); # done
913 ▶ } elsif ( $data =~m{\n\r?\n} or
Condition0%0/3
$data =~ /\n\r?\n/ or length $data > $self->{"hdr_maxsz"}[2]
l!l&&r!l&&!r
000
MC/DC0%0/2
$data =~ /\n\r?\n/ or length $data > $self->{"hdr_maxsz"}[2]
$data =~ /\n\r?\n/ length $data > $self->{"hdr_maxsz"}[2]
Truth table0%0/3
A or B
A= $data =~ /\n\r?\n/B= length $data > $self->{"hdr_maxsz"}[2]
ABresult
1X1
011
000
914 length($data)>$self->{hdr_maxsz}[2] ) {
915 0 ▶ ($obj||$self)->fatal("invalid chunk trailer",1,$time);
Condition0%0/3
$obj || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
$obj || $self
$obj $self
Truth table0%0/3
A || B
A= $objB= $self
ABresult
1X1
011
000
916 0 $self->{error} = 1;
917 0 return $bytes;
918 } else {
919 # need more
920 0 ▶ $DEBUG && $rq->xdebug("need more bytes for chunk trailer");
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG and $rq->xdebug("need more bytes for chunk trailer") 0 0 0
921 0 return $bytes
922 }
923 }
924 }
925 }
926  
927 12 goto READ_DATA;
928 }
929  
930 # parse and normalize header
931 sub parse_hdrfields {
932 31 ▶ my ($hdr,$fields) = @_;
Subroutine parse_hdrfields: called CC=8 SCAR=22.1
Pod documented
933 31 ▶ return '' if ! defined $hdr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hdr 13 18 31
934 18 my $bad = '';
935 parse:
936 36 ▶ while ( $hdr =~m{\G$token_value_cont}gc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr =~ /\G$token_value_cont/cg) 24 36 60
937 24 ▶ if ($3 eq '') {
Branch: 1 branch
BranchTrueFalseTotal
if ($3 eq '') { } 24 0 24
938 # no continuation line
939 24 push @{$fields->{ lc($1) }},$2;
940 } else {
941 # with continuation line
942 0 my ($k,$v) = ($1,$2.$3);
943 # <space>value-part<space> -> ' ' + value-part
944 0 $v =~s{[\r\n]+[ \t](.*?)[ \t]*}{ $1}g;
945 0 push @{$fields->{ lc($k) }},$v;
946 }
947 }
948 36 ▶ if (pos($hdr)//0 != length($hdr)) {
Branch: 1 branch
BranchTrueFalseTotal
if (pos $hdr // 0 != length $hdr) 18 18 36
Condition66%2/3
pos $hdr // 0 != length $hdr
l!l&&r!l&&!r
18018
MC/DC50%1/2
pos $hdr // 0 != length $hdr
pos $hdr 0 != length $hdr
Truth table66%2/3
A // B
A= pos $hdrB= 0 != length $hdr
ABresult
1X1
011
000
949 # bad line inside
950 18 ▶ substr($hdr,0,pos($hdr)//0,'');
Condition50%1/2
pos $hdr // 0
l!l
180
MC/DC0%0/1
pos $hdr // 0
pos $hdr
Truth table50%1/2
A
A= pos $hdr
Aresult
11
00
951 18 ▶ $bad .= $1 if $hdr =~s{\A([^\n]*)\n}{};
Branch: 1 branch
BranchTrueFalseTotal
if $hdr =~ s/\A([^\n]*)\n// 0 18 18
952 18 goto parse;
953 }
954 18 return $bad;
955 }
956  
957 sub parse_reqhdr {
958 17 ▶ my ($data,$hdr,$external_length) = @_;
Subroutine parse_reqhdr: called CC=26 SCAR=36.3
Pod documented
959 17 ▶ $data =~m{\A
Branch: 1 branch
BranchTrueFalseTotal
$data =~ m[\A ([A-Z]{2,20})[\040\t]+ # $1: method (\S+)[\040\t]+ # $2: path/URI HTTP/(1\.[01])[\40\t]* # $3: version \r?\n # (CR)LF ([^\r\n].*?\n)? # $4: fields \r?\n # final (CR)LF \Z]sx or return "invalid request header" 0 17 17
960 ([A-Z]{2,20})[\040\t]+ # $1: method
961 (\S+)[\040\t]+ # $2: path/URI
962 HTTP/(1\.[01])[\40\t]* # $3: version
963 \r?\n # (CR)LF
964 ([^\r\n].*?\n)? # $4: fields
965 \r?\n # final (CR)LF
966 \Z}sx or return "invalid request header";
967  
968 17 my $version = $3;
969 17 my $method = $1;
970 17 %$hdr = (
971 method => $method,
972 url => $2,
973 version => $version,
974 info => "$method $2 HTTP/$version",
975 # fields - hash of fields
976 # junk - bad header fields
977 # expect - expectations from expect header
978 # upgrade - { websocket => key }
979 # content_length
980 # chunked
981 );
982  
983 17 my %kv;
984 17 my $bad = parse_hdrfields($4,\%kv);
985 17 ▶ $hdr->{junk} = $bad if $bad ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $bad ne "" 0 17 17
986 17 $hdr->{fields} = \%kv;
987  
988 17 ▶ if ($version>=1.1 and $kv{expect}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= "1.1" and $kv{"expect"}) 1 16 17
Condition100%3/3
$version >= "1.1" and $kv{"expect"}
!ll&&!rl&&r
5111
MC/DC100%2/2
$version >= "1.1" and $kv{"expect"}
$version >= "1.1" $kv{"expect"}
Truth table100%3/3
A and B
A= $version >= "1.1"B= $kv{"expect"}
ABresult
0X0
100
111
989 1 for(@{$kv{expect}}) {
990 # ignore all but 100-continue
991 1 ▶ $hdr->{expect}{lc($1)} = 1 if m{\b(100-continue)\b}i
Branch: 1 branch
BranchTrueFalseTotal
if /\b(100-continue)\b/i 1 0 1
992 }
993 }
994  
995 # RFC2616 4.4.3:
996 # chunked transfer-encoding takes preferece before content-length
997 17 ▶ if ( $version >= 1.1 and
Branch: 2 branches
BranchTrueFalseTotal
if ($version >= 1.1 and grep {/(?:^|[ \t,])chunked(?:$|[ \t,;])/i;} @{$kv{'transfer-encoding'} or [];}) { } 4 13 17
elsif (my $cl = $kv{'content-length'}) { } 5 8 13
Condition100%3/3
$version >= "1.1" and grep {/(?:^|[ \t,])chunked(?:$|[ \t,;])/i;} @{$kv{"transfer-encoding"} or [];}
!ll&&!rl&&r
584
MC/DC100%2/2
$version >= "1.1" and grep {/(?:^|[ \t,])chunked(?:$|[ \t,;])/i;} @{$kv{"transfer-encoding"} or [];}
$version >= "1.1" grep {/(?:^|[ \t,])chunked(?:$|[ \t,;])/i;} @{$kv{"transfer-encoding"} or [];}
Truth table100%3/3
A and B
A= $version >= "1.1"B= grep {/(?:^|[ \t,])chunked(?:$|[ \t,;])/i;} @{$kv{"transfer-encoding"} or [];}
ABresult
0X0
100
111
998 4 grep { m{(?:^|[ \t,])chunked(?:$|[ \t,;])}i }
999 12 ▶ @{ $kv{'transfer-encoding'} || [] }
Branch: 1 branch
BranchTrueFalseTotal
$kv{"transfer-encoding"} or [] 8 4 12
1000 ) {
1001 4 $hdr->{chunked} = 1;
1002  
1003 } elsif ( my $cl = $kv{'content-length'} ) {
1004 return "multiple different content-length header in request"
1005 5 ▶ if @$cl>1 and do { my %x; @x{@$cl} = (); keys(%x) } > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$cl > 1 and do { my %x; @x{@$cl} = (); keys %x } > 1 0 5 5
Condition33%1/3
@$cl > 1 and do { my %x; @x{@$cl} = (); keys %x } > 1
!ll&&!rl&&r
500
MC/DC0%0/2
@$cl > 1 and do { my %x; @x{@$cl} = (); keys %x } > 1
@$cl > 1 do { my %x; @x{@$cl} = (); keys %x } > 1
Truth table33%1/3
A and B
A= @$cl > 1B= do { my %x; @x{@$cl} = (); keys %x } > 1
ABresult
0X0
100
111
1006 5 ▶ return "invalid content-length '$cl->[0]' in request"
Branch: 1 branch
BranchTrueFalseTotal
unless $cl->[0] =~ /^(\d+)$/ 1 4 5
1007 if $cl->[0] !~m{^(\d+)$};
1008 4 $hdr->{content_length} = $cl->[0];
1009 }
1010  
1011 16 ▶ if ( $METHODS_WITHOUT_RQBODY{$method} ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($METHODS_WITHOUT_RQBODY{$method}) { } 8 8 16
elsif ($METHODS_WITH_RQBODY{$method}) { } 8 0 8
elsif (not $hdr->{'chunked'}) { } 0 0 0
1012 # Complain if the client announced a body.
1013 return "no body allowed with $method"
1014 8 ▶ if $hdr->{content_length} or $hdr->{chunked};
Branch: 1 branch
BranchTrueFalseTotal
if $hdr->{"content_length"} or $hdr->{"chunked"} 0 8 8
Condition33%1/3
$hdr->{"content_length"} or $hdr->{"chunked"}
l!l&&r!l&&!r
008
MC/DC0%0/2
$hdr->{"content_length"} or $hdr->{"chunked"}
$hdr->{"content_length"} $hdr->{"chunked"}
Truth table33%1/3
A or B
A= $hdr->{"content_length"}B= $hdr->{"chunked"}
ABresult
1X1
011
000
1015  
1016 } elsif ( $METHODS_WITH_RQBODY{$method} ) {
1017 return "content-length or transfer-encoding chunked must be given with method $method"
1018 if ! $hdr->{chunked}
1019 and ! defined $hdr->{content_length}
1020 8 ▶ and ! $external_length;
Branch: 1 branch
BranchTrueFalseTotal
unless $hdr->{"chunked"} or defined $hdr->{"content_length"} or $external_length 0 8 8
Condition50%3/6
$hdr->{"chunked"} or defined $hdr->{"content_length"} or $external_length
l!l&&r!l&&!r
800
$hdr->{"chunked"} or defined $hdr->{"content_length"}
l!l&&r!l&&!r
440
MC/DC0%0/3
$hdr->{"chunked"} or defined $hdr->{"content_length"} or $external_length
$hdr->{"chunked"} defined $hdr->{"content_length"} $external_length
Truth table50%2/4
A or B or C
A= $hdr->{"chunked"}B= defined $hdr->{"content_length"}C= $external_length
ABCresult
1XX1
01X1
0011
0000
1021  
1022 } elsif ( ! $hdr->{chunked} ) {
1023 # if not given content-length is considered 0
1024 0 ▶ $hdr->{content_length} ||= 0;
Condition0%0/2
$hdr->{"content_length"} ||= 0
l!l
00
MC/DC0%0/1
$hdr->{"content_length"} ||= 0
$hdr->{"content_length"}
Truth table0%0/2
A
A= $hdr->{"content_length"}
Aresult
11
00
1025 }
1026  
1027 # Connection upgrade
1028 16 ▶ if ($version >= 1.1 and $kv{upgrade} and my %upgrade
Branch: 1 branch
BranchTrueFalseTotal
if ($version >= "1.1" and $kv{"upgrade"} and my(%upgrade) = map({lc $_, 1;} map({/($token)/g;} @{$kv{"upgrade"};}))) 1 15 16
Condition83%5/6
$version >= "1.1" and $kv{"upgrade"} and my(%upgrade) = map({lc $_, 1;} map({/($token)/g;} @{$kv{"upgrade"};}))
!ll&&!rl&&r
1501
$version >= "1.1" and $kv{"upgrade"}
!ll&&!rl&&r
5101
MC/DC66%2/3
$version >= "1.1" and $kv{"upgrade"} and my(%upgrade) = map({lc $_, 1;} map({/($token)/g;} @{$kv{"upgrade"};}))
$version >= "1.1" $kv{"upgrade"} my(%upgrade) = map({lc $_, 1;} map({/($token)/g;} @{$kv{"upgrade"};}))
Truth table75%3/4
A and B and C
A= $version >= "1.1"B= $kv{"upgrade"}C= my(%upgrade) = map({lc $_, 1;} map({/($token)/g;} @{$kv{"upgrade"};}))
ABCresult
0XX0
10X0
1100
1111
1029 1 = map { lc($_) => 1 } map { m{($token)}g } @{$kv{upgrade}}) {
1030 1 $hdr->{upgrade} = \%upgrade;
1031 }
1032  
1033 16 return; # no error
1034 }
1035  
1036 sub parse_rsphdr {
1037 14 ▶ my ($data,$request,$hdr,$warn) = @_;
Subroutine parse_rsphdr: called CC=29 SCAR=36.7
Pod documented
1038 14 ▶ $data =~ m{\A
Branch: 1 branch
BranchTrueFalseTotal
$data =~ m[\A HTTP/(1\.[01])[\040\t]+ # $1: version (\d\d\d) # $2: code (?:[\040\t]+([^\r\n]*))? # $3: reason \r?\n ([^\r\n].*?\n)? # $4: fields \r?\n # empty line \Z]sx or return "invalid response header" 0 14 14
1039 HTTP/(1\.[01])[\040\t]+ # $1: version
1040 (\d\d\d) # $2: code
1041 (?:[\040\t]+([^\r\n]*))? # $3: reason
1042 \r?\n
1043 ([^\r\n].*?\n)? # $4: fields
1044 \r?\n # empty line
1045 \Z}sx or return "invalid response header";
1046  
1047 14 my $version = $1;
1048 14 my $code = $2;
1049 14 %$hdr = (
1050 version => $version,
1051 code => $code,
1052 reason => $3,
1053 # fields
1054 # junk
1055 # content_length
1056 # chunked
1057 # upgrade
1058 # preliminary
1059 );
1060  
1061 14 my %kv;
1062 14 my $bad = parse_hdrfields($4,\%kv);
1063 14 $hdr->{fields} = \%kv;
1064 14 ▶ $hdr->{junk} = $bad if $bad ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $bad ne "" 0 14 14
1065  
1066 14 ▶ if ($code<=199) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code <= 199) 2 12 14
1067 # Preliminary responses do not contain any body.
1068 2 $hdr->{preliminary} = 1;
1069 2 $hdr->{content_length} = 0;
1070 2 ▶ if ($code == 100 and $request->{expect}{'100-continue'}
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 100 and $request->{'expect'}{'100-continue'} or $code == 102 or $code == 101) { } 2 0 2
Condition66%6/9
$code == 100 and $request->{"expect"}{"100-continue"} or $code == 102 or $code == 101
l!l&&r!l&&!r
110
$code == 100 and $request->{"expect"}{"100-continue"} or $code == 102
l!l&&r!l&&!r
101
$code == 100 and $request->{"expect"}{"100-continue"}
!ll&&!rl&&r
101
MC/DC0%0/4
$code == 100 and $request->{"expect"}{"100-continue"} or $code == 102 or $code == 101
$code == 100 $request->{"expect"}{"100-continue"} $code == 102 $code == 101
Truth table28%2/7
A and B or C or D
A= $code == 100B= $request->{"expect"}{"100-continue"}C= $code == 102D= $code == 101
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
1071 or $code == 102 or $code == 101) {
1072 # 100 should only happen with Expect: 100-continue from client
1073 } else {
1074 0 ▶ push @$warn,"unexpected intermediate status code $code" if $warn;
Branch: 1 branch
BranchTrueFalseTotal
if $warn 0 0 0
1075 }
1076 }
1077  
1078 # Switching Protocols
1079 # Any upgrade must have both a "Connection: upgrade" and a
1080 # "Upgrade: newprotocol" header.
1081 14 ▶ if ($code == 101) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code == 101) 1 13 14
1082 1 my %proto;
1083 1 ▶ if ($request->{upgrade}
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{"upgrade"} and grep {/\bUPGRADE\b/i;} @{$kv{"connection"} or [];}) 1 0 1
Condition33%1/3
$request->{"upgrade"} and grep {/\bUPGRADE\b/i;} @{$kv{"connection"} or [];}
!ll&&!rl&&r
001
MC/DC0%0/2
$request->{"upgrade"} and grep {/\bUPGRADE\b/i;} @{$kv{"connection"} or [];}
$request->{"upgrade"} grep {/\bUPGRADE\b/i;} @{$kv{"connection"} or [];}
Truth table33%1/3
A and B
A= $request->{"upgrade"}B= grep {/\bUPGRADE\b/i;} @{$kv{"connection"} or [];}
ABresult
0X0
100
111
1084 1 ▶ and grep { m{\bUPGRADE\b}i } @{$kv{connection} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$kv{"connection"} or [] 0 1 1
1085 1 ▶ for(@{$kv{upgrade} || []}) {
Branch: 1 branch
BranchTrueFalseTotal
$kv{"upgrade"} or [] 0 1 1
1086 1 $proto{lc($_)} = 1 for split(m{\s*[,;]\s*});
1087 }
1088 }
1089  
1090 1 ▶ if (keys(%proto) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %proto == 1) { } 1 0 1
1091 1 $hdr->{upgrade} = (keys %proto)[0];
1092 1 $hdr->{preliminary} = 0;
1093 1 $hdr->{content_length} = undef;
1094 } else {
1095 0 return "invalid or unsupported connection upgrade";
1096 }
1097 }
1098  
1099 # successful response to CONNECT
1100 14 ▶ if ($request->{method} eq 'CONNECT' and $code >= 200 and $code < 300) {
Branch: 1 branch
BranchTrueFalseTotal
if ($request->{"method"} eq "CONNECT" and $code >= 200 and $code < 300) 1 13 14
Condition66%4/6
$request->{"method"} eq "CONNECT" and $code >= 200 and $code < 300
!ll&&!rl&&r
1301
$request->{"method"} eq "CONNECT" and $code >= 200
!ll&&!rl&&r
1301
MC/DC33%1/3
$request->{"method"} eq "CONNECT" and $code >= 200 and $code < 300
$request->{"method"} eq "CONNECT" $code >= 200 $code < 300
Truth table50%2/4
A and B and C
A= $request->{"method"} eq "CONNECT"B= $code >= 200C= $code < 300
ABCresult
0XX0
10X0
1100
1111
1101 1 $hdr->{upgrade} = 'CONNECT';
1102 1 $hdr->{content_length} = 0;
1103 1 delete $hdr->{chunked};
1104 1 return;
1105 }
1106  
1107 # RFC2616 4.4.3:
1108 # chunked transfer-encoding takes preferece before content-length
1109 13 ▶ if ( $version >= 1.1 and
Branch: 2 branches
BranchTrueFalseTotal
if ($version >= 1.1 and grep {/(?:^|[ \t,])chunked(?:$|[ \t,;])/i;} @{$kv{'transfer-encoding'} or [];}) { } 2 11 13
elsif (my $cl = $kv{'content-length'}) { } 5 6 11
Condition100%3/3
$version >= "1.1" and grep {/(?:^|[ \t,])chunked(?:$|[ \t,;])/i;} @{$kv{"transfer-encoding"} or [];}
!ll&&!rl&&r
472
MC/DC100%2/2
$version >= "1.1" and grep {/(?:^|[ \t,])chunked(?:$|[ \t,;])/i;} @{$kv{"transfer-encoding"} or [];}
$version >= "1.1" grep {/(?:^|[ \t,])chunked(?:$|[ \t,;])/i;} @{$kv{"transfer-encoding"} or [];}
Truth table100%3/3
A and B
A= $version >= "1.1"B= grep {/(?:^|[ \t,])chunked(?:$|[ \t,;])/i;} @{$kv{"transfer-encoding"} or [];}
ABresult
0X0
100
111
1110 2 grep { m{(?:^|[ \t,])chunked(?:$|[ \t,;])}i }
1111 9 ▶ @{ $kv{'transfer-encoding'} || [] }
Branch: 1 branch
BranchTrueFalseTotal
$kv{"transfer-encoding"} or [] 7 2 9
1112 ) {
1113 2 $hdr->{chunked} = 1;
1114  
1115 } elsif ( my $cl = $kv{'content-length'} ) {
1116 return "multiple different content-length header in response"
1117 5 ▶ if @$cl>1 and do { my %x; @x{@$cl} = (); keys(%x) } > 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$cl > 1 and do { my %x; @x{@$cl} = (); keys %x } > 1 0 5 5
Condition33%1/3
@$cl > 1 and do { my %x; @x{@$cl} = (); keys %x } > 1
!ll&&!rl&&r
500
MC/DC0%0/2
@$cl > 1 and do { my %x; @x{@$cl} = (); keys %x } > 1
@$cl > 1 do { my %x; @x{@$cl} = (); keys %x } > 1
Truth table33%1/3
A and B
A= @$cl > 1B= do { my %x; @x{@$cl} = (); keys %x } > 1
ABresult
0X0
100
111
1118 5 ▶ return "invalid content-length '$cl->[0]' in response"
Branch: 1 branch
BranchTrueFalseTotal
unless $cl->[0] =~ /^(\d+)$/ 1 4 5
1119 if $cl->[0] !~m{^(\d+)$};
1120 4 $hdr->{content_length} = $cl->[0];
1121 }
1122  
1123 12 ▶ if ($CODE_WITHOUT_RPBODY{$code}
Branch: 1 branch
BranchTrueFalseTotal
if ($CODE_WITHOUT_RPBODY{$code} or $METHODS_WITHOUT_RPBODY{$request->{"method"}}) 4 8 12
Condition66%2/3
$CODE_WITHOUT_RPBODY{$code} or $METHODS_WITHOUT_RPBODY{$request->{"method"}}
l!l&&r!l&&!r
408
MC/DC50%1/2
$CODE_WITHOUT_RPBODY{$code} or $METHODS_WITHOUT_RPBODY{$request->{"method"}}
$CODE_WITHOUT_RPBODY{$code} $METHODS_WITHOUT_RPBODY{$request->{"method"}}
Truth table66%2/3
A or B
A= $CODE_WITHOUT_RPBODY{$code}B= $METHODS_WITHOUT_RPBODY{$request->{"method"}}
ABresult
1X1
011
000
1124 or $METHODS_WITHOUT_RPBODY{$request->{method}}) {
1125 # no content, even if specified
1126 4 $hdr->{content_length} = 0;
1127 4 delete $hdr->{chunked};
1128 4 return;
1129 }
1130  
1131 8 return;
1132 }
1133  
1134  
1135 sub new_request {
1136 17 ▶ my $self = shift;
Subroutine new_request: called CC=1 SCAR=0.0
Pod documented
1137 17 return $self->{upper_flow}->new_request(@_,$self)
1138 }
1139  
1140 # return open requests
1141 sub open_requests {
1142 0 ▶ my $self = shift;
Subroutine open_requests: not called CC=4 SCAR=30.0
Pod documented
1143 0 ▶ my @rq = @_ ? @{$self->{requests}}[@_] : @{$self->{requests}};
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
1144 return wantarray
1145 0 ▶ ? map { $_->{obj} ? ($_->{obj}):() } @rq
Branch: 2 branches
BranchTrueFalseTotal
wantarray ? : 0 0 0
$_->{'obj'} ? : 0 0 0
1146 : 0 + @rq;
1147 }
1148  
1149 sub fatal {
1150 0 ▶ my ($self,$reason,$dir,$time) = @_;
Subroutine fatal: not called CC=2 SCAR=17.9
Pod documented
1151 0 ▶ %TRACE && $self->xtrace($reason);
Branch: 1 branch
BranchTrueFalseTotal
%Net::Inspect::L7::HTTP::TRACE and $self->xtrace($reason) 0 0 0
1152 }
1153  
1154 sub xtrace {
1155 0 ▶ my $self = shift;
Subroutine xtrace: not called CC=1 SCAR=6.9
Pod undocumented
1156 0 my $msg = shift;
1157 0 $msg = "$$.$self->{connid} $msg";
1158 0 unshift @_,$msg;
1159 0 goto &trace;
1160 }
1161  
1162 sub xdebug {
1163 0 ▶ $DEBUG or return;
Subroutine xdebug: not called CC=2 SCAR=17.9
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG or return 0 0 0
1164 0 my $self = shift;
1165 0 my $msg = shift;
1166 0 $msg = "$$.$self->{connid} $msg";
1167 0 unshift @_,$msg;
1168 0 goto &debug;
1169 }
1170  
1171 sub dump_state {
1172 0 ▶ $DEBUG or defined wantarray or return;
Subroutine dump_state: not called CC=5 SCAR=34.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$Net::Inspect::L7::HTTP::DEBUG or defined wantarray or return 0 0 0
Condition0%0/3
$Net::Inspect::L7::HTTP::DEBUG or defined wantarray
l!l&&r!l&&!r
000
MC/DC0%0/2
$Net::Inspect::L7::HTTP::DEBUG or defined wantarray
$Net::Inspect::L7::HTTP::DEBUG defined wantarray
Truth table0%0/3
A or B
A= $Net::Inspect::L7::HTTP::DEBUGB= defined wantarray
ABresult
1X1
011
000
1173 0 my $self = shift;
1174 0 my $m = $self->{meta};
1175 my $msg = sprintf("%s.%d -> %s.%d ",
1176 0 $m->{saddr},$m->{sport},$m->{daddr},$m->{dport});
1177 0 my $rqs = $self->{requests};
1178 0 ▶ for( my $i=0;$i<@$rqs;$i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$rqs) 0 0 0
1179 $msg .= sprintf("request#$i state=%05b %s",
1180 0 $rqs->[$i]{state},$rqs->[$i]{request}{info});
1181 }
1182 0 ▶ return $msg if defined wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if defined wantarray 0 0 0
1183 0 $self->xdebug($msg);
1184 }
1185  
1186  
1187 {
1188 package Net::Inspect::L7::HTTP::_DebugRequest;
1189 sub xdebug {
1190 0 ▶ my $rq = shift;
Subroutine xdebug: not called CC=1 SCAR=6.9
1191 0 my $msg = shift;
1192 0 unshift @_, $rq->{conn}, "#$rq->{reqid} $msg";
1193 0 goto &Net::Inspect::L7::HTTP::xdebug;
1194 }
1195 }
1196  
1197 1 1;
1198  
1199 __END__
« Inspect/L4/TCP.pm ↑ Summary Inspect/L7/HTTP/WebSocket.pm »