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