line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::WebSocketProxy; |
2
|
|
|
|
|
|
|
|
3
|
17
|
|
|
17
|
|
565936
|
use strict; |
|
17
|
|
|
|
|
40
|
|
|
17
|
|
|
|
|
459
|
|
4
|
17
|
|
|
17
|
|
89
|
use warnings; |
|
17
|
|
|
|
|
28
|
|
|
17
|
|
|
|
|
444
|
|
5
|
|
|
|
|
|
|
|
6
|
17
|
|
|
17
|
|
75
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
17
|
|
|
|
|
32
|
|
|
17
|
|
|
|
|
95
|
|
7
|
17
|
|
|
17
|
|
8649
|
use Mojo::WebSocketProxy::Backend; |
|
17
|
|
|
|
|
44
|
|
|
17
|
|
|
|
|
601
|
|
8
|
17
|
|
|
17
|
|
6075
|
use Mojo::WebSocketProxy::Config; |
|
17
|
|
|
|
|
50
|
|
|
17
|
|
|
|
|
122
|
|
9
|
17
|
|
|
17
|
|
6789
|
use Mojo::WebSocketProxy::Dispatcher; |
|
17
|
|
|
|
|
48
|
|
|
17
|
|
|
|
|
183
|
|
10
|
|
|
|
|
|
|
|
11
|
17
|
|
|
17
|
|
7046
|
use Log::Any qw($log); |
|
17
|
|
|
|
|
113088
|
|
|
17
|
|
|
|
|
81
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# Other backend types may be available; we default to 'jsonrpc' in the code below |
14
|
17
|
|
|
17
|
|
36235
|
use Mojo::WebSocketProxy::Backend::JSONRPC; |
|
17
|
|
|
|
|
41
|
|
|
17
|
|
|
|
|
9581
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.12'; ## VERSION |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
sub register { |
19
|
22
|
|
|
22
|
1
|
95424
|
my ($self, $app, $config) = @_; |
20
|
|
|
|
|
|
|
|
21
|
22
|
50
|
|
|
|
95
|
die 'No base path found!' unless $config->{base_path}; |
22
|
|
|
|
|
|
|
|
23
|
22
|
|
|
|
|
46
|
my $url_setter; |
24
|
22
|
50
|
33
|
|
|
153
|
$url_setter = delete $config->{url} if $config->{url} and ref($config->{url}) eq 'CODE'; |
25
|
|
|
|
|
|
|
$app->helper( |
26
|
|
|
|
|
|
|
call_rpc => sub { |
27
|
2
|
|
|
2
|
|
57
|
my ($c, $req_storage) = @_; |
28
|
2
|
50
|
33
|
|
|
16
|
$url_setter->($c, $req_storage) if $url_setter && !$req_storage->{url}; |
29
|
2
|
|
|
|
|
12
|
return $c->forward($req_storage); |
30
|
22
|
|
|
|
|
206
|
}); |
31
|
|
|
|
|
|
|
$app->helper( |
32
|
|
|
|
|
|
|
wsp_error => sub { |
33
|
5
|
|
|
5
|
|
66
|
shift; # $c |
34
|
5
|
|
|
|
|
19
|
my ($msg_type, $code, $message, $details) = @_; |
35
|
|
|
|
|
|
|
|
36
|
5
|
|
|
|
|
22
|
my $error = { |
37
|
|
|
|
|
|
|
code => $code, |
38
|
|
|
|
|
|
|
message => $message |
39
|
|
|
|
|
|
|
}; |
40
|
5
|
50
|
33
|
|
|
27
|
$error->{details} = $details if ref($details) eq 'HASH' && keys %$details; |
41
|
|
|
|
|
|
|
|
42
|
5
|
50
|
33
|
|
|
23
|
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
|
|
|
|
|
71
|
msg_type => $msg_type, |
48
|
|
|
|
|
|
|
error => $error, |
49
|
|
|
|
|
|
|
}; |
50
|
22
|
|
|
|
|
2476
|
}); |
51
|
|
|
|
|
|
|
|
52
|
22
|
|
|
|
|
1437
|
my $r = $app->routes; |
53
|
22
|
|
|
|
|
298
|
for ($r->under($config->{base_path})) { |
54
|
22
|
|
|
|
|
7241
|
$_->to('Dispatcher#ok', namespace => 'Mojo::WebSocketProxy'); |
55
|
22
|
|
|
|
|
888
|
$_->websocket('/')->to('Dispatcher#open_connection', namespace => 'Mojo::WebSocketProxy'); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
22
|
|
|
|
|
4068
|
my $actions = delete $config->{actions}; |
59
|
22
|
|
|
|
|
183
|
my $dispatcher_config = Mojo::WebSocketProxy::Config->new; |
60
|
22
|
|
|
|
|
194
|
$dispatcher_config->init($config); |
61
|
|
|
|
|
|
|
|
62
|
22
|
50
|
|
|
|
75
|
if (ref $actions eq 'ARRAY') { |
63
|
22
|
|
|
|
|
79
|
for (my $i = 0; $i < @$actions; $i++) { |
64
|
30
|
|
|
|
|
102
|
$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
|
|
|
|
309
|
)) unless exists $config->{backends}{default}; |
75
|
|
|
|
|
|
|
|
76
|
22
|
50
|
|
|
|
90
|
if (my $backend_configs = delete $config->{backends}) { |
77
|
22
|
|
|
|
|
65
|
foreach my $name (keys %$backend_configs) { |
78
|
2
|
|
|
|
|
4
|
my %args = %{$backend_configs->{$name}}; |
|
2
|
|
|
|
|
9
|
|
79
|
2
|
|
100
|
|
|
7
|
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
|
|
|
|
|
805
|
return $dispatcher_config; |
88
|
22
|
|
|
|
|
205
|
}); |
89
|
|
|
|
|
|
|
|
90
|
22
|
|
|
|
|
1651
|
return; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
1; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
__END__ |