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')" );
97 $self->_context->prepare_body();
106 gethostbyaddr( inet_aton( $self->address ), AF_INET )
112 sub args { shift->arguments(@_) }
113 sub body_params { shift->body_parameters(@_) }
114 sub input { shift->body(@_) }
115 sub params { shift->parameters(@_) }
116 sub query_params { shift->query_parameters(@_) }
117 sub path_info { shift->path(@_) }
118 sub snippets { shift->captures(@_) }
122 Catalyst::Request - provides information about the current client request
133 $req->body_parameters;
134 $req->content_encoding;
135 $req->content_length;
143 $req->query_keywords;
151 $req->query_parameters;
155 $req->captures; # previously knows as snippets
162 See also L<Catalyst>, L<Catalyst::Request::Upload>.
166 This is the Catalyst Request class, which provides an interface to data for the
167 current client request. The request object is prepared by L<Catalyst::Engine>,
168 thus hiding the details of the particular engine implementation.
174 [DEPRECATED] Returns the name of the requested action.
177 Use C<< $c->action >> instead (which returns a
178 L<Catalyst::Action|Catalyst::Action> object).
182 Returns the IP address of the client.
184 =head2 $req->arguments
186 Returns a reference to an array containing the arguments.
188 print $c->request->arguments->[0];
190 For example, if your action was
192 package MyApp::C::Foo;
198 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
199 would be the first and only argument.
203 Shortcut for arguments.
207 Contains the URI base. This will always have a trailing slash.
209 If your application was queried with the URI
210 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
214 Returns the message body of the request, unless Content-Type is
215 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
217 =head2 $req->body_parameters
219 Returns a reference to a hash containing body (POST) parameters. Values can
220 be either a scalar or an arrayref containing scalars.
222 print $c->request->body_parameters->{field};
223 print $c->request->body_parameters->{field}->[0];
225 These are the parameters from the POST part of the request, if any.
227 =head2 $req->body_params
229 Shortcut for body_parameters.
231 =head2 $req->content_encoding
233 Shortcut for $req->headers->content_encoding.
235 =head2 $req->content_length
237 Shortcut for $req->headers->content_length.
239 =head2 $req->content_type
241 Shortcut for $req->headers->content_type.
245 A convenient method to access $req->cookies.
247 $cookie = $c->request->cookie('name');
248 @cookies = $c->request->cookie;
256 return keys %{ $self->cookies };
263 unless ( exists $self->cookies->{$name} ) {
267 return $self->cookies->{$name};
273 Returns a reference to a hash containing the cookies.
275 print $c->request->cookies->{mycookie}->value;
277 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
282 Shortcut for $req->headers->header.
286 Returns an L<HTTP::Headers> object containing the headers for the current request.
288 print $c->request->headers->header('X-Catalyst');
290 =head2 $req->hostname
292 Returns the hostname of the client.
296 Alias for $req->body.
298 =head2 $req->query_keywords
300 Contains the keywords portion of a query string, when no '=' signs are
303 http://localhost/path?some+keywords
305 $c->request->query_keywords will contain 'some keywords'
309 This contains the matching part of a Regex action. Otherwise
310 it returns the same as 'action', except for default actions,
311 which return an empty string.
315 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
319 Returns GET and POST parameters with a CGI.pm-compatible param method. This
320 is an alternative method for accessing parameters in $c->req->parameters.
322 $value = $c->request->param( 'foo' );
323 @values = $c->request->param( 'foo' );
324 @params = $c->request->param;
326 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
327 arguments to this method, like this:
329 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
331 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
332 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
333 (creating it if it didn't exist before), and C<quxx> as another value for
342 return keys %{ $self->parameters };
349 unless ( exists $self->parameters->{$param} ) {
350 return wantarray ? () : undef;
353 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
355 ? @{ $self->parameters->{$param} }
356 : $self->parameters->{$param}->[0];
360 ? ( $self->parameters->{$param} )
361 : $self->parameters->{$param};
366 $self->parameters->{$field} = [@_];
370 =head2 $req->parameters
372 Returns a reference to a hash containing GET and POST parameters. Values can
373 be either a scalar or an arrayref containing scalars.
375 print $c->request->parameters->{field};
376 print $c->request->parameters->{field}->[0];
378 This is the combination of C<query_parameters> and C<body_parameters>.
382 Shortcut for $req->parameters.
386 Returns the path, i.e. the part of the URI after $req->base, for the current request.
388 =head2 $req->path_info
390 Alias for path, added for compability with L<CGI>.
395 my ( $self, @params ) = @_;
398 $self->uri->path(@params);
401 elsif ( defined( my $path = $self->{path} ) ) {
405 my $path = $self->uri->path;
406 my $location = $self->base->path;
407 $path =~ s/^(\Q$location\E)?//;
409 $self->{path} = $path;
415 =head2 $req->protocol
417 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
419 =head2 $req->query_parameters
421 =head2 $req->query_params
423 Returns a reference to a hash containing query string (GET) parameters. Values can
424 be either a scalar or an arrayref containing scalars.
426 print $c->request->query_parameters->{field};
427 print $c->request->query_parameters->{field}->[0];
429 =head2 $req->read( [$maxlength] )
431 Reads a chunk of data from the request body. This method is intended to be
432 used in a while loop, reading $maxlength bytes on every call. $maxlength
433 defaults to the size of the request if not specified.
435 You have to set MyApp->config->{parse_on_demand} to use this directly.
439 sub read { shift->_context->read(@_); }
443 Shortcut for $req->headers->referer. Returns the referring page.
447 Returns true or false, indicating whether the connection is secure (https).
449 =head2 $req->captures
451 Returns a reference to an array containing regex captures.
453 my @captures = @{ $c->request->captures };
455 =head2 $req->snippets
457 C<captures> used to be called snippets. This is still available for backwoards
458 compatibility, but is considered deprecated.
462 A convenient method to access $req->uploads.
464 $upload = $c->request->upload('field');
465 @uploads = $c->request->upload('field');
466 @fields = $c->request->upload;
468 for my $upload ( $c->request->upload('field') ) {
469 print $upload->filename;
478 return keys %{ $self->uploads };
485 unless ( exists $self->uploads->{$upload} ) {
486 return wantarray ? () : undef;
489 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
491 ? @{ $self->uploads->{$upload} }
492 : $self->uploads->{$upload}->[0];
496 ? ( $self->uploads->{$upload} )
497 : $self->uploads->{$upload};
503 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
505 if ( exists $self->uploads->{$field} ) {
506 for ( $self->uploads->{$field} ) {
507 $_ = [$_] unless ref($_) eq "ARRAY";
508 push( @$_, $upload );
512 $self->uploads->{$field} = $upload;
520 Returns a reference to a hash containing uploads. Values can be either a
521 L<Catalyst::Request::Upload> object, or an arrayref of
522 L<Catalyst::Request::Upload> objects.
524 my $upload = $c->request->uploads->{field};
525 my $upload = $c->request->uploads->{field}->[0];
529 Returns a URI object for the current request. Stringifies to the URI text.
531 =head2 $req->uri_with( { key => 'value' } );
533 Returns a rewritten URI object for the current request. Key/value pairs
534 passed in will override existing parameters. Unmodified pairs will be
540 my( $self, $args ) = @_;
542 carp( 'No arguments passed to uri_with()' ) unless $args;
544 for my $value ( values %$args ) {
545 next unless defined $value;
546 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
548 utf8::encode( $_ ) if utf8::is_utf8($_);
552 my $uri = $self->uri->clone;
555 %{ $uri->query_form_hash },
563 Returns the currently logged in user. Deprecated. The method recommended for
564 newer plugins is $c->user.
566 =head2 $req->user_agent
568 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
577 Sebastian Riedel, C<sri@cpan.org>
579 Marcus Ramberg, C<mramberg@cpan.org>
583 This program is free software, you can redistribute it and/or modify
584 it under the same terms as Perl itself.