Gepok.pm

filter: Statementstmt 22.8% 84 / 368 Branchbran 0.0% 0 / 144 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 22 Subroutinesub 35.0% 20 / 57 Podpod 87.5% 7 / 8 totaltotal 17.3% 111 / 641 CC 97 SCAR 50.5
CC
97
Cov
15%
_after_init
420.0
_finalize_response
420.0
BUILD
238.7
CRAP 155.5
↑ Summary Plack/Handler/Gepok.pm »
1 package Gepok;
2  
3 2 our $DATE = '2019-09-11'; # DATE
4 2 our $VERSION = '0.292'; # VERSION
5  
6 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use HTTP::Daemon::Patch::IPv6;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use HTTP::Daemon::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use HTTP::Daemon::UNIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use HTTP::Date qw(time2str);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use HTTP::Status qw(status_message);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use IO::Scalar;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 2 ▶use IO::Socket qw(:crlf);
Subroutine BEGIN: called CC=1 SCAR=0.0
20 2 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 2 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 2 ▶use Proc::Daemon::Prefork;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Time::HiRes qw(gettimeofday);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 2 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 has name => (is => 'rw',
29 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
30 0 my $name = $0;
31 0 $name =~ s!.*/!!;
32 0 $name;
33 2 });
34 0 ▶has daemonize => (is => 'rw', default=>sub{1});
Subroutine __ANON__: not called CC=1 SCAR=1.2
35 #has sock_path => (is => 'rw');
36 2 has pid_path => (is => 'rw');
37 2 has scoreboard_path => (is => 'rw');
38 2 has error_log_path => (is => 'rw');
39 2 has access_log_path => (is => 'rw');
40 0 ▶has http_ports => (is => 'rw', default => sub{[]});
Subroutine __ANON__: not called CC=1 SCAR=1.2
41 0 ▶has https_ports => (is => 'rw', default => sub{[]});
Subroutine __ANON__: not called CC=1 SCAR=1.2
42 0 ▶has unix_sockets => (is => 'rw', default => sub{[]});
Subroutine __ANON__: not called CC=1 SCAR=1.2
43 0 ▶has timeout => (is => 'rw', default => sub{120});
Subroutine __ANON__: not called CC=1 SCAR=1.2
44 0 ▶has require_root => (is => 'rw', default => sub{0});
Subroutine __ANON__: not called CC=1 SCAR=1.2
45 2 has ssl_key_file => (is => 'rw');
46 2 has ssl_cert_file => (is => 'rw');
47 0 ▶has start_servers => (is => 'rw', default => sub{3});
Subroutine __ANON__: not called CC=1 SCAR=1.2
48 0 ▶has max_clients => (is => 'rw', default=>sub{150});
Subroutine __ANON__: not called CC=1 SCAR=1.2
49 0 ▶has max_requests_per_child => (is => 'rw', default=>sub{1000});
Subroutine __ANON__: not called CC=1 SCAR=1.2
50 2 has _daemon => (is => 'rw'); # Proc::Daemon::Prefork
51 2 has _server_socks => (is => 'rw'); # store server sockets
52 2 has _app => (is => 'rw'); # store PSGI app
53 2 has product_name => (is => 'rw');
54 2 has product_version => (is => 'rw');
55  
56 2 has "ssl_$_" => (is => 'rw') for (qw(verify_mode verify_callback ca_path ca_file));
57  
58 0 ▶sub BUILD {
Subroutine BUILD: not called CC=16 SCAR=54.8
Pod undocumented
59 0 my ($self) = @_;
60  
61 0 ▶ my $is_root = $> ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$> ? : 0 0 0
62 0 ▶ my $log_dir = $is_root ? "/var/log" : File::HomeDir->my_home;
Branch: 1 branch
BranchTrueFalseTotal
$is_root ? : 0 0 0
63 0 ▶ my $run_dir = $is_root ? "/var/run" : File::HomeDir->my_home;
Branch: 1 branch
BranchTrueFalseTotal
$is_root ? : 0 0 0
64  
65 0 ▶ unless ($self->error_log_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->error_log_path) 0 0 0
66 0 $self->error_log_path($log_dir."/".$self->name."-error.log");
67 }
68 0 ▶ unless ($self->access_log_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->access_log_path) 0 0 0
69 0 $self->access_log_path($log_dir."/".$self->name."-access.log");
70 }
71 0 ▶ unless ($self->pid_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->pid_path) 0 0 0
72 0 $self->pid_path($run_dir."/".$self->name.".pid");
73 }
74 0 ▶ unless ($self->scoreboard_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->scoreboard_path) 0 0 0
75 0 $self->scoreboard_path($run_dir."/".$self->name.".scoreboard");
76 }
77 0 ▶ unless ($self->product_name) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->product_name) 0 0 0
78 0 $self->product_name(ref($self));
79 }
80 0 ▶ unless (defined $self->product_version) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->product_version) 0 0 0
81 2 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 0 ▶ $self->product_version($Gepok::VERSION // "?");
Condition0%0/2
$VERSION // "?"
l!l
00
MC/DC0%0/1
$VERSION // "?"
$VERSION
Truth table0%0/2
A
A= $VERSION
Aresult
11
00
83 }
84 0 ▶ if (defined (my $vc = $self->ssl_verify_callback)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $vc = $self->ssl_verify_callback)) 0 0 0
85 0 ▶ $self->ssl_verify_callback($vc = sub { 0 }) if $vc eq '0';
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $vc eq 0 0 0 0
86 0 ▶ $self->ssl_verify_callback($vc = sub { 1 }) if $vc eq '1';
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if $vc eq 1 0 0 0
87 0 ▶ die "ssl_verify_callback needs to be a coderef, or constant '1' or '0'" unless ref $vc eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $vc eq "CODE" 0 0 0
88 }
89 0 ▶ unless ($self->_daemon) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_daemon) 0 0 0
90 0 my $already_init;
91 my $daemon = Proc::Daemon::Prefork->new(
92 name => $self->name,
93 error_log_path => $self->error_log_path,
94 access_log_path => $self->access_log_path,
95 pid_path => $self->pid_path,
96 scoreboard_path => $self->scoreboard_path,
97 daemonize => $self->daemonize,
98 prefork => $self->start_servers,
99 max_children => $self->max_clients,
100 0 ▶ before_daemonize => sub { $self->_after_init; $already_init++; 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 ▶ after_init => sub { $self->_after_init unless $already_init },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $already_init 0 0 0
102 0 ▶ main_loop => sub { $self->_main_loop },
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 0 require_root => $self->require_root,
104 # currently auto reloading is turned off
105 );
106 0 $self->_daemon($daemon);
107 }
108 }
109  
110 0 ▶sub run {
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
111 0 my ($self, $app) = @_;
112 0 $self->_app($app);
113 0 $self->_daemon->run;
114 }
115  
116 # alias for run()
117 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
118 0 my $self = shift;
119 0 $self->run(@_);
120 }
121  
122 0 ▶sub stop {
Subroutine stop: not called CC=1 SCAR=6.9
Pod documented
123 0 my ($self) = @_;
124 0 $self->_daemon->kill_running;
125 }
126  
127 0 ▶sub restart {
Subroutine restart: not called CC=1 SCAR=6.9
Pod documented
128 0 my ($self) = @_;
129 0 $self->_daemon->kill_running;
130 0 $self->_daemon->run;
131 }
132  
133 0 ▶sub is_running {
Subroutine is_running: not called CC=2 SCAR=17.9
Pod documented
134 0 my ($self) = @_;
135 0 my $pid = $self->_daemon->check_pidfile;
136 0 ▶ $pid ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$pid ? : 0 0 0
137 }
138  
139 0 ▶sub _after_init {
Subroutine _after_init: not called CC=20 SCAR=60.4
140 0 my ($self) = @_;
141  
142 0 my @server_socks;
143 my @server_sock_infos;
144 0 my $ary;
145  
146 0 $ary = $self->unix_sockets;
147 0 ▶ if (defined($ary) && ref($ary) ne 'ARRAY') { $ary = [split /\s*,\s*/,$ary] }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ary and ref $ary ne "ARRAY") 0 0 0
Condition0%0/3
defined $ary and ref $ary ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ary and ref $ary ne "ARRAY"
defined $ary ref $ary ne "ARRAY"
Truth table0%0/3
A and B
A= defined $aryB= ref $ary ne "ARRAY"
ABresult
0X0
100
111
148 0 for my $path (@$ary) {
149 0 my %args;
150 0 $args{Timeout} = $self->timeout;
151 0 $args{Local} = $path;
152 0 log_info("Binding to Unix socket %s (http) ...", $path);
153 0 my $sock = HTTP::Daemon::UNIX->new(%args);
154 0 ▶ die "Unable to bind to Unix socket $path: $@" unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
155 0 push @server_socks, $sock;
156 0 push @server_sock_infos, "$path (unix)";
157 }
158  
159 0 $ary = $self->http_ports;
160 0 ▶ if (defined($ary) && ref($ary) ne 'ARRAY') { $ary = [split /\s*,\s*/,$ary] }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ary and ref $ary ne "ARRAY") 0 0 0
Condition0%0/3
defined $ary and ref $ary ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ary and ref $ary ne "ARRAY"
defined $ary ref $ary ne "ARRAY"
Truth table0%0/3
A and B
A= defined $aryB= ref $ary ne "ARRAY"
ABresult
0X0
100
111
161 0 for my $port (@$ary) {
162 0 my %args;
163 0 $args{Reuse} = 1;
164 0 $args{Timeout} = $self->timeout;
165 0 ▶ if ($port =~ /^(?:0\.0\.0\.0|\*)?:?(\d+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($port =~ /^(?:0\.0\.0\.0|\*)?:?(\d+)$/) { } 0 0 0
elsif ($port =~ /^([^:]+):(\d+)$/) { } 0 0 0
166 0 $args{LocalPort} = $1;
167 } elsif ($port =~ /^([^:]+):(\d+)$/) {
168 0 $args{LocalHost} = $1;
169 0 $args{LocalPort} = $2;
170 } else {
171 0 die "Invalid http_port syntax `$port`, please specify ".
172 ":N or 1.2.3.4:N";
173 }
174 0 log_info("Binding to TCP socket %s (http) ...", $port);
175 0 my $sock = HTTP::Daemon->new(%args);
176 0 ▶ die "Unable to bind to TCP socket $port" unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
177 0 push @server_socks, $sock;
178 0 push @server_sock_infos, "$port (tcp)";
179 }
180  
181 0 $ary = $self->https_ports;
182 0 ▶ if (defined($ary) && ref($ary) ne 'ARRAY') { $ary = [split /\s*,\s*/,$ary] }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $ary and ref $ary ne "ARRAY") 0 0 0
Condition0%0/3
defined $ary and ref $ary ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ary and ref $ary ne "ARRAY"
defined $ary ref $ary ne "ARRAY"
Truth table0%0/3
A and B
A= defined $aryB= ref $ary ne "ARRAY"
ABresult
0X0
100
111
183 0 for my $port (@$ary) {
184 0 my %args;
185 0 $args{Reuse} = 1;
186  
187 # temporarily not passing Timeout param because this does make
188 # request/packet larger than 2k hangs
189 #$args{Timeout} = $self->timeout; # can hang with larger POST?
190  
191 0 $args{SSL_key_file} = $self->ssl_key_file;
192 0 $args{SSL_cert_file} = $self->ssl_cert_file;
193  
194 0 for (qw(verify_mode verify_callback ca_path ca_file)) {
195 0 my $meth = "ssl_$_";
196 0 my $val = $self->$meth;
197 0 ▶ $args{"SSL_$_"} = $val if defined $val;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val 0 0 0
198 }
199  
200 0 ▶ if ($port =~ /^(?:0\.0\.0\.0)?:?(\d+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($port =~ /^(?:0\.0\.0\.0)?:?(\d+)$/) { } 0 0 0
elsif ($port =~ /^(\d+\.\d+\.\d+\.\d+):(\d+)$/) { } 0 0 0
201 0 $args{LocalPort} = $1;
202 } elsif ($port =~ /^(\d+\.\d+\.\d+\.\d+):(\d+)$/) {
203 0 $args{LocalHost} = $1;
204 0 $args{LocalPort} = $2;
205 } else {
206 0 die "Invalid http_port syntax `$port`, please specify ".
207 ":N or 1.2.3.4:N";
208 }
209  
210 0 log_info("Binding to TCP socket %s (https) ...", $port);
211 0 my $sock = HTTP::Daemon::SSL->new(%args);
212 0 ▶ die "Unable to bind to TCP socket $port, common cause include ".
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
213 "port taken or missing server key/cert file" unless $sock;
214 0 push @server_socks, $sock;
215 0 push @server_sock_infos, "$port (tcp, https)";
216 }
217  
218 0 ▶ die "Please specify at least one HTTP/HTTPS/Unix socket port"
Branch: 1 branch
BranchTrueFalseTotal
unless @server_socks 0 0 0
219 unless @server_socks;
220  
221 0 $self->_server_socks(\@server_socks);
222 0 warn "Will be binding to ".join(", ", @server_sock_infos)."\n";
223 0 $self->before_prefork();
224  
225 0 1;
226 }
227  
228 0 ▶sub before_prefork {}
Subroutine before_prefork: not called CC=1 SCAR=0.0
Pod documented
229  
230 0 ▶sub _main_loop {
Subroutine _main_loop: not called CC=7 SCAR=40.3
231 0 my ($self) = @_;
232 0 ▶ if ($self->_daemon->{parent_pid} == $$) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_daemon->{'parent_pid'} == $$) { } 0 0 0
233 0 log_info("Entering main loop");
234 } else {
235 0 log_info("Child process started (PID $$)");
236 }
237 0 $self->_daemon->update_scoreboard({child_start_time=>time()});
238  
239 0 my $sel = IO::Select->new(@{ $self->_server_socks });
240  
241 0 ▶ for (my $i=1; $i<=$self->max_requests_per_child; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $self->max_requests_per_child) 0 0 0
242 0 $self->_daemon->set_label("listening");
243 0 my @ready = $sel->can_read();
244 0 for my $s (@ready) {
245 0 my $sock = $s->accept();
246 # sock can be undef
247 0 ▶ next unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
248 0 $self->{_connect_time} = [gettimeofday];
249 0 $self->_set_label_serving($sock);
250 0 while (1) {
251 0 $self->_daemon->update_scoreboard({
252 req_start_time => time(),
253 num_reqs => $i,
254 state => "R",
255 });
256 0 $self->{_start_req_time} = [gettimeofday];
257 0 my $req = $sock->get_request;
258 0 $self->{_finish_req_time} = [gettimeofday];
259 0 ▶ last unless $req;
Branch: 1 branch
BranchTrueFalseTotal
unless $req 0 0 0
260 $self->{_client_proto} =
261 0 ▶ $sock->proto_ge("1.1") ? "HTTP/1.1" : "HTTP/1.0";
Branch: 1 branch
BranchTrueFalseTotal
$sock->proto_ge('1.1') ? : 0 0 0
262 0 $self->_daemon->update_scoreboard({state => "W"});
263 0 my $res = $self->_handle_psgi($req, $sock);
264 0 $self->access_log($req, $res, $sock);
265 }
266 0 $self->_daemon->update_scoreboard({state => "_"});
267 }
268 }
269 }
270  
271 # taken from Starman, with modifications. turn PSGI response into actual HTTP
272 # response and send it to client.
273 0 ▶sub _finalize_response {
Subroutine _finalize_response: not called CC=20 SCAR=60.4
274 0 my($self, $env, $res, $sock) = @_;
275  
276 0 ▶ if ($env->{'psgix.harakiri.commit'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"psgix.harakiri.commit"}) 0 0 0
277 0 $self->{_client_keepalive} = 0;
278 0 $self->{_client_harakiri} = 1;
279 }
280  
281 0 my $server_proto = $env->{SERVER_PROTOCOL};
282 0 my $client_proto = $self->{_client_proto};
283 0 my $status = $res->[0];
284 0 my $message = status_message($status);
285 0 $self->{_res_status} = $status;
286  
287 # generate HTTP status + response headers
288  
289 0 my(@headers, %headers);
290 0 push @headers, "$server_proto $status $message";
291 0 push @headers, "Server: ".
292 $self->product_name."/".$self->product_version;
293 0 ▶ while (my ($k, $v) = splice @{$res->[1]}, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@{$$res[1];}, 0, 2)) 0 0 0
294 0 push @headers, "$k: $v";
295 0 $headers{lc $k} = $v;
296 }
297  
298 0 ▶ if (!$headers{date}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($headers{"date"}) 0 0 0
299 0 push @headers, "Date: " . time2str(time());
300 }
301  
302 0 my $keepalive;
303 0 ▶ if ($env->{HTTP_CONNECTION}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"HTTP_CONNECTION"}) 0 0 0
304 0 ▶ $keepalive = $env->{HTTP_CONNECTION} =~ /alive/i ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$env->{'HTTP_CONNECTION'} =~ /alive/i ? : 0 0 0
305 }
306 # default is keep-alive for HTTP/1.1, but close for HTTP/1.0
307 0 ▶ $keepalive //= ($client_proto eq 'HTTP/1.1' ? 1:0);
Branch: 1 branch
BranchTrueFalseTotal
$client_proto eq 'HTTP/1.1' ? : 0 0 0
Condition0%0/2
$keepalive //= $client_proto eq "HTTP/1.1" ? 1 : 0
l!l
00
MC/DC0%0/1
$keepalive //= $client_proto eq "HTTP/1.1" ? 1 : 0
$keepalive
Truth table0%0/2
A
A= $keepalive
Aresult
11
00
308 # normally HTTP::Daemon prints this, but we're not sending response using
309 # HTTP::Daemon
310 0 ▶ push @headers, "Connection: ".($keepalive ? "Keep-Alive" : "Close");
Branch: 1 branch
BranchTrueFalseTotal
$keepalive ? : 0 0 0
311  
312 0 my $chunked;
313 0 my $cl = $headers{'content-length'};
314 0 ▶ if ($client_proto eq 'HTTP/1.1') {
Branch: 1 branch
BranchTrueFalseTotal
if ($client_proto eq 'HTTP/1.1') { } 0 0 0
315 0 ▶ if ($status =~ /^[123]/ && $status != 304 && (!defined($cl) || $cl)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status =~ /^[123]/ and $status != 304 and !defined($cl) || $cl) 0 0 0
Condition0%0/9
$status =~ /^[123]/ and $status != 304 and !defined($cl) || $cl
!ll&&!rl&&r
000
$status =~ /^[123]/ and $status != 304
!ll&&!rl&&r
000
not defined $cl or $cl
l!l&&r!l&&!r
000
MC/DC0%0/4
$status =~ /^[123]/ and $status != 304 and !defined($cl) || $cl
$status =~ /^[123]/ $status != 304 not defined $cl $cl
Truth table0%0/5
A and B and not(C) or D
A= $status =~ /^[123]/B= $status != 304C= not defined $clD= $cl
ABCDresult
0XXX0
10XX0
11000
111X1
11011
316 0 $chunked = 1;
317 }
318 0 ▶ if (my $te = $headers{'transfer-encoding'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $te = $headers{"transfer-encoding"}) 0 0 0
319 0 $chunked = $te eq 'chunked';
320 }
321 } else {
322 # "A server MUST NOT send transfer-codings to an HTTP/1.0 client."
323 0 $chunked = 0;
324 }
325 0 ▶ push @headers, "Transfer-Encoding: chunked" if $chunked;
Branch: 1 branch
BranchTrueFalseTotal
if $chunked 0 0 0
326 0 $self->{_chunked} = $chunked;
327  
328 #warn "chunked=$chunked, keep-alive=$keepalive, client_proto=$client_proto";
329  
330 0 ▶ if ($client_proto le 'HTTP/1.0' && $keepalive && !defined($cl)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client_proto le 'HTTP/1.0' and $keepalive and not defined $cl) { } 0 0 0
Condition0%0/6
$client_proto le "HTTP/1.0" and $keepalive and not defined $cl
!ll&&!rl&&r
000
$client_proto le "HTTP/1.0" and $keepalive
!ll&&!rl&&r
000
MC/DC0%0/3
$client_proto le "HTTP/1.0" and $keepalive and not defined $cl
$client_proto le "HTTP/1.0" $keepalive not defined $cl
Truth table0%0/4
A and B and not(C)
A= $client_proto le "HTTP/1.0"B= $keepaliveC= not defined $cl
ABCresult
0XX0
10X0
1100
1111
331 # if HTTP/1.0 client requests keep-alive (like Wget), we need
332 # Content-Length so client knows when response ends.
333  
334 # produce body first so we can calculate content-length
335 0 $self->_finalize_body($env, $res, $sock, 1);
336 0 push @headers, "Content-Length: ".$self->{_res_content_length};
337 0 syswrite $sock, join($CRLF, @headers, '') . $CRLF; # print header
338 0 syswrite $sock, $_ for @{$self->{_body}}; # print body
339 } else {
340 # print headers + body normally
341  
342 0 syswrite $sock, join($CRLF, @headers, '') . $CRLF; # print header
343 0 $self->_finalize_body($env, $res, $sock);
344 }
345 }
346  
347 # either print body to $sock, or store it in $self-> (for HTTP/1.0 Keep-Alive
348 # clients)
349 0 ▶sub _finalize_body {
Subroutine _finalize_body: not called CC=4 SCAR=30.0
350 0 my ($self, $env, $res, $sock, $save) = @_;
351 0 my $cl = 0;
352 0 ▶ $self->{_body} = [] if $save;
Branch: 1 branch
BranchTrueFalseTotal
if $save 0 0 0
353 0 ▶ if (defined $res->[2]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $res->[2]) { } 0 0 0
354 Plack::Util::foreach(
355 $res->[2],
356 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
357 0 my $buffer = $_[0];
358 0 my $len = length $buffer;
359 0 $cl += $len;
360 0 ▶ if ($self->{_chunked}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_chunked"}) 0 0 0
361 0 ▶ return unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 0 0
362 0 $buffer = sprintf("%x", $len) . $CRLF . $buffer . $CRLF;
363 }
364 0 $self->_write_sock($sock, $save, $buffer);
365 0 });
366 0 ▶ $self->_write_sock($sock, $save, "0$CRLF$CRLF") if $self->{_chunked};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_chunked"} 0 0 0
367 } else {
368 return Plack::Util::inline_object(
369 0 ▶ write => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
370 0 my $buffer = $_[0];
371 0 my $len = length $buffer;
372 0 $cl += $len;
373 0 ▶ if ($self->{_chunked}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_chunked"}) 0 0 0
374 0 ▶ return unless $len;
Branch: 1 branch
BranchTrueFalseTotal
unless $len 0 0 0
375 0 $buffer = sprintf("%x", $len) . $CRLF . $buffer . $CRLF;
376 }
377 0 $self->_write_sock($sock, $save, $buffer);
378 },
379 # poll_cb => sub { ... },
380 0 ▶ close => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
381 $self->_write_sock($sock, $save, "0$CRLF$CRLF")
382 0 ▶ if $self->{_chunked};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_chunked"} 0 0 0
383 }
384 0 );
385 }
386 0 $self->{_res_content_length} = $cl;
387 }
388  
389 0 ▶sub _write_sock {
Subroutine _write_sock: not called CC=4 SCAR=30.0
390 0 my ($self, $sock, $save, $buffer) = @_;
391 0 ▶ if ($save) {
Branch: 1 branch
BranchTrueFalseTotal
if ($save) { } 0 0 0
392 0 push @{$self->{_body}}, $buffer;
393 } else {
394 # large $buffer might need to be written in several steps, especially in
395 # SSL sockets which might have smaller buffer size (like 16k)
396 0 my $tot_written = 0;
397 0 while (1) {
398 0 my $written = syswrite $sock, $buffer, length($buffer)-$tot_written,
399 $tot_written;
400 # XXX what to do on error, i.e. $written is undef?
401 0 ▶ $tot_written += $written // 0;
Condition0%0/2
$written // 0
l!l
00
MC/DC0%0/1
$written // 0
$written
Truth table0%0/2
A
A= $written
Aresult
11
00
402 0 ▶ last unless $tot_written < length($buffer);
Branch: 1 branch
BranchTrueFalseTotal
unless $tot_written < length $buffer 0 0 0
403 }
404 }
405 }
406  
407 # run PSGI app, send PSGI response to client as HTTP response, and return it
408 0 ▶sub _handle_psgi {
Subroutine _handle_psgi: not called CC=2 SCAR=17.9
409 0 my ($self, $req, $sock) = @_;
410  
411 0 my $env = $self->_prepare_env($req, $sock);
412  
413 # cache first before socket closes
414 0 $self->{_sock_peerhost} = $env->{REMOTE_ADDR};
415  
416 0 my $res = Plack::Util::run_app($self->_app, $env);
417  
418 # trap i/o error when sending response
419 0 eval {
420 0 ▶ if (ref($res) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq 'CODE') { } 0 0 0
421 0 ▶ $res->(sub { $self->_finalize_response($env, $_[0], $sock) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
422 } else {
423 0 $self->_finalize_response($env, $res, $sock);
424 }
425 };
426  
427 0 $res;
428 }
429  
430 # prepare PSGI env
431 0 ▶sub _prepare_env {
Subroutine _prepare_env: not called CC=10 SCAR=47.0
432 0 my ($self, $req, $sock) = @_;
433  
434 0 my $httpd = ${*$sock}{httpd_daemon};
435 0 my $is_unix = $httpd->isa('HTTP::Daemon::UNIX');
436 0 my $is_ssl = $httpd->isa('HTTP::Daemon::SSL');
437 0 my $uri = $req->uri->as_string;
438 0 my ($qs, $pi);
439 0 ▶ if ($uri =~ /(.*)\?(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($uri =~ /(.*)\?(.*)/) { } 0 0 0
440 0 $pi = $1;
441 0 $qs = $2;
442 } else {
443 0 $pi = $uri;
444 0 $qs = "";
445 }
446 0 $pi = uri_unescape($pi);
447  
448 #warn "uri=$uri, qs=$qs\n";
449 my $env = {
450 REQUEST_METHOD => $req->method,
451 SCRIPT_NAME => '',
452 PATH_INFO => $pi,
453 REQUEST_URI => $uri,
454 QUERY_STRING => $qs,
455 SERVER_PORT => $is_unix ? 0 : $httpd->sockport,
456 SERVER_NAME => $is_unix ? $httpd->hostpath : $httpd->sockhost,
457 SERVER_PROTOCOL => 'HTTP/1.1',
458 REMOTE_ADDR => $is_unix ? '127.0.0.1' : $sock->peerhost,
459  
460 'psgi.version' => [ 1, 1 ],
461 'psgi.input' => IO::Scalar->new(\($req->{_content})),
462 'psgi.errors' => *STDERR,
463 'psgi.url_scheme' => $is_ssl ? 'https' : 'http',
464 'psgi.run_once' => Plack::Util::FALSE,
465 'psgi.multithread' => Plack::Util::FALSE,
466 'psgi.multiprocess' => Plack::Util::TRUE,
467 'psgi.streaming' => Plack::Util::TRUE,
468 'psgi.nonblocking' => Plack::Util::FALSE,
469 'psgix.input.buffered' => Plack::Util::TRUE,
470 'psgix.io' => $sock,
471 'psgix.input.buffered' => Plack::Util::TRUE,
472 'psgix.harakiri' => Plack::Util::TRUE,
473  
474 # additional/server-specific
475 'gepok' => 1,
476 'gepok.connect_time' => $self->{_connect_time},
477 'gepok.start_request_time' => $self->{_start_req_time},
478 'gepok.finish_request_time' => $self->{_finish_req_time},
479 'gepok.client_protocol' => $self->{_client_proto},
480 0 ▶ 'gepok.socket' => $sock,
Branch: 4 branches
BranchTrueFalseTotal
$is_unix ? : 0 0 0
$is_unix ? : 0 0 0
$is_unix ? : 0 0 0
$is_ssl ? : 0 0 0
481 'gepok.httpd_socket' => $httpd,
482 };
483 0 ▶ $env->{HTTPS} = 'on' if $is_ssl;
Branch: 1 branch
BranchTrueFalseTotal
if $is_ssl 0 0 0
484 0 ▶ if ($is_unix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_unix) { } 0 0 0
485 0 $env->{'gepok.unix_socket'} = 1;
486 } else {
487 #
488 }
489  
490 # HTTP_ vars
491 0 my $rh = $req->headers;
492 0 for my $hn ($rh->header_field_names) {
493 0 my $key = uc($hn); $key =~ s/[^A-Z0-9]/_/g;
494 0 ▶ $key = "HTTP_$key" unless $key =~ /\A(?:CONTENT_(?:TYPE|LENGTH))\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /\A(?:CONTENT_(?:TYPE|LENGTH))\z/ 0 0 0
495 0 $env->{$key} = join(", ", $rh->header($hn));
496 }
497  
498 0 $env;
499 }
500  
501 0 ▶sub _set_label_serving {
Subroutine _set_label_serving: not called CC=6 SCAR=37.4
502 0 my ($self, $sock) = @_;
503 # sock can be undef when client timed out
504 0 ▶ return unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
505  
506 0 my $httpd = ${*$sock}{httpd_daemon};
507 0 my $is_unix = $httpd->isa('HTTP::Daemon::UNIX');
508  
509 0 ▶ if ($is_unix) {
Branch: 1 branch
BranchTrueFalseTotal
if ($is_unix) { } 0 0 0
510 0 my $sock_path = $httpd->hostpath;
511 0 my ($pid, $uid, $gid) = $httpd->peercred;
512 0 log_trace("Unix socket info: path=$sock_path, ".
513 "pid=$pid, uid=$uid, gid=$gid");
514 0 $self->_daemon->set_label("serving unix (pid=$pid, uid=$uid, ".
515 "path=$sock_path)");
516 } else {
517 0 ▶ my $is_ssl = $httpd->isa('HTTP::Daemon::SSL') ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$httpd->isa('HTTP::Daemon::SSL') ? : 0 0 0
518 0 my $server_port = $sock->sockport;
519 0 ▶ my $remote_ip = $sock->peerhost // "127.0.0.1";
Condition0%0/2
$sock->peerhost // "127.0.0.1"
l!l
00
MC/DC0%0/1
$sock->peerhost // "127.0.0.1"
$sock->peerhost
Truth table0%0/2
A
A= $sock->peerhost
Aresult
11
00
520 0 my $remote_port = $sock->peerport;
521 0 ▶ if (log_is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if (log_is_trace()) 0 0 0
522 0 log_trace(join("",
523 "TCP socket info: https=$is_ssl, ",
524 "server_port=$server_port, ",
525 "remote_ip=$remote_ip, ",
526 "remote_port=$remote_port"));
527 }
528 0 my $num_clients = keys %{$self->_daemon->{children}};
529 0 my $max_clients = $self->max_clients;
530 0 $self->_daemon->set_label(
531 sprintf(
532 "%d/%d serving TCP :%d (https=%s, remote=%s:%s",
533 $num_clients, $max_clients,
534 $server_port,
535 $is_ssl,
536 $remote_ip, $remote_port,
537 ),
538 );
539 }
540 }
541  
542 0 ▶sub __escape {
Subroutine __escape: not called CC=1 SCAR=6.9
543 0 my $s = shift;
544 0 $s =~ s/\n/\\n/g;
545 0 $s;
546 }
547  
548 0 ▶sub __escape_quote {
Subroutine __escape_quote: not called CC=1 SCAR=6.9
549 0 my $s = shift;
550 0 $s =~ s/\n/\\n/g;
551 0 $s =~ s/"/\\"/g;
552 0 $s;
553 }
554  
555 0 ▶sub access_log {
Subroutine access_log: not called CC=10 SCAR=47.0
Pod documented
556 0 my ($self, $req, $sock) = @_;
557 0 ▶ return unless $self->access_log_path;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->access_log_path 0 0 0
558  
559 0 my $reqh = $req->headers;
560 0 ▶ if (log_is_trace) {
Branch: 1 branch
BranchTrueFalseTotal
if (log_is_trace()) 0 0 0
561 log_trace("\$self->{_sock_peerhost}=%s, (gmtime(\$self->{_finish_req_time}))[0]=%s, \$req->method=%s, \$req->uri->as_string=%s, \$self->{_res_status}=%s, \$self->{res_content_length}=%s, ".
562 "\$reqh->header('referer')=%s, \$reqh->header('user-agent')=%s",
563 $self->{_sock_peerhost},
564 (gmtime($self->{_finish_req_time}))[0],
565 $req->method,
566 $req->uri->as_string,
567 $self->{_res_status},
568 $self->{_res_content_length},
569 0 scalar($reqh->header("referer")),
570 scalar($reqh->header("user-agent")),
571 );
572 }
573 my $logline = sprintf(
574 "%s - %s [%s] \"%s %s\" %d %s \"%s\" \"%s\"\n",
575 $self->{_sock_peerhost} // "-",
576 "-", # XXX auth user
577 POSIX::strftime("%d/%b/%Y:%H:%M:%S +0000",
578 gmtime($self->{_finish_req_time}[0])),
579 $req->method,
580 __escape_quote($req->uri->as_string),
581 $self->{_res_status} // 0,
582 0 ▶ $self->{_res_content_length} // "-",
Condition0%0/10
$self->{"_sock_peerhost"} // "-"
l!l
00
$self->{"_res_status"} // 0
l!l
00
$self->{"_res_content_length"} // "-"
l!l
00
scalar $reqh->header("referer") // "-"
l!l
00
scalar $reqh->header("user-agent") // "-"
l!l
00
MC/DC0%0/1
$self->{"_sock_peerhost"} // "-"
$self->{"_sock_peerhost"}
MC/DC0%0/1
$self->{"_res_status"} // 0
$self->{"_res_status"}
MC/DC0%0/1
$self->{"_res_content_length"} // "-"
$self->{"_res_content_length"}
MC/DC0%0/1
scalar $reqh->header("referer") // "-"
scalar $reqh->header("referer")
MC/DC0%0/1
scalar $reqh->header("user-agent") // "-"
scalar $reqh->header("user-agent")
Truth table0%0/2
A
A= $self->{"_sock_peerhost"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"_res_status"}
Aresult
11
00
Truth table0%0/2
A
A= $self->{"_res_content_length"}
Aresult
11
00
Truth table0%0/2
A
A= scalar $reqh->header("referer")
Aresult
11
00
Truth table0%0/2
A
A= scalar $reqh->header("user-agent")
Aresult
11
00
583 scalar($reqh->header("referer")) // "-",
584 scalar($reqh->header("user-agent")) // "-",
585 );
586 #$log->tracef("logline=%s", $logline);
587 0 ▶ if ($self->daemonize) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->daemonize) { } 0 0 0
elsif (not defined $ENV{'PLACK_ENV'}) { } 0 0 0
588 0 syswrite($self->_daemon->{_access_log}, $logline);
589 } elsif (!defined($ENV{PLACK_ENV})) {
590 0 warn $logline;
591 }
592 }
593  
594 2 1;
595  
596 # ABSTRACT: PSGI server with built-in HTTPS support, Unix sockets, preforking
597  
598 __END__
↑ Summary Plack/Handler/Gepok.pm »