Mojo/WebSocketProxy/Dispatcher.pm

filter: Statementstmt 91.1% 154 / 169 Branchbran 66.6% 28 / 42 Conditioncond 57.6% 15 / 26 MC/DCmcdc 26.6% 4 / 15 Subroutinesub 94.4% 34 / 36 Podpod 100% 7 / 7 totaltotal 82.0% 242 / 295 CC 37 SCAR 13.9
CC
37
Cov
83%
__ANON__
8.8
forward
7.8
open_connection
7.4
CRAP 4.0
« Mojo/WebSocketProxy/Config.pm ↑ Summary Mojo/WebSocketProxy/Backend.pm »
1 package Mojo::WebSocketProxy::Dispatcher;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use Mojo::Base 'Mojolicious::Controller';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use Mojo::WebSocketProxy::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use Mojo::WebSocketProxy::Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 20 ▶use Class::Method::Modifiers;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 20 ▶use JSON::MaybeUTF8 qw(:v1);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 20 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 20 ▶use Unicode::Normalize ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15 20 ▶use Future::Mojo 0.004; # ->new_timeout
Subroutine BEGIN: called CC=1 SCAR=0.0
16 20 ▶use Future::Utils qw(fmap);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 20 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 20 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 20 ▶use DataDog::DogStatsd::Helper qw(stats_inc);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 20 ▶use constant TIMEOUT => $ENV{MOJO_WEBSOCKETPROXY_TIMEOUT} || 15;
Subroutine BEGIN: called CC=2 SCAR=6.9
Condition100%2/2
$ENV{'MOJO_WEBSOCKETPROXY_TIMEOUT'} || 15
l!l
119
MC/DC100%1/1
$ENV{'MOJO_WEBSOCKETPROXY_TIMEOUT'} || 15
$ENV{'MOJO_WEBSOCKETPROXY_TIMEOUT'}
Truth table100%2/2
A
A= $ENV{'MOJO_WEBSOCKETPROXY_TIMEOUT'}
Aresult
11
00
22 20 ▶use Mojo::WebSocketProxy::RequestLogger;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 20 our $VERSION = '0.16'; ## VERSION
25 35 ▶around 'send' => sub {
Subroutine __ANON__: called CC=7 SCAR=21.8
26 35 my ($orig, $c, $api_response, $req_storage) = @_;
27  
28 35 my $config = $c->wsp_config->{config};
29  
30 35 my $max_response_size = $config->{max_response_size};
31 35 ▶ if ($max_response_size && length(encode_json_utf8($api_response)) > $max_response_size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($max_response_size and length encode_json_utf8($api_response) > $max_response_size) 0 35 35
Condition33%1/3
$max_response_size and length encode_json_utf8($api_response) > $max_response_size
!ll&&!rl&&r
3500
MC/DC0%0/2
$max_response_size and length encode_json_utf8($api_response) > $max_response_size
$max_response_size length encode_json_utf8($api_response) > $max_response_size
Truth table33%1/3
A and B
A= $max_response_sizeB= length encode_json_utf8($api_response) > $max_response_size
ABresult
0X0
100
111
32 0 $api_response->{json} = $c->wsp_error('error', 'ResponseTooLarge', 'Response too large.');
33 }
34  
35 35 my $before_send_api_response = $config->{before_send_api_response};
36 $_->($c, $req_storage, $api_response->{json})
37 0 ▶ for grep { $_ } (ref $before_send_api_response eq 'ARRAY' ? @{$before_send_api_response} : $before_send_api_response);
Branch: 1 branch
BranchTrueFalseTotal
ref $before_send_api_response eq 'ARRAY' ? : 0 35 35
38  
39 35 my $ret = $orig->($c, $api_response);
40  
41 35 my $after_sent_api_response = $config->{after_sent_api_response};
42 0 ▶ $_->($c, $req_storage) for grep { $_ } (ref $after_sent_api_response eq 'ARRAY' ? @{$after_sent_api_response} : $after_sent_api_response);
Branch: 1 branch
BranchTrueFalseTotal
ref $after_sent_api_response eq 'ARRAY' ? : 0 35 35
43  
44 35 return $ret;
45 20 };
46  
47 25 ▶sub ok {
Subroutine ok: called CC=1 SCAR=0.0
Pod documented
48 25 return 1;
49 }
50  
51 25 ▶sub open_connection {
Subroutine open_connection: called CC=6 SCAR=20.0
Pod documented
52 25 my ($c) = @_;
53  
54 25 ▶ my $remote_address = $c->tx->remote_address // 'undef';
Condition50%1/2
$c->tx->remote_address // "undef"
l!l
250
MC/DC0%0/1
$c->tx->remote_address // "undef"
$c->tx->remote_address
Truth table50%1/2
A
A= $c->tx->remote_address
Aresult
11
00
55 25 $log->debugf('accepting a websocket connection from %s', $remote_address);
56 # Enable permessage-deflate
57 25 $c->tx->with_compression;
58  
59 25 my $config = $c->wsp_config->{config};
60  
61 25 ▶ Mojo::IOLoop->singleton->stream($c->tx->connection)->timeout($config->{stream_timeout}) if $config->{stream_timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"stream_timeout"} 0 25 25
62 25 ▶ Mojo::IOLoop->singleton->max_connections($config->{max_connections}) if $config->{max_connections};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"max_connections"} 0 25 25
63  
64 25 ▶ $config->{opened_connection}->($c) if $config->{opened_connection};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"opened_connection"} 0 25 25
65  
66 $c->on(
67 34 ▶ text => sub {
Subroutine __ANON__: called CC=4 SCAR=16.6
68 34 my ($c, $msg) = @_;
69  
70 34 my $original = "$msg";
71 # Incoming data will be JSON-formatted text, as a Unicode string.
72 # We normalize the entire string before decoding.
73  
74 34 ▶ my $decoded = eval { Encode::decode_utf8($msg, Encode::FB_CROAK) } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $decoded = eval { do { &Encode::decode_utf8($msg, 1) } } or do { $c->tx->emit("encoding_error", _get_error_details("code", "INVALID_UTF8", "reason", "Malformed UTF-8 data", "message", $msg)); return } 34 0 34
75 0 $c->tx->emit(
76 encoding_error => _get_error_details(
77 code => 'INVALID_UTF8',
78 reason => 'Malformed UTF-8 data',
79 message => $msg
80 ));
81 0 return;
82 };
83  
84 # The Unicode::Normalize::NFC check is added as a safety net. However, the error is not triggered so far.
85 34 ▶ my $normalized_msg = eval { Unicode::Normalize::NFC($decoded) } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $normalized_msg = eval { do { &Unicode::Normalize::NFC($decoded) } } or do { $c->tx->emit("encoding_error", _get_error_details("code", "INVALID_UNICODE", "reason", "Malformed Unicode data", "message", $msg)); return } 34 0 34
86 0 $c->tx->emit(
87 encoding_error => _get_error_details(
88 code => 'INVALID_UNICODE',
89 reason => 'Malformed Unicode data',
90 message => $msg
91 ));
92 0 return;
93 };
94  
95 34 ▶ my $args = eval { decode_json_text($normalized_msg); } or do {
Branch: 1 branch
BranchTrueFalseTotal
my $args = eval { do { decode_json_text($normalized_msg) } } or do { $c->tx->emit("encoding_error", _get_error_details("code", "INVALID_JSON", "reason", "Malformed JSON data", "message", $msg)); return } 34 0 34
96 0 $c->tx->emit(
97 encoding_error => _get_error_details(
98 code => 'INVALID_JSON',
99 reason => 'Malformed JSON data',
100 message => $msg
101 ));
102 0 return;
103 };
104  
105 34 on_message($c, $args);
106 25 });
107  
108 $c->on(
109 1 ▶ binary => sub {
Subroutine __ANON__: called CC=3 SCAR=13.1
110 1 my ($d, $bytes) = @_;
111 1 ▶ $config->{binary_frame}(@_) if $bytes and exists($config->{binary_frame});
Branch: 1 branch
BranchTrueFalseTotal
if $bytes and exists $config->{"binary_frame"} 1 0 1
Condition33%1/3
$bytes and exists $config->{"binary_frame"}
!ll&&!rl&&r
001
MC/DC0%0/2
$bytes and exists $config->{"binary_frame"}
$bytes exists $config->{"binary_frame"}
Truth table33%1/3
A and B
A= $bytesB= exists $config->{"binary_frame"}
ABresult
0X0
100
111
112 25 });
113  
114 25 ▶ $c->on(finish => $config->{finish_connection}) if $config->{finish_connection};
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"finish_connection"} 0 25 25
115  
116 25 return;
117 }
118  
119 34 ▶sub on_message {
Subroutine on_message: called CC=5 SCAR=16.1
Pod documented
120 34 my ($c, $args) = @_;
121  
122 34 my $config = $c->wsp_config->{config};
123  
124 34 my $req_storage = {};
125 34 $req_storage->{args} = $args;
126  
127 34 $req_storage->{logger} = Mojo::WebSocketProxy::RequestLogger->new;
128 # We still want to run any hooks even for invalid requests.
129 34 ▶ if (my $err = Mojo::WebSocketProxy::Parser::parse_req($c, $req_storage)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $err = Mojo::WebSocketProxy::Parser::parse_req($c, $req_storage)) 1 33 34
130 1 $c->send({json => $err}, $req_storage);
131 1 ▶ return $c->_run_hooks($config->{after_dispatch} || [])->retain;
Condition50%1/2
$config->{'after_dispatch'} || []
l!l
01
MC/DC0%0/1
$config->{'after_dispatch'} || []
$config->{'after_dispatch'}
Truth table50%1/2
A
A= $config->{'after_dispatch'}
Aresult
11
00
132 }
133  
134 33 ▶ my $action = $c->dispatch($args) or do {
Branch: 1 branch
BranchTrueFalseTotal
my $action = $c->dispatch($args) or do { my $err = $c->wsp_error("error", "UnrecognisedRequest", "Unrecognised request"); $c->send({"json", $err}, $req_storage); return $c->_run_hooks($config->{"after_dispatch"} || [])->retain } 31 2 33
135 2 my $err = $c->wsp_error('error', UnrecognisedRequest => 'Unrecognised request');
136 2 $c->send({json => $err}, $req_storage);
137 2 ▶ return $c->_run_hooks($config->{after_dispatch} || [])->retain;
Condition50%1/2
$config->{'after_dispatch'} || []
l!l
02
MC/DC0%0/1
$config->{'after_dispatch'} || []
$config->{'after_dispatch'}
Truth table50%1/2
A
A= $config->{'after_dispatch'}
Aresult
11
00
138 };
139  
140 31 @{$req_storage}{keys %$action} = (values %$action);
141 31 $req_storage->{method} = $req_storage->{name};
142  
143 # main processing pipeline
144 my $f = $c->before_forward($req_storage)->transform(
145 23 ▶ done => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
146 # Note that we completely ignore the return value of ->before_forward here.
147 23 ▶ return $req_storage->{instead_of_forward}->($c, $req_storage) if $req_storage->{instead_of_forward};
Branch: 1 branch
BranchTrueFalseTotal
if $req_storage->{"instead_of_forward"} 2 21 23
148 21 return $c->forward($req_storage);
149 }
150 )->then(
151 23 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
152 23 my $result = shift;
153 23 ▶ return $c->after_forward($result, $req_storage)->transform(done => sub { $result });
Subroutine __ANON__: called CC=1 SCAR=0.0
154 },
155 7 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
156 7 my $result = shift;
157 7 Future->done($result);
158 31 });
159  
160 return Future->wait_any(
161 Future::Mojo->new_timeout(TIMEOUT)->else(
162 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
163 1 return Future->done($c->wsp_error('error', Timeout => 'Timeout'));
164 }
165 ),
166 $f
167 )->then(
168 31 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.1
169 31 my ($result) = @_;
170 31 ▶ $c->send({json => $result}, $req_storage) if $result;
Branch: 1 branch
BranchTrueFalseTotal
if $result 8 23 31
171 31 ▶ return $c->_run_hooks($config->{after_dispatch} || []);
Condition50%1/2
$config->{'after_dispatch'} || []
l!l
031
MC/DC0%0/1
$config->{'after_dispatch'} || []
$config->{'after_dispatch'}
Truth table50%1/2
A
A= $config->{'after_dispatch'}
Aresult
11
00
172 }
173 )->on_fail(
174 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
175 0 $c->app->log->error("An error occurred handling on_message. Error @_");
176 31 })->retain;
177 }
178  
179 31 ▶sub before_forward {
Subroutine before_forward: called CC=3 SCAR=11.0
Pod documented
180 31 my ($c, $req_storage) = @_;
181  
182 31 my $config = $c->wsp_config->{config};
183  
184 31 my $before_forward_hooks = [];
185  
186 # Global hooks are always first
187 31 for ($config, $req_storage) {
188 62 ▶ push @$before_forward_hooks, ref($_->{before_forward}) eq 'ARRAY' ? @{$_->{before_forward}} : $_->{before_forward};
Branch: 1 branch
BranchTrueFalseTotal
ref $_->{'before_forward'} eq 'ARRAY' ? : 7 55 62
189 }
190  
191 # We always want to clear these after every request.
192 31 delete $req_storage->{before_forward};
193  
194 31 return $c->_run_hooks($before_forward_hooks, $req_storage);
195 }
196  
197 23 ▶sub after_forward {
Subroutine after_forward: called CC=2 SCAR=7.1
Pod documented
198 23 my ($c, $result, $req_storage) = @_;
199  
200 23 my $config = $c->wsp_config->{config};
201 23 ▶ return $c->_run_hooks($config->{after_forward} || [], $result, $req_storage);
Condition50%1/2
$config->{'after_forward'} || []
l!l
023
MC/DC0%0/1
$config->{'after_forward'} || []
$config->{'after_forward'}
Truth table50%1/2
A
A= $config->{'after_forward'}
Aresult
11
00
202 }
203  
204 88 ▶sub _run_hooks {
Subroutine _run_hooks: called CC=1 SCAR=0.0
205 88 my @hook_params = @_;
206 88 my $c = shift @hook_params;
207 88 my $hooks = shift @hook_params;
208  
209 19 ▶ my $result_f = fmap {
Subroutine __ANON__: called CC=4 SCAR=13.9
210 19 my $hook = shift;
211 19 ▶ my $result = $hook->($c, @hook_params) or return Future->done;
Branch: 1 branch
BranchTrueFalseTotal
my $result = &$hook($c, @hook_params) or return "Future"->done 9 10 19
212 9 ▶ return $result if blessed($result) && $result->isa('Future');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($result) and $result->isa("Future") 4 5 9
Condition66%2/3
builtin::blessed($result) and $result->isa("Future")
!ll&&!rl&&r
504
MC/DC50%1/2
builtin::blessed($result) and $result->isa("Future")
builtin::blessed($result) $result->isa("Future")
Truth table66%2/3
A and B
A= builtin::blessed($result)B= $result->isa("Future")
ABresult
0X0
100
111
213 5 return Future->fail($result);
214 }
215 88 foreach => [grep { defined } @$hooks],
216 concurrent => 1;
217 88 return $result_f;
218 }
219  
220 33 ▶sub dispatch {
Subroutine dispatch: called CC=1 SCAR=0.0
Pod documented
221 33 my ($c, $args) = @_;
222  
223 33 my $log = $c->app->log;
224 33 $log->debug("websocket got json " . $c->dumper($args));
225  
226 my ($action) =
227 0 sort { $a->{order} <=> $b->{order} }
228 33 grep { defined }
229 33 map { $c->wsp_config->{actions}->{$_} } keys %$args;
230  
231 33 return $action;
232 }
233  
234 23 ▶sub forward {
Subroutine forward: called CC=7 SCAR=20.5
Pod documented
235 23 my ($c, $req_storage) = @_;
236  
237 23 my $config = $c->wsp_config->{config};
238  
239 23 for my $hook (qw/ before_call before_get_rpc_response after_got_rpc_response /) {
240 $req_storage->{$hook} = [
241 0 ▶ grep { $_ } (ref $config->{$hook} eq 'ARRAY' ? @{$config->{$hook}} : $config->{$hook}),
Branch: 1 branch
BranchTrueFalseTotal
ref $config->{$hook} eq 'ARRAY' ? : 0 69 69
242 0 ▶ grep { $_ } (ref $req_storage->{$hook} eq 'ARRAY' ? @{$req_storage->{$hook}} : $req_storage->{$hook}),
Branch: 1 branch
BranchTrueFalseTotal
ref $req_storage->{$hook} eq 'ARRAY' ? : 0 69 69
243 ];
244 }
245  
246 # default to config (generic) only if call specific is not defined
247 23 ▶ $req_storage->{rpc_failure_cb} //= $config->{rpc_failure_cb};
Condition66%2/3
$req_storage->{"rpc_failure_cb"} //= $config->{"rpc_failure_cb"}
l!l&&r!l&&!r
3020
MC/DC50%1/2
$req_storage->{"rpc_failure_cb"} //= $config->{"rpc_failure_cb"}
$req_storage->{"rpc_failure_cb"} $config->{"rpc_failure_cb"}
Truth table66%2/3
A //= B
A= $req_storage->{"rpc_failure_cb"}B= $config->{"rpc_failure_cb"}
ABresult
1X1
011
000
248  
249 23 ▶ my $backend_name = $req_storage->{backend} // "default";
Condition100%2/2
$req_storage->{"backend"} // "default"
l!l
122
MC/DC100%1/1
$req_storage->{"backend"} // "default"
$req_storage->{"backend"}
Truth table100%2/2
A
A= $req_storage->{"backend"}
Aresult
11
00
250 23 ▶ my $backend = $c->wsp_config->{backends}{$backend_name}
Branch: 1 branch
BranchTrueFalseTotal
my $backend = $c->wsp_config->{"backends"}{$backend_name} or die "Cannot dispatch request - no backend named '${backend_name}'" 23 0 23
251 or die "Cannot dispatch request - no backend named '$backend_name'";
252  
253 23 $backend->call_rpc($c, $req_storage);
254  
255 23 return;
256 }
257  
258 0 ▶sub _get_error_details {
Subroutine _get_error_details: not called CC=1 SCAR=6.9
259 0 my (%args) = @_;
260  
261 return {
262 error => 'Error Processing Request',
263 details => {
264 error_code => $args{code},
265 reason => $args{reason},
266 request_body => $args{message},
267 },
268 0 };
269 }
270  
271 20 1;
272  
273 __END__
« Mojo/WebSocketProxy/Config.pm ↑ Summary Mojo/WebSocketProxy/Backend.pm »