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