| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package CGI::Application::Dispatch; |
|
2
|
1
|
|
|
1
|
|
1621
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
51
|
|
|
3
|
1
|
|
|
1
|
|
7
|
use warnings; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
39
|
|
|
4
|
1
|
|
|
1
|
|
18
|
use Carp 'carp'; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
74
|
|
|
5
|
1
|
|
|
1
|
|
880
|
use Try::Tiny; |
|
|
1
|
|
|
|
|
1611
|
|
|
|
1
|
|
|
|
|
100
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '3.12'; |
|
8
|
|
|
|
|
|
|
our $DEBUG = 0; |
|
9
|
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
BEGIN { |
|
11
|
1
|
|
|
1
|
|
6
|
use constant IS_MODPERL => exists($ENV{MOD_PERL}); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
73
|
|
|
12
|
1
|
|
|
|
|
185
|
use constant IS_MODPERL2 => |
|
13
|
1
|
|
|
1
|
|
5
|
(IS_MODPERL() and exists $ENV{MOD_PERL_API_VERSION} and $ENV{MOD_PERL_API_VERSION} == 2); |
|
|
1
|
|
|
|
|
2
|
|
|
14
|
|
|
|
|
|
|
|
|
15
|
1
|
|
|
1
|
|
58
|
if(IS_MODPERL2()) { |
|
16
|
|
|
|
|
|
|
require Apache2::RequestUtil; |
|
17
|
|
|
|
|
|
|
require Apache2::RequestRec; |
|
18
|
|
|
|
|
|
|
require APR::Table; |
|
19
|
|
|
|
|
|
|
require Apache2::Const; |
|
20
|
|
|
|
|
|
|
Apache2::Const->import(qw(OK SERVER_ERROR HTTP_BAD_REQUEST NOT_FOUND REDIRECT)); |
|
21
|
0
|
|
|
|
|
0
|
} elsif(IS_MODPERL()) { |
|
22
|
|
|
|
|
|
|
require Apache::Constants; |
|
23
|
|
|
|
|
|
|
Apache::Constants->import(qw(OK SERVER_ERROR BAD_REQUEST NOT_FOUND REDIRECT)); |
|
24
|
|
|
|
|
|
|
} |
|
25
|
|
|
|
|
|
|
} |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
# these return values have different values used in different ENV |
|
28
|
|
|
|
|
|
|
use Exception::Class ( |
|
29
|
1
|
|
|
|
|
15
|
'CGI::Application::Dispatch::Exception', |
|
30
|
|
|
|
|
|
|
'CGI::Application::Dispatch::ERROR' => { |
|
31
|
|
|
|
|
|
|
isa => 'CGI::Application::Dispatch::Exception', |
|
32
|
|
|
|
|
|
|
alias => 'throw_error', |
|
33
|
|
|
|
|
|
|
description => 500, |
|
34
|
|
|
|
|
|
|
}, |
|
35
|
|
|
|
|
|
|
'CGI::Application::Dispatch::NOT_FOUND' => { |
|
36
|
|
|
|
|
|
|
isa => 'CGI::Application::Dispatch::Exception', |
|
37
|
|
|
|
|
|
|
alias => 'throw_not_found', |
|
38
|
|
|
|
|
|
|
description => 404, |
|
39
|
|
|
|
|
|
|
}, |
|
40
|
|
|
|
|
|
|
'CGI::Application::Dispatch::BAD_REQUEST' => { |
|
41
|
|
|
|
|
|
|
isa => 'CGI::Application::Dispatch::Exception', |
|
42
|
|
|
|
|
|
|
alias => 'throw_bad_request', |
|
43
|
|
|
|
|
|
|
description => 400, |
|
44
|
|
|
|
|
|
|
}, |
|
45
|
1
|
|
|
1
|
|
966
|
); |
|
|
1
|
|
|
|
|
17597
|
|
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=pod |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=head1 NAME |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
CGI::Application::Dispatch - Dispatch requests to CGI::Application based objects |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=head2 Out of Box |
|
56
|
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
Under mod_perl: |
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
SetHandler perl-script |
|
61
|
|
|
|
|
|
|
PerlHandler CGI::Application::Dispatch |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
Under normal cgi: |
|
65
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
This would be the instance script for your application, such |
|
67
|
|
|
|
|
|
|
as /cgi-bin/dispatch.cgi: |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
#!/usr/bin/perl |
|
70
|
|
|
|
|
|
|
use FindBin::Real 'Bin'; |
|
71
|
|
|
|
|
|
|
use lib Bin() . '/../../rel/path/to/my/perllib'; |
|
72
|
|
|
|
|
|
|
use CGI::Application::Dispatch; |
|
73
|
|
|
|
|
|
|
CGI::Application::Dispatch->dispatch(); |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head2 With a dispatch table |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
package MyApp::Dispatch; |
|
78
|
|
|
|
|
|
|
use base 'CGI::Application::Dispatch'; |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub dispatch_args { |
|
81
|
|
|
|
|
|
|
return { |
|
82
|
|
|
|
|
|
|
prefix => 'MyApp', |
|
83
|
|
|
|
|
|
|
table => [ |
|
84
|
|
|
|
|
|
|
'' => { app => 'Welcome', rm => 'start' }, |
|
85
|
|
|
|
|
|
|
':app/:rm' => { }, |
|
86
|
|
|
|
|
|
|
'admin/:app/:rm' => { prefix => 'MyApp::Admin' }, |
|
87
|
|
|
|
|
|
|
], |
|
88
|
|
|
|
|
|
|
}; |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
Under mod_perl: |
|
92
|
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
SetHandler perl-script |
|
95
|
|
|
|
|
|
|
PerlHandler MyApp::Dispatch |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
Under normal cgi: |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
This would be the instance script for your application, such |
|
101
|
|
|
|
|
|
|
as /cgi-bin/dispatch.cgi: |
|
102
|
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
#!/usr/bin/perl |
|
104
|
|
|
|
|
|
|
use FindBin::Real 'Bin'; |
|
105
|
|
|
|
|
|
|
use lib Bin() . '/../../rel/path/to/my/perllib'; |
|
106
|
|
|
|
|
|
|
use MyApp::Dispatch; |
|
107
|
|
|
|
|
|
|
MyApp::Dispatch->dispatch(); |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
This module provides a way (as a mod_perl handler or running under |
|
112
|
|
|
|
|
|
|
vanilla CGI) to look at the path (as returned by L) of |
|
113
|
|
|
|
|
|
|
the incoming request, parse off the desired module and its run mode, |
|
114
|
|
|
|
|
|
|
create an instance of that module and run it. |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
It currently supports both generations of mod_perl (1.x and |
|
117
|
|
|
|
|
|
|
2.x). Although, for simplicity, all examples involving Apache |
|
118
|
|
|
|
|
|
|
configuration and mod_perl code will be shown using mod_perl 1.x. |
|
119
|
|
|
|
|
|
|
This may change as mp2 usage increases. |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
It will translate a URI like this (under mod_perl): |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
/app/module_name/run_mode |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
or this (vanilla cgi) |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
/app/index.cgi/module_name/run_mode |
|
128
|
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
into something that will be functionally similar to this |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
my $app = Module::Name->new(..); |
|
132
|
|
|
|
|
|
|
$app->mode_param(sub {'run_mode'}); #this will set the run mode |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
=head1 METHODS |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 dispatch(%args) |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
This is the primary method used during dispatch. Even under mod_perl, |
|
139
|
|
|
|
|
|
|
the L method uses this under the hood. |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
#!/usr/bin/perl |
|
142
|
|
|
|
|
|
|
use strict; |
|
143
|
|
|
|
|
|
|
use CGI::Application::Dispatch; |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
CGI::Application::Dispatch->dispatch( |
|
146
|
|
|
|
|
|
|
prefix => 'MyApp', |
|
147
|
|
|
|
|
|
|
default => 'module_name', |
|
148
|
|
|
|
|
|
|
); |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
This method accepts the following name value pairs: |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=over |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item default |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Specify a value to use for the path if one is not available. |
|
157
|
|
|
|
|
|
|
This could be the case if the default page is selected (eg: "/" ). |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=item prefix |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
This option will set the string that will be prepended to the name of |
|
162
|
|
|
|
|
|
|
the application module before it is loaded and created. So to use our |
|
163
|
|
|
|
|
|
|
previous example request of |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
/app/index.cgi/module_name/run_mode |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
This would by default load and create a module named |
|
168
|
|
|
|
|
|
|
'Module::Name'. But let's say that you have all of your application |
|
169
|
|
|
|
|
|
|
specific modules under the 'My' namespace. If you set this option to |
|
170
|
|
|
|
|
|
|
'My' then it would instead load the 'My::Module::Name' application |
|
171
|
|
|
|
|
|
|
module instead. |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=item args_to_new |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
This is a hash of arguments that are passed into the C |
|
176
|
|
|
|
|
|
|
constructor of the application. |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
=item table |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
In most cases, simply using Dispatch with the C and C |
|
181
|
|
|
|
|
|
|
is enough to simplify your application and your URLs, but there are |
|
182
|
|
|
|
|
|
|
many cases where you want more power. Enter the dispatch table. Since |
|
183
|
|
|
|
|
|
|
this table can be slightly complicated, a whole section exists on its |
|
184
|
|
|
|
|
|
|
use. Please see the L section. |
|
185
|
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=item debug |
|
187
|
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
Set to a true value to send debugging output for this module to |
|
189
|
|
|
|
|
|
|
STDERR. Off by default. |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item error_document |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
This string is similar to Apache ErrorDocument directive. If this value is not |
|
194
|
|
|
|
|
|
|
present, then Dispatch will return a NOT FOUND error either to the browser with |
|
195
|
|
|
|
|
|
|
simple hardcoded message (under CGI) or to Apache (under mod_perl). |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
This value can be one of the following: |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
B |
|
200
|
|
|
|
|
|
|
- if it starts with a single double-quote character (C<">). This double-quote |
|
201
|
|
|
|
|
|
|
character will be trimmed from final output. |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
B |
|
204
|
|
|
|
|
|
|
- if it starts with less-than sign (C<<>). First character will be excluded |
|
205
|
|
|
|
|
|
|
as well. Path of this file should be relative to server DOCUMENT_ROOT. |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
B - if no leading C<"> or |
|
208
|
|
|
|
|
|
|
C<<> will be found. |
|
209
|
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
Custom messages will be displayed I. (Under |
|
211
|
|
|
|
|
|
|
mod_perl, please use ErrorDocument directive in Apache configuration files.) |
|
212
|
|
|
|
|
|
|
This value can contain C<%s> placeholder for L Perl function. This |
|
213
|
|
|
|
|
|
|
placeholder will be replaced with numeric HTTP error code. Currently |
|
214
|
|
|
|
|
|
|
CGI::Application::Dispatch uses three HTTP errors: |
|
215
|
|
|
|
|
|
|
|
|
216
|
|
|
|
|
|
|
B<400 Bad Request> |
|
217
|
|
|
|
|
|
|
- If there are invalid characters in module name (parameter :app) or |
|
218
|
|
|
|
|
|
|
runmode name (parameter :rm). |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
B<404 Not Found> |
|
221
|
|
|
|
|
|
|
- When the path does not match anything in the L, |
|
222
|
|
|
|
|
|
|
or module could not be found in @INC, or run mode did not exist. |
|
223
|
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
B<500 Internal Server Error> |
|
225
|
|
|
|
|
|
|
- If application error occurs. |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
Examples of using error_document (assume error 404 have been returned): |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
# return in browser 'Opss... HTTP Error #404' |
|
230
|
|
|
|
|
|
|
error_document => '"Opss... HTTP Error #%s' |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
# return contents of file $ENV{DOCUMENT_ROOT}/errors/error404.html |
|
233
|
|
|
|
|
|
|
error_document => '
|
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
# internal redirect to /errors/error404.html |
|
236
|
|
|
|
|
|
|
error_document => '/errors/error%s.html' |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
# external redirect to |
|
239
|
|
|
|
|
|
|
# http://host.domain/cgi-bin/errors.cgi?error=404 |
|
240
|
|
|
|
|
|
|
error_document => 'http://host.domain/cgi-bin/errors.cgi?error=%s' |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=item auto_rest |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
This tells Dispatch that you are using REST by default and that you |
|
245
|
|
|
|
|
|
|
care about which HTTP method is being used. Dispatch will append the |
|
246
|
|
|
|
|
|
|
HTTP method name (upper case by default) to the run mode that is |
|
247
|
|
|
|
|
|
|
determined after finding the appropriate dispatch rule. So a GET |
|
248
|
|
|
|
|
|
|
request that translates into C<< MyApp::Module->foo >> will become |
|
249
|
|
|
|
|
|
|
C<< MyApp::Module->foo_GET >>. |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
This can be overridden on a per-rule basis in a custom dispatch table. |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=item auto_rest_lc |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
In combinaion with L this tells Dispatch that you prefer |
|
256
|
|
|
|
|
|
|
lower cased HTTP method names. So instead of C and |
|
257
|
|
|
|
|
|
|
C you'll have C and C. |
|
258
|
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
=back |
|
260
|
|
|
|
|
|
|
|
|
261
|
|
|
|
|
|
|
=cut |
|
262
|
|
|
|
|
|
|
|
|
263
|
|
|
|
|
|
|
sub dispatch { |
|
264
|
22
|
|
|
22
|
1
|
11593
|
my ($self, %args) = @_; |
|
265
|
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
# merge dispatch_args() and %args with %args taking precendence |
|
267
|
22
|
|
|
|
|
95
|
my $dispatch_args = $self->dispatch_args(\%args); |
|
268
|
22
|
|
|
|
|
229
|
for my $arg (keys %$dispatch_args) { |
|
269
|
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
# args_to_new should be merged |
|
271
|
79
|
100
|
|
|
|
143
|
if($arg eq 'args_to_new') { |
|
272
|
22
|
|
100
|
|
|
93
|
$args{args_to_new} ||= {}; |
|
273
|
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
# merge the PARAMS hash |
|
275
|
22
|
100
|
|
|
|
64
|
if($dispatch_args->{args_to_new}->{PARAMS}) { |
|
276
|
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
# merge the hashes |
|
278
|
10
|
|
|
|
|
23
|
$args{args_to_new}->{PARAMS} = { |
|
279
|
10
|
100
|
|
|
|
60
|
%{$dispatch_args->{args_to_new}->{PARAMS}}, |
|
280
|
10
|
|
|
|
|
16
|
%{$args{args_to_new}->{PARAMS} || {}}, |
|
281
|
|
|
|
|
|
|
}; |
|
282
|
|
|
|
|
|
|
} |
|
283
|
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
# combine any TMPL_PATHs |
|
285
|
22
|
50
|
|
|
|
61
|
if($dispatch_args->{args_to_new}->{TMPL_PATH}) { |
|
286
|
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
# make sure the orginial is an array ref |
|
288
|
0
|
0
|
|
|
|
0
|
if($args{args_to_new}->{TMPL_PATH}) { |
|
289
|
0
|
0
|
|
|
|
0
|
if(!ref $args{args_to_new}->{TMPL_PATH}) { |
|
290
|
0
|
|
|
|
|
0
|
$args{args_to_new}->{TMPL_PATH} = [$args{args_to_new}->{TMPL_PATH}]; |
|
291
|
|
|
|
|
|
|
} |
|
292
|
|
|
|
|
|
|
} else { |
|
293
|
0
|
|
|
|
|
0
|
$args{args_to_new}->{TMPL_PATH} = []; |
|
294
|
|
|
|
|
|
|
} |
|
295
|
|
|
|
|
|
|
|
|
296
|
|
|
|
|
|
|
# now add the rest to the end |
|
297
|
0
|
0
|
|
|
|
0
|
if(ref $dispatch_args->{args_to_new}->{TMPL_PATH}) { |
|
298
|
0
|
|
|
|
|
0
|
push( |
|
299
|
0
|
|
|
|
|
0
|
@{$args{args_to_new}->{TMPL_PATH}}, |
|
300
|
0
|
|
|
|
|
0
|
@{$dispatch_args->{args_to_new}->{TMPL_PATH}}, |
|
301
|
|
|
|
|
|
|
); |
|
302
|
|
|
|
|
|
|
} else { |
|
303
|
0
|
|
|
|
|
0
|
push( |
|
304
|
0
|
|
|
|
|
0
|
@{$args{args_to_new}->{TMPL_PATH}}, |
|
305
|
|
|
|
|
|
|
$dispatch_args->{args_to_new}->{TMPL_PATH}, |
|
306
|
|
|
|
|
|
|
); |
|
307
|
|
|
|
|
|
|
} |
|
308
|
|
|
|
|
|
|
} |
|
309
|
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
# now merge the args_to_new hashes |
|
311
|
22
|
|
|
|
|
29
|
$args{args_to_new} = {%{$dispatch_args->{args_to_new}}, %{$args{args_to_new}},}; |
|
|
22
|
|
|
|
|
48
|
|
|
|
22
|
|
|
|
|
89
|
|
|
312
|
|
|
|
|
|
|
} else { |
|
313
|
|
|
|
|
|
|
|
|
314
|
|
|
|
|
|
|
# anything else should override |
|
315
|
57
|
100
|
|
|
|
186
|
$args{$arg} = $dispatch_args->{$arg} unless exists $args{$arg}; |
|
316
|
|
|
|
|
|
|
} |
|
317
|
|
|
|
|
|
|
} |
|
318
|
|
|
|
|
|
|
|
|
319
|
22
|
50
|
|
|
|
62
|
$DEBUG = $args{debug} ? 1 : 0; |
|
320
|
|
|
|
|
|
|
|
|
321
|
|
|
|
|
|
|
# check for extra args (for backwards compatibility) |
|
322
|
22
|
|
|
|
|
55
|
for (keys %args) { |
|
323
|
|
|
|
|
|
|
next |
|
324
|
80
|
50
|
100
|
|
|
673
|
if( $_ eq 'prefix' |
|
|
|
|
66
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
325
|
|
|
|
|
|
|
or $_ eq 'default' |
|
326
|
|
|
|
|
|
|
or $_ eq 'debug' |
|
327
|
|
|
|
|
|
|
or $_ eq 'rm' |
|
328
|
|
|
|
|
|
|
or $_ eq 'args_to_new' |
|
329
|
|
|
|
|
|
|
or $_ eq 'table' |
|
330
|
|
|
|
|
|
|
or $_ eq 'auto_rest' |
|
331
|
|
|
|
|
|
|
or $_ eq 'auto_rest_lc' |
|
332
|
|
|
|
|
|
|
or $_ eq 'not_found' |
|
333
|
|
|
|
|
|
|
or $_ eq 'error_document'); |
|
334
|
1
|
|
|
|
|
185
|
carp "Passing extra args ('$_') to dispatch() is deprecated! Please use 'args_to_new'"; |
|
335
|
1
|
|
|
|
|
33
|
$args{args_to_new}->{$_} = delete $args{$_}; |
|
336
|
|
|
|
|
|
|
} |
|
337
|
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
# TODO: delete this block some time later |
|
339
|
22
|
50
|
|
|
|
70
|
if(exists $args{not_found}) { |
|
340
|
0
|
|
|
|
|
0
|
carp 'Passing not_found to dispatch() is deprecated! Please use error_document instead'; |
|
341
|
0
|
0
|
|
|
|
0
|
$args{error_document} = delete($args{not_found}) |
|
342
|
|
|
|
|
|
|
unless exists($args{error_document}); |
|
343
|
|
|
|
|
|
|
} |
|
344
|
|
|
|
|
|
|
|
|
345
|
22
|
|
|
|
|
49
|
%args = map { lc $_ => $args{$_} } keys %args; # lc for backwards |
|
|
79
|
|
|
|
|
241
|
|
|
346
|
|
|
|
|
|
|
# compatability |
|
347
|
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
# get the PATH_INFO |
|
349
|
22
|
|
|
|
|
90
|
my $path_info = $self->dispatch_path(); |
|
350
|
|
|
|
|
|
|
|
|
351
|
|
|
|
|
|
|
# use the 'default' if we need to |
|
352
|
22
|
100
|
50
|
|
|
113
|
$path_info = $args{default} || '' if(!$path_info || $path_info eq '/'); |
|
|
|
|
100
|
|
|
|
|
|
353
|
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
# make sure they all start and end with a '/', to correspond with |
|
355
|
|
|
|
|
|
|
# the RE we'll make |
|
356
|
22
|
100
|
|
|
|
59
|
$path_info = "/$path_info" unless(index($path_info, '/') == 0); |
|
357
|
22
|
100
|
|
|
|
66
|
$path_info = "$path_info/" unless(substr($path_info, -1) eq '/'); |
|
358
|
|
|
|
|
|
|
|
|
359
|
22
|
|
|
|
|
27
|
my ($module, $rm, $local_prefix, $local_args_to_new, $output); |
|
360
|
|
|
|
|
|
|
|
|
361
|
|
|
|
|
|
|
# take args from path |
|
362
|
0
|
|
|
|
|
0
|
my $named_args; |
|
363
|
|
|
|
|
|
|
try { |
|
364
|
22
|
100
|
|
22
|
|
670
|
$named_args = $self->_parse_path($path_info, $args{table}) |
|
365
|
|
|
|
|
|
|
or throw_not_found("Resource not found"); |
|
366
|
|
|
|
|
|
|
} catch { |
|
367
|
1
|
|
|
1
|
|
383
|
$output = $self->http_error($_, $args{error_document}); |
|
368
|
22
|
|
|
|
|
158
|
}; |
|
369
|
22
|
100
|
|
|
|
317
|
return $output if $output; |
|
370
|
|
|
|
|
|
|
|
|
371
|
21
|
50
|
|
|
|
50
|
if($DEBUG) { |
|
372
|
0
|
|
|
|
|
0
|
require Data::Dumper; |
|
373
|
0
|
|
|
|
|
0
|
warn "[Dispatch] Named args from match: " . Data::Dumper::Dumper($named_args) . "\n"; |
|
374
|
|
|
|
|
|
|
} |
|
375
|
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
# eval and catch any exceptions that might be thrown |
|
377
|
|
|
|
|
|
|
try { |
|
378
|
21
|
50
|
33
|
21
|
|
615
|
if(exists($named_args->{PARAMS}) || exists($named_args->{TMPL_PATH})) { |
|
379
|
0
|
|
|
|
|
0
|
carp "PARAMS and TMPL_PATH are not allowed here. Did you mean to use args_to_new?"; |
|
380
|
0
|
|
|
|
|
0
|
throw_error("PARAMS and TMPL_PATH not allowed"); |
|
381
|
|
|
|
|
|
|
} |
|
382
|
|
|
|
|
|
|
|
|
383
|
21
|
|
|
|
|
82
|
($module, $local_prefix, $rm, $local_args_to_new) = |
|
384
|
21
|
|
|
|
|
25
|
delete @{$named_args}{qw(app prefix rm args_to_new)}; |
|
385
|
|
|
|
|
|
|
|
|
386
|
|
|
|
|
|
|
# If another name for dispatch_url_remainder has been set move |
|
387
|
|
|
|
|
|
|
# the value to the requested name |
|
388
|
21
|
100
|
|
|
|
67
|
if($$named_args{'*'}) { |
|
389
|
1
|
|
|
|
|
4
|
$$named_args{$$named_args{'*'}} = $$named_args{'dispatch_url_remainder'}; |
|
390
|
1
|
|
|
|
|
2
|
delete $$named_args{'*'}; |
|
391
|
1
|
|
|
|
|
2
|
delete $$named_args{'dispatch_url_remainder'}; |
|
392
|
|
|
|
|
|
|
} |
|
393
|
|
|
|
|
|
|
|
|
394
|
21
|
100
|
|
|
|
46
|
$module or throw_error("App not defined"); |
|
395
|
20
|
|
|
|
|
64
|
$module = $self->translate_module_name($module); |
|
396
|
|
|
|
|
|
|
|
|
397
|
20
|
|
100
|
|
|
82
|
$local_prefix ||= $args{prefix}; |
|
398
|
20
|
100
|
|
|
|
55
|
$module = $local_prefix . '::' . $module if($local_prefix); |
|
399
|
|
|
|
|
|
|
|
|
400
|
20
|
|
66
|
|
|
70
|
$local_args_to_new ||= $args{args_to_new}; |
|
401
|
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
# add the rest of the named_args to PARAMS |
|
403
|
20
|
|
|
|
|
38
|
@{$local_args_to_new->{PARAMS}}{keys %$named_args} = values %$named_args; |
|
|
20
|
|
|
|
|
46
|
|
|
404
|
|
|
|
|
|
|
|
|
405
|
20
|
50
|
|
|
|
48
|
my $auto_rest = |
|
406
|
|
|
|
|
|
|
defined $named_args->{auto_rest} ? $named_args->{auto_rest} : $args{auto_rest}; |
|
407
|
20
|
0
|
33
|
|
|
49
|
if($auto_rest && defined $rm && length $rm) { |
|
|
|
|
33
|
|
|
|
|
|
408
|
0
|
0
|
|
|
|
0
|
my $method_lc = |
|
409
|
|
|
|
|
|
|
defined $named_args->{auto_rest_lc} |
|
410
|
|
|
|
|
|
|
? $named_args->{auto_rest_lc} |
|
411
|
|
|
|
|
|
|
: $args{auto_rest_lc}; |
|
412
|
0
|
|
|
|
|
0
|
my $http_method = $self->_http_method; |
|
413
|
0
|
0
|
|
|
|
0
|
$http_method = lc $http_method if $method_lc; |
|
414
|
0
|
|
|
|
|
0
|
$rm .= "_$http_method"; |
|
415
|
|
|
|
|
|
|
} |
|
416
|
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
# load and run the module |
|
418
|
20
|
|
|
|
|
60
|
$self->require_module($module); |
|
419
|
19
|
|
|
|
|
63
|
$output = $self->_run_app($module, $rm, $local_args_to_new); |
|
420
|
|
|
|
|
|
|
} catch { |
|
421
|
2
|
|
|
2
|
|
1242
|
my $e = $_; |
|
422
|
2
|
50
|
33
|
|
|
18
|
unless ( ref $e && $e->isa('Exception::Class::Base') ) { |
|
423
|
0
|
|
|
|
|
0
|
$e = Exception::Class::Base->new($e); |
|
424
|
|
|
|
|
|
|
} |
|
425
|
2
|
|
|
|
|
9
|
$output = $self->http_error($e, $args{error_document}); |
|
426
|
21
|
|
|
|
|
150
|
}; |
|
427
|
21
|
|
|
|
|
646
|
return $output; |
|
428
|
|
|
|
|
|
|
} |
|
429
|
|
|
|
|
|
|
|
|
430
|
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
=pod |
|
432
|
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
=head2 dispatch_path() |
|
434
|
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
This method returns the path that is to be processed. |
|
436
|
|
|
|
|
|
|
|
|
437
|
|
|
|
|
|
|
By default it returns the value of C<$ENV{PATH_INFO}> |
|
438
|
|
|
|
|
|
|
(or C<< $r->path_info >> under mod_perl) which should work for |
|
439
|
|
|
|
|
|
|
most cases. It allows the ability for subclasses to override the value if |
|
440
|
|
|
|
|
|
|
they need to do something more specific. |
|
441
|
|
|
|
|
|
|
|
|
442
|
|
|
|
|
|
|
=cut |
|
443
|
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
sub dispatch_path { |
|
445
|
23
|
|
|
23
|
1
|
103625
|
return $ENV{PATH_INFO}; |
|
446
|
|
|
|
|
|
|
} |
|
447
|
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
sub http_error { |
|
449
|
3
|
|
|
3
|
0
|
7
|
my ($self, $e, $errdoc) = @_; |
|
450
|
|
|
|
|
|
|
|
|
451
|
3
|
50
|
|
|
|
25
|
warn '[Dispatch] ERROR' |
|
452
|
|
|
|
|
|
|
. ($ENV{REQUEST_URI} ? " for request '$ENV{REQUEST_URI}': " : ': ') |
|
453
|
|
|
|
|
|
|
. $e->error . "\n"; |
|
454
|
|
|
|
|
|
|
|
|
455
|
3
|
50
|
|
|
|
102
|
my $errno = |
|
456
|
|
|
|
|
|
|
$e->isa('CGI::Application::Dispatch::Exception') |
|
457
|
|
|
|
|
|
|
? $e->description |
|
458
|
|
|
|
|
|
|
: 500; |
|
459
|
|
|
|
|
|
|
|
|
460
|
3
|
|
|
|
|
8
|
my ($url, $output); |
|
461
|
|
|
|
|
|
|
|
|
462
|
3
|
50
|
|
|
|
11
|
if($errdoc) { |
|
463
|
0
|
|
|
|
|
0
|
my $str = sprintf($errdoc, $errno); |
|
464
|
0
|
0
|
|
|
|
0
|
if(IS_MODPERL) { #compile out all other stuff |
|
|
|
0
|
|
|
|
|
|
|
465
|
|
|
|
|
|
|
$url = $str; # no messages, please |
|
466
|
0
|
|
|
|
|
0
|
} elsif(index($str, '"') == 0) { # Error message |
|
467
|
0
|
|
|
|
|
0
|
$output = substr($str, 1); |
|
468
|
|
|
|
|
|
|
} elsif(index($str, '<') == 0) { # Local file |
|
469
|
|
|
|
|
|
|
# Is it secure? |
|
470
|
0
|
|
|
|
|
0
|
require File::Spec; |
|
471
|
0
|
|
|
|
|
0
|
$str = File::Spec->catdir($ENV{DOCUMENT_ROOT}, substr($str, 1)); |
|
472
|
0
|
|
|
|
|
0
|
local *FH; |
|
473
|
0
|
0
|
0
|
|
|
0
|
if(-f $str && open(FH, '<', $str)) { |
|
474
|
0
|
|
|
|
|
0
|
local $/ = undef; |
|
475
|
0
|
|
|
|
|
0
|
$output = ; |
|
476
|
0
|
|
|
|
|
0
|
close FH; |
|
477
|
|
|
|
|
|
|
} else { |
|
478
|
0
|
|
|
|
|
0
|
warn "[Dispatch] Error opening error document '$str'.\n"; |
|
479
|
|
|
|
|
|
|
} |
|
480
|
|
|
|
|
|
|
} else { # Last case is url |
|
481
|
0
|
|
|
|
|
0
|
$url = $str; |
|
482
|
|
|
|
|
|
|
} |
|
483
|
|
|
|
|
|
|
|
|
484
|
0
|
0
|
|
|
|
0
|
if($DEBUG) { |
|
485
|
0
|
0
|
|
|
|
0
|
warn "[Dispatch] Redirection for HTTP error #$errno to $url\n" |
|
486
|
|
|
|
|
|
|
if $url; |
|
487
|
0
|
0
|
|
|
|
0
|
warn "[Dispatch] Displaying message for HTTP error #$errno\n" |
|
488
|
|
|
|
|
|
|
if $output; |
|
489
|
|
|
|
|
|
|
} |
|
490
|
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
} |
|
492
|
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
# if we're under mod_perl |
|
494
|
3
|
|
|
|
|
4
|
if(IS_MODPERL) { |
|
495
|
|
|
|
|
|
|
my $r = $self->_r; |
|
496
|
|
|
|
|
|
|
$r->status($errno); |
|
497
|
|
|
|
|
|
|
|
|
498
|
|
|
|
|
|
|
# if we just want to redirect |
|
499
|
|
|
|
|
|
|
$r->headers_out->{'Location'} = $url if $url; |
|
500
|
|
|
|
|
|
|
return ''; |
|
501
|
|
|
|
|
|
|
} else { # else print the HTTP stuff ourselves |
|
502
|
|
|
|
|
|
|
|
|
503
|
|
|
|
|
|
|
# stolen from http_protocol.c in Apache sources |
|
504
|
|
|
|
|
|
|
# we don't actually use anything other than 200, 307, 400, 404 and 500 |
|
505
|
|
|
|
|
|
|
|
|
506
|
3
|
|
|
|
|
19
|
my %status_lines = ( |
|
507
|
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
# 100 => 'Continue', |
|
509
|
|
|
|
|
|
|
# 101 => 'Switching Protocols', |
|
510
|
|
|
|
|
|
|
# 102 => 'Processing', |
|
511
|
|
|
|
|
|
|
200 => 'OK', |
|
512
|
|
|
|
|
|
|
|
|
513
|
|
|
|
|
|
|
# 201 => 'Created', |
|
514
|
|
|
|
|
|
|
# 202 => 'Accepted', |
|
515
|
|
|
|
|
|
|
# 203 => 'Non-Authoritative Information', |
|
516
|
|
|
|
|
|
|
# 204 => 'No Content', |
|
517
|
|
|
|
|
|
|
# 205 => 'Reset Content', |
|
518
|
|
|
|
|
|
|
# 206 => 'Partial Content', |
|
519
|
|
|
|
|
|
|
# 207 => 'Multi-Status', |
|
520
|
|
|
|
|
|
|
# 300 => 'Multiple Choices', |
|
521
|
|
|
|
|
|
|
# 301 => 'Moved Permanently', |
|
522
|
|
|
|
|
|
|
# 302 => 'Found', |
|
523
|
|
|
|
|
|
|
# 303 => 'See Other', |
|
524
|
|
|
|
|
|
|
# 304 => 'Not Modified', |
|
525
|
|
|
|
|
|
|
# 305 => 'Use Proxy', |
|
526
|
|
|
|
|
|
|
307 => 'Temporary Redirect', |
|
527
|
|
|
|
|
|
|
400 => 'Bad Request', |
|
528
|
|
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
# 401 => 'Authorization Required', |
|
530
|
|
|
|
|
|
|
# 402 => 'Payment Required', |
|
531
|
|
|
|
|
|
|
# 403 => 'Forbidden', |
|
532
|
|
|
|
|
|
|
404 => 'Not Found', |
|
533
|
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
# 405 => 'Method Not Allowed', |
|
535
|
|
|
|
|
|
|
# 406 => 'Not Acceptable', |
|
536
|
|
|
|
|
|
|
# 407 => 'Proxy Authentication Required', |
|
537
|
|
|
|
|
|
|
# 408 => 'Request Time-out', |
|
538
|
|
|
|
|
|
|
# 409 => 'Conflict', |
|
539
|
|
|
|
|
|
|
# 410 => 'Gone', |
|
540
|
|
|
|
|
|
|
# 411 => 'Length Required', |
|
541
|
|
|
|
|
|
|
# 412 => 'Precondition Failed', |
|
542
|
|
|
|
|
|
|
# 413 => 'Request Entity Too Large', |
|
543
|
|
|
|
|
|
|
# 414 => 'Request-URI Too Large', |
|
544
|
|
|
|
|
|
|
# 415 => 'Unsupported Media Type', |
|
545
|
|
|
|
|
|
|
# 416 => 'Requested Range Not Satisfiable', |
|
546
|
|
|
|
|
|
|
# 417 => 'Expectation Failed', |
|
547
|
|
|
|
|
|
|
# 422 => 'Unprocessable Entity', |
|
548
|
|
|
|
|
|
|
# 423 => 'Locked', |
|
549
|
|
|
|
|
|
|
# 424 => 'Failed Dependency', |
|
550
|
|
|
|
|
|
|
500 => 'Internal Server Error', |
|
551
|
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
# 501 => 'Method Not Implemented', |
|
553
|
|
|
|
|
|
|
# 502 => 'Bad Gateway', |
|
554
|
|
|
|
|
|
|
# 503 => 'Service Temporarily Unavailable', |
|
555
|
|
|
|
|
|
|
# 504 => 'Gateway Time-out', |
|
556
|
|
|
|
|
|
|
# 505 => 'HTTP Version Not Supported', |
|
557
|
|
|
|
|
|
|
# 506 => 'Variant Also Negotiates', |
|
558
|
|
|
|
|
|
|
# 507 => 'Insufficient Storage', |
|
559
|
|
|
|
|
|
|
# 510 => 'Not Extended', |
|
560
|
|
|
|
|
|
|
); |
|
561
|
|
|
|
|
|
|
|
|
562
|
3
|
50
|
|
|
|
10
|
$errno = 500 if(!exists $status_lines{$errno}); |
|
563
|
|
|
|
|
|
|
|
|
564
|
3
|
50
|
|
|
|
7
|
if($url) { |
|
565
|
|
|
|
|
|
|
|
|
566
|
|
|
|
|
|
|
# somewhat mailformed header, no errors in access.log, but browsers |
|
567
|
|
|
|
|
|
|
# display contents of $url document and old URI in address bar. |
|
568
|
0
|
|
|
|
|
0
|
$output = "HTTP/1.0 $errno $status_lines{$errno}\n"; |
|
569
|
0
|
|
|
|
|
0
|
$output .= "Location: $url\n\n"; |
|
570
|
|
|
|
|
|
|
} else { |
|
571
|
|
|
|
|
|
|
|
|
572
|
3
|
50
|
|
|
|
8
|
unless($output) { |
|
573
|
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
# TODO: possibly provide more feedback in a way that |
|
575
|
|
|
|
|
|
|
# is XSS safe. (I'm not sure that passing through the |
|
576
|
|
|
|
|
|
|
# raw ENV variable directly is safe.) |
|
577
|
|
|
|
|
|
|
# We tried: $ENV{REQUEST_URI} |