blib/lib/HTTP/Daemon.pm

filter: Statementstmt 21.9% 88 / 401 Branchbran 7.4% 16 / 216 Conditioncond 5.4% 3 / 55 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 48.7% 19 / 39 Podpod 100% 4 / 4 totaltotal 17.4% 130 / 745 CC 134 SCAR 70.4
CC
134
Cov
16%
get_request
3306.0
send_response
306.0
send_file_response
90.0
CRAP 1143.3
↑ Summary
1 package HTTP::Daemon; # git description: v6.15-42-g961eff5
2  
3 # ABSTRACT: A simple http server class
4  
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '6.17';
9  
10 2 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 our @ISA = qw(IO::Socket::IP);
13  
14 2 our $PROTO = "HTTP/1.1";
15  
16 2 our $DEBUG;
17  
18 1 ▶sub new {
Subroutine new: called CC=7 SCAR=25.0
Pod documented
19 1 my ($class, %args) = @_;
20 1 ▶ $args{Listen} ||= 5;
Condition50%1/2
$args{"Listen"} ||= 5
l!l
01
MC/DC0%0/1
$args{"Listen"} ||= 5
$args{"Listen"}
Truth table50%1/2
A
A= $args{"Listen"}
Aresult
11
00
21 1 ▶ $args{Proto} ||= 'tcp';
Condition50%1/2
$args{"Proto"} ||= "tcp"
l!l
01
MC/DC0%0/1
$args{"Proto"} ||= "tcp"
$args{"Proto"}
Truth table50%1/2
A
A= $args{"Proto"}
Aresult
11
00
22  
23 # Handle undefined or empty local address the same way as
24 # IO::Socket::INET -- use unspecified address
25 1 for my $key (qw(LocalAddr LocalHost)) {
26 2 ▶ if (exists $args{$key} && (!defined $args{$key} || $args{$key} eq '')) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{$key} and !defined($args{$key}) || $args{$key} eq "") 0 2 2
Condition16%1/6
exists $args{$key} and !defined($args{$key}) || $args{$key} eq ""
!ll&&!rl&&r
200
not defined $args{$key} or $args{$key} eq ""
l!l&&r!l&&!r
000
MC/DC0%0/3
exists $args{$key} and !defined($args{$key}) || $args{$key} eq ""
exists $args{$key} not defined $args{$key} $args{$key} eq ""
Truth table25%1/4
A and not(B) or C
A= exists $args{$key}B= not defined $args{$key}C= $args{$key} eq ""
ABCresult
0XX0
1000
11X1
1011
27 0 delete $args{$key};
28 }
29 }
30 1 return $class->SUPER::new(%args);
31 }
32  
33 0 ▶sub accept {
Subroutine accept: not called CC=4 SCAR=30.0
Pod documented
34 0 my $self = shift;
35 0 ▶ my $pkg = shift || "HTTP::Daemon::ClientConn";
Condition0%0/2
shift() || 'HTTP::Daemon::ClientConn'
l!l
00
MC/DC0%0/1
shift() || 'HTTP::Daemon::ClientConn'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
36 0 my ($sock, $peer) = $self->SUPER::accept($pkg);
37 0 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) { } 0 0 0
38 0 ${*$sock}{'httpd_daemon'} = $self;
39 0 ▶ return wantarray ? ($sock, $peer) : $sock;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
40 }
41 else {
42 0 return;
43 }
44 }
45  
46 1 ▶sub url {
Subroutine url: called CC=5 SCAR=16.6
Pod documented
47 1 my $self = shift;
48  
49 1 my $host = $self->sockhost;
50 1 $host =~ s/%/%25/g;
51 1 ▶ $host = "127.0.0.1" if $host eq "0.0.0.0";
Branch: 1 branch
BranchTrueFalseTotal
if $host eq "0.0.0.0" 0 1 1
52 1 ▶ $host = "::1" if $host eq "::";
Branch: 1 branch
BranchTrueFalseTotal
if $host eq "::" 0 1 1
53 1 ▶ $host = "[$host]" if $self->sockdomain == Socket::AF_INET6;
Branch: 1 branch
BranchTrueFalseTotal
if $self->sockdomain == 10 1 0 1
54  
55 1 my $url = $self->_default_scheme . "://" . $host;
56 1 my $port = $self->sockport;
57 1 ▶ $url .= ":$port" if $port != $self->_default_port;
Branch: 1 branch
BranchTrueFalseTotal
if $port != $self->_default_port 1 0 1
58 1 $url .= "/";
59 1 $url;
60 }
61  
62 1 ▶sub _default_port {
Subroutine _default_port: called CC=1 SCAR=0.0
63 1 80;
64 }
65  
66 1 ▶sub _default_scheme {
Subroutine _default_scheme: called CC=1 SCAR=0.0
67 1 "http";
68 }
69  
70 0 ▶sub product_tokens {
Subroutine product_tokens: not called CC=1 SCAR=6.9
Pod documented
71 0 "libwww-perl-daemon/$HTTP::Daemon::VERSION";
72 }
73  
74 package # hide from PAUSE
75 HTTP::Daemon::ClientConn;
76  
77 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 2 ▶use IO::Socket::IP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
81 2 our @ISA = qw(IO::Socket::IP);
82 2 our $DEBUG;
83 2 *DEBUG = \$HTTP::Daemon::DEBUG;
84  
85 2 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
86 2 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
87 2 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 2 ▶use HTTP::Date qw(time2str);
Subroutine BEGIN: called CC=1 SCAR=0.0
89 2 ▶use LWP::MediaTypes qw(guess_media_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
90 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 # "\r\n" is not portable
93 2 my $CRLF = "\015\012";
94 2 my $HTTP_1_0 = _http_version("HTTP/1.0");
95 2 my $HTTP_1_1 = _http_version("HTTP/1.1");
96  
97  
98 0 ▶sub get_request {
Subroutine get_request: not called CC=57 SCAR=81.0
99 0 my ($self, $only_headers) = @_;
100 0 ▶ if (${*$self}{'httpd_nomore'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { *$self }->{"httpd_nomore"}) 0 0 0
101 0 $self->reason("No more requests from this connection");
102 0 return;
103 }
104  
105 0 $self->reason("");
106 0 my $buf = ${*$self}{'httpd_rbuf'};
107 0 ▶ $buf = "" unless defined $buf;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $buf 0 0 0
108  
109 0 my $timeout = ${*$self}{'io_socket_timeout'};
110 0 my $fdset = "";
111 0 vec($fdset, $self->fileno, 1) = 1;
112 0 local ($_);
113  
114 READ_HEADER:
115 0 while (1) {
116  
117 # loop until we have the whole header in $buf
118 0 $buf =~ s/^(?:\015?\012)+//; # ignore leading blank lines
119 0 ▶ if ($buf =~ /\012/) { # potential, has at least one line
Branch: 2 branches
BranchTrueFalseTotal
if ($buf =~ /\012/) { } 0 0 0
elsif (length $buf > 16384) { } 0 0 0
120 0 ▶ if ($buf =~ /^\w+[^\012]+HTTP\/\d+\.\d+\015?\012/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ m[^\w+[^\012]+HTTP/\d+\.\d+\015?\012]) { } 0 0 0
121 0 ▶ if ($buf =~ /\015?\012\015?\012/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($buf =~ /\015?\012\015?\012/) { } 0 0 0
elsif (length $buf > 16384) { } 0 0 0
122 0 last READ_HEADER; # we have it
123 }
124 elsif (length($buf) > 16 * 1024) {
125 0 $self->send_error(413); # REQUEST_ENTITY_TOO_LARGE
126 0 $self->reason("Very long header");
127 0 return;
128 }
129 }
130 else {
131 0 last READ_HEADER; # HTTP/0.9 client
132 }
133 }
134 elsif (length($buf) > 16 * 1024) {
135 0 $self->send_error(414); # REQUEST_URI_TOO_LARGE
136 0 $self->reason("Very long first line");
137 0 return;
138 }
139 0 ▶ print STDERR "Need more data for complete header\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
140 0 ▶ return unless $self->_need_more($buf, $timeout, $fdset);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_need_more($buf, $timeout, $fdset) 0 0 0
141 }
142 0 ▶ if ($buf !~ s/^(\S+)[ \t]+(\S+)(?:[ \t]+(HTTP\/\d+\.\d+))?[^\012]*\012//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($buf =~ s[^(\S+)[ \t]+(\S+)(?:[ \t]+(HTTP/\d+\.\d+))?[^\012]*\012][]) 0 0 0
143 0 ${*$self}{'httpd_client_proto'} = _http_version("HTTP/1.0");
144 0 $self->send_error(400); # BAD_REQUEST
145 0 $self->reason("Bad request line: $buf");
146 0 return;
147 }
148 0 my $method = $1;
149 0 my $uri = $2;
150 0 ▶ my $proto = $3 || "HTTP/0.9";
Condition0%0/2
$3 || 'HTTP/0.9'
l!l
00
MC/DC0%0/1
$3 || 'HTTP/0.9'
$3
Truth table0%0/2
A
A= $3
Aresult
11
00
151 0 ▶ $uri = "http://$uri" if $method eq "CONNECT";
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "CONNECT" 0 0 0
152 0 $uri = $HTTP::URI_CLASS->new($uri, $self->daemon->url);
153 0 my $r = HTTP::Request->new($method, $uri);
154 0 $r->protocol($proto);
155 0 ${*$self}{'httpd_client_proto'} = $proto = _http_version($proto);
156 0 ${*$self}{'httpd_head'} = ($method eq "HEAD");
157  
158 0 ▶ if ($proto >= $HTTP_1_0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto >= $HTTP_1_0) 0 0 0
159  
160 # we expect to find some headers
161 0 my ($key, $val);
162 HEADER:
163 0 ▶ while ($buf =~ s/^([^\012]*)\012//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ s/^([^\012]*)\012//) 0 0 0
164 0 $_ = $1;
165 0 s/\015$//;
166 0 ▶ if (/^([^:\s]+)\s*:\s*(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^([^:\s]+)\s*:\s*(.*)/) { } 0 0 0
elsif (/^\s+(.*)/) { } 0 0 0
167 0 ▶ $r->push_header($key, $val) if $key;
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
168 0 ($key, $val) = ($1, $2);
169 }
170 elsif (/^\s+(.*)/) {
171 0 $val .= " $1";
172 }
173 else {
174 0 last HEADER;
175 }
176 }
177 0 ▶ $r->push_header($key, $val) if $key;
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
178 }
179  
180 0 my $conn = $r->header('Connection');
181 0 ▶ if ($proto >= $HTTP_1_1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($proto >= $HTTP_1_1) { } 0 0 0
182 0 ▶ ${*$self}{'httpd_nomore'}++ if $conn && lc($conn) =~ /\bclose\b/;
Branch: 1 branch
BranchTrueFalseTotal
if $conn and lc($conn) =~ /\bclose\b/ 0 0 0
Condition0%0/3
$conn and lc($conn) =~ /\bclose\b/
!ll&&!rl&&r
000
MC/DC0%0/2
$conn and lc($conn) =~ /\bclose\b/
$conn lc($conn) =~ /\bclose\b/
Truth table0%0/3
A and B
A= $connB= lc($conn) =~ /\bclose\b/
ABresult
0X0
100
111
183 }
184 else {
185 0 ▶ ${*$self}{'httpd_nomore'}++
Branch: 1 branch
BranchTrueFalseTotal
unless $conn and lc($conn) =~ /\bkeep-alive\b/ 0 0 0
Condition0%0/3
$conn and lc($conn) =~ /\bkeep-alive\b/
!ll&&!rl&&r
000
MC/DC0%0/2
$conn and lc($conn) =~ /\bkeep-alive\b/
$conn lc($conn) =~ /\bkeep-alive\b/
Truth table0%0/3
A and B
A= $connB= lc($conn) =~ /\bkeep-alive\b/
ABresult
0X0
100
111
186 unless $conn && lc($conn) =~ /\bkeep-alive\b/;
187 }
188  
189 0 ▶ if ($only_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($only_headers) 0 0 0
190 0 ${*$self}{'httpd_rbuf'} = $buf;
191 0 return $r;
192 }
193  
194 # Find out how much content to read
195 0 my $tr_enc = $r->header('Transfer-Encoding');
196 0 my $ct_type = $r->header('Content-Type');
197 0 my $ct_len = $r->header('Content-Length');
198  
199 # Act on the Expect header, if it's there
200 0 for my $e ($r->header('Expect')) {
201 0 ▶ if (lc($e) eq '100-continue') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $e eq '100-continue') { } 0 0 0
202 0 $self->send_status_line(100);
203 0 $self->send_crlf;
204 }
205 else {
206 0 $self->send_error(417);
207 0 $self->reason("Unsupported Expect header value");
208 0 return;
209 }
210 }
211  
212 0 ▶ if ($tr_enc && lc($tr_enc) eq 'chunked') {
Branch: 4 branches
BranchTrueFalseTotal
if ($tr_enc and lc $tr_enc eq 'chunked') { } 0 0 0
elsif ($tr_enc) { } 0 0 0
elsif ($ct_len) { } 0 0 0
elsif ($ct_type and $ct_type =~ m[^multipart/\w+\s*;.*boundary\s*=\s*("?)(\w+)\1]i) { } 0 0 0
Condition0%0/3
$tr_enc and lc $tr_enc eq "chunked"
!ll&&!rl&&r
000
MC/DC0%0/2
$tr_enc and lc $tr_enc eq "chunked"
$tr_enc lc $tr_enc eq "chunked"
Truth table0%0/3
A and B
A= $tr_encB= lc $tr_enc eq "chunked"
ABresult
0X0
100
111
213  
214 # Handle chunked transfer encoding
215 0 my $body = "";
216 CHUNK:
217 0 while (1) {
218 0 ▶ print STDERR "Chunked\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
219 0 ▶ if ($buf =~ s/^([^\012]*)\012//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ s/^([^\012]*)\012//) { } 0 0 0
220 0 my $chunk_head = $1;
221 0 ▶ unless ($chunk_head =~ /^([0-9A-Fa-f]+)/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($chunk_head =~ /^([0-9A-Fa-f]+)/) 0 0 0
222 0 $self->send_error(400);
223 0 $self->reason("Bad chunk header $chunk_head");
224 0 return;
225 }
226 0 my $size = hex($1);
227 0 ▶ last CHUNK if $size == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $size == 0 0 0 0
228  
229 0 my $missing = $size - length($buf) + 2; # 2=CRLF at chunk end
230 # must read until we have a complete chunk
231 0 ▶ while ($missing > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($missing > 0) 0 0 0
232 0 ▶ print STDERR "Need $missing more bytes\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
233 0 my $n = $self->_need_more($buf, $timeout, $fdset);
234 0 ▶ return unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
235 0 $missing -= $n;
236 }
237 0 $body .= substr($buf, 0, $size);
238 0 substr($buf, 0, $size + 2) = '';
239  
240 }
241 else {
242 # need more data in order to have a complete chunk header
243 0 ▶ return unless $self->_need_more($buf, $timeout, $fdset);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_need_more($buf, $timeout, $fdset) 0 0 0
244 }
245 }
246 0 $r->content($body);
247  
248 # pretend it was a normal entity body
249 0 $r->remove_header('Transfer-Encoding');
250 0 $r->header('Content-Length', length($body));
251  
252 0 my ($key, $val);
253 FOOTER:
254 0 while (1) {
255 0 ▶ if ($buf !~ /\012/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $buf =~ /\012/) { } 0 0 0
256  
257 # need at least one line to look at
258 0 ▶ return unless $self->_need_more($buf, $timeout, $fdset);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_need_more($buf, $timeout, $fdset) 0 0 0
259 }
260 else {
261 0 $buf =~ s/^([^\012]*)\012//;
262 0 $_ = $1;
263 0 s/\015$//;
264 0 ▶ if (/^([\w\-]+)\s*:\s*(.*)/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^([\w\-]+)\s*:\s*(.*)/) { } 0 0 0
elsif (/^\s+(.*)/) { } 0 0 0
elsif (not length $_) { } 0 0 0
265 0 ▶ $r->push_header($key, $val) if $key;
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
266 0 ($key, $val) = ($1, $2);
267 }
268 elsif (/^\s+(.*)/) {
269 0 $val .= " $1";
270 }
271 elsif (!length) {
272 0 last FOOTER;
273 }
274 else {
275 0 $self->reason("Bad footer syntax");
276 0 return;
277 }
278 }
279 }
280 0 ▶ $r->push_header($key, $val) if $key;
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
281  
282 }
283 elsif ($tr_enc) {
284 0 $self->send_error(501); # Unknown transfer encoding
285 0 $self->reason("Unknown transfer encoding '$tr_enc'");
286 0 return;
287  
288 }
289 elsif ($ct_len) {
290  
291 # After a security issue, we ensure we comply to
292 # RFC-7230 -- HTTP/1.1 Message Syntax and Routing
293 # section 3.3.2 -- Content-Length
294 # section 3.3.3 -- Message Body Length
295  
296 # split and clean up Content-Length ', ' separated string
297 my @vals
298 0 = map { my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str }
299 split ',', $ct_len;
300  
301 # check that they are all numbers (RFC: Content-Length = 1*DIGIT)
302 0 my @nums = grep {/^[0-9]+$/} @vals;
303 0 ▶ unless (@vals == @nums) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@vals == @nums) 0 0 0
304 0 my $reason = "Content-Length value must be an unsigned integer";
305 0 $self->send_error(400, $reason);
306 0 $self->reason($reason);
307 0 return;
308 }
309  
310 # check they are all the same
311 0 my $ct_len = shift @nums;
312 0 foreach (@nums) {
313 0 ▶ next if $_ == $ct_len;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $ct_len 0 0 0
314 0 my $reason = "Content-Length values are not the same";
315 0 $self->send_error(400, $reason);
316 0 $self->reason($reason);
317 0 return;
318 }
319  
320 # ensure we have now a fixed header, with only 1 value
321 0 $r->header('Content-Length' => $ct_len);
322  
323 # Plain body specified by "Content-Length"
324 0 my $missing = $ct_len - length($buf);
325 0 ▶ while ($missing > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($missing > 0) 0 0 0
326 0 ▶ print "Need $missing more bytes of content\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
327 0 my $n = $self->_need_more($buf, $timeout, $fdset);
328 0 ▶ return unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
329 0 $missing -= $n;
330 }
331 0 ▶ if (length($buf) > $ct_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf > $ct_len) { } 0 0 0
332 0 $r->content(substr($buf, 0, $ct_len));
333 0 substr($buf, 0, $ct_len) = '';
334 }
335 else {
336 0 $r->content($buf);
337 0 $buf = '';
338 }
339 }
340 ▶ elsif ($ct_type
Condition0%0/3
$ct_type and $ct_type =~ m[^multipart/\w+\s*;.*boundary\s*=\s*("?)(\w+)\1]i
!ll&&!rl&&r
000
MC/DC0%0/2
$ct_type and $ct_type =~ m[^multipart/\w+\s*;.*boundary\s*=\s*("?)(\w+)\1]i
$ct_type $ct_type =~ m[^multipart/\w+\s*;.*boundary\s*=\s*("?)(\w+)\1]i
Truth table0%0/3
A and B
A= $ct_typeB= $ct_type =~ m[^multipart/\w+\s*;.*boundary\s*=\s*("?)(\w+)\1]i
ABresult
0X0
100
111
341 && $ct_type =~ m/^multipart\/\w+\s*;.*boundary\s*=\s*("?)(\w+)\1/i)
342 {
343  
344 # Handle multipart content type
345 0 my $boundary = "$CRLF--$2--";
346 0 my $index;
347 0 while (1) {
348 0 $index = index($buf, $boundary);
349 0 ▶ last if $index >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index >= 0 0 0 0
350  
351 # end marker not yet found
352 0 ▶ return unless $self->_need_more($buf, $timeout, $fdset);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_need_more($buf, $timeout, $fdset) 0 0 0
353 }
354 0 $index += length($boundary);
355 0 $r->content(substr($buf, 0, $index));
356 0 substr($buf, 0, $index) = '';
357  
358 }
359 0 ${*$self}{'httpd_rbuf'} = $buf;
360  
361 0 $r;
362 }
363  
364 0 ▶sub _need_more {
Subroutine _need_more: not called CC=8 SCAR=42.8
365 0 my $self = shift;
366  
367 #my($buf,$timeout,$fdset) = @_;
368 0 ▶ if ($_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) 0 0 0
369 0 my ($timeout, $fdset) = @_[1, 2];
370 0 ▶ print STDERR "select(,,,$timeout)\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
371 0 my $n = select($fdset, undef, undef, $timeout);
372 0 ▶ unless ($n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 0 0 0
373 0 ▶ $self->reason(defined($n) ? "Timeout" : "select: $!");
Branch: 1 branch
BranchTrueFalseTotal
defined $n ? : 0 0 0
374 0 return;
375 }
376 }
377 0 ▶ print STDERR "sysread()\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
378 0 my $n = sysread($self, $_[0], 2048, length($_[0]));
379 0 ▶ $self->reason(defined($n) ? "Client closed" : "sysread: $!") unless $n;
Branch: 2 branches
BranchTrueFalseTotal
unless $n 0 0 0
defined $n ? : 0 0 0
380 0 $n;
381 }
382  
383 0 ▶sub read_buffer {
Subroutine read_buffer: not called CC=2 SCAR=17.9
384 0 my $self = shift;
385 0 my $old = ${*$self}{'httpd_rbuf'};
386 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
387 0 ${*$self}{'httpd_rbuf'} = shift;
388 }
389 0 $old;
390 }
391  
392 0 ▶sub reason {
Subroutine reason: not called CC=2 SCAR=17.9
393 0 my $self = shift;
394 0 my $old = ${*$self}{'httpd_reason'};
395 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
396 0 ${*$self}{'httpd_reason'} = shift;
397 }
398 0 $old;
399 }
400  
401 0 ▶sub proto_ge {
Subroutine proto_ge: not called CC=1 SCAR=6.9
402 0 my $self = shift;
403 0 ${*$self}{'httpd_client_proto'} >= _http_version(shift);
404 }
405  
406 4 ▶sub _http_version {
Subroutine _http_version: called CC=2 SCAR=7.1
407 4 local ($_) = shift;
408 4 ▶ return 0 unless m,^(?:HTTP/)?(\d+)\.(\d+)$,i;
Branch: 1 branch
BranchTrueFalseTotal
unless m[^(?:HTTP/)?(\d+)\.(\d+)$]i 0 4 4
409 4 $1 * 1000 + $2;
410 }
411  
412 0 ▶sub antique_client {
Subroutine antique_client: not called CC=1 SCAR=6.9
413 0 my $self = shift;
414 0 ${*$self}{'httpd_client_proto'} < $HTTP_1_0;
415 }
416  
417 0 ▶sub force_last_request {
Subroutine force_last_request: not called CC=1 SCAR=6.9
418 0 my $self = shift;
419 0 ${*$self}{'httpd_nomore'}++;
420 }
421  
422 0 ▶sub head_request {
Subroutine head_request: not called CC=1 SCAR=6.9
423 0 my $self = shift;
424 0 ${*$self}{'httpd_head'};
425 }
426  
427  
428 0 ▶sub send_status_line {
Subroutine send_status_line: not called CC=7 SCAR=40.3
429 0 my ($self, $status, $message, $proto) = @_;
430 0 ▶ return if $self->antique_client;
Branch: 1 branch
BranchTrueFalseTotal
if $self->antique_client 0 0 0
431 0 ▶ $status ||= RC_OK;
Condition0%0/2
$status ||= 200
l!l
00
MC/DC0%0/1
$status ||= 200
$status
Truth table0%0/2
A
A= $status
Aresult
11
00
432 0 ▶ $message ||= status_message($status) || "";
Condition0%0/5
$message ||= &status_message($status) || ""
l!l&&r!l&&!r
000
&status_message($status) || ''
l!l
00
MC/DC0%0/2
$message ||= &status_message($status) || ""
$message &status_message($status)
Truth table0%0/3
A ||= B
A= $messageB= &status_message($status)
ABresult
1X1
011
000
433 0 ▶ $proto ||= $HTTP::Daemon::PROTO || "HTTP/1.1";
Condition0%0/5
$proto ||= $HTTP::Daemon::PROTO || "HTTP/1.1"
l!l&&r!l&&!r
000
$HTTP::Daemon::PROTO || 'HTTP/1.1'
l!l
00
MC/DC0%0/2
$proto ||= $HTTP::Daemon::PROTO || "HTTP/1.1"
$proto $HTTP::Daemon::PROTO
Truth table0%0/3
A ||= B
A= $protoB= $HTTP::Daemon::PROTO
ABresult
1X1
011
000
434 0 print $self "$proto $status $message$CRLF";
435 }
436  
437 0 ▶sub send_crlf {
Subroutine send_crlf: not called CC=1 SCAR=6.9
438 0 my $self = shift;
439 0 print $self $CRLF;
440 }
441  
442 0 ▶sub send_basic_header {
Subroutine send_basic_header: not called CC=3 SCAR=24.8
443 0 my $self = shift;
444 0 ▶ return if $self->antique_client;
Branch: 1 branch
BranchTrueFalseTotal
if $self->antique_client 0 0 0
445 0 $self->send_status_line(@_);
446 0 print $self "Date: ", time2str(time), $CRLF;
447 0 my $product = $self->daemon->product_tokens;
448 0 ▶ print $self "Server: $product$CRLF" if $product;
Branch: 1 branch
BranchTrueFalseTotal
if $product 0 0 0
449 }
450  
451 0 ▶sub send_header {
Subroutine send_header: not called CC=3 SCAR=24.8
452 0 my $self = shift;
453 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
454 0 my ($k, $v) = splice(@_, 0, 2);
455 0 ▶ $v = "" unless defined($v);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
456 0 print $self "$k: $v$CRLF";
457 }
458 }
459  
460 0 ▶sub send_response {
Subroutine send_response: not called CC=17 SCAR=57.2
461 0 my $self = shift;
462 0 my $res = shift;
463 0 ▶ if (!ref $res) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $res) 0 0 0
464 0 ▶ $res ||= RC_OK;
Condition0%0/2
$res ||= 200
l!l
00
MC/DC0%0/1
$res ||= 200
$res
Truth table0%0/2
A
A= $res
Aresult
11
00
465 0 $res = HTTP::Response->new($res, @_);
466 }
467 0 my $content = $res->content;
468 0 my $chunked;
469 0 ▶ unless ($self->antique_client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->antique_client) 0 0 0
470 0 my $code = $res->code;
471 0 $self->send_basic_header($code, $res->message, $res->protocol);
472 0 ▶ if ($code =~ /^(1\d\d|[23]04)$/) {
Branch: 4 branches
BranchTrueFalseTotal
if ($code =~ /^(1\d\d|[23]04)$/) { } 0 0 0
elsif ($res->request and $res->request->method eq 'HEAD') { } 0 0 0
elsif (ref $content eq 'CODE') { } 0 0 0
elsif (length $content) { } 0 0 0
473  
474 # make sure content is empty
475 0 $res->remove_header("Content-Length");
476 0 $content = "";
477 }
478 ▶ elsif ($res->request && $res->request->method eq "HEAD") {
Condition0%0/3
$res->request and $res->request->method eq "HEAD"
!ll&&!rl&&r
000
MC/DC0%0/2
$res->request and $res->request->method eq "HEAD"
$res->request $res->request->method eq "HEAD"
Truth table0%0/3
A and B
A= $res->requestB= $res->request->method eq "HEAD"
ABresult
0X0
100
111
479  
480 # probably OK
481 }
482 elsif (ref($content) eq "CODE") {
483 0 ▶ if ($self->proto_ge("HTTP/1.1")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->proto_ge('HTTP/1.1')) { } 0 0 0
484 0 $res->push_header("Transfer-Encoding" => "chunked");
485 0 $chunked++;
486 }
487 else {
488 0 $self->force_last_request;
489 }
490 }
491 elsif (length($content)) {
492 0 $res->header("Content-Length" => length($content));
493 }
494 else {
495 0 $self->force_last_request;
496 0 $res->header('connection', 'close');
497 }
498 0 print $self $res->headers_as_string($CRLF);
499 0 print $self $CRLF; # separates headers and content
500 }
501 0 ▶ if ($self->head_request) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->head_request) { } 0 0 0
elsif (ref $content eq 'CODE') { } 0 0 0
elsif (length $content) { } 0 0 0
502  
503 # no content
504 }
505 elsif (ref($content) eq "CODE") {
506 0 while (1) {
507 0 my $chunk = &$content();
508 0 ▶ last unless defined($chunk) && length($chunk);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $chunk and length $chunk 0 0 0
Condition0%0/3
defined $chunk and length $chunk
!ll&&!rl&&r
000
MC/DC0%0/2
defined $chunk and length $chunk
defined $chunk length $chunk
Truth table0%0/3
A and B
A= defined $chunkB= length $chunk
ABresult
0X0
100
111
509 0 ▶ if ($chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunked) { } 0 0 0
510 0 printf $self "%x%s%s%s", length($chunk), $CRLF, $chunk, $CRLF;
511 }
512 else {
513 0 print $self $chunk;
514 }
515 }
516 0 ▶ print $self "0$CRLF$CRLF" if $chunked; # no trailers either
Branch: 1 branch
BranchTrueFalseTotal
if $chunked 0 0 0
517 }
518 elsif (length $content) {
519 0 print $self $content;
520 }
521 }
522  
523 0 ▶sub send_redirect {
Subroutine send_redirect: not called CC=8 SCAR=42.8
524 0 my ($self, $loc, $status, $content) = @_;
525 0 ▶ $status ||= RC_MOVED_PERMANENTLY;
Condition0%0/2
$status ||= 301
l!l
00
MC/DC0%0/1
$status ||= 301
$status
Truth table0%0/2
A
A= $status
Aresult
11
00
526 0 ▶ Carp::croak("Status '$status' is not redirect") unless is_redirect($status);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_redirect($status) 0 0 0
527 0 $self->send_basic_header($status);
528 0 my $base = $self->daemon->url;
529 0 ▶ $loc = $HTTP::URI_CLASS->new($loc, $base) unless ref($loc);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $loc 0 0 0
530 0 $loc = $loc->abs($base);
531 0 print $self "Location: $loc$CRLF";
532  
533 0 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 0 0 0
534 0 ▶ my $ct_type = $content =~ /^\s*</ ? "text/html" : "text/plain";
Branch: 1 branch
BranchTrueFalseTotal
$content =~ /^\s*</ ? : 0 0 0
535 0 print $self "Content-Type: $ct_type$CRLF";
536 }
537 0 print $self $CRLF;
538 0 ▶ print $self $content if $content && !$self->head_request;
Branch: 1 branch
BranchTrueFalseTotal
if $content and not $self->head_request 0 0 0
Condition0%0/3
$content and not $self->head_request
!ll&&!rl&&r
000
MC/DC0%0/2
$content and not $self->head_request
$content not $self->head_request
Truth table0%0/3
A and not(B)
A= $contentB= not $self->head_request
ABresult
0X0
100
111
539 0 $self->force_last_request; # no use keeping the connection open
540 }
541  
542 0 ▶sub send_error {
Subroutine send_error: not called CC=6 SCAR=37.4
543 0 my ($self, $status, $error) = @_;
544 0 ▶ $status ||= RC_BAD_REQUEST;
Condition0%0/2
$status ||= 400
l!l
00
MC/DC0%0/1
$status ||= 400
$status
Truth table0%0/2
A
A= $status
Aresult
11
00
545 0 ▶ Carp::croak("Status '$status' is not an error") unless is_error($status);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_error($status) 0 0 0
546 0 my $mess = status_message($status);
547 0 ▶ $error ||= "";
Condition0%0/2
$error ||= ""
l!l
00
MC/DC0%0/1
$error ||= ""
$error
Truth table0%0/2
A
A= $error
Aresult
11
00
548 0 $mess = <<EOT;
549 <title>$status $mess</title>
550 <h1>$status $mess</h1>
551 $error
552 EOT
553 0 ▶ unless ($self->antique_client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->antique_client) 0 0 0
554 0 $self->send_basic_header($status);
555 0 print $self "Content-Type: text/html$CRLF";
556 0 print $self "Content-Length: " . length($mess) . $CRLF;
557 0 print $self $CRLF;
558 }
559 0 ▶ print $self $mess unless $self->head_request;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->head_request 0 0 0
560 0 $status;
561 }
562  
563 0 ▶sub send_file_response {
Subroutine send_file_response: not called CC=9 SCAR=45.0
564 0 my ($self, $file) = @_;
565 0 ▶ if (-d $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $file) { } 0 0 0
elsif (-f _) { } 0 0 0
566 0 $self->send_dir($file);
567 }
568 elsif (-f _) {
569  
570 # plain file
571 0 local (*F);
572 0 ▶ sysopen(F, $file, 0) or return $self->send_error(RC_FORBIDDEN);
Branch: 1 branch
BranchTrueFalseTotal
sysopen F, $file, 0 or return $self->send_error(403) 0 0 0
573 0 binmode(F);
574 0 my ($mime_type, $file_enc) = guess_media_type($file);
575 0 my ($size, $mtime) = (stat _)[7, 9];
576 0 ▶ unless ($self->antique_client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->antique_client) 0 0 0
577 0 $self->send_basic_header;
578 0 print $self "Content-Type: $mime_type$CRLF";
579 0 ▶ print $self "Content-Encoding: $file_enc$CRLF" if $file_enc;
Branch: 1 branch
BranchTrueFalseTotal
if $file_enc 0 0 0
580 0 ▶ print $self "Content-Length: $size$CRLF" if $size;
Branch: 1 branch
BranchTrueFalseTotal
if $size 0 0 0
581 0 ▶ print $self "Last-Modified: ", time2str($mtime), "$CRLF" if $mtime;
Branch: 1 branch
BranchTrueFalseTotal
if $mtime 0 0 0
582 0 print $self $CRLF;
583 }
584 0 ▶ $self->send_file(\*F) unless $self->head_request;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->head_request 0 0 0
585 0 return RC_OK;
586 }
587 else {
588 0 $self->send_error(RC_NOT_FOUND);
589 }
590 }
591  
592 0 ▶sub send_dir {
Subroutine send_dir: not called CC=2 SCAR=17.9
593 0 my ($self, $dir) = @_;
594 0 ▶ $self->send_error(RC_NOT_FOUND) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
595 0 $self->send_error(RC_NOT_IMPLEMENTED);
596 }
597  
598 5 ▶sub send_file {
Subroutine send_file: called CC=8 SCAR=21.3
599 5 my ($self, $file) = @_;
600 5 my $opened = 0;
601 5 ▶ if (!ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file) 5 0 5
602 5 ▶ open(my $fh, '<', $file) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return undef 2 3 5
603 0 ▶ binmode($fh) || do { close($fh); return undef };
Branch: 1 branch
BranchTrueFalseTotal
binmode $fh or do { close $fh; return undef } 2 0 2
604 2 $file = $fh;
605 2 $opened++;
606 }
607 2 my $cnt = 0;
608 2 my $buf = "";
609 2 my $n;
610 2 ▶ while ($n = sysread($file, $buf, 8 * 1024)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n = sysread($file, $buf, 8192)) 1 2 3
611 1 ▶ last if !$n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 1 1
612 1 $cnt += $n;
613 1 print $self $buf;
614 }
615 2 ▶ close($file) if $opened;
Branch: 1 branch
BranchTrueFalseTotal
if $opened 2 0 2
616  
617 # Return a "true zero" for empty-but-successful copies so callers
618 # using `send_file or die` can distinguish open failure (undef)
619 # from a successful zero-byte transfer.
620 2 ▶ $cnt || '0E0';
Branch: 1 branch
BranchTrueFalseTotal
$cnt or "0E0" 1 1 2
621 }
622  
623 0 ▶sub daemon {
Subroutine daemon: not called CC=1 SCAR=6.9
624 0 my $self = shift;
625 0 ${*$self}{'httpd_daemon'};
626 }
627  
628  
629 2 1;
630  
631 __END__
↑ Summary