line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Plack::Middleware::SocketIO::XHRMultipart; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
26
|
|
4
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
29
|
|
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
4
|
use base 'Plack::Middleware::SocketIO::Base'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
67
|
|
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
4
|
use Plack::Middleware::SocketIO::Handle; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
547
|
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub new { |
11
|
0
|
|
|
0
|
1
|
|
my $self = shift->SUPER::new(@_); |
12
|
|
|
|
|
|
|
|
13
|
0
|
|
0
|
|
|
|
$self->{boundary} ||= 'socketio'; |
14
|
|
|
|
|
|
|
|
15
|
0
|
|
|
|
|
|
return $self; |
16
|
|
|
|
|
|
|
} |
17
|
|
|
|
|
|
|
|
18
|
0
|
|
|
0
|
1
|
|
sub name {'xhr-multipart'} |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
sub finalize { |
21
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
22
|
0
|
|
|
|
|
|
my ($cb) = @_; |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
my $req = $self->req; |
25
|
0
|
|
|
|
|
|
my $name = $self->name; |
26
|
0
|
|
|
|
|
|
my $resource = $self->resource; |
27
|
|
|
|
|
|
|
|
28
|
0
|
0
|
|
|
|
|
return $self->_finalize_stream($req, $cb) if $req->method eq 'GET'; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
return |
31
|
0
|
0
|
0
|
|
|
|
unless $req->method eq 'POST' |
32
|
|
|
|
|
|
|
&& $req->path =~ m{^/$resource/$name/(\d+)/send$}; |
33
|
|
|
|
|
|
|
|
34
|
0
|
|
|
|
|
|
return $self->_finalize_send($req, $1); |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub _finalize_stream { |
38
|
0
|
|
|
0
|
|
|
my $self = shift; |
39
|
0
|
|
|
|
|
|
my ($req, $cb) = @_; |
40
|
|
|
|
|
|
|
|
41
|
0
|
|
|
|
|
|
my $handle = $self->_build_handle($req->env->{'psgix.io'}); |
42
|
0
|
0
|
|
|
|
|
return unless $handle; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
return sub { |
45
|
0
|
|
|
0
|
|
|
my $respond = shift; |
46
|
|
|
|
|
|
|
|
47
|
0
|
|
|
|
|
|
my $boundary = $self->{boundary}; |
48
|
|
|
|
|
|
|
|
49
|
0
|
|
|
|
|
|
my $conn = $self->add_connection(on_connect => $cb); |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
$conn->on_write( |
52
|
|
|
|
|
|
|
sub { |
53
|
0
|
|
|
|
|
|
my $self = shift; |
54
|
0
|
|
|
|
|
|
my ($message) = @_; |
55
|
|
|
|
|
|
|
|
56
|
0
|
|
|
|
|
|
my $string = ''; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
$string .= "Content-Type: text/plain\x0a\x0a"; |
59
|
0
|
0
|
|
|
|
|
if ($message eq '') { |
60
|
0
|
|
|
|
|
|
$string .= "-1--$boundary--\x0a"; |
61
|
|
|
|
|
|
|
} |
62
|
|
|
|
|
|
|
else { |
63
|
0
|
|
|
|
|
|
$string .= "$message\x0a--$boundary\x0a"; |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
0
|
|
|
|
|
|
$handle->write($string); |
67
|
|
|
|
|
|
|
} |
68
|
0
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
0
|
|
|
|
|
|
$handle->heartbeat_timeout(10); |
71
|
0
|
|
|
|
|
|
$handle->on_heartbeat(sub { $conn->send_heartbeat }); |
|
0
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$handle->on_eof( |
74
|
|
|
|
|
|
|
sub { |
75
|
0
|
|
|
|
|
|
my $handle = shift; |
76
|
|
|
|
|
|
|
|
77
|
0
|
|
|
|
|
|
$handle->close; |
78
|
|
|
|
|
|
|
|
79
|
0
|
|
|
|
|
|
$self->client_disconnected($conn); |
80
|
|
|
|
|
|
|
} |
81
|
0
|
|
|
|
|
|
); |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
$handle->on_error( |
84
|
|
|
|
|
|
|
sub { |
85
|
0
|
|
|
|
|
|
$self->client_disconnected($conn); |
86
|
|
|
|
|
|
|
|
87
|
0
|
|
|
|
|
|
$handle->close; |
88
|
|
|
|
|
|
|
} |
89
|
0
|
|
|
|
|
|
); |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
$handle->write( |
92
|
|
|
|
|
|
|
join "\x0d\x0a" => 'HTTP/1.1 200 OK', |
93
|
|
|
|
|
|
|
qq{Content-Type: multipart/x-mixed-replace;boundary="$boundary"}, |
94
|
|
|
|
|
|
|
'Connection: keep-alive', '', '' |
95
|
|
|
|
|
|
|
); |
96
|
|
|
|
|
|
|
|
97
|
0
|
|
|
|
|
|
$conn->send_id_message($conn->id); |
98
|
|
|
|
|
|
|
|
99
|
0
|
|
|
|
|
|
$self->client_connected($conn); |
100
|
0
|
|
|
|
|
|
}; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub _finalize_send { |
104
|
0
|
|
|
0
|
|
|
my $self = shift; |
105
|
0
|
|
|
|
|
|
my ($req, $id) = @_; |
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
my $conn = $self->find_connection_by_id($id); |
108
|
0
|
0
|
|
|
|
|
return unless $conn; |
109
|
|
|
|
|
|
|
|
110
|
0
|
|
|
|
|
|
my $retval = [ |
111
|
|
|
|
|
|
|
200, |
112
|
|
|
|
|
|
|
['Content-Type' => 'text/plain', 'Transfer-Encoding' => 'chunked'], |
113
|
|
|
|
|
|
|
["2\x0d\x0aok\x0d\x0a" . "0\x0d\x0a\x0d\x0a"] |
114
|
|
|
|
|
|
|
]; |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
|
|
|
my $data = $req->body_parameters->get('data'); |
117
|
|
|
|
|
|
|
|
118
|
0
|
|
|
|
|
|
$conn->read($data); |
119
|
|
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
return $retval; |
121
|
|
|
|
|
|
|
} |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
1; |
124
|
|
|
|
|
|
|
__END__ |