File Coverage

blib/lib/PocketIO/Transport/Htmlfile.pm
Criterion Covered Total %
statement 9 42 21.4
branch 0 4 0.0
condition 0 2 0.0
subroutine 3 8 37.5
pod 1 1 100.0
total 13 57 22.8


line stmt bran cond sub pod time code
1             package PocketIO::Transport::Htmlfile;
2              
3 6     6   35 use strict;
  6         12  
  6         201  
4 6     6   31 use warnings;
  6         14  
  6         158  
5              
6 6     6   34 use base 'PocketIO::Transport::Base';
  6         12  
  6         5185  
7              
8             sub dispatch {
9 0     0 1   my $self = shift;
10              
11 0 0         if ($self->{env}->{REQUEST_METHOD} eq 'GET') {
12 0           return $self->_dispatch_stream;
13             }
14              
15 0           return $self->_dispatch_send;
16             }
17              
18             sub _dispatch_stream {
19 0     0     my $self = shift;
20              
21 0           my $conn = $self->conn;
22              
23 0           my $handle = $self->{handle};
24              
25             return sub {
26             my $close_cb =
27 0     0     sub { $handle->close; $self->client_disconnected($conn); };
  0            
  0            
28 0           $handle->on_eof($close_cb);
29 0           $handle->on_error($close_cb);
30              
31 0           $handle->on_heartbeat(sub { $conn->send_heartbeat });
  0            
32              
33 0           $handle->write(
34             join "\x0d\x0a" => 'HTTP/1.1 200 OK',
35             'Content-Type: text/html',
36             'Connection: keep-alive',
37             'Transfer-Encoding: chunked',
38             'Access-Control-Allow-Origin: *',
39             'Access-Control-Allow-Credentials: *',
40             '',
41             sprintf('%x', 173 + 83),
42             ''
43             . (' ' x 173),
44             ''
45             );
46              
47             $conn->on(
48             write => sub {
49 0           my $conn = shift;
50 0           my ($message) = @_;
51              
52 0           $message = $self->_format_message($message);
53              
54 0           $handle->write(
55             join "\x0d\x0a" => sprintf('%x', length($message)),
56             $message,
57             ''
58             );
59             }
60 0           );
61              
62 0           $conn->on(close => $close_cb);
63              
64 0           $self->client_connected($conn);
65 0           };
66             }
67              
68             sub _dispatch_send {
69 0     0     my $self = shift;
70              
71 0   0       my $content_length = $self->{env}->{CONTENT_LENGTH} || 0;
72 0           my $rcount =
73             $self->{env}->{'psgi.input'}->read(my $chunk, $content_length);
74              
75 0 0         PocketIO::Exception->throw(500) unless $rcount == $content_length;
76              
77 0           $self->conn->parse_message($chunk);
78              
79 0           return [200, ['Content-Length' => 1], ['1']];
80             }
81              
82             sub _format_message {
83 0     0     my $self = shift;
84 0           my ($message) = @_;
85              
86 0           $message =~ s/"/\\"/g;
87 0           return qq{};
88             }
89              
90             1;
91             __END__