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 keywords 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;
62 $req->query_parameters;
66 $req->captures; # previously knows as snippets
73 See also L<Catalyst>, L<Catalyst::Request::Upload>.
77 This is the Catalyst Request class, which provides an interface to data for the
78 current client request. The request object is prepared by L<Catalyst::Engine>,
79 thus hiding the details of the particular engine implementation.
85 [DEPRECATED] Returns the name of the requested action.
88 Use C<< $c->action >> instead (which returns a
89 L<Catalyst::Action|Catalyst::Action> object).
93 Returns the IP address of the client.
95 =head2 $req->arguments
97 Returns a reference to an array containing the arguments.
99 print $c->request->arguments->[0];
101 For example, if your action was
103 package MyApp::C::Foo;
109 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
110 would be the first and only argument.
114 Shortcut for arguments.
118 Contains the URI base. This will always have a trailing slash.
120 If your application was queried with the URI
121 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
126 my ( $self, $base ) = @_;
128 return $self->{base} unless $base;
130 $self->{base} = $base;
132 # set the value in path for backwards-compat
137 return $self->{base};
142 Returns the message body of the request, unless Content-Type is
143 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
149 $self->{_context}->prepare_body;
151 return unless $self->{_body};
153 return $self->{_body}->body;
156 =head2 $req->body_parameters
158 Returns a reference to a hash containing body (POST) parameters. Values can
159 be either a scalar or an arrayref containing scalars.
161 print $c->request->body_parameters->{field};
162 print $c->request->body_parameters->{field}->[0];
164 These are the parameters from the POST part of the request, if any.
166 =head2 $req->body_params
168 Shortcut for body_parameters.
172 sub body_parameters {
173 my ( $self, $params ) = @_;
174 $self->{_context}->prepare_body;
175 $self->{body_parameters} = $params if $params;
176 return $self->{body_parameters};
179 =head2 $req->content_encoding
181 Shortcut for $req->headers->content_encoding.
183 =head2 $req->content_length
185 Shortcut for $req->headers->content_length.
187 =head2 $req->content_type
189 Shortcut for $req->headers->content_type.
193 A convenient method to access $req->cookies.
195 $cookie = $c->request->cookie('name');
196 @cookies = $c->request->cookie;
204 return keys %{ $self->cookies };
211 unless ( exists $self->cookies->{$name} ) {
215 return $self->cookies->{$name};
221 Returns a reference to a hash containing the cookies.
223 print $c->request->cookies->{mycookie}->value;
225 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
230 Shortcut for $req->headers->header.
234 Returns an L<HTTP::Headers> object containing the headers for the current request.
236 print $c->request->headers->header('X-Catalyst');
238 =head2 $req->hostname
240 Returns the hostname of the client.
247 if ( @_ == 0 && not $self->{hostname} ) {
249 gethostbyaddr( inet_aton( $self->address ), AF_INET );
253 $self->{hostname} = shift;
256 return $self->{hostname};
261 Alias for $req->body.
263 =head2 $req->keywords
265 Contains the keywords portion of a query string, when no '=' signs are
268 http://localhost/path?some+keywords
270 $c->request->keywords will contain 'some keywords'
274 This contains the matching part of a Regex action. Otherwise
275 it returns the same as 'action', except for default actions,
276 which return an empty string.
280 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
284 Returns GET and POST parameters with a CGI.pm-compatible param method. This
285 is an alternative method for accessing parameters in $c->req->parameters.
287 $value = $c->request->param( 'foo' );
288 @values = $c->request->param( 'foo' );
289 @params = $c->request->param;
291 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
292 arguments to this method, like this:
294 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
296 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
297 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
298 (creating it if it didn't exist before), and C<quxx> as another value for
307 return keys %{ $self->parameters };
314 unless ( exists $self->parameters->{$param} ) {
315 return wantarray ? () : undef;
318 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
320 ? @{ $self->parameters->{$param} }
321 : $self->parameters->{$param}->[0];
325 ? ( $self->parameters->{$param} )
326 : $self->parameters->{$param};
331 $self->parameters->{$field} = [@_];
335 =head2 $req->parameters
337 Returns a reference to a hash containing GET and POST parameters. Values can
338 be either a scalar or an arrayref containing scalars.
340 print $c->request->parameters->{field};
341 print $c->request->parameters->{field}->[0];
343 This is the combination of C<query_parameters> and C<body_parameters>.
347 Shortcut for $req->parameters.
352 my ( $self, $params ) = @_;
353 $self->{_context}->prepare_body;
356 $self->{parameters} = $params;
359 $self->{_context}->log->warn(
360 "Attempt to retrieve '$params' with req->params(), " .
361 "you probably meant to call req->param('$params')" );
364 return $self->{parameters};
369 Returns the path, i.e. the part of the URI after $req->base, for the current request.
371 =head2 $req->path_info
373 Alias for path, added for compability with L<CGI>.
378 my ( $self, $params ) = @_;
381 $self->uri->path($params);
384 return $self->{path} if $self->{path};
387 my $path = $self->uri->path;
388 my $location = $self->base->path;
389 $path =~ s/^(\Q$location\E)?//;
391 $self->{path} = $path;
396 =head2 $req->protocol
398 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
400 =head2 $req->query_parameters
402 Returns a reference to a hash containing query string (GET) parameters. Values can
403 be either a scalar or an arrayref containing scalars.
405 print $c->request->query_parameters->{field};
406 print $c->request->query_parameters->{field}->[0];
408 =head2 $req->read( [$maxlength] )
410 Reads a chunk of data from the request body. This method is intended to be
411 used in a while loop, reading $maxlength bytes on every call. $maxlength
412 defaults to the size of the request if not specified.
414 You have to set MyApp->config->{parse_on_demand} to use this directly.
418 sub read { shift->{_context}->read(@_); }
422 Shortcut for $req->headers->referer. Returns the referring page.
426 Returns true or false, indicating whether the connection is secure (https).
428 =head2 $req->captures
430 Returns a reference to an array containing regex captures.
432 my @captures = @{ $c->request->captures };
434 =head2 $req->snippets
436 C<captures> used to be called snippets. This is still available for backwoards
437 compatibility, but is considered deprecated.
441 A convenient method to access $req->uploads.
443 $upload = $c->request->upload('field');
444 @uploads = $c->request->upload('field');
445 @fields = $c->request->upload;
447 for my $upload ( $c->request->upload('field') ) {
448 print $upload->filename;
457 return keys %{ $self->uploads };
464 unless ( exists $self->uploads->{$upload} ) {
465 return wantarray ? () : undef;
468 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
470 ? @{ $self->uploads->{$upload} }
471 : $self->uploads->{$upload}->[0];
475 ? ( $self->uploads->{$upload} )
476 : $self->uploads->{$upload};
482 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
484 if ( exists $self->uploads->{$field} ) {
485 for ( $self->uploads->{$field} ) {
486 $_ = [$_] unless ref($_) eq "ARRAY";
487 push( @$_, $upload );
491 $self->uploads->{$field} = $upload;
499 Returns a reference to a hash containing uploads. Values can be either a
500 L<Catalyst::Request::Upload> object, or an arrayref of
501 L<Catalyst::Request::Upload> objects.
503 my $upload = $c->request->uploads->{field};
504 my $upload = $c->request->uploads->{field}->[0];
509 my ( $self, $uploads ) = @_;
510 $self->{_context}->prepare_body;
511 $self->{uploads} = $uploads if $uploads;
512 return $self->{uploads};
517 Returns a URI object for the current request. Stringifies to the URI text.
519 =head2 $req->uri_with( { key => 'value' } );
521 Returns a rewritten URI object for the current request. Key/value pairs
522 passed in will override existing parameters. Unmodified pairs will be
528 my( $self, $args ) = @_;
530 carp( 'No arguments passed to uri_with()' ) unless $args;
532 for my $value ( values %$args ) {
533 next unless defined $value;
534 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
540 my $uri = $self->uri->clone;
543 %{ $uri->query_form_hash },
551 Returns the currently logged in user. Deprecated. The method recommended for
552 newer plugins is $c->user.
554 =head2 $req->user_agent
556 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
561 Sebastian Riedel, C<sri@cpan.org>
563 Marcus Ramberg, C<mramberg@cpan.org>
567 This program is free software, you can redistribute it and/or modify
568 it under the same terms as Perl itself.