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