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