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