lib/Chj/HTTP/Daemon.pm

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