Terse/WebSocket.pm

filter: Statementstmt 5.0% 7 / 140 Branchbran 0.0% 0 / 82 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 21 Subroutinesub 28.5% 2 / 7 Podpod 20.0% 1 / 5 totaltotal 3.4% 10 / 289 CC 56 SCAR 53.3
CC
56
Cov
3%
headers
380.0
start
182.0
new
132.0
CRAP 205.5
↑ Summary Terse/Controller.pm »
1 package Terse::WebSocket;
2  
3 3 ▶use base 'Terse';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 sub new {
7 0 ▶ my ($class, $t) = @_;
Subroutine new: not called CC=11 SCAR=48.8
Pod documented
8 0 my $self = $class->SUPER::new();
9 0 my $version = '';
10 0 my $env = $t->request->env;
11 0 ▶ if (!$env->{$t->{_sock}} || !$env->{$t->{_stream_check}}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$env{$t->{"_sock"}} and $$env{$t->{"_stream_check"}}) 0 0 0
Condition0%0/3
$$env{$t->{"_sock"}} and $$env{$t->{"_stream_check"}}
!ll&&!rl&&r
000
MC/DC0%0/2
$$env{$t->{"_sock"}} and $$env{$t->{"_stream_check"}}
$$env{$t->{"_sock"}} $$env{$t->{"_stream_check"}}
Truth table0%0/3
A and B
A= $$env{$t->{"_sock"}}B= $$env{$t->{"_stream_check"}}
ABresult
0X0
100
111
12 0 return 'Invalid environment no _sock or _stream_check in env';
13 }
14 $self->options = {
15 secret => 'ABABCABC-ABC-ABC-ABCD-ABCABCABCABC',
16 upgrade => $env->{HTTP_UPGRADE},
17 connection => $env->{HTTP_CONNECTION},
18 host => $env->{HTTP_HOST},
19 origin => $env->{HTTP_ORIGIN},
20 ($env->{HTTP_SEC_WEBSOCKET_KEY}
21 ? (sec_websocket_key => $env->{HTTP_SEC_WEBSOCKET_KEY})
22 : ()
23 ),
24 ($env->{HTTP_SEC_WEBSOCKET_KEY1}
25 ? (sec_websocket_key1 => $env->{HTTP_SEC_WEBSOCKET_KEY1})
26 : (sec_websocket_key2 => $env->{HTTP_SEC_WEBSOCKET_KEY2})
27 0 ▶ ),
Branch: 2 branches
BranchTrueFalseTotal
$env->{'HTTP_SEC_WEBSOCKET_KEY'} ? : 0 0 0
$env->{'HTTP_SEC_WEBSOCKET_KEY1'} ? : 0 0 0
28 subprotocol => 'chat',
29 };
30 0 ▶ if (exists $env->{HTTP_SEC_WEBSOCKET_VERSION}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $env->{"HTTP_SEC_WEBSOCKET_VERSION"}) 0 0 0
31 0 $fields->{'sec_websocket_version'} = $env->{HTTP_SEC_WEBSOCKET_VERSION};
32 0 ▶ if ($env->{HTTP_SEC_WEBSOCKET_VERSION} eq '13') {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{'HTTP_SEC_WEBSOCKET_VERSION'} eq '13') { } 0 0 0
33 0 $self->version = 'draft-ietf-hybi-17';
34 }
35 else {
36 0 $self->version = 'draft-ietf-hybi-10';
37 }
38 }
39 $self->resource_name = "$env->{SCRIPT_NAME}$env->{PATH_INFO}"
40 0 ▶ . ($env->{QUERY_STRING} ? "?$env->{QUERY_STRING}" : "");
Branch: 1 branch
BranchTrueFalseTotal
$env->{'QUERY_STRING'} ? : 0 0 0
41 0 $self->psgix = $env->{'psgix.io'};
42 0 ▶ if ($env->{HTTP_X_FORWARDED_PROTO} && $env->{HTTP_X_FORWARDED_PROTO} eq 'https') {
Branch: 1 branch
BranchTrueFalseTotal
if ($env->{"HTTP_X_FORWARDED_PROTO"} and $env->{"HTTP_X_FORWARDED_PROTO"} eq "https") 0 0 0
Condition0%0/3
$env->{"HTTP_X_FORWARDED_PROTO"} and $env->{"HTTP_X_FORWARDED_PROTO"} eq "https"
!ll&&!rl&&r
000
MC/DC0%0/2
$env->{"HTTP_X_FORWARDED_PROTO"} and $env->{"HTTP_X_FORWARDED_PROTO"} eq "https"
$env->{"HTTP_X_FORWARDED_PROTO"} $env->{"HTTP_X_FORWARDED_PROTO"} eq "https"
Truth table0%0/3
A and B
A= $env->{"HTTP_X_FORWARDED_PROTO"}B= $env->{"HTTP_X_FORWARDED_PROTO"} eq "https"
ABresult
0X0
100
111
43 0 $self->secure(1);
44 }
45 0 ▶ unless ($self->parse($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->parse($_[0])) 0 0 0
46 0 $self->error($req->error);
47 0 return;
48 }
49 0 return $self;
50 }
51  
52 sub start {
53 0 ▶ my ($self, $t, $cbs, $responder) = @_;
Subroutine start: not called CC=13 SCAR=52.0
Pod undocumented
54 0 my $writer = eval { $responder->([101, [$self->headers]]); };
55 0 ▶ $cbs->{($@ ? 'error' : 'connect')}->($self, $responder, $@);
Branch: 1 branch
BranchTrueFalseTotal
$@ ? : 0 0 0
56 0 ▶ my $reset_rate = $t->websocket_reset_rate ||= 100000;
Condition0%0/2
$t->websocket_reset_rate ||= 100000
l!l
00
MC/DC0%0/1
$t->websocket_reset_rate ||= 100000
$t->websocket_reset_rate
Truth table0%0/2
A
A= $t->websocket_reset_rate
Aresult
11
00
57 0 eval {
58 0 my $ping_rate = $reset_rate;
59 0 while (1) {
60 0 $ping_rate--;
61 0 my $response;
62 0 ▶ if ($ping_rate < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ping_rate < 0) 0 0 0
63 0 $ping_rate = $reset_rate;
64 0 $self->send($ping);
65 0 ▶ $response = $self->recieve() while($ping_rate-- > 0 && !$response);
Branch: 1 branch
BranchTrueFalseTotal
$ping_rate-- > 0 and not $response and do { $response = $self->recieve } 0 0 0
Condition0%0/3
$ping_rate-- > 0 and not $response
!ll&&!rl&&r
000
MC/DC0%0/2
$ping_rate-- > 0 and not $response
$ping_rate-- > 0 not $response
Truth table0%0/3
A and not(B)
A= $ping_rate-- > 0B= not $response
ABresult
0X0
100
111
66 0 ▶ if (!$response || $response ne 'pong') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $response or $response ne "pong") 0 0 0
Condition0%0/3
not $response or $response ne "pong"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $response or $response ne "pong"
not $response $response ne "pong"
Truth table0%0/3
not(A) or B
A= not $responseB= $response ne "pong"
ABresult
1X1
011
000
67 0 last;
68 }
69 0 $ping_rate = $reset_rate;
70 }
71 0 $response = $self->recieve();
72 0 ▶ if ($response) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response) 0 0 0
73 0 ▶ if ($response =~ m/^invalid_(length|version|host|required_key)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($response =~ /^invalid_(length|version|host|required_key)$/) { } 0 0 0
74 0 $cbs->{error}->($self, $response, $responder);
75 0 last;
76 } else {
77 0 $ping_rate = $reset_rate;
78 0 $cbs->{recieve}->($self, $response, $responder);
79 }
80 }
81 }
82 };
83 0 ▶ $cbs->{error}->($self, $responder, $@) if ($@);
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
84 0 ▶ $cbs->{disconnect}->($self, $responder) if $cbs->{disconnect};
Branch: 1 branch
BranchTrueFalseTotal
if $cbs->{"disconnect"} 0 0 0
85 0 ▶ delete $t->{_application}->websockets->{$t->sid->value} if $cbs->{close_delete};
Branch: 1 branch
BranchTrueFalseTotal
if $cbs->{"close_delete"} 0 0 0
86 0 $responder->([200, []]);
87 }
88  
89 sub headers {
90 0 ▶ my ($self) = @_;
Subroutine headers: not called CC=19 SCAR=59.4
Pod undocumented
91 0 ▶ my $version = $self->version || 'draft-ietf-hybi-10';
Condition0%0/2
$self->version || 'draft-ietf-hybi-10'
l!l
00
MC/DC0%0/1
$self->version || 'draft-ietf-hybi-10'
$self->version
Truth table0%0/2
A
A= $self->version
Aresult
11
00
92 0 my @headers = ();
93 0 push @headers, Upgrade => 'WebSocket';
94 0 push @headers, Connection => 'Upgrade';
95 0 ▶ 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') { } 0 0 0
elsif ($version eq 'draft-ietf-hybi-10' or $version eq 'draft-ietf-hybi-17') { } 0 0 0
Condition0%0/3
$version eq "draft-hixie-75" or $version eq "draft-ietf-hybi-00"
l!l&&r!l&&!r
000
MC/DC0%0/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 table0%0/3
A or B
A= $version eq "draft-hixie-75"B= $version eq "draft-ietf-hybi-00"
ABresult
1X1
011
000
96 0 ▶ return 'invalid_host' unless defined $self->options->host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->options->host 0 0 0
97 0 my $location = 'ws';
98 0 ▶ $location .= 's' if $self->options->secure;
Branch: 1 branch
BranchTrueFalseTotal
if $self->options->secure 0 0 0
99 0 $location .= '://';
100 0 $location .= $self->options->host;
101 0 ▶ $location .= ':' . $self->options->port if defined $self->options->port;
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->options->port 0 0 0
102 0 ▶ $location .= $self->resource_name || '/';
Condition0%0/2
$self->resource_name || '/'
l!l
00
MC/DC0%0/1
$self->resource_name || '/'
$self->resource_name
Truth table0%0/2
A
A= $self->resource_name
Aresult
11
00
103 0 ▶ my $origin = $self->options->origin ? $self->options->origin : 'http://' . $self->options->host;
Branch: 1 branch
BranchTrueFalseTotal
$self->options->origin ? : 0 0 0
104 0 ▶ $origin =~ s{^http:}{https:} if !$self->options->origin && $self->options->secure;
Branch: 1 branch
BranchTrueFalseTotal
if not $self->options->origin and $self->options->secure 0 0 0
Condition0%0/3
not $self->options->origin and $self->options->secure
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->options->origin and $self->options->secure
not $self->options->origin $self->options->secure
Truth table0%0/3
not(A) and B
A= not $self->options->originB= $self->options->secure
ABresult
0X0
100
111
105 0 ▶ if ($version eq 'draft-hixie-75') {
Branch: 2 branches
BranchTrueFalseTotal
if ($version eq 'draft-hixie-75') { } 0 0 0
elsif ($version eq 'draft-ietf-hybi-00') { } 0 0 0
106 0 ▶ push @headers, 'WebSocket-Protocol' => $self->subprotocol
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->options->subprotocol 0 0 0
107 if defined $self->options->subprotocol;
108 0 push @headers, 'WebSocket-Origin' => $origin;
109 0 push @headers, 'WebSocket-Location' => $location;
110 }
111 elsif ($version eq 'draft-ietf-hybi-00') {
112 0 ▶ push @headers, 'Sec-WebSocket-Protocol' => $self->options->subprotocol
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->options->subprotocol 0 0 0
113 if defined $self->options->subprotocol;
114 0 push @headers, 'Sec-WebSocket-Origin' => $origin;
115 0 push @headers, 'Sec-WebSocket-Location' => $location;
116 }
117 }
118 ▶ elsif ($version eq 'draft-ietf-hybi-10' || $version eq 'draft-ietf-hybi-17') {
Condition0%0/3
$version eq "draft-ietf-hybi-10" or $version eq "draft-ietf-hybi-17"
l!l&&r!l&&!r
000
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 table0%0/3
A or B
A= $version eq "draft-ietf-hybi-10"B= $version eq "draft-ietf-hybi-17"
ABresult
1X1
011
000
119 0 ▶ return 'invalid_required_key' unless defined $self->options->key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->options->key 0 0 0
120 0 my $key = $self->options->key;
121 0 $key .= $self->options->secret;
122 0 $key = Digest::SHA::sha1($key);
123 0 $key = MIME::Base64::encode_base64($key);
124 0 $key =~ s{\s+}{}g;
125 0 push @headers, 'Sec-WebSocket-Accept' => $key;
126 0 ▶ push @headers, 'Sec-WebSocket-Protocol' => $self->options->subprotocol
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->options->subprotocol 0 0 0
127 if defined $self->options->subprotocol;
128 }
129 else {
130 0 return 'invalid_version';
131 }
132 0 return @headers;
133 }
134  
135 sub send {
136 0 ▶ my ($self, $message) = @_;
Subroutine send: not called CC=6 SCAR=37.4
Pod undocumented
137 0 my $pg = $self->psgix;
138 0 ▶ my $mask = $self->mask ||= 0;
Condition0%0/2
$self->mask ||= 0
l!l
00
MC/DC0%0/1
$self->mask ||= 0
$self->mask
Truth table0%0/2
A
A= $self->mask
Aresult
11
00
139 0 my (@ENCODED) = map { ord($_) } split //, $message;
140 0 my $length = scalar @ENCODED + 128;
141 0 ▶ if ($length > 254 || $mask) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 254 or $mask) { } 0 0 0
Condition0%0/3
$length > 254 or $mask
l!l&&r!l&&!r
000
MC/DC0%0/2
$length > 254 or $mask
$length > 254 $mask
Truth table0%0/3
A or B
A= $length > 254B= $mask
ABresult
1X1
011
000
142 0 @MASK = map { int(rand(256)) } 0 .. 3;
143 0 my $i;
144 0 $ENCODED[$i++] = $_ ^ $MASK[$i % 4] for (@ENCODED);
145 0 unshift @ENCODED, @MASK;
146 0 ▶ if ($length > 256) {
Branch: 1 branch
BranchTrueFalseTotal
if ($length > 256) { } 0 0 0
147 0 my $times = int(($length + 2) / 254) - 1;
148 0 my $excess = $length - (($times * 256) + 128);
149 0 unshift @ENCODED, (254, $times, $excess);
150 } else {
151 0 unshift @ENCODED, $length;
152 }
153 } else {
154 0 unshift @ENCODED, $length - 128;
155 }
156 0 syswrite $pg, join("", map {chr($_)} (129, @ENCODED));
157 0 return $self;
158 }
159  
160 sub recieve {
161 0 ▶ my ($self, @ENCODED) = @_;
Subroutine recieve: not called CC=11 SCAR=48.8
Pod undocumented
162 0 my $length;
163 0 ▶ if (! scalar @ENCODED ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @ENCODED) 0 0 0
164 0 ▶ return shift @{ $self->next_frame } if scalar @{ $self->next_frame ||= [] };
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->next_frame ||= [];} 0 0 0
Condition0%0/2
$self->next_frame ||= []
l!l
00
MC/DC0%0/1
$self->next_frame ||= []
$self->next_frame
Truth table0%0/2
A
A= $self->next_frame
Aresult
11
00
165 0 my $pg = $self->psgix;
166 0 my $content = "";
167 0 $length = sysread($pg, $content, 8192);
168 0 ▶ return unless $length;
Branch: 1 branch
BranchTrueFalseTotal
unless $length 0 0 0
169 0 ▶ $length = sysread($pg, $content, 8192, length($content)) while $length >= 8192;
Branch: 1 branch
BranchTrueFalseTotal
$length >= 8192 and do { $length = sysread($pg, $content, 8192, length $content) } 0 0 0
170 0 @ENCODED = map { unpack "C", $_ } split //, $content;
171 }
172 0 my @bits = split //, sprintf("%b\n", $ENCODED[0]);
173 0 $self->fin = $bits[0];
174 0 $self->rsv = [@bits[1 .. 3]];
175 0 $self->op = shift @ENCODED;
176 0 ▶ if ($ENCODED[0] == 254) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENCODED[0] == 254) { } 0 0 0
177 0 my @length = splice @ENCODED, 0, 3;
178 0 $length = ((($length[0] + 2) * $length[1]) + $length[2]);
179 } else {
180 0 $length = shift @ENCODED;
181 0 $length -= 128;
182 }
183 0 ▶ return pack "C*", join("", @ENCODED) if (scalar @ENCODED == $length);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ENCODED == $length 0 0 0
184 0 my @MASK = splice @ENCODED, 0, 4;
185 0 ▶ if (scalar @ENCODED > $length) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @ENCODED > $length) 0 0 0
186 0 my $next = $self->recieve(splice @ENCODED, $length, scalar @ENCODED);
187 0 ▶ return $next if ($next eq 'invalid_length');
Branch: 1 branch
BranchTrueFalseTotal
if $next eq "invalid_length" 0 0 0
188 0 unshift @{ $self->next_frame }, $next;
189 }
190 0 ▶ return 'invalid_length' if (scalar @ENCODED != $length);
Branch: 1 branch
BranchTrueFalseTotal
if scalar @ENCODED != $length 0 0 0
191 0 return join "", map { pack "C", ($ENCODED[$_] ^ $MASK[$_ % 4]) } 0 .. $#ENCODED;
192 }
193  
194 3 1;
195  
196 __END__;
↑ Summary Terse/Controller.pm »