blib/lib/Web/Dispatch/Parser.pm

filter: Statementstmt 92.1% 118 / 128 Branchbran 83.6% 87 / 104 Conditioncond 73.0% 19 / 26 MC/DCmcdc 50.0% 7 / 14 Subroutinesub 86.6% 13 / 15 Podpod 0.0% 0 / 2 totaltotal 84.4% 244 / 289 CC 70 SCAR 24.7
CC
70
Cov
87%
_url_path_match
20.2
_parse_spec
13.1
_parse_param_handler
12.4
CRAP 11.8
« blib/lib/Web/Simple/HackedPlack.pm ↑ Summary blib/lib/Web/Simple/Application.pm »
1 package Web::Dispatch::Parser;
2  
3 sub DEBUG () { 0 }
4  
5 16 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.4
6 16 ▶ if ($ENV{WEB_DISPATCH_PARSER_DEBUG}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"WEB_DISPATCH_PARSER_DEBUG"}) 0 16 16
7 16 ▶ no warnings 'redefine';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 *DEBUG = sub () { 1 }
9 0 }
10 }
11  
12 16 ▶use Sub::Quote;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 16 ▶use Web::Dispatch::Predicates;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 16 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 16 has _cache => (
17 is => 'lazy', default => quote_sub q{ {} }
18 );
19  
20 0 ▶sub diag { if (DEBUG) { warn $_[0] } }
Subroutine diag: not called CC=1 SCAR=6.9
Pod undocumented
21  
22 0 ▶sub _wtf {
Subroutine _wtf: not called CC=2 SCAR=17.9
23 0 my ($self, $error) = @_;
24 0 ▶ my $hat = (' ' x (pos||0)).'^';
Condition0%0/2
pos $_ || 0
l!l
00
MC/DC0%0/1
pos $_ || 0
pos $_
Truth table0%0/2
A
A= pos $_
Aresult
11
00
25 0 warn "Warning parsing dispatch specification: ${error}\n
26 ${_}
27 ${hat} here\n";
28 }
29  
30 1 ▶sub _blam {
Subroutine _blam: called CC=2 SCAR=7.1
31 1 my ($self, $error) = @_;
32 1 ▶ my $hat = (' ' x (pos||0)).'^';
Condition50%1/2
pos $_ || 0
l!l
10
MC/DC0%0/1
pos $_ || 0
pos $_
Truth table50%1/2
A
A= pos $_
Aresult
11
00
33 1 die "Error parsing dispatch specification: ${error}\n
34 ${_}
35 ${hat} here\n";
36 }
37  
38 252 ▶sub parse {
Subroutine parse: called CC=2 SCAR=7.0
Pod undocumented
39 252 my ($self, $spec) = @_;
40 252 $spec =~ s/\s+//g; # whitespace is not valid
41 252 ▶ return $self->_cache->{$spec} ||= $self->_parse_spec($spec);
Condition66%2/3
$self->_cache->{$spec} ||= $self->_parse_spec($spec)
l!l&&r!l&&!r
1101420
MC/DC0%0/2
$self->_cache->{$spec} ||= $self->_parse_spec($spec)
$self->_cache->{$spec} $self->_parse_spec($spec)
Truth table66%2/3
A ||= B
A= $self->_cache->{$spec}B= $self->_parse_spec($spec)
ABresult
1X1
011
000
42 }
43  
44 146 ▶sub _parse_spec {
Subroutine _parse_spec: called CC=13 SCAR=25.8
45 146 my ($self, $spec, $nested) = @_;
46 146 ▶ return match_true() unless length($spec);
Branch: 1 branch
BranchTrueFalseTotal
unless length $spec 3 143 146
47 143 for ($_[1]) {
48 143 my @match;
49 my $close;
50 143 ▶ PARSE: { do {
Branch: 1 branch
BranchTrueFalseTotal
pos $_ == length $_ or do { do { push @match, $self->_parse_spec_section($_) or $self->_blam("Unable to work out what the next section is"); if (/\G\)/cg) { $self->_blam("Found closing ) with no opening (") unless $nested; $close = 1; last PARSE; } ; last PARSE if pos $_ == length $_; $match[-1] = $self->_parse_spec_combinator($_, $match[-1]) or $self->_blam("No valid combinator - expected + or |") } } 4 69 73
51 212 ▶ push @match, $self->_parse_spec_section($_)
Branch: 1 branch
BranchTrueFalseTotal
push @match, $self->_parse_spec_section($_) or $self->_blam("Unable to work out what the next section is") 211 0 211
52 or $self->_blam("Unable to work out what the next section is");
53 211 ▶ if (/\G\)/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\)/cg) 3 208 211
54 3 ▶ $self->_blam("Found closing ) with no opening (") unless $nested;
Branch: 1 branch
BranchTrueFalseTotal
unless $nested 0 3 3
55 3 $close = 1;
56 3 last PARSE;
57 }
58 208 ▶ last PARSE if (pos == length);
Branch: 1 branch
BranchTrueFalseTotal
if pos $_ == length $_ 135 73 208
59 73 ▶ $match[-1] = $self->_parse_spec_combinator($_, $match[-1])
Branch: 1 branch
BranchTrueFalseTotal
$match[-1] = $self->_parse_spec_combinator($_, $match[-1]) or $self->_blam("No valid combinator - expected + or |") 73 0 73
60 or $self->_blam('No valid combinator - expected + or |');
61 } until (pos == length) }; # accept trailing whitespace
62 142 ▶ if (!$close and $nested and pos == length) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $close and $nested and pos $_ == length $_) 1 141 142
Condition83%5/6
not $close and $nested and pos $_ == length $_
!ll&&!rl&&r
14101
not $close and $nested
!ll&&!rl&&r
31381
MC/DC66%2/3
not $close and $nested and pos $_ == length $_
not $close $nested pos $_ == length $_
Truth table75%3/4
not(A) and B and C
A= not $closeB= $nestedC= pos $_ == length $_
ABCresult
0XX0
10X0
1100
1111
63 1 pos = $nested - 1;
64 1 $self->_blam("No closing ) found for opening (");
65 }
66 141 ▶ return $match[0] if (@match == 1);
Branch: 1 branch
BranchTrueFalseTotal
if @match == 1 74 67 141
67 67 return match_and(@match);
68 }
69 }
70  
71 73 ▶sub _parse_spec_combinator {
Subroutine _parse_spec_combinator: called CC=8 SCAR=21.5
72 73 my ($self, $spec, $match) = @_;
73 73 for ($_[1]) {
74  
75 73 ▶ /\G\+/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G\+/cg and return $match 69 4 73
76 return $match;
77  
78 /\G\|/gc and
79 4 ▶ return do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\|/cg and return do { my(@match) = $match; PARSE: { do { push @match, $self->_parse_spec_section($_) or $self->_blam("Unable to work out what the next section is"); last PARSE if pos $_ == length $_; last PARSE unless /\G\|/cg } until pos $_ == length $_; } ; return match_or(@match) } 4 0 4
80 4 my @match = $match;
81 4 ▶ PARSE: { do {
Branch: 1 branch
BranchTrueFalseTotal
pos $_ == length $_ or do { do { push @match, $self->_parse_spec_section($_) or $self->_blam("Unable to work out what the next section is"); last PARSE if pos $_ == length $_; last PARSE unless /\G\|/cg } } 0 1 1
82 5 ▶ push @match, $self->_parse_spec_section($_)
Branch: 1 branch
BranchTrueFalseTotal
push @match, $self->_parse_spec_section($_) or $self->_blam("Unable to work out what the next section is") 5 0 5
83 or $self->_blam("Unable to work out what the next section is");
84 5 ▶ last PARSE if (pos == length);
Branch: 1 branch
BranchTrueFalseTotal
if pos $_ == length $_ 4 1 5
85 1 ▶ last PARSE unless /\G\|/gc; # give up when next thing isn't |
Branch: 1 branch
BranchTrueFalseTotal
unless /\G\|/cg 0 1 1
86 } until (pos == length) }; # accept trailing whitespace
87 4 return match_or(@match);
88 };
89 }
90 0 return;
91 }
92  
93 218 ▶sub _parse_spec_section {
Subroutine _parse_spec_section: called CC=11 SCAR=24.0
94 218 my ($self) = @_;
95 218 for ($_[1]) {
96  
97 # ~
98  
99 218 ▶ /\G~/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G~/cg and return match_path("^\$") 2 216 218
100 return match_path('^$');
101  
102 # GET POST PUT HEAD ...
103  
104 216 ▶ /\G([A-Z]+)/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G([A-Z]+)/cg and return match_method($1) 25 191 216
105 return match_method($1);
106  
107 # /...
108  
109 191 ▶ /\G(?=\/)/gc and
Branch: 1 branch
BranchTrueFalseTotal
m[\G(?=/)]cg and return $self->_url_path_match($_) 94 97 191
110 return $self->_url_path_match($_);
111  
112 # .* and .html
113  
114 97 ▶ /\G\.(\*|\w+)/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G\.(\*|\w+)/cg and return match_extension($1) 5 92 97
115 return match_extension($1);
116  
117 # (...)
118  
119 92 ▶ /\G\(/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G\(/cg and return $self->_parse_spec($_, pos $_) 4 88 92
120 return $self->_parse_spec($_, pos);
121  
122 # !something
123  
124 88 ▶ /\G!/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G!/cg and return match_not($self->_parse_spec_section($_)) 1 87 88
125 return match_not($self->_parse_spec_section($_));
126  
127 # ?<param spec>
128 87 ▶ /\G\?/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G\?/cg and return $self->_parse_param_handler($_, "query") 82 5 87
129 return $self->_parse_param_handler($_, 'query');
130  
131 # %<param spec>
132 5 ▶ /\G\%/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G\%/cg and return $self->_parse_param_handler($_, "body") 4 1 5
133 return $self->_parse_param_handler($_, 'body');
134  
135 # *<param spec>
136 1 ▶ /\G\*/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G\*/cg and return $self->_parse_param_handler($_, "uploads") 1 0 1
137 return $self->_parse_param_handler($_, 'uploads');
138 }
139 0 return; # () will trigger the blam in our caller
140 }
141  
142 94 ▶sub _url_path_match {
Subroutine _url_path_match: called CC=20 SCAR=30.0
143 94 my ($self) = @_;
144 94 for ($_[1]) {
145 94 my (@path, @names, $seen_nameless);
146 94 my $end = '';
147 94 my $keep_dot;
148 94 ▶ PATH: while (/\G\//gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (m[\G/]cg) 192 83 275
149 /\G\.\.\./gc
150 192 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\.\.\./cg and do { $end = "(/.*)"; last PATH } 4 188 192
151 4 $end = '(/.*)';
152 4 last PATH;
153 };
154  
155 188 ▶ my ($segment) = $self->_url_path_segment_match($_)
Branch: 1 branch
BranchTrueFalseTotal
my($segment) = $self->_url_path_segment_match($_) or $self->_blam("Couldn't parse path match segment") 188 0 188
156 or $self->_blam("Couldn't parse path match segment");
157  
158 188 ▶ if (ref($segment)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $segment) 17 171 188
159 17 ($segment, $keep_dot, my $name) = @$segment;
160 17 ▶ if (defined($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 6 11 17
161 6 ▶ $self->_blam("Can't mix positional and named captures in path match")
Branch: 1 branch
BranchTrueFalseTotal
if $seen_nameless 0 6 6
162 if $seen_nameless;
163 6 push @names, $name;
164 } else {
165 11 ▶ $self->_blam("Can't mix positional and named captures in path match")
Branch: 1 branch
BranchTrueFalseTotal
if @names 0 11 11
166 if @names;
167 11 $seen_nameless = 1;
168 }
169 }
170 188 push @path, $segment;
171  
172 /\G\.\.\./gc
173 188 ▶ and do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\.\.\./cg and do { $end = "(|/.*)"; last PATH } 4 184 188
174 4 $end = '(|/.*)';
175 4 last PATH;
176 };
177 184 ▶ /\G\.\*/gc
Branch: 1 branch
BranchTrueFalseTotal
/\G\.\*/cg and $keep_dot = 1 2 182 184
178 and $keep_dot = 1;
179  
180 184 ▶ last PATH if $keep_dot;
Branch: 1 branch
BranchTrueFalseTotal
if $keep_dot 3 181 184
181 }
182 94 ▶ if (@path && !$end && !$keep_dot) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path and not $end and not $keep_dot) 83 11 94
Condition100%6/6
@path and not $end and not $keep_dot
!ll&&!rl&&r
8383
@path and not $end
!ll&&!rl&&r
2686
MC/DC100%3/3
@path and not $end and not $keep_dot
@path not $end not $keep_dot
Truth table100%4/4
A and not(B) and not(C)
A= @pathB= not $endC= not $keep_dot
ABCresult
0XX0
10X0
1100
1111
183 83 ▶ length and $_ .= '(?:\.\w+)?' for $path[-1];
Branch: 1 branch
BranchTrueFalseTotal
length $_ and $_ .= '(?:\\.\\w+)?' 53 30 83
184 }
185 94 my $re = '^('.join('/','',@path).')'.$end.'$';
186 94 $re = qr/$re/;
187 94 ▶ if ($end) {
Branch: 1 branch
BranchTrueFalseTotal
if ($end) { } 8 86 94
188 8 ▶ return match_path_strip($re, @names ? \@names : ());
Branch: 1 branch
BranchTrueFalseTotal
@names ? : 0 8 8
189 } else {
190 86 ▶ return match_path($re, @names ? \@names : ());
Branch: 1 branch
BranchTrueFalseTotal
@names ? : 4 82 86
191 }
192 }
193 0 return;
194 }
195  
196 188 ▶sub _url_path_segment_match {
Subroutine _url_path_segment_match: called CC=7 SCAR=19.6
197 188 my ($self) = @_;
198 188 for ($_[1]) {
199 # trailing / -> require / on end of URL
200 188 ▶ /\G(?:(?=[+|\)])|$)/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G(?:(?=[+|\)])|$)/cg and return "" 30 158 188
201 return '';
202 # word chars only -> exact path part match
203 158 ▶ /
Branch: 1 branch
BranchTrueFalseTotal
/ \G( (?: # start matching at a space followed by: [\w\-] # word chars or dashes | # OR \. # a period (?!\.) # not followed by another period ) + # then grab as far as possible ) /cgx and return quotemeta $1 141 17 158
204 \G(
205 (?: # start matching at a space followed by:
206 [\w\-] # word chars or dashes
207 | # OR
208 \. # a period
209 (?!\.) # not followed by another period
210 )
211 + # then grab as far as possible
212 )
213 /gcx and
214 return "\Q$1";
215 # ** -> capture unlimited path parts
216 17 ▶ /\G\*\*(?:(\.\*)?\:(\w+))?/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G\*\*(?:(\.\*)?\:(\w+))?/cg and return ["(.*?[^/])", $1, $2] 5 12 17
217 return [ '(.*?[^/])', $1, $2 ];
218 # * -> capture path part
219 # *:name -> capture named path part
220 12 ▶ /\G\*(?:(\.\*)?\:(\w+))?/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G\*(?:(\.\*)?\:(\w+))?/cg and return ["([^/]+?)", $1, $2] 9 3 12
221 return [ '([^/]+?)', $1, $2 ];
222  
223 # :name -> capture named path part
224 3 ▶ /\G\:(\w+)/gc and
Branch: 1 branch
BranchTrueFalseTotal
/\G\:(\w+)/cg and return ["([^/]+?)", 0, $1] 3 0 3
225 return [ '([^/]+?)', 0, $1 ];
226 }
227 0 return ();
228 }
229  
230 87 ▶sub _parse_param_handler {
Subroutine _parse_param_handler: called CC=12 SCAR=25.2
231 87 my ($self, $spec, $type) = @_;
232  
233 87 for ($_[1]) {
234 87 my (@required, @single, %multi, $star, $multistar, %positional, $have_kw);
235 87 my %spec;
236 87 my $pos_idx = 0;
237 87 ▶ PARAM: { do {
Branch: 1 branch
BranchTrueFalseTotal
/\G\&/cg and do { do { my $is_kw = /\G\:/cg; my $multi = /\G\@/cg; if (/\G\*/cg) { $self->_blam("* is always named; no need to supply :") if $is_kw; if ($star) { $self->_blam("Can only use one * or \@* in a parameter match"); } ; $spec{"star"} = {"multi", $multi}; } else { /\G([\w.]*)/cg or $self->_blam("Expected parameter name"); my $name = $1; /\G\=/cg ? push(@{$spec{"required"} ||= [];}, $name) : /\G\~/cg || $self->_blam("Expected = or ~ after parameter name"); push @{$spec{$is_kw ? "named" : "positional"} ||= [];}, {"name", $name, "multi", $multi}; } } } 23 87 110
238  
239 # ?:foo or ?@:foo
240  
241 110 my $is_kw = /\G\:/gc;
242  
243 # ?@foo or ?@*
244  
245 110 my $multi = /\G\@/gc;
246  
247 # @* or *
248  
249 110 ▶ if (/\G\*/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if (/\G\*/cg) { } 17 93 110
250  
251 17 ▶ $self->_blam("* is always named; no need to supply :") if $is_kw;
Branch: 1 branch
BranchTrueFalseTotal
if $is_kw 0 17 17
252  
253 17 ▶ if ($star) {
Branch: 1 branch
BranchTrueFalseTotal
if ($star) 0 17 17
254 0 $self->_blam("Can only use one * or \@* in a parameter match");
255 }
256  
257 17 $spec{star} = { multi => $multi };
258 } else {
259  
260 # @foo= or foo= or @foo~ or foo~
261  
262 93 ▶ /\G([\w.]*)/gc or $self->_blam('Expected parameter name');
Branch: 1 branch
BranchTrueFalseTotal
/\G([\w.]*)/cg or $self->_blam("Expected parameter name") 93 0 93
263  
264 93 my $name = $1;
265  
266 # check for = or ~ on the end
267  
268 /\G\=/gc
269 93 ▶ ? push(@{$spec{required}||=[]}, $name)
Branch: 1 branch
BranchTrueFalseTotal
/\G\=/cg ? : 79 14 93
Condition60%3/5
$spec{"required"} ||= []
l!l
1267
/\G\~/cg or $self->_blam("Expected = or ~ after parameter name")
l!l&&r!l&&!r
1400
MC/DC100%1/1
$spec{"required"} ||= []
$spec{"required"}
MC/DC0%0/2
/\G\~/cg or $self->_blam("Expected = or ~ after parameter name")
/\G\~/cg $self->_blam("Expected = or ~ after parameter name")
Truth table100%2/2
A
A= $spec{"required"}
Aresult
11
00
Truth table33%1/3
A or B
A= /\G\~/cgB= $self->_blam("Expected = or ~ after parameter name")
ABresult
1X1
011
000
270 : (/\G\~/gc or $self->_blam('Expected = or ~ after parameter name'));
271  
272 # record positional or keyword
273  
274 93 ▶ push @{$spec{$is_kw ? 'named' : 'positional'}||=[]},
Branch: 1 branch
BranchTrueFalseTotal
$is_kw ? : 52 41 93
Condition100%2/2
$spec{$is_kw ? "named" : "positional"} ||= []
l!l
1182
MC/DC100%1/1
$spec{$is_kw ? "named" : "positional"} ||= []
$spec{$is_kw ? "named" : "positional"}
Truth table100%2/2
A
A= $spec{$is_kw ? "named" : "positional"}
Aresult
11
00
275 { name => $name, multi => $multi };
276 }
277 } while (/\G\&/gc) }
278  
279 87 return Web::Dispatch::Predicates->can("match_${type}")->(\%spec);
280 }
281 }
282  
283 16 1;
« blib/lib/Web/Simple/HackedPlack.pm ↑ Summary blib/lib/Web/Simple/Application.pm »