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