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