1 package Catalyst::Request;
3 use IO::Socket qw[AF_INET inet_aton];
12 has action => (is => 'rw');
13 has address => (is => 'rw');
14 has arguments => (is => 'rw', default => sub { [] });
15 has cookies => (is => 'rw', default => sub { {} });
16 has query_keywords => (is => 'rw');
17 has match => (is => 'rw');
18 has method => (is => 'rw');
19 has protocol => (is => 'rw');
20 has query_parameters => (is => 'rw', default => sub { {} });
21 has secure => (is => 'rw', default => 0);
22 has captures => (is => 'rw', default => sub { [] });
23 has uri => (is => 'rw');
24 has user => (is => 'rw');
27 isa => 'HTTP::Headers',
28 handles => [qw(content_encoding content_length content_type header referer user_agent)],
36 has body_parameters => (
40 default => sub { {} },
43 before body_parameters => sub {
45 $self->_context->prepare_body();
52 default => sub { {} },
55 before uploads => sub {
57 $self->_context->prepare_body;
64 default => sub { {} },
67 before parameters => sub {
68 my ($self, $params) = @_;
69 $self->_context->prepare_body();
70 if ( $params && !ref $params ) {
71 $self->_context->log->warn(
72 "Attempt to retrieve '$params' with req->params(), " .
73 "you probably meant to call req->param('$params')" );
85 return $self->path if $self->uri;
95 $self->_context->prepare_body();
104 gethostbyaddr( inet_aton( $self->address ), AF_INET )
110 sub args { shift->arguments(@_) }
111 sub body_params { shift->body_parameters(@_) }
112 sub input { shift->body(@_) }
113 sub params { shift->parameters(@_) }
114 sub query_params { shift->query_parameters(@_) }
115 sub path_info { shift->path(@_) }
116 sub snippets { shift->captures(@_) }
120 Catalyst::Request - provides information about the current client request
131 $req->body_parameters;
132 $req->content_encoding;
133 $req->content_length;
141 $req->query_keywords;
149 $req->query_parameters;
153 $req->captures; # previously knows as snippets
160 See also L<Catalyst>, L<Catalyst::Request::Upload>.
164 This is the Catalyst Request class, which provides an interface to data for the
165 current client request. The request object is prepared by L<Catalyst::Engine>,
166 thus hiding the details of the particular engine implementation.
172 [DEPRECATED] Returns the name of the requested action.
175 Use C<< $c->action >> instead (which returns a
176 L<Catalyst::Action|Catalyst::Action> object).
180 Returns the IP address of the client.
182 =head2 $req->arguments
184 Returns a reference to an array containing the arguments.
186 print $c->request->arguments->[0];
188 For example, if your action was
190 package MyApp::C::Foo;
196 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
197 would be the first and only argument.
201 Shortcut for arguments.
205 Contains the URI base. This will always have a trailing slash.
207 If your application was queried with the URI
208 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
212 Returns the message body of the request, unless Content-Type is
213 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
215 =head2 $req->body_parameters
217 Returns a reference to a hash containing body (POST) parameters. Values can
218 be either a scalar or an arrayref containing scalars.
220 print $c->request->body_parameters->{field};
221 print $c->request->body_parameters->{field}->[0];
223 These are the parameters from the POST part of the request, if any.
225 =head2 $req->body_params
227 Shortcut for body_parameters.
229 =head2 $req->content_encoding
231 Shortcut for $req->headers->content_encoding.
233 =head2 $req->content_length
235 Shortcut for $req->headers->content_length.
237 =head2 $req->content_type
239 Shortcut for $req->headers->content_type.
243 A convenient method to access $req->cookies.
245 $cookie = $c->request->cookie('name');
246 @cookies = $c->request->cookie;
254 return keys %{ $self->cookies };
261 unless ( exists $self->cookies->{$name} ) {
265 return $self->cookies->{$name};
271 Returns a reference to a hash containing the cookies.
273 print $c->request->cookies->{mycookie}->value;
275 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
280 Shortcut for $req->headers->header.
284 Returns an L<HTTP::Headers> object containing the headers for the current request.
286 print $c->request->headers->header('X-Catalyst');
288 =head2 $req->hostname
290 Returns the hostname of the client.
294 Alias for $req->body.
296 =head2 $req->query_keywords
298 Contains the keywords portion of a query string, when no '=' signs are
301 http://localhost/path?some+keywords
303 $c->request->query_keywords will contain 'some keywords'
307 This contains the matching part of a Regex action. Otherwise
308 it returns the same as 'action', except for default actions,
309 which return an empty string.
313 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
317 Returns GET and POST parameters with a CGI.pm-compatible param method. This
318 is an alternative method for accessing parameters in $c->req->parameters.
320 $value = $c->request->param( 'foo' );
321 @values = $c->request->param( 'foo' );
322 @params = $c->request->param;
324 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
325 arguments to this method, like this:
327 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
329 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
330 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
331 (creating it if it didn't exist before), and C<quxx> as another value for
340 return keys %{ $self->parameters };
347 unless ( exists $self->parameters->{$param} ) {
348 return wantarray ? () : undef;
351 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
353 ? @{ $self->parameters->{$param} }
354 : $self->parameters->{$param}->[0];
358 ? ( $self->parameters->{$param} )
359 : $self->parameters->{$param};
364 $self->parameters->{$field} = [@_];
368 =head2 $req->parameters
370 Returns a reference to a hash containing GET and POST parameters. Values can
371 be either a scalar or an arrayref containing scalars.
373 print $c->request->parameters->{field};
374 print $c->request->parameters->{field}->[0];
376 This is the combination of C<query_parameters> and C<body_parameters>.
380 Shortcut for $req->parameters.
384 Returns the path, i.e. the part of the URI after $req->base, for the current request.
386 =head2 $req->path_info
388 Alias for path, added for compability with L<CGI>.
393 my ( $self, @params ) = @_;
396 $self->uri->path(@params);
399 elsif ( defined( my $path = $self->{path} ) ) {
403 my $path = $self->uri->path;
404 my $location = $self->base->path;
405 $path =~ s/^(\Q$location\E)?//;
407 $self->{path} = $path;
413 =head2 $req->protocol
415 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
417 =head2 $req->query_parameters
419 =head2 $req->query_params
421 Returns a reference to a hash containing query string (GET) parameters. Values can
422 be either a scalar or an arrayref containing scalars.
424 print $c->request->query_parameters->{field};
425 print $c->request->query_parameters->{field}->[0];
427 =head2 $req->read( [$maxlength] )
429 Reads a chunk of data from the request body. This method is intended to be
430 used in a while loop, reading $maxlength bytes on every call. $maxlength
431 defaults to the size of the request if not specified.
433 You have to set MyApp->config->{parse_on_demand} to use this directly.
437 sub read { shift->_context->read(@_); }
441 Shortcut for $req->headers->referer. Returns the referring page.
445 Returns true or false, indicating whether the connection is secure (https).
447 =head2 $req->captures
449 Returns a reference to an array containing regex captures.
451 my @captures = @{ $c->request->captures };
453 =head2 $req->snippets
455 C<captures> used to be called snippets. This is still available for backwoards
456 compatibility, but is considered deprecated.
460 A convenient method to access $req->uploads.
462 $upload = $c->request->upload('field');
463 @uploads = $c->request->upload('field');
464 @fields = $c->request->upload;
466 for my $upload ( $c->request->upload('field') ) {
467 print $upload->filename;
476 return keys %{ $self->uploads };
483 unless ( exists $self->uploads->{$upload} ) {
484 return wantarray ? () : undef;
487 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
489 ? @{ $self->uploads->{$upload} }
490 : $self->uploads->{$upload}->[0];
494 ? ( $self->uploads->{$upload} )
495 : $self->uploads->{$upload};
501 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
503 if ( exists $self->uploads->{$field} ) {
504 for ( $self->uploads->{$field} ) {
505 $_ = [$_] unless ref($_) eq "ARRAY";
506 push( @$_, $upload );
510 $self->uploads->{$field} = $upload;
518 Returns a reference to a hash containing uploads. Values can be either a
519 L<Catalyst::Request::Upload> object, or an arrayref of
520 L<Catalyst::Request::Upload> objects.
522 my $upload = $c->request->uploads->{field};
523 my $upload = $c->request->uploads->{field}->[0];
527 Returns a URI object for the current request. Stringifies to the URI text.
529 =head2 $req->uri_with( { key => 'value' } );
531 Returns a rewritten URI object for the current request. Key/value pairs
532 passed in will override existing parameters. Unmodified pairs will be
538 my( $self, $args ) = @_;
540 carp( 'No arguments passed to uri_with()' ) unless $args;
542 for my $value ( values %$args ) {
543 next unless defined $value;
544 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
546 utf8::encode( $_ ) if utf8::is_utf8($_);
550 my $uri = $self->uri->clone;
553 %{ $uri->query_form_hash },
561 Returns the currently logged in user. Deprecated. The method recommended for
562 newer plugins is $c->user.
564 =head2 $req->user_agent
566 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
575 Sebastian Riedel, C<sri@cpan.org>
577 Marcus Ramberg, C<mramberg@cpan.org>
581 This program is free software, you can redistribute it and/or modify
582 it under the same terms as Perl itself.
586 __PACKAGE__->meta->make_immutable;