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 env => (is => 'ro', writer => '_set_env');
19 has _read_position => ( is => 'rw', default => 0 );
20 has _read_length => ( is => 'ro',
23 $self->header('Content-Length') || 0;
28 has action => (is => 'rw');
29 has address => (is => 'rw');
30 has arguments => (is => 'rw', default => sub { [] });
31 has cookies => (is => 'rw', default => sub { {} });
32 has query_keywords => (is => 'rw');
33 has match => (is => 'rw');
34 has method => (is => 'rw');
35 has protocol => (is => 'rw');
36 has query_parameters => (is => 'rw', default => sub { {} });
37 has secure => (is => 'rw', default => 0);
38 has captures => (is => 'rw', default => sub { [] });
39 has uri => (is => 'rw', predicate => 'has_uri');
40 has remote_user => (is => 'rw');
43 isa => 'HTTP::Headers',
44 handles => [qw(content_encoding content_length content_type header referer user_agent)],
45 default => sub { HTTP::Headers->new() },
53 clearer => '_clear_context',
56 # Amount of data to read from input on each pass
57 our $CHUNKSIZE = 64 * 1024;
60 my ($self, $maxlength) = @_;
61 my $remaining = $self->_read_length - $self->_read_position;
62 $maxlength ||= $CHUNKSIZE;
64 # Are we done reading?
65 if ( $remaining <= 0 ) {
69 my $readlen = ( $remaining > $maxlength ) ? $maxlength : $remaining;
70 my $rc = $self->read_chunk( my $buffer, $readlen );
72 if (0 == $rc) { # Nothing more to read even though Content-Length
73 # said there should be.
76 $self->_read_position( $self->_read_position + $rc );
80 Catalyst::Exception->throw(
81 message => "Unknown error reading input: $!" );
87 return $self->env->{'psgi.input'}->read(@_);
90 has body_parameters => (
94 default => sub { {} },
100 default => sub { {} },
107 default => sub { {} },
111 # - Can we lose the before modifiers which just call prepare_body ?
112 # they are wasteful, slow us down and feel cluttery.
114 # Can we make _body an attribute, have the rest of
115 # these lazy build from there and kill all the direct hash access
116 # in Catalyst.pm and Engine.pm?
120 my $context = $self->_context || return;
121 $context->prepare_body;
122 } for qw/parameters body_parameters/;
124 around parameters => sub {
125 my ($orig, $self, $params) = @_;
127 if ( !ref $params ) {
128 $self->_context->log->warn(
129 "Attempt to retrieve '$params' with req->params(), " .
130 "you probably meant to call req->param('$params')"
134 return $self->$orig($params);
145 return $self->path if $self->has_uri;
150 is => 'rw', clearer => '_clear_body', predicate => '_has_body',
152 # Eugh, ugly. Should just be able to rename accessor methods to 'body'
153 # and provide a custom reader..
156 $self->_context->prepare_body();
157 croak 'body is a reader' if scalar @_;
158 return blessed $self->_body ? $self->_body->body : $self->_body;
167 gethostbyaddr( inet_aton( $self->address ), AF_INET ) || $self->address
171 has _path => ( is => 'rw', predicate => '_has_path', clearer => '_clear_path' );
173 # XXX: Deprecated in docs ages ago (2006), deprecated with warning in 5.8000 due
174 # to confusion between Engines and Plugin::Authentication. Remove in 5.8100?
175 has user => (is => 'rw');
177 sub args { shift->arguments(@_) }
178 sub body_params { shift->body_parameters(@_) }
179 sub input { shift->body(@_) }
180 sub params { shift->parameters(@_) }
181 sub query_params { shift->query_parameters(@_) }
182 sub path_info { shift->path(@_) }
183 sub snippets { shift->captures(@_) }
185 =for stopwords param params
189 Catalyst::Request - provides information about the current client request
200 $req->body_parameters;
201 $req->content_encoding;
202 $req->content_length;
210 $req->query_keywords;
218 $req->query_parameters;
222 $req->captures; # previously knows as snippets
229 See also L<Catalyst>, L<Catalyst::Request::Upload>.
233 This is the Catalyst Request class, which provides an interface to data for the
234 current client request. The request object is prepared by L<Catalyst::Engine>,
235 thus hiding the details of the particular engine implementation.
241 [DEPRECATED] Returns the name of the requested action.
244 Use C<< $c->action >> instead (which returns a
245 L<Catalyst::Action|Catalyst::Action> object).
249 Returns the IP address of the client.
251 =head2 $req->arguments
253 Returns a reference to an array containing the arguments.
255 print $c->request->arguments->[0];
257 For example, if your action was
259 package MyApp::Controller::Foo;
265 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
266 would be the first and only argument.
268 Arguments get automatically URI-unescaped for you.
272 Shortcut for L</arguments>.
276 Contains the URI base. This will always have a trailing slash. Note that the
277 URI scheme (e.g., http vs. https) must be determined through heuristics;
278 depending on your server configuration, it may be incorrect. See $req->secure
281 If your application was queried with the URI
282 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
286 Returns the message body of the request, as returned by L<HTTP::Body>: a string,
287 unless Content-Type is C<application/x-www-form-urlencoded>, C<text/xml>, or
288 C<multipart/form-data>, in which case a L<File::Temp> object is returned.
290 =head2 $req->body_parameters
292 Returns a reference to a hash containing body (POST) parameters. Values can
293 be either a scalar or an arrayref containing scalars.
295 print $c->request->body_parameters->{field};
296 print $c->request->body_parameters->{field}->[0];
298 These are the parameters from the POST part of the request, if any.
300 =head2 $req->body_params
302 Shortcut for body_parameters.
304 =head2 $req->content_encoding
306 Shortcut for $req->headers->content_encoding.
308 =head2 $req->content_length
310 Shortcut for $req->headers->content_length.
312 =head2 $req->content_type
314 Shortcut for $req->headers->content_type.
318 A convenient method to access $req->cookies.
320 $cookie = $c->request->cookie('name');
321 @cookies = $c->request->cookie;
329 return keys %{ $self->cookies };
336 unless ( exists $self->cookies->{$name} ) {
340 return $self->cookies->{$name};
346 Returns a reference to a hash containing the cookies.
348 print $c->request->cookies->{mycookie}->value;
350 The cookies in the hash are indexed by name, and the values are L<CGI::Simple::Cookie>
355 Shortcut for $req->headers->header.
359 Returns an L<HTTP::Headers> object containing the headers for the current request.
361 print $c->request->headers->header('X-Catalyst');
363 =head2 $req->hostname
365 Returns the hostname of the client. Use C<< $req->uri->host >> to get the hostname of the server.
369 Alias for $req->body.
371 =head2 $req->query_keywords
373 Contains the keywords portion of a query string, when no '=' signs are
376 http://localhost/path?some+keywords
378 $c->request->query_keywords will contain 'some keywords'
382 This contains the matching part of a Regex action. Otherwise
383 it returns the same as 'action', except for default actions,
384 which return an empty string.
388 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
392 Returns GET and POST parameters with a CGI.pm-compatible param method. This
393 is an alternative method for accessing parameters in $c->req->parameters.
395 $value = $c->request->param( 'foo' );
396 @values = $c->request->param( 'foo' );
397 @params = $c->request->param;
399 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
400 arguments to this method, like this:
402 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
404 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
405 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
406 (creating it if it didn't exist before), and C<quxx> as another value for
409 B<NOTE> this is considered a legacy interface and care should be taken when
410 using it. C<< scalar $c->req->param( 'foo' ) >> will return only the first
411 C<foo> param even if multiple are present; C<< $c->req->param( 'foo' ) >> will
412 return a list of as many are present, which can have unexpected consequences
413 when writing code of the form:
417 baz => $c->req->param( 'baz' ),
420 If multiple C<baz> parameters are provided this code might corrupt data or
421 cause a hash initialization error. For a more straightforward interface see
422 C<< $c->req->parameters >>.
430 return keys %{ $self->parameters };
437 unless ( exists $self->parameters->{$param} ) {
438 return wantarray ? () : undef;
441 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
443 ? @{ $self->parameters->{$param} }
444 : $self->parameters->{$param}->[0];
448 ? ( $self->parameters->{$param} )
449 : $self->parameters->{$param};
454 $self->parameters->{$field} = [@_];
458 =head2 $req->parameters
460 Returns a reference to a hash containing GET and POST parameters. Values can
461 be either a scalar or an arrayref containing scalars.
463 print $c->request->parameters->{field};
464 print $c->request->parameters->{field}->[0];
466 This is the combination of C<query_parameters> and C<body_parameters>.
470 Shortcut for $req->parameters.
474 Returns the path, i.e. the part of the URI after $req->base, for the current request.
476 http://localhost/path/foo
478 $c->request->path will contain 'path/foo'
480 =head2 $req->path_info
482 Alias for path, added for compatibility with L<CGI>.
487 my ( $self, @params ) = @_;
490 $self->uri->path(@params);
493 elsif ( $self->_has_path ) {
497 my $path = $self->uri->path;
498 my $location = $self->base->path;
499 $path =~ s/^(\Q$location\E)?//;
507 =head2 $req->protocol
509 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
511 =head2 $req->query_parameters
513 =head2 $req->query_params
515 Returns a reference to a hash containing query string (GET) parameters. Values can
516 be either a scalar or an arrayref containing scalars.
518 print $c->request->query_parameters->{field};
519 print $c->request->query_parameters->{field}->[0];
521 =head2 $req->read( [$maxlength] )
523 Reads a chunk of data from the request body. This method is intended to be
524 used in a while loop, reading $maxlength bytes on every call. $maxlength
525 defaults to the size of the request if not specified.
527 =head2 $req->read_chunk(\$buff, $max)
531 You have to set MyApp->config(parse_on_demand => 1) to use this directly.
535 Shortcut for $req->headers->referer. Returns the referring page.
539 Returns true or false, indicating whether the connection is secure
540 (https). Note that the URI scheme (e.g., http vs. https) must be determined
541 through heuristics, and therefore the reliability of $req->secure will depend
542 on your server configuration. If you are serving secure pages on the standard
543 SSL port (443) and/or setting the HTTPS environment variable, $req->secure
546 =head2 $req->captures
548 Returns a reference to an array containing captured args from chained
549 actions or regex captures.
551 my @captures = @{ $c->request->captures };
553 =head2 $req->snippets
555 C<captures> used to be called snippets. This is still available for backwards
556 compatibility, but is considered deprecated.
560 A convenient method to access $req->uploads.
562 $upload = $c->request->upload('field');
563 @uploads = $c->request->upload('field');
564 @fields = $c->request->upload;
566 for my $upload ( $c->request->upload('field') ) {
567 print $upload->filename;
576 return keys %{ $self->uploads };
583 unless ( exists $self->uploads->{$upload} ) {
584 return wantarray ? () : undef;
587 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
589 ? @{ $self->uploads->{$upload} }
590 : $self->uploads->{$upload}->[0];
594 ? ( $self->uploads->{$upload} )
595 : $self->uploads->{$upload};
601 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
603 if ( exists $self->uploads->{$field} ) {
604 for ( $self->uploads->{$field} ) {
605 $_ = [$_] unless ref($_) eq "ARRAY";
606 push( @$_, $upload );
610 $self->uploads->{$field} = $upload;
618 Returns a reference to a hash containing uploads. Values can be either a
619 L<Catalyst::Request::Upload> object, or an arrayref of
620 L<Catalyst::Request::Upload> objects.
622 my $upload = $c->request->uploads->{field};
623 my $upload = $c->request->uploads->{field}->[0];
627 Returns a L<URI> object for the current request. Stringifies to the URI text.
629 =head2 $req->mangle_params( { key => 'value' }, $appendmode);
631 Returns a hashref of parameters stemming from the current request's params,
632 plus the ones supplied. Keys for which no current param exists will be
633 added, keys with undefined values will be removed and keys with existing
634 params will be replaced. Note that you can supply a true value as the final
635 argument to change behavior with regards to existing parameters, appending
636 values rather than replacing them.
640 # URI query params foo=1
641 my $hashref = $req->mangle_params({ foo => 2 });
642 # Result is query params of foo=2
646 # URI query params foo=1
647 my $hashref = $req->mangle_params({ foo => 2 }, 1);
648 # Result is query params of foo=1&foo=2
650 This is the code behind C<uri_with>.
655 my ($self, $args, $append) = @_;
657 carp('No arguments passed to mangle_params()') unless $args;
659 foreach my $value ( values %$args ) {
660 next unless defined $value;
661 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
663 utf8::encode( $_ ) if utf8::is_utf8($_);
667 my %params = %{ $self->uri->query_form_hash };
668 foreach my $key (keys %{ $args }) {
669 my $val = $args->{$key};
672 if($append && exists($params{$key})) {
674 # This little bit of heaven handles appending a new value onto
675 # an existing one regardless if the existing value is an array
676 # or not, and regardless if the new value is an array or not
678 ref($params{$key}) eq 'ARRAY' ? @{ $params{$key} } : $params{$key},
679 ref($val) eq 'ARRAY' ? @{ $val } : $val
683 $params{$key} = $val;
687 # If the param wasn't defined then we delete it.
688 delete($params{$key});
696 =head2 $req->uri_with( { key => 'value' } );
698 Returns a rewritten URI object for the current request. Key/value pairs
699 passed in will override existing parameters. You can remove an existing
700 parameter by passing in an undef value. Unmodified pairs will be
703 You may also pass an optional second parameter that puts C<uri_with> into
706 $req->uri_with( { key => 'value' }, { mode => 'append' } );
708 See C<mangle_params> for an explanation of this behavior.
713 my( $self, $args, $behavior) = @_;
715 carp( 'No arguments passed to uri_with()' ) unless $args;
718 if((ref($behavior) eq 'HASH') && defined($behavior->{mode}) && ($behavior->{mode} eq 'append')) {
722 my $params = $self->mangle_params($args, $append);
724 my $uri = $self->uri->clone;
725 $uri->query_form($params);
730 =head2 $req->remote_user
732 Returns the value of the C<REMOTE_USER> environment variable.
734 =head2 $req->user_agent
736 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
745 Catalyst Contributors, see Catalyst.pm
749 This library is free software. You can redistribute it and/or modify
750 it under the same terms as Perl itself.
754 __PACKAGE__->meta->make_immutable;