blib/lib/HTTP/Daemon.pm

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