line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::WebSocketProxy; |
2
|
|
|
|
|
|
|
|
3
|
17
|
|
|
17
|
|
569495
|
use strict; |
|
17
|
|
|
|
|
43
|
|
|
17
|
|
|
|
|
672
|
|
4
|
17
|
|
|
17
|
|
82
|
use warnings; |
|
17
|
|
|
|
|
30
|
|
|
17
|
|
|
|
|
491
|
|
5
|
|
|
|
|
|
|
|
6
|
17
|
|
|
17
|
|
76
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
17
|
|
|
|
|
32
|
|
|
17
|
|
|
|
|
109
|
|
7
|
17
|
|
|
17
|
|
8518
|
use Mojo::WebSocketProxy::Backend; |
|
17
|
|
|
|
|
36
|
|
|
17
|
|
|
|
|
591
|
|
8
|
17
|
|
|
17
|
|
6177
|
use Mojo::WebSocketProxy::Config; |
|
17
|
|
|
|
|
38
|
|
|
17
|
|
|
|
|
120
|
|
9
|
17
|
|
|
17
|
|
6980
|
use Mojo::WebSocketProxy::Dispatcher; |
|
17
|
|
|
|
|
46
|
|
|
17
|
|
|
|
|
166
|
|
10
|
|
|
|
|
|
|
|
11
|
17
|
|
|
17
|
|
6982
|
use Log::Any qw($log); |
|
17
|
|
|
|
|
115088
|
|
|
17
|
|
|
|
|
81
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Other backend types may be available; we default to 'jsonrpc' in the code below |
14
|
17
|
|
|
17
|
|
36864
|
use Mojo::WebSocketProxy::Backend::JSONRPC; |
|
17
|
|
|
|
|
43
|
|
|
17
|
|
|
|
|
9776
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.13'; ## VERSION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub register { |
19
|
22
|
|
|
22
|
1
|
93029
|
my ($self, $app, $config) = @_; |
20
|
|
|
|
|
|
|
|
21
|
22
|
50
|
|
|
|
130
|
die 'No base path found!' unless $config->{base_path}; |
22
|
|
|
|
|
|
|
|
23
|
22
|
|
|
|
|
40
|
my $url_setter; |
24
|
22
|
50
|
33
|
|
|
155
|
$url_setter = delete $config->{url} if $config->{url} and ref($config->{url}) eq 'CODE'; |
25
|
|
|
|
|
|
|
$app->helper( |
26
|
|
|
|
|
|
|
call_rpc => sub { |
27
|
2
|
|
|
2
|
|
42
|
my ($c, $req_storage) = @_; |
28
|
2
|
50
|
33
|
|
|
8
|
$url_setter->($c, $req_storage) if $url_setter && !$req_storage->{url}; |
29
|
2
|
|
|
|
|
8
|
return $c->forward($req_storage); |
30
|
22
|
|
|
|
|
193
|
}); |
31
|
|
|
|
|
|
|
$app->helper( |
32
|
|
|
|
|
|
|
wsp_error => sub { |
33
|
5
|
|
|
5
|
|
57
|
shift; # $c |
34
|
5
|
|
|
|
|
20
|
my ($msg_type, $code, $message, $details) = @_; |
35
|
|
|
|
|
|
|
|
36
|
5
|
|
|
|
|
23
|
my $error = { |
37
|
|
|
|
|
|
|
code => $code, |
38
|
|
|
|
|
|
|
message => $message |
39
|
|
|
|
|
|
|
}; |
40
|
5
|
50
|
33
|
|
|
25
|
$error->{details} = $details if ref($details) eq 'HASH' && keys %$details; |
41
|
|
|
|
|
|
|
|
42
|
5
|
50
|
33
|
|
|
61
|
if ($details && ref($details) ne 'HASH') { |
43
|
0
|
|
|
|
|
0
|
$log->debugf("Details in a websocket error must be a hash reference instead of a %s", ref($details)); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
return { |
47
|
5
|
|
|
|
|
29
|
msg_type => $msg_type, |
48
|
|
|
|
|
|
|
error => $error, |
49
|
|
|
|
|
|
|
}; |
50
|
22
|
|
|
|
|
2476
|
}); |
51
|
|
|
|
|
|
|
|
52
|
22
|
|
|
|
|
1659
|
my $r = $app->routes; |
53
|
22
|
|
|
|
|
277
|
for ($r->under($config->{base_path})) { |
54
|
22
|
|
|
|
|
6896
|
$_->to('Dispatcher#ok', namespace => 'Mojo::WebSocketProxy'); |
55
|
22
|
|
|
|
|
999
|
$_->websocket('/')->to('Dispatcher#open_connection', namespace => 'Mojo::WebSocketProxy'); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
22
|
|
|
|
|
4081
|
my $actions = delete $config->{actions}; |
59
|
22
|
|
|
|
|
184
|
my $dispatcher_config = Mojo::WebSocketProxy::Config->new; |
60
|
22
|
|
|
|
|
189
|
$dispatcher_config->init($config); |
61
|
|
|
|
|
|
|
|
62
|
22
|
50
|
|
|
|
69
|
if (ref $actions eq 'ARRAY') { |
63
|
22
|
|
|
|
|
72
|
for (my $i = 0; $i < @$actions; $i++) { |
64
|
30
|
|
|
|
|
107
|
$dispatcher_config->add_action($actions->[$i], $i); |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
} else { |
67
|
0
|
|
|
|
|
0
|
die 'No actions found!'; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
# For backwards compatibility, we always want to add a plain JSON::RPC backend |
71
|
|
|
|
|
|
|
$dispatcher_config->add_backend( |
72
|
|
|
|
|
|
|
default => Mojo::WebSocketProxy::Backend->backend_instance( |
73
|
|
|
|
|
|
|
jsonrpc => url => delete $config->{url}, |
74
|
22
|
50
|
|
|
|
290
|
)) unless exists $config->{backends}{default}; |
75
|
|
|
|
|
|
|
|
76
|
22
|
50
|
|
|
|
75
|
if (my $backend_configs = delete $config->{backends}) { |
77
|
22
|
|
|
|
|
95
|
foreach my $name (keys %$backend_configs) { |
78
|
2
|
|
|
|
|
3
|
my %args = %{$backend_configs->{$name}}; |
|
2
|
|
|
|
|
6
|
|
79
|
2
|
|
100
|
|
|
8
|
my $type = delete($args{type}) // 'jsonrpc'; |
80
|
2
|
|
|
|
|
7
|
$dispatcher_config->add_backend($name => Mojo::WebSocketProxy::Backend->backend_instance($type => %args)); |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
$app->helper( |
85
|
|
|
|
|
|
|
wsp_config => sub { |
86
|
256
|
|
|
256
|
|
2101
|
my $c = shift; |
87
|
256
|
|
|
|
|
773
|
return $dispatcher_config; |
88
|
22
|
|
|
|
|
209
|
}); |
89
|
|
|
|
|
|
|
|
90
|
22
|
|
|
|
|
1729
|
return; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__ |