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