| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package CGI::Snapp::Dispatch::Regexp; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
12841
|
use parent 'CGI::Snapp::Dispatch'; |
|
|
2
|
|
|
|
|
709
|
|
|
|
2
|
|
|
|
|
11
|
|
|
4
|
2
|
|
|
2
|
|
107
|
use strict; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
77
|
|
|
5
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
77
|
|
|
6
|
|
|
|
|
|
|
|
|
7
|
2
|
|
|
2
|
|
10
|
use Carp; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
158
|
|
|
8
|
|
|
|
|
|
|
|
|
9
|
2
|
|
|
2
|
|
10
|
use Hash::FieldHash ':all'; |
|
|
2
|
|
|
|
|
4
|
|
|
|
2
|
|
|
|
|
1004
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = '1.04'; |
|
12
|
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
# -------------------------------------------------- |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
sub dispatch_args |
|
16
|
|
|
|
|
|
|
{ |
|
17
|
2
|
|
|
2
|
1
|
5
|
my($self, $args) = @_; |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
return |
|
20
|
|
|
|
|
|
|
{ |
|
21
|
2
|
|
|
|
|
44
|
args_to_new => {}, |
|
22
|
|
|
|
|
|
|
default => '', |
|
23
|
|
|
|
|
|
|
prefix => '', |
|
24
|
|
|
|
|
|
|
table => |
|
25
|
|
|
|
|
|
|
[ |
|
26
|
|
|
|
|
|
|
qr|/([^/]+)/?| => {names => ['app']}, |
|
27
|
|
|
|
|
|
|
qr|/([^/]+)/([^/]+)/?| => {names => [qw/app rm/]}, |
|
28
|
|
|
|
|
|
|
], |
|
29
|
|
|
|
|
|
|
}; |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
} # End of dispatch_args. |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
# -------------------------------------------------- |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub _parse_path |
|
36
|
|
|
|
|
|
|
{ |
|
37
|
2
|
|
|
2
|
|
5
|
my($self, $http_method, $path_info, $table) = @_; |
|
38
|
|
|
|
|
|
|
|
|
39
|
2
|
|
|
|
|
8
|
$self -> log(debug => "_parse_path($path_info, ...)"); |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
# Compare each rule in the table with the path_info, and process the 1st match. |
|
42
|
|
|
|
|
|
|
|
|
43
|
2
|
|
|
|
|
2
|
my($rule); |
|
44
|
|
|
|
|
|
|
|
|
45
|
2
|
|
|
|
|
8
|
for (my $i = 0; $i < scalar @$table; $i += 2) |
|
46
|
|
|
|
|
|
|
{ |
|
47
|
2
|
|
|
|
|
4
|
$rule = $$table[$i]; |
|
48
|
|
|
|
|
|
|
|
|
49
|
2
|
50
|
|
|
|
13
|
next if (! defined $rule); |
|
50
|
|
|
|
|
|
|
|
|
51
|
2
|
|
|
|
|
11
|
$self -> log(debug => "Trying to match path info '$path_info' against rule '$rule'"); |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
# If we find a match, then run with it. |
|
54
|
|
|
|
|
|
|
|
|
55
|
2
|
50
|
|
|
|
68
|
if (my @values = ($path_info =~ m#^$rule$#) ) |
|
56
|
|
|
|
|
|
|
{ |
|
57
|
2
|
|
|
|
|
7
|
$self -> log(debug => 'Matched!'); |
|
58
|
|
|
|
|
|
|
|
|
59
|
2
|
|
|
|
|
3
|
my(%named_args) = %{$$table[++$i]}; |
|
|
2
|
|
|
|
|
9
|
|
|
60
|
2
|
|
|
|
|
8
|
my($names) = delete $named_args{names}; |
|
61
|
2
|
50
|
|
|
|
10
|
@named_args{@$names} = @values if (ref $names eq 'ARRAY'); |
|
62
|
|
|
|
|
|
|
|
|
63
|
2
|
|
|
|
|
12
|
return {%named_args}; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# No rule matched the given path info. |
|
68
|
|
|
|
|
|
|
|
|
69
|
0
|
|
|
|
|
|
$self -> log(debug => 'Nothing matched'); |
|
70
|
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
return {}; |
|
72
|
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
} # End of _parse_path. |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# -------------------------------------------------- |
|
76
|
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
1; |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=pod |
|
80
|
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
=head1 NAME |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
CGI::Snapp::Dispatch::Regexp - Dispatch requests to CGI::Snapp-based objects |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
=head1 Synopsis |
|
86
|
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
I |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
use CGI::Snapp::Dispatch::Regexp; |
|
90
|
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
CGI::Snapp::Dispatch::Regexp -> new -> dispatch |
|
92
|
|
|
|
|
|
|
( |
|
93
|
|
|
|
|
|
|
prefix => 'MyApp', |
|
94
|
|
|
|
|
|
|
table => |
|
95
|
|
|
|
|
|
|
[ |
|
96
|
|
|
|
|
|
|
qr|/([^/]+)/?| => { names => ['app'] }, |
|
97
|
|
|
|
|
|
|
qr|/([^/]+)/([^/]+)/?| => { names => [qw(app rm)] }, |
|
98
|
|
|
|
|
|
|
qr|/([^/]+)/([^/]+)/page(\d+)\.html?| => { names => [qw(app rm page)] }, |
|
99
|
|
|
|
|
|
|
], |
|
100
|
|
|
|
|
|
|
); |
|
101
|
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
This would also work in a PSGI context. I |
|
103
|
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
CGI::Snapp::Dispatch::Regexp -> new -> as_psgi |
|
105
|
|
|
|
|
|
|
( |
|
106
|
|
|
|
|
|
|
... |
|
107
|
|
|
|
|
|
|
); |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
See t/psgi.regexp.t and t/regexp.t. |
|
110
|
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
This usage of new(), so unlike L, is dicussed in L. |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
=head1 Description |
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
CGI::Snapp::Dispatch::Regexp is a sub-class of L which overrides 2 methods: |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=over 4 |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=item o dispatch_args() |
|
120
|
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=item o _parse_path() |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
=back |
|
124
|
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
The point is to allow you to use regexps as rules to match the path info, whereas L always |
|
126
|
|
|
|
|
|
|
assumes your rules are strings. |
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
See L for more detail. |
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head1 Distributions |
|
131
|
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
This module is available as a Unix-style distro (*.tgz). |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
See L |
|
135
|
|
|
|
|
|
|
for help on unpacking and installing distros. |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=head1 Installation |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
Install L as you would for any C module: |
|
140
|
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
Run: |
|
142
|
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
cpanm CGI::Snapp::Dispatch |
|
144
|
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
or run: |
|
146
|
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sudo cpan CGI::Snapp::Dispatch |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
or unpack the distro, and then either: |
|
150
|
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
perl Build.PL |
|
152
|
|
|
|
|
|
|
./Build |
|
153
|
|
|
|
|
|
|
./Build test |
|
154
|
|
|
|
|
|
|
sudo ./Build install |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
or: |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
perl Makefile.PL |
|
159
|
|
|
|
|
|
|
make (or dmake or nmake) |
|
160
|
|
|
|
|
|
|
make test |
|
161
|
|
|
|
|
|
|
make install |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 Constructor and Initialization |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
C is called as C<< my($app) = CGI::Snapp::Dispatch::Regexp -> new(k1 => v1, k2 => v2, ...) >>. |
|
166
|
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
It returns a new object of type C. |
|
168
|
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
This module accepts exactly the same parameters as does L. |
|
170
|
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
See L for details. |
|
172
|
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head1 Methods |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=head2 dispatch_args($args) |
|
176
|
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
Returns a hashref of args to be used by L or |
|
178
|
|
|
|
|
|
|
L. |
|
179
|
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
Default output: |
|
181
|
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
{ |
|
183
|
|
|
|
|
|
|
args_to_new => {}, |
|
184
|
|
|
|
|
|
|
default => '', |
|
185
|
|
|
|
|
|
|
prefix => '', |
|
186
|
|
|
|
|
|
|
table => |
|
187
|
|
|
|
|
|
|
[ |
|
188
|
|
|
|
|
|
|
qr|/([^/]+)/?| => {names => ['app']}, |
|
189
|
|
|
|
|
|
|
qr|/([^/]+)/([^/]+)/?| => {names => [qw/app rm/]}, |
|
190
|
|
|
|
|
|
|
], |
|
191
|
|
|
|
|
|
|
}; |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
The differences between this structure and what's used by L are discussed in the L. |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
=head1 FAQ |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head2 Is there any sample code? |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Yes. See t/psgi.regexp.t and t/regexp.t. |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
This module works with both L and L. |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head2 What is the structure of the dispatch table? |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
Basically it's the same as in L. |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
The important difference is in the I key, which can be seen just above, under L.
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
The pairs of elements in the I, compared to what's handled by L are:
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=over 4 |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
=item o A regexp instead of a string |
|
214
|
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
=item o A hashref with a key of I and an array ref of field names |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=back |
|
218
|
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
See the L for a more complex example. |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head1 Troubleshooting |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
See L. |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head1 Machine-Readable Change Log |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
The file CHANGES was converted into Changelog.ini by L. |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=head1 Version Numbers |
|
230
|
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
Version numbers < 1.00 represent development versions. From 1.00 up, they are production versions. |
|
232
|
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
=head1 Credits |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
See L. This module is a fork of that code. |
|
236
|
|
|
|
|
|
|
|
|
237
|
|
|
|
|
|
|
=head1 Support |
|
238
|
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Email the author, or log a bug on RT: |
|
240
|
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
L. |
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
=head1 Author |
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
L was written by Ron Savage Iron@savage.net.auE> in 2012. |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
Home page: L. |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=head1 Copyright |
|
250
|
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
Australian copyright (c) 2012, Ron Savage. |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
All Programs of mine are 'OSI Certified Open Source Software'; |
|
254
|
|
|
|
|
|
|
you can redistribute them and/or modify them under the terms of |
|
255
|
|
|
|
|
|
|
The Artistic License, a copy of which is available at: |
|
256
|
|
|
|
|
|
|
http://www.opensource.org/licenses/index.html |
|
257
|
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
=cut |
| |