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