1 package Catalyst::Request;
3 use IO::Socket qw[AF_INET inet_aton];
13 use namespace::clean -except => 'meta';
15 with 'MooseX::Emulate::Class::Accessor::Fast';
17 has action => (is => 'rw');
18 has address => (is => 'rw');
19 has arguments => (is => 'rw', default => sub { [] });
20 has cookies => (is => 'rw', default => sub { {} });
21 has query_keywords => (is => 'rw');
22 has match => (is => 'rw');
23 has method => (is => 'rw');
24 has protocol => (is => 'rw');
25 has query_parameters => (is => 'rw', default => sub { {} });
26 has secure => (is => 'rw', default => 0);
27 has captures => (is => 'rw', default => sub { [] });
28 has uri => (is => 'rw', predicate => 'has_uri');
29 has user => (is => 'rw');
32 isa => 'HTTP::Headers',
33 handles => [qw(content_encoding content_length content_type header referer user_agent)],
34 default => sub { HTTP::Headers->new() },
40 # - Can we lose the before modifiers which just call prepare_body ?
41 # they are wasteful, slow us down and feel cluttery.
42 # Can we call prepare_body at BUILD time?
43 # Can we make _body an attribute, have the rest of
44 # these lazy build from there and kill all the direct hash access
45 # in Catalyst.pm and Engine.pm?
51 clearer => '_clear_context',
54 has body_parameters => (
58 default => sub { {} },
61 before body_parameters => sub {
63 $self->_context->prepare_body();
69 default => sub { {} },
76 default => sub { {} },
79 before parameters => sub {
80 my ($self, $params) = @_;
81 if ( $params && !ref $params ) {
82 $self->_context->log->warn(
83 "Attempt to retrieve '$params' with req->params(), " .
84 "you probably meant to call req->param('$params')" );
96 return $self->path if $self->has_uri;
101 is => 'rw', clearer => '_clear_body', predicate => '_has_body',
103 # Eugh, ugly. Should just be able to rename accessor methods to 'body'
104 # and provide a custom reader..
107 $self->_context->prepare_body();
108 $self->_body(@_) if scalar @_;
109 return blessed $self->_body ? $self->_body->body : $self->_body;
118 gethostbyaddr( inet_aton( $self->address ), AF_INET ) || 'localhost'
122 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.
213 Arguments just get passed through and B<don't> get unescaped automatically, so
214 you should do that explicitly.
218 Shortcut for arguments.
222 Contains the URI base. This will always have a trailing slash.
224 If your application was queried with the URI
225 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
229 Returns the message body of the request, unless Content-Type is
230 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
232 =head2 $req->body_parameters
234 Returns a reference to a hash containing body (POST) parameters. Values can
235 be either a scalar or an arrayref containing scalars.
237 print $c->request->body_parameters->{field};
238 print $c->request->body_parameters->{field}->[0];
240 These are the parameters from the POST part of the request, if any.
242 =head2 $req->body_params
244 Shortcut for body_parameters.
246 =head2 $req->content_encoding
248 Shortcut for $req->headers->content_encoding.
250 =head2 $req->content_length
252 Shortcut for $req->headers->content_length.
254 =head2 $req->content_type
256 Shortcut for $req->headers->content_type.
260 A convenient method to access $req->cookies.
262 $cookie = $c->request->cookie('name');
263 @cookies = $c->request->cookie;
271 return keys %{ $self->cookies };
278 unless ( exists $self->cookies->{$name} ) {
282 return $self->cookies->{$name};
288 Returns a reference to a hash containing the cookies.
290 print $c->request->cookies->{mycookie}->value;
292 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
297 Shortcut for $req->headers->header.
301 Returns an L<HTTP::Headers> object containing the headers for the current request.
303 print $c->request->headers->header('X-Catalyst');
305 =head2 $req->hostname
307 Returns the hostname of the client.
311 Alias for $req->body.
313 =head2 $req->query_keywords
315 Contains the keywords portion of a query string, when no '=' signs are
318 http://localhost/path?some+keywords
320 $c->request->query_keywords will contain 'some keywords'
324 This contains the matching part of a Regex action. Otherwise
325 it returns the same as 'action', except for default actions,
326 which return an empty string.
330 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
334 Returns GET and POST parameters with a CGI.pm-compatible param method. This
335 is an alternative method for accessing parameters in $c->req->parameters.
337 $value = $c->request->param( 'foo' );
338 @values = $c->request->param( 'foo' );
339 @params = $c->request->param;
341 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
342 arguments to this method, like this:
344 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
346 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
347 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
348 (creating it if it didn't exist before), and C<quxx> as another value for
357 return keys %{ $self->parameters };
364 unless ( exists $self->parameters->{$param} ) {
365 return wantarray ? () : undef;
368 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
370 ? @{ $self->parameters->{$param} }
371 : $self->parameters->{$param}->[0];
375 ? ( $self->parameters->{$param} )
376 : $self->parameters->{$param};
381 $self->parameters->{$field} = [@_];
385 =head2 $req->parameters
387 Returns a reference to a hash containing GET and POST parameters. Values can
388 be either a scalar or an arrayref containing scalars.
390 print $c->request->parameters->{field};
391 print $c->request->parameters->{field}->[0];
393 This is the combination of C<query_parameters> and C<body_parameters>.
397 Shortcut for $req->parameters.
401 Returns the path, i.e. the part of the URI after $req->base, for the current request.
403 =head2 $req->path_info
405 Alias for path, added for compatibility with L<CGI>.
410 my ( $self, @params ) = @_;
413 $self->uri->path(@params);
416 elsif ( $self->_has_path ) {
420 my $path = $self->uri->path;
421 my $location = $self->base->path;
422 $path =~ s/^(\Q$location\E)?//;
430 =head2 $req->protocol
432 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
434 =head2 $req->query_parameters
436 =head2 $req->query_params
438 Returns a reference to a hash containing query string (GET) parameters. Values can
439 be either a scalar or an arrayref containing scalars.
441 print $c->request->query_parameters->{field};
442 print $c->request->query_parameters->{field}->[0];
444 =head2 $req->read( [$maxlength] )
446 Reads a chunk of data from the request body. This method is intended to be
447 used in a while loop, reading $maxlength bytes on every call. $maxlength
448 defaults to the size of the request if not specified.
450 You have to set MyApp->config->{parse_on_demand} to use this directly.
454 Shortcut for $req->headers->referer. Returns the referring page.
458 Returns true or false, indicating whether the connection is secure (https).
460 =head2 $req->captures
462 Returns a reference to an array containing captured args from chained
463 actions or regex captures.
465 my @captures = @{ $c->request->captures };
467 =head2 $req->snippets
469 C<captures> used to be called snippets. This is still available for backwards
470 compatibility, but is considered deprecated.
474 A convenient method to access $req->uploads.
476 $upload = $c->request->upload('field');
477 @uploads = $c->request->upload('field');
478 @fields = $c->request->upload;
480 for my $upload ( $c->request->upload('field') ) {
481 print $upload->filename;
490 return keys %{ $self->uploads };
497 unless ( exists $self->uploads->{$upload} ) {
498 return wantarray ? () : undef;
501 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
503 ? @{ $self->uploads->{$upload} }
504 : $self->uploads->{$upload}->[0];
508 ? ( $self->uploads->{$upload} )
509 : $self->uploads->{$upload};
515 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
517 if ( exists $self->uploads->{$field} ) {
518 for ( $self->uploads->{$field} ) {
519 $_ = [$_] unless ref($_) eq "ARRAY";
520 push( @$_, $upload );
524 $self->uploads->{$field} = $upload;
532 Returns a reference to a hash containing uploads. Values can be either a
533 L<Catalyst::Request::Upload> object, or an arrayref of
534 L<Catalyst::Request::Upload> objects.
536 my $upload = $c->request->uploads->{field};
537 my $upload = $c->request->uploads->{field}->[0];
541 Returns a URI object for the current request. Stringifies to the URI text.
543 =head2 $req->uri_with( { key => 'value' } );
545 Returns a rewritten URI object for the current request. Key/value pairs
546 passed in will override existing parameters. You can remove an existing
547 parameter by passing in an undef value. Unmodified pairs will be
553 my( $self, $args ) = @_;
555 carp( 'No arguments passed to uri_with()' ) unless $args;
557 foreach my $value ( values %$args ) {
558 next unless defined $value;
559 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
561 utf8::encode( $_ ) if utf8::is_utf8($_);
565 my $uri = $self->uri->clone;
566 my %query = ( %{ $uri->query_form_hash }, %$args );
569 # remove undef values
570 map { defined $query{ $_ } ? ( $_ => $query{ $_ } ) : () } keys %query
577 Returns the currently logged in user. Deprecated. The method recommended for
578 newer plugins is $c->user.
580 =head2 $req->user_agent
582 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
591 Catalyst Contributors, see Catalyst.pm
595 This program is free software, you can redistribute it and/or modify
596 it under the same terms as Perl itself.
600 __PACKAGE__->meta->make_immutable;