tabs => spaces
[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(
94b8f5de 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
85d9fce6 102 package MyApp::C::Foo;
103
104 sub moose : Local {
105 ...
106 }
c436c1e8 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 {
e4592f86 147 my $self = shift;
fbcc39ad 148 $self->{_context}->prepare_body;
847e3257 149
150 return unless $self->{_body};
151
fbcc39ad 152 return $self->{_body}->body;
153}
154
b5ecfcf0 155=head2 $req->body_parameters
fbcc39ad 156
3e19f4f6 157Returns a reference to a hash containing body (POST) parameters. Values can
fbcc39ad 158be either a scalar or an arrayref containing scalars.
159
160 print $c->request->body_parameters->{field};
161 print $c->request->body_parameters->{field}->[0];
c436c1e8 162
d631b5f9 163These are the parameters from the POST part of the request, if any.
fbcc39ad 164
b5ecfcf0 165=head2 $req->body_params
fbcc39ad 166
3e19f4f6 167Shortcut for body_parameters.
fbcc39ad 168
169=cut
170
171sub body_parameters {
172 my ( $self, $params ) = @_;
173 $self->{_context}->prepare_body;
174 $self->{body_parameters} = $params if $params;
175 return $self->{body_parameters};
176}
177
b5ecfcf0 178=head2 $req->content_encoding
b5176d9e 179
3e19f4f6 180Shortcut for $req->headers->content_encoding.
b5176d9e 181
b5ecfcf0 182=head2 $req->content_length
b5176d9e 183
3e19f4f6 184Shortcut for $req->headers->content_length.
b5176d9e 185
b5ecfcf0 186=head2 $req->content_type
b5176d9e 187
3e19f4f6 188Shortcut for $req->headers->content_type.
b5176d9e 189
b5ecfcf0 190=head2 $req->cookie
3ad654e0 191
3e19f4f6 192A convenient method to access $req->cookies.
3ad654e0 193
194 $cookie = $c->request->cookie('name');
195 @cookies = $c->request->cookie;
196
197=cut
198
199sub cookie {
200 my $self = shift;
201
202 if ( @_ == 0 ) {
b77e7869 203 return keys %{ $self->cookies };
3ad654e0 204 }
205
206 if ( @_ == 1 ) {
207
208 my $name = shift;
209
b77e7869 210 unless ( exists $self->cookies->{$name} ) {
3ad654e0 211 return undef;
212 }
fbcc39ad 213
b77e7869 214 return $self->cookies->{$name};
3ad654e0 215 }
216}
217
b5ecfcf0 218=head2 $req->cookies
fc7ec1d9 219
b22c6668 220Returns a reference to a hash containing the cookies.
fc7ec1d9 221
222 print $c->request->cookies->{mycookie}->value;
223
3e19f4f6 224The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
c436c1e8 225objects.
226
b5ecfcf0 227=head2 $req->header
b5176d9e 228
3e19f4f6 229Shortcut for $req->headers->header.
b5176d9e 230
b5ecfcf0 231=head2 $req->headers
fc7ec1d9 232
3e19f4f6 233Returns an L<HTTP::Headers> object containing the headers for the current request.
fc7ec1d9 234
235 print $c->request->headers->header('X-Catalyst');
236
b5ecfcf0 237=head2 $req->hostname
0556eb49 238
3e19f4f6 239Returns the hostname of the client.
b4ca0ee8 240
241=cut
242
243sub hostname {
244 my $self = shift;
245
a268a011 246 if ( @_ == 0 && not $self->{hostname} ) {
fbcc39ad 247 $self->{hostname} =
248 gethostbyaddr( inet_aton( $self->address ), AF_INET );
b4ca0ee8 249 }
250
a268a011 251 if ( @_ == 1 ) {
252 $self->{hostname} = shift;
b4ca0ee8 253 }
254
255 return $self->{hostname};
256}
0556eb49 257
b5ecfcf0 258=head2 $req->input
61bacdcc 259
3e19f4f6 260Alias for $req->body.
61bacdcc 261
b5ecfcf0 262=head2 $req->match
fc7ec1d9 263
3e19f4f6 264This contains the matching part of a Regex action. Otherwise
2c83fd5a 265it returns the same as 'action', except for default actions,
266which return an empty string.
fc7ec1d9 267
b5ecfcf0 268=head2 $req->method
b5176d9e 269
270Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
271
b5ecfcf0 272=head2 $req->param
e7c0c583 273
3e19f4f6 274Returns GET and POST parameters with a CGI.pm-compatible param method. This
275is an alternative method for accessing parameters in $c->req->parameters.
e7c0c583 276
a82c2894 277 $value = $c->request->param( 'foo' );
278 @values = $c->request->param( 'foo' );
e7c0c583 279 @params = $c->request->param;
280
3e705254 281Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
a82c2894 282arguments to this method, like this:
283
85d9fce6 284 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
a82c2894 285
286will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
287C<quxx>. Previously this would have added C<bar> as another value to C<foo>
3e19f4f6 288(creating it if it didn't exist before), and C<quxx> as another value for
289C<gorch>.
a82c2894 290
e7c0c583 291=cut
292
293sub param {
294 my $self = shift;
295
296 if ( @_ == 0 ) {
297 return keys %{ $self->parameters };
298 }
299
bfde09a2 300 if ( @_ == 1 ) {
e7c0c583 301
bfde09a2 302 my $param = shift;
6bd2b72c 303
bfde09a2 304 unless ( exists $self->parameters->{$param} ) {
305 return wantarray ? () : undef;
306 }
307
308 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
309 return (wantarray)
310 ? @{ $self->parameters->{$param} }
311 : $self->parameters->{$param}->[0];
312 }
313 else {
314 return (wantarray)
315 ? ( $self->parameters->{$param} )
316 : $self->parameters->{$param};
317 }
d7945f32 318 }
a82c2894 319 elsif ( @_ > 1 ) {
320 my $field = shift;
321 $self->parameters->{$field} = [@_];
d7945f32 322 }
e7c0c583 323}
b5176d9e 324
b5ecfcf0 325=head2 $req->parameters
61b1e958 326
3e19f4f6 327Returns a reference to a hash containing GET and POST parameters. Values can
d08ced28 328be either a scalar or an arrayref containing scalars.
fc7ec1d9 329
e7c0c583 330 print $c->request->parameters->{field};
331 print $c->request->parameters->{field}->[0];
fc7ec1d9 332
c436c1e8 333This is the combination of C<query_parameters> and C<body_parameters>.
334
b5ecfcf0 335=head2 $req->params
3e19f4f6 336
337Shortcut for $req->parameters.
338
fbcc39ad 339=cut
340
341sub parameters {
342 my ( $self, $params ) = @_;
343 $self->{_context}->prepare_body;
ee26f4bd 344 if ( $params ) {
345 if ( ref $params ) {
346 $self->{parameters} = $params;
347 }
348 else {
349 $self->{_context}->log->warn(
350 "Attempt to retrieve '$params' with req->params(), " .
351 "you probably meant to call req->param('$params')" );
352 }
353 }
fbcc39ad 354 return $self->{parameters};
355}
356
b5ecfcf0 357=head2 $req->path
fc7ec1d9 358
3e19f4f6 359Returns the path, i.e. the part of the URI after $req->base, for the current request.
fc7ec1d9 360
b5ecfcf0 361=head2 $req->path_info
fbcc39ad 362
3e19f4f6 363Alias for path, added for compability with L<CGI>.
fbcc39ad 364
365=cut
366
367sub path {
02fb5d78 368 my ( $self, @params ) = @_;
4f5ebacd 369
02fb5d78 370 if (@params) {
371 $self->uri->path(@params);
372 undef $self->{path};
fbcc39ad 373 }
02fb5d78 374 elsif ( defined( my $path = $self->{path} ) ) {
375 return $path;
e561386f 376 }
02fb5d78 377 else {
378 my $path = $self->uri->path;
379 my $location = $self->base->path;
380 $path =~ s/^(\Q$location\E)?//;
381 $path =~ s/^\///;
382 $self->{path} = $path;
fbcc39ad 383
02fb5d78 384 return $path;
385 }
fbcc39ad 386}
387
b5ecfcf0 388=head2 $req->protocol
bfde09a2 389
3e19f4f6 390Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
bfde09a2 391
b5ecfcf0 392=head2 $req->query_parameters
fbcc39ad 393
3e19f4f6 394Returns a reference to a hash containing query string (GET) parameters. Values can
fbcc39ad 395be either a scalar or an arrayref containing scalars.
396
397 print $c->request->query_parameters->{field};
398 print $c->request->query_parameters->{field}->[0];
399
b5ecfcf0 400=head2 $req->read( [$maxlength] )
fbcc39ad 401
3e19f4f6 402Reads a chunk of data from the request body. This method is intended to be
403used in a while loop, reading $maxlength bytes on every call. $maxlength
fbcc39ad 404defaults to the size of the request if not specified.
405
406You have to set MyApp->config->{parse_on_demand} to use this directly.
407
408=cut
409
410sub read { shift->{_context}->read(@_); }
411
b5ecfcf0 412=head2 $req->referer
fc7ec1d9 413
3e19f4f6 414Shortcut for $req->headers->referer. Returns the referring page.
fc7ec1d9 415
b5ecfcf0 416=head2 $req->secure
bfde09a2 417
3e19f4f6 418Returns true or false, indicating whether the connection is secure (https).
bfde09a2 419
2982e768 420=head2 $req->captures
421
422Returns a reference to an array containing regex captures.
fc7ec1d9 423
2982e768 424 my @captures = @{ $c->request->captures };
425
426=head2 $req->snippets
fc7ec1d9 427
2982e768 428C<captures> used to be called snippets. This is still available for backwoards
429compatibility, but is considered deprecated.
fc7ec1d9 430
b5ecfcf0 431=head2 $req->upload
e7c0c583 432
3e19f4f6 433A convenient method to access $req->uploads.
e7c0c583 434
435 $upload = $c->request->upload('field');
436 @uploads = $c->request->upload('field');
437 @fields = $c->request->upload;
bfde09a2 438
e7c0c583 439 for my $upload ( $c->request->upload('field') ) {
146554c5 440 print $upload->filename;
e7c0c583 441 }
442
443=cut
444
445sub upload {
446 my $self = shift;
447
448 if ( @_ == 0 ) {
449 return keys %{ $self->uploads };
450 }
451
bfde09a2 452 if ( @_ == 1 ) {
e7c0c583 453
bfde09a2 454 my $upload = shift;
455
456 unless ( exists $self->uploads->{$upload} ) {
457 return wantarray ? () : undef;
458 }
6bd2b72c 459
bfde09a2 460 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
461 return (wantarray)
462 ? @{ $self->uploads->{$upload} }
463 : $self->uploads->{$upload}->[0];
464 }
465 else {
466 return (wantarray)
fbcc39ad 467 ? ( $self->uploads->{$upload} )
468 : $self->uploads->{$upload};
bfde09a2 469 }
d7945f32 470 }
bfde09a2 471
a4f5c51e 472 if ( @_ > 1 ) {
bfde09a2 473
474 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
475
476 if ( exists $self->uploads->{$field} ) {
477 for ( $self->uploads->{$field} ) {
478 $_ = [$_] unless ref($_) eq "ARRAY";
479 push( @$_, $upload );
480 }
481 }
482 else {
483 $self->uploads->{$field} = $upload;
484 }
485 }
e7c0c583 486 }
487}
488
b5ecfcf0 489=head2 $req->uploads
fc7ec1d9 490
bfde09a2 491Returns a reference to a hash containing uploads. Values can be either a
84e7aa89 492L<Catalyst::Request::Upload> object, or an arrayref of
493L<Catalyst::Request::Upload> objects.
e7c0c583 494
495 my $upload = $c->request->uploads->{field};
496 my $upload = $c->request->uploads->{field}->[0];
497
77d12cae 498=cut
499
fbcc39ad 500sub uploads {
501 my ( $self, $uploads ) = @_;
502 $self->{_context}->prepare_body;
503 $self->{uploads} = $uploads if $uploads;
504 return $self->{uploads};
77d12cae 505}
506
b5ecfcf0 507=head2 $req->uri
fbcc39ad 508
3e19f4f6 509Returns a URI object for the current request. Stringifies to the URI text.
fbcc39ad 510
bd917b94 511=head2 $req->uri_with( { key => 'value' } );
512
3338e8ce 513Returns a rewritten URI object for the current request. Key/value pairs
514passed in will override existing parameters. Unmodified pairs will be
515preserved.
bd917b94 516
517=cut
518
519sub uri_with {
520 my( $self, $args ) = @_;
521
522 carp( 'No arguments passed to uri_with()' ) unless $args;
fbb513f7 523
fc42a730 524 for my $value ( values %$args ) {
d0f0fcf6 525 next unless defined $value;
fbb513f7 526 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
527 $_ = "$_";
528 utf8::encode( $_ );
fc42a730 529 }
fc42a730 530 };
fbb513f7 531
bd917b94 532 my $uri = $self->uri->clone;
533
534 $uri->query_form( {
27d9619a 535 %{ $uri->query_form_hash },
bd917b94 536 %$args
537 } );
538 return $uri;
539}
540
b5ecfcf0 541=head2 $req->user
7ce7ca2e 542
3e19f4f6 543Returns the currently logged in user. Deprecated. The method recommended for
544newer plugins is $c->user.
7ce7ca2e 545
b5ecfcf0 546=head2 $req->user_agent
b5176d9e 547
3e19f4f6 548Shortcut to $req->headers->user_agent. Returns the user agent (browser)
549version string.
b5176d9e 550
3e19f4f6 551=head1 AUTHORS
fc7ec1d9 552
553Sebastian Riedel, C<sri@cpan.org>
3e19f4f6 554
61b1e958 555Marcus Ramberg, C<mramberg@cpan.org>
fc7ec1d9 556
557=head1 COPYRIGHT
558
e7c0c583 559This program is free software, you can redistribute it and/or modify
61b1e958 560it under the same terms as Perl itself.
fc7ec1d9 561
562=cut
563
5641;