1 package Catalyst::Request;
3 use IO::Socket qw[AF_INET inet_aton];
13 with 'MooseX::Emulate::Class::Accessor::Fast';
15 has action => (is => 'rw');
16 has address => (is => 'rw');
17 has arguments => (is => 'rw', default => sub { [] });
18 has cookies => (is => 'rw', default => sub { {} });
19 has query_keywords => (is => 'rw');
20 has match => (is => 'rw');
21 has method => (is => 'rw');
22 has protocol => (is => 'rw');
23 has query_parameters => (is => 'rw', default => sub { {} });
24 has secure => (is => 'rw', default => 0);
25 has captures => (is => 'rw', default => sub { [] });
26 has uri => (is => 'rw', predicate => 'has_uri');
27 has user => (is => 'rw');
30 isa => 'HTTP::Headers',
31 handles => [qw(content_encoding content_length content_type header referer user_agent)],
32 default => sub { HTTP::Headers->new() },
38 # - Can we lose the before modifiers which just call prepare_body ?
39 # they are wasteful, slow us down and feel cluttery.
40 # Can we call prepare_body at BUILD time?
41 # Can we make _body an attribute, have the rest of
42 # these lazy build from there and kill all the direct hash access
43 # in Catalyst.pm and Engine.pm?
49 clearer => '_clear_context',
52 has body_parameters => (
56 default => sub { {} },
59 before body_parameters => sub {
61 $self->_context->prepare_body();
67 default => sub { {} },
74 default => sub { {} },
77 before parameters => sub {
78 my ($self, $params) = @_;
79 if ( $params && !ref $params ) {
80 $self->_context->log->warn(
81 "Attempt to retrieve '$params' with req->params(), " .
82 "you probably meant to call req->param('$params')" );
94 return $self->path if $self->has_uri;
99 is => 'rw', clearer => '_clear_body', predicate => '_has_body',
101 # Eugh, ugly. Should just be able to rename accessor methods to 'body'
102 # and provide a custom reader..
105 $self->_context->prepare_body();
106 $self->_body(@_) if scalar @_;
107 return blessed $self->_body ? $self->_body->body : $self->_body;
116 gethostbyaddr( inet_aton( $self->address ), AF_INET ) || 'localhost'
120 has _path => ( is => 'rw', predicate => '_has_path', clearer => '_clear_path' );
124 sub args { shift->arguments(@_) }
125 sub body_params { shift->body_parameters(@_) }
126 sub input { shift->body(@_) }
127 sub params { shift->parameters(@_) }
128 sub query_params { shift->query_parameters(@_) }
129 sub path_info { shift->path(@_) }
130 sub snippets { shift->captures(@_) }
134 Catalyst::Request - provides information about the current client request
145 $req->body_parameters;
146 $req->content_encoding;
147 $req->content_length;
155 $req->query_keywords;
163 $req->query_parameters;
167 $req->captures; # previously knows as snippets
174 See also L<Catalyst>, L<Catalyst::Request::Upload>.
178 This is the Catalyst Request class, which provides an interface to data for the
179 current client request. The request object is prepared by L<Catalyst::Engine>,
180 thus hiding the details of the particular engine implementation.
186 [DEPRECATED] Returns the name of the requested action.
189 Use C<< $c->action >> instead (which returns a
190 L<Catalyst::Action|Catalyst::Action> object).
194 Returns the IP address of the client.
196 =head2 $req->arguments
198 Returns a reference to an array containing the arguments.
200 print $c->request->arguments->[0];
202 For example, if your action was
204 package MyApp::C::Foo;
210 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
211 would be the first and only argument.
215 Shortcut for arguments.
219 Contains the URI base. This will always have a trailing slash.
221 If your application was queried with the URI
222 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
226 Returns the message body of the request, unless Content-Type is
227 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
229 =head2 $req->body_parameters
231 Returns a reference to a hash containing body (POST) parameters. Values can
232 be either a scalar or an arrayref containing scalars.
234 print $c->request->body_parameters->{field};
235 print $c->request->body_parameters->{field}->[0];
237 These are the parameters from the POST part of the request, if any.
239 =head2 $req->body_params
241 Shortcut for body_parameters.
243 =head2 $req->content_encoding
245 Shortcut for $req->headers->content_encoding.
247 =head2 $req->content_length
249 Shortcut for $req->headers->content_length.
251 =head2 $req->content_type
253 Shortcut for $req->headers->content_type.
257 A convenient method to access $req->cookies.
259 $cookie = $c->request->cookie('name');
260 @cookies = $c->request->cookie;
268 return keys %{ $self->cookies };
275 unless ( exists $self->cookies->{$name} ) {
279 return $self->cookies->{$name};
285 Returns a reference to a hash containing the cookies.
287 print $c->request->cookies->{mycookie}->value;
289 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
294 Shortcut for $req->headers->header.
298 Returns an L<HTTP::Headers> object containing the headers for the current request.
300 print $c->request->headers->header('X-Catalyst');
302 =head2 $req->hostname
304 Returns the hostname of the client.
308 Alias for $req->body.
310 =head2 $req->query_keywords
312 Contains the keywords portion of a query string, when no '=' signs are
315 http://localhost/path?some+keywords
317 $c->request->query_keywords will contain 'some keywords'
321 This contains the matching part of a Regex action. Otherwise
322 it returns the same as 'action', except for default actions,
323 which return an empty string.
327 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
331 Returns GET and POST parameters with a CGI.pm-compatible param method. This
332 is an alternative method for accessing parameters in $c->req->parameters.
334 $value = $c->request->param( 'foo' );
335 @values = $c->request->param( 'foo' );
336 @params = $c->request->param;
338 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
339 arguments to this method, like this:
341 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
343 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
344 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
345 (creating it if it didn't exist before), and C<quxx> as another value for
354 return keys %{ $self->parameters };
361 unless ( exists $self->parameters->{$param} ) {
362 return wantarray ? () : undef;
365 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
367 ? @{ $self->parameters->{$param} }
368 : $self->parameters->{$param}->[0];
372 ? ( $self->parameters->{$param} )
373 : $self->parameters->{$param};
378 $self->parameters->{$field} = [@_];
382 =head2 $req->parameters
384 Returns a reference to a hash containing GET and POST parameters. Values can
385 be either a scalar or an arrayref containing scalars.
387 print $c->request->parameters->{field};
388 print $c->request->parameters->{field}->[0];
390 This is the combination of C<query_parameters> and C<body_parameters>.
394 Shortcut for $req->parameters.
398 Returns the path, i.e. the part of the URI after $req->base, for the current request.
400 =head2 $req->path_info
402 Alias for path, added for compability with L<CGI>.
407 my ( $self, @params ) = @_;
410 $self->uri->path(@params);
413 elsif ( $self->_has_path ) {
417 my $path = $self->uri->path;
418 my $location = $self->base->path;
419 $path =~ s/^(\Q$location\E)?//;
427 =head2 $req->protocol
429 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
431 =head2 $req->query_parameters
433 =head2 $req->query_params
435 Returns a reference to a hash containing query string (GET) parameters. Values can
436 be either a scalar or an arrayref containing scalars.
438 print $c->request->query_parameters->{field};
439 print $c->request->query_parameters->{field}->[0];
441 =head2 $req->read( [$maxlength] )
443 Reads a chunk of data from the request body. This method is intended to be
444 used in a while loop, reading $maxlength bytes on every call. $maxlength
445 defaults to the size of the request if not specified.
447 You have to set MyApp->config->{parse_on_demand} to use this directly.
451 Shortcut for $req->headers->referer. Returns the referring page.
455 Returns true or false, indicating whether the connection is secure (https).
457 =head2 $req->captures
459 Returns a reference to an array containing captured args from chained
460 actions or regex captures.
462 my @captures = @{ $c->request->captures };
464 =head2 $req->snippets
466 C<captures> used to be called snippets. This is still available for backwoards
467 compatibility, but is considered deprecated.
471 A convenient method to access $req->uploads.
473 $upload = $c->request->upload('field');
474 @uploads = $c->request->upload('field');
475 @fields = $c->request->upload;
477 for my $upload ( $c->request->upload('field') ) {
478 print $upload->filename;
487 return keys %{ $self->uploads };
494 unless ( exists $self->uploads->{$upload} ) {
495 return wantarray ? () : undef;
498 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
500 ? @{ $self->uploads->{$upload} }
501 : $self->uploads->{$upload}->[0];
505 ? ( $self->uploads->{$upload} )
506 : $self->uploads->{$upload};
512 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
514 if ( exists $self->uploads->{$field} ) {
515 for ( $self->uploads->{$field} ) {
516 $_ = [$_] unless ref($_) eq "ARRAY";
517 push( @$_, $upload );
521 $self->uploads->{$field} = $upload;
529 Returns a reference to a hash containing uploads. Values can be either a
530 L<Catalyst::Request::Upload> object, or an arrayref of
531 L<Catalyst::Request::Upload> objects.
533 my $upload = $c->request->uploads->{field};
534 my $upload = $c->request->uploads->{field}->[0];
538 Returns a URI object for the current request. Stringifies to the URI text.
540 =head2 $req->uri_with( { key => 'value' } );
542 Returns a rewritten URI object for the current request. Key/value pairs
543 passed in will override existing parameters. You can remove an existing
544 parameter by passing in an undef value. Unmodified pairs will be
550 my( $self, $args ) = @_;
552 carp( 'No arguments passed to uri_with()' ) unless $args;
554 foreach my $value ( values %$args ) {
555 next unless defined $value;
556 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
558 utf8::encode( $_ ) if utf8::is_utf8($_);
562 my $uri = $self->uri->clone;
563 my %query = ( %{ $uri->query_form_hash }, %$args );
566 # remove undef values
567 map { defined $query{ $_ } ? ( $_ => $query{ $_ } ) : () } keys %query
574 Returns the currently logged in user. Deprecated. The method recommended for
575 newer plugins is $c->user.
577 =head2 $req->user_agent
579 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
588 Catalyst Contributors, see Catalyst.pm
592 This program is free software, you can redistribute it and/or modify
593 it under the same terms as Perl itself.
597 __PACKAGE__->meta->make_immutable;