Perlbal/Test/WebServer.pm

filter: Statementstmt 97.1% 168 / 173 Branchbran 79.3% 73 / 92 Conditioncond 66.6% 28 / 42 MC/DCmcdc 40.0% 10 / 25 Subroutinesub 100% 14 / 14 Podpod 0.0% 0 / 3 totaltotal 83.9% 293 / 349 CC 65 SCAR 31.5
CC
65
Cov
88%
serve_client
36.6
__ANON__
19.1
start_webserver
15.6
CRAP 23.3
« Perlbal/Plugin/LazyCDN.pm ↑ Summary Perlbal/Test/WebClient.pm »
1 #!/usr/bin/perl
2  
3 package Perlbal::Test::WebServer;
4  
5 58 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 58 ▶use IO::Socket::INET;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 58 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 58 ▶use Socket qw(MSG_NOSIGNAL IPPROTO_TCP TCP_NODELAY SOL_SOCKET);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 58 ▶use Perlbal::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 58 ▶use Perlbal::Test::WebClient;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 58 require Exporter;
14 58 ▶use vars qw(@ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
15 58 @ISA = qw(Exporter);
16 58 @EXPORT = qw(start_webserver);
17  
18 58 our @webserver_pids;
19  
20 58 my $testpid; # of the test suite's main program, the one running the HTTP client
21  
22 58 ▶END {
Subroutine END: called CC=3 SCAR=11.0
23 # ensure we kill off the webserver
24 58 ▶ kill 9, @webserver_pids if $testpid && $testpid == $$;
Branch: 1 branch
BranchTrueFalseTotal
if $testpid and $testpid == $$ 9 49 58
Condition100%3/3
$testpid and $testpid == $$
!ll&&!rl&&r
4459
MC/DC100%2/2
$testpid and $testpid == $$
$testpid $testpid == $$
Truth table100%3/3
A and B
A= $testpidB= $testpid == $$
ABresult
0X0
100
111
25 }
26  
27  
28 81 ▶sub start_webserver {
Subroutine start_webserver: called CC=12 SCAR=27.5
Pod undocumented
29 81 my $port = new_port();
30  
31 # dummy mode
32 81 ▶ if ($ENV{'TEST_PERLBAL_USE_EXISTING'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"TEST_PERLBAL_USE_EXISTING"}) 0 81 81
33 0 return $port;
34 }
35  
36 81 $testpid = $$;
37  
38 81 ▶ if (my $child = fork) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $child = fork) 45 36 81
39 # i am parent, wait for child to startup
40 45 push @webserver_pids, $child;
41 45 my $sock = wait_on_child($child, $port);
42 45 ▶ die "Unable to spawn webserver on port $port\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 45 45
43 unless $sock;
44 45 print $sock "GET /reqdecr,status HTTP/1.0\r\n\r\n";
45 45 my $line = <$sock>;
46 45 ▶ die "Didn't get 200 OK: " . (defined $line ? $line : "(undef)")
Branch: 2 branches
BranchTrueFalseTotal
unless $line and $line =~ /200 OK/ 0 45 45
defined $line ? : 0 0 0
Condition33%1/3
$line and $line =~ /200 OK/
!ll&&!rl&&r
0045
MC/DC0%0/2
$line and $line =~ /200 OK/
$line $line =~ /200 OK/
Truth table33%1/3
A and B
A= $lineB= $line =~ /200 OK/
ABresult
0X0
100
111
47 unless $line && $line =~ /200 OK/;
48 45 return $port;
49 }
50  
51 # i am child, start up
52 36 ▶ my $ssock = IO::Socket::INET->new(LocalPort => $port, ReuseAddr => 1, Listen => 3)
Branch: 1 branch
BranchTrueFalseTotal
my $ssock = "IO::Socket::INET"->new("LocalPort", $port, "ReuseAddr", 1, "Listen", 3) or die "Unable to start socket: $!\n" 36 0 36
53 or die "Unable to start socket: $!\n";
54 36 ▶ while (my $csock = $ssock->accept) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $csock = $ssock->accept) 83 0 83
55 83 ▶ exit 0 unless $csock;
Branch: 1 branch
BranchTrueFalseTotal
unless $csock 0 83 83
56 83 ▶ fork and next; # parent starts waiting for next request
Branch: 1 branch
BranchTrueFalseTotal
fork and next 47 36 83
57 36 ▶ setsockopt($csock, IPPROTO_TCP, TCP_NODELAY, pack("l", 1)) or die;
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $csock, 6, 1, "\1\0\0\0" or die 36 0 36
58 36 serve_client($csock);
59 }
60 }
61  
62 36 ▶sub serve_client {
Subroutine serve_client: called CC=35 SCAR=36.0
Pod undocumented
63 36 my $csock = shift;
64 36 my $req_num = 0;
65 36 my $did_options = 0;
66 36 my @reqs;
67  
68 REQ:
69 36 while (1) {
70 166 my $req = '';
71 166 my $clen = undef;
72 166 ▶ while (<$csock>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $csock)) 748 6 754
73 748 $req .= $_;
74 748 ▶ if (/^content-length:\s*(\d+)/i) { $clen = $1; };
Branch: 1 branch
BranchTrueFalseTotal
if (/^content-length:\s*(\d+)/i) 86 662 748
75 748 ▶ last if ! $_ || /^\r?\n/;
Branch: 1 branch
BranchTrueFalseTotal
if not $_ or /^\r?\n/ 160 588 748
Condition66%2/3
not $_ or /^\r?\n/
l!l&&r!l&&!r
0160588
MC/DC50%1/2
not $_ or /^\r?\n/
not $_ /^\r?\n/
Truth table66%2/3
not(A) or B
A= not $_B= /^\r?\n/
ABresult
1X1
011
000
76 }
77 166 ▶ exit 0 unless $req;
Branch: 1 branch
BranchTrueFalseTotal
unless $req 6 160 166
78  
79 # parse out things we want to have
80 160 my @cmds;
81 160 my $httpver = 0; # 0 = 1.0, 1 = 1.1, undef = neither
82 160 my $method;
83 160 ▶ if ($req =~ m!^([A-Z]+) /?(\S+) HTTP/(1\.\d+)\r?\n?!) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req =~ m[^([A-Z]+) /?(\S+) HTTP/(1\.\d+)\r?\n?]) 159 1 160
84 159 $method = $1;
85 159 my $cmds = durl($2);
86 159 @cmds = split(/\s*,\s*/, $cmds);
87 159 $req_num++;
88 159 ▶ $httpver = ($3 eq '1.0' ? 0 : ($3 eq '1.1' ? 1 : undef));
Branch: 2 branches
BranchTrueFalseTotal
$3 eq '1.0' ? : 159 0 159
$3 eq '1.1' ? : 0 0 0
89 }
90 160 my $msg = HTTP::Request->parse($req);
91 160 my $keeping_alive = undef;
92  
93 160 my $body;
94 160 ▶ if ($clen) {
Branch: 1 branch
BranchTrueFalseTotal
if ($clen) 85 75 160
95 85 ▶ die "Can't read a body on a GET or HEAD" if $method =~ /^GET|HEAD$/;
Branch: 1 branch
BranchTrueFalseTotal
if $method =~ /^GET|HEAD$/ 0 85 85
96 85 my $read = read $csock, $body, $clen;
97 85 ▶ die "Didn't read $clen bytes. Got $read." if $clen != $read;
Branch: 1 branch
BranchTrueFalseTotal
if $clen != $read 0 85 85
98 }
99  
100 160 ▶ my $response = sub {
Subroutine __ANON__: called CC=17 SCAR=29.5
101 160 my %opts = @_;
102 160 my $code = delete $opts{code};
103 160 my $codetext = delete $opts{codetext};
104 160 my $content = delete $opts{content};
105 160 my $ctype = delete $opts{type};
106 160 my $extra_hdr = delete $opts{headers};
107 160 ▶ die "unknown data in opts: %opts" if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 160 160
108  
109 160 ▶ $extra_hdr ||= '';
Condition100%2/2
$extra_hdr ||= ""
l!l
27133
MC/DC100%1/1
$extra_hdr ||= ""
$extra_hdr
Truth table100%2/2
A
A= $extra_hdr
Aresult
11
00
110 160 ▶ $code ||= $content ? 200 : 200;
Branch: 1 branch
BranchTrueFalseTotal
$content ? : 131 15 146
Condition66%2/3
$code ||= $content ? 200 : 200
l!l&&r!l&&!r
141460
MC/DC0%0/2
$code ||= $content ? 200 : 200
$code $content ? 200 : 200
Truth table66%2/3
A ||= B
A= $codeB= $content ? 200 : 200
ABresult
1X1
011
000
111 160 ▶ $codetext ||= { 200 => 'OK', 500 => 'Internal Server Error', 204 => "No Content" }->{$code};
Condition33%1/3
$codetext ||= {200, "OK", 500, "Internal Server Error", 204, "No Content"}->{$code}
l!l&&r!l&&!r
01600
MC/DC0%0/2
$codetext ||= {200, "OK", 500, "Internal Server Error", 204, "No Content"}->{$code}
$codetext {200, "OK", 500, "Internal Server Error", 204, "No Content"}->{$code}
Truth table33%1/3
A ||= B
A= $codetextB= {200, "OK", 500, "Internal Server Error", 204, "No Content"}->{$code}
ABresult
1X1
011
000
112 160 ▶ $content ||= "";
Condition100%2/2
$content ||= ""
l!l
13129
MC/DC100%1/1
$content ||= ""
$content
Truth table100%2/2
A
A= $content
Aresult
11
00
113  
114 160 my $clen = length $content;
115 160 ▶ $ctype ||= "text/plain" unless $code == 204;
Branch: 1 branch
BranchTrueFalseTotal
unless $code == 204 148 12 160
Condition50%1/2
$ctype ||= "text/plain"
l!l
0148
MC/DC0%0/1
$ctype ||= "text/plain"
$ctype
Truth table50%1/2
A
A= $ctype
Aresult
11
00
116 160 ▶ $extra_hdr .= "Content-Type: $ctype\r\n" if $ctype;
Branch: 1 branch
BranchTrueFalseTotal
if $ctype 148 12 160
117  
118 160 my $hdr_keepalive = "";
119  
120 160 ▶ unless (defined $keeping_alive) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $keeping_alive) 158 2 160
121 158 ▶ my $hdr_connection = $msg->header('Connection') || '';
Condition100%2/2
$msg->header('Connection') || ''
l!l
14315
MC/DC100%1/1
$msg->header('Connection') || ''
$msg->header('Connection')
Truth table100%2/2
A
A= $msg->header('Connection')
Aresult
11
00
122 158 ▶ if ($httpver == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($httpver == 1) 0 158 158
123 0 ▶ if ($hdr_connection =~ /\bclose\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hdr_connection =~ /\bclose\b/i) { } 0 0 0
124 0 $keeping_alive = 0;
125 } else {
126 0 $keeping_alive = "1.1implicit";
127 }
128 }
129 158 ▶ if ($httpver == 0 && $hdr_connection =~ /\bkeep-alive\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($httpver == 0 and $hdr_connection =~ /\bkeep-alive\b/i) 130 28 158
Condition66%2/3
$httpver == 0 and $hdr_connection =~ /\bkeep-alive\b/i
!ll&&!rl&&r
028130
MC/DC50%1/2
$httpver == 0 and $hdr_connection =~ /\bkeep-alive\b/i
$httpver == 0 $hdr_connection =~ /\bkeep-alive\b/i
Truth table66%2/3
A and B
A= $httpver == 0B= $hdr_connection =~ /\bkeep-alive\b/i
ABresult
0X0
100
111
130 130 $keeping_alive = "1.0keepalive";
131 }
132 }
133  
134 160 ▶ if ($keeping_alive) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keeping_alive) { } 130 30 160
135 130 $hdr_keepalive = "Connection: keep-alive\n";
136 } else {
137 30 $hdr_keepalive = "Connection: close\n";
138 }
139  
140 160 return "HTTP/1.0 $code $codetext\r\n" .
141 $hdr_keepalive .
142 "Content-Length: $clen\r\n" .
143 $extra_hdr .
144 "\r\n" .
145 "$content";
146 160 };
147  
148 160 ▶ my $send = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
149 160 my $res = shift;
150 160 print $csock $res;
151 160 ▶ exit 0 unless $keeping_alive;
Branch: 1 branch
BranchTrueFalseTotal
unless $keeping_alive 30 130 160
152 160 };
153  
154 # 500 if no commands were given or we don't know their HTTP version
155 # or we didn't parse a proper HTTP request
156 160 ▶ unless (@cmds && defined $httpver && $msg) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@cmds and defined $httpver and $msg) 1 159 160
Condition66%4/6
@cmds and defined $httpver and $msg
!ll&&!rl&&r
10159
@cmds and defined $httpver
!ll&&!rl&&r
10159
MC/DC33%1/3
@cmds and defined $httpver and $msg
@cmds defined $httpver $msg
Truth table50%2/4
A and B and C
A= @cmdsB= defined $httpverC= $msg
ABCresult
0XX0
10X0
1100
1111
157 1 print STDERR "500 response!\n";
158 1 $send->($response->(code => 500));
159 1 next REQ;
160 }
161  
162 159 ▶ if ($method eq "OPTIONS") {
Branch: 1 branch
BranchTrueFalseTotal
if ($method eq "OPTIONS") 1 158 159
163 1 $did_options = 1;
164 1 $send->($response->(code => 200));
165 1 next REQ;
166 }
167  
168 # prepare a simple 200 to send; undef this if you want to control
169 # your own output below
170 158 my $to_send;
171  
172 158 foreach my $cmd (@cmds) {
173 180 $cmd =~ s/^\s+//;
174 180 $cmd =~ s/\s+$//;
175  
176 180 ▶ if ($cmd =~ /^sleep:([\d\.]+)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^sleep:([\d\.]+)$/i) 4 176 180
177 4 my $sleeptime = $1;
178 #print "I, $$, should sleep for $sleeptime.\n";
179 58 ▶ use Time::HiRes;
Subroutine BEGIN: called CC=1 SCAR=0.0
180 4 my $t1 = Time::HiRes::time();
181 4 select undef, undef, undef, $1;
182 4 my $t2 = Time::HiRes::time();
183 4 my $td = $t2 - $t1;
184 #print "I, $$, slept for $td\n";
185 }
186  
187 180 ▶ if ($cmd =~ /^keepalive:([01])$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^keepalive:([01])$/i) 2 178 180
188 2 $keeping_alive = $1;
189 }
190  
191 180 ▶ if ($cmd eq "status") {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "status") 129 51 180
192 129 ▶ my $len = $clen || 0;
Condition100%2/2
$clen || 0
l!l
8544
MC/DC100%1/1
$clen || 0
$clen
Truth table100%2/2
A
A= $clen
Aresult
11
00
193 129 ▶ my $bu = $msg->header('X-PERLBAL-BUFFERED-UPLOAD-REASON') || '';
Condition100%2/2
$msg->header('X-PERLBAL-BUFFERED-UPLOAD-REASON') || ''
l!l
3126
MC/DC100%1/1
$msg->header('X-PERLBAL-BUFFERED-UPLOAD-REASON') || ''
$msg->header('X-PERLBAL-BUFFERED-UPLOAD-REASON')
Truth table100%2/2
A
A= $msg->header('X-PERLBAL-BUFFERED-UPLOAD-REASON')
Aresult
11
00
194 129 $to_send = $response->(content =>
195 "pid = $$\nreqnum = $req_num\nmethod = $method\n".
196 "length = $len\nbuffered = $bu\noptions = $did_options\n");
197 }
198  
199 180 ▶ if ($cmd eq "reqdecr") {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "reqdecr") 15 165 180
200 15 $req_num--;
201 }
202  
203 180 ▶ if ($cmd =~ /^kill:(\d+):(\w+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^kill:(\d+):(\w+)$/) 1 179 180
204 1 kill $2, $1;
205 }
206  
207 180 ▶ if ($cmd =~ /^reproxy_url:(.+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^reproxy_url:(.+)/i) 12 168 180
208 12 $to_send = $response->(headers => "X-Reproxy-URL: $1\r\n",
209 code => 204,
210 );
211 }
212  
213 180 ▶ if ($cmd =~ /^reproxy_url204:(.+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^reproxy_url204:(.+)/i) 2 178 180
214 2 $to_send = $response->(headers => "X-Reproxy-URL: $1\r\n");
215 }
216  
217 180 ▶ if ($cmd =~ /^reproxy_url_cached:(\d+):(.+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^reproxy_url_cached:(\d+):(.+)/i) 2 178 180
218 2 kill 'USR1', $testpid;
219 2 $to_send = $response->(headers =>
220 "X-Reproxy-URL: $2\r\nX-Reproxy-Cache-For: $1; Last-Modified Content-Type\r\nLast-Modified: 199\r\nContent-Type: application/badger\r\n");
221 }
222  
223 180 ▶ if ($cmd =~ /^reproxy_url_multi:((?:\d+:){2,})(\S+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^reproxy_url_multi:((?:\d+:){2,})(\S+)/i) 1 179 180
224 1 my $ports = $1;
225 1 my $path = $2;
226 1 my @urls;
227 1 foreach my $port (split(/:/, $ports)) {
228 2 push @urls, "http://127.0.0.1:$port$path";
229 }
230 1 $to_send = $response->(headers => "X-Reproxy-URL: @urls\r\n");
231 }
232  
233 180 ▶ if ($cmd =~ /^reproxy_file:(.+)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^reproxy_file:(.+)/i) 10 170 180
234 10 $to_send = $response->(headers => "X-Reproxy-File: $1\r\n");
235 }
236  
237 180 ▶ if ($cmd =~ /^subreq:(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^subreq:(\d+)$/) 1 179 180
238 1 my $port = $1;
239 1 my $wc = Perlbal::Test::WebClient->new;
240 1 $wc->server("127.0.0.1:$port");
241 1 $wc->keepalive(0);
242 1 $wc->http_version('1.0');
243 1 my $resp = $wc->request("status");
244 1 my $subpid;
245 1 ▶ if ($resp && $resp->content =~ /^pid = (\d+)$/m) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp and $resp->content =~ /^pid = (\d+)$/m) 1 0 1
Condition33%1/3
$resp and $resp->content =~ /^pid = (\d+)$/m
!ll&&!rl&&r
001
MC/DC0%0/2
$resp and $resp->content =~ /^pid = (\d+)$/m
$resp $resp->content =~ /^pid = (\d+)$/m
Truth table33%1/3
A and B
A= $respB= $resp->content =~ /^pid = (\d+)$/m
ABresult
0X0
100
111
246 1 $subpid = $1;
247 }
248 1 $to_send = $response->(content => "pid = $$\nsubpid = $subpid\nreqnum = $req_num\n");
249 }
250  
251 180 ▶ if ($cmd =~ /^reflect_request_headers$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /^reflect_request_headers$/) 1 179 180
252 1 $to_send = $response->(content => $msg->headers->as_string);
253 }
254 }
255  
256 158 ▶ $send->($to_send || $response->());
Condition33%1/3
$to_send || &$response()
l!l&&r!l&&!r
15800
MC/DC0%0/2
$to_send || &$response()
$to_send &$response()
Truth table33%1/3
A || B
A= $to_sendB= &$response()
ABresult
1X1
011
000
257 } # while(1)
258 }
259  
260 # de-url escape
261 159 ▶sub durl {
Subroutine durl: called CC=1 SCAR=0.1
Pod undocumented
262 159 my ($a) = @_;
263 159 $a =~ tr/+/ /;
264 0 $a =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
265 159 return $a;
266 }
267  
268 58 1;
« Perlbal/Plugin/LazyCDN.pm ↑ Summary Perlbal/Test/WebClient.pm »