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