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   1015 use strict;
  1         3  
  1         24  
4 1     1   5 use warnings;
  1         1  
  1         25  
5              
6 1     1   4 use base 'SockJS::Transport::Base';
  1         1  
  1         416  
7              
8             sub new {
9 3     3 0 35097 my $self = shift->SUPER::new(@_);
10 3         5 my (%params) = @_;
11              
12 3   50     12 $self->{response_limit} = $params{response_limit} || 128 * 1024;
13              
14 3         17 push @{$self->{allowed_methods}}, 'POST';
  3         8  
15              
16 3         6 return $self;
17             }
18              
19             sub dispatch_POST {
20 3     3 0 5 my $self = shift;
21 3         6 my ($env, $conn) = @_;
22              
23 3         4 my $limit = $self->{response_limit};
24              
25             return sub {
26 3     3   445 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         215 $writer->write(('h' x 2048) . "\n");
36              
37 3 100 66     180 if ($conn->is_connected && !$conn->is_reconnecting) {
38 1         5 $writer->write('c[2010,"Another connection still open"]' . "\n");
39 1         34 $writer->write('');
40 1         32 $writer->close;
41 1         31 return;
42             }
43              
44             $conn->write_cb(
45             sub {
46 2         4 my $conn = shift;
47 2         4 my ($message) = @_;
48              
49 2         13 $writer->write($message . "\n");
50              
51 2         72 $limit -= length($message) - 1;
52              
53 2 50       6 if ($limit <= 0) {
54 0         0 $writer->write('');
55 0         0 $writer->close;
56              
57 0         0 $conn->reconnecting;
58             }
59             }
60 2         16 );
61              
62             $conn->close_cb(
63             sub {
64 1         2 my $conn = shift;
65              
66 1         5 $writer->write('');
67 1         33 $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;