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', predicate => 'has_env');
18 # XXX Deprecated crap here - warn?
19 has action => (is => 'rw');
20 # XXX: Deprecated in docs ages ago (2006), deprecated with warning in 5.8000 due
21 # to confusion between Engines and Plugin::Authentication. Remove in 5.8100?
22 has user => (is => 'rw');
23 sub snippets { shift->captures(@_) }
25 has _read_position => (
26 # FIXME: work around Moose bug RT#75367
29 writer => '_set_read_position',
33 # FIXME: work around Moose bug RT#75367
38 $self->header('Content-Length') || 0;
43 has address => (is => 'rw');
44 has arguments => (is => 'rw', default => sub { [] });
45 has cookies => (is => 'ro', builder => 'prepare_cookies', lazy => 1);
50 if ( my $header = $self->header('Cookie') ) {
51 return { CGI::Simple::Cookie->parse($header) };
56 has query_keywords => (is => 'rw');
57 has match => (is => 'rw');
58 has method => (is => 'rw');
59 has protocol => (is => 'rw');
60 has query_parameters => (is => 'rw', default => sub { {} });
61 has secure => (is => 'rw', default => 0);
62 has captures => (is => 'rw', default => sub { [] });
63 has uri => (is => 'rw', predicate => 'has_uri');
64 has remote_user => (is => 'rw');
67 isa => 'HTTP::Headers',
68 handles => [qw(content_encoding content_length content_type header referer user_agent)],
69 builder => 'prepare_headers',
77 my $headers = HTTP::Headers->new();
79 for my $header (keys %{ $env }) {
80 next unless $header =~ /^(HTTP|CONTENT|COOKIE)/i;
81 (my $field = $header) =~ s/^HTTPS?_//;
83 $headers->header($field => $env->{$header});
96 predicate=>'has_io_fh',
98 builder=>'_build_io_fh');
102 return $self->env->{'psgix.io'}
103 || die "Your Server does not support psgix.io";
106 has data_handlers => ( is=>'ro', isa=>'HashRef', default=>sub { +{} } );
111 builder=>'_build_body_data');
113 sub _build_body_data {
115 my $content_type = $self->content_type;
116 my ($match) = grep { $content_type =~/$_/i }
117 keys(%{$self->data_handlers});
120 my $fh = $self->body;
122 return $self->data_handlers->{$match}->($fh, $self);
128 # Amount of data to read from input on each pass
129 our $CHUNKSIZE = 64 * 1024;
132 my ($self, $maxlength) = @_;
133 my $remaining = $self->_read_length - $self->_read_position;
134 $maxlength ||= $CHUNKSIZE;
136 # Are we done reading?
137 if ( $remaining <= 0 ) {
141 my $readlen = ( $remaining > $maxlength ) ? $maxlength : $remaining;
142 my $rc = $self->read_chunk( my $buffer, $readlen );
144 if (0 == $rc) { # Nothing more to read even though Content-Length
145 # said there should be.
148 $self->_set_read_position( $self->_read_position + $rc );
152 Catalyst::Exception->throw(
153 message => "Unknown error reading input: $!" );
159 return $self->env->{'psgi.input'}->read(@_);
162 has body_parameters => (
166 builder => 'prepare_body_parameters',
172 default => sub { {} },
178 builder => '_build_parameters',
179 clearer => '_clear_parameters',
183 # - Can we lose the before modifiers which just call prepare_body ?
184 # they are wasteful, slow us down and feel cluttery.
186 # Can we make _body an attribute, have the rest of
187 # these lazy build from there and kill all the direct hash access
188 # in Catalyst.pm and Engine.pm?
190 sub prepare_parameters {
192 $self->_clear_parameters;
193 return $self->parameters;
196 sub _build_parameters {
199 my $body_parameters = $self->body_parameters;
200 my $query_parameters = $self->query_parameters;
201 # We copy, no references
202 foreach my $name (keys %$query_parameters) {
203 my $param = $query_parameters->{$name};
204 $parameters->{$name} = ref $param eq 'ARRAY' ? [ @$param ] : $param;
207 # Merge query and body parameters
208 foreach my $name (keys %$body_parameters) {
209 my $param = $body_parameters->{$name};
210 my @values = ref $param eq 'ARRAY' ? @$param : ($param);
211 if ( my $existing = $parameters->{$name} ) {
212 unshift(@values, (ref $existing eq 'ARRAY' ? @$existing : $existing));
214 $parameters->{$name} = @values > 1 ? \@values : $values[0];
221 predicate => '_has_uploadtmp',
227 #warn "XXX ${\$self->_uploadtmp}" if $self->_has_uploadtmp;
229 if(my $plack_body = $self->env->{'plack.request.http.body'}) {
231 $self->_body($plack_body);
232 $self->_body->cleanup(1); # Make extra sure!
233 $self->_body->tmpdir( $self->_uploadtmp )
234 if $self->_has_uploadtmp;
239 if ( my $length = $self->_read_length ) {
240 unless ( $self->_body ) {
241 my $type = $self->header('Content-Type');
242 $self->_body(HTTP::Body->new( $type, $length ));
243 $self->_body->cleanup(1); # Make extra sure!
244 $self->_body->tmpdir( $self->_uploadtmp )
245 if $self->_has_uploadtmp;
248 # Check for definedness as you could read '0'
249 while ( defined ( my $buffer = $self->read() ) ) {
250 $self->prepare_body_chunk($buffer);
253 # paranoia against wrong Content-Length header
254 my $remaining = $length - $self->_read_position;
255 if ( $remaining > 0 ) {
256 Catalyst::Exception->throw(
257 "Wrong Content-Length value: $length" );
261 # Defined but will cause all body code to be skipped
266 sub prepare_bodyXXX {
268 if(my $plack_body = $self->env->{'plack.request.http.body'}) {
275 die "XXX ${\$self->_uploadtmp}" x1000; $self->_has_uploadtmp;
277 if ( my $length = $self->_read_length ) {
278 unless ( $self->_body ) {
280 ## If something plack middle already ready the body, just use
284 if(my $plack_body = $self->env->{'plack.request.http.body'}) {
287 my $type = $self->header('Content-Type');
288 $body = HTTP::Body->new($type, $length);
290 ## Play nice with Plak Middleware that looks for a body
291 $self->env->{'plack.request.http.body'} = $body;
294 $body->cleanup(1); # Make extra sure!
295 $body->tmpdir( $self->_uploadtmp )
296 if $self->_has_uploadtmp;
300 # Check for definedness as you could read '0'
301 while ( defined ( my $buffer = $self->read() ) ) {
302 $self->prepare_body_chunk($buffer);
305 # paranoia against wrong Content-Length header
306 my $remaining = $length - $self->_read_position;
307 if ( $remaining > 0 ) {
308 Catalyst::Exception->throw(
309 "Wrong Content-Length value: $length" );
313 # Defined but will cause all body code to be skipped
319 sub prepare_body_chunk {
320 my ( $self, $chunk ) = @_;
322 $self->_body->add($chunk);
325 sub prepare_body_parameters {
328 $self->prepare_body if ! $self->_has_body;
329 return {} unless $self->_body;
331 return $self->_body->param;
334 sub prepare_connection {
337 my $env = $self->env;
339 $self->address( $env->{REMOTE_ADDR} );
340 $self->hostname( $env->{REMOTE_HOST} )
341 if exists $env->{REMOTE_HOST};
342 $self->protocol( $env->{SERVER_PROTOCOL} );
343 $self->remote_user( $env->{REMOTE_USER} );
344 $self->method( $env->{REQUEST_METHOD} );
345 $self->secure( $env->{'psgi.url_scheme'} eq 'https' ? 1 : 0 );
348 # XXX - FIXME - method is here now, move this crap...
349 around parameters => sub {
350 my ($orig, $self, $params) = @_;
352 if ( !ref $params ) {
354 "Attempt to retrieve '$params' with req->params(), " .
355 "you probably meant to call req->param('$params')"
359 return $self->$orig($params);
370 return $self->path if $self->has_uri;
375 is => 'rw', clearer => '_clear_body', predicate => '_has_body',
377 # Eugh, ugly. Should just be able to rename accessor methods to 'body'
378 # and provide a custom reader..
381 $self->prepare_body unless $self->_has_body;
382 croak 'body is a reader' if scalar @_;
383 return blessed $self->_body ? $self->_body->body : $self->_body;
392 gethostbyaddr( inet_aton( $self->address ), AF_INET ) || $self->address
396 has _path => ( is => 'rw', predicate => '_has_path', clearer => '_clear_path' );
398 sub args { shift->arguments(@_) }
399 sub body_params { shift->body_parameters(@_) }
400 sub input { shift->body(@_) }
401 sub params { shift->parameters(@_) }
402 sub query_params { shift->query_parameters(@_) }
403 sub path_info { shift->path(@_) }
405 =for stopwords param params
409 Catalyst::Request - provides information about the current client request
414 $req->address eq "127.0.0.1";
420 $req->body_parameters;
421 $req->content_encoding;
422 $req->content_length;
430 $req->query_keywords;
438 $req->query_parameters;
449 See also L<Catalyst>, L<Catalyst::Request::Upload>.
453 This is the Catalyst Request class, which provides an interface to data for the
454 current client request. The request object is prepared by L<Catalyst::Engine>,
455 thus hiding the details of the particular engine implementation.
461 Returns the IP address of the client.
463 =head2 $req->arguments
465 Returns a reference to an array containing the arguments.
467 print $c->request->arguments->[0];
469 For example, if your action was
471 package MyApp::Controller::Foo;
477 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
478 would be the first and only argument.
480 Arguments get automatically URI-unescaped for you.
484 Shortcut for L</arguments>.
488 Contains the URI base. This will always have a trailing slash. Note that the
489 URI scheme (e.g., http vs. https) must be determined through heuristics;
490 depending on your server configuration, it may be incorrect. See $req->secure
493 If your application was queried with the URI
494 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
498 Returns the message body of the request, as returned by L<HTTP::Body>: a string,
499 unless Content-Type is C<application/x-www-form-urlencoded>, C<text/xml>, or
500 C<multipart/form-data>, in which case a L<File::Temp> object is returned.
502 =head2 $req->body_data
504 Returns a Perl representation of POST/PUT body data that is not classic HTML
505 form data, such as JSON, XML, etc. By default, Catalyst will parse incoming
506 data of the type 'application/json' and return access to that data via this
507 method. You may define addition data_handlers via a global configuration
508 setting. See L<Catalyst\DATA HANDLERS> for more information.
510 =head2 $req->body_parameters
512 Returns a reference to a hash containing body (POST) parameters. Values can
513 be either a scalar or an arrayref containing scalars.
515 print $c->request->body_parameters->{field};
516 print $c->request->body_parameters->{field}->[0];
518 These are the parameters from the POST part of the request, if any.
520 =head2 $req->body_params
522 Shortcut for body_parameters.
524 =head2 $req->content_encoding
526 Shortcut for $req->headers->content_encoding.
528 =head2 $req->content_length
530 Shortcut for $req->headers->content_length.
532 =head2 $req->content_type
534 Shortcut for $req->headers->content_type.
538 A convenient method to access $req->cookies.
540 $cookie = $c->request->cookie('name');
541 @cookies = $c->request->cookie;
549 return keys %{ $self->cookies };
556 unless ( exists $self->cookies->{$name} ) {
560 return $self->cookies->{$name};
566 Returns a reference to a hash containing the cookies.
568 print $c->request->cookies->{mycookie}->value;
570 The cookies in the hash are indexed by name, and the values are L<CGI::Simple::Cookie>
575 Shortcut for $req->headers->header.
579 Returns an L<HTTP::Headers> object containing the headers for the current request.
581 print $c->request->headers->header('X-Catalyst');
583 =head2 $req->hostname
585 Returns the hostname of the client. Use C<< $req->uri->host >> to get the hostname of the server.
589 Alias for $req->body.
591 =head2 $req->query_keywords
593 Contains the keywords portion of a query string, when no '=' signs are
596 http://localhost/path?some+keywords
598 $c->request->query_keywords will contain 'some keywords'
602 This contains the matching part of a Regex action. Otherwise
603 it returns the same as 'action', except for default actions,
604 which return an empty string.
608 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
612 Returns GET and POST parameters with a CGI.pm-compatible param method. This
613 is an alternative method for accessing parameters in $c->req->parameters.
615 $value = $c->request->param( 'foo' );
616 @values = $c->request->param( 'foo' );
617 @params = $c->request->param;
619 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
620 arguments to this method, like this:
622 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
624 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
625 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
626 (creating it if it didn't exist before), and C<quxx> as another value for
629 B<NOTE> this is considered a legacy interface and care should be taken when
630 using it. C<< scalar $c->req->param( 'foo' ) >> will return only the first
631 C<foo> param even if multiple are present; C<< $c->req->param( 'foo' ) >> will
632 return a list of as many are present, which can have unexpected consequences
633 when writing code of the form:
637 baz => $c->req->param( 'baz' ),
640 If multiple C<baz> parameters are provided this code might corrupt data or
641 cause a hash initialization error. For a more straightforward interface see
642 C<< $c->req->parameters >>.
650 return keys %{ $self->parameters };
657 unless ( exists $self->parameters->{$param} ) {
658 return wantarray ? () : undef;
661 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
663 ? @{ $self->parameters->{$param} }
664 : $self->parameters->{$param}->[0];
668 ? ( $self->parameters->{$param} )
669 : $self->parameters->{$param};
674 $self->parameters->{$field} = [@_];
678 =head2 $req->parameters
680 Returns a reference to a hash containing GET and POST parameters. Values can
681 be either a scalar or an arrayref containing scalars.
683 print $c->request->parameters->{field};
684 print $c->request->parameters->{field}->[0];
686 This is the combination of C<query_parameters> and C<body_parameters>.
690 Shortcut for $req->parameters.
694 Returns the path, i.e. the part of the URI after $req->base, for the current request.
696 http://localhost/path/foo
698 $c->request->path will contain 'path/foo'
700 =head2 $req->path_info
702 Alias for path, added for compatibility with L<CGI>.
707 my ( $self, @params ) = @_;
710 $self->uri->path(@params);
713 elsif ( $self->_has_path ) {
717 my $path = $self->uri->path;
718 my $location = $self->base->path;
719 $path =~ s/^(\Q$location\E)?//;
727 =head2 $req->protocol
729 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
731 =head2 $req->query_parameters
733 =head2 $req->query_params
735 Returns a reference to a hash containing query string (GET) parameters. Values can
736 be either a scalar or an arrayref containing scalars.
738 print $c->request->query_parameters->{field};
739 print $c->request->query_parameters->{field}->[0];
741 =head2 $req->read( [$maxlength] )
743 Reads a chunk of data from the request body. This method is intended to be
744 used in a while loop, reading $maxlength bytes on every call. $maxlength
745 defaults to the size of the request if not specified.
747 =head2 $req->read_chunk(\$buff, $max)
751 You have to set MyApp->config(parse_on_demand => 1) to use this directly.
755 Shortcut for $req->headers->referer. Returns the referring page.
759 Returns true or false, indicating whether the connection is secure
760 (https). The reliability of $req->secure may depend on your server
761 configuration; Catalyst relies on PSGI to determine whether or not a
762 request is secure (Catalyst looks at psgi.url_scheme), and different
763 PSGI servers may make this determination in different ways (as by
764 directly passing along information from the server, interpreting any of
765 several HTTP headers, or using heuristics of their own).
767 =head2 $req->captures
769 Returns a reference to an array containing captured args from chained
770 actions or regex captures.
772 my @captures = @{ $c->request->captures };
776 A convenient method to access $req->uploads.
778 $upload = $c->request->upload('field');
779 @uploads = $c->request->upload('field');
780 @fields = $c->request->upload;
782 for my $upload ( $c->request->upload('field') ) {
783 print $upload->filename;
792 return keys %{ $self->uploads };
799 unless ( exists $self->uploads->{$upload} ) {
800 return wantarray ? () : undef;
803 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
805 ? @{ $self->uploads->{$upload} }
806 : $self->uploads->{$upload}->[0];
810 ? ( $self->uploads->{$upload} )
811 : $self->uploads->{$upload};
817 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
819 if ( exists $self->uploads->{$field} ) {
820 for ( $self->uploads->{$field} ) {
821 $_ = [$_] unless ref($_) eq "ARRAY";
822 push( @$_, $upload );
826 $self->uploads->{$field} = $upload;
834 Returns a reference to a hash containing uploads. Values can be either a
835 L<Catalyst::Request::Upload> object, or an arrayref of
836 L<Catalyst::Request::Upload> objects.
838 my $upload = $c->request->uploads->{field};
839 my $upload = $c->request->uploads->{field}->[0];
843 Returns a L<URI> object for the current request. Stringifies to the URI text.
845 =head2 $req->mangle_params( { key => 'value' }, $appendmode);
847 Returns a hashref of parameters stemming from the current request's params,
848 plus the ones supplied. Keys for which no current param exists will be
849 added, keys with undefined values will be removed and keys with existing
850 params will be replaced. Note that you can supply a true value as the final
851 argument to change behavior with regards to existing parameters, appending
852 values rather than replacing them.
856 # URI query params foo=1
857 my $hashref = $req->mangle_params({ foo => 2 });
858 # Result is query params of foo=2
862 # URI query params foo=1
863 my $hashref = $req->mangle_params({ foo => 2 }, 1);
864 # Result is query params of foo=1&foo=2
866 This is the code behind C<uri_with>.
871 my ($self, $args, $append) = @_;
873 carp('No arguments passed to mangle_params()') unless $args;
875 foreach my $value ( values %$args ) {
876 next unless defined $value;
877 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
879 utf8::encode( $_ ) if utf8::is_utf8($_);
883 my %params = %{ $self->uri->query_form_hash };
884 foreach my $key (keys %{ $args }) {
885 my $val = $args->{$key};
888 if($append && exists($params{$key})) {
890 # This little bit of heaven handles appending a new value onto
891 # an existing one regardless if the existing value is an array
892 # or not, and regardless if the new value is an array or not
894 ref($params{$key}) eq 'ARRAY' ? @{ $params{$key} } : $params{$key},
895 ref($val) eq 'ARRAY' ? @{ $val } : $val
899 $params{$key} = $val;
903 # If the param wasn't defined then we delete it.
904 delete($params{$key});
912 =head2 $req->uri_with( { key => 'value' } );
914 Returns a rewritten URI object for the current request. Key/value pairs
915 passed in will override existing parameters. You can remove an existing
916 parameter by passing in an undef value. Unmodified pairs will be
919 You may also pass an optional second parameter that puts C<uri_with> into
922 $req->uri_with( { key => 'value' }, { mode => 'append' } );
924 See C<mangle_params> for an explanation of this behavior.
929 my( $self, $args, $behavior) = @_;
931 carp( 'No arguments passed to uri_with()' ) unless $args;
934 if((ref($behavior) eq 'HASH') && defined($behavior->{mode}) && ($behavior->{mode} eq 'append')) {
938 my $params = $self->mangle_params($args, $append);
940 my $uri = $self->uri->clone;
941 $uri->query_form($params);
946 =head2 $req->remote_user
948 Returns the value of the C<REMOTE_USER> environment variable.
950 =head2 $req->user_agent
952 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
957 Returns a psgix.io bidirectional socket, if your server supports one. Used for
958 when you want to jailbreak out of PSGI and handle bidirectional client server
959 communication manually, such as when you are using cometd or websockets.
963 You should never need to call these yourself in application code,
964 however they are useful if extending Catalyst by applying a request role.
966 =head2 $self->prepare_headers()
968 Sets up the C<< $res->headers >> accessor.
970 =head2 $self->prepare_body()
972 Sets up the body using L<HTTP::Body>
974 =head2 $self->prepare_body_chunk()
976 Add a chunk to the request body.
978 =head2 $self->prepare_body_parameters()
980 Sets up parameters from body.
982 =head2 $self->prepare_cookies()
984 Parse cookies from header. Sets up a L<CGI::Simple::Cookie> object.
986 =head2 $self->prepare_connection()
988 Sets up various fields in the request like the local and remote addresses,
989 request method, hostname requested etc.
991 =head2 $self->prepare_parameters()
993 Ensures that the body has been parsed, then builds the parameters, which are
994 combined from those in the request and those in the body.
996 If parameters have already been set will clear the parameters and build them again.
1005 Catalyst Contributors, see Catalyst.pm
1009 This library is free software. You can redistribute it and/or modify
1010 it under the same terms as Perl itself.
1014 __PACKAGE__->meta->make_immutable;