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