1 package Catalyst::Request;
4 use base 'Class::Accessor::Fast';
6 use IO::Socket qw[AF_INET inet_aton];
11 __PACKAGE__->mk_accessors(
12 qw/action address arguments cookies headers match method
13 protocol query_parameters secure captures uri user/
17 *body_params = \&body_parameters;
19 *params = \¶meters;
20 *query_params = \&query_parameters;
22 *snippets = \&captures;
24 sub content_encoding { shift->headers->content_encoding(@_) }
25 sub content_length { shift->headers->content_length(@_) }
26 sub content_type { shift->headers->content_type(@_) }
27 sub header { shift->headers->header(@_) }
28 sub referer { shift->headers->referer(@_) }
29 sub user_agent { shift->headers->user_agent(@_) }
33 Catalyst::Request - provides information about the current client request
44 $req->body_parameters;
45 $req->content_encoding;
61 $req->query_parameters;
65 $req->captures; # previously knows as snippets
72 See also L<Catalyst>, L<Catalyst::Request::Upload>.
76 This is the Catalyst Request class, which provides an interface to data for the
77 current client request. The request object is prepared by L<Catalyst::Engine>,
78 thus hiding the details of the particular engine implementation.
84 [DEPRECATED] Returns the name of the requested action.
87 Use C<< $c->action >> instead (which returns a
88 L<Catalyst::Action|Catalyst::Action> object).
92 Returns the IP address of the client.
94 =head2 $req->arguments
96 Returns a reference to an array containing the arguments.
98 print $c->request->arguments->[0];
100 For example, if your action was
102 package MyApp::C::Foo;
108 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
109 would be the first and only argument.
113 Shortcut for arguments.
117 Contains the URI base. This will always have a trailing slash.
119 If your application was queried with the URI
120 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
125 my ( $self, $base ) = @_;
127 return $self->{base} unless $base;
129 $self->{base} = $base;
131 # set the value in path for backwards-compat
136 return $self->{base};
141 Returns the message body of the request, unless Content-Type is
142 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
147 my ( $self, $body ) = @_;
148 $self->{_context}->prepare_body;
149 $self->{_body}->body($body) if $body;
150 return $self->{_body}->body;
153 =head2 $req->body_parameters
155 Returns a reference to a hash containing body (POST) parameters. Values can
156 be either a scalar or an arrayref containing scalars.
158 print $c->request->body_parameters->{field};
159 print $c->request->body_parameters->{field}->[0];
161 These are the parameters from the POST part of the request, if any.
163 =head2 $req->body_params
165 Shortcut for body_parameters.
169 sub body_parameters {
170 my ( $self, $params ) = @_;
171 $self->{_context}->prepare_body;
172 $self->{body_parameters} = $params if $params;
173 return $self->{body_parameters};
176 =head2 $req->content_encoding
178 Shortcut for $req->headers->content_encoding.
180 =head2 $req->content_length
182 Shortcut for $req->headers->content_length.
184 =head2 $req->content_type
186 Shortcut for $req->headers->content_type.
190 A convenient method to access $req->cookies.
192 $cookie = $c->request->cookie('name');
193 @cookies = $c->request->cookie;
201 return keys %{ $self->cookies };
208 unless ( exists $self->cookies->{$name} ) {
212 return $self->cookies->{$name};
218 Returns a reference to a hash containing the cookies.
220 print $c->request->cookies->{mycookie}->value;
222 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
227 Shortcut for $req->headers->header.
231 Returns an L<HTTP::Headers> object containing the headers for the current request.
233 print $c->request->headers->header('X-Catalyst');
235 =head2 $req->hostname
237 Returns the hostname of the client.
244 if ( @_ == 0 && not $self->{hostname} ) {
246 gethostbyaddr( inet_aton( $self->address ), AF_INET );
250 $self->{hostname} = shift;
253 return $self->{hostname};
258 Alias for $req->body.
262 This contains the matching part of a Regex action. Otherwise
263 it returns the same as 'action', except for default actions,
264 which return an empty string.
268 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
272 Returns GET and POST parameters with a CGI.pm-compatible param method. This
273 is an alternative method for accessing parameters in $c->req->parameters.
275 $value = $c->request->param( 'foo' );
276 @values = $c->request->param( 'foo' );
277 @params = $c->request->param;
279 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
280 arguments to this method, like this:
282 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
284 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
285 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
286 (creating it if it didn't exist before), and C<quxx> as another value for
295 return keys %{ $self->parameters };
302 unless ( exists $self->parameters->{$param} ) {
303 return wantarray ? () : undef;
306 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
308 ? @{ $self->parameters->{$param} }
309 : $self->parameters->{$param}->[0];
313 ? ( $self->parameters->{$param} )
314 : $self->parameters->{$param};
319 $self->parameters->{$field} = [@_];
323 =head2 $req->parameters
325 Returns a reference to a hash containing GET and POST parameters. Values can
326 be either a scalar or an arrayref containing scalars.
328 print $c->request->parameters->{field};
329 print $c->request->parameters->{field}->[0];
331 This is the combination of C<query_parameters> and C<body_parameters>.
335 Shortcut for $req->parameters.
340 my ( $self, $params ) = @_;
341 $self->{_context}->prepare_body;
344 $self->{parameters} = $params;
347 $self->{_context}->log->warn(
348 "Attempt to retrieve '$params' with req->params(), " .
349 "you probably meant to call req->param('$params')" );
352 return $self->{parameters};
357 Returns the path, i.e. the part of the URI after $req->base, for the current request.
359 =head2 $req->path_info
361 Alias for path, added for compability with L<CGI>.
366 my ( $self, $params ) = @_;
369 $self->uri->path($params);
372 return $self->{path} if $self->{path};
375 my $path = $self->uri->path;
376 my $location = $self->base->path;
377 $path =~ s/^(\Q$location\E)?//;
379 $self->{path} = $path;
384 =head2 $req->protocol
386 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
388 =head2 $req->query_parameters
390 Returns a reference to a hash containing query string (GET) parameters. Values can
391 be either a scalar or an arrayref containing scalars.
393 print $c->request->query_parameters->{field};
394 print $c->request->query_parameters->{field}->[0];
396 =head2 $req->read( [$maxlength] )
398 Reads a chunk of data from the request body. This method is intended to be
399 used in a while loop, reading $maxlength bytes on every call. $maxlength
400 defaults to the size of the request if not specified.
402 You have to set MyApp->config->{parse_on_demand} to use this directly.
406 sub read { shift->{_context}->read(@_); }
410 Shortcut for $req->headers->referer. Returns the referring page.
414 Returns true or false, indicating whether the connection is secure (https).
416 =head2 $req->captures
418 Returns a reference to an array containing regex captures.
420 my @captures = @{ $c->request->captures };
422 =head2 $req->snippets
424 C<captures> used to be called snippets. This is still available for backwoards
425 compatibility, but is considered deprecated.
429 A convenient method to access $req->uploads.
431 $upload = $c->request->upload('field');
432 @uploads = $c->request->upload('field');
433 @fields = $c->request->upload;
435 for my $upload ( $c->request->upload('field') ) {
436 print $upload->filename;
445 return keys %{ $self->uploads };
452 unless ( exists $self->uploads->{$upload} ) {
453 return wantarray ? () : undef;
456 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
458 ? @{ $self->uploads->{$upload} }
459 : $self->uploads->{$upload}->[0];
463 ? ( $self->uploads->{$upload} )
464 : $self->uploads->{$upload};
470 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
472 if ( exists $self->uploads->{$field} ) {
473 for ( $self->uploads->{$field} ) {
474 $_ = [$_] unless ref($_) eq "ARRAY";
475 push( @$_, $upload );
479 $self->uploads->{$field} = $upload;
487 Returns a reference to a hash containing uploads. Values can be either a
488 L<Catalyst::Request::Upload> object, or an arrayref of
489 L<Catalyst::Request::Upload> objects.
491 my $upload = $c->request->uploads->{field};
492 my $upload = $c->request->uploads->{field}->[0];
497 my ( $self, $uploads ) = @_;
498 $self->{_context}->prepare_body;
499 $self->{uploads} = $uploads if $uploads;
500 return $self->{uploads};
505 Returns a URI object for the current request. Stringifies to the URI text.
507 =head2 $req->uri_with( { key => 'value' } );
509 Returns a rewritten URI object for the current request. Key/value pairs
510 passed in will override existing parameters. Unmodified pairs will be
516 my( $self, $args ) = @_;
518 carp( 'No arguments passed to uri_with()' ) unless $args;
520 for my $value ( values %$args ) {
521 next unless defined $value;
522 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
528 my $uri = $self->uri->clone;
531 %{ $uri->query_form_hash },
539 Returns the currently logged in user. Deprecated. The method recommended for
540 newer plugins is $c->user.
542 =head2 $req->user_agent
544 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
549 Sebastian Riedel, C<sri@cpan.org>
551 Marcus Ramberg, C<mramberg@cpan.org>
555 This program is free software, you can redistribute it and/or modify
556 it under the same terms as Perl itself.