minor doc improvements
[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', except for default actions,
255 which return an empty string.
256
257 =head2 $req->method
258
259 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
260
261 =head2 $req->param
262
263 Returns GET and POST parameters with a CGI.pm-compatible param method. This 
264 is an alternative method for accessing parameters in $c->req->parameters.
265
266     $value  = $c->request->param( 'foo' );
267     @values = $c->request->param( 'foo' );
268     @params = $c->request->param;
269
270 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
271 arguments to this method, like this:
272
273         $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
274
275 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
276 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
277 (creating it if it didn't exist before), and C<quxx> as another value for
278 C<gorch>.
279
280 =cut
281
282 sub param {
283     my $self = shift;
284
285     if ( @_ == 0 ) {
286         return keys %{ $self->parameters };
287     }
288
289     if ( @_ == 1 ) {
290
291         my $param = shift;
292
293         unless ( exists $self->parameters->{$param} ) {
294             return wantarray ? () : undef;
295         }
296
297         if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
298             return (wantarray)
299               ? @{ $self->parameters->{$param} }
300               : $self->parameters->{$param}->[0];
301         }
302         else {
303             return (wantarray)
304               ? ( $self->parameters->{$param} )
305               : $self->parameters->{$param};
306         }
307     }
308     elsif ( @_ > 1 ) {
309         my $field = shift;
310         $self->parameters->{$field} = [@_];
311     }
312 }
313
314 =head2 $req->parameters
315
316 Returns a reference to a hash containing GET and POST parameters. Values can
317 be either a scalar or an arrayref containing scalars.
318
319     print $c->request->parameters->{field};
320     print $c->request->parameters->{field}->[0];
321
322 This is the combination of C<query_parameters> and C<body_parameters>.
323
324 =head2 $req->params
325
326 Shortcut for $req->parameters.
327
328 =cut
329
330 sub parameters {
331     my ( $self, $params ) = @_;
332     $self->{_context}->prepare_body;
333     if ( $params ) {
334         if ( ref $params ) {
335             $self->{parameters} = $params;
336         }
337         else {
338             $self->{_context}->log->warn( 
339                 "Attempt to retrieve '$params' with req->params(), " .
340                 "you probably meant to call req->param('$params')" );
341         }
342     }
343     return $self->{parameters};
344 }
345
346 =head2 $req->path
347
348 Returns the path, i.e. the part of the URI after $req->base, for the current request.
349
350 =head2 $req->path_info
351
352 Alias for path, added for compability with L<CGI>.
353
354 =cut
355
356 sub path {
357     my ( $self, $params ) = @_;
358
359     if ($params) {
360         $self->uri->path($params);
361     }
362     else {
363         return $self->{path} if $self->{path};
364     }
365
366     my $path     = $self->uri->path;
367     my $location = $self->base->path;
368     $path =~ s/^(\Q$location\E)?//;
369     $path =~ s/^\///;
370     $self->{path} = $path;
371
372     return $path;
373 }
374
375 =head2 $req->protocol
376
377 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
378
379 =head2 $req->query_parameters
380
381 Returns a reference to a hash containing query string (GET) parameters. Values can
382 be either a scalar or an arrayref containing scalars.
383
384     print $c->request->query_parameters->{field};
385     print $c->request->query_parameters->{field}->[0];
386     
387 =head2 $req->read( [$maxlength] )
388
389 Reads a chunk of data from the request body. This method is intended to be
390 used in a while loop, reading $maxlength bytes on every call. $maxlength
391 defaults to the size of the request if not specified.
392
393 You have to set MyApp->config->{parse_on_demand} to use this directly.
394
395 =cut
396
397 sub read { shift->{_context}->read(@_); }
398
399 =head2 $req->referer
400
401 Shortcut for $req->headers->referer. Returns the referring page.
402
403 =head2 $req->secure
404
405 Returns true or false, indicating whether the connection is secure (https).
406
407 =head2 $req->snippets
408
409 Returns a reference to an array containing regex snippets.
410
411     my @snippets = @{ $c->request->snippets };
412
413 =head2 $req->upload
414
415 A convenient method to access $req->uploads.
416
417     $upload  = $c->request->upload('field');
418     @uploads = $c->request->upload('field');
419     @fields  = $c->request->upload;
420
421     for my $upload ( $c->request->upload('field') ) {
422         print $upload->filename;
423     }
424
425 =cut
426
427 sub upload {
428     my $self = shift;
429
430     if ( @_ == 0 ) {
431         return keys %{ $self->uploads };
432     }
433
434     if ( @_ == 1 ) {
435
436         my $upload = shift;
437
438         unless ( exists $self->uploads->{$upload} ) {
439             return wantarray ? () : undef;
440         }
441
442         if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
443             return (wantarray)
444               ? @{ $self->uploads->{$upload} }
445               : $self->uploads->{$upload}->[0];
446         }
447         else {
448             return (wantarray)
449               ? ( $self->uploads->{$upload} )
450               : $self->uploads->{$upload};
451         }
452     }
453
454     if ( @_ > 1 ) {
455
456         while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
457
458             if ( exists $self->uploads->{$field} ) {
459                 for ( $self->uploads->{$field} ) {
460                     $_ = [$_] unless ref($_) eq "ARRAY";
461                     push( @$_, $upload );
462                 }
463             }
464             else {
465                 $self->uploads->{$field} = $upload;
466             }
467         }
468     }
469 }
470
471 =head2 $req->uploads
472
473 Returns a reference to a hash containing uploads. Values can be either a
474 hashref or a arrayref containing L<Catalyst::Request::Upload> objects.
475
476     my $upload = $c->request->uploads->{field};
477     my $upload = $c->request->uploads->{field}->[0];
478
479 =cut
480
481 sub uploads {
482     my ( $self, $uploads ) = @_;
483     $self->{_context}->prepare_body;
484     $self->{uploads} = $uploads if $uploads;
485     return $self->{uploads};
486 }
487
488 =head2 $req->uri
489
490 Returns a URI object for the current request. Stringifies to the URI text.
491
492 =head2 $req->user
493
494 Returns the currently logged in user. Deprecated. The method recommended for
495 newer plugins is $c->user.
496
497 =head2 $req->user_agent
498
499 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
500 version string.
501
502 =head1 AUTHORS
503
504 Sebastian Riedel, C<sri@cpan.org>
505
506 Marcus Ramberg, C<mramberg@cpan.org>
507
508 =head1 COPYRIGHT
509
510 This program is free software, you can redistribute it and/or modify
511 it under the same terms as Perl itself.
512
513 =cut
514
515 1;