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