File Coverage

blib/lib/PocketIO/Transport/BasePolling.pm
Criterion Covered Total %
statement 12 57 21.0
branch 0 8 0.0
condition 0 2 0.0
subroutine 4 13 30.7
pod 1 1 100.0
total 17 81 20.9


line stmt bran cond sub pod time code
1             package PocketIO::Transport::BasePolling;
2              
3 6     6   40 use strict;
  6         13  
  6         202  
4 6     6   30 use warnings;
  6         11  
  6         181  
5              
6 6     6   29 use base 'PocketIO::Transport::Base';
  6         11  
  6         489  
7              
8 6     6   34 use PocketIO::Exception;
  6         10  
  6         6280  
9              
10             sub dispatch {
11 0     0 1   my $self = shift;
12              
13 0 0         if ($self->{env}->{REQUEST_METHOD} eq 'GET') {
14 0           return $self->_dispatch_stream;
15             }
16              
17 0           return $self->_dispatch_send;
18             }
19              
20             sub _dispatch_stream {
21 0     0     my $self = shift;
22              
23 0           my $conn = $self->conn;
24              
25 0           my $handle = $self->{handle};
26              
27             return sub {
28 0     0     my $respond = shift;
29              
30             my $close_cb =
31 0           sub { $handle->close; $self->client_disconnected($conn); };
  0            
  0            
32 0           $handle->on_eof($close_cb);
33 0           $handle->on_error($close_cb);
34              
35 0           $handle->on_heartbeat(sub { $conn->send_heartbeat });
  0            
36              
37 0 0         if ($conn->has_staged_messages) {
38 0           $self->_write($conn, $handle, $conn->staged_message);
39             }
40             else {
41             $conn->on(
42             write => sub {
43 0           my $conn = shift;
44 0           my ($message) = @_;
45              
46 0           $conn->on(write => undef);
47 0           $self->_write($conn, $handle, $message);
48             }
49 0           );
50             }
51              
52 0           $conn->on(close => $close_cb);
53              
54 0 0         if ($conn->is_connected) {
55 0           $conn->reconnected;
56             }
57             else {
58 0           $self->client_connected($conn);
59             }
60 0           };
61             }
62              
63             sub _dispatch_send {
64 0     0     my $self = shift;
65              
66 0           my $conn = $self->conn;
67              
68 0           my $data = $self->_get_content;
69              
70 0           $conn->parse_message($data);
71              
72 0           return [200, ['Content-Length' => 1], ['1']];
73             }
74              
75             sub _get_content {
76 0     0     my $self = shift;
77              
78 0   0       my $content_length = $self->{env}->{CONTENT_LENGTH} || 0;
79 0           my $rcount =
80             $self->{env}->{'psgi.input'}->read(my $chunk, $content_length);
81              
82 0 0         PocketIO::Exception->throw(500) unless $rcount == $content_length;
83              
84 0           return $chunk;
85             }
86              
87 0     0     sub _content_type {'text/plain'}
88              
89             sub _write {
90 0     0     my $self = shift;
91 0           my ($conn, $handle, $message) = @_;
92              
93 0           $message = $self->_format_message($message);
94              
95             $handle->write(
96             join(
97             "\x0d\x0a" => 'HTTP/1.1 200 OK',
98             'Content-Type: ' . $self->_content_type,
99             'Content-Length: ' . length($message),
100             'Access-Control-Allow-Origin: *',
101             'Access-Control-Allow-Credentials: *',
102             '', $message,
103             ),
104             sub {
105 0     0     $handle->close;
106 0           $conn->reconnecting;
107             }
108 0           );
109             }
110              
111 0     0     sub _format_message { $_[1] }
112              
113             1;
114             __END__