SockJS.pm untested

filter: Statementstmt 0.0% 0 / 175 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 21 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 266 CC 21 SCAR 31.7
CC
21
Cov
0%
call
56.0
_dispatch_transport
56.0
_dispatch_info
20.0
CRAP 23.9
1 package SockJS;
2  
3 use strict;
4 use warnings;
5  
6 our $VERSION = '0.10';
7  
8 use overload '&{}' => sub { shift->to_app(@_) }, fallback => 1;
9  
10 use JSON ();
11 use Digest::MD5 ();
12 use Scalar::Util ();
13  
14 use Plack::Middleware::Chunked;
15 use SockJS::Middleware::Http10;
16 use SockJS::Middleware::JSessionID;
17 use SockJS::Middleware::Cors;
18 use SockJS::Middleware::Cache;
19 use SockJS::Transport;
20 use SockJS::Session;
21 use SockJS::Connection;
22  
23 sub new {
24 my $class = shift;
25 my (%params) = @_;
26  
27 my $self = {};
28 bless $self, $class;
29  
30 $self->{handler} = $params{handler};
31  
32 $self->{websocket} = $params{websocket};
33 $self->{cookie} = $params{cookie};
34 $self->{chunked} = $params{chunked};
35 $self->{sockjs_url} = $params{sockjs_url};
36 $self->{session_factory} = $params{session_factory};
37 $self->{response_limit} = $params{response_limit};
38  
39 $self->{websocket} = 1 unless defined $params{websocket};
40 $self->{chunked} = 1 unless defined $params{chunked};
41 $self->{sockjs_url} ||= 'http://cdn.sockjs.org/sockjs-0.3.4.min.js';
42 $self->{session_factory} ||= sub { shift; SockJS::Session->new(@_) };
43  
44 $self->{connections} = {};
45  
46 return $self;
47 }
48  
49 sub to_app {
50 my $self = shift;
51  
52 my $app = sub { $self->call(@_) };
53  
54 $app = SockJS::Middleware::Http10->new->wrap($app);
55 $app = Plack::Middleware::Chunked->new->wrap($app) if $self->{chunked};
56 $app =
57 SockJS::Middleware::JSessionID->new( cookie => $self->{cookie} )
58 ->wrap($app);
59 $app = SockJS::Middleware::Cors->new->wrap($app);
60 $app = SockJS::Middleware::Cache->new->wrap($app);
61  
62 return $app;
63 }
64  
65 sub call {
66 my $self = shift;
67 my ($env) = @_;
68  
69 my $path_info = $env->{PATH_INFO};
70 $path_info = '' unless defined $path_info;
71  
72 if ($path_info eq '' || $path_info eq '/') {
73 return $self->_dispatch_welcome_page($env);
74 }
75 elsif ($path_info =~ m{^/[^\/\.]+/([^\/\.]+)/([^\/\.]+)$}) {
76 my ($session_id, $transport) = ($1, $2);
77  
78 return $self->_dispatch_transport($env, $session_id, $transport);
79 }
80 elsif ($path_info =~ m{^/websocket$}) {
81 my ($session_id, $transport) = ('raw', 'raw_websocket');
82  
83 return $self->_dispatch_transport($env, $session_id, $transport);
84 }
85 elsif ($path_info eq '/info') {
86 return $self->_dispatch_info($env);
87 }
88 elsif ($path_info =~ m{^/iframe[^\/]*\.html$}) {
89 return $self->_dispatch_iframe($env);
90 }
91  
92 return [404, ['Content-Length' => 9], ['Not found']];
93 }
94  
95 sub _dispatch_welcome_page {
96 my $self = shift;
97 my ($env) = @_;
98  
99 my $message = "Welcome to SockJS!\n";
100  
101 return [
102 200,
103 [
104 'Content-Type' => 'text/plain; charset=UTF-8',
105 'Content-Length' => length($message)
106 ],
107 [$message]
108 ];
109 }
110  
111 sub _dispatch_transport {
112 my $self = shift;
113 my ($env, $id, $path) = @_;
114  
115 my $transport =
116 SockJS::Transport->build($path,
117 response_limit => $self->{response_limit});
118 return [404, ['Content-Type' => 'text/plain'], ['Not found']]
119 unless $transport;
120  
121 $env->{'sockjs.transport'} = $transport->name;
122  
123 my $conn = $self->{connections}->{$id};
124  
125 if (!$conn || $transport->name =~ m/websocket/) {
126 $conn = SockJS::Connection->new(type => $transport->name);
127  
128 if ($transport->name =~ m/websocket/) {
129 push @{$self->{connections}->{$id}}, $conn;
130 }
131 else {
132 $self->{connections}->{$id} = $conn;
133 }
134  
135 my $session =
136 $self->{session_factory}
137 ->($self, id => $id, connection => $conn, type => $transport->name);
138  
139 my $close_timer;
140 $conn->on(connect => sub { $self->{handler}->($session, $env); });
141 $conn->on(data => sub { shift; $session->fire_event(data => @_) });
142 $conn->on(
143 close => sub {
144 my $conn = shift;
145  
146 $session->fire_event(close => @_);
147  
148 $close_timer = AnyEvent->timer(
149 after => .5,
150 cb => sub {
151 if (ref $self->{connections}->{$id} eq 'ARRAY') {
152 $self->{connections}->{$id} =
153 [grep { "$_" ne "$conn" }
154 @{$self->{connections}->{$id}}];
155 delete $self->{connections}->{$id}
156 unless @{$self->{connections}->{$id}};
157 }
158 else {
159 delete $self->{connections}->{$id};
160 }
161 }
162 );
163 }
164 );
165 }
166  
167 my $response;
168 eval { $response = $transport->dispatch($env, $conn) } || do {
169 my $e = $@;
170  
171 warn $e;
172  
173 my ($code, $error) = (500, $e);
174  
175 if (Scalar::Util::blessed($e)) {
176 $code = $e->code;
177 $error = $e->message;
178 }
179  
180 $response = [
181 $code,
182 [
183 'Content-Type' => 'text/plain; charset=UTF-8',
184 'Content-Length' => length($error),
185 ],
186 [$error]
187 ];
188 };
189  
190 return $response;
191 }
192  
193 sub _dispatch_info {
194 my $self = shift;
195 my ($env) = @_;
196  
197 $env->{'sockjs.allowed_methods'} = [qw/OPTIONS GET/];
198  
199 if ($env->{REQUEST_METHOD} eq 'OPTIONS') {
200 $env->{'sockjs.cacheable'} = 1;
201  
202 return [ 204, [], [] ];
203 }
204  
205 my $info = JSON::encode_json(
206 {
207 websocket => $self->{websocket} ? JSON::true
208 : JSON::false,
209 cookie_needed => $self->{cookie} ? JSON::true
210 : JSON::false,
211 origins => ['*:*'],
212 entropy => int(rand(2**32))
213 }
214 );
215  
216 return [
217 200,
218 [
219 'Content-Type' => 'application/json; charset=UTF-8',
220 'Content-Length' => length($info),
221 ],
222 [$info]
223 ];
224 }
225  
226 sub _dispatch_iframe {
227 my $self = shift;
228 my ($env) = @_;
229  
230 my $sockjs_url = $self->{sockjs_url};
231 my $body = <<"EOF";
232 <!DOCTYPE html>
233 <html>
234 <head>
235 <meta http-equiv="X-UA-Compatible" content="IE=edge" />
236 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
237 <script src="$sockjs_url"></script>
238 <script>
239 document.domain = document.domain;
240 SockJS.bootstrap_iframe();
241 </script>
242 </head>
243 <body>
244 <h2>Don't panic!</h2>
245 <p>This is a SockJS hidden iframe. It's used for cross domain magic.</p>
246 </body>
247 </html>
248 EOF
249  
250 my $etag = Digest::MD5::md5_hex($body);
251  
252 if (my $expected = $env->{HTTP_IF_NONE_MATCH}) {
253 if ($expected eq $etag) {
254 return [304, [], ['']];
255 }
256 }
257  
258 $env->{'sockjs.cacheable'} = 1;
259  
260 return [
261 200,
262 [
263 'Content-Type' => 'text/html; charset=UTF-8',
264 'Etag' => Digest::MD5::md5_hex($body),
265 'Content-Length' => length($body),
266 ],
267 [$body]
268 ];
269 }
270  
271 1;
272 __END__