Perlbal/Test/WebClient.pm

filter: Statementstmt 88.4% 107 / 121 Branchbran 68.9% 40 / 58 Conditioncond 52.0% 13 / 25 MC/DCmcdc 21.4% 3 / 14 Subroutinesub 93.3% 14 / 15 Podpod 0.0% 0 / 8 totaltotal 73.4% 177 / 241 CC 39 SCAR 31.1
CC
39
Cov
78%
request
38.1
__ANON__
6.6
host
6.0
CRAP 22.3
« Perlbal/Test/WebServer.pm ↑ Summary Perlbal/ClientManage.pm »
1 #!/usr/bin/perl
2  
3 package Perlbal::Test::WebClient;
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 Perlbal::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 58 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 58 ▶use Socket qw(MSG_NOSIGNAL IPPROTO_TCP TCP_NODELAY SOL_SOCKET);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 58 require Exporter;
12 58 ▶use vars qw(@ISA @EXPORT $FLAG_NOSIGNAL);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 58 @ISA = qw(Exporter);
14 58 @EXPORT = qw(new);
15  
16 58 $FLAG_NOSIGNAL = 0;
17 58 eval { $FLAG_NOSIGNAL = MSG_NOSIGNAL; };
18  
19 # create a blank object
20 13 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
21 13 my $class = shift;
22 13 my $self = {};
23 13 bless $self, $class;
24 13 return $self;
25 }
26  
27 # get/set what server we should be testing; "ip:port" generally
28 13 ▶sub server {
Subroutine server: called CC=2 SCAR=7.4
Pod undocumented
29 13 my $self = shift;
30 13 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 13 0 13
31 13 $self->{_sock} = undef;
32 13 return $self->{server} = shift;
33 } else {
34 0 return $self->{server};
35 }
36 }
37  
38 # get/set what hostname we send with requests
39 0 ▶sub host {
Subroutine host: not called CC=2 SCAR=17.9
Pod undocumented
40 0 my $self = shift;
41 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
42 0 $self->{_sock} = undef;
43 0 return $self->{host} = shift;
44 } else {
45 0 return $self->{host};
46 }
47 }
48  
49 # set which HTTP version to emulate; specify '1.0' or '1.1'
50 13 ▶sub http_version {
Subroutine http_version: called CC=2 SCAR=7.6
Pod undocumented
51 13 my $self = shift;
52 13 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 13 0 13
53 13 return $self->{http_version} = shift;
54 } else {
55 0 return $self->{http_version};
56 }
57 }
58  
59 # set on or off to enable or disable persistent connection
60 15 ▶sub keepalive {
Subroutine keepalive: called CC=3 SCAR=11.0
Pod undocumented
61 15 my $self = shift;
62 15 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 15 0 15
63 15 ▶ $self->{keepalive} = shift() ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
shift() ? : 11 4 15
64 }
65 15 return $self->{keepalive};
66 }
67  
68 # construct and send a request
69 150 ▶sub request {
Subroutine request: called CC=29 SCAR=36.4
Pod undocumented
70 150 my $self = shift;
71 150 ▶ return undef unless $self->{server};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"server"} 0 150 150
72  
73 150 ▶ my $opts = ref $_[0] eq "HASH" ? shift : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 107 43 150
74 150 my $opt_headers = delete $opts->{'headers'};
75 150 my $opt_host = delete $opts->{'host'};
76 150 my $opt_method = delete $opts->{'method'};
77 150 my $opt_content = delete $opts->{'content'};
78 150 my $opt_extra_rn = delete $opts->{'extra_rn'};
79 150 my $opt_return_reader = delete $opts->{'return_reader'};
80 150 my $opt_post_header_pause = delete $opts->{'post_header_pause'};
81 150 ▶ die "Bogus options: " . join(", ", keys %$opts) if %$opts;
Branch: 1 branch
BranchTrueFalseTotal
if %$opts 0 150 150
82  
83 150 my $cmds = join(',', map { eurl($_) } @_);
84 150 ▶ return undef unless $cmds;
Branch: 1 branch
BranchTrueFalseTotal
unless $cmds 0 150 150
85  
86 # keep-alive header if 1.0, also means add content-length header
87 150 my $headers = '';
88 150 ▶ if ($self->{keepalive}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'keepalive'}) { } 145 5 150
89 145 $headers .= "Connection: keep-alive\r\n";
90 } else {
91 5 $headers .= "Connection: close\r\n";
92 }
93  
94 150 ▶ if ($opt_headers) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_headers) 17 133 150
95 17 $headers .= $opt_headers;
96 }
97  
98 150 ▶ if (my $hostname = $opt_host || $self->{host}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $hostname = $opt_host || $self->{"host"}) 17 133 150
Condition66%2/3
$opt_host || $self->{'host'}
l!l&&r!l&&!r
170133
MC/DC50%1/2
$opt_host || $self->{'host'}
$opt_host $self->{'host'}
Truth table66%2/3
A || B
A= $opt_hostB= $self->{'host'}
ABresult
1X1
011
000
99 17 $headers .= "Host: $hostname\r\n";
100 }
101 150 ▶ my $method = $opt_method || "GET";
Condition100%2/2
$opt_method || 'GET'
l!l
7575
MC/DC100%1/1
$opt_method || 'GET'
$opt_method
Truth table100%2/2
A
A= $opt_method
Aresult
11
00
102 150 my $body = "";
103  
104 150 ▶ if ($opt_content) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_content) 74 76 150
105 74 $headers .= "Content-Length: " . length($opt_content) . "\r\n";
106 74 $body = $opt_content;
107 }
108  
109 150 ▶ if ($opt_extra_rn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_extra_rn) 36 114 150
110 36 $body .= "\r\n"; # some browsers on POST send an extra \r\n that's not part of content-length
111 }
112  
113 150 my $send = "$method /$cmds HTTP/$self->{http_version}\r\n$headers\r\n";
114  
115 150 ▶ unless ($opt_post_header_pause) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opt_post_header_pause) 114 36 150
116 114 $send .= $body;
117 }
118  
119 150 my $len = length $send;
120  
121 # send setup
122 150 my $rv;
123 150 my $sock = delete $self->{_sock};
124 150 ▶ local $SIG{'PIPE'} = "IGNORE" unless $FLAG_NOSIGNAL;
Branch: 1 branch
BranchTrueFalseTotal
unless $Perlbal::Test::WebClient::FLAG_NOSIGNAL 0 150 150
125  
126 ### send it cached
127 150 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 133 17 150
128 133 $rv = send($sock, $send, $FLAG_NOSIGNAL);
129 133 ▶ if ($! || ! defined $rv) {
Branch: 2 branches
BranchTrueFalseTotal
if ($! or not defined $rv) { } 0 133 133
elsif ($rv != $len) { } 0 133 133
Condition33%1/3
$! or not defined $rv
l!l&&r!l&&!r
00133
MC/DC0%0/2
$! or not defined $rv
$! not defined $rv
Truth table33%1/3
A or not(B)
A= $!B= not defined $rv
ABresult
1X1
011
000
130 0 undef $self->{_sock};
131 } elsif ($rv != $len) {
132 0 return undef;
133 }
134 }
135  
136 # failing that, send it through a new socket
137 150 ▶ unless ($rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 17 133 150
138 17 $self->{_reqdone} = 0;
139  
140 $sock = IO::Socket::INET->new(
141 PeerAddr => $self->{server},
142 17 ▶ Timeout => 3,
Branch: 1 branch
BranchTrueFalseTotal
$sock = "IO::Socket::INET"->new("PeerAddr", $self->{"server"}, "Timeout", 3) or return undef 17 0 17
143 ) or return undef;
144 17 ▶ setsockopt($sock, IPPROTO_TCP, TCP_NODELAY, pack("l", 1)) or die "failed to set sockopt: $!\n";
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $sock, 6, 1, "\1\0\0\0" or die "failed to set sockopt: $!\n" 17 0 17
145  
146 17 $rv = send($sock, $send, $FLAG_NOSIGNAL);
147 17 ▶ if ($! || $rv != $len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($! or $rv != $len) 0 17 17
Condition33%1/3
$! or $rv != $len
l!l&&r!l&&!r
0017
MC/DC0%0/2
$! or $rv != $len
$! $rv != $len
Truth table33%1/3
A or B
A= $!B= $rv != $len
ABresult
1X1
011
000
148 0 return undef;
149 }
150 }
151  
152 150 ▶ if ($opt_post_header_pause) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_post_header_pause) 36 114 150
153 36 select undef, undef, undef, $opt_post_header_pause;
154 36 my $len = length $body;
155 36 ▶ if ($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 36 0 36
156 36 my $rv = send($sock, $body, $FLAG_NOSIGNAL);
157 36 ▶ if ($! || ! defined $rv) {
Branch: 2 branches
BranchTrueFalseTotal
if ($! or not defined $rv) { } 0 36 36
elsif ($rv != $len) { } 0 36 36
Condition33%1/3
$! or not defined $rv
l!l&&r!l&&!r
0036
MC/DC0%0/2
$! or not defined $rv
$! not defined $rv
Truth table33%1/3
A or not(B)
A= $!B= not defined $rv
ABresult
1X1
011
000
158 0 undef $self->{_sock};
159 } elsif ($rv != $len) {
160 0 return undef;
161 }
162 }
163 }
164  
165 150 ▶ my $parse_it = sub {
Subroutine __ANON__: called CC=6 SCAR=18.8
166 150 my ($resp, $firstline) = resp_from_sock($sock);
167  
168 150 ▶ my $conhdr = $resp->header("Connection") || "";
Condition50%1/2
$resp->header('Connection') || ''
l!l
1500
MC/DC0%0/1
$resp->header('Connection') || ''
$resp->header('Connection')
Truth table50%1/2
A
A= $resp->header('Connection')
Aresult
11
00
169 150 ▶ if (($firstline =~ m!\bHTTP/1\.1\b! && $conhdr !~ m!\bclose\b!i) ||
Branch: 1 branch
BranchTrueFalseTotal
if ($firstline =~ m[\bHTTP/1\.1\b] and not $conhdr =~ /\bclose\b/i or $firstline =~ m[\bHTTP/1\.0\b] and $conhdr =~ /\bkeep-alive\b/i) { } 142 8 150
Condition55%5/9
$firstline =~ m[\bHTTP/1\.1\b] and not $conhdr =~ /\bclose\b/i or $firstline =~ m[\bHTTP/1\.0\b] and $conhdr =~ /\bkeep-alive\b/i
l!l&&r!l&&!r
01428
$firstline =~ m[\bHTTP/1\.1\b] and not $conhdr =~ /\bclose\b/i
!ll&&!rl&&r
15000
$firstline =~ m[\bHTTP/1\.0\b] and $conhdr =~ /\bkeep-alive\b/i
!ll&&!rl&&r
08142
MC/DC25%1/4
$firstline =~ m[\bHTTP/1\.1\b] and not $conhdr =~ /\bclose\b/i or $firstline =~ m[\bHTTP/1\.0\b] and $conhdr =~ /\bkeep-alive\b/i
$firstline =~ m[\bHTTP/1\.1\b] not $conhdr =~ /\bclose\b/i $firstline =~ m[\bHTTP/1\.0\b] $conhdr =~ /\bkeep-alive\b/i
Truth table28%2/7
A and not(B) or C and D
A= $firstline =~ m[\bHTTP/1\.1\b]B= not $conhdr =~ /\bclose\b/iC= $firstline =~ m[\bHTTP/1\.0\b]D= $conhdr =~ /\bkeep-alive\b/i
ABCDresult
11XX1
0X111
10111
0X0X0
0X100
100X0
10100
170 ($firstline =~ m!\bHTTP/1\.0\b! && $conhdr =~ m!\bkeep-alive\b!i)) {
171 142 $self->{_sock} = $sock;
172 142 $self->{_reqdone}++;
173 } else {
174 8 $self->{_reqdone} = 0;
175 }
176  
177 150 return $resp;
178 150 };
179  
180 150 ▶ if ($opt_return_reader) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt_return_reader) { } 0 150 150
181 0 return $parse_it;
182 } else {
183 150 return $parse_it->();
184 }
185 }
186  
187 89 ▶sub reqdone {
Subroutine reqdone: called CC=1 SCAR=0.0
Pod undocumented
188 89 my $self = shift;
189 89 return $self->{_reqdone};
190 }
191  
192 # general purpose URL escaping function
193 152 ▶sub eurl {
Subroutine eurl: called CC=1 SCAR=0.1
Pod undocumented
194 152 my $a = $_[0];
195 0 $a =~ s/([^a-zA-Z0-9_\,\-.\/\\\: ])/uc sprintf("%%%02x",ord($1))/eg;
196 152 $a =~ tr/ /+/;
197 152 return $a;
198 }
199  
200 58 1;
« Perlbal/Test/WebServer.pm ↑ Summary Perlbal/ClientManage.pm »