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]; |
bd917b94 |
7 | use Carp; |
fc42a730 |
8 | use utf8; |
e669e88a |
9 | use URI::QueryParam; |
b4ca0ee8 |
10 | |
fc7ec1d9 |
11 | __PACKAGE__->mk_accessors( |
8b76bfcf |
12 | qw/action address arguments cookies headers match method |
2982e768 |
13 | protocol query_parameters secure captures uri user/ |
fc7ec1d9 |
14 | ); |
15 | |
fbcc39ad |
16 | *args = \&arguments; |
17 | *body_params = \&body_parameters; |
18 | *input = \&body; |
19 | *params = \¶meters; |
20 | *query_params = \&query_parameters; |
21 | *path_info = \&path; |
2982e768 |
22 | *snippets = \&captures; |
fc7ec1d9 |
23 | |
f7e4e231 |
24 | sub content_encoding { shift->headers->content_encoding(@_) } |
fbcc39ad |
25 | sub content_length { shift->headers->content_length(@_) } |
26 | sub content_type { shift->headers->content_type(@_) } |
27 | sub header { shift->headers->header(@_) } |
28 | sub referer { shift->headers->referer(@_) } |
29 | sub user_agent { shift->headers->user_agent(@_) } |
f7e4e231 |
30 | |
fc7ec1d9 |
31 | =head1 NAME |
32 | |
3e19f4f6 |
33 | Catalyst::Request - provides information about the current client request |
fc7ec1d9 |
34 | |
35 | =head1 SYNOPSIS |
36 | |
b22c6668 |
37 | $req = $c->request; |
38 | $req->action; |
39 | $req->address; |
b22c6668 |
40 | $req->arguments; |
3e19f4f6 |
41 | $req->args; |
b22c6668 |
42 | $req->base; |
06e1b616 |
43 | $req->body; |
fbcc39ad |
44 | $req->body_parameters; |
b5176d9e |
45 | $req->content_encoding; |
46 | $req->content_length; |
47 | $req->content_type; |
b77e7869 |
48 | $req->cookie; |
b22c6668 |
49 | $req->cookies; |
b5176d9e |
50 | $req->header; |
b22c6668 |
51 | $req->headers; |
52 | $req->hostname; |
61bacdcc |
53 | $req->input; |
b22c6668 |
54 | $req->match; |
55 | $req->method; |
e7c0c583 |
56 | $req->param; |
e7c0c583 |
57 | $req->parameters; |
3e19f4f6 |
58 | $req->params; |
b22c6668 |
59 | $req->path; |
bfde09a2 |
60 | $req->protocol; |
fbcc39ad |
61 | $req->query_parameters; |
62 | $req->read; |
b5176d9e |
63 | $req->referer; |
bfde09a2 |
64 | $req->secure; |
2982e768 |
65 | $req->captures; # previously knows as snippets |
e7c0c583 |
66 | $req->upload; |
b22c6668 |
67 | $req->uploads; |
77d12cae |
68 | $req->uri; |
7ce7ca2e |
69 | $req->user; |
66294129 |
70 | $req->user_agent; |
b22c6668 |
71 | |
3e22baa5 |
72 | See also L<Catalyst>, L<Catalyst::Request::Upload>. |
fc7ec1d9 |
73 | |
74 | =head1 DESCRIPTION |
75 | |
3e19f4f6 |
76 | This is the Catalyst Request class, which provides an interface to data for the |
77 | current client request. The request object is prepared by L<Catalyst::Engine>, |
78 | thus hiding the details of the particular engine implementation. |
b22c6668 |
79 | |
80 | =head1 METHODS |
fc7ec1d9 |
81 | |
b5ecfcf0 |
82 | =head2 $req->action |
fc7ec1d9 |
83 | |
aae8d418 |
84 | [DEPRECATED] Returns the name of the requested action. |
85 | |
86 | |
87 | Use C<< $c->action >> instead (which returns a |
88 | L<Catalyst::Action|Catalyst::Action> object). |
fc7ec1d9 |
89 | |
b5ecfcf0 |
90 | =head2 $req->address |
0556eb49 |
91 | |
3e19f4f6 |
92 | Returns the IP address of the client. |
61b1e958 |
93 | |
b5ecfcf0 |
94 | =head2 $req->arguments |
61b1e958 |
95 | |
b22c6668 |
96 | Returns a reference to an array containing the arguments. |
fc7ec1d9 |
97 | |
98 | print $c->request->arguments->[0]; |
99 | |
c436c1e8 |
100 | For example, if your action was |
101 | |
102 | package MyApp::C::Foo; |
103 | |
104 | sub moose : Local { |
105 | ... |
106 | } |
107 | |
3e19f4f6 |
108 | and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah> |
c436c1e8 |
109 | would be the first and only argument. |
110 | |
b5ecfcf0 |
111 | =head2 $req->args |
3e19f4f6 |
112 | |
113 | Shortcut for arguments. |
114 | |
b5ecfcf0 |
115 | =head2 $req->base |
fc7ec1d9 |
116 | |
c436c1e8 |
117 | Contains the URI base. This will always have a trailing slash. |
118 | |
3e19f4f6 |
119 | If your application was queried with the URI |
120 | C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>. |
fc7ec1d9 |
121 | |
e561386f |
122 | =cut |
123 | |
124 | sub base { |
125 | my ( $self, $base ) = @_; |
6aa02946 |
126 | |
e561386f |
127 | return $self->{base} unless $base; |
6aa02946 |
128 | |
e561386f |
129 | $self->{base} = $base; |
6aa02946 |
130 | |
e561386f |
131 | # set the value in path for backwards-compat |
132 | if ( $self->uri ) { |
133 | $self->path; |
134 | } |
6aa02946 |
135 | |
e561386f |
136 | return $self->{base}; |
137 | } |
138 | |
b5ecfcf0 |
139 | =head2 $req->body |
06e1b616 |
140 | |
3e19f4f6 |
141 | Returns the message body of the request, unless Content-Type is |
e060fe05 |
142 | C<application/x-www-form-urlencoded> or C<multipart/form-data>. |
143 | |
fbcc39ad |
144 | =cut |
145 | |
146 | sub body { |
147 | my ( $self, $body ) = @_; |
148 | $self->{_context}->prepare_body; |
6e92cc64 |
149 | $self->{_body}->body($body) if $body; |
fbcc39ad |
150 | return $self->{_body}->body; |
151 | } |
152 | |
b5ecfcf0 |
153 | =head2 $req->body_parameters |
fbcc39ad |
154 | |
3e19f4f6 |
155 | Returns a reference to a hash containing body (POST) parameters. Values can |
fbcc39ad |
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 | |
b5ecfcf0 |
163 | =head2 $req->body_params |
fbcc39ad |
164 | |
3e19f4f6 |
165 | Shortcut for body_parameters. |
fbcc39ad |
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 | |
b5ecfcf0 |
176 | =head2 $req->content_encoding |
b5176d9e |
177 | |
3e19f4f6 |
178 | Shortcut for $req->headers->content_encoding. |
b5176d9e |
179 | |
b5ecfcf0 |
180 | =head2 $req->content_length |
b5176d9e |
181 | |
3e19f4f6 |
182 | Shortcut for $req->headers->content_length. |
b5176d9e |
183 | |
b5ecfcf0 |
184 | =head2 $req->content_type |
b5176d9e |
185 | |
3e19f4f6 |
186 | Shortcut for $req->headers->content_type. |
b5176d9e |
187 | |
b5ecfcf0 |
188 | =head2 $req->cookie |
3ad654e0 |
189 | |
3e19f4f6 |
190 | A convenient method to access $req->cookies. |
3ad654e0 |
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 | |
b5ecfcf0 |
216 | =head2 $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 | |
3e19f4f6 |
222 | The cookies in the hash are indexed by name, and the values are L<CGI::Cookie> |
c436c1e8 |
223 | objects. |
224 | |
b5ecfcf0 |
225 | =head2 $req->header |
b5176d9e |
226 | |
3e19f4f6 |
227 | Shortcut for $req->headers->header. |
b5176d9e |
228 | |
b5ecfcf0 |
229 | =head2 $req->headers |
fc7ec1d9 |
230 | |
3e19f4f6 |
231 | Returns an L<HTTP::Headers> object containing the headers for the current request. |
fc7ec1d9 |
232 | |
233 | print $c->request->headers->header('X-Catalyst'); |
234 | |
b5ecfcf0 |
235 | =head2 $req->hostname |
0556eb49 |
236 | |
3e19f4f6 |
237 | Returns the hostname of the client. |
b4ca0ee8 |
238 | |
239 | =cut |
240 | |
241 | sub hostname { |
242 | my $self = shift; |
243 | |
a268a011 |
244 | if ( @_ == 0 && not $self->{hostname} ) { |
fbcc39ad |
245 | $self->{hostname} = |
246 | gethostbyaddr( inet_aton( $self->address ), AF_INET ); |
b4ca0ee8 |
247 | } |
248 | |
a268a011 |
249 | if ( @_ == 1 ) { |
250 | $self->{hostname} = shift; |
b4ca0ee8 |
251 | } |
252 | |
253 | return $self->{hostname}; |
254 | } |
0556eb49 |
255 | |
b5ecfcf0 |
256 | =head2 $req->input |
61bacdcc |
257 | |
3e19f4f6 |
258 | Alias for $req->body. |
61bacdcc |
259 | |
b5ecfcf0 |
260 | =head2 $req->match |
fc7ec1d9 |
261 | |
3e19f4f6 |
262 | This contains the matching part of a Regex action. Otherwise |
2c83fd5a |
263 | it returns the same as 'action', except for default actions, |
264 | which return an empty string. |
fc7ec1d9 |
265 | |
b5ecfcf0 |
266 | =head2 $req->method |
b5176d9e |
267 | |
268 | Contains the request method (C<GET>, C<POST>, C<HEAD>, etc). |
269 | |
b5ecfcf0 |
270 | =head2 $req->param |
e7c0c583 |
271 | |
3e19f4f6 |
272 | Returns GET and POST parameters with a CGI.pm-compatible param method. This |
273 | is an alternative method for accessing parameters in $c->req->parameters. |
e7c0c583 |
274 | |
a82c2894 |
275 | $value = $c->request->param( 'foo' ); |
276 | @values = $c->request->param( 'foo' ); |
e7c0c583 |
277 | @params = $c->request->param; |
278 | |
3e705254 |
279 | Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple |
a82c2894 |
280 | arguments to this method, like this: |
281 | |
3e705254 |
282 | $c->request->param( 'foo', 'bar', 'gorch', 'quxx' ); |
a82c2894 |
283 | |
284 | will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and |
285 | C<quxx>. Previously this would have added C<bar> as another value to C<foo> |
3e19f4f6 |
286 | (creating it if it didn't exist before), and C<quxx> as another value for |
287 | C<gorch>. |
a82c2894 |
288 | |
e7c0c583 |
289 | =cut |
290 | |
291 | sub param { |
292 | my $self = shift; |
293 | |
294 | if ( @_ == 0 ) { |
295 | return keys %{ $self->parameters }; |
296 | } |
297 | |
bfde09a2 |
298 | if ( @_ == 1 ) { |
e7c0c583 |
299 | |
bfde09a2 |
300 | my $param = shift; |
6bd2b72c |
301 | |
bfde09a2 |
302 | unless ( exists $self->parameters->{$param} ) { |
303 | return wantarray ? () : undef; |
304 | } |
305 | |
306 | if ( ref $self->parameters->{$param} eq 'ARRAY' ) { |
307 | return (wantarray) |
308 | ? @{ $self->parameters->{$param} } |
309 | : $self->parameters->{$param}->[0]; |
310 | } |
311 | else { |
312 | return (wantarray) |
313 | ? ( $self->parameters->{$param} ) |
314 | : $self->parameters->{$param}; |
315 | } |
d7945f32 |
316 | } |
a82c2894 |
317 | elsif ( @_ > 1 ) { |
318 | my $field = shift; |
319 | $self->parameters->{$field} = [@_]; |
d7945f32 |
320 | } |
e7c0c583 |
321 | } |
b5176d9e |
322 | |
b5ecfcf0 |
323 | =head2 $req->parameters |
61b1e958 |
324 | |
3e19f4f6 |
325 | Returns a reference to a hash containing GET and POST parameters. Values can |
d08ced28 |
326 | be either a scalar or an arrayref containing scalars. |
fc7ec1d9 |
327 | |
e7c0c583 |
328 | print $c->request->parameters->{field}; |
329 | print $c->request->parameters->{field}->[0]; |
fc7ec1d9 |
330 | |
c436c1e8 |
331 | This is the combination of C<query_parameters> and C<body_parameters>. |
332 | |
b5ecfcf0 |
333 | =head2 $req->params |
3e19f4f6 |
334 | |
335 | Shortcut for $req->parameters. |
336 | |
fbcc39ad |
337 | =cut |
338 | |
339 | sub parameters { |
340 | my ( $self, $params ) = @_; |
341 | $self->{_context}->prepare_body; |
ee26f4bd |
342 | if ( $params ) { |
343 | if ( ref $params ) { |
344 | $self->{parameters} = $params; |
345 | } |
346 | else { |
347 | $self->{_context}->log->warn( |
348 | "Attempt to retrieve '$params' with req->params(), " . |
349 | "you probably meant to call req->param('$params')" ); |
350 | } |
351 | } |
fbcc39ad |
352 | return $self->{parameters}; |
353 | } |
354 | |
b5ecfcf0 |
355 | =head2 $req->path |
fc7ec1d9 |
356 | |
3e19f4f6 |
357 | Returns the path, i.e. the part of the URI after $req->base, for the current request. |
fc7ec1d9 |
358 | |
b5ecfcf0 |
359 | =head2 $req->path_info |
fbcc39ad |
360 | |
3e19f4f6 |
361 | Alias for path, added for compability with L<CGI>. |
fbcc39ad |
362 | |
363 | =cut |
364 | |
365 | sub path { |
366 | my ( $self, $params ) = @_; |
4f5ebacd |
367 | |
368 | if ($params) { |
4f5ebacd |
369 | $self->uri->path($params); |
fbcc39ad |
370 | } |
e561386f |
371 | else { |
372 | return $self->{path} if $self->{path}; |
373 | } |
fbcc39ad |
374 | |
4f5ebacd |
375 | my $path = $self->uri->path; |
fbcc39ad |
376 | my $location = $self->base->path; |
377 | $path =~ s/^(\Q$location\E)?//; |
fbcc39ad |
378 | $path =~ s/^\///; |
e561386f |
379 | $self->{path} = $path; |
4f5ebacd |
380 | |
fbcc39ad |
381 | return $path; |
382 | } |
383 | |
b5ecfcf0 |
384 | =head2 $req->protocol |
bfde09a2 |
385 | |
3e19f4f6 |
386 | Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request. |
bfde09a2 |
387 | |
b5ecfcf0 |
388 | =head2 $req->query_parameters |
fbcc39ad |
389 | |
3e19f4f6 |
390 | Returns a reference to a hash containing query string (GET) parameters. Values can |
fbcc39ad |
391 | be either a scalar or an arrayref containing scalars. |
392 | |
393 | print $c->request->query_parameters->{field}; |
394 | print $c->request->query_parameters->{field}->[0]; |
395 | |
b5ecfcf0 |
396 | =head2 $req->read( [$maxlength] ) |
fbcc39ad |
397 | |
3e19f4f6 |
398 | Reads a chunk of data from the request body. This method is intended to be |
399 | used in a while loop, reading $maxlength bytes on every call. $maxlength |
fbcc39ad |
400 | defaults to the size of the request if not specified. |
401 | |
402 | You have to set MyApp->config->{parse_on_demand} to use this directly. |
403 | |
404 | =cut |
405 | |
406 | sub read { shift->{_context}->read(@_); } |
407 | |
b5ecfcf0 |
408 | =head2 $req->referer |
fc7ec1d9 |
409 | |
3e19f4f6 |
410 | Shortcut for $req->headers->referer. Returns the referring page. |
fc7ec1d9 |
411 | |
b5ecfcf0 |
412 | =head2 $req->secure |
bfde09a2 |
413 | |
3e19f4f6 |
414 | Returns true or false, indicating whether the connection is secure (https). |
bfde09a2 |
415 | |
2982e768 |
416 | =head2 $req->captures |
417 | |
418 | Returns a reference to an array containing regex captures. |
fc7ec1d9 |
419 | |
2982e768 |
420 | my @captures = @{ $c->request->captures }; |
421 | |
422 | =head2 $req->snippets |
fc7ec1d9 |
423 | |
2982e768 |
424 | C<captures> used to be called snippets. This is still available for backwoards |
425 | compatibility, but is considered deprecated. |
fc7ec1d9 |
426 | |
b5ecfcf0 |
427 | =head2 $req->upload |
e7c0c583 |
428 | |
3e19f4f6 |
429 | A convenient method to access $req->uploads. |
e7c0c583 |
430 | |
431 | $upload = $c->request->upload('field'); |
432 | @uploads = $c->request->upload('field'); |
433 | @fields = $c->request->upload; |
bfde09a2 |
434 | |
e7c0c583 |
435 | for my $upload ( $c->request->upload('field') ) { |
146554c5 |
436 | print $upload->filename; |
e7c0c583 |
437 | } |
438 | |
439 | =cut |
440 | |
441 | sub upload { |
442 | my $self = shift; |
443 | |
444 | if ( @_ == 0 ) { |
445 | return keys %{ $self->uploads }; |
446 | } |
447 | |
bfde09a2 |
448 | if ( @_ == 1 ) { |
e7c0c583 |
449 | |
bfde09a2 |
450 | my $upload = shift; |
451 | |
452 | unless ( exists $self->uploads->{$upload} ) { |
453 | return wantarray ? () : undef; |
454 | } |
6bd2b72c |
455 | |
bfde09a2 |
456 | if ( ref $self->uploads->{$upload} eq 'ARRAY' ) { |
457 | return (wantarray) |
458 | ? @{ $self->uploads->{$upload} } |
459 | : $self->uploads->{$upload}->[0]; |
460 | } |
461 | else { |
462 | return (wantarray) |
fbcc39ad |
463 | ? ( $self->uploads->{$upload} ) |
464 | : $self->uploads->{$upload}; |
bfde09a2 |
465 | } |
d7945f32 |
466 | } |
bfde09a2 |
467 | |
a4f5c51e |
468 | if ( @_ > 1 ) { |
bfde09a2 |
469 | |
470 | while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) { |
471 | |
472 | if ( exists $self->uploads->{$field} ) { |
473 | for ( $self->uploads->{$field} ) { |
474 | $_ = [$_] unless ref($_) eq "ARRAY"; |
475 | push( @$_, $upload ); |
476 | } |
477 | } |
478 | else { |
479 | $self->uploads->{$field} = $upload; |
480 | } |
481 | } |
e7c0c583 |
482 | } |
483 | } |
484 | |
b5ecfcf0 |
485 | =head2 $req->uploads |
fc7ec1d9 |
486 | |
bfde09a2 |
487 | Returns a reference to a hash containing uploads. Values can be either a |
84e7aa89 |
488 | L<Catalyst::Request::Upload> object, or an arrayref of |
489 | L<Catalyst::Request::Upload> objects. |
e7c0c583 |
490 | |
491 | my $upload = $c->request->uploads->{field}; |
492 | my $upload = $c->request->uploads->{field}->[0]; |
493 | |
77d12cae |
494 | =cut |
495 | |
fbcc39ad |
496 | sub uploads { |
497 | my ( $self, $uploads ) = @_; |
498 | $self->{_context}->prepare_body; |
499 | $self->{uploads} = $uploads if $uploads; |
500 | return $self->{uploads}; |
77d12cae |
501 | } |
502 | |
b5ecfcf0 |
503 | =head2 $req->uri |
fbcc39ad |
504 | |
3e19f4f6 |
505 | Returns a URI object for the current request. Stringifies to the URI text. |
fbcc39ad |
506 | |
bd917b94 |
507 | =head2 $req->uri_with( { key => 'value' } ); |
508 | |
3338e8ce |
509 | Returns a rewritten URI object for the current request. Key/value pairs |
510 | passed in will override existing parameters. Unmodified pairs will be |
511 | preserved. |
bd917b94 |
512 | |
513 | =cut |
514 | |
515 | sub uri_with { |
516 | my( $self, $args ) = @_; |
517 | |
518 | carp( 'No arguments passed to uri_with()' ) unless $args; |
fbb513f7 |
519 | |
fc42a730 |
520 | for my $value ( values %$args ) { |
d0f0fcf6 |
521 | next unless defined $value; |
fbb513f7 |
522 | for ( ref $value eq 'ARRAY' ? @$value : $value ) { |
523 | $_ = "$_"; |
524 | utf8::encode( $_ ); |
fc42a730 |
525 | } |
fc42a730 |
526 | }; |
fbb513f7 |
527 | |
bd917b94 |
528 | my $uri = $self->uri->clone; |
529 | |
530 | $uri->query_form( { |
27d9619a |
531 | %{ $uri->query_form_hash }, |
bd917b94 |
532 | %$args |
533 | } ); |
534 | return $uri; |
535 | } |
536 | |
b5ecfcf0 |
537 | =head2 $req->user |
7ce7ca2e |
538 | |
3e19f4f6 |
539 | Returns the currently logged in user. Deprecated. The method recommended for |
540 | newer plugins is $c->user. |
7ce7ca2e |
541 | |
b5ecfcf0 |
542 | =head2 $req->user_agent |
b5176d9e |
543 | |
3e19f4f6 |
544 | Shortcut to $req->headers->user_agent. Returns the user agent (browser) |
545 | version string. |
b5176d9e |
546 | |
3e19f4f6 |
547 | =head1 AUTHORS |
fc7ec1d9 |
548 | |
549 | Sebastian Riedel, C<sri@cpan.org> |
3e19f4f6 |
550 | |
61b1e958 |
551 | Marcus Ramberg, C<mramberg@cpan.org> |
fc7ec1d9 |
552 | |
553 | =head1 COPYRIGHT |
554 | |
e7c0c583 |
555 | This program is free software, you can redistribute it and/or modify |
61b1e958 |
556 | it under the same terms as Perl itself. |
fc7ec1d9 |
557 | |
558 | =cut |
559 | |
560 | 1; |