1 package Catalyst::Request;
4 use IO::Socket qw[AF_INET inet_aton];
14 has action => (is => 'rw');
15 has address => (is => 'rw');
16 has arguments => (is => 'rw', default => sub { [] });
17 has cookies => (is => 'rw', default => sub { {} });
18 has query_keywords => (is => 'rw');
19 has match => (is => 'rw');
20 has method => (is => 'rw');
21 has protocol => (is => 'rw');
22 has query_parameters => (is => 'rw', default => sub { {} });
23 has secure => (is => 'rw', default => 0);
24 has captures => (is => 'rw', default => sub { [] });
25 has uri => (is => 'rw');
26 has user => (is => 'rw');
29 isa => 'HTTP::Headers',
30 handles => [qw(content_encoding content_length content_type header referer user_agent)],
31 default => sub { HTTP::Headers->new() },
37 #can we lose the before modifiers which just call prepare_body ?
38 #they are wasteful, slow us down and feel cluttery.
39 # Can we call prepare_body at BUILD time?
40 # Can we make _body an attribute and have the rest of these lazy build from there?
48 has body_parameters => (
52 default => sub { {} },
55 before body_parameters => sub {
57 $self->_context->prepare_body();
64 default => sub { {} },
67 # modifier was a noop (groditi)
68 # before uploads => sub {
70 # #$self->_context->prepare_body;
77 default => sub { {} },
80 before parameters => sub {
81 my ($self, $params) = @_;
82 #$self->_context->prepare_body();
83 if ( $params && !ref $params ) {
84 $self->_context->log->warn(
85 "Attempt to retrieve '$params' with req->params(), " .
86 "you probably meant to call req->param('$params')" );
98 return $self->path if $self->uri;
108 $self->_context->prepare_body();
117 gethostbyaddr( inet_aton( $self->address ), AF_INET )
123 sub args { shift->arguments(@_) }
124 sub body_params { shift->body_parameters(@_) }
125 sub input { shift->body(@_) }
126 sub params { shift->parameters(@_) }
127 sub query_params { shift->query_parameters(@_) }
128 sub path_info { shift->path(@_) }
129 sub snippets { shift->captures(@_) }
133 Catalyst::Request - provides information about the current client request
144 $req->body_parameters;
145 $req->content_encoding;
146 $req->content_length;
154 $req->query_keywords;
162 $req->query_parameters;
166 $req->captures; # previously knows as snippets
173 See also L<Catalyst>, L<Catalyst::Request::Upload>.
177 This is the Catalyst Request class, which provides an interface to data for the
178 current client request. The request object is prepared by L<Catalyst::Engine>,
179 thus hiding the details of the particular engine implementation.
185 [DEPRECATED] Returns the name of the requested action.
188 Use C<< $c->action >> instead (which returns a
189 L<Catalyst::Action|Catalyst::Action> object).
193 Returns the IP address of the client.
195 =head2 $req->arguments
197 Returns a reference to an array containing the arguments.
199 print $c->request->arguments->[0];
201 For example, if your action was
203 package MyApp::C::Foo;
209 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
210 would be the first and only argument.
214 Shortcut for arguments.
218 Contains the URI base. This will always have a trailing slash.
220 If your application was queried with the URI
221 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
225 Returns the message body of the request, unless Content-Type is
226 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
228 =head2 $req->body_parameters
230 Returns a reference to a hash containing body (POST) parameters. Values can
231 be either a scalar or an arrayref containing scalars.
233 print $c->request->body_parameters->{field};
234 print $c->request->body_parameters->{field}->[0];
236 These are the parameters from the POST part of the request, if any.
238 =head2 $req->body_params
240 Shortcut for body_parameters.
242 =head2 $req->content_encoding
244 Shortcut for $req->headers->content_encoding.
246 =head2 $req->content_length
248 Shortcut for $req->headers->content_length.
250 =head2 $req->content_type
252 Shortcut for $req->headers->content_type.
256 A convenient method to access $req->cookies.
258 $cookie = $c->request->cookie('name');
259 @cookies = $c->request->cookie;
267 return keys %{ $self->cookies };
274 unless ( exists $self->cookies->{$name} ) {
278 return $self->cookies->{$name};
284 Returns a reference to a hash containing the cookies.
286 print $c->request->cookies->{mycookie}->value;
288 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
293 Shortcut for $req->headers->header.
297 Returns an L<HTTP::Headers> object containing the headers for the current request.
299 print $c->request->headers->header('X-Catalyst');
301 =head2 $req->hostname
303 Returns the hostname of the client.
307 Alias for $req->body.
309 =head2 $req->query_keywords
311 Contains the keywords portion of a query string, when no '=' signs are
314 http://localhost/path?some+keywords
316 $c->request->query_keywords will contain 'some keywords'
320 This contains the matching part of a Regex action. Otherwise
321 it returns the same as 'action', except for default actions,
322 which return an empty string.
326 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
330 Returns GET and POST parameters with a CGI.pm-compatible param method. This
331 is an alternative method for accessing parameters in $c->req->parameters.
333 $value = $c->request->param( 'foo' );
334 @values = $c->request->param( 'foo' );
335 @params = $c->request->param;
337 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
338 arguments to this method, like this:
340 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
342 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
343 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
344 (creating it if it didn't exist before), and C<quxx> as another value for
353 return keys %{ $self->parameters };
360 unless ( exists $self->parameters->{$param} ) {
361 return wantarray ? () : undef;
364 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
366 ? @{ $self->parameters->{$param} }
367 : $self->parameters->{$param}->[0];
371 ? ( $self->parameters->{$param} )
372 : $self->parameters->{$param};
377 $self->parameters->{$field} = [@_];
381 =head2 $req->parameters
383 Returns a reference to a hash containing GET and POST parameters. Values can
384 be either a scalar or an arrayref containing scalars.
386 print $c->request->parameters->{field};
387 print $c->request->parameters->{field}->[0];
389 This is the combination of C<query_parameters> and C<body_parameters>.
393 Shortcut for $req->parameters.
397 Returns the path, i.e. the part of the URI after $req->base, for the current request.
399 =head2 $req->path_info
401 Alias for path, added for compability with L<CGI>.
406 my ( $self, @params ) = @_;
409 $self->uri->path(@params);
412 elsif ( defined( my $path = $self->{path} ) ) {
416 my $path = $self->uri->path;
417 my $location = $self->base->path;
418 $path =~ s/^(\Q$location\E)?//;
420 $self->{path} = $path;
426 =head2 $req->protocol
428 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
430 =head2 $req->query_parameters
432 =head2 $req->query_params
434 Returns a reference to a hash containing query string (GET) parameters. Values can
435 be either a scalar or an arrayref containing scalars.
437 print $c->request->query_parameters->{field};
438 print $c->request->query_parameters->{field}->[0];
440 =head2 $req->read( [$maxlength] )
442 Reads a chunk of data from the request body. This method is intended to be
443 used in a while loop, reading $maxlength bytes on every call. $maxlength
444 defaults to the size of the request if not specified.
446 You have to set MyApp->config->{parse_on_demand} to use this directly.
450 Shortcut for $req->headers->referer. Returns the referring page.
454 Returns true or false, indicating whether the connection is secure (https).
456 =head2 $req->captures
458 Returns a reference to an array containing regex captures.
460 my @captures = @{ $c->request->captures };
462 =head2 $req->snippets
464 C<captures> used to be called snippets. This is still available for backwoards
465 compatibility, but is considered deprecated.
469 A convenient method to access $req->uploads.
471 $upload = $c->request->upload('field');
472 @uploads = $c->request->upload('field');
473 @fields = $c->request->upload;
475 for my $upload ( $c->request->upload('field') ) {
476 print $upload->filename;
485 return keys %{ $self->uploads };
492 unless ( exists $self->uploads->{$upload} ) {
493 return wantarray ? () : undef;
496 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
498 ? @{ $self->uploads->{$upload} }
499 : $self->uploads->{$upload}->[0];
503 ? ( $self->uploads->{$upload} )
504 : $self->uploads->{$upload};
510 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
512 if ( exists $self->uploads->{$field} ) {
513 for ( $self->uploads->{$field} ) {
514 $_ = [$_] unless ref($_) eq "ARRAY";
515 push( @$_, $upload );
519 $self->uploads->{$field} = $upload;
527 Returns a reference to a hash containing uploads. Values can be either a
528 L<Catalyst::Request::Upload> object, or an arrayref of
529 L<Catalyst::Request::Upload> objects.
531 my $upload = $c->request->uploads->{field};
532 my $upload = $c->request->uploads->{field}->[0];
536 Returns a URI object for the current request. Stringifies to the URI text.
538 =head2 $req->uri_with( { key => 'value' } );
540 Returns a rewritten URI object for the current request. Key/value pairs
541 passed in will override existing parameters. Unmodified pairs will be
547 my( $self, $args ) = @_;
549 carp( 'No arguments passed to uri_with()' ) unless $args;
551 for my $value ( values %$args ) {
552 next unless defined $value;
553 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
555 utf8::encode( $_ ) if utf8::is_utf8($_);
559 my $uri = $self->uri->clone;
562 %{ $uri->query_form_hash },
570 Returns the currently logged in user. Deprecated. The method recommended for
571 newer plugins is $c->user.
573 =head2 $req->user_agent
575 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
584 Sebastian Riedel, C<sri@cpan.org>
586 Marcus Ramberg, C<mramberg@cpan.org>
590 This program is free software, you can redistribute it and/or modify
591 it under the same terms as Perl itself.
595 __PACKAGE__->meta->make_immutable;