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