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 around parameters => sub {
80 my ($orig, $self, $params) = @_;
83 $self->_context->log->warn(
84 "Attempt to retrieve '$params' with req->params(), " .
85 "you probably meant to call req->param('$params')"
89 return $self->$orig($params);
100 return $self->path if $self->has_uri;
105 is => 'rw', clearer => '_clear_body', predicate => '_has_body',
107 # Eugh, ugly. Should just be able to rename accessor methods to 'body'
108 # and provide a custom reader..
111 $self->_context->prepare_body();
112 $self->_body(@_) if scalar @_;
113 return blessed $self->_body ? $self->_body->body : $self->_body;
122 gethostbyaddr( inet_aton( $self->address ), AF_INET ) || 'localhost'
126 has _path => ( is => 'rw', predicate => '_has_path', clearer => '_clear_path' );
128 sub args { shift->arguments(@_) }
129 sub body_params { shift->body_parameters(@_) }
130 sub input { shift->body(@_) }
131 sub params { shift->parameters(@_) }
132 sub query_params { shift->query_parameters(@_) }
133 sub path_info { shift->path(@_) }
134 sub snippets { shift->captures(@_) }
138 Catalyst::Request - provides information about the current client request
149 $req->body_parameters;
150 $req->content_encoding;
151 $req->content_length;
159 $req->query_keywords;
167 $req->query_parameters;
171 $req->captures; # previously knows as snippets
178 See also L<Catalyst>, L<Catalyst::Request::Upload>.
182 This is the Catalyst Request class, which provides an interface to data for the
183 current client request. The request object is prepared by L<Catalyst::Engine>,
184 thus hiding the details of the particular engine implementation.
190 [DEPRECATED] Returns the name of the requested action.
193 Use C<< $c->action >> instead (which returns a
194 L<Catalyst::Action|Catalyst::Action> object).
198 Returns the IP address of the client.
200 =head2 $req->arguments
202 Returns a reference to an array containing the arguments.
204 print $c->request->arguments->[0];
206 For example, if your action was
208 package MyApp::C::Foo;
214 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
215 would be the first and only argument.
217 Arguments just get passed through and B<don't> get unescaped automatically, so
218 you should do that explicitly.
222 Shortcut for arguments.
226 Contains the URI base. This will always have a trailing slash.
228 If your application was queried with the URI
229 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
233 Returns the message body of the request, unless Content-Type is
234 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
236 =head2 $req->body_parameters
238 Returns a reference to a hash containing body (POST) parameters. Values can
239 be either a scalar or an arrayref containing scalars.
241 print $c->request->body_parameters->{field};
242 print $c->request->body_parameters->{field}->[0];
244 These are the parameters from the POST part of the request, if any.
246 =head2 $req->body_params
248 Shortcut for body_parameters.
250 =head2 $req->content_encoding
252 Shortcut for $req->headers->content_encoding.
254 =head2 $req->content_length
256 Shortcut for $req->headers->content_length.
258 =head2 $req->content_type
260 Shortcut for $req->headers->content_type.
264 A convenient method to access $req->cookies.
266 $cookie = $c->request->cookie('name');
267 @cookies = $c->request->cookie;
275 return keys %{ $self->cookies };
282 unless ( exists $self->cookies->{$name} ) {
286 return $self->cookies->{$name};
292 Returns a reference to a hash containing the cookies.
294 print $c->request->cookies->{mycookie}->value;
296 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
301 Shortcut for $req->headers->header.
305 Returns an L<HTTP::Headers> object containing the headers for the current request.
307 print $c->request->headers->header('X-Catalyst');
309 =head2 $req->hostname
311 Returns the hostname of the client.
315 Alias for $req->body.
317 =head2 $req->query_keywords
319 Contains the keywords portion of a query string, when no '=' signs are
322 http://localhost/path?some+keywords
324 $c->request->query_keywords will contain 'some keywords'
328 This contains the matching part of a Regex action. Otherwise
329 it returns the same as 'action', except for default actions,
330 which return an empty string.
334 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
338 Returns GET and POST parameters with a CGI.pm-compatible param method. This
339 is an alternative method for accessing parameters in $c->req->parameters.
341 $value = $c->request->param( 'foo' );
342 @values = $c->request->param( 'foo' );
343 @params = $c->request->param;
345 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
346 arguments to this method, like this:
348 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
350 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
351 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
352 (creating it if it didn't exist before), and C<quxx> as another value for
361 return keys %{ $self->parameters };
368 unless ( exists $self->parameters->{$param} ) {
369 return wantarray ? () : undef;
372 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
374 ? @{ $self->parameters->{$param} }
375 : $self->parameters->{$param}->[0];
379 ? ( $self->parameters->{$param} )
380 : $self->parameters->{$param};
385 $self->parameters->{$field} = [@_];
389 =head2 $req->parameters
391 Returns a reference to a hash containing GET and POST parameters. Values can
392 be either a scalar or an arrayref containing scalars.
394 print $c->request->parameters->{field};
395 print $c->request->parameters->{field}->[0];
397 This is the combination of C<query_parameters> and C<body_parameters>.
401 Shortcut for $req->parameters.
405 Returns the path, i.e. the part of the URI after $req->base, for the current request.
407 =head2 $req->path_info
409 Alias for path, added for compatibility with L<CGI>.
414 my ( $self, @params ) = @_;
417 $self->uri->path(@params);
420 elsif ( $self->_has_path ) {
424 my $path = $self->uri->path;
425 my $location = $self->base->path;
426 $path =~ s/^(\Q$location\E)?//;
434 =head2 $req->protocol
436 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
438 =head2 $req->query_parameters
440 =head2 $req->query_params
442 Returns a reference to a hash containing query string (GET) parameters. Values can
443 be either a scalar or an arrayref containing scalars.
445 print $c->request->query_parameters->{field};
446 print $c->request->query_parameters->{field}->[0];
448 =head2 $req->read( [$maxlength] )
450 Reads a chunk of data from the request body. This method is intended to be
451 used in a while loop, reading $maxlength bytes on every call. $maxlength
452 defaults to the size of the request if not specified.
454 You have to set MyApp->config->{parse_on_demand} to use this directly.
458 Shortcut for $req->headers->referer. Returns the referring page.
462 Returns true or false, indicating whether the connection is secure (https).
464 =head2 $req->captures
466 Returns a reference to an array containing captured args from chained
467 actions or regex captures.
469 my @captures = @{ $c->request->captures };
471 =head2 $req->snippets
473 C<captures> used to be called snippets. This is still available for backwards
474 compatibility, but is considered deprecated.
478 A convenient method to access $req->uploads.
480 $upload = $c->request->upload('field');
481 @uploads = $c->request->upload('field');
482 @fields = $c->request->upload;
484 for my $upload ( $c->request->upload('field') ) {
485 print $upload->filename;
494 return keys %{ $self->uploads };
501 unless ( exists $self->uploads->{$upload} ) {
502 return wantarray ? () : undef;
505 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
507 ? @{ $self->uploads->{$upload} }
508 : $self->uploads->{$upload}->[0];
512 ? ( $self->uploads->{$upload} )
513 : $self->uploads->{$upload};
519 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
521 if ( exists $self->uploads->{$field} ) {
522 for ( $self->uploads->{$field} ) {
523 $_ = [$_] unless ref($_) eq "ARRAY";
524 push( @$_, $upload );
528 $self->uploads->{$field} = $upload;
536 Returns a reference to a hash containing uploads. Values can be either a
537 L<Catalyst::Request::Upload> object, or an arrayref of
538 L<Catalyst::Request::Upload> objects.
540 my $upload = $c->request->uploads->{field};
541 my $upload = $c->request->uploads->{field}->[0];
545 Returns a URI object for the current request. Stringifies to the URI text.
547 =head2 $req->uri_with( { key => 'value' } );
549 Returns a rewritten URI object for the current request. Key/value pairs
550 passed in will override existing parameters. You can remove an existing
551 parameter by passing in an undef value. Unmodified pairs will be
557 my( $self, $args ) = @_;
559 carp( 'No arguments passed to uri_with()' ) unless $args;
561 foreach my $value ( values %$args ) {
562 next unless defined $value;
563 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
565 utf8::encode( $_ ) if utf8::is_utf8($_);
569 my $uri = $self->uri->clone;
570 my %query = ( %{ $uri->query_form_hash }, %$args );
573 # remove undef values
574 map { defined $query{ $_ } ? ( $_ => $query{ $_ } ) : () } keys %query
581 Returns the currently logged in user. Deprecated. The method recommended for
582 newer plugins is $c->user.
584 =head2 $req->user_agent
586 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
595 Catalyst Contributors, see Catalyst.pm
599 This program is free software, you can redistribute it and/or modify
600 it under the same terms as Perl itself.
604 __PACKAGE__->meta->make_immutable;