Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Request; |
2 | |
3 | use strict; |
4 | use base 'Class::Accessor::Fast'; |
5 | |
b4ca0ee8 |
6 | use IO::Socket qw[AF_INET inet_aton]; |
7 | |
fc7ec1d9 |
8 | __PACKAGE__->mk_accessors( |
fbcc39ad |
9 | qw/action address arguments base cookies handle headers match method |
10 | protocol query_parameters secure snippets uri user/ |
fc7ec1d9 |
11 | ); |
12 | |
fbcc39ad |
13 | *args = \&arguments; |
14 | *body_params = \&body_parameters; |
15 | *input = \&body; |
16 | *params = \¶meters; |
17 | *query_params = \&query_parameters; |
18 | *path_info = \&path; |
fc7ec1d9 |
19 | |
f7e4e231 |
20 | sub content_encoding { shift->headers->content_encoding(@_) } |
fbcc39ad |
21 | sub content_length { shift->headers->content_length(@_) } |
22 | sub content_type { shift->headers->content_type(@_) } |
23 | sub header { shift->headers->header(@_) } |
24 | sub referer { shift->headers->referer(@_) } |
25 | sub user_agent { shift->headers->user_agent(@_) } |
f7e4e231 |
26 | |
fc7ec1d9 |
27 | =head1 NAME |
28 | |
29 | Catalyst::Request - Catalyst Request Class |
30 | |
31 | =head1 SYNOPSIS |
32 | |
b22c6668 |
33 | |
34 | $req = $c->request; |
35 | $req->action; |
36 | $req->address; |
37 | $req->args; |
38 | $req->arguments; |
39 | $req->base; |
06e1b616 |
40 | $req->body; |
fbcc39ad |
41 | $req->body_parameters; |
b5176d9e |
42 | $req->content_encoding; |
43 | $req->content_length; |
44 | $req->content_type; |
b77e7869 |
45 | $req->cookie; |
b22c6668 |
46 | $req->cookies; |
fbcc39ad |
47 | $req->handle; |
b5176d9e |
48 | $req->header; |
b22c6668 |
49 | $req->headers; |
50 | $req->hostname; |
61bacdcc |
51 | $req->input; |
b22c6668 |
52 | $req->match; |
53 | $req->method; |
e7c0c583 |
54 | $req->param; |
b22c6668 |
55 | $req->params; |
e7c0c583 |
56 | $req->parameters; |
b22c6668 |
57 | $req->path; |
bfde09a2 |
58 | $req->protocol; |
fbcc39ad |
59 | $req->query_parameters; |
60 | $req->read; |
b5176d9e |
61 | $req->referer; |
bfde09a2 |
62 | $req->secure; |
b22c6668 |
63 | $req->snippets; |
e7c0c583 |
64 | $req->upload; |
b22c6668 |
65 | $req->uploads; |
77d12cae |
66 | $req->uri; |
66294129 |
67 | $req->user; |
68 | $req->user_agent; |
b22c6668 |
69 | |
70 | See also L<Catalyst>. |
fc7ec1d9 |
71 | |
72 | =head1 DESCRIPTION |
73 | |
b22c6668 |
74 | This is the Catalyst Request class, which provides a set of accessors to the |
75 | request data. The request object is prepared by the specialized Catalyst |
76 | Engine module thus hiding the details of the particular engine implementation. |
77 | |
78 | |
79 | =head1 METHODS |
fc7ec1d9 |
80 | |
b22c6668 |
81 | =over 4 |
fc7ec1d9 |
82 | |
b22c6668 |
83 | =item $req->action |
fc7ec1d9 |
84 | |
61b1e958 |
85 | Contains the requested action. |
fc7ec1d9 |
86 | |
87 | print $c->request->action; |
88 | |
b22c6668 |
89 | =item $req->address |
0556eb49 |
90 | |
91 | Contains the remote address. |
92 | |
93 | print $c->request->address |
94 | |
b22c6668 |
95 | =item $req->args |
96 | |
61b1e958 |
97 | Shortcut for arguments |
98 | |
99 | =item $req->arguments |
100 | |
b22c6668 |
101 | Returns a reference to an array containing the arguments. |
fc7ec1d9 |
102 | |
103 | print $c->request->arguments->[0]; |
104 | |
b22c6668 |
105 | =item $req->base |
fc7ec1d9 |
106 | |
61b1e958 |
107 | Contains the url base. This will always have a trailing slash. |
fc7ec1d9 |
108 | |
06e1b616 |
109 | =item $req->body |
110 | |
e060fe05 |
111 | Contains the message body of the request unless Content-Type is |
112 | C<application/x-www-form-urlencoded> or C<multipart/form-data>. |
113 | |
114 | print $c->request->body |
06e1b616 |
115 | |
fbcc39ad |
116 | =cut |
117 | |
118 | sub body { |
119 | my ( $self, $body ) = @_; |
120 | $self->{_context}->prepare_body; |
121 | return $self->{_body}->body; |
122 | } |
123 | |
124 | =item $req->body_parameters |
125 | |
126 | Returns a reference to a hash containing body parameters. Values can |
127 | be either a scalar or an arrayref containing scalars. |
128 | |
129 | print $c->request->body_parameters->{field}; |
130 | print $c->request->body_parameters->{field}->[0]; |
131 | |
132 | =item $req->body_params |
133 | |
134 | An alias for body_parameters. |
135 | |
136 | =cut |
137 | |
138 | sub body_parameters { |
139 | my ( $self, $params ) = @_; |
140 | $self->{_context}->prepare_body; |
141 | $self->{body_parameters} = $params if $params; |
142 | return $self->{body_parameters}; |
143 | } |
144 | |
b5176d9e |
145 | =item $req->content_encoding |
146 | |
147 | Shortcut to $req->headers->content_encoding |
148 | |
149 | =item $req->content_length |
150 | |
151 | Shortcut to $req->headers->content_length |
152 | |
153 | =item $req->content_type |
154 | |
155 | Shortcut to $req->headers->content_type |
156 | |
3ad654e0 |
157 | =item $req->cookie |
158 | |
159 | A convenient method to $req->cookies. |
160 | |
161 | $cookie = $c->request->cookie('name'); |
162 | @cookies = $c->request->cookie; |
163 | |
164 | =cut |
165 | |
166 | sub cookie { |
167 | my $self = shift; |
168 | |
169 | if ( @_ == 0 ) { |
b77e7869 |
170 | return keys %{ $self->cookies }; |
3ad654e0 |
171 | } |
172 | |
173 | if ( @_ == 1 ) { |
174 | |
175 | my $name = shift; |
176 | |
b77e7869 |
177 | unless ( exists $self->cookies->{$name} ) { |
3ad654e0 |
178 | return undef; |
179 | } |
fbcc39ad |
180 | |
b77e7869 |
181 | return $self->cookies->{$name}; |
3ad654e0 |
182 | } |
183 | } |
184 | |
b22c6668 |
185 | =item $req->cookies |
fc7ec1d9 |
186 | |
b22c6668 |
187 | Returns a reference to a hash containing the cookies. |
fc7ec1d9 |
188 | |
189 | print $c->request->cookies->{mycookie}->value; |
190 | |
fbcc39ad |
191 | =item $req->handle |
696e8ae5 |
192 | |
fbcc39ad |
193 | Request IO handle. |
696e8ae5 |
194 | |
b5176d9e |
195 | =item $req->header |
196 | |
197 | Shortcut to $req->headers->header |
198 | |
b22c6668 |
199 | =item $req->headers |
fc7ec1d9 |
200 | |
b22c6668 |
201 | Returns an L<HTTP::Headers> object containing the headers. |
fc7ec1d9 |
202 | |
203 | print $c->request->headers->header('X-Catalyst'); |
204 | |
b22c6668 |
205 | =item $req->hostname |
0556eb49 |
206 | |
b4ca0ee8 |
207 | Lookup the current users DNS hostname. |
0556eb49 |
208 | |
209 | print $c->request->hostname |
b4ca0ee8 |
210 | |
211 | =cut |
212 | |
213 | sub hostname { |
214 | my $self = shift; |
215 | |
a268a011 |
216 | if ( @_ == 0 && not $self->{hostname} ) { |
fbcc39ad |
217 | $self->{hostname} = |
218 | gethostbyaddr( inet_aton( $self->address ), AF_INET ); |
b4ca0ee8 |
219 | } |
220 | |
a268a011 |
221 | if ( @_ == 1 ) { |
222 | $self->{hostname} = shift; |
b4ca0ee8 |
223 | } |
224 | |
225 | return $self->{hostname}; |
226 | } |
0556eb49 |
227 | |
61bacdcc |
228 | =item $req->input |
229 | |
e060fe05 |
230 | Shortcut for $req->body. |
61bacdcc |
231 | |
b22c6668 |
232 | =item $req->match |
fc7ec1d9 |
233 | |
c1f33816 |
234 | This contains the matching part of a regexp action. Otherwise |
235 | it returns the same as 'action'. |
fc7ec1d9 |
236 | |
237 | print $c->request->match; |
238 | |
b5176d9e |
239 | =item $req->method |
240 | |
241 | Contains the request method (C<GET>, C<POST>, C<HEAD>, etc). |
242 | |
e7c0c583 |
243 | print $c->request->method; |
244 | |
245 | =item $req->param |
246 | |
2ef2fb0f |
247 | Get request parameters with a CGI.pm-compatible param method. This |
248 | is a method for accessing parameters in $c->req->parameters. |
e7c0c583 |
249 | |
250 | $value = $c->request->param('foo'); |
251 | @values = $c->request->param('foo'); |
252 | @params = $c->request->param; |
253 | |
254 | =cut |
255 | |
256 | sub param { |
257 | my $self = shift; |
258 | |
259 | if ( @_ == 0 ) { |
260 | return keys %{ $self->parameters }; |
261 | } |
262 | |
bfde09a2 |
263 | if ( @_ == 1 ) { |
e7c0c583 |
264 | |
bfde09a2 |
265 | my $param = shift; |
6bd2b72c |
266 | |
bfde09a2 |
267 | unless ( exists $self->parameters->{$param} ) { |
268 | return wantarray ? () : undef; |
269 | } |
270 | |
271 | if ( ref $self->parameters->{$param} eq 'ARRAY' ) { |
272 | return (wantarray) |
273 | ? @{ $self->parameters->{$param} } |
274 | : $self->parameters->{$param}->[0]; |
275 | } |
276 | else { |
277 | return (wantarray) |
278 | ? ( $self->parameters->{$param} ) |
279 | : $self->parameters->{$param}; |
280 | } |
d7945f32 |
281 | } |
bfde09a2 |
282 | |
03222156 |
283 | if ( @_ > 1 ) { |
bfde09a2 |
284 | |
285 | while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) { |
fbcc39ad |
286 | |
a4def412 |
287 | next unless defined $field; |
bfde09a2 |
288 | |
289 | if ( exists $self->parameters->{$field} ) { |
290 | for ( $self->parameters->{$field} ) { |
291 | $_ = [$_] unless ref($_) eq "ARRAY"; |
292 | push( @$_, $value ); |
293 | } |
294 | } |
295 | else { |
296 | $self->parameters->{$field} = $value; |
297 | } |
298 | } |
d7945f32 |
299 | } |
e7c0c583 |
300 | } |
b5176d9e |
301 | |
b22c6668 |
302 | =item $req->params |
fc7ec1d9 |
303 | |
61b1e958 |
304 | Shortcut for $req->parameters. |
305 | |
306 | =item $req->parameters |
307 | |
e7c0c583 |
308 | Returns a reference to a hash containing parameters. Values can |
d08ced28 |
309 | be either a scalar or an arrayref containing scalars. |
fc7ec1d9 |
310 | |
e7c0c583 |
311 | print $c->request->parameters->{field}; |
312 | print $c->request->parameters->{field}->[0]; |
fc7ec1d9 |
313 | |
fbcc39ad |
314 | =cut |
315 | |
316 | sub parameters { |
317 | my ( $self, $params ) = @_; |
318 | $self->{_context}->prepare_body; |
319 | $self->{parameters} = $params if $params; |
320 | return $self->{parameters}; |
321 | } |
322 | |
b22c6668 |
323 | =item $req->path |
fc7ec1d9 |
324 | |
325 | Contains the path. |
326 | |
327 | print $c->request->path; |
328 | |
fbcc39ad |
329 | =item $req->path_info |
330 | |
331 | alias for path, added for compability with L<CGI> |
332 | |
333 | =cut |
334 | |
335 | sub path { |
336 | my ( $self, $params ) = @_; |
337 | |
338 | if ( $params ) { |
339 | # base must always have a trailing slash |
340 | $params .= '/' unless ( $params =~ /\/$/ ); |
341 | $self->uri->path( $params ); |
342 | } |
343 | |
344 | my $path = $self->uri->path; |
345 | my $location = $self->base->path; |
346 | $path =~ s/^(\Q$location\E)?//; |
347 | $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; |
348 | $path =~ s/^\///; |
349 | |
350 | return $path; |
351 | } |
352 | |
bfde09a2 |
353 | =item $req->protocol |
354 | |
355 | Contains the protocol. |
356 | |
fbcc39ad |
357 | =item $req->query_parameters |
358 | |
359 | Returns a reference to a hash containing query parameters. Values can |
360 | be either a scalar or an arrayref containing scalars. |
361 | |
362 | print $c->request->query_parameters->{field}; |
363 | print $c->request->query_parameters->{field}->[0]; |
364 | |
365 | =item $req->read( [$maxlength] ) |
366 | |
367 | Read a chunk of data from the request body. This method is designed to be |
368 | used in a while loop, reading $maxlength bytes on every call. $maxlength |
369 | defaults to the size of the request if not specified. |
370 | |
371 | You have to set MyApp->config->{parse_on_demand} to use this directly. |
372 | |
373 | =cut |
374 | |
375 | sub read { shift->{_context}->read(@_); } |
376 | |
b5176d9e |
377 | =item $req->referer |
fc7ec1d9 |
378 | |
61b1e958 |
379 | Shortcut to $req->headers->referer. Referring page. |
fc7ec1d9 |
380 | |
bfde09a2 |
381 | =item $req->secure |
382 | |
26e73131 |
383 | Contains a boolean denoting whether the communication is secure. |
bfde09a2 |
384 | |
b22c6668 |
385 | =item $req->snippets |
fc7ec1d9 |
386 | |
b22c6668 |
387 | Returns a reference to an array containing regex snippets. |
fc7ec1d9 |
388 | |
389 | my @snippets = @{ $c->request->snippets }; |
390 | |
e7c0c583 |
391 | =item $req->upload |
392 | |
393 | A convenient method to $req->uploads. |
394 | |
395 | $upload = $c->request->upload('field'); |
396 | @uploads = $c->request->upload('field'); |
397 | @fields = $c->request->upload; |
bfde09a2 |
398 | |
e7c0c583 |
399 | for my $upload ( $c->request->upload('field') ) { |
146554c5 |
400 | print $upload->filename; |
e7c0c583 |
401 | } |
402 | |
403 | =cut |
404 | |
405 | sub upload { |
406 | my $self = shift; |
407 | |
408 | if ( @_ == 0 ) { |
409 | return keys %{ $self->uploads }; |
410 | } |
411 | |
bfde09a2 |
412 | if ( @_ == 1 ) { |
e7c0c583 |
413 | |
bfde09a2 |
414 | my $upload = shift; |
415 | |
416 | unless ( exists $self->uploads->{$upload} ) { |
417 | return wantarray ? () : undef; |
418 | } |
6bd2b72c |
419 | |
bfde09a2 |
420 | if ( ref $self->uploads->{$upload} eq 'ARRAY' ) { |
421 | return (wantarray) |
422 | ? @{ $self->uploads->{$upload} } |
423 | : $self->uploads->{$upload}->[0]; |
424 | } |
425 | else { |
426 | return (wantarray) |
fbcc39ad |
427 | ? ( $self->uploads->{$upload} ) |
428 | : $self->uploads->{$upload}; |
bfde09a2 |
429 | } |
d7945f32 |
430 | } |
bfde09a2 |
431 | |
a4f5c51e |
432 | if ( @_ > 1 ) { |
bfde09a2 |
433 | |
434 | while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) { |
435 | |
436 | if ( exists $self->uploads->{$field} ) { |
437 | for ( $self->uploads->{$field} ) { |
438 | $_ = [$_] unless ref($_) eq "ARRAY"; |
439 | push( @$_, $upload ); |
440 | } |
441 | } |
442 | else { |
443 | $self->uploads->{$field} = $upload; |
444 | } |
445 | } |
e7c0c583 |
446 | } |
447 | } |
448 | |
b22c6668 |
449 | =item $req->uploads |
fc7ec1d9 |
450 | |
bfde09a2 |
451 | Returns a reference to a hash containing uploads. Values can be either a |
146554c5 |
452 | hashref or a arrayref containing C<Catalyst::Request::Upload> objects. |
e7c0c583 |
453 | |
454 | my $upload = $c->request->uploads->{field}; |
455 | my $upload = $c->request->uploads->{field}->[0]; |
456 | |
77d12cae |
457 | =cut |
458 | |
fbcc39ad |
459 | sub uploads { |
460 | my ( $self, $uploads ) = @_; |
461 | $self->{_context}->prepare_body; |
462 | $self->{uploads} = $uploads if $uploads; |
463 | return $self->{uploads}; |
77d12cae |
464 | } |
465 | |
fbcc39ad |
466 | =item $req->uri |
467 | |
468 | Returns a URI object for the request. |
469 | |
66294129 |
470 | =item $req->user |
471 | |
472 | Contains the user name of user if authentication check was successful. |
473 | |
b5176d9e |
474 | =item $req->user_agent |
475 | |
61b1e958 |
476 | Shortcut to $req->headers->user_agent. User Agent version string. |
b5176d9e |
477 | |
b22c6668 |
478 | =back |
479 | |
fc7ec1d9 |
480 | =head1 AUTHOR |
481 | |
482 | Sebastian Riedel, C<sri@cpan.org> |
61b1e958 |
483 | Marcus Ramberg, C<mramberg@cpan.org> |
fc7ec1d9 |
484 | |
485 | =head1 COPYRIGHT |
486 | |
e7c0c583 |
487 | This program is free software, you can redistribute it and/or modify |
61b1e958 |
488 | it under the same terms as Perl itself. |
fc7ec1d9 |
489 | |
490 | =cut |
491 | |
492 | 1; |