line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MVC::Neaf; |
2
|
|
|
|
|
|
|
|
3
|
81
|
|
|
81
|
|
4665241
|
use 5.006; |
|
81
|
|
|
|
|
1006
|
|
4
|
81
|
|
|
81
|
|
523
|
use strict; |
|
81
|
|
|
|
|
167
|
|
|
81
|
|
|
|
|
1921
|
|
5
|
81
|
|
|
81
|
|
387
|
use warnings; |
|
81
|
|
|
|
|
145
|
|
|
81
|
|
|
|
|
6210
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.2901'; |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 NAME |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
MVC::Neaf - Not Even A (Web Application) Framework |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 OVERVIEW |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Neaf C<[ni:f]> stands for Not Even A Framework. |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
The B is assumed to be just a regular Perl module, |
18
|
|
|
|
|
|
|
no restrictions are imposed on it. |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
The B is an object with one method, C, receiving a hashref |
21
|
|
|
|
|
|
|
and returning rendered content as string plus optional content-type header. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
The B is a prefix tree of subroutines called I. |
24
|
|
|
|
|
|
|
Each such handler receives a L object |
25
|
|
|
|
|
|
|
containing I it needs to know about the outside world, |
26
|
|
|
|
|
|
|
and returns a simple C<\%hashref> which is forwarded to View. |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
Alternatively, it can die. C is a valid way to return |
29
|
|
|
|
|
|
|
a customizable "404 Not Found" page. |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
Please see the C directory in this distribution |
32
|
|
|
|
|
|
|
that demonstrates the features of Neaf. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=head1 SYNOPSIS |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
The following application, outputting a greeting, is ready to run |
37
|
|
|
|
|
|
|
as a L script, L application, or Apache handler. |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
use strict; |
40
|
|
|
|
|
|
|
use warnings; |
41
|
|
|
|
|
|
|
use MVC::Neaf; |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
get+post '/hello' => sub { |
44
|
|
|
|
|
|
|
my $req = shift; |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my $name = $req->param( name => qr/[-'\w\s]+/ ) || "Mystical stranger"; |
47
|
|
|
|
|
|
|
return { |
48
|
|
|
|
|
|
|
name => $name, |
49
|
|
|
|
|
|
|
}; |
50
|
|
|
|
|
|
|
}, default => { |
51
|
|
|
|
|
|
|
-view => 'TT', |
52
|
|
|
|
|
|
|
-type => "text/plain", |
53
|
|
|
|
|
|
|
-template => \"Hello, [% name %]", |
54
|
|
|
|
|
|
|
}; |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
neaf->run; |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
A neaf application has some command-line interface built in: |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
perl myapp.pl --list |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Will give a summary of available routes. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
perl myapp.pl --listen :31415 |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Will start a default C server (C works as well) |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
perl myapp.pl --post --upload foo=/path/to/file /bar?life=42 --view Dumper |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
Will run just one request and stop right before template processing, |
71
|
|
|
|
|
|
|
dumping stash instead. |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=head1 CREATING AN APPLICATION |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=head2 THE CONTROLLER |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
The handler sub receives one and only argument, the B object, |
78
|
|
|
|
|
|
|
and outputs a C<\%hashref>. |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
It may also die, which will be interpreted as an error 500, |
81
|
|
|
|
|
|
|
UNLESS error message starts with 3 digits and a whitespace, |
82
|
|
|
|
|
|
|
in which case this is considered the return status. |
83
|
|
|
|
|
|
|
E.g. C is a valid method to return |
84
|
|
|
|
|
|
|
a configurable "Not Found" page right away. |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
Handlers are set up using the L method discussed below. |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=head2 THE REQUEST |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
L interface is |
91
|
|
|
|
|
|
|
similar to that of L or L with some minor differences: |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# What was requested: |
94
|
|
|
|
|
|
|
http(s)://server.name:1337/mathing/route/some/more/slashes?foo=1&bar=2 |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
# What is being returned: |
97
|
|
|
|
|
|
|
$req->http_version; # = HTTP/1.0 or HTTP/1.1 |
98
|
|
|
|
|
|
|
$req->scheme ; # = http or https |
99
|
|
|
|
|
|
|
$req->method ; # = GET |
100
|
|
|
|
|
|
|
$req->hostname ; # = server.name |
101
|
|
|
|
|
|
|
$req->port ; # = 1337 |
102
|
|
|
|
|
|
|
$req->path ; # = /mathing/route/some/more/slashes |
103
|
|
|
|
|
|
|
$req->prefix ; # = /mathing/route |
104
|
|
|
|
|
|
|
$req->postfix ; # = /some/more/slashes |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
$req->param( foo => '\d+' ); # = 1 |
107
|
|
|
|
|
|
|
$req->get_cookie( session => '.+' ); # = whatever it was set to before |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
One I difference is that there's no (easy) way to fetch |
110
|
|
|
|
|
|
|
query parameters or cookies without validation. |
111
|
|
|
|
|
|
|
Just use pattern C if you know better. |
112
|
|
|
|
|
|
|
But see also L, forms are quite powerful. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
Also there are some methods that affect the reply, |
115
|
|
|
|
|
|
|
mainly the headers, like C or C. |
116
|
|
|
|
|
|
|
This is a step towards a know-it-all God object, |
117
|
|
|
|
|
|
|
however, mapping those properties into a hashref turned out to be |
118
|
|
|
|
|
|
|
too cumbersome. |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=head2 THE RESPONSE |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
B may contain regular keys, typically alphanumeric, |
123
|
|
|
|
|
|
|
as well as a predefined set of dash-prefixed keys to control |
124
|
|
|
|
|
|
|
Neaf itself. |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
return { |
127
|
|
|
|
|
|
|
-view => 'TT', |
128
|
|
|
|
|
|
|
-template => 'users.html', |
129
|
|
|
|
|
|
|
users => \@list, |
130
|
|
|
|
|
|
|
extras => \%hash, |
131
|
|
|
|
|
|
|
}; |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
And that's it. |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
I<-Note -that -dash-prefixed -options -look -antique |
136
|
|
|
|
|
|
|
even to the author of this writing. |
137
|
|
|
|
|
|
|
However, it is a concise and B way to separate |
138
|
|
|
|
|
|
|
auxiliary parameters from users's data, |
139
|
|
|
|
|
|
|
without requiring a more complex return structure |
140
|
|
|
|
|
|
|
(two hashes, array of arrays etc).> |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
The small but growing list of these -options is as follows: |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=over |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
=item * -content - Return raw data and skip view processing. |
147
|
|
|
|
|
|
|
E.g. display generated image. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=item * -continue - A callback that receives the Request object. |
150
|
|
|
|
|
|
|
It will be executed AFTER the headers and the first content chunk |
151
|
|
|
|
|
|
|
are served to the client, and may use C<$req-Ewrite( $data );> |
152
|
|
|
|
|
|
|
and C<$req-Eclose;> to output more data. |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=item * -headers - Pass a hash or array of values for header generation. |
155
|
|
|
|
|
|
|
This is an alternative to L's C method. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=item * -jsonp - Used by C view module as a callback name to produce a |
158
|
|
|
|
|
|
|
L response. |
159
|
|
|
|
|
|
|
Callback MUST be a set of identifiers separated by dots. |
160
|
|
|
|
|
|
|
Otherwise it's ignored for security reasons. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=item * -location - HTTP Location: header for 3xx statuses. |
163
|
|
|
|
|
|
|
This is set by C<$request-Eredirect(...)>. |
164
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
B<[DEPRECATED]> This will be phased out at some point, |
166
|
|
|
|
|
|
|
use C<-header =E [ location =E ... ]> instead. |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=item * -payload - if present, the C view will render this instead of |
169
|
|
|
|
|
|
|
the whole response hash. |
170
|
|
|
|
|
|
|
This can be used, for instance, to return non-hash data in a REST API. |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Also used to be C<-serial> which is now deprecated. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> Name and meaning may change in the future. |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=item * -status - HTTP status (200, 404, 500 etc). |
177
|
|
|
|
|
|
|
Default is 200 if the handler managed to live through, and 500 if it died. |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item * -template - Set template name for a text processing view |
180
|
|
|
|
|
|
|
(currently L based on L). |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
=item * -type - Content-type HTTP header. |
183
|
|
|
|
|
|
|
View module may set this parameter if unset. |
184
|
|
|
|
|
|
|
Default is generated by the renderer - see L. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=item * -view - select B module. |
187
|
|
|
|
|
|
|
Views are initialized lazily and cached by the framework. |
188
|
|
|
|
|
|
|
C, C, C, and C<$view_predefined_object> |
189
|
|
|
|
|
|
|
are currently supported. |
190
|
|
|
|
|
|
|
New short aliases may be created by |
191
|
|
|
|
|
|
|
Cload_view( "name" =E $your_view );> (see below). |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
The default is C denoting the the L engine. |
194
|
|
|
|
|
|
|
Adding C<-template> key will cause switching to C, |
195
|
|
|
|
|
|
|
but it is deprecated and will go away in v.0.25. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=back |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Though more dash-prefixed parameters may be returned |
200
|
|
|
|
|
|
|
and will be passed to the View module as of current, |
201
|
|
|
|
|
|
|
they are not guaranteed to work in the future. |
202
|
|
|
|
|
|
|
Please either avoid them, or send patches. |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=head1 FUNCTIONAL AND OBJECT-ORIENTED API |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
By default, NEAF exports a pretty standard route declaration interface: |
207
|
|
|
|
|
|
|
C + C + C + C + C + C for delete, |
208
|
|
|
|
|
|
|
C for setting up custom method combinations, |
209
|
|
|
|
|
|
|
and a single L function (see below) for configuring the application. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
A C<:sugar> export keyword was used for it previously, |
212
|
|
|
|
|
|
|
but it is no longer needed. |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
All prototyped declarative functions described below |
215
|
|
|
|
|
|
|
are really frontends to a single L instance |
216
|
|
|
|
|
|
|
which is also returned by a C call without parameters. |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
More than one neaf application object can be created as simply |
219
|
|
|
|
|
|
|
as Cnew> if anybody needs that. |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
Given the above, functional and object-oriented ways |
222
|
|
|
|
|
|
|
to declare the same thing will now follow in pairs. |
223
|
|
|
|
|
|
|
See L for implementation details. |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Returned value, unless specified otherwise, |
226
|
|
|
|
|
|
|
is always the Neaf application itself (but who cares). |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=cut |
229
|
|
|
|
|
|
|
|
230
|
81
|
|
|
81
|
|
509
|
use Carp; |
|
81
|
|
|
|
|
209
|
|
|
81
|
|
|
|
|
5946
|
|
231
|
81
|
|
|
81
|
|
601
|
use Scalar::Util qw( blessed reftype ); |
|
81
|
|
|
|
|
227
|
|
|
81
|
|
|
|
|
4655
|
|
232
|
81
|
|
|
81
|
|
32462
|
use parent qw(Exporter); |
|
81
|
|
|
|
|
22378
|
|
|
81
|
|
|
|
|
522
|
|
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
our @EXPORT; |
235
|
|
|
|
|
|
|
our @EXPORT_OK = qw( neaf_err ); |
236
|
|
|
|
|
|
|
my @EXPORT_SUGAR = qw( neaf ); # Will populate later - see @ALL_METHODS below |
237
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( |
238
|
|
|
|
|
|
|
sugar => \@EXPORT_SUGAR, |
239
|
|
|
|
|
|
|
); |
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
# NOTE We want MVC::Neaf->new() to create an application object. |
242
|
|
|
|
|
|
|
# We also want to profoundly document all the methods of said. |
243
|
|
|
|
|
|
|
# We also want `perdoc MVC::Neaf` to introduce the framework |
244
|
|
|
|
|
|
|
# and its DSL and features. |
245
|
|
|
|
|
|
|
# And thus we outsource our numerous helper methods to a separate class |
246
|
|
|
|
|
|
|
# and only leave exported stuff in this file. |
247
|
81
|
|
|
81
|
|
8742
|
use parent qw(MVC::Neaf::Route::Main); |
|
81
|
|
|
|
|
173
|
|
|
81
|
|
|
|
|
301
|
|
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
our $Inst; |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
=head2 add_route() |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
The add_route() function and its numerous aliases define a handler |
254
|
|
|
|
|
|
|
for given by URI path and HTTP method(s). |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
$neaf->add_route( '/path' => CODEREF, %options ) |
257
|
|
|
|
|
|
|
|
258
|
|
|
|
|
|
|
is equivalent to |
259
|
|
|
|
|
|
|
|
260
|
|
|
|
|
|
|
get+post '/path' => sub { CODE; }, %options; |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=over |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
=item * post '/path' => sub { CODE; }, %options; |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
Ditto, but sets method => 'POST' |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
=item * head ... - autogenerated by C, |
269
|
|
|
|
|
|
|
but can be specified explicitly if needed |
270
|
|
|
|
|
|
|
|
271
|
|
|
|
|
|
|
=item * put ... |
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
=item * patch ... |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
=item * del ... is for C (because C is a Perl's own keyword). |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=item * any [ 'get', 'post', 'CUSTOM_METHOD' ] => '/path' => \&handler |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=back |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
HTTP method declarations can be combined using the C<+> sign, as in |
282
|
|
|
|
|
|
|
|
283
|
|
|
|
|
|
|
get + post '/submit' => sub { |
284
|
|
|
|
|
|
|
my $req = shift; |
285
|
|
|
|
|
|
|
# do a lot of common stuff here |
286
|
|
|
|
|
|
|
if ($req->is_post) { |
287
|
|
|
|
|
|
|
# a few lines unique to POST method |
288
|
|
|
|
|
|
|
$req->redirect('/done'); |
289
|
|
|
|
|
|
|
}; |
290
|
|
|
|
|
|
|
return { ... } |
291
|
|
|
|
|
|
|
}; |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
post + put + patch '/some/item' => sub { |
294
|
|
|
|
|
|
|
my $req = shift; |
295
|
|
|
|
|
|
|
# generate item from $req->body |
296
|
|
|
|
|
|
|
}; |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
Any incoming request to uri matching C |
299
|
|
|
|
|
|
|
(C too, but NOT C) |
300
|
|
|
|
|
|
|
will now be directed to CODEREF. |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
Longer paths are GUARANTEED to be checked first. |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
Dies if the same method and path combination is given twice |
305
|
|
|
|
|
|
|
(but see C and C below). |
306
|
|
|
|
|
|
|
Multiple methods may be given for the same path. |
307
|
|
|
|
|
|
|
|
308
|
|
|
|
|
|
|
Exactly one leading slash will be prepended no matter what you do. |
309
|
|
|
|
|
|
|
(C, C and C////path> are all the same). |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
The C MUST accept exactly one argument, |
312
|
|
|
|
|
|
|
referred to as C<$request> or C<$req> hereafter, |
313
|
|
|
|
|
|
|
and return an unblessed hashref with response data. |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
%options may include: |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=over |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
=item * C - list of allowed HTTP methods. |
320
|
|
|
|
|
|
|
Default is [GET, POST]. |
321
|
|
|
|
|
|
|
Multiple handles can be defined for the same path, provided that |
322
|
|
|
|
|
|
|
methods do not intersect. |
323
|
|
|
|
|
|
|
HEAD method is automatically handled if GET is present, however, |
324
|
|
|
|
|
|
|
one MAY define a separate HEAD handler explicitly. |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
=item * C => C - allow URI subpaths |
327
|
|
|
|
|
|
|
to be handled by this handler. |
328
|
|
|
|
|
|
|
|
329
|
|
|
|
|
|
|
A 404 error will be generated unless C is present |
330
|
|
|
|
|
|
|
and PATH_INFO matches the regex (without the leading slashes). |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
If path_info_regex matches, it will be available in the controller |
333
|
|
|
|
|
|
|
as C<$req-Epostfix>. |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
If capture groups are present in said regular expression, |
336
|
|
|
|
|
|
|
their content will also be available as C<$req-Epath_info_split>. |
337
|
|
|
|
|
|
|
|
338
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> Name and semantics MAY change in the future. |
339
|
|
|
|
|
|
|
|
340
|
|
|
|
|
|
|
=item * C => { name => C, name2 => C<'\d+'> } |
341
|
|
|
|
|
|
|
|
342
|
|
|
|
|
|
|
Add predefined regular expression validation to certain request parameters, |
343
|
|
|
|
|
|
|
so that they can be queried by name only. |
344
|
|
|
|
|
|
|
See L. |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> Name and semantics MAY change in the future. |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
=item * strict => 1|0 |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
If true, request's C and C |
351
|
|
|
|
|
|
|
will emit HTTP error 422 |
352
|
|
|
|
|
|
|
whenever mandatory validation fails. |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
If parameter or cookie is missing, just return default. |
355
|
|
|
|
|
|
|
This MAY change in the future. |
356
|
|
|
|
|
|
|
|
357
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> Name and meaning MAY change in the future. |
358
|
|
|
|
|
|
|
|
359
|
|
|
|
|
|
|
=item * C - default View object for this Controller. |
360
|
|
|
|
|
|
|
Must be a name of preloaded view, |
361
|
|
|
|
|
|
|
an object with a C method, or a CODEREF |
362
|
|
|
|
|
|
|
receiving hashref and returning a list of two scalars |
363
|
|
|
|
|
|
|
(content and content-type). |
364
|
|
|
|
|
|
|
|
365
|
|
|
|
|
|
|
B<[DEPRECATED]> Use C<-view> instead, meaning is exactly the same. |
366
|
|
|
|
|
|
|
|
367
|
|
|
|
|
|
|
=item * C - if set, set Expires: HTTP header accordingly. |
368
|
|
|
|
|
|
|
|
369
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> Name and semantics MAY change in the future. |
370
|
|
|
|
|
|
|
|
371
|
|
|
|
|
|
|
=item * C - a C<\%hash> of fallback values to be added to hash |
372
|
|
|
|
|
|
|
returned by the handler. |
373
|
|
|
|
|
|
|
Consider using C below if you need to append |
374
|
|
|
|
|
|
|
the same values to multiple handlers. |
375
|
|
|
|
|
|
|
|
376
|
|
|
|
|
|
|
=item * C => 1 - replace old route even if it exists. |
377
|
|
|
|
|
|
|
If not set, route collisions causes exception. |
378
|
|
|
|
|
|
|
Use this if you know better. |
379
|
|
|
|
|
|
|
|
380
|
|
|
|
|
|
|
This still issues a warning. |
381
|
|
|
|
|
|
|
|
382
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> Name and meaning may change in the future. |
383
|
|
|
|
|
|
|
|
384
|
|
|
|
|
|
|
=item * C => 1 - if route is already defined, do nothing. |
385
|
|
|
|
|
|
|
If not, allow to redefine it later. |
386
|
|
|
|
|
|
|
|
387
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> Name and meaning may change in the future. |
388
|
|
|
|
|
|
|
|
389
|
|
|
|
|
|
|
=item * C - just for information, has no action on execution. |
390
|
|
|
|
|
|
|
This will be displayed if application called with --list (see L). |
391
|
|
|
|
|
|
|
|
392
|
|
|
|
|
|
|
=item * C => 0|1 - a flag just for information. |
393
|
|
|
|
|
|
|
In theory, public endpoints should be searchable from the outside |
394
|
|
|
|
|
|
|
while non-public ones should only be reachable from other parts of application. |
395
|
|
|
|
|
|
|
This is not enforced whatsoever. |
396
|
|
|
|
|
|
|
|
397
|
|
|
|
|
|
|
=back |
398
|
|
|
|
|
|
|
|
399
|
|
|
|
|
|
|
Also, any number of dash-prefixed keys MAY be present. |
400
|
|
|
|
|
|
|
This is the same as putting them into C hash. |
401
|
|
|
|
|
|
|
|
402
|
|
|
|
|
|
|
See L for implementation. |
403
|
|
|
|
|
|
|
|
404
|
|
|
|
|
|
|
=cut |
405
|
|
|
|
|
|
|
|
406
|
|
|
|
|
|
|
=head2 static() |
407
|
|
|
|
|
|
|
|
408
|
|
|
|
|
|
|
neaf static => '/path' => $local_path, %options; |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
neaf static => '/other/path' => [ $content, $content_type ]; |
411
|
|
|
|
|
|
|
|
412
|
|
|
|
|
|
|
$neaf->static( $req_path => $file_path, %options ) |
413
|
|
|
|
|
|
|
|
414
|
|
|
|
|
|
|
Serve static content located under C<$file_path>. |
415
|
|
|
|
|
|
|
Both directories and single files may be added. |
416
|
|
|
|
|
|
|
|
417
|
|
|
|
|
|
|
Note that non-absolute local paths will be calculated relative to |
418
|
|
|
|
|
|
|
the file where static() was called, not current working directory. |
419
|
|
|
|
|
|
|
For files ending in C<.pm> and having a matching package name, |
420
|
|
|
|
|
|
|
the file name without C<.pm> suffix will be used: |
421
|
|
|
|
|
|
|
|
422
|
|
|
|
|
|
|
# in /www/lib/perl5/My/App.pm |
423
|
|
|
|
|
|
|
package My::App; |
424
|
|
|
|
|
|
|
use MVC::Neaf; |
425
|
|
|
|
|
|
|
neaf static => '/css' => './resources/css'; |
426
|
|
|
|
|
|
|
# points to /www/lib/perl5/My/App/resources/css/ |
427
|
|
|
|
|
|
|
|
428
|
|
|
|
|
|
|
If an arrayref of C<[ $content, $content_type ]> is given as second argument, |
429
|
|
|
|
|
|
|
serve that content from memory instead. |
430
|
|
|
|
|
|
|
|
431
|
|
|
|
|
|
|
%options may include: |
432
|
|
|
|
|
|
|
|
433
|
|
|
|
|
|
|
=over |
434
|
|
|
|
|
|
|
|
435
|
|
|
|
|
|
|
=item * C => C - buffer size for reading/writing files. |
436
|
|
|
|
|
|
|
Default is 4096. Smaller values may be set, but are NOT recommended. |
437
|
|
|
|
|
|
|
|
438
|
|
|
|
|
|
|
=item * C => C - if given, files below the buffer size |
439
|
|
|
|
|
|
|
will be stored in memory for C seconds. |
440
|
|
|
|
|
|
|
|
441
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> Cache API is not yet established. |
442
|
|
|
|
|
|
|
|
443
|
|
|
|
|
|
|
=item * allow_dots => 1|0 - if true, serve files/directories |
444
|
|
|
|
|
|
|
starting with a dot (.git etc), otherwise give a 404. |
445
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> |
447
|
|
|
|
|
|
|
|
448
|
|
|
|
|
|
|
=item * dir_index => 1|0 - if true, generate index for a directory; |
449
|
|
|
|
|
|
|
otherwise a 404 is returned, and deliberately so, for security reasons. |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> |
452
|
|
|
|
|
|
|
|
453
|
|
|
|
|
|
|
=item * dir_template - specify template for directory listing |
454
|
|
|
|
|
|
|
(with images etc). A sane default is provided. |
455
|
|
|
|
|
|
|
|
456
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> |
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
=item * view - specify view object for rendering directory template. |
459
|
|
|
|
|
|
|
By default a localized C instance is used. |
460
|
|
|
|
|
|
|
|
461
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> Name MAY be changed (dir_view etc). |
462
|
|
|
|
|
|
|
|
463
|
|
|
|
|
|
|
=item * override - override the route that was here before. |
464
|
|
|
|
|
|
|
See C above. |
465
|
|
|
|
|
|
|
|
466
|
|
|
|
|
|
|
=item * tentative - don't complain if replaced later. |
467
|
|
|
|
|
|
|
|
468
|
|
|
|
|
|
|
=item * description - comment. The default is "Static content at $directory" |
469
|
|
|
|
|
|
|
|
470
|
|
|
|
|
|
|
=item * public => 0|1 - a flag just for information. |
471
|
|
|
|
|
|
|
In theory, public endpoints should be searchable from the outside |
472
|
|
|
|
|
|
|
while non-public ones should only be reachable from other parts of application. |
473
|
|
|
|
|
|
|
This is not enforced whatsoever. |
474
|
|
|
|
|
|
|
|
475
|
|
|
|
|
|
|
=back |
476
|
|
|
|
|
|
|
|
477
|
|
|
|
|
|
|
See L for implementation. |
478
|
|
|
|
|
|
|
|
479
|
|
|
|
|
|
|
File type detection is based on extentions so far, and the list is quite short. |
480
|
|
|
|
|
|
|
This MAY change in the future. |
481
|
|
|
|
|
|
|
Known file types are listed in C<%MVC::Neaf::X::Files::ExtType> hash. |
482
|
|
|
|
|
|
|
Patches welcome. |
483
|
|
|
|
|
|
|
|
484
|
|
|
|
|
|
|
I
|
485
|
|
|
|
|
|
|
using a web application framework. |
486
|
|
|
|
|
|
|
Use a real web server instead. |
487
|
|
|
|
|
|
|
Not need to set up one for merely testing icons/js/css, though.> |
488
|
|
|
|
|
|
|
|
489
|
|
|
|
|
|
|
=cut |
490
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
=head2 set_path_defaults() |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
neaf default => \%values, path => '/prefix', method => [ 'GET', 'POST' ]; |
494
|
|
|
|
|
|
|
|
495
|
|
|
|
|
|
|
$neaf->set_path_defaults ( \%values, path => [ '/other', '/prefixes' ] ); |
496
|
|
|
|
|
|
|
|
497
|
|
|
|
|
|
|
Append these values to ANY controller return under given path(s), |
498
|
|
|
|
|
|
|
unless overridden by return from handler. |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
Longer paths override shorter ones; |
501
|
|
|
|
|
|
|
route-specific defaults override path-based defaults; |
502
|
|
|
|
|
|
|
explicit values returned from handler override all or the above. |
503
|
|
|
|
|
|
|
|
504
|
|
|
|
|
|
|
For example, |
505
|
|
|
|
|
|
|
|
506
|
|
|
|
|
|
|
neaf default '/api' => { -view => 'JS', version => My::Model->VERSION }; |
507
|
|
|
|
|
|
|
|
508
|
|
|
|
|
|
|
=cut |
509
|
|
|
|
|
|
|
|
510
|
|
|
|
|
|
|
=head2 add_hook() |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
neaf "phase" => sub { ... }, path => [ ... ], exclude => [ ... ]; |
513
|
|
|
|
|
|
|
|
514
|
|
|
|
|
|
|
$neaf->add_hook ( phase => CODEREF, %options ); |
515
|
|
|
|
|
|
|
|
516
|
|
|
|
|
|
|
Set hook that will be executed on a given request processing phase. |
517
|
|
|
|
|
|
|
|
518
|
|
|
|
|
|
|
Valid phases include: |
519
|
|
|
|
|
|
|
|
520
|
|
|
|
|
|
|
=over |
521
|
|
|
|
|
|
|
|
522
|
|
|
|
|
|
|
=item * pre_route [die] |
523
|
|
|
|
|
|
|
|
524
|
|
|
|
|
|
|
=item * pre_logic [die] |
525
|
|
|
|
|
|
|
|
526
|
|
|
|
|
|
|
=item * pre_content |
527
|
|
|
|
|
|
|
|
528
|
|
|
|
|
|
|
=item * pre_render [die] |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
=item * pre_reply [reverse] |
531
|
|
|
|
|
|
|
|
532
|
|
|
|
|
|
|
=item * pre_cleanup [reverse] |
533
|
|
|
|
|
|
|
|
534
|
|
|
|
|
|
|
=back |
535
|
|
|
|
|
|
|
|
536
|
|
|
|
|
|
|
See L below for detailed |
537
|
|
|
|
|
|
|
discussion of each phase. |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
The CODEREF receives one and only argument - the C<$request> object. |
540
|
|
|
|
|
|
|
Return value is B, see explanation below. |
541
|
|
|
|
|
|
|
|
542
|
|
|
|
|
|
|
Use C<$request>'s C, C, and C methods |
543
|
|
|
|
|
|
|
for communication between hooks. |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
Dying in a hook MAY cause interruption of request processing |
546
|
|
|
|
|
|
|
or merely a warning, depending on the phase. |
547
|
|
|
|
|
|
|
|
548
|
|
|
|
|
|
|
%options may include: |
549
|
|
|
|
|
|
|
|
550
|
|
|
|
|
|
|
=over |
551
|
|
|
|
|
|
|
|
552
|
|
|
|
|
|
|
=item * path => '/path' - where the hook applies. Default is '/'. |
553
|
|
|
|
|
|
|
Multiple locations may be supplied via C<[ /foo, /bar ...]> |
554
|
|
|
|
|
|
|
|
555
|
|
|
|
|
|
|
=item * exclude => '/path/skip' - don't apply to these locations, |
556
|
|
|
|
|
|
|
even if under '/path'. |
557
|
|
|
|
|
|
|
Multiple locations may be supplied via C<[ /foo, /bar ...]> |
558
|
|
|
|
|
|
|
|
559
|
|
|
|
|
|
|
=item * method => 'METHOD' || [ list ] |
560
|
|
|
|
|
|
|
List of request HTTP methods to which given hook applies. |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
=item * prepend => 0|1 - all other parameters being equal, |
563
|
|
|
|
|
|
|
hooks will be executed in order of adding. |
564
|
|
|
|
|
|
|
This option allows to override this and run given hook first. |
565
|
|
|
|
|
|
|
Note that this does NOT override path bubbling order. |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
=back |
568
|
|
|
|
|
|
|
|
569
|
|
|
|
|
|
|
=cut |
570
|
|
|
|
|
|
|
|
571
|
|
|
|
|
|
|
=head2 alias() |
572
|
|
|
|
|
|
|
|
573
|
|
|
|
|
|
|
neaf alias $newpath => $oldpath |
574
|
|
|
|
|
|
|
|
575
|
|
|
|
|
|
|
$neaf->alias( $newpath => $oldpath ) |
576
|
|
|
|
|
|
|
|
577
|
|
|
|
|
|
|
Create a new name for already registered route. |
578
|
|
|
|
|
|
|
The handler will be executed as is, |
579
|
|
|
|
|
|
|
but the hooks and defaults will be re-calculated. |
580
|
|
|
|
|
|
|
So be careful. |
581
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
B<[CAUTION]> As of 0.21, C does NOT follow tentative/override switches. |
583
|
|
|
|
|
|
|
This needs to be fixed in the future. |
584
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
=cut |
586
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
=head2 load_view() |
588
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
neaf view => 'name' => 'Driver::Class' => %options; |
590
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
$neaf->load_view( $name, $object || coderef || ($module_name, %options) ) |
592
|
|
|
|
|
|
|
|
593
|
|
|
|
|
|
|
Setup view under name C<$name>. |
594
|
|
|
|
|
|
|
Subsequent requests with C<-view = $name> would be processed by that view |
595
|
|
|
|
|
|
|
object. |
596
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
Use C to fetch the object itself. |
598
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
=over |
600
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
=item * if object is given, just save it. |
602
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
=item * if module name + parameters are given, try to load module |
604
|
|
|
|
|
|
|
and create a new() instance. |
605
|
|
|
|
|
|
|
|
606
|
|
|
|
|
|
|
Short aliases C, C, and C may be used |
607
|
|
|
|
|
|
|
for corresponding C modules. |
608
|
|
|
|
|
|
|
|
609
|
|
|
|
|
|
|
The templates that allow for paths |
610
|
|
|
|
|
|
|
(i.e. currently just L) |
611
|
|
|
|
|
|
|
will have non-absolute paths calculated relative to the file where |
612
|
|
|
|
|
|
|
static() was called, not to the current directory. |
613
|
|
|
|
|
|
|
|
614
|
|
|
|
|
|
|
=item * if coderef is given, use it as a C method. |
615
|
|
|
|
|
|
|
The coderef must take 1 argument - the hash returned from application - |
616
|
|
|
|
|
|
|
and return a string + optional content-type. |
617
|
|
|
|
|
|
|
|
618
|
|
|
|
|
|
|
=back |
619
|
|
|
|
|
|
|
|
620
|
|
|
|
|
|
|
Returns the view object, NOT the calling Neaf object. |
621
|
|
|
|
|
|
|
|
622
|
|
|
|
|
|
|
=cut |
623
|
|
|
|
|
|
|
|
624
|
|
|
|
|
|
|
=head2 set_session_handler() |
625
|
|
|
|
|
|
|
|
626
|
|
|
|
|
|
|
neaf session => $engine => %options |
627
|
|
|
|
|
|
|
|
628
|
|
|
|
|
|
|
$neaf->set_session_handler( %options ) |
629
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
Set a handler for managing sessions. |
631
|
|
|
|
|
|
|
|
632
|
|
|
|
|
|
|
If such handler is set, the request object will provide C, |
633
|
|
|
|
|
|
|
C, and C methods to manage |
634
|
|
|
|
|
|
|
cross-request user data. |
635
|
|
|
|
|
|
|
|
636
|
|
|
|
|
|
|
% options may include: |
637
|
|
|
|
|
|
|
|
638
|
|
|
|
|
|
|
=over |
639
|
|
|
|
|
|
|
|
640
|
|
|
|
|
|
|
=item * C (required in method form, first argument in DSL form) |
641
|
|
|
|
|
|
|
- an object providing the storage primitives; |
642
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
=item * C - time to live for session (default is 0, which means until |
644
|
|
|
|
|
|
|
browser is closed); |
645
|
|
|
|
|
|
|
|
646
|
|
|
|
|
|
|
=item * C - name of cookie storing session id. |
647
|
|
|
|
|
|
|
The default is "session". |
648
|
|
|
|
|
|
|
|
649
|
|
|
|
|
|
|
=item * C - if set, add the whole session into data hash |
650
|
|
|
|
|
|
|
under this name before view processing. |
651
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
=back |
653
|
|
|
|
|
|
|
|
654
|
|
|
|
|
|
|
The engine MUST provide the following methods |
655
|
|
|
|
|
|
|
(see L for details): |
656
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
=over |
658
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
=item * session_ttl (implemented in MVC::Neaf::X::Session); |
660
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
=item * session_id_regex (implemented in MVC::Neaf::X::Session); |
662
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
=item * get_session_id (implemented in MVC::Neaf::X::Session); |
664
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
=item * create_session (implemented in MVC::Neaf::X::Session); |
666
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
=item * save_session (required); |
668
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
=item * load_session (required); |
670
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
=item * delete_session (implemented in MVC::Neaf::X::Session); |
672
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
=back |
674
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
=cut |
676
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
=head2 add_form() |
678
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
neaf form => name => \%spec, engine => ... |
680
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
$neaf->add_form( name => $validator ) |
682
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
Create a named form for future query data validation |
684
|
|
|
|
|
|
|
via C<$request-Eform("name")>. |
685
|
|
|
|
|
|
|
See L. |
686
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
The C<$validator> is one of: |
688
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
=over |
690
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
=item * An object with C method accepting one C<\%hashref> |
692
|
|
|
|
|
|
|
argument (the raw form data). |
693
|
|
|
|
|
|
|
|
694
|
|
|
|
|
|
|
=item * A CODEREF accepting the same argument. |
695
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
=back |
697
|
|
|
|
|
|
|
|
698
|
|
|
|
|
|
|
Whatever is returned by validator is forwarded into the controller. |
699
|
|
|
|
|
|
|
|
700
|
|
|
|
|
|
|
Neaf comes with a set of predefined validator classes that return |
701
|
|
|
|
|
|
|
a convenient object that contains collected valid data, errors (if any), |
702
|
|
|
|
|
|
|
and an is_valid flag. |
703
|
|
|
|
|
|
|
|
704
|
|
|
|
|
|
|
The C parameter of the functional form has predefined values |
705
|
|
|
|
|
|
|
C (the default), C, and C (all case-insensitive) |
706
|
|
|
|
|
|
|
pointing towards L, L, |
707
|
|
|
|
|
|
|
and L, respectively. |
708
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
You are encouraged to use C |
710
|
|
|
|
|
|
|
(See L and L) |
711
|
|
|
|
|
|
|
for anything except super-basic regex checks. |
712
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
If an arbitrary class name is given instead, C will be called |
714
|
|
|
|
|
|
|
on that class with \%spec ref as first parameter. |
715
|
|
|
|
|
|
|
|
716
|
|
|
|
|
|
|
Consider the following script: |
717
|
|
|
|
|
|
|
|
718
|
|
|
|
|
|
|
use MVC::Neaf; |
719
|
|
|
|
|
|
|
neaf form => my => { foo => '\d+', bar => '[yn]' }; |
720
|
|
|
|
|
|
|
get '/check' => sub { |
721
|
|
|
|
|
|
|
my $req = shift; |
722
|
|
|
|
|
|
|
my $in = $req->form("my"); |
723
|
|
|
|
|
|
|
return $in->is_valid ? { ok => $in->data } : { error => $in->error }; |
724
|
|
|
|
|
|
|
}; |
725
|
|
|
|
|
|
|
neaf->run |
726
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
And by running this one gets |
728
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
bash$ curl http://localhost:5000/check?bar=xxx |
730
|
|
|
|
|
|
|
{"error":{"bar":"BAD_FORMAT"}} |
731
|
|
|
|
|
|
|
bash$ curl http://localhost:5000/check?bar=y |
732
|
|
|
|
|
|
|
{"ok":{"bar":"y"}} |
733
|
|
|
|
|
|
|
bash$ curl http://localhost:5000/check?bar=yy |
734
|
|
|
|
|
|
|
{"error":{"bar":"BAD_FORMAT"}} |
735
|
|
|
|
|
|
|
bash$ curl http://localhost:5000/check?foo=137\&bar=n |
736
|
|
|
|
|
|
|
{"ok":{"bar":"n","foo":"137"}} |
737
|
|
|
|
|
|
|
bash$ curl http://localhost:5000/check?foo=leet |
738
|
|
|
|
|
|
|
{"error":{"foo":"BAD_FORMAT"}} |
739
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
=cut |
741
|
|
|
|
|
|
|
|
742
|
|
|
|
|
|
|
=head2 set_error_handler() |
743
|
|
|
|
|
|
|
|
744
|
|
|
|
|
|
|
$neaf->set_error_handler ( $status => CODEREF( $request, %options ), %where ) |
745
|
|
|
|
|
|
|
|
746
|
|
|
|
|
|
|
Set custom error handler. |
747
|
|
|
|
|
|
|
|
748
|
|
|
|
|
|
|
Status MUST be a 3-digit number (as in HTTP). |
749
|
|
|
|
|
|
|
|
750
|
|
|
|
|
|
|
%where may include C, C, and C keys. |
751
|
|
|
|
|
|
|
If omitted, just install error handler globally. |
752
|
|
|
|
|
|
|
|
753
|
|
|
|
|
|
|
Other allowed keys MAY appear in the future. |
754
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
The following options will be passed to coderef: |
756
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
=over |
758
|
|
|
|
|
|
|
|
759
|
|
|
|
|
|
|
=item * status - status being returned; |
760
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
=item * caller - file:line where the route was set up; |
762
|
|
|
|
|
|
|
This is DEPRECATED and will silently disappear around version 0.25 |
763
|
|
|
|
|
|
|
|
764
|
|
|
|
|
|
|
=item * error - exception, an L object. |
765
|
|
|
|
|
|
|
|
766
|
|
|
|
|
|
|
=back |
767
|
|
|
|
|
|
|
|
768
|
|
|
|
|
|
|
The coderef MUST return an unblessed hash just like a normal controller does. |
769
|
|
|
|
|
|
|
|
770
|
|
|
|
|
|
|
In case of exception or unexpected return format |
771
|
|
|
|
|
|
|
default HTML error page will be returned. |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
Also available in static form, as C \%hash )>. |
774
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
This is a synonym to C $status, ... } }>. |
776
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
=cut |
778
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
=head2 load_resources() |
780
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
$neaf->load_resources( $file_name || \*FH ) |
782
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
Load pseudo-files from a file, like templates or static files. |
784
|
|
|
|
|
|
|
This is automatically called upon C if C<__DATA__> is present, |
785
|
|
|
|
|
|
|
unless Cmagic(0)> was called. |
786
|
|
|
|
|
|
|
|
787
|
|
|
|
|
|
|
The format is as follows: |
788
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
@@ [TT] main.html |
790
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
[% some_tt_template %] |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
@@ /favicon.ico format=base64 type=png |
794
|
|
|
|
|
|
|
|
795
|
|
|
|
|
|
|
iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAMAAABEpIrGAAAABGdBTUEAAL |
796
|
|
|
|
|
|
|
GPC/xhBQAAAAFzUkdCAK7OHOkAAAAgY0hS<....more encoded lines> |
797
|
|
|
|
|
|
|
|
798
|
|
|
|
|
|
|
I, |
799
|
|
|
|
|
|
|
in a slightly incompatible way.> |
800
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
If view is specified in brackets, preload template. |
802
|
|
|
|
|
|
|
A missing view is skipped, no error. |
803
|
|
|
|
|
|
|
|
804
|
|
|
|
|
|
|
Otherwise file is considered a static resource. |
805
|
|
|
|
|
|
|
|
806
|
|
|
|
|
|
|
Extra options may follow file name: |
807
|
|
|
|
|
|
|
|
808
|
|
|
|
|
|
|
=over |
809
|
|
|
|
|
|
|
|
810
|
|
|
|
|
|
|
=item * C |
811
|
|
|
|
|
|
|
|
812
|
|
|
|
|
|
|
=item * C |
813
|
|
|
|
|
|
|
|
814
|
|
|
|
|
|
|
=back |
815
|
|
|
|
|
|
|
|
816
|
|
|
|
|
|
|
Unknown options are skipped. |
817
|
|
|
|
|
|
|
Unknown format value will cause exception though. |
818
|
|
|
|
|
|
|
|
819
|
|
|
|
|
|
|
B<[EXPERIMENTAL]> This method and exact format of data is being worked on. |
820
|
|
|
|
|
|
|
|
821
|
|
|
|
|
|
|
=cut |
822
|
|
|
|
|
|
|
|
823
|
|
|
|
|
|
|
=head2 magic |
824
|
|
|
|
|
|
|
|
825
|
|
|
|
|
|
|
neaf->magic( 0 || 1) |
826
|
|
|
|
|
|
|
my $get = neaf->magic |
827
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
Get/set whether automatic actions should occur. |
829
|
|
|
|
|
|
|
Currently only affects calling L upon L. |
830
|
|
|
|
|
|
|
|
831
|
|
|
|
|
|
|
=head2 set_helper |
832
|
|
|
|
|
|
|
|
833
|
|
|
|
|
|
|
neaf helper "name" => sub { ... }, %options; |
834
|
|
|
|
|
|
|
|
835
|
|
|
|
|
|
|
neaf->set_helper( "name" => \&coderef, ... ); |
836
|
|
|
|
|
|
|
|
837
|
|
|
|
|
|
|
Create a method in L package that is only visible |
838
|
|
|
|
|
|
|
in the current application. |
839
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
Options may include: |
841
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
=over |
843
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
=item * path => C<[ '/foo', '/bar' ]> - restrict the helper |
845
|
|
|
|
|
|
|
to given prefix(es) only. |
846
|
|
|
|
|
|
|
Helpers with the same name may be created for different paths. |
847
|
|
|
|
|
|
|
In such case, longer paths take over as usual. |
848
|
|
|
|
|
|
|
|
849
|
|
|
|
|
|
|
Colliding prefixes will cause an error, but see below. |
850
|
|
|
|
|
|
|
|
851
|
|
|
|
|
|
|
=item * method => C<[ 'GET', 'POST' ]> - restrict the helper to given methods only. |
852
|
|
|
|
|
|
|
|
853
|
|
|
|
|
|
|
=item * exclude => C<[ '/foo/bar' ]> - do NOT provide this helper for |
854
|
|
|
|
|
|
|
given prefixes. |
855
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
=item * tentative - allow to override this helper later. |
857
|
|
|
|
|
|
|
|
858
|
|
|
|
|
|
|
=item * override - override the existing helper, no matter what. |
859
|
|
|
|
|
|
|
|
860
|
|
|
|
|
|
|
=back |
861
|
|
|
|
|
|
|
|
862
|
|
|
|
|
|
|
B<[EXPERIMENTAL]>. Name and meaning may change in the future. |
863
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
=head2 run() |
865
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
neaf->run(); |
867
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
Start the application. |
869
|
|
|
|
|
|
|
This SHOULD be the last statement in your application's main file. |
870
|
|
|
|
|
|
|
|
871
|
|
|
|
|
|
|
If called in void context, assumes execution as C |
872
|
|
|
|
|
|
|
and prints results to C. |
873
|
|
|
|
|
|
|
If command line options are present at the moment, |
874
|
|
|
|
|
|
|
enters debug mode via L. |
875
|
|
|
|
|
|
|
Call C for more. |
876
|
|
|
|
|
|
|
|
877
|
|
|
|
|
|
|
Otherwise returns a C-compliant coderef. |
878
|
|
|
|
|
|
|
This will also happen if you application is C'd, |
879
|
|
|
|
|
|
|
meaning that it returns a true value and actually serves nothing until |
880
|
|
|
|
|
|
|
C is called again. |
881
|
|
|
|
|
|
|
|
882
|
|
|
|
|
|
|
Running under mod_perl requires setting a handler with |
883
|
|
|
|
|
|
|
L. |
884
|
|
|
|
|
|
|
|
885
|
|
|
|
|
|
|
=cut |
886
|
|
|
|
|
|
|
|
887
|
|
|
|
|
|
|
=head1 EXPORTED HELPER FUNCTIONS |
888
|
|
|
|
|
|
|
|
889
|
|
|
|
|
|
|
Neaf tries hard to keep user's namespace clean, however, |
890
|
|
|
|
|
|
|
some helper functions are needed. |
891
|
|
|
|
|
|
|
|
892
|
|
|
|
|
|
|
=head2 neaf_err $error |
893
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
Rethrow Neaf's internal exceptions immediately, do nothing otherwise. |
895
|
|
|
|
|
|
|
|
896
|
|
|
|
|
|
|
If no argument if given, acts on current C<$@> value. |
897
|
|
|
|
|
|
|
|
898
|
|
|
|
|
|
|
Currently Neaf uses exception mechanism for internal signalling, |
899
|
|
|
|
|
|
|
so this function may be of use if there's a lot of C blocks |
900
|
|
|
|
|
|
|
in the controller. E.g. |
901
|
|
|
|
|
|
|
|
902
|
|
|
|
|
|
|
use MVC::Neaf qw(neaf_err); |
903
|
|
|
|
|
|
|
|
904
|
|
|
|
|
|
|
# somewhere in controller |
905
|
|
|
|
|
|
|
eval { |
906
|
|
|
|
|
|
|
check_permissions() |
907
|
|
|
|
|
|
|
or $req->error(403); |
908
|
|
|
|
|
|
|
do_something() |
909
|
|
|
|
|
|
|
and $req->redirect("/success"); |
910
|
|
|
|
|
|
|
}; |
911
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
if (my $err = $@) { |
913
|
|
|
|
|
|
|
neaf_err; |
914
|
|
|
|
|
|
|
# do the rest of error handling |
915
|
|
|
|
|
|
|
}; |
916
|
|
|
|
|
|
|
|
917
|
|
|
|
|
|
|
Or alternatively with L: |
918
|
|
|
|
|
|
|
|
919
|
|
|
|
|
|
|
try { |
920
|
|
|
|
|
|
|
... |
921
|
|
|
|
|
|
|
} catch { |
922
|
|
|
|
|
|
|
neaf_err $_; |
923
|
|
|
|
|
|
|
# proceed with normal error handling |
924
|
|
|
|
|
|
|
} |
925
|
|
|
|
|
|
|
|
926
|
|
|
|
|
|
|
See also L. |
927
|
|
|
|
|
|
|
|
928
|
|
|
|
|
|
|
=cut |
929
|
|
|
|
|
|
|
|
930
|
|
|
|
|
|
|
sub neaf_err(;$) { ## no critic # prototype it for less typing on user's part |
931
|
2
|
|
33
|
2
|
1
|
152
|
my $err = shift || $@; |
932
|
2
|
100
|
66
|
|
|
28
|
die $err if blessed $err and $err->isa("MVC::Neaf::Exception"); |
933
|
1
|
50
|
33
|
|
|
10
|
die $err if !ref $err and $err =~ /^(\d\d\d)\s/s; # die 403 |
934
|
1
|
|
|
|
|
2
|
return; |
935
|
|
|
|
|
|
|
}; |
936
|
|
|
|
|
|
|
|
937
|
|
|
|
|
|
|
=head2 neaf() |
938
|
|
|
|
|
|
|
|
939
|
|
|
|
|
|
|
If called without arguments, returns the default C instance. |
940
|
|
|
|
|
|
|
|
941
|
|
|
|
|
|
|
If arguments are given, works as described above: |
942
|
|
|
|
|
|
|
|
943
|
|
|
|
|
|
|
neaf $action => @options; |
944
|
|
|
|
|
|
|
|
945
|
|
|
|
|
|
|
Possible actions include: |
946
|
|
|
|
|
|
|
|
947
|
|
|
|
|
|
|
=over |
948
|
|
|
|
|
|
|
|
949
|
|
|
|
|
|
|
=item * view - L |
950
|
|
|
|
|
|
|
|
951
|
|
|
|
|
|
|
=item * session - L |
952
|
|
|
|
|
|
|
|
953
|
|
|
|
|
|
|
=item * default - L |
954
|
|
|
|
|
|
|
|
955
|
|
|
|
|
|
|
=item * helper - L |
956
|
|
|
|
|
|
|
|
957
|
|
|
|
|
|
|
=item * alias - L |
958
|
|
|
|
|
|
|
|
959
|
|
|
|
|
|
|
=item * static - L |
960
|
|
|
|
|
|
|
|
961
|
|
|
|
|
|
|
=item * route - L |
962
|
|
|
|
|
|
|
|
963
|
|
|
|
|
|
|
Don't do this, use C or C instead. |
964
|
|
|
|
|
|
|
|
965
|
|
|
|
|
|
|
=item * hook - L |
966
|
|
|
|
|
|
|
|
967
|
|
|
|
|
|
|
Don't do this, use phase name instead. |
968
|
|
|
|
|
|
|
|
969
|
|
|
|
|
|
|
=item * error - L |
970
|
|
|
|
|
|
|
|
971
|
|
|
|
|
|
|
Don't do this, use 3-digit error code instead. |
972
|
|
|
|
|
|
|
|
973
|
|
|
|
|
|
|
=back |
974
|
|
|
|
|
|
|
|
975
|
|
|
|
|
|
|
=cut |
976
|
|
|
|
|
|
|
|
977
|
|
|
|
|
|
|
my %method_shortcut = ( |
978
|
|
|
|
|
|
|
route => 'route', |
979
|
|
|
|
|
|
|
error => 'set_error_handler', |
980
|
|
|
|
|
|
|
view => 'load_view', |
981
|
|
|
|
|
|
|
hook => 'add_hook', |
982
|
|
|
|
|
|
|
helper => 'set_helper', |
983
|
|
|
|
|
|
|
session => 'set_session_handler', |
984
|
|
|
|
|
|
|
default => 'set_path_defaults', |
985
|
|
|
|
|
|
|
alias => 'alias', |
986
|
|
|
|
|
|
|
static => 'static', |
987
|
|
|
|
|
|
|
form => 'add_form', |
988
|
|
|
|
|
|
|
); |
989
|
|
|
|
|
|
|
|
990
|
|
|
|
|
|
|
sub neaf(@) { ## no critic # DSL |
991
|
327
|
100
|
|
327
|
1
|
155505
|
return $MVC::Neaf::Inst unless @_; |
992
|
|
|
|
|
|
|
|
993
|
|
|
|
|
|
|
# If something dies here, it's probably the calling code to blame |
994
|
|
|
|
|
|
|
# and not us |
995
|
48
|
|
|
|
|
201
|
local $Carp::Internal{+__PACKAGE__} = 1; |
996
|
|
|
|
|
|
|
|
997
|
48
|
|
|
|
|
167
|
my ($action, @args) = @_; |
998
|
|
|
|
|
|
|
|
999
|
48
|
100
|
|
|
|
292
|
if ($action =~ /^\d\d\d$/) { |
1000
|
7
|
|
|
|
|
20
|
unshift @args, $action; |
1001
|
7
|
|
|
|
|
12
|
$action = 'error'; |
1002
|
|
|
|
|
|
|
}; |
1003
|
48
|
100
|
|
|
|
186
|
if ($MVC::Neaf::Route::Main::hook_phases{$action}) { |
1004
|
14
|
|
|
|
|
53
|
unshift @args, $action; |
1005
|
14
|
|
|
|
|
32
|
$action = 'hook'; |
1006
|
|
|
|
|
|
|
}; |
1007
|
|
|
|
|
|
|
|
1008
|
48
|
100
|
|
|
|
155
|
if ($action eq 'session') { |
1009
|
3
|
|
|
|
|
10
|
unshift @args, 'engine'; |
1010
|
|
|
|
|
|
|
}; |
1011
|
|
|
|
|
|
|
|
1012
|
48
|
50
|
|
|
|
146
|
if ($action eq 'route' ) { |
1013
|
0
|
|
|
|
|
0
|
carp "neaf route is DEPRECATED, use get+post+put instead"; |
1014
|
|
|
|
|
|
|
}; |
1015
|
|
|
|
|
|
|
|
1016
|
48
|
|
|
|
|
138
|
my $method = $method_shortcut{$action}; |
1017
|
48
|
50
|
33
|
|
|
735
|
croak "neaf: don't know how to handle '$action'" |
1018
|
|
|
|
|
|
|
unless $method and MVC::Neaf->can($method); |
1019
|
|
|
|
|
|
|
|
1020
|
48
|
|
|
|
|
244
|
return $MVC::Neaf::Inst->$method( @args ); |
1021
|
|
|
|
|
|
|
}; |
1022
|
|
|
|
|
|
|
|
1023
|
|
|
|
|
|
|
# Generate alias subs |
1024
|
|
|
|
|
|
|
my @ALL_METHODS = qw( get head post put patch delete ); |
1025
|
|
|
|
|
|
|
my %ALIAS; |
1026
|
|
|
|
|
|
|
$ALIAS{$_} = uc $_ for @ALL_METHODS; |
1027
|
|
|
|
|
|
|
$ALIAS{del} = delete $ALIAS{delete}; # ouch, no delete '/foo' => bar |
1028
|
|
|
|
|
|
|
$ALIAS{any} = \@ALL_METHODS; |
1029
|
|
|
|
|
|
|
|
1030
|
|
|
|
|
|
|
foreach (keys %ALIAS) { |
1031
|
|
|
|
|
|
|
my $method = $ALIAS{$_}; |
1032
|
|
|
|
|
|
|
my $is_any = $_ eq 'any'; |
1033
|
|
|
|
|
|
|
|
1034
|
|
|
|
|
|
|
my $code = sub(@) { ## no critic |
1035
|
|
|
|
|
|
|
# any |
1036
|
79
|
100
|
100
|
79
|
|
5008
|
if ($is_any and ref $_[0] eq 'ARRAY') { |
|
|
100
|
66
|
|
|
|
|
1037
|
2
|
|
|
|
|
10
|
$method = shift; |
1038
|
|
|
|
|
|
|
} elsif (@_ == 1 and UNIVERSAL::isa( $_[0], __PACKAGE__ )) { |
1039
|
|
|
|
|
|
|
# get + post sugar |
1040
|
7
|
|
|
|
|
55
|
return $_[0]->_dup_route( $method ); |
1041
|
|
|
|
|
|
|
}; |
1042
|
|
|
|
|
|
|
|
1043
|
|
|
|
|
|
|
# normal operation |
1044
|
72
|
|
|
|
|
215
|
my $path = shift; |
1045
|
72
|
100
|
66
|
|
|
709
|
my $handler = ref $_[0] && reftype $_[0] eq 'CODE' ? shift : pop; |
1046
|
|
|
|
|
|
|
|
1047
|
72
|
|
|
|
|
223
|
return neaf()->add_route( |
1048
|
|
|
|
|
|
|
$path, $handler, @_, method => $method, caller => [caller(0)] ); |
1049
|
|
|
|
|
|
|
}; |
1050
|
|
|
|
|
|
|
|
1051
|
|
|
|
|
|
|
push @EXPORT_SUGAR, $_; |
1052
|
81
|
|
|
81
|
|
59233
|
no strict 'refs'; ## no critic |
|
81
|
|
|
|
|
214
|
|
|
81
|
|
|
|
|
12766
|
|
1053
|
|
|
|
|
|
|
*{$_} = $code; |
1054
|
|
|
|
|
|
|
}; |
1055
|
|
|
|
|
|
|
push @EXPORT, @EXPORT_SUGAR; |
1056
|
|
|
|
|
|
|
|
1057
|
|
|
|
|
|
|
=pod |
1058
|
|
|
|
|
|
|
|
1059
|
|
|
|
|
|
|
=head1 DEVELOPMENT AND DEBUGGING METHODS |
1060
|
|
|
|
|
|
|
|
1061
|
|
|
|
|
|
|
No more prototyped/exported functions below here. |
1062
|
|
|
|
|
|
|
|
1063
|
|
|
|
|
|
|
=head2 run_test() |
1064
|
|
|
|
|
|
|
|
1065
|
|
|
|
|
|
|
neaf->run_test( \%PSGI_ENV, %options ) |
1066
|
|
|
|
|
|
|
|
1067
|
|
|
|
|
|
|
neaf->run_test( "/path?parameter=value", %options ) |
1068
|
|
|
|
|
|
|
|
1069
|
|
|
|
|
|
|
Run a L request and return a list of |
1070
|
|
|
|
|
|
|
C<($status, HTTP::Headers::Fast, $whole_content )>. |
1071
|
|
|
|
|
|
|
|
1072
|
|
|
|
|
|
|
Returns just the content in scalar context. |
1073
|
|
|
|
|
|
|
|
1074
|
|
|
|
|
|
|
Just as the name suggests, useful for testing only (it reduces boilerplate). |
1075
|
|
|
|
|
|
|
|
1076
|
|
|
|
|
|
|
Continuation responses are supported, but will be returned in one chunk. |
1077
|
|
|
|
|
|
|
|
1078
|
|
|
|
|
|
|
%options may include: |
1079
|
|
|
|
|
|
|
|
1080
|
|
|
|
|
|
|
=over |
1081
|
|
|
|
|
|
|
|
1082
|
|
|
|
|
|
|
=item * method - set method (default is GET) |
1083
|
|
|
|
|
|
|
|
1084
|
|
|
|
|
|
|
=item * cookie = \%hash - force HTTP_COOKIE header |
1085
|
|
|
|
|
|
|
|
1086
|
|
|
|
|
|
|
=item * header = \%hash - override some headers |
1087
|
|
|
|
|
|
|
This gets overridden by type, cookie etc. in case of conflict |
1088
|
|
|
|
|
|
|
|
1089
|
|
|
|
|
|
|
=item * body = 'DATA' - force body in request |
1090
|
|
|
|
|
|
|
|
1091
|
|
|
|
|
|
|
=item * type - content-type of body |
1092
|
|
|
|
|
|
|
|
1093
|
|
|
|
|
|
|
=item * uploads - a hash of L objects. |
1094
|
|
|
|
|
|
|
|
1095
|
|
|
|
|
|
|
=item * secure = 0|1 - C vs C |
1096
|
|
|
|
|
|
|
|
1097
|
|
|
|
|
|
|
=item * override = \%hash - force certain data in C |
1098
|
|
|
|
|
|
|
Gets overridden by all of the above. |
1099
|
|
|
|
|
|
|
|
1100
|
|
|
|
|
|
|
=back |
1101
|
|
|
|
|
|
|
|
1102
|
|
|
|
|
|
|
See also L which author of this writing has overlooked. |
1103
|
|
|
|
|
|
|
|
1104
|
|
|
|
|
|
|
=cut |
1105
|
|
|
|
|
|
|
|
1106
|
|
|
|
|
|
|
=head2 get_routes() |
1107
|
|
|
|
|
|
|
|
1108
|
|
|
|
|
|
|
neaf->get_routes( $callback->(\%route_spec, $path, $method) ) |
1109
|
|
|
|
|
|
|
|
1110
|
|
|
|
|
|
|
Returns a 2-level hashref with ALL routes for inspection. |
1111
|
|
|
|
|
|
|
|
1112
|
|
|
|
|
|
|
So C<$hash{'/path'}{'GET'} = { handler, expected params, description etc }> |
1113
|
|
|
|
|
|
|
|
1114
|
|
|
|
|
|
|
If callback is present, run it against route definition |
1115
|
|
|
|
|
|
|
and append to hash its return value, but ONLY if it's true. |
1116
|
|
|
|
|
|
|
|
1117
|
|
|
|
|
|
|
As of 0.20, route definitions are only protected by shallow copy, |
1118
|
|
|
|
|
|
|
so be careful with them. |
1119
|
|
|
|
|
|
|
|
1120
|
|
|
|
|
|
|
B<[EXPERIMENTAL]>. Name and meaning MAY change in the future. |
1121
|
|
|
|
|
|
|
|
1122
|
|
|
|
|
|
|
=cut |
1123
|
|
|
|
|
|
|
|
1124
|
|
|
|
|
|
|
# Setup default instance, no more code after this |
1125
|
|
|
|
|
|
|
# aside from deprecated methods |
1126
|
|
|
|
|
|
|
$Inst = __PACKAGE__->new; |
1127
|
|
|
|
|
|
|
|
1128
|
|
|
|
|
|
|
=head1 REQUEST PROCESSING PHASES AND HOOKS |
1129
|
|
|
|
|
|
|
|
1130
|
|
|
|
|
|
|
Hooks are subroutines executed during various phases of request processing. |
1131
|
|
|
|
|
|
|
Each hook is characterized by phase, code to be executed, path, and method. |
1132
|
|
|
|
|
|
|
Multiple hooks MAY be added for the same phase/path/method combination. |
1133
|
|
|
|
|
|
|
ALL hooks matching a given route will be executed, either short to long or |
1134
|
|
|
|
|
|
|
long to short (aka "event bubbling"), depending on the phase. |
1135
|
|
|
|
|
|
|
|
1136
|
|
|
|
|
|
|
B<[CAUTION]> Don't overuse hooks. |
1137
|
|
|
|
|
|
|
This may lead to a convoluted, hard to follow application. |
1138
|
|
|
|
|
|
|
Use hooks for repeated auxiliary tasks such as checking permissions or writing |
1139
|
|
|
|
|
|
|
down statistics, NOT for primary application logic. |
1140
|
|
|
|
|
|
|
|
1141
|
|
|
|
|
|
|
Hook return values are discarded, and deliberately so. |
1142
|
|
|
|
|
|
|
I
|
1143
|
|
|
|
|
|
|
Perl will interpret the last statement in the code as such. |
1144
|
|
|
|
|
|
|
Therefore writers of hooks would have to be extremely careful to avoid |
1145
|
|
|
|
|
|
|
breaking the execution chain. |
1146
|
|
|
|
|
|
|
On the other hand, proper exception handling is required anyway for |
1147
|
|
|
|
|
|
|
implementing any kind of callbacks.> |
1148
|
|
|
|
|
|
|
|
1149
|
|
|
|
|
|
|
As a rule of thumb, the following primitives should be used to maintain |
1150
|
|
|
|
|
|
|
state across hooks and the main controller: |
1151
|
|
|
|
|
|
|
|
1152
|
|
|
|
|
|
|
=over |
1153
|
|
|
|
|
|
|
|
1154
|
|
|
|
|
|
|
=item * Use C if you intend to share data between requests. |
1155
|
|
|
|
|
|
|
|
1156
|
|
|
|
|
|
|
=item * Use C if you intend to render the data for the user. |
1157
|
|
|
|
|
|
|
|
1158
|
|
|
|
|
|
|
=item * Use C as a last resort for temporary, private data. |
1159
|
|
|
|
|
|
|
|
1160
|
|
|
|
|
|
|
=back |
1161
|
|
|
|
|
|
|
|
1162
|
|
|
|
|
|
|
The following list of phases MAY change in the future. |
1163
|
|
|
|
|
|
|
Current request processing diagram looks as follows: |
1164
|
|
|
|
|
|
|
|
1165
|
|
|
|
|
|
|
[*] request created |
1166
|
|
|
|
|
|
|
. <- pre_route [no path] [can die] |
1167
|
|
|
|
|
|
|
| |
1168
|
|
|
|
|
|
|
* route - select handler |
1169
|
|
|
|
|
|
|
| |
1170
|
|
|
|
|
|
|
. <- pre_logic [can die] |
1171
|
|
|
|
|
|
|
[*] execute main handler |
1172
|
|
|
|
|
|
|
* apply path-based defaults - reply() is populated now |
1173
|
|
|
|
|
|
|
| |
1174
|
|
|
|
|
|
|
. <- pre_content |
1175
|
|
|
|
|
|
|
? checking whether content already generated |
1176
|
|
|
|
|
|
|
|\ |
1177
|
|
|
|
|
|
|
| . <- pre_render [can die - template error produced] |
1178
|
|
|
|
|
|
|
| [*] render - -content is present now |
1179
|
|
|
|
|
|
|
|/ |
1180
|
|
|
|
|
|
|
* generate default headers (content type & length, cookies, etc) |
1181
|
|
|
|
|
|
|
. <- pre_reply [path traversal long to short] |
1182
|
|
|
|
|
|
|
| |
1183
|
|
|
|
|
|
|
[*] headers sent out, no way back! |
1184
|
|
|
|
|
|
|
* output the rest of reply (if -continue specified) |
1185
|
|
|
|
|
|
|
* execute postponed actions (if any) |
1186
|
|
|
|
|
|
|
| |
1187
|
|
|
|
|
|
|
. <- pre_cleanup [path traversal long to short] [no effect on headers] |
1188
|
|
|
|
|
|
|
[*] request destroyed |
1189
|
|
|
|
|
|
|
|
1190
|
|
|
|
|
|
|
=head2 pre_route |
1191
|
|
|
|
|
|
|
|
1192
|
|
|
|
|
|
|
Executed AFTER the request has been received, but BEFORE the path has been |
1193
|
|
|
|
|
|
|
resolved and handler found. |
1194
|
|
|
|
|
|
|
|
1195
|
|
|
|
|
|
|
Dying in this phase stops both further hook processing and controller execution. |
1196
|
|
|
|
|
|
|
Instead, the corresponding error handler is executed right away. |
1197
|
|
|
|
|
|
|
|
1198
|
|
|
|
|
|
|
Options C and C are not available on this stage. |
1199
|
|
|
|
|
|
|
|
1200
|
|
|
|
|
|
|
May be useful for mangling path. |
1201
|
|
|
|
|
|
|
Use C<$request-Eset_path($new_path)> if you need to. |
1202
|
|
|
|
|
|
|
|
1203
|
|
|
|
|
|
|
=head2 pre_logic |
1204
|
|
|
|
|
|
|
|
1205
|
|
|
|
|
|
|
Executed AFTER finding the correct route, but BEFORE processing the main |
1206
|
|
|
|
|
|
|
handler code (one that returns C<\%hash>, see C above). |
1207
|
|
|
|
|
|
|
|
1208
|
|
|
|
|
|
|
Hooks are executed in order, shorted paths to longer. |
1209
|
|
|
|
|
|
|
C is not available at this stage, |
1210
|
|
|
|
|
|
|
as the controller has not been executed yet. |
1211
|
|
|
|
|
|
|
|
1212
|
|
|
|
|
|
|
Dying in this phase stops both further hook processing and controller execution. |
1213
|
|
|
|
|
|
|
Instead, the corresponding error handler is executed right away. |
1214
|
|
|
|
|
|
|
|
1215
|
|
|
|
|
|
|
B<[EXAMPLE]> use this hook to produce a 403 error if the user is not logged in |
1216
|
|
|
|
|
|
|
and looking for a restricted area of the site: |
1217
|
|
|
|
|
|
|
|
1218
|
|
|
|
|
|
|
neaf pre_logic => sub { |
1219
|
|
|
|
|
|
|
my $request = shift; |
1220
|
|
|
|
|
|
|
$request->session->{user_id} or die 403; |
1221
|
|
|
|
|
|
|
}, path => '/admin', exclude => '/admin/static'; |
1222
|
|
|
|
|
|
|
|
1223
|
|
|
|
|
|
|
=head2 pre_content |
1224
|
|
|
|
|
|
|
|
1225
|
|
|
|
|
|
|
This hook is run AFTER the main handler has returned or died, but BEFORE |
1226
|
|
|
|
|
|
|
content rendering/serialization is performed. |
1227
|
|
|
|
|
|
|
|
1228
|
|
|
|
|
|
|
C hash is available at this stage. |
1229
|
|
|
|
|
|
|
|
1230
|
|
|
|
|
|
|
Dying is ignored, only producing a warning. |
1231
|
|
|
|
|
|
|
|
1232
|
|
|
|
|
|
|
=head2 pre_render |
1233
|
|
|
|
|
|
|
|
1234
|
|
|
|
|
|
|
This hook is run BEFORE content rendering is performed, and ONLY IF |
1235
|
|
|
|
|
|
|
the content is going to be rendered, |
1236
|
|
|
|
|
|
|
i.e. no C<-content> key set in response hash on previous stages. |
1237
|
|
|
|
|
|
|
|
1238
|
|
|
|
|
|
|
Dying will stop rendering, resulting in a template error instead. |
1239
|
|
|
|
|
|
|
|
1240
|
|
|
|
|
|
|
=head2 pre_reply |
1241
|
|
|
|
|
|
|
|
1242
|
|
|
|
|
|
|
This hook is run AFTER the headers have been generated, but BEFORE the reply is |
1243
|
|
|
|
|
|
|
actually sent to client. This is the last chance to amend something. |
1244
|
|
|
|
|
|
|
|
1245
|
|
|
|
|
|
|
Hooks are executed in REVERSE order, from longer to shorter paths. |
1246
|
|
|
|
|
|
|
|
1247
|
|
|
|
|
|
|
C hash is available at this stage. |
1248
|
|
|
|
|
|
|
|
1249
|
|
|
|
|
|
|
Dying is ignored, only producing a warning. |
1250
|
|
|
|
|
|
|
|
1251
|
|
|
|
|
|
|
=head2 pre_cleanup |
1252
|
|
|
|
|
|
|
|
1253
|
|
|
|
|
|
|
This hook is run AFTER all postponed actions set up in controller |
1254
|
|
|
|
|
|
|
(via C<-continue> etc), but BEFORE the request object is actually destroyed. |
1255
|
|
|
|
|
|
|
This can be useful to free some resource or write statistics. |
1256
|
|
|
|
|
|
|
|
1257
|
|
|
|
|
|
|
The client connection MAY be closed at this point and SHOULD NOT be relied upon. |
1258
|
|
|
|
|
|
|
|
1259
|
|
|
|
|
|
|
Hooks are executed in REVERSE order, from longer to shorter paths. |
1260
|
|
|
|
|
|
|
|
1261
|
|
|
|
|
|
|
Dying is ignored, only producing a warning. |
1262
|
|
|
|
|
|
|
|
1263
|
|
|
|
|
|
|
=head1 MORE EXAMPLES |
1264
|
|
|
|
|
|
|
|
1265
|
|
|
|
|
|
|
See the examples directory in this distro or at |
1266
|
|
|
|
|
|
|
L |
1267
|
|
|
|
|
|
|
for complete working examples. |
1268
|
|
|
|
|
|
|
These below are just code snippets. |
1269
|
|
|
|
|
|
|
|
1270
|
|
|
|
|
|
|
All of them are supposed to start and end with: |
1271
|
|
|
|
|
|
|
|
1272
|
|
|
|
|
|
|
use strict; |
1273
|
|
|
|
|
|
|
use warnings; |
1274
|
|
|
|
|
|
|
use MVC::Neaf; |
1275
|
|
|
|
|
|
|
|
1276
|
|
|
|
|
|
|
# ... snippet here |
1277
|
|
|
|
|
|
|
|
1278
|
|
|
|
|
|
|
neaf->run; |
1279
|
|
|
|
|
|
|
|
1280
|
|
|
|
|
|
|
=head2 Static content |
1281
|
|
|
|
|
|
|
|
1282
|
|
|
|
|
|
|
neaf->static( '/images' => "/local/images" ); |
1283
|
|
|
|
|
|
|
neaf->static( '/favicon.ico' => "/local/images/icon_32x32.png" ); |
1284
|
|
|
|
|
|
|
neaf->static( '/robots.txt' => [ "Disallow: *\n", "text/plain "] ); |
1285
|
|
|
|
|
|
|
|
1286
|
|
|
|
|
|
|
=head2 Form submission |
1287
|
|
|
|
|
|
|
|
1288
|
|
|
|
|
|
|
# You're still encouraged to use LIVR for more detailed validation |
1289
|
|
|
|
|
|
|
my %profile = ( |
1290
|
|
|
|
|
|
|
name => [ required => '\w+' ], |
1291
|
|
|
|
|
|
|
age => '\d+', |
1292
|
|
|
|
|
|
|
); |
1293
|
|
|
|
|
|
|
neaf form my_form => \%profile; |
1294
|
|
|
|
|
|
|
|
1295
|
|
|
|
|
|
|
get+post '/submit' => sub { |
1296
|
|
|
|
|
|
|
my $req = shift; |
1297
|
|
|
|
|
|
|
|
1298
|
|
|
|
|
|
|
my $form = $req->form( "my_form" ); |
1299
|
|
|
|
|
|
|
if ($req->is_post and $form->is_valid) { |
1300
|
|
|
|
|
|
|
my $id = do_something( $form->data ); |
1301
|
|
|
|
|
|
|
$req->redirect( "/result/$id" ); |
1302
|
|
|
|
|
|
|
}; |
1303
|
|
|
|
|
|
|
|
1304
|
|
|
|
|
|
|
return { |
1305
|
|
|
|
|
|
|
-template => 'form.tt', |
1306
|
|
|
|
|
|
|
errors => $form->error, |
1307
|
|
|
|
|
|
|
fill_values => $form->raw, |
1308
|
|
|
|
|
|
|
}; |
1309
|
|
|
|
|
|
|
}; |
1310
|
|
|
|
|
|
|
|
1311
|
|
|
|
|
|
|
=head2 Adding JSONP callbacks |
1312
|
|
|
|
|
|
|
|
1313
|
|
|
|
|
|
|
neaf pre_render => sub { |
1314
|
|
|
|
|
|
|
my $req = shift; |
1315
|
|
|
|
|
|
|
$req->reply->{-jsonp} = $req->param("callback" => '.*'); |
1316
|
|
|
|
|
|
|
# Even if you put no restriction here, no XSS comes through |
1317
|
|
|
|
|
|
|
# as JS View has its own default filter |
1318
|
|
|
|
|
|
|
}, path => '/js/api'; |
1319
|
|
|
|
|
|
|
|
1320
|
|
|
|
|
|
|
More examples to follow as usage (hopefully) accumulates. |
1321
|
|
|
|
|
|
|
|
1322
|
|
|
|
|
|
|
=head1 FOUNDATIONS OF NEAF |
1323
|
|
|
|
|
|
|
|
1324
|
|
|
|
|
|
|
=over |
1325
|
|
|
|
|
|
|
|
1326
|
|
|
|
|
|
|
=item * Data in, data out. |
1327
|
|
|
|
|
|
|
|
1328
|
|
|
|
|
|
|
A I should receive an I and return a I or I. |
1329
|
|
|
|
|
|
|
Everything else should be confined within the function. |
1330
|
|
|
|
|
|
|
This applies to both Neaf's own methods and the user code. |
1331
|
|
|
|
|
|
|
|
1332
|
|
|
|
|
|
|
A notable exception is the session mechanism which is naturally stateful |
1333
|
|
|
|
|
|
|
and thus hard to implement in functional style. |
1334
|
|
|
|
|
|
|
|
1335
|
|
|
|
|
|
|
=item * Sane defaults. |
1336
|
|
|
|
|
|
|
|
1337
|
|
|
|
|
|
|
Everything can be configured, nothing needs to be. |
1338
|
|
|
|
|
|
|
C view needs work in this respect. |
1339
|
|
|
|
|
|
|
|
1340
|
|
|
|
|
|
|
=item * It's not software unless you can run it. |
1341
|
|
|
|
|
|
|
|
1342
|
|
|
|
|
|
|
Don't rely on a specific server environment. |
1343
|
|
|
|
|
|
|
Be ready to run as a standalone program or inside a test script. |
1344
|
|
|
|
|
|
|
|
1345
|
|
|
|
|
|
|
=item * Trust nobody. |
1346
|
|
|
|
|
|
|
|
1347
|
|
|
|
|
|
|
Validate incoming data. |
1348
|
|
|
|
|
|
|
This is not yet enforced for HTTP headers and body. |
1349
|
|
|
|
|
|
|
|
1350
|
|
|
|
|
|
|
=item * Unicode inside the perimeter. |
1351
|
|
|
|
|
|
|
|
1352
|
|
|
|
|
|
|
This is not yet implemented (but planned) for body and file uploads |
1353
|
|
|
|
|
|
|
because these may well be binary data. |
1354
|
|
|
|
|
|
|
|
1355
|
|
|
|
|
|
|
=back |
1356
|
|
|
|
|
|
|
|
1357
|
|
|
|
|
|
|
=head1 BUGS |
1358
|
|
|
|
|
|
|
|
1359
|
|
|
|
|
|
|
This software is still in BETA stage. |
1360
|
|
|
|
|
|
|
|
1361
|
|
|
|
|
|
|
Test coverage is maintained at >80% currently, |
1362
|
|
|
|
|
|
|
but who knows what lurks in the other 20%. |
1363
|
|
|
|
|
|
|
|
1364
|
|
|
|
|
|
|
See the C file in this distribution for a vague roadmap. |
1365
|
|
|
|
|
|
|
|
1366
|
|
|
|
|
|
|
Please report any bugs or feature requests to |
1367
|
|
|
|
|
|
|
L. |
1368
|
|
|
|
|
|
|
|
1369
|
|
|
|
|
|
|
Alternatively, email them to C, or report through |
1370
|
|
|
|
|
|
|
the web interface at L. |
1371
|
|
|
|
|
|
|
|
1372
|
|
|
|
|
|
|
Feedback and/or critique are welcome. |
1373
|
|
|
|
|
|
|
|
1374
|
|
|
|
|
|
|
=head1 SUPPORT |
1375
|
|
|
|
|
|
|
|
1376
|
|
|
|
|
|
|
Feel free to email the author to get instant help! |
1377
|
|
|
|
|
|
|
|
1378
|
|
|
|
|
|
|
You can find documentation for this module with the C command: |
1379
|
|
|
|
|
|
|
|
1380
|
|
|
|
|
|
|
perldoc MVC::Neaf |
1381
|
|
|
|
|
|
|
perldoc MVC::Neaf::Request |
1382
|
|
|
|
|
|
|
|
1383
|
|
|
|
|
|
|
You can also look for information at: |
1384
|
|
|
|
|
|
|
|
1385
|
|
|
|
|
|
|
=over |
1386
|
|
|
|
|
|
|
|
1387
|
|
|
|
|
|
|
=item * Github: L |
1388
|
|
|
|
|
|
|
|
1389
|
|
|
|
|
|
|
=item * MetaCPAN: L |
1390
|
|
|
|
|
|
|
|
1391
|
|
|
|
|
|
|
=item * C |
1392
|
|
|
|
|
|
|
|
1393
|
|
|
|
|
|
|
L |
1394
|
|
|
|
|
|
|
|
1395
|
|
|
|
|
|
|
=item * AnnoCPAN: Annotated CPAN documentation |
1396
|
|
|
|
|
|
|
|
1397
|
|
|
|
|
|
|
L |
1398
|
|
|
|
|
|
|
|
1399
|
|
|
|
|
|
|
=item * CPAN Ratings |
1400
|
|
|
|
|
|
|
|
1401
|
|
|
|
|
|
|
L |
1402
|
|
|
|
|
|
|
|
1403
|
|
|
|
|
|
|
=item * Search CPAN |
1404
|
|
|
|
|
|
|
|
1405
|
|
|
|
|
|
|
L |
1406
|
|
|
|
|
|
|
|
1407
|
|
|
|
|
|
|
=back |
1408
|
|
|
|
|
|
|
|
1409
|
|
|
|
|
|
|
=head1 SEE ALSO |
1410
|
|
|
|
|
|
|
|
1411
|
|
|
|
|
|
|
The L framework has very similar concept. |
1412
|
|
|
|
|
|
|
|
1413
|
|
|
|
|
|
|
Neaf has a lot of similarities to L, |
1414
|
|
|
|
|
|
|
initially unintentional. |
1415
|
|
|
|
|
|
|
|
1416
|
|
|
|
|
|
|
=head1 ACKNOWLEDGEMENTS |
1417
|
|
|
|
|
|
|
|
1418
|
|
|
|
|
|
|
Ideas were shamelessly stolen from L, L, L, |
1419
|
|
|
|
|
|
|
and L. |
1420
|
|
|
|
|
|
|
|
1421
|
|
|
|
|
|
|
L was used heavily in the beginning of development, |
1422
|
|
|
|
|
|
|
though Neaf was C-ready from the start. |
1423
|
|
|
|
|
|
|
|
1424
|
|
|
|
|
|
|
Thanks to L |
1425
|
|
|
|
|
|
|
for introducing me to the MVC concept. |
1426
|
|
|
|
|
|
|
|
1427
|
|
|
|
|
|
|
Thanks to L |
1428
|
|
|
|
|
|
|
for early feedback and great insights about pure functions and side effects. |
1429
|
|
|
|
|
|
|
|
1430
|
|
|
|
|
|
|
Thanks to L |
1431
|
|
|
|
|
|
|
for driving me towards proper hooks model. |
1432
|
|
|
|
|
|
|
|
1433
|
|
|
|
|
|
|
Thanks to L |
1434
|
|
|
|
|
|
|
for early feedback and feature proposals. |
1435
|
|
|
|
|
|
|
|
1436
|
|
|
|
|
|
|
Thanks to Alexey Kuznetsov |
1437
|
|
|
|
|
|
|
for requesting REST support and thus |
1438
|
|
|
|
|
|
|
adding of multiple methods for the same path. |
1439
|
|
|
|
|
|
|
|
1440
|
|
|
|
|
|
|
=head1 LICENSE AND COPYRIGHT |
1441
|
|
|
|
|
|
|
|
1442
|
|
|
|
|
|
|
Copyright 2016-2023 Konstantin S. Uvarin C. |
1443
|
|
|
|
|
|
|
|
1444
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify it |
1445
|
|
|
|
|
|
|
under the terms of either: the GNU General Public License as published |
1446
|
|
|
|
|
|
|
by the Free Software Foundation; or the Artistic License. |
1447
|
|
|
|
|
|
|
|
1448
|
|
|
|
|
|
|
See L for more information. |
1449
|
|
|
|
|
|
|
|
1450
|
|
|
|
|
|
|
=cut |
1451
|
|
|
|
|
|
|
|
1452
|
|
|
|
|
|
|
1; # End of MVC::Neaf |