lib/auto/share/dist/App-SocialCalc-Multiplayer/third-party/lib/PocketIO/Transport/Htmlfile.pm untested

filter: Statementstmt 0.0% 0 / 62 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 90 CC 5 SCAR 20.4
CC
5
Cov
0%
dispatch
20.0
_dispatch_send
6.0
name
2.0
CRAP 7.7
1 package PocketIO::Transport::Htmlfile;
2  
3 use strict;
4 use warnings;
5  
6 use base 'PocketIO::Transport::Base';
7  
8 use HTTP::Body;
9 use PocketIO::Response::Chunked;
10  
11 sub name {'htmlfile'}
12  
13 sub dispatch {
14 my $self = shift;
15 my ($cb) = @_;
16  
17 my $req = $self->req;
18 my $name = $self->name;
19  
20 if ($req->method eq 'GET') {
21 return $self->_dispatch_stream($cb) if $req->path =~ m{^/$name//\d+$};
22 }
23  
24 return
25 unless $req->method eq 'POST'
26 && $req->path_info =~ m{^/$name/(\d+)/send$};
27  
28 return $self->_dispatch_send($req, $1);
29 }
30  
31 sub _dispatch_stream {
32 my $self = shift;
33 my ($cb) = @_;
34  
35 my $handle = $self->_build_handle($self->env->{'psgix.io'});
36  
37 return sub {
38 my $conn = $self->add_connection(on_connect => $cb);
39  
40 my $close_cb = sub { $handle->close; $self->client_disconnected($conn); };
41 $handle->on_eof($close_cb);
42 $handle->on_error($close_cb);
43  
44 $handle->on_heartbeat(sub { $conn->send_heartbeat });
45  
46 my $id = $self->_wrap_into_script($conn->build_id_message);
47  
48 $handle->write(
49 join "\x0d\x0a" => 'HTTP/1.1 200 OK',
50 'Content-Type: text/html',
51 'Connection: keep-alive',
52 'Transfer-Encoding: chunked',
53 '',
54 sprintf('%x', 244 + 12),
55 '<html><body>' . (' ' x 244),
56 sprintf('%x', length($id)),
57 $id,
58 ''
59 );
60  
61 $conn->on_write(
62 sub {
63 my $conn = shift;
64 my ($message) = @_;
65  
66 $message = $self->_wrap_into_script($message);
67  
68 $handle->write(
69 join "\x0d\x0a" => sprintf('%x', length($message)),
70 $message,
71 ''
72 );
73 }
74 );
75  
76 $self->client_connected($conn);
77 };
78 }
79  
80 sub _dispatch_send {
81 my $self = shift;
82 my ($req, $id) = @_;
83  
84 my $conn = $self->find_connection($id);
85 return unless $conn;
86  
87 my $raw_body = $req->content;
88 my $zeros = $raw_body =~ s/\0//g;
89  
90 my $body = HTTP::Body->new($self->env->{CONTENT_TYPE},
91 $self->env->{CONTENT_LENGTH} - $zeros);
92 $body->add($raw_body);
93  
94 my $data = $body->param->{data};
95  
96 $conn->read($data);
97  
98 return PocketIO::Response::Chunked->finalize;
99 }
100  
101 sub _wrap_into_script {
102 my $self = shift;
103 my ($message) = @_;
104  
105 $message =~ s/"/\\"/g;
106 return qq{<script>parent.s._("$message", document);</script>};
107 }
108  
109 1;
110 __END__