Mojo/WebSocketProxy/Parser.pm

filter: Statementstmt 70.4% 31 / 44 Branchbran 41.6% 10 / 24 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 5 / 5 Podpod 100% 1 / 1 totaltotal 56.8% 50 / 88 CC 19 SCAR 32.0
CC
19
Cov
57%
_check_sanity
42.2
_failed_key_value
13.1
parse_req
3.0
CRAP 24.6
↑ Summary Mojo/WebSocketProxy/Backend/JSONRPC.pm »
1 package Mojo::WebSocketProxy::Parser;
2  
3 21 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 21 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 21 our $VERSION = '0.16'; ## VERSION
7  
8 34 ▶sub parse_req {
Subroutine parse_req: called CC=3 SCAR=11.0
Pod documented
9 34 my ($c, $req_storage) = @_;
10  
11 34 my $result;
12 34 my $args = $req_storage->{args};
13 34 ▶ if (ref $args ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args ne "HASH") 1 33 34
14 # for invalid call, eg: not json
15 1 $req_storage->{args} = {};
16 1 $result = $c->wsp_error('error', 'BadRequest', 'The application sent an invalid request.');
17 }
18  
19 34 ▶ $result = _check_sanity($c, $req_storage) unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 33 1 34
20  
21 34 return $result;
22 }
23  
24 33 ▶sub _check_sanity {
Subroutine _check_sanity: called CC=11 SCAR=37.4
25 33 my ($c, $req_storage) = @_;
26  
27 33 my @failed;
28 33 my $args = $req_storage->{args};
29 33 my $config = $c->wsp_config->{config};
30  
31 OUTER:
32 33 foreach my $k (keys %$args) {
33 33 ▶ if (not ref $args->{$k}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $args->{$k}) { } 33 0 33
34 33 ▶ last OUTER if (@failed = _failed_key_value($k, $args->{$k}, $config->{skip_check_sanity}));
Branch: 1 branch
BranchTrueFalseTotal
if @failed = _failed_key_value($k, $args->{$k}, $config->{"skip_check_sanity"}) 0 33 33
35 } else {
36 0 ▶ if (ref $args->{$k} eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args->{$k} eq 'HASH') { } 0 0 0
elsif (ref $args->{$k} eq 'ARRAY') { } 0 0 0
37 0 foreach my $l (keys %{$args->{$k}}) {
38 last OUTER
39 0 ▶ if (@failed = _failed_key_value($l, $args->{$k}->{$l}, $config->{skip_check_sanity}));
Branch: 1 branch
BranchTrueFalseTotal
if @failed = _failed_key_value($l, $args->{$k}{$l}, $config->{"skip_check_sanity"}) 0 0 0
40 }
41 } elsif (ref $args->{$k} eq 'ARRAY') {
42 0 foreach my $l (@{$args->{$k}}) {
43 0 ▶ last OUTER if (@failed = _failed_key_value($k, $l, $config->{skip_check_sanity}));
Branch: 1 branch
BranchTrueFalseTotal
if @failed = _failed_key_value($k, $l, $config->{"skip_check_sanity"}) 0 0 0
44 }
45 }
46 }
47 }
48  
49 33 ▶ if (@failed) {
Branch: 1 branch
BranchTrueFalseTotal
if (@failed) 0 33 33
50 0 my $result = $c->wsp_error('error', 'SanityCheckFailed', 'Parameters sanity check failed.');
51 # emit notification
52 0 $c->tx->emit(sanity_failed => \@failed);
53 0 return $result;
54 }
55 33 return;
56 }
57  
58 33 ▶sub _failed_key_value {
Subroutine _failed_key_value: called CC=7 SCAR=25.7
59 33 my ($key, $value, $skip_check_sanity) = @_;
60  
61 33 my $key_regex = qr/^[A-Za-z0-9_-]{1,50}$/;
62 33 ▶ if ($key !~ /$key_regex/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($key =~ /$key_regex/) 0 33 33
63 0 return ($key, $value);
64 }
65  
66 33 ▶ if ($skip_check_sanity && $key =~ /$skip_check_sanity/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip_check_sanity and $key =~ /$skip_check_sanity/) 0 33 33
Condition33%1/3
$skip_check_sanity and $key =~ /$skip_check_sanity/
!ll&&!rl&&r
3300
MC/DC0%0/2
$skip_check_sanity and $key =~ /$skip_check_sanity/
$skip_check_sanity $key =~ /$skip_check_sanity/
Truth table33%1/3
A and B
A= $skip_check_sanityB= $key =~ /$skip_check_sanity/
ABresult
0X0
100
111
67 0 return;
68 }
69  
70 33 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (not $key =~ /$key_regex/ or $value and not $value =~ /^[\p{Script=Common}\p{Letter}\s\w\@_:!-~]{0,300}$/) 0 33 33
Condition33%2/6
not $key =~ /$key_regex/ or $value and not $value =~ /^[\p{Script=Common}\p{Letter}\s\w\@_:!-~]{0,300}$/
l!l&&r!l&&!r
0033
$value and not $value =~ /^[\p{Script=Common}\p{Letter}\s\w\@_:!-~]{0,300}$/
!ll&&!rl&&r
0330
MC/DC0%0/3
not $key =~ /$key_regex/ or $value and not $value =~ /^[\p{Script=Common}\p{Letter}\s\w\@_:!-~]{0,300}$/
not $key =~ /$key_regex/ $value not $value =~ /^[\p{Script=Common}\p{Letter}\s\w\@_:!-~]{0,300}$/
Truth table25%1/4
not(A) or B and not(C)
A= not $key =~ /$key_regex/B= $valueC= not $value =~ /^[\p{Script=Common}\p{Letter}\s\w\@_:!-~]{0,300}$/
ABCresult
1XX1
0111
00X0
0100
71 $key !~ /$key_regex/
72 # !-~ to allow a range of acceptable characters. To find what is the range, look at ascii table
73  
74 # \p{L} is to ensure we include other Unicode letters outside the ASCII range
75 # \p{Script=Common} is to match double byte characters in Japanese keyboards, eg: '1−1−1'
76 # refer: http://perldoc.perl.org/perlunicode.html and http://perldoc.perl.org/perluniprops.html
77 # null-values are allowed
78 or ($value and $value !~ /^[\p{Script=Common}\p{Letter}\s\w\@_:!-~]{0,300}$/))
79 {
80 0 return ($key, $value);
81 }
82 33 return;
83 }
84  
85 21 1;
86  
87 __END__
↑ Summary Mojo/WebSocketProxy/Backend/JSONRPC.pm »