1 package Catalyst::Request;
3 use IO::Socket qw[AF_INET inet_aton];
13 has action => (is => 'rw');
14 has address => (is => 'rw');
15 has arguments => (is => 'rw', default => sub { [] });
16 has cookies => (is => 'rw', default => sub { {} });
17 has query_keywords => (is => 'rw');
18 has match => (is => 'rw');
19 has method => (is => 'rw');
20 has protocol => (is => 'rw');
21 has query_parameters => (is => 'rw', default => sub { {} });
22 has secure => (is => 'rw', default => 0);
23 has captures => (is => 'rw', default => sub { [] });
24 has uri => (is => 'rw');
25 has user => (is => 'rw');
28 isa => 'HTTP::Headers',
29 handles => [qw(content_encoding content_length content_type header referer user_agent)],
30 default => sub { HTTP::Headers->new() },
40 has body_parameters => (
44 default => sub { {} },
47 before body_parameters => sub {
49 $self->_context->prepare_body();
56 default => sub { {} },
59 before uploads => sub {
61 #$self->_context->prepare_body;
68 default => sub { {} },
71 before parameters => sub {
72 my ($self, $params) = @_;
73 #$self->_context->prepare_body();
74 if ( $params && !ref $params ) {
75 $self->_context->log->warn(
76 "Attempt to retrieve '$params' with req->params(), " .
77 "you probably meant to call req->param('$params')" );
89 return $self->path if $self->uri;
99 $self->_context->prepare_body();
108 gethostbyaddr( inet_aton( $self->address ), AF_INET )
114 sub args { shift->arguments(@_) }
115 sub body_params { shift->body_parameters(@_) }
116 sub input { shift->body(@_) }
117 sub params { shift->parameters(@_) }
118 sub query_params { shift->query_parameters(@_) }
119 sub path_info { shift->path(@_) }
120 sub snippets { shift->captures(@_) }
124 Catalyst::Request - provides information about the current client request
135 $req->body_parameters;
136 $req->content_encoding;
137 $req->content_length;
145 $req->query_keywords;
153 $req->query_parameters;
157 $req->captures; # previously knows as snippets
164 See also L<Catalyst>, L<Catalyst::Request::Upload>.
168 This is the Catalyst Request class, which provides an interface to data for the
169 current client request. The request object is prepared by L<Catalyst::Engine>,
170 thus hiding the details of the particular engine implementation.
176 [DEPRECATED] Returns the name of the requested action.
179 Use C<< $c->action >> instead (which returns a
180 L<Catalyst::Action|Catalyst::Action> object).
184 Returns the IP address of the client.
186 =head2 $req->arguments
188 Returns a reference to an array containing the arguments.
190 print $c->request->arguments->[0];
192 For example, if your action was
194 package MyApp::C::Foo;
200 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
201 would be the first and only argument.
205 Shortcut for arguments.
209 Contains the URI base. This will always have a trailing slash.
211 If your application was queried with the URI
212 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
216 Returns the message body of the request, unless Content-Type is
217 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
219 =head2 $req->body_parameters
221 Returns a reference to a hash containing body (POST) parameters. Values can
222 be either a scalar or an arrayref containing scalars.
224 print $c->request->body_parameters->{field};
225 print $c->request->body_parameters->{field}->[0];
227 These are the parameters from the POST part of the request, if any.
229 =head2 $req->body_params
231 Shortcut for body_parameters.
233 =head2 $req->content_encoding
235 Shortcut for $req->headers->content_encoding.
237 =head2 $req->content_length
239 Shortcut for $req->headers->content_length.
241 =head2 $req->content_type
243 Shortcut for $req->headers->content_type.
247 A convenient method to access $req->cookies.
249 $cookie = $c->request->cookie('name');
250 @cookies = $c->request->cookie;
258 return keys %{ $self->cookies };
265 unless ( exists $self->cookies->{$name} ) {
269 return $self->cookies->{$name};
275 Returns a reference to a hash containing the cookies.
277 print $c->request->cookies->{mycookie}->value;
279 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
284 Shortcut for $req->headers->header.
288 Returns an L<HTTP::Headers> object containing the headers for the current request.
290 print $c->request->headers->header('X-Catalyst');
292 =head2 $req->hostname
294 Returns the hostname of the client.
298 Alias for $req->body.
300 =head2 $req->query_keywords
302 Contains the keywords portion of a query string, when no '=' signs are
305 http://localhost/path?some+keywords
307 $c->request->query_keywords will contain 'some keywords'
311 This contains the matching part of a Regex action. Otherwise
312 it returns the same as 'action', except for default actions,
313 which return an empty string.
317 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
321 Returns GET and POST parameters with a CGI.pm-compatible param method. This
322 is an alternative method for accessing parameters in $c->req->parameters.
324 $value = $c->request->param( 'foo' );
325 @values = $c->request->param( 'foo' );
326 @params = $c->request->param;
328 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
329 arguments to this method, like this:
331 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
333 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
334 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
335 (creating it if it didn't exist before), and C<quxx> as another value for
344 return keys %{ $self->parameters };
351 unless ( exists $self->parameters->{$param} ) {
352 return wantarray ? () : undef;
355 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
357 ? @{ $self->parameters->{$param} }
358 : $self->parameters->{$param}->[0];
362 ? ( $self->parameters->{$param} )
363 : $self->parameters->{$param};
368 $self->parameters->{$field} = [@_];
372 =head2 $req->parameters
374 Returns a reference to a hash containing GET and POST parameters. Values can
375 be either a scalar or an arrayref containing scalars.
377 print $c->request->parameters->{field};
378 print $c->request->parameters->{field}->[0];
380 This is the combination of C<query_parameters> and C<body_parameters>.
384 Shortcut for $req->parameters.
388 Returns the path, i.e. the part of the URI after $req->base, for the current request.
390 =head2 $req->path_info
392 Alias for path, added for compability with L<CGI>.
397 my ( $self, @params ) = @_;
400 $self->uri->path(@params);
403 elsif ( defined( my $path = $self->{path} ) ) {
407 my $path = $self->uri->path;
408 my $location = $self->base->path;
409 $path =~ s/^(\Q$location\E)?//;
411 $self->{path} = $path;
417 =head2 $req->protocol
419 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
421 =head2 $req->query_parameters
423 =head2 $req->query_params
425 Returns a reference to a hash containing query string (GET) parameters. Values can
426 be either a scalar or an arrayref containing scalars.
428 print $c->request->query_parameters->{field};
429 print $c->request->query_parameters->{field}->[0];
431 =head2 $req->read( [$maxlength] )
433 Reads a chunk of data from the request body. This method is intended to be
434 used in a while loop, reading $maxlength bytes on every call. $maxlength
435 defaults to the size of the request if not specified.
437 You have to set MyApp->config->{parse_on_demand} to use this directly.
441 sub read { shift->_context->read(@_); }
445 Shortcut for $req->headers->referer. Returns the referring page.
449 Returns true or false, indicating whether the connection is secure (https).
451 =head2 $req->captures
453 Returns a reference to an array containing regex captures.
455 my @captures = @{ $c->request->captures };
457 =head2 $req->snippets
459 C<captures> used to be called snippets. This is still available for backwoards
460 compatibility, but is considered deprecated.
464 A convenient method to access $req->uploads.
466 $upload = $c->request->upload('field');
467 @uploads = $c->request->upload('field');
468 @fields = $c->request->upload;
470 for my $upload ( $c->request->upload('field') ) {
471 print $upload->filename;
480 return keys %{ $self->uploads };
487 unless ( exists $self->uploads->{$upload} ) {
488 return wantarray ? () : undef;
491 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
493 ? @{ $self->uploads->{$upload} }
494 : $self->uploads->{$upload}->[0];
498 ? ( $self->uploads->{$upload} )
499 : $self->uploads->{$upload};
505 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
507 if ( exists $self->uploads->{$field} ) {
508 for ( $self->uploads->{$field} ) {
509 $_ = [$_] unless ref($_) eq "ARRAY";
510 push( @$_, $upload );
514 $self->uploads->{$field} = $upload;
522 Returns a reference to a hash containing uploads. Values can be either a
523 L<Catalyst::Request::Upload> object, or an arrayref of
524 L<Catalyst::Request::Upload> objects.
526 my $upload = $c->request->uploads->{field};
527 my $upload = $c->request->uploads->{field}->[0];
531 Returns a URI object for the current request. Stringifies to the URI text.
533 =head2 $req->uri_with( { key => 'value' } );
535 Returns a rewritten URI object for the current request. Key/value pairs
536 passed in will override existing parameters. Unmodified pairs will be
542 my( $self, $args ) = @_;
544 carp( 'No arguments passed to uri_with()' ) unless $args;
546 for my $value ( values %$args ) {
547 next unless defined $value;
548 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
550 utf8::encode( $_ ) if utf8::is_utf8($_);
554 my $uri = $self->uri->clone;
557 %{ $uri->query_form_hash },
565 Returns the currently logged in user. Deprecated. The method recommended for
566 newer plugins is $c->user.
568 =head2 $req->user_agent
570 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
579 Sebastian Riedel, C<sri@cpan.org>
581 Marcus Ramberg, C<mramberg@cpan.org>
585 This program is free software, you can redistribute it and/or modify
586 it under the same terms as Perl itself.
590 __PACKAGE__->meta->make_immutable;