blib/lib/HTTP/Daemon.pm

filter: Statementstmt 18.0% 72 / 400 Branchbran 2.8% 6 / 212 Conditioncond 5.4% 3 / 55 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 46.1% 18 / 39 Podpod 100% 4 / 4 totaltotal 13.9% 103 / 740 SCAR 93.9
CC
132
Cov
12%
get_request
3306.0
send_response
306.0
send_file_response
90.0
CRAP 11947.8
↑ Summary Build »
1 package HTTP::Daemon; # git description: v6.14-30-gd7c9267
2  
3 # ABSTRACT: A simple http server class
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  
8 1 our $VERSION = '6.15';
9  
10 1 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use IO::Socket::IP;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 our @ISA = qw(IO::Socket::IP);
13  
14 1 our $PROTO = "HTTP/1.1";
15  
16 1 our $DEBUG;
17  
18 sub new {
19 1 ▶ my ($class, %args) = @_;
Subroutine new: called CC=7 SCAR=25.0
Pod documented
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 sub accept {
34 0 ▶ my $self = shift;
Subroutine accept: not called CC=4 SCAR=30.0
Pod documented
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 sub url {
47 1 ▶ my $self = shift;
Subroutine url: called CC=5 SCAR=16.6
Pod documented
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 sub _default_port {
63 1 ▶ 80;
Subroutine _default_port: called CC=1 SCAR=0.0
64 }
65  
66 sub _default_scheme {
67 1 ▶ "http";
Subroutine _default_scheme: called CC=1 SCAR=0.0
68 }
69  
70 sub product_tokens {
71 0 ▶ "libwww-perl-daemon/$HTTP::Daemon::VERSION";
Subroutine product_tokens: not called CC=1 SCAR=6.9
Pod documented
72 }
73  
74 package # hide from PAUSE
75 HTTP::Daemon::ClientConn;
76  
77 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
79  
80 1 ▶use IO::Socket::IP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
81 1 our @ISA = qw(IO::Socket::IP);
82 1 our $DEBUG;
83 1 *DEBUG = \$HTTP::Daemon::DEBUG;
84  
85 1 ▶use HTTP::Request ();
Subroutine BEGIN: called CC=1 SCAR=0.0
86 1 ▶use HTTP::Response ();
Subroutine BEGIN: called CC=1 SCAR=0.0
87 1 ▶use HTTP::Status;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 1 ▶use HTTP::Date qw(time2str);
Subroutine BEGIN: called CC=1 SCAR=0.0
89 1 ▶use LWP::MediaTypes qw(guess_media_type);
Subroutine BEGIN: called CC=1 SCAR=0.0
90 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
91  
92 # "\r\n" is not portable
93 1 my $CRLF = "\015\012";
94 1 my $HTTP_1_0 = _http_version("HTTP/1.0");
95 1 my $HTTP_1_1 = _http_version("HTTP/1.1");
96  
97  
98 sub get_request {
99 0 ▶ my ($self, $only_headers) = @_;
Subroutine get_request: not called CC=57 SCAR=81.0
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 0 my @vals = map {my $str = $_; $str =~ s/^\s+//; $str =~ s/\s+$//; $str }
298 split ',', $ct_len;
299 # check that they are all numbers (RFC: Content-Length = 1*DIGIT)
300 0 my @nums = grep { /^[0-9]+$/} @vals;
301 0 ▶ unless (@vals == @nums) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@vals == @nums) 0 0 0
302 0 my $reason = "Content-Length value must be an unsigned integer";
303 0 $self->send_error(400, $reason);
304 0 $self->reason($reason);
305 0 return;
306 }
307 # check they are all the same
308 0 my $ct_len = shift @nums;
309 0 foreach (@nums) {
310 0 ▶ next if $_ == $ct_len;
Branch: 1 branch
BranchTrueFalseTotal
if $_ == $ct_len 0 0 0
311 0 my $reason = "Content-Length values are not the same";
312 0 $self->send_error(400, $reason);
313 0 $self->reason($reason);
314 0 return;
315 }
316 # ensure we have now a fixed header, with only 1 value
317 0 $r->header('Content-Length' => $ct_len);
318  
319 # Plain body specified by "Content-Length"
320 0 my $missing = $ct_len - length($buf);
321 0 ▶ while ($missing > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($missing > 0) 0 0 0
322 0 ▶ print "Need $missing more bytes of content\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
323 0 my $n = $self->_need_more($buf, $timeout, $fdset);
324 0 ▶ return unless $n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
325 0 $missing -= $n;
326 }
327 0 ▶ if (length($buf) > $ct_len) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $buf > $ct_len) { } 0 0 0
328 0 $r->content(substr($buf, 0, $ct_len));
329 0 substr($buf, 0, $ct_len) = '';
330 }
331 else {
332 0 $r->content($buf);
333 0 $buf = '';
334 }
335 }
336 ▶ elsif ($ct_type && $ct_type =~ m/^multipart\/\w+\s*;.*boundary\s*=\s*("?)(\w+)\1/i) {
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
337  
338 # Handle multipart content type
339 0 my $boundary = "$CRLF--$2--";
340 0 my $index;
341 0 while (1) {
342 0 $index = index($buf, $boundary);
343 0 ▶ last if $index >= 0;
Branch: 1 branch
BranchTrueFalseTotal
if $index >= 0 0 0 0
344  
345 # end marker not yet found
346 0 ▶ return unless $self->_need_more($buf, $timeout, $fdset);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_need_more($buf, $timeout, $fdset) 0 0 0
347 }
348 0 $index += length($boundary);
349 0 $r->content(substr($buf, 0, $index));
350 0 substr($buf, 0, $index) = '';
351  
352 }
353 0 ${*$self}{'httpd_rbuf'} = $buf;
354  
355 0 $r;
356 }
357  
358 sub _need_more {
359 0 ▶ my $self = shift;
Subroutine _need_more: not called CC=8 SCAR=42.8
360  
361 #my($buf,$timeout,$fdset) = @_;
362 0 ▶ if ($_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1]) 0 0 0
363 0 my ($timeout, $fdset) = @_[1, 2];
364 0 ▶ print STDERR "select(,,,$timeout)\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
365 0 my $n = select($fdset, undef, undef, $timeout);
366 0 ▶ unless ($n) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n) 0 0 0
367 0 ▶ $self->reason(defined($n) ? "Timeout" : "select: $!");
Branch: 1 branch
BranchTrueFalseTotal
defined $n ? : 0 0 0
368 0 return;
369 }
370 }
371 0 ▶ print STDERR "sysread()\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
372 0 my $n = sysread($self, $_[0], 2048, length($_[0]));
373 0 ▶ $self->reason(defined($n) ? "Client closed" : "sysread: $!") unless $n;
Branch: 2 branches
BranchTrueFalseTotal
unless $n 0 0 0
defined $n ? : 0 0 0
374 0 $n;
375 }
376  
377 sub read_buffer {
378 0 ▶ my $self = shift;
Subroutine read_buffer: not called CC=2 SCAR=17.9
379 0 my $old = ${*$self}{'httpd_rbuf'};
380 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
381 0 ${*$self}{'httpd_rbuf'} = shift;
382 }
383 0 $old;
384 }
385  
386 sub reason {
387 0 ▶ my $self = shift;
Subroutine reason: not called CC=2 SCAR=17.9
388 0 my $old = ${*$self}{'httpd_reason'};
389 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
390 0 ${*$self}{'httpd_reason'} = shift;
391 }
392 0 $old;
393 }
394  
395 sub proto_ge {
396 0 ▶ my $self = shift;
Subroutine proto_ge: not called CC=1 SCAR=6.9
397 0 ${*$self}{'httpd_client_proto'} >= _http_version(shift);
398 }
399  
400 sub _http_version {
401 2 ▶ local ($_) = shift;
Subroutine _http_version: called CC=2 SCAR=7.1
402 2 ▶ return 0 unless m,^(?:HTTP/)?(\d+)\.(\d+)$,i;
Branch: 1 branch
BranchTrueFalseTotal
unless m[^(?:HTTP/)?(\d+)\.(\d+)$]i 0 2 2
403 2 $1 * 1000 + $2;
404 }
405  
406 sub antique_client {
407 0 ▶ my $self = shift;
Subroutine antique_client: not called CC=1 SCAR=6.9
408 0 ${*$self}{'httpd_client_proto'} < $HTTP_1_0;
409 }
410  
411 sub force_last_request {
412 0 ▶ my $self = shift;
Subroutine force_last_request: not called CC=1 SCAR=6.9
413 0 ${*$self}{'httpd_nomore'}++;
414 }
415  
416 sub head_request {
417 0 ▶ my $self = shift;
Subroutine head_request: not called CC=1 SCAR=6.9
418 0 ${*$self}{'httpd_head'};
419 }
420  
421  
422 sub send_status_line {
423 0 ▶ my ($self, $status, $message, $proto) = @_;
Subroutine send_status_line: not called CC=7 SCAR=40.3
424 0 ▶ return if $self->antique_client;
Branch: 1 branch
BranchTrueFalseTotal
if $self->antique_client 0 0 0
425 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
426 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
427 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
428 0 print $self "$proto $status $message$CRLF";
429 }
430  
431 sub send_crlf {
432 0 ▶ my $self = shift;
Subroutine send_crlf: not called CC=1 SCAR=6.9
433 0 print $self $CRLF;
434 }
435  
436 sub send_basic_header {
437 0 ▶ my $self = shift;
Subroutine send_basic_header: not called CC=3 SCAR=24.8
438 0 ▶ return if $self->antique_client;
Branch: 1 branch
BranchTrueFalseTotal
if $self->antique_client 0 0 0
439 0 $self->send_status_line(@_);
440 0 print $self "Date: ", time2str(time), $CRLF;
441 0 my $product = $self->daemon->product_tokens;
442 0 ▶ print $self "Server: $product$CRLF" if $product;
Branch: 1 branch
BranchTrueFalseTotal
if $product 0 0 0
443 }
444  
445 sub send_header {
446 0 ▶ my $self = shift;
Subroutine send_header: not called CC=3 SCAR=24.8
447 0 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
448 0 my ($k, $v) = splice(@_, 0, 2);
449 0 ▶ $v = "" unless defined($v);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
450 0 print $self "$k: $v$CRLF";
451 }
452 }
453  
454 sub send_response {
455 0 ▶ my $self = shift;
Subroutine send_response: not called CC=17 SCAR=57.2
456 0 my $res = shift;
457 0 ▶ if (!ref $res) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $res) 0 0 0
458 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
459 0 $res = HTTP::Response->new($res, @_);
460 }
461 0 my $content = $res->content;
462 0 my $chunked;
463 0 ▶ unless ($self->antique_client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->antique_client) 0 0 0
464 0 my $code = $res->code;
465 0 $self->send_basic_header($code, $res->message, $res->protocol);
466 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
467  
468 # make sure content is empty
469 0 $res->remove_header("Content-Length");
470 0 $content = "";
471 }
472 ▶ 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
473  
474 # probably OK
475 }
476 elsif (ref($content) eq "CODE") {
477 0 ▶ if ($self->proto_ge("HTTP/1.1")) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->proto_ge('HTTP/1.1')) { } 0 0 0
478 0 $res->push_header("Transfer-Encoding" => "chunked");
479 0 $chunked++;
480 }
481 else {
482 0 $self->force_last_request;
483 }
484 }
485 elsif (length($content)) {
486 0 $res->header("Content-Length" => length($content));
487 }
488 else {
489 0 $self->force_last_request;
490 0 $res->header('connection', 'close');
491 }
492 0 print $self $res->headers_as_string($CRLF);
493 0 print $self $CRLF; # separates headers and content
494 }
495 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
496  
497 # no content
498 }
499 elsif (ref($content) eq "CODE") {
500 0 while (1) {
501 0 my $chunk = &$content();
502 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
503 0 ▶ if ($chunked) {
Branch: 1 branch
BranchTrueFalseTotal
if ($chunked) { } 0 0 0
504 0 printf $self "%x%s%s%s", length($chunk), $CRLF, $chunk, $CRLF;
505 }
506 else {
507 0 print $self $chunk;
508 }
509 }
510 0 ▶ print $self "0$CRLF$CRLF" if $chunked; # no trailers either
Branch: 1 branch
BranchTrueFalseTotal
if $chunked 0 0 0
511 }
512 elsif (length $content) {
513 0 print $self $content;
514 }
515 }
516  
517 sub send_redirect {
518 0 ▶ my ($self, $loc, $status, $content) = @_;
Subroutine send_redirect: not called CC=8 SCAR=42.8
519 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
520 0 ▶ Carp::croak("Status '$status' is not redirect") unless is_redirect($status);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_redirect($status) 0 0 0
521 0 $self->send_basic_header($status);
522 0 my $base = $self->daemon->url;
523 0 ▶ $loc = $HTTP::URI_CLASS->new($loc, $base) unless ref($loc);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $loc 0 0 0
524 0 $loc = $loc->abs($base);
525 0 print $self "Location: $loc$CRLF";
526  
527 0 ▶ if ($content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content) 0 0 0
528 0 ▶ my $ct_type = $content =~ /^\s*</ ? "text/html" : "text/plain";
Branch: 1 branch
BranchTrueFalseTotal
$content =~ /^\s*</ ? : 0 0 0
529 0 print $self "Content-Type: $ct_type$CRLF";
530 }
531 0 print $self $CRLF;
532 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
533 0 $self->force_last_request; # no use keeping the connection open
534 }
535  
536 sub send_error {
537 0 ▶ my ($self, $status, $error) = @_;
Subroutine send_error: not called CC=6 SCAR=37.4
538 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
539 0 ▶ Carp::croak("Status '$status' is not an error") unless is_error($status);
Branch: 1 branch
BranchTrueFalseTotal
unless &is_error($status) 0 0 0
540 0 my $mess = status_message($status);
541 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
542 0 $mess = <<EOT;
543 <title>$status $mess</title>
544 <h1>$status $mess</h1>
545 $error
546 EOT
547 0 ▶ unless ($self->antique_client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->antique_client) 0 0 0
548 0 $self->send_basic_header($status);
549 0 print $self "Content-Type: text/html$CRLF";
550 0 print $self "Content-Length: " . length($mess) . $CRLF;
551 0 print $self $CRLF;
552 }
553 0 ▶ print $self $mess unless $self->head_request;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->head_request 0 0 0
554 0 $status;
555 }
556  
557 sub send_file_response {
558 0 ▶ my ($self, $file) = @_;
Subroutine send_file_response: not called CC=9 SCAR=45.0
559 0 ▶ if (-d $file) {
Branch: 2 branches
BranchTrueFalseTotal
if (-d $file) { } 0 0 0
elsif (-f _) { } 0 0 0
560 0 $self->send_dir($file);
561 }
562 elsif (-f _) {
563  
564 # plain file
565 0 local (*F);
566 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
567 0 binmode(F);
568 0 my ($mime_type, $file_enc) = guess_media_type($file);
569 0 my ($size, $mtime) = (stat _)[7, 9];
570 0 ▶ unless ($self->antique_client) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->antique_client) 0 0 0
571 0 $self->send_basic_header;
572 0 print $self "Content-Type: $mime_type$CRLF";
573 0 ▶ print $self "Content-Encoding: $file_enc$CRLF" if $file_enc;
Branch: 1 branch
BranchTrueFalseTotal
if $file_enc 0 0 0
574 0 ▶ print $self "Content-Length: $size$CRLF" if $size;
Branch: 1 branch
BranchTrueFalseTotal
if $size 0 0 0
575 0 ▶ print $self "Last-Modified: ", time2str($mtime), "$CRLF" if $mtime;
Branch: 1 branch
BranchTrueFalseTotal
if $mtime 0 0 0
576 0 print $self $CRLF;
577 }
578 0 ▶ $self->send_file(\*F) unless $self->head_request;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->head_request 0 0 0
579 0 return RC_OK;
580 }
581 else {
582 0 $self->send_error(RC_NOT_FOUND);
583 }
584 }
585  
586 sub send_dir {
587 0 ▶ my ($self, $dir) = @_;
Subroutine send_dir: not called CC=2 SCAR=17.9
588 0 ▶ $self->send_error(RC_NOT_FOUND) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
589 0 $self->send_error(RC_NOT_IMPLEMENTED);
590 }
591  
592 sub send_file {
593 0 ▶ my ($self, $file) = @_;
Subroutine send_file: not called CC=6 SCAR=37.4
594 0 my $opened = 0;
595 0 local (*FILE);
596 0 ▶ if (!ref($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file) 0 0 0
597 0 ▶ open(FILE, $file) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
open FILE, $file or return undef 0 0 0
598 0 binmode(FILE);
599 0 $file = \*FILE;
600 0 $opened++;
601 }
602 0 my $cnt = 0;
603 0 my $buf = "";
604 0 my $n;
605 0 ▶ while ($n = sysread($file, $buf, 8 * 1024)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n = sysread($file, $buf, 8192)) 0 0 0
606 0 ▶ last if !$n;
Branch: 1 branch
BranchTrueFalseTotal
unless $n 0 0 0
607 0 $cnt += $n;
608 0 print $self $buf;
609 }
610 0 ▶ close($file) if $opened;
Branch: 1 branch
BranchTrueFalseTotal
if $opened 0 0 0
611 0 $cnt;
612 }
613  
614 sub daemon {
615 0 ▶ my $self = shift;
Subroutine daemon: not called CC=1 SCAR=6.9
616 0 ${*$self}{'httpd_daemon'};
617 }
618  
619  
620 1 1;
621  
622 __END__
↑ Summary Build »