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