Dispatch/PSGI.pm

filter: Statementstmt 85.2% 150 / 176 Branchbran 70.3% 83 / 118 Conditioncond 75.0% 45 / 60 MC/DCmcdc 38.7% 12 / 31 Subroutinesub 100% 13 / 13 Podpod 80.0% 4 / 5 totaltotal 76.1% 307 / 403 CC 84 SCAR 29.4
CC
84
Cov
79%
__ANON__
27.6
as_psgi
27.3
_run_app
18.3
CRAP 19.0
« Dispatch/Regexp.pm ↑ Summary
1 package CGI::Application::Dispatch::PSGI;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp 'carp';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTTP::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '3.12';
8 1 our $DEBUG = 0;
9  
10 =pod
11  
12 =head1 NAME
13  
14 CGI::Application::Dispatch::PSGI - Dispatch requests to
15 CGI::Application based objects using PSGI
16  
17 =head1 SYNOPSIS
18  
19 =head2 Out of Box
20  
21 Under mod_perl:
22  
23 # change "Apache1" to "Apache2" as needed.
24  
25 <Location />
26 SetHandler perl-script
27 PerlHandler Plack::Handler::Apache1
28 PerlSetVar psgi_app /path/to/app.psgi
29 </Location>
30  
31 <Perl>
32 use Plack::Handler::Apache1;
33 Plack::Handler::Apache1->preload("/path/to/app.psgi");
34 </Perl>
35  
36 Under CGI:
37  
38 This would be the instance script for your application, such
39 as /cgi-bin/dispatch.cgi:
40  
41 ### in your dispatch.psgi:
42 # ( in a persistent environment, use FindBin::Real instead. )
43 use FindBin 'Bin';
44 use lib "$Bin/../perllib';
45 use Your::Application::Dispatch;
46 Your::Application::Dispatch->as_psgi;
47  
48 ### In Your::Application::Dispatch;
49 package Your::Application::Dispatch;
50 use base 'CGI::Application::Dispatch::PSGI';
51  
52  
53 =head2 With a dispatch table
54  
55 package MyApp::Dispatch;
56 use base 'CGI::Application::Dispatch::PSGI';
57  
58 sub dispatch_args {
59 return {
60 prefix => 'MyApp',
61 table => [
62 '' => { app => 'Welcome', rm => 'start' },
63 ':app/:rm' => { },
64 'admin/:app/:rm' => { prefix => 'MyApp::Admin' },
65 ],
66 };
67 }
68  
69 The C<< .psgi >> file is constructed as above.
70  
71 =head2 With a custom query object
72  
73 If you want to supply your own PSGI object, something like this in
74 your .psgi file will work:
75  
76 sub {
77 my $env = shift;
78 my $app = CGI::Application::Dispatch::PSGI->as_psgi(
79 table => [
80 '/:rm' => { app => 'TestApp' }
81 ],
82 args_to_new => {
83 QUERY => CGI::PSGI->new($env)
84 }
85 );
86 return $app->($env);
87 }
88  
89  
90 =head1 DESCRIPTION
91  
92 This module provides a way to look at the path (as returned by C<<
93 $env->{PATH_INFO} >>) of the incoming request, parse off the desired
94 module and its run mode, create an instance of that module and run it.
95  
96 It will translate a URI like this (in a persistent environment)
97  
98 /app/module_name/run_mode
99  
100 or this (vanilla CGI)
101  
102 /app/index.cgi/module_name/run_mode
103  
104 into something that will be functionally similar to this
105  
106 my $app = Module::Name->new(..);
107 $app->mode_param(sub {'run_mode'}); #this will set the run mode
108  
109 =head1 METHODS
110  
111 =cut
112  
113 11 ▶sub as_psgi {
Subroutine as_psgi: called CC=21 SCAR=33.1
Pod documented
114 11 my ($self, %args) = @_;
115  
116 # merge dispatch_args() and %args with %args taking precendence
117 11 my $dispatch_args = $self->dispatch_args(\%args);
118 11 for my $arg (keys %$dispatch_args) {
119  
120 # args_to_new should be merged
121 43 ▶ if($arg eq 'args_to_new') {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg eq 'args_to_new') { } 11 32 43
122 11 ▶ $args{args_to_new} ||= {};
Condition100%2/2
$args{"args_to_new"} ||= {}
l!l
110
MC/DC100%1/1
$args{"args_to_new"} ||= {}
$args{"args_to_new"}
Truth table100%2/2
A
A= $args{"args_to_new"}
Aresult
11
00
123  
124 # merge the PARAMS hash
125 11 ▶ if($dispatch_args->{args_to_new}->{PARAMS}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dispatch_args->{"args_to_new"}{"PARAMS"}) 2 9 11
126  
127 # merge the hashes
128 $args{args_to_new}->{PARAMS} = {
129 2 %{$dispatch_args->{args_to_new}->{PARAMS}},
130 2 ▶ %{$args{args_to_new}->{PARAMS} || {}},
Condition100%2/2
$args{'args_to_new'}{'PARAMS'} || {}
l!l
11
MC/DC100%1/1
$args{'args_to_new'}{'PARAMS'} || {}
$args{'args_to_new'}{'PARAMS'}
Truth table100%2/2
A
A= $args{'args_to_new'}{'PARAMS'}
Aresult
11
00
131 };
132 }
133  
134 # combine any TMPL_PATHs
135 11 ▶ if($dispatch_args->{args_to_new}->{TMPL_PATH}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dispatch_args->{"args_to_new"}{"TMPL_PATH"}) 0 11 11
136  
137 # make sure the orginial is an array ref
138 0 ▶ if($args{args_to_new}->{TMPL_PATH}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'args_to_new'}{'TMPL_PATH'}) { } 0 0 0
139 0 ▶ if(!ref $args{args_to_new}->{TMPL_PATH}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $args{"args_to_new"}{"TMPL_PATH"}) 0 0 0
140 0 $args{args_to_new}->{TMPL_PATH} = [$args{args_to_new}->{TMPL_PATH}];
141 }
142 } else {
143 0 $args{args_to_new}->{TMPL_PATH} = [];
144 }
145  
146 # now add the rest to the end
147 0 ▶ if(ref $dispatch_args->{args_to_new}->{TMPL_PATH}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $dispatch_args->{'args_to_new'}{'TMPL_PATH'}) { } 0 0 0
148 push(
149 0 @{$args{args_to_new}->{TMPL_PATH}},
150 0 @{$dispatch_args->{args_to_new}->{TMPL_PATH}},
151 );
152 } else {
153 push(
154 0 @{$args{args_to_new}->{TMPL_PATH}},
155 $dispatch_args->{args_to_new}->{TMPL_PATH},
156 0 );
157 }
158 }
159  
160 # now merge the args_to_new hashes
161 11 $args{args_to_new} = {%{$dispatch_args->{args_to_new}}, %{$args{args_to_new}},};
162 } else {
163  
164 # anything else should override
165 32 ▶ $args{$arg} = $dispatch_args->{$arg} unless exists $args{$arg};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$arg} 20 12 32
166 }
167 }
168  
169 11 ▶ $DEBUG = $args{debug} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'debug'} ? : 0 11 11
170  
171 # check for extra args (for backwards compatibility)
172 11 for (keys %args) {
173 next
174 44 ▶ if( $_ eq 'prefix'
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "prefix" or $_ eq "default" or $_ eq "debug" or $_ eq "rm" or $_ eq "args_to_new" or $_ eq "table" or $_ eq "auto_rest" or $_ eq "auto_rest_lc" 44 0 44
Condition76%16/21
$_ eq "prefix" or $_ eq "default" or $_ eq "debug" or $_ eq "rm" or $_ eq "args_to_new" or $_ eq "table" or $_ eq "auto_rest" or $_ eq "auto_rest_lc"
l!l&&r!l&&!r
4400
$_ eq "prefix" or $_ eq "default" or $_ eq "debug" or $_ eq "rm" or $_ eq "args_to_new" or $_ eq "table" or $_ eq "auto_rest"
l!l&&r!l&&!r
4310
$_ eq "prefix" or $_ eq "default" or $_ eq "debug" or $_ eq "rm" or $_ eq "args_to_new" or $_ eq "table"
l!l&&r!l&&!r
32111
$_ eq "prefix" or $_ eq "default" or $_ eq "debug" or $_ eq "rm" or $_ eq "args_to_new"
l!l&&r!l&&!r
211112
$_ eq "prefix" or $_ eq "default" or $_ eq "debug" or $_ eq "rm"
l!l&&r!l&&!r
21023
$_ eq "prefix" or $_ eq "default" or $_ eq "debug"
l!l&&r!l&&!r
21023
$_ eq "prefix" or $_ eq "default"
l!l&&r!l&&!r
111023
MC/DC0%0/8
$_ eq "prefix" or $_ eq "default" or $_ eq "debug" or $_ eq "rm" or $_ eq "args_to_new" or $_ eq "table" or $_ eq "auto_rest" or $_ eq "auto_rest_lc"
$_ eq "prefix" $_ eq "default" $_ eq "debug" $_ eq "rm" $_ eq "args_to_new" $_ eq "table" $_ eq "auto_rest" $_ eq "auto_rest_lc"
Truth table55%5/9
A or B or C or D or E or F or G or H
A= $_ eq "prefix"B= $_ eq "default"C= $_ eq "debug"D= $_ eq "rm"E= $_ eq "args_to_new"F= $_ eq "table"G= $_ eq "auto_rest"H= $_ eq "auto_rest_lc"
ABCDEFGHresult
1XXXXXXX1
01XXXXXX1
001XXXXX1
0001XXXX1
00001XXX1
000001XX1
0000001X1
000000011
000000000
175 or $_ eq 'default'
176 or $_ eq 'debug'
177 or $_ eq 'rm'
178 or $_ eq 'args_to_new'
179 or $_ eq 'table'
180 or $_ eq 'auto_rest'
181 or $_ eq 'auto_rest_lc');
182 0 die "Passing extra args ('$_') to as_psgi() is not supported! Did you mean to use 'args_to_new' ?";
183 0 $args{args_to_new}->{$_} = delete $args{$_};
184 }
185  
186 28 ▶ return sub {
Subroutine __ANON__: called CC=25 SCAR=33.2
187 28 my $env = shift;
188  
189 # get the PATH_INFO
190 28 my $path_info = $env->{PATH_INFO};
191  
192 # use the 'default' if we need to
193 28 ▶ $path_info = $args{default} || '' if(!$path_info || $path_info eq '/');
Branch: 1 branch
BranchTrueFalseTotal
if not $path_info or $path_info eq "/" 3 25 28
Condition80%4/5
not $path_info or $path_info eq "/"
l!l&&r!l&&!r
0325
$args{'default'} || ''
l!l
21
MC/DC50%1/2
not $path_info or $path_info eq "/"
not $path_info $path_info eq "/"
MC/DC100%1/1
$args{'default'} || ''
$args{'default'}
Truth table66%2/3
not(A) or B
A= not $path_infoB= $path_info eq "/"
ABresult
1X1
011
000
Truth table100%2/2
A
A= $args{'default'}
Aresult
11
00
194  
195 # make sure they all start and end with a '/', to correspond
196 # with the RE we'll make
197 28 ▶ $path_info = "/$path_info" unless(index($path_info, '/') == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless index($path_info, "/") == 0 1 27 28
198 28 ▶ $path_info = "$path_info/" unless(substr($path_info, -1) eq '/');
Branch: 1 branch
BranchTrueFalseTotal
unless substr($path_info, -1) eq "/" 27 1 28
199  
200 28 my ($module, $rm, $local_prefix, $local_args_to_new);
201 # take args from path
202 28 my $named_args;
203 28 eval {
204 28 ▶ $named_args = $self->_parse_path($path_info, $args{table},$env)
Branch: 1 branch
BranchTrueFalseTotal
$named_args = $self->_parse_path($path_info, $args{"table"}, $env) or "HTTP::Exception"->throw(404, "status_message", "Resource not found") 26 2 28
205 or HTTP::Exception->throw(404, status_message => 'Resource not found');
206 };
207 28 ▶ if (my $e = HTTP::Exception->caught) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = "HTTP::Exception"->caught) 2 26 28
208 2 return $self->http_error($e);
209 }
210  
211 26 ▶ if($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::Application::Dispatch::PSGI::DEBUG) 0 26 26
212 0 require Data::Dumper;
213 0 warn "[Dispatch] Named args from match: " . Data::Dumper::Dumper($named_args) . "\n";
214 }
215  
216 26 ▶ if(exists($named_args->{PARAMS}) || exists($named_args->{TMPL_PATH})) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $named_args->{"PARAMS"} or exists $named_args->{"TMPL_PATH"}) 0 26 26
Condition33%1/3
exists $named_args->{"PARAMS"} or exists $named_args->{"TMPL_PATH"}
l!l&&r!l&&!r
0026
MC/DC0%0/2
exists $named_args->{"PARAMS"} or exists $named_args->{"TMPL_PATH"}
exists $named_args->{"PARAMS"} exists $named_args->{"TMPL_PATH"}
Truth table33%1/3
A or B
A= exists $named_args->{"PARAMS"}B= exists $named_args->{"TMPL_PATH"}
ABresult
1X1
011
000
217 0 carp "PARAMS and TMPL_PATH are not allowed here. Did you mean to use args_to_new?";
218 0 HTTP::Exception->throw(500, status_message => 'PARAMS and TMPL_PATH not allowed');
219 }
220  
221 # eval and catch any exceptions that might be thrown
222 26 my ($output, @final_dispatch_args);
223 26 my $psgi_app;
224 26 eval {
225 ($module, $local_prefix, $rm, $local_args_to_new) =
226 26 delete @{$named_args}{qw(app prefix rm args_to_new)};
227  
228 # If another name for dispatch_url_remainder has been set move
229 # the value to the requested name
230 26 ▶ if($$named_args{'*'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($named_args->{"*"}) 1 25 26
231 1 $$named_args{$$named_args{'*'}} = $$named_args{'dispatch_url_remainder'};
232 1 delete $$named_args{'*'};
233 1 delete $$named_args{'dispatch_url_remainder'};
234 }
235  
236 26 ▶ $module or HTTP::Exception->throw(500, status_message => 'App not defined');
Branch: 1 branch
BranchTrueFalseTotal
$module or "HTTP::Exception"->throw(500, "status_message", "App not defined") 26 0 26
237 26 $module = $self->translate_module_name($module);
238  
239 26 ▶ $local_prefix ||= $args{prefix};
Condition100%3/3
$local_prefix ||= $args{"prefix"}
l!l&&r!l&&!r
1214
MC/DC100%2/2
$local_prefix ||= $args{"prefix"}
$local_prefix $args{"prefix"}
Truth table100%3/3
A ||= B
A= $local_prefixB= $args{"prefix"}
ABresult
1X1
011
000
240 26 ▶ $module = $local_prefix . '::' . $module if($local_prefix);
Branch: 1 branch
BranchTrueFalseTotal
if $local_prefix 22 4 26
241  
242 26 ▶ $local_args_to_new ||= $args{args_to_new};
Condition66%2/3
$local_args_to_new ||= $args{"args_to_new"}
l!l&&r!l&&!r
2240
MC/DC0%0/2
$local_args_to_new ||= $args{"args_to_new"}
$local_args_to_new $args{"args_to_new"}
Truth table66%2/3
A ||= B
A= $local_args_to_newB= $args{"args_to_new"}
ABresult
1X1
011
000
243  
244 # add the rest of the named_args to PARAMS
245 26 @{$local_args_to_new->{PARAMS}}{keys %$named_args} = values %$named_args;
246  
247 my $auto_rest =
248 26 ▶ defined $named_args->{auto_rest} ? $named_args->{auto_rest} : $args{auto_rest};
Branch: 1 branch
BranchTrueFalseTotal
defined $named_args->{'auto_rest'} ? : 3 23 26
249 26 ▶ if($auto_rest && defined $rm && length $rm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($auto_rest and defined $rm and length $rm) 3 23 26
Condition83%5/6
$auto_rest and defined $rm and length $rm
!ll&&!rl&&r
2213
$auto_rest and defined $rm
!ll&&!rl&&r
2204
MC/DC66%2/3
$auto_rest and defined $rm and length $rm
$auto_rest defined $rm length $rm
Truth table75%3/4
A and B and C
A= $auto_restB= defined $rmC= length $rm
ABCresult
0XX0
10X0
1100
1111
250 my $method_lc =
251 defined $named_args->{auto_rest_lc}
252 ? $named_args->{auto_rest_lc}
253 3 ▶ : $args{auto_rest_lc};
Branch: 1 branch
BranchTrueFalseTotal
defined $named_args->{'auto_rest_lc'} ? : 1 2 3
254 3 my $http_method = $env->{REQUEST_METHOD};
255 3 ▶ $http_method = lc $http_method if $method_lc;
Branch: 1 branch
BranchTrueFalseTotal
if $method_lc 1 2 3
256 3 $rm .= "_$http_method";
257 }
258 # load and run the module
259 26 @final_dispatch_args = ($module, $rm, $local_args_to_new);
260 26 $self->require_module($module);
261 24 $psgi_app = $self->_run_app($module, $rm, $local_args_to_new,$env);
262 };
263 26 ▶ if (my $e = HTTP::Exception->caught) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $e = 'HTTP::Exception'->caught) { } 3 23 26
elsif ($e = 'Exception::Class'->caught) { } 0 23 23
264 3 return $self->http_error($e);
265 }
266 elsif ($e = Exception::Class->caught) {
267 0 ▶ ref $e ? $e->rethrow : die $e;
Branch: 1 branch
BranchTrueFalseTotal
ref $e ? : 0 0 0
268 }
269 23 return $psgi_app;
270 }
271 11 }
272  
273 =head2 as_psgi(%args)
274  
275 This is the primary method used during dispatch.
276  
277 #!/usr/bin/perl
278 use strict;
279 use CGI::Application::Dispatch::PSGI;
280  
281 CGI::Application::Dispatch::PSGI->as_psgi(
282 prefix => 'MyApp',
283 default => 'module_name',
284 );
285  
286 This method accepts the following name value pairs:
287  
288 =over
289  
290 =item default
291  
292 Specify a value to use for the path if one is not available.
293 This could be the case if the default page is selected (eg: "/" ).
294  
295 =item prefix
296  
297 This option will set the string that will be prepended to the name of
298 the application module before it is loaded and created. So to use our
299 previous example request of
300  
301 /app/index.cgi/module_name/run_mode
302  
303 This would by default load and create a module named
304 'Module::Name'. But let's say that you have all of your application
305 specific modules under the 'My' namespace. If you set this option to
306 'My' then it would instead load the 'My::Module::Name' application
307 module instead.
308  
309 =item args_to_new
310  
311 This is a hash of arguments that are passed into the C<new()>
312 constructor of the application.
313  
314 =item table
315  
316 In most cases, simply using Dispatch with the C<default> and C<prefix>
317 is enough to simplify your application and your URLs, but there are
318 many cases where you want more power. Enter the dispatch table. Since
319 this table can be slightly complicated, a whole section exists on its
320 use. Please see the L<DISPATCH TABLE> section.
321  
322 =item debug
323  
324 Set to a true value to send debugging output for this module to
325 STDERR. Off by default.
326  
327 =item auto_rest
328  
329 This tells Dispatch that you are using REST by default and that you
330 care about which HTTP method is being used. Dispatch will append the
331 HTTP method name (upper case by default) to the run mode that is
332 determined after finding the appropriate dispatch rule. So a GET
333 request that translates into C<< MyApp::Module->foo >> will become
334 C<< MyApp::Module->foo_GET >>.
335  
336 This can be overridden on a per-rule basis in a custom dispatch table.
337  
338 =item auto_rest_lc
339  
340 In combinaion with L<auto_rest> this tells Dispatch that you prefer
341 lower cased HTTP method names. So instead of C<foo_POST> and
342 C<foo_GET> you'll have C<foo_post> and C<foo_get>.
343  
344 =back
345  
346 =cut
347  
348 5 ▶sub http_error {
Subroutine http_error: called CC=6 SCAR=18.7
Pod undocumented
349 5 my ($self, $e) = @_;
350  
351 warn '[Dispatch] ERROR'
352 5 ▶ . ($ENV{REQUEST_URI} ? " for request '$ENV{REQUEST_URI}': " : ': ')
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'REQUEST_URI'} ? : 0 5 5
353 . $e->error . "\n";
354  
355 5 ▶ my $errno = $e->isa('HTTP::Exception::Base') ? $e->code : 500;
Branch: 1 branch
BranchTrueFalseTotal
$e->isa('HTTP::Exception::Base') ? : 5 0 5
356 5 ▶ my $output = $e->isa('HTTP::Exception::Base') ? $e->status_message : "Internal Server Error";
Branch: 1 branch
BranchTrueFalseTotal
$e->isa('HTTP::Exception::Base') ? : 5 0 5
357  
358 # The custom status message was most useful for logging. Return
359 # generic messages to the user.
360 5 ▶ $output = 'Not Found' if ($e->code == 404);
Branch: 1 branch
BranchTrueFalseTotal
if $e->code == 404 4 1 5
361 5 ▶ $output = 'Internal Server Error' if ($e->code == 500);
Branch: 1 branch
BranchTrueFalseTotal
if $e->code == 500 0 5 5
362  
363  
364 5 return [ $errno, [], [ $output ] ];
365 }
366  
367 # protected method - designed to be used by sub classes, not by end users
368 28 ▶sub _parse_path {
Subroutine _parse_path: called CC=14 SCAR=26.8
369 28 my ($self, $path, $table, $env) = @_;
370  
371 # get the module name from the table
372 28 ▶ return unless defined($path);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $path 0 28 28
373  
374 28 ▶ unless(ref($table) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $table eq "ARRAY") 0 28 28
375 0 warn "[Dispatch] Invalid or no dispatch table!\n";
376 0 return;
377 }
378  
379 # look at each rule and stop when we get a match
380 28 ▶ for(my $i = 0 ; $i < scalar(@$table) ; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$table) 77 2 79
381  
382 77 my $rule = $table->[$i];
383  
384 # are we trying to dispatch based on HTTP_METHOD?
385 77 my $http_method_regex = qr/\[([^\]]+)\]$/;
386 77 ▶ if($rule =~ /$http_method_regex/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ /$http_method_regex/) 7 70 77
387 7 my $http_method = $1;
388  
389 # go ahead to the next rule
390 7 ▶ next unless lc($1) eq lc($env->{REQUEST_METHOD});
Branch: 1 branch
BranchTrueFalseTotal
unless lc $1 eq lc $env->{"REQUEST_METHOD"} 3 4 7
391  
392 # remove the method portion from the rule
393 4 $rule =~ s/$http_method_regex//;
394 }
395  
396 # make sure they start and end with a '/' to match how
397 # PATH_INFO is formatted
398 74 ▶ $rule = "/$rule" unless(index($rule, '/') == 0);
Branch: 1 branch
BranchTrueFalseTotal
unless index($rule, "/") == 0 74 0 74
399 74 ▶ $rule = "$rule/" if(substr($rule, -1) ne '/');
Branch: 1 branch
BranchTrueFalseTotal
if substr($rule, -1) ne "/" 74 0 74
400  
401 74 my @names = ();
402  
403 # translate the rule into a regular expression, but remember
404 # where the named args are
405 # '/:foo' will become '/([^\/]*)'
406 # and
407 # '/:bar?' will become '/?([^\/]*)?'
408 # and then remember which position it matches
409  
410 74 $rule =~ s{
411 (^|/) # beginning or a /
412 (:([^/\?]+)(\?)?) # stuff in between
413 }{
414 100 push(@names, $3);
415 100 ▶ $1 . ($4 ? '?([^/]*)?' : '([^/]*)')
Branch: 1 branch
BranchTrueFalseTotal
$4 ? : 10 90 100
416 }gxe;
417  
418 # '/*/' will become '/(.*)/$' the end / is added to the end of
419 # both $rule and $path elsewhere
420 74 ▶ if($rule =~ m{/\*/$}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rule =~ m[/\*/$]) 9 65 74
421 9 $rule =~ s{/\*/$}{/(.*)/\$};
422 9 push(@names, 'dispatch_url_remainder');
423 }
424  
425 warn
426 74 ▶ "[Dispatch] Trying to match '${path}' against rule '$table->[$i]' using regex '${rule}'\n"
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::Application::Dispatch::PSGI::DEBUG 0 74 74
427 if $DEBUG;
428  
429 # if we found a match, then run with it
430 74 ▶ if(my @values = ($path =~ m#^$rule$#)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@values) = $path =~ /^$rule$/) 26 48 74
431  
432 26 ▶ warn "[Dispatch] Matched!\n" if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::Application::Dispatch::PSGI::DEBUG 0 26 26
433  
434 26 my %named_args = %{$table->[++$i]};
435 26 ▶ @named_args{@names} = @values if @names;
Branch: 1 branch
BranchTrueFalseTotal
if @names 25 1 26
436  
437 26 return \%named_args;
438 }
439 }
440  
441 2 return;
442 }
443  
444 24 ▶sub _run_app {
Subroutine _run_app: called CC=14 SCAR=29.1
445 24 my ($self, $module, $rm, $args,$env) = @_;
446  
447 24 ▶ if($DEBUG) {
Branch: 1 branch
BranchTrueFalseTotal
if ($CGI::Application::Dispatch::PSGI::DEBUG) 0 24 24
448 0 require Data::Dumper;
449 0 warn "[Dispatch] Final args to pass to new(): " . Data::Dumper::Dumper($args) . "\n";
450 }
451  
452 24 ▶ if($rm) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rm) 21 3 24
453  
454 # check runmode name
455 21 ($rm) = ($rm =~ /^([a-zA-Z_][\w']+)$/);
456 21 ▶ HTTP::Exception->throw(400, status_message => "Invalid characters in runmode name") unless $rm;
Branch: 1 branch
BranchTrueFalseTotal
unless $rm 0 21 21
457  
458 }
459  
460 # now create and run then application object
461 24 ▶ warn "[Dispatch] creating instance of $module\n" if($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::Application::Dispatch::PSGI::DEBUG 0 24 24
462  
463 24 my $psgi;
464 24 eval {
465 24 my $app = do {
466 24 ▶ if (ref($args) eq 'HASH' and not defined $args->{QUERY}) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args eq 'HASH' and not defined $args->{'QUERY'}) { } 9 15 24
elsif (ref $args eq 'HASH') { } 15 0 15
Condition66%2/3
ref $args eq "HASH" and not defined $args->{"QUERY"}
!ll&&!rl&&r
0159
MC/DC50%1/2
ref $args eq "HASH" and not defined $args->{"QUERY"}
ref $args eq "HASH" not defined $args->{"QUERY"}
Truth table66%2/3
A and not(B)
A= ref $args eq "HASH"B= not defined $args->{"QUERY"}
ABresult
0X0
100
111
467 9 require CGI::PSGI;
468 9 $args->{QUERY} = CGI::PSGI->new($env);
469 9 $module->new($args);
470 }
471 elsif (ref($args) eq 'HASH') {
472 15 $module->new($args);
473 }
474 else {
475 0 $module->new();
476 }
477 };
478 24 ▶ $app->mode_param(sub { return $rm }) if($rm);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if $rm 21 3 24
479 24 $psgi = $app->run_as_psgi;
480 };
481  
482 # App threw an HTTP::Exception? Cool. Bubble it up.
483 24 my $e;
484 24 ▶ if ($e = HTTP::Exception->caught) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e = 'HTTP::Exception'->caught) { } 1 23 24
485 1 $e->rethrow;
486 }
487 else {
488 23 $e = Exception::Class->caught();
489  
490 # catch invalid run-mode stuff
491 23 ▶ if (not ref $e and $e =~ /No such run mode/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $e and $e =~ /No such run mode/) { } 0 23 23
elsif (defined $e and length $e) { } 0 23 23
Condition33%1/3
not ref $e and $e =~ /No such run mode/
!ll&&!rl&&r
0230
MC/DC0%0/2
not ref $e and $e =~ /No such run mode/
not ref $e $e =~ /No such run mode/
Truth table33%1/3
not(A) and B
A= not ref $eB= $e =~ /No such run mode/
ABresult
0X0
100
111
492 0 HTTP::Exception->throw(404, status_message => "RM '$rm' not found");
493 }
494 # otherwise, it's an internal server error.
495 ▶ elsif (defined $e and length $e) {
Condition33%1/3
defined $e and length $e
!ll&&!rl&&r
0230
MC/DC0%0/2
defined $e and length $e
defined $e length $e
Truth table33%1/3
A and B
A= defined $eB= length $e
ABresult
0X0
100
111
496 0 HTTP::Exception->throw(500, status_message => "Unknown error: $e");
497 #return $psgi;
498 }
499 else {
500 # no exception
501 23 return $psgi;
502 }
503 }
504 }
505  
506 =head2 dispatch_args()
507  
508 Returns a hashref of args that will be passed to L<dispatch>(). It
509 will return the following structure by default.
510  
511 {
512 prefix => '',
513 args_to_new => {},
514 table => [
515 ':app' => {},
516 ':app/:rm' => {},
517 ],
518 }
519  
520 This is the perfect place to override when creating a subclass to
521 provide a richer dispatch L<table>.
522  
523 When called, it receives 1 argument, which is a reference to the hash
524 of args passed into L<dispatch>.
525  
526 =cut
527  
528 10 ▶sub dispatch_args {
Subroutine dispatch_args: called CC=4 SCAR=13.9
Pod documented
529 10 my ($self, $args) = @_;
530 return {
531 default => ($args->{default} || ''),
532 prefix => ($args->{prefix} || ''),
533 10 ▶ args_to_new => ($args->{args_to_new} || {}),
Condition100%6/6
$args->{'default'} || ''
l!l
28
$args->{'prefix'} || ''
l!l
73
$args->{'args_to_new'} || {}
l!l
19
MC/DC100%1/1
$args->{'default'} || ''
$args->{'default'}
MC/DC100%1/1
$args->{'prefix'} || ''
$args->{'prefix'}
MC/DC100%1/1
$args->{'args_to_new'} || {}
$args->{'args_to_new'}
Truth table100%2/2
A
A= $args->{'default'}
Aresult
11
00
Truth table100%2/2
A
A= $args->{'prefix'}
Aresult
11
00
Truth table100%2/2
A
A= $args->{'args_to_new'}
Aresult
11
00
534 table => [
535 ':app' => {},
536 ':app/:rm' => {},
537 ],
538 };
539 }
540  
541 =head2 translate_module_name($input)
542  
543 This method is used to control how the module name is translated from
544 the matching section of the path (see L<"Path Parsing">. The main
545 reason that this method exists is so that it can be overridden if it
546 doesn't do exactly what you want.
547  
548 The following transformations are performed on the input:
549  
550 =over
551  
552 =item The text is split on '_'s (underscores)
553 and each word has its first letter capitalized. The words are then joined
554 back together and each instance of an underscore is replaced by '::'.
555  
556  
557 =item The text is split on '-'s (hyphens)
558 and each word has its first letter capitalized. The words are then joined
559 back together and each instance of a hyphen removed.
560  
561 =back
562  
563 Here are some examples to make it even clearer:
564  
565 module_name => Module::Name
566 module-name => ModuleName
567 admin_top-scores => Admin::TopScores
568  
569 =cut
570  
571 25 ▶sub translate_module_name {
Subroutine translate_module_name: called CC=1 SCAR=0.0
Pod documented
572 25 my ($self, $input) = @_;
573  
574 25 $input = join('::', map { ucfirst($_) } split(/_/, $input));
575 25 $input = join('', map { ucfirst($_) } split(/-/, $input));
576  
577 25 return $input;
578 }
579  
580 =head2 require_module($module_name)
581  
582 This class method is used internally to take a module name (supplied
583 by L<get_module_name>) and require it in a secure fashion. It is
584 provided as a public class method so that if you override other
585 functionality of this module, you can still safely require user
586 specified modules. If there are any problems requiring the named
587 module, then we will C<croak>.
588  
589 CGI::Application::Dispatch::PSGI->require_module('MyApp::Module::Name');
590  
591 =cut
592  
593 26 ▶sub require_module {
Subroutine require_module: called CC=6 SCAR=18.9
Pod documented
594 26 my ($self, $module) = @_;
595  
596 26 ▶ $module or HTTP::Exception->throw(404, status_message => "Can't define module name");
Branch: 1 branch
BranchTrueFalseTotal
$module or "HTTP::Exception"->throw(404, "status_message", "Can't define module name") 26 0 26
597  
598 #untaint the module name
599 26 ($module) = ($module =~ /^([A-Za-z][A-Za-z0-9_\-\:\']+)$/);
600  
601 26 ▶ unless($module) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($module) 0 26 26
602 0 HTTP::Exception->throw(400, status_message => "Invalid characters in module name");
603 }
604  
605 26 ▶ warn "[Dispatch] loading module $module\n" if($DEBUG);
Branch: 1 branch
BranchTrueFalseTotal
if $CGI::Application::Dispatch::PSGI::DEBUG 0 26 26
606 26 eval "require $module";
607 26 ▶ return unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 24 2 26
608  
609 2 my $module_path = $module;
610 2 $module_path =~ s/::/\//g;
611  
612 2 ▶ if($@ =~ /Can't locate $module_path.pm/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@ =~ /Can't locate $module_path.pm/) { } 2 0 2
613 2 HTTP::Exception->throw(404, status_message => "Can't find module $module");
614 }
615 else {
616 0 HTTP::Exception->throw(500, status_message => "Unable to load module '$module': $@");
617 }
618 }
619  
620 1 1;
621  
622 __END__
« Dispatch/Regexp.pm ↑ Summary