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