WebSocket/Response.pm

filter: Statementstmt 97.8% 135 / 138 Branchbran 82.3% 56 / 68 Conditioncond 90.9% 10 / 11 MC/DCmcdc 71.4% 5 / 7 Subroutinesub 100% 26 / 26 Podpod 100% 13 / 13 totaltotal 93.1% 245 / 263 CC 39 SCAR 19.1
CC
39
Cov
93%
headers
17.2
_finalize
7.1
_parse_body
5.1
CRAP 6.7
« WebSocket/Request.pm ↑ Summary WebSocket/Cookie/Response.pm »
1 package Protocol::WebSocket::Response;
2  
3 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 14 ▶use base 'Protocol::WebSocket::Message';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 14 require Carp;
9 14 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 14 ▶use Digest::SHA ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 14 ▶use Protocol::WebSocket::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 14 ▶use Protocol::WebSocket::Cookie::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 14 ▶sub location { @_ > 1 ? $_[0]->{location} = $_[1] : $_[0]->{location} }
Subroutine location: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 7 7 14
16  
17 195 ▶sub resource_name {
Subroutine resource_name: called CC=2 SCAR=6.9
Pod documented
18 195 ▶ @_ > 1 ? $_[0]->{resource_name} = $_[1] : $_[0]->{resource_name};
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 16 179 195
19 }
20  
21 260 ▶sub cookies { @_ > 1 ? $_[0]->{cookies} = $_[1] : $_[0]->{cookies} }
Subroutine cookies: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 260 260
22  
23 1 ▶sub cookie {
Subroutine cookie: called CC=1 SCAR=0.0
Pod documented
24 1 my $self = shift;
25  
26 1 push @{$self->{cookies}}, $self->_build_cookie(@_);
27 }
28  
29 136 ▶sub key { @_ > 1 ? $_[0]->{key} = $_[1] : $_[0]->{key} }
Subroutine key: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 4 132 136
30  
31 19 ▶sub number1 { shift->_number('number1', 'key1', @_) }
Subroutine number1: called CC=1 SCAR=0.0
Pod documented
32 19 ▶sub number2 { shift->_number('number2', 'key2', @_) }
Subroutine number2: called CC=1 SCAR=0.0
Pod documented
33  
34 38 ▶sub _number {
Subroutine _number: called CC=3 SCAR=11.0
35 38 my $self = shift;
36 38 my ($name, $key, $value) = @_;
37  
38 38 my $method = "SUPER::$name";
39 38 ▶ return $self->$method($value) if defined $value;
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 6 32 38
40  
41 32 $value = $self->$method();
42 32 ▶ $value = $self->_extract_number($self->$key) if not defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 4 28 32
43  
44 32 return $value;
45 }
46  
47 2 ▶sub key1 { @_ > 1 ? $_[0]->{key1} = $_[1] : $_[0]->{key1} }
Subroutine key1: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 2 2
48 2 ▶sub key2 { @_ > 1 ? $_[0]->{key2} = $_[1] : $_[0]->{key2} }
Subroutine key2: called CC=2 SCAR=7.6
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 2 2
49  
50 38 ▶sub status {
Subroutine status: called CC=1 SCAR=0.0
Pod documented
51 38 return '101';
52 }
53  
54 244 ▶sub headers {
Subroutine headers: called CC=17 SCAR=28.4
Pod documented
55 244 my $self = shift;
56  
57 244 ▶ my $version = $self->version || 'draft-ietf-hybi-10';
Condition100%2/2
$self->version || 'draft-ietf-hybi-10'
l!l
21826
MC/DC100%1/1
$self->version || 'draft-ietf-hybi-10'
$self->version
Truth table100%2/2
A
A= $self->version
Aresult
11
00
58  
59 244 my $headers = [];
60  
61 244 push @$headers, Upgrade => 'WebSocket';
62 244 push @$headers, Connection => 'Upgrade';
63  
64 244 ▶ if ($version eq 'draft-hixie-75' || $version eq 'draft-ietf-hybi-00') {
Branch: 2 branches
BranchTrueFalseTotal
if ($version eq 'draft-hixie-75' or $version eq 'draft-ietf-hybi-00') { } 178 66 244
elsif ($version eq 'draft-ietf-hybi-10' or $version eq 'draft-ietf-hybi-17') { } 66 0 66
Condition100%3/3
$version eq "draft-hixie-75" or $version eq "draft-ietf-hybi-00"
l!l&&r!l&&!r
5812066
MC/DC100%2/2
$version eq "draft-hixie-75" or $version eq "draft-ietf-hybi-00"
$version eq "draft-hixie-75" $version eq "draft-ietf-hybi-00"
Truth table100%3/3
A or B
A= $version eq "draft-hixie-75"B= $version eq "draft-ietf-hybi-00"
ABresult
1X1
011
000
65 178 ▶ Carp::croak(qq/host is required/) unless defined $self->host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->host 0 178 178
66  
67 178 my $location = $self->_build_url(
68 host => $self->host,
69 secure => $self->secure,
70 resource_name => $self->resource_name,
71 );
72 178 ▶ my $origin =
Branch: 1 branch
BranchTrueFalseTotal
$self->origin ? : 136 42 178
73 $self->origin ? $self->origin : 'http://' . $location->host;
74 178 ▶ $origin =~ s{^http:}{https:} if !$self->origin && $self->secure;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->origin and $self->secure 13 165 178
Condition100%3/3
not $self->origin and $self->secure
!ll&&!rl&&r
1362913
MC/DC100%2/2
not $self->origin and $self->secure
not $self->origin $self->secure
Truth table100%3/3
not(A) and B
A= not $self->originB= $self->secure
ABresult
0X0
100
111
75  
76 178 ▶ if ($version eq 'draft-hixie-75') {
Branch: 2 branches
BranchTrueFalseTotal
if ($version eq 'draft-hixie-75') { } 58 120 178
elsif ($version eq 'draft-ietf-hybi-00') { } 120 0 120
77 58 ▶ push @$headers, 'WebSocket-Protocol' => $self->subprotocol
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->subprotocol 16 42 58
78 if defined $self->subprotocol;
79 58 push @$headers, 'WebSocket-Origin' => $origin;
80 58 push @$headers, 'WebSocket-Location' => $location->to_string;
81 }
82 elsif ($version eq 'draft-ietf-hybi-00') {
83 120 ▶ push @$headers, 'Sec-WebSocket-Protocol' => $self->subprotocol
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->subprotocol 16 104 120
84 if defined $self->subprotocol;
85 120 push @$headers, 'Sec-WebSocket-Origin' => $origin;
86 120 push @$headers, 'Sec-WebSocket-Location' => $location->to_string;
87 }
88 }
89 ▶ elsif ($version eq 'draft-ietf-hybi-10' || $version eq 'draft-ietf-hybi-17') {
Condition66%2/3
$version eq "draft-ietf-hybi-10" or $version eq "draft-ietf-hybi-17"
l!l&&r!l&&!r
36300
MC/DC0%0/2
$version eq "draft-ietf-hybi-10" or $version eq "draft-ietf-hybi-17"
$version eq "draft-ietf-hybi-10" $version eq "draft-ietf-hybi-17"
Truth table66%2/3
A or B
A= $version eq "draft-ietf-hybi-10"B= $version eq "draft-ietf-hybi-17"
ABresult
1X1
011
000
90 66 ▶ Carp::croak(qq/key is required/) unless defined $self->key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->key 0 66 66
91  
92 66 my $key = $self->key;
93 66 $key .= '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; # WTF
94 66 $key = Digest::SHA::sha1($key);
95 66 $key = MIME::Base64::encode_base64($key);
96 66 $key =~ s{\s+}{}g;
97  
98 66 push @$headers, 'Sec-WebSocket-Accept' => $key;
99  
100 66 ▶ push @$headers, 'Sec-WebSocket-Protocol' => $self->subprotocol
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->subprotocol 26 40 66
101 if defined $self->subprotocol;
102 }
103 else {
104 0 Carp::croak('Version ' . $version . ' is not supported');
105 }
106  
107 244 ▶ if (@{$self->cookies}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->cookies;}) 16 228 244
108 16 my $cookie = join ',' => map { $_->to_string } @{$self->cookies};
109 16 push @$headers, 'Set-Cookie' => $cookie;
110 }
111  
112 244 return $headers;
113 }
114  
115 19 ▶sub body {
Subroutine body: called CC=2 SCAR=6.9
Pod documented
116 19 my $self = shift;
117  
118 19 ▶ return $self->checksum if $self->version eq 'draft-ietf-hybi-00';
Branch: 1 branch
BranchTrueFalseTotal
if $self->version eq "draft-ietf-hybi-00" 9 10 19
119  
120 10 return '';
121 }
122  
123 19 ▶sub to_string {
Subroutine to_string: called CC=2 SCAR=6.9
Pod documented
124 19 my $self = shift;
125  
126 19 my $status = $self->status;
127  
128 19 my $string = '';
129 19 $string .= "HTTP/1.1 $status WebSocket Protocol Handshake\x0d\x0a";
130  
131 19 ▶ for (my $i = 0; $i < @{$self->headers}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->headers;}) 75 19 94
132 75 my $key = $self->headers->[$i];
133 75 my $value = $self->headers->[$i + 1];
134  
135 75 $string .= "$key: $value\x0d\x0a";
136 }
137  
138 19 $string .= "\x0d\x0a";
139  
140 19 $string .= $self->body;
141  
142 19 return $string;
143 }
144  
145 19 ▶sub _parse_first_line {
Subroutine _parse_first_line: called CC=3 SCAR=11.0
146 19 my ($self, $line) = @_;
147  
148 19 my $status = $self->status;
149 19 ▶ unless ($line =~ m{^HTTP/1\.1 $status }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ m[^HTTP/1\.1 $status ]) 4 15 19
150 4 my $vis = $line;
151 4 ▶ if( length( $vis ) > 80 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $vis > 80) 1 3 4
152 1 substr( $vis, 77 )= '...';
153 }
154 4 $self->error('Wrong response line. Got [[' . $vis . "]], expected [[HTTP/1.1 $status ]]");
155 4 return;
156 }
157  
158 15 return $self;
159 }
160  
161 15 ▶sub _parse_body {
Subroutine _parse_body: called CC=5 SCAR=16.3
162 15 my $self = shift;
163  
164 15 ▶ if ($self->field('Sec-WebSocket-Accept')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->field('Sec-WebSocket-Accept')) { } 6 9 15
elsif ($self->field('Sec-WebSocket-Origin')) { } 7 2 9
165 6 $self->version('draft-ietf-hybi-10');
166 }
167 elsif ($self->field('Sec-WebSocket-Origin')) {
168 7 $self->version('draft-ietf-hybi-00');
169  
170 7 ▶ return 1 if length $self->{buffer} < 16;
Branch: 1 branch
BranchTrueFalseTotal
if length $self->{"buffer"} < 16 2 5 7
171  
172 5 my $checksum = substr $self->{buffer}, 0, 16, '';
173 5 $self->checksum($checksum);
174 }
175 else {
176 2 $self->version('draft-hixie-75');
177 }
178  
179 13 ▶ return $self if $self->_finalize;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_finalize 13 0 13
180  
181 0 $self->error('Not a valid response');
182 0 return;
183 }
184  
185 13 ▶sub _finalize {
Subroutine _finalize: called CC=7 SCAR=19.6
186 13 my $self = shift;
187  
188 13 ▶ if ($self->version eq 'draft-hixie-75') {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->version eq 'draft-hixie-75') { } 2 11 13
elsif ($self->version eq 'draft-ietf-hybi-00') { } 5 6 11
189 2 my $location = $self->field('WebSocket-Location');
190 2 ▶ return unless defined $location;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $location 0 2 2
191 2 $self->location($location);
192  
193 2 my $url = $self->_build_url;
194 2 ▶ return unless $url->parse($self->location);
Branch: 1 branch
BranchTrueFalseTotal
unless $url->parse($self->location) 0 2 2
195  
196 2 $self->secure($url->secure);
197 2 $self->host($url->host);
198 2 $self->resource_name($url->resource_name);
199  
200 2 $self->origin($self->field('WebSocket-Origin'));
201  
202 2 $self->subprotocol($self->field('WebSocket-Protocol'));
203 }
204 elsif ($self->version eq 'draft-ietf-hybi-00') {
205 5 my $location = $self->field('Sec-WebSocket-Location');
206 5 ▶ return unless defined $location;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $location 0 5 5
207 5 $self->location($location);
208  
209 5 my $url = $self->_build_url;
210 5 ▶ return unless $url->parse($self->location);
Branch: 1 branch
BranchTrueFalseTotal
unless $url->parse($self->location) 0 5 5
211  
212 5 $self->secure($url->secure);
213 5 $self->host($url->host);
214 5 $self->resource_name($url->resource_name);
215  
216 5 $self->origin($self->field('Sec-WebSocket-Origin'));
217 5 $self->subprotocol($self->field('Sec-WebSocket-Protocol'));
218 }
219 else {
220 6 $self->subprotocol($self->field('Sec-WebSocket-Protocol'));
221 }
222  
223 13 return 1;
224 }
225  
226 185 ▶sub _build_url { shift; Protocol::WebSocket::URL->new(@_) }
Subroutine _build_url: called CC=1 SCAR=0.0
227 1 ▶sub _build_cookie { shift; Protocol::WebSocket::Cookie::Response->new(@_) }
Subroutine _build_cookie: called CC=1 SCAR=0.0
228  
229 14 1;
230 __END__
« WebSocket/Request.pm ↑ Summary WebSocket/Cookie/Response.pm »