Fixed dispatcher, so $c->req->action(undef) works again
[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/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 =cut
85
86 sub action { shift->{_context}->action(@_) }
87
88 =item $req->address
89
90 Returns the IP address of the client.
91
92 =item $req->arguments
93
94 Returns a reference to an array containing the arguments.
95
96     print $c->request->arguments->[0];
97
98 For example, if your action was
99
100         package MyApp::C::Foo;
101         
102         sub moose : Local {
103                 ...
104         }
105
106 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
107 would be the first and only argument.
108
109 =item $req->args
110
111 Shortcut for arguments.
112
113 =item $req->base
114
115 Contains the URI base. This will always have a trailing slash.
116
117 If your application was queried with the URI
118 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
119
120 =cut
121
122 sub base {
123     my ( $self, $base ) = @_;
124
125     return $self->{base} unless $base;
126
127     $self->{base} = $base;
128
129     # set the value in path for backwards-compat
130     if ( $self->uri ) {
131         $self->path;
132     }
133
134     return $self->{base};
135 }
136
137 =item $req->body
138
139 Returns the message body of the request, unless Content-Type is
140 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
141
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
152 Returns a reference to a hash containing body (POST) parameters. Values can
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];
157
158 These are the parameters from the POST part of the request, if any.
159     
160 =item $req->body_params
161
162 Shortcut for body_parameters.
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
173 =item $req->content_encoding
174
175 Shortcut for $req->headers->content_encoding.
176
177 =item $req->content_length
178
179 Shortcut for $req->headers->content_length.
180
181 =item $req->content_type
182
183 Shortcut for $req->headers->content_type.
184
185 =item $req->cookie
186
187 A convenient method to access $req->cookies.
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 ) {
198         return keys %{ $self->cookies };
199     }
200
201     if ( @_ == 1 ) {
202
203         my $name = shift;
204
205         unless ( exists $self->cookies->{$name} ) {
206             return undef;
207         }
208
209         return $self->cookies->{$name};
210     }
211 }
212
213 =item $req->cookies
214
215 Returns a reference to a hash containing the cookies.
216
217     print $c->request->cookies->{mycookie}->value;
218
219 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
220 objects.
221
222 =item $req->header
223
224 Shortcut for $req->headers->header.
225
226 =item $req->headers
227
228 Returns an L<HTTP::Headers> object containing the headers for the current request.
229
230     print $c->request->headers->header('X-Catalyst');
231
232 =item $req->hostname
233
234 Returns the hostname of the client.
235     
236 =cut
237
238 sub hostname {
239     my $self = shift;
240
241     if ( @_ == 0 && not $self->{hostname} ) {
242         $self->{hostname} =
243           gethostbyaddr( inet_aton( $self->address ), AF_INET );
244     }
245
246     if ( @_ == 1 ) {
247         $self->{hostname} = shift;
248     }
249
250     return $self->{hostname};
251 }
252
253 =item $req->input
254
255 Alias for $req->body.
256
257 =item $req->match
258
259 This contains the matching part of a Regex action. Otherwise
260 it returns the same as 'action'.
261
262 =item $req->method
263
264 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
265
266 =item $req->param
267
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.
270
271     $value  = $c->request->param( 'foo' );
272     @values = $c->request->param( 'foo' );
273     @params = $c->request->param;
274
275 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
276 arguments to this method, like this:
277
278         $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
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>
282 (creating it if it didn't exist before), and C<quxx> as another value for
283 C<gorch>.
284
285 =cut
286
287 sub param {
288     my $self = shift;
289
290     if ( @_ == 0 ) {
291         return keys %{ $self->parameters };
292     }
293
294     if ( @_ == 1 ) {
295
296         my $param = shift;
297
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         }
312     }
313     elsif ( @_ > 1 ) {
314         my $field = shift;
315         $self->parameters->{$field} = [@_];
316     }
317 }
318
319 =item $req->parameters
320
321 Returns a reference to a hash containing GET and POST parameters. Values can
322 be either a scalar or an arrayref containing scalars.
323
324     print $c->request->parameters->{field};
325     print $c->request->parameters->{field}->[0];
326
327 This is the combination of C<query_parameters> and C<body_parameters>.
328
329 =item $req->params
330
331 Shortcut for $req->parameters.
332
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
342 =item $req->path
343
344 Returns the path, i.e. the part of the URI after $req->base, for the current request.
345
346 =item $req->path_info
347
348 Alias for path, added for compability with L<CGI>.
349
350 =cut
351
352 sub path {
353     my ( $self, $params ) = @_;
354
355     if ($params) {
356         $self->uri->path($params);
357     }
358     else {
359         return $self->{path} if $self->{path};
360     }
361
362     my $path     = $self->uri->path;
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/^\///;
367     $self->{path} = $path;
368
369     return $path;
370 }
371
372 =item $req->protocol
373
374 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
375
376 =item $req->query_parameters
377
378 Returns a reference to a hash containing query string (GET) parameters. Values can
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
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
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
396 =item $req->referer
397
398 Shortcut for $req->headers->referer. Returns the referring page.
399
400 =item $req->secure
401
402 Returns true or false, indicating whether the connection is secure (https).
403
404 =item $req->snippets
405
406 Returns a reference to an array containing regex snippets.
407
408     my @snippets = @{ $c->request->snippets };
409
410 =item $req->upload
411
412 A convenient method to access $req->uploads.
413
414     $upload  = $c->request->upload('field');
415     @uploads = $c->request->upload('field');
416     @fields  = $c->request->upload;
417
418     for my $upload ( $c->request->upload('field') ) {
419         print $upload->filename;
420     }
421
422 =cut
423
424 sub upload {
425     my $self = shift;
426
427     if ( @_ == 0 ) {
428         return keys %{ $self->uploads };
429     }
430
431     if ( @_ == 1 ) {
432
433         my $upload = shift;
434
435         unless ( exists $self->uploads->{$upload} ) {
436             return wantarray ? () : undef;
437         }
438
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)
446               ? ( $self->uploads->{$upload} )
447               : $self->uploads->{$upload};
448         }
449     }
450
451     if ( @_ > 1 ) {
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         }
465     }
466 }
467
468 =item $req->uploads
469
470 Returns a reference to a hash containing uploads. Values can be either a
471 hashref or a arrayref containing L<Catalyst::Request::Upload> objects.
472
473     my $upload = $c->request->uploads->{field};
474     my $upload = $c->request->uploads->{field}->[0];
475
476 =cut
477
478 sub uploads {
479     my ( $self, $uploads ) = @_;
480     $self->{_context}->prepare_body;
481     $self->{uploads} = $uploads if $uploads;
482     return $self->{uploads};
483 }
484
485 =item $req->uri
486
487 Returns a URI object for the current request. Stringifies to the URI text.
488
489 =item $req->user
490
491 Returns the currently logged in user. Deprecated. The method recommended for
492 newer plugins is $c->user.
493
494 =item $req->user_agent
495
496 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
497 version string.
498
499 =back
500
501 =head1 AUTHORS
502
503 Sebastian Riedel, C<sri@cpan.org>
504
505 Marcus Ramberg, C<mramberg@cpan.org>
506
507 =head1 COPYRIGHT
508
509 This program is free software, you can redistribute it and/or modify
510 it under the same terms as Perl itself.
511
512 =cut
513
514 1;