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() },
43 clearer => '_clear_context',
46 has body_parameters => (
50 default => sub { {} },
56 default => sub { {} },
63 default => sub { {} },
67 # - Can we lose the before modifiers which just call prepare_body ?
68 # they are wasteful, slow us down and feel cluttery.
70 # Can we make _body an attribute, have the rest of
71 # these lazy build from there and kill all the direct hash access
72 # in Catalyst.pm and Engine.pm?
76 my $context = $self->_context || return;
77 $context->prepare_body;
78 } for qw/parameters body_parameters/;
80 around parameters => sub {
81 my ($orig, $self, $params) = @_;
84 $self->_context->log->warn(
85 "Attempt to retrieve '$params' with req->params(), " .
86 "you probably meant to call req->param('$params')"
90 return $self->$orig($params);
101 return $self->path if $self->has_uri;
106 is => 'rw', clearer => '_clear_body', predicate => '_has_body',
108 # Eugh, ugly. Should just be able to rename accessor methods to 'body'
109 # and provide a custom reader..
112 $self->_context->prepare_body();
113 $self->_body(@_) if scalar @_;
114 return blessed $self->_body ? $self->_body->body : $self->_body;
123 gethostbyaddr( inet_aton( $self->address ), AF_INET ) || 'localhost'
127 has _path => ( is => 'rw', predicate => '_has_path', clearer => '_clear_path' );
129 sub args { shift->arguments(@_) }
130 sub body_params { shift->body_parameters(@_) }
131 sub input { shift->body(@_) }
132 sub params { shift->parameters(@_) }
133 sub query_params { shift->query_parameters(@_) }
134 sub path_info { shift->path(@_) }
135 sub snippets { shift->captures(@_) }
139 Catalyst::Request - provides information about the current client request
150 $req->body_parameters;
151 $req->content_encoding;
152 $req->content_length;
160 $req->query_keywords;
168 $req->query_parameters;
172 $req->captures; # previously knows as snippets
179 See also L<Catalyst>, L<Catalyst::Request::Upload>.
183 This is the Catalyst Request class, which provides an interface to data for the
184 current client request. The request object is prepared by L<Catalyst::Engine>,
185 thus hiding the details of the particular engine implementation.
191 [DEPRECATED] Returns the name of the requested action.
194 Use C<< $c->action >> instead (which returns a
195 L<Catalyst::Action|Catalyst::Action> object).
199 Returns the IP address of the client.
201 =head2 $req->arguments
203 Returns a reference to an array containing the arguments.
205 print $c->request->arguments->[0];
207 For example, if your action was
209 package MyApp::C::Foo;
215 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
216 would be the first and only argument.
218 Arguments just get passed through and B<don't> get unescaped automatically, so
219 you should do that explicitly.
223 Shortcut for arguments.
227 Contains the URI base. This will always have a trailing slash. Note that the
228 URI scheme (eg., http vs. https) must be determined through heuristics;
229 depending on your server configuration, it may be incorrect. See $req->secure
232 If your application was queried with the URI
233 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
237 Returns the message body of the request, unless Content-Type is
238 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
240 =head2 $req->body_parameters
242 Returns a reference to a hash containing body (POST) parameters. Values can
243 be either a scalar or an arrayref containing scalars.
245 print $c->request->body_parameters->{field};
246 print $c->request->body_parameters->{field}->[0];
248 These are the parameters from the POST part of the request, if any.
250 =head2 $req->body_params
252 Shortcut for body_parameters.
254 =head2 $req->content_encoding
256 Shortcut for $req->headers->content_encoding.
258 =head2 $req->content_length
260 Shortcut for $req->headers->content_length.
262 =head2 $req->content_type
264 Shortcut for $req->headers->content_type.
268 A convenient method to access $req->cookies.
270 $cookie = $c->request->cookie('name');
271 @cookies = $c->request->cookie;
279 return keys %{ $self->cookies };
286 unless ( exists $self->cookies->{$name} ) {
290 return $self->cookies->{$name};
296 Returns a reference to a hash containing the cookies.
298 print $c->request->cookies->{mycookie}->value;
300 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
305 Shortcut for $req->headers->header.
309 Returns an L<HTTP::Headers> object containing the headers for the current request.
311 print $c->request->headers->header('X-Catalyst');
313 =head2 $req->hostname
315 Returns the hostname of the client.
319 Alias for $req->body.
321 =head2 $req->query_keywords
323 Contains the keywords portion of a query string, when no '=' signs are
326 http://localhost/path?some+keywords
328 $c->request->query_keywords will contain 'some keywords'
332 This contains the matching part of a Regex action. Otherwise
333 it returns the same as 'action', except for default actions,
334 which return an empty string.
338 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
342 Returns GET and POST parameters with a CGI.pm-compatible param method. This
343 is an alternative method for accessing parameters in $c->req->parameters.
345 $value = $c->request->param( 'foo' );
346 @values = $c->request->param( 'foo' );
347 @params = $c->request->param;
349 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
350 arguments to this method, like this:
352 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
354 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
355 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
356 (creating it if it didn't exist before), and C<quxx> as another value for
365 return keys %{ $self->parameters };
372 unless ( exists $self->parameters->{$param} ) {
373 return wantarray ? () : undef;
376 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
378 ? @{ $self->parameters->{$param} }
379 : $self->parameters->{$param}->[0];
383 ? ( $self->parameters->{$param} )
384 : $self->parameters->{$param};
389 $self->parameters->{$field} = [@_];
393 =head2 $req->parameters
395 Returns a reference to a hash containing GET and POST parameters. Values can
396 be either a scalar or an arrayref containing scalars.
398 print $c->request->parameters->{field};
399 print $c->request->parameters->{field}->[0];
401 This is the combination of C<query_parameters> and C<body_parameters>.
405 Shortcut for $req->parameters.
409 Returns the path, i.e. the part of the URI after $req->base, for the current request.
411 =head2 $req->path_info
413 Alias for path, added for compatibility with L<CGI>.
418 my ( $self, @params ) = @_;
421 $self->uri->path(@params);
424 elsif ( $self->_has_path ) {
428 my $path = $self->uri->path;
429 my $location = $self->base->path;
430 $path =~ s/^(\Q$location\E)?//;
438 =head2 $req->protocol
440 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
442 =head2 $req->query_parameters
444 =head2 $req->query_params
446 Returns a reference to a hash containing query string (GET) parameters. Values can
447 be either a scalar or an arrayref containing scalars.
449 print $c->request->query_parameters->{field};
450 print $c->request->query_parameters->{field}->[0];
452 =head2 $req->read( [$maxlength] )
454 Reads a chunk of data from the request body. This method is intended to be
455 used in a while loop, reading $maxlength bytes on every call. $maxlength
456 defaults to the size of the request if not specified.
458 You have to set MyApp->config->{parse_on_demand} to use this directly.
462 Shortcut for $req->headers->referer. Returns the referring page.
466 Returns true or false, indicating whether the connection is secure
467 (https). Note that the URI scheme (eg., http vs. https) must be determined
468 through heuristics, and therefore the reliablity of $req->secure will depend
469 on your server configuration. If you are serving secure pages on the standard
470 SSL port (443) and/or setting the HTTPS environment variable, $req->secure
473 =head2 $req->captures
475 Returns a reference to an array containing captured args from chained
476 actions or regex captures.
478 my @captures = @{ $c->request->captures };
480 =head2 $req->snippets
482 C<captures> used to be called snippets. This is still available for backwards
483 compatibility, but is considered deprecated.
487 A convenient method to access $req->uploads.
489 $upload = $c->request->upload('field');
490 @uploads = $c->request->upload('field');
491 @fields = $c->request->upload;
493 for my $upload ( $c->request->upload('field') ) {
494 print $upload->filename;
503 return keys %{ $self->uploads };
510 unless ( exists $self->uploads->{$upload} ) {
511 return wantarray ? () : undef;
514 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
516 ? @{ $self->uploads->{$upload} }
517 : $self->uploads->{$upload}->[0];
521 ? ( $self->uploads->{$upload} )
522 : $self->uploads->{$upload};
528 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
530 if ( exists $self->uploads->{$field} ) {
531 for ( $self->uploads->{$field} ) {
532 $_ = [$_] unless ref($_) eq "ARRAY";
533 push( @$_, $upload );
537 $self->uploads->{$field} = $upload;
545 Returns a reference to a hash containing uploads. Values can be either a
546 L<Catalyst::Request::Upload> object, or an arrayref of
547 L<Catalyst::Request::Upload> objects.
549 my $upload = $c->request->uploads->{field};
550 my $upload = $c->request->uploads->{field}->[0];
554 Returns a URI object for the current request. Stringifies to the URI text.
556 =head2 $req->uri_with( { key => 'value' } );
558 Returns a rewritten URI object for the current request. Key/value pairs
559 passed in will override existing parameters. You can remove an existing
560 parameter by passing in an undef value. Unmodified pairs will be
566 my( $self, $args ) = @_;
568 carp( 'No arguments passed to uri_with()' ) unless $args;
570 foreach my $value ( values %$args ) {
571 next unless defined $value;
572 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
574 utf8::encode( $_ ) if utf8::is_utf8($_);
578 my $uri = $self->uri->clone;
579 my %query = ( %{ $uri->query_form_hash }, %$args );
582 # remove undef values
583 map { defined $query{ $_ } ? ( $_ => $query{ $_ } ) : () } keys %query
590 Returns the currently logged in user. Deprecated. The method recommended for
591 newer plugins is $c->user.
593 =head2 $req->user_agent
595 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
604 Catalyst Contributors, see Catalyst.pm
608 This program is free software, you can redistribute it and/or modify
609 it under the same terms as Perl itself.
613 __PACKAGE__->meta->make_immutable;