File Coverage

blib/lib/SockJS/Transport/XHRStreaming.pm
Criterion Covered Total %
statement 43 47 91.4
branch 6 8 75.0
condition 3 5 60.0
subroutine 6 6 100.0
pod 0 2 0.0
total 58 68 85.2


line stmt bran cond sub pod time code
1             package SockJS::Transport::XHRStreaming;
2              
3 1     1   452 use strict;
  1         2  
  1         27  
4 1     1   5 use warnings;
  1         2  
  1         28  
5              
6 1     1   5 use base 'SockJS::Transport::Base';
  1         2  
  1         459  
7              
8             sub new {
9 3     3 0 6505 my $self = shift->SUPER::new(@_);
10 3         6 my (%params) = @_;
11              
12 3   50     11 $self->{response_limit} = $params{response_limit} || 128 * 1024;
13              
14 3         23 push @{$self->{allowed_methods}}, 'POST';
  3         9  
15              
16 3         8 return $self;
17             }
18              
19             sub dispatch_POST {
20 3     3 0 4 my $self = shift;
21 3         7 my ($env, $conn) = @_;
22              
23 3         7 my $limit = $self->{response_limit};
24              
25             return sub {
26 3     3   427 my $respond = shift;
27              
28 3         19 my $writer = $respond->(
29             [
30             200,
31             [ 'Content-Type' => 'application/javascript; charset=UTF-8' ]
32             ]
33             );
34              
35 3         67 $writer->write(('h' x 2048) . "\n");
36              
37 3 100 66     173 if ($conn->is_connected && !$conn->is_reconnecting) {
38 1         5 $writer->write('c[2010,"Another connection still open"]' . "\n");
39 1         40 $writer->write('');
40 1         56 $writer->close;
41 1         44 return;
42             }
43              
44             $conn->write_cb(
45             sub {
46 2         4 my $conn = shift;
47 2         5 my ($message) = @_;
48              
49 2         14 $writer->write($message . "\n");
50              
51 2         87 $limit -= length($message) - 1;
52              
53 2 50       8 if ($limit <= 0) {
54 0         0 $writer->write('');
55 0         0 $writer->close;
56              
57 0         0 $conn->reconnecting;
58             }
59             }
60 2         14 );
61              
62             $conn->close_cb(
63             sub {
64 1         2 my $conn = shift;
65              
66 1         5 $writer->write('');
67 1         41 $writer->close;
68             }
69 2         9 );
70              
71 2 100       6 if ($conn->is_closed) {
    50          
72 1         4 $conn->connected;
73 1         2 $conn->close;
74             }
75             elsif ($conn->is_connected) {
76 0         0 $conn->reconnected;
77             }
78             else {
79 1         2 $limit -= 4;
80 1         4 $conn->write('o');
81              
82 1         3 $conn->connected;
83             }
84 3         26 };
85             }
86              
87             1;