Added req->query_keywords method
[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(
3b4d1251 12 qw/action address arguments cookies headers query_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;
3b4d1251 54 $req->query_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
85d9fce6 103 package MyApp::C::Foo;
104
105 sub moose : Local {
106 ...
107 }
c436c1e8 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
3b4d1251 263=head2 $req->query_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->query_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
85d9fce6 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 {
02fb5d78 378 my ( $self, @params ) = @_;
4f5ebacd 379
02fb5d78 380 if (@params) {
381 $self->uri->path(@params);
382 undef $self->{path};
fbcc39ad 383 }
02fb5d78 384 elsif ( defined( my $path = $self->{path} ) ) {
385 return $path;
e561386f 386 }
02fb5d78 387 else {
388 my $path = $self->uri->path;
389 my $location = $self->base->path;
390 $path =~ s/^(\Q$location\E)?//;
391 $path =~ s/^\///;
392 $self->{path} = $path;
fbcc39ad 393
02fb5d78 394 return $path;
395 }
fbcc39ad 396}
397
b5ecfcf0 398=head2 $req->protocol
bfde09a2 399
3e19f4f6 400Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
bfde09a2 401
b5ecfcf0 402=head2 $req->query_parameters
fbcc39ad 403
3e19f4f6 404Returns a reference to a hash containing query string (GET) parameters. Values can
fbcc39ad 405be either a scalar or an arrayref containing scalars.
406
407 print $c->request->query_parameters->{field};
408 print $c->request->query_parameters->{field}->[0];
409
b5ecfcf0 410=head2 $req->read( [$maxlength] )
fbcc39ad 411
3e19f4f6 412Reads a chunk of data from the request body. This method is intended to be
413used in a while loop, reading $maxlength bytes on every call. $maxlength
fbcc39ad 414defaults to the size of the request if not specified.
415
416You have to set MyApp->config->{parse_on_demand} to use this directly.
417
418=cut
419
420sub read { shift->{_context}->read(@_); }
421
b5ecfcf0 422=head2 $req->referer
fc7ec1d9 423
3e19f4f6 424Shortcut for $req->headers->referer. Returns the referring page.
fc7ec1d9 425
b5ecfcf0 426=head2 $req->secure
bfde09a2 427
3e19f4f6 428Returns true or false, indicating whether the connection is secure (https).
bfde09a2 429
2982e768 430=head2 $req->captures
431
432Returns a reference to an array containing regex captures.
fc7ec1d9 433
2982e768 434 my @captures = @{ $c->request->captures };
435
436=head2 $req->snippets
fc7ec1d9 437
2982e768 438C<captures> used to be called snippets. This is still available for backwoards
439compatibility, but is considered deprecated.
fc7ec1d9 440
b5ecfcf0 441=head2 $req->upload
e7c0c583 442
3e19f4f6 443A convenient method to access $req->uploads.
e7c0c583 444
445 $upload = $c->request->upload('field');
446 @uploads = $c->request->upload('field');
447 @fields = $c->request->upload;
bfde09a2 448
e7c0c583 449 for my $upload ( $c->request->upload('field') ) {
146554c5 450 print $upload->filename;
e7c0c583 451 }
452
453=cut
454
455sub upload {
456 my $self = shift;
457
458 if ( @_ == 0 ) {
459 return keys %{ $self->uploads };
460 }
461
bfde09a2 462 if ( @_ == 1 ) {
e7c0c583 463
bfde09a2 464 my $upload = shift;
465
466 unless ( exists $self->uploads->{$upload} ) {
467 return wantarray ? () : undef;
468 }
6bd2b72c 469
bfde09a2 470 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
471 return (wantarray)
472 ? @{ $self->uploads->{$upload} }
473 : $self->uploads->{$upload}->[0];
474 }
475 else {
476 return (wantarray)
fbcc39ad 477 ? ( $self->uploads->{$upload} )
478 : $self->uploads->{$upload};
bfde09a2 479 }
d7945f32 480 }
bfde09a2 481
a4f5c51e 482 if ( @_ > 1 ) {
bfde09a2 483
484 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
485
486 if ( exists $self->uploads->{$field} ) {
487 for ( $self->uploads->{$field} ) {
488 $_ = [$_] unless ref($_) eq "ARRAY";
489 push( @$_, $upload );
490 }
491 }
492 else {
493 $self->uploads->{$field} = $upload;
494 }
495 }
e7c0c583 496 }
497}
498
b5ecfcf0 499=head2 $req->uploads
fc7ec1d9 500
bfde09a2 501Returns a reference to a hash containing uploads. Values can be either a
84e7aa89 502L<Catalyst::Request::Upload> object, or an arrayref of
503L<Catalyst::Request::Upload> objects.
e7c0c583 504
505 my $upload = $c->request->uploads->{field};
506 my $upload = $c->request->uploads->{field}->[0];
507
77d12cae 508=cut
509
fbcc39ad 510sub uploads {
511 my ( $self, $uploads ) = @_;
512 $self->{_context}->prepare_body;
513 $self->{uploads} = $uploads if $uploads;
514 return $self->{uploads};
77d12cae 515}
516
b5ecfcf0 517=head2 $req->uri
fbcc39ad 518
3e19f4f6 519Returns a URI object for the current request. Stringifies to the URI text.
fbcc39ad 520
bd917b94 521=head2 $req->uri_with( { key => 'value' } );
522
3338e8ce 523Returns a rewritten URI object for the current request. Key/value pairs
524passed in will override existing parameters. Unmodified pairs will be
525preserved.
bd917b94 526
527=cut
528
529sub uri_with {
530 my( $self, $args ) = @_;
531
532 carp( 'No arguments passed to uri_with()' ) unless $args;
fbb513f7 533
fc42a730 534 for my $value ( values %$args ) {
d0f0fcf6 535 next unless defined $value;
fbb513f7 536 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
537 $_ = "$_";
538 utf8::encode( $_ );
fc42a730 539 }
fc42a730 540 };
fbb513f7 541
bd917b94 542 my $uri = $self->uri->clone;
543
544 $uri->query_form( {
27d9619a 545 %{ $uri->query_form_hash },
bd917b94 546 %$args
547 } );
548 return $uri;
549}
550
b5ecfcf0 551=head2 $req->user
7ce7ca2e 552
3e19f4f6 553Returns the currently logged in user. Deprecated. The method recommended for
554newer plugins is $c->user.
7ce7ca2e 555
b5ecfcf0 556=head2 $req->user_agent
b5176d9e 557
3e19f4f6 558Shortcut to $req->headers->user_agent. Returns the user agent (browser)
559version string.
b5176d9e 560
3e19f4f6 561=head1 AUTHORS
fc7ec1d9 562
563Sebastian Riedel, C<sri@cpan.org>
3e19f4f6 564
61b1e958 565Marcus Ramberg, C<mramberg@cpan.org>
fc7ec1d9 566
567=head1 COPYRIGHT
568
e7c0c583 569This program is free software, you can redistribute it and/or modify
61b1e958 570it under the same terms as Perl itself.
fc7ec1d9 571
572=cut
573
5741;