1 package Catalyst::Request;
3 use IO::Socket qw[AF_INET inet_aton];
13 with 'MooseX::Emulate::Class::Accessor::Fast';
15 has action => (is => 'rw');
16 has address => (is => 'rw');
17 has arguments => (is => 'rw', default => sub { [] });
18 has cookies => (is => 'rw', default => sub { {} });
19 has query_keywords => (is => 'rw');
20 has match => (is => 'rw');
21 has method => (is => 'rw');
22 has protocol => (is => 'rw');
23 has query_parameters => (is => 'rw', default => sub { {} });
24 has secure => (is => 'rw', default => 0);
25 has captures => (is => 'rw', default => sub { [] });
26 has uri => (is => 'rw', predicate => 'has_uri');
27 has user => (is => 'rw');
30 isa => 'HTTP::Headers',
31 handles => [qw(content_encoding content_length content_type header referer user_agent)],
32 default => sub { HTTP::Headers->new() },
38 # - Can we lose the before modifiers which just call prepare_body ?
39 # they are wasteful, slow us down and feel cluttery.
40 # Can we call prepare_body at BUILD time?
41 # Can we make _body an attribute, have the rest of
42 # these lazy build from there and kill all the direct hash access
43 # in Catalyst.pm and Engine.pm?
49 clearer => '_clear_context',
52 has body_parameters => (
56 default => sub { {} },
59 before body_parameters => sub {
61 $self->_context->prepare_body();
68 default => sub { {} },
75 default => sub { {} },
78 before parameters => sub {
79 my ($self, $params) = @_;
80 if ( $params && !ref $params ) {
81 $self->_context->log->warn(
82 "Attempt to retrieve '$params' with req->params(), " .
83 "you probably meant to call req->param('$params')" );
95 return $self->path if $self->has_uri;
100 is => 'rw', clearer => '_clear_body', predicate => '_has_body',
102 # Eugh, ugly. Should just be able to rename accessor methods to 'body'
103 # and provide a custom reader..
106 $self->_context->prepare_body();
107 $self->_body(@_) if scalar @_;
108 return blessed $self->_body ? $self->_body->body : $self->_body;
117 gethostbyaddr( inet_aton( $self->address ), AF_INET ) || 'localhost'
121 has _path => ( is => 'rw', predicate => '_has_path', clearer => '_clear_path' );
125 sub args { shift->arguments(@_) }
126 sub body_params { shift->body_parameters(@_) }
127 sub input { shift->body(@_) }
128 sub params { shift->parameters(@_) }
129 sub query_params { shift->query_parameters(@_) }
130 sub path_info { shift->path(@_) }
131 sub snippets { shift->captures(@_) }
135 Catalyst::Request - provides information about the current client request
146 $req->body_parameters;
147 $req->content_encoding;
148 $req->content_length;
156 $req->query_keywords;
164 $req->query_parameters;
168 $req->captures; # previously knows as snippets
175 See also L<Catalyst>, L<Catalyst::Request::Upload>.
179 This is the Catalyst Request class, which provides an interface to data for the
180 current client request. The request object is prepared by L<Catalyst::Engine>,
181 thus hiding the details of the particular engine implementation.
187 [DEPRECATED] Returns the name of the requested action.
190 Use C<< $c->action >> instead (which returns a
191 L<Catalyst::Action|Catalyst::Action> object).
195 Returns the IP address of the client.
197 =head2 $req->arguments
199 Returns a reference to an array containing the arguments.
201 print $c->request->arguments->[0];
203 For example, if your action was
205 package MyApp::C::Foo;
211 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
212 would be the first and only argument.
216 Shortcut for arguments.
220 Contains the URI base. This will always have a trailing slash.
222 If your application was queried with the URI
223 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
227 Returns the message body of the request, unless Content-Type is
228 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
230 =head2 $req->body_parameters
232 Returns a reference to a hash containing body (POST) parameters. Values can
233 be either a scalar or an arrayref containing scalars.
235 print $c->request->body_parameters->{field};
236 print $c->request->body_parameters->{field}->[0];
238 These are the parameters from the POST part of the request, if any.
240 =head2 $req->body_params
242 Shortcut for body_parameters.
244 =head2 $req->content_encoding
246 Shortcut for $req->headers->content_encoding.
248 =head2 $req->content_length
250 Shortcut for $req->headers->content_length.
252 =head2 $req->content_type
254 Shortcut for $req->headers->content_type.
258 A convenient method to access $req->cookies.
260 $cookie = $c->request->cookie('name');
261 @cookies = $c->request->cookie;
269 return keys %{ $self->cookies };
276 unless ( exists $self->cookies->{$name} ) {
280 return $self->cookies->{$name};
286 Returns a reference to a hash containing the cookies.
288 print $c->request->cookies->{mycookie}->value;
290 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
295 Shortcut for $req->headers->header.
299 Returns an L<HTTP::Headers> object containing the headers for the current request.
301 print $c->request->headers->header('X-Catalyst');
303 =head2 $req->hostname
305 Returns the hostname of the client.
309 Alias for $req->body.
311 =head2 $req->query_keywords
313 Contains the keywords portion of a query string, when no '=' signs are
316 http://localhost/path?some+keywords
318 $c->request->query_keywords will contain 'some keywords'
322 This contains the matching part of a Regex action. Otherwise
323 it returns the same as 'action', except for default actions,
324 which return an empty string.
328 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
332 Returns GET and POST parameters with a CGI.pm-compatible param method. This
333 is an alternative method for accessing parameters in $c->req->parameters.
335 $value = $c->request->param( 'foo' );
336 @values = $c->request->param( 'foo' );
337 @params = $c->request->param;
339 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
340 arguments to this method, like this:
342 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
344 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
345 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
346 (creating it if it didn't exist before), and C<quxx> as another value for
355 return keys %{ $self->parameters };
362 unless ( exists $self->parameters->{$param} ) {
363 return wantarray ? () : undef;
366 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
368 ? @{ $self->parameters->{$param} }
369 : $self->parameters->{$param}->[0];
373 ? ( $self->parameters->{$param} )
374 : $self->parameters->{$param};
379 $self->parameters->{$field} = [@_];
383 =head2 $req->parameters
385 Returns a reference to a hash containing GET and POST parameters. Values can
386 be either a scalar or an arrayref containing scalars.
388 print $c->request->parameters->{field};
389 print $c->request->parameters->{field}->[0];
391 This is the combination of C<query_parameters> and C<body_parameters>.
395 Shortcut for $req->parameters.
399 Returns the path, i.e. the part of the URI after $req->base, for the current request.
401 =head2 $req->path_info
403 Alias for path, added for compability with L<CGI>.
408 my ( $self, @params ) = @_;
411 $self->uri->path(@params);
414 elsif ( $self->_has_path ) {
418 my $path = $self->uri->path;
419 my $location = $self->base->path;
420 $path =~ s/^(\Q$location\E)?//;
428 =head2 $req->protocol
430 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
432 =head2 $req->query_parameters
434 =head2 $req->query_params
436 Returns a reference to a hash containing query string (GET) parameters. Values can
437 be either a scalar or an arrayref containing scalars.
439 print $c->request->query_parameters->{field};
440 print $c->request->query_parameters->{field}->[0];
442 =head2 $req->read( [$maxlength] )
444 Reads a chunk of data from the request body. This method is intended to be
445 used in a while loop, reading $maxlength bytes on every call. $maxlength
446 defaults to the size of the request if not specified.
448 You have to set MyApp->config->{parse_on_demand} to use this directly.
452 Shortcut for $req->headers->referer. Returns the referring page.
456 Returns true or false, indicating whether the connection is secure (https).
458 =head2 $req->captures
460 Returns a reference to an array containing captured args from chained
461 actions or regex captures.
463 my @captures = @{ $c->request->captures };
465 =head2 $req->snippets
467 C<captures> used to be called snippets. This is still available for backwoards
468 compatibility, but is considered deprecated.
472 A convenient method to access $req->uploads.
474 $upload = $c->request->upload('field');
475 @uploads = $c->request->upload('field');
476 @fields = $c->request->upload;
478 for my $upload ( $c->request->upload('field') ) {
479 print $upload->filename;
488 return keys %{ $self->uploads };
495 unless ( exists $self->uploads->{$upload} ) {
496 return wantarray ? () : undef;
499 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
501 ? @{ $self->uploads->{$upload} }
502 : $self->uploads->{$upload}->[0];
506 ? ( $self->uploads->{$upload} )
507 : $self->uploads->{$upload};
513 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
515 if ( exists $self->uploads->{$field} ) {
516 for ( $self->uploads->{$field} ) {
517 $_ = [$_] unless ref($_) eq "ARRAY";
518 push( @$_, $upload );
522 $self->uploads->{$field} = $upload;
530 Returns a reference to a hash containing uploads. Values can be either a
531 L<Catalyst::Request::Upload> object, or an arrayref of
532 L<Catalyst::Request::Upload> objects.
534 my $upload = $c->request->uploads->{field};
535 my $upload = $c->request->uploads->{field}->[0];
539 Returns a URI object for the current request. Stringifies to the URI text.
541 =head2 $req->uri_with( { key => 'value' } );
543 Returns a rewritten URI object for the current request. Key/value pairs
544 passed in will override existing parameters. You can remove an existing
545 parameter by passing in an undef value. Unmodified pairs will be
551 my( $self, $args ) = @_;
553 carp( 'No arguments passed to uri_with()' ) unless $args;
555 foreach my $value ( values %$args ) {
556 next unless defined $value;
557 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
559 utf8::encode( $_ ) if utf8::is_utf8($_);
563 my $uri = $self->uri->clone;
564 my %query = ( %{ $uri->query_form_hash }, %$args );
567 # remove undef values
568 map { defined $query{ $_ } ? ( $_ => $query{ $_ } ) : () } keys %query
575 Returns the currently logged in user. Deprecated. The method recommended for
576 newer plugins is $c->user.
578 =head2 $req->user_agent
580 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
589 Catalyst Contributors, see Catalyst.pm
593 This program is free software, you can redistribute it and/or modify
594 it under the same terms as Perl itself.
598 __PACKAGE__->meta->make_immutable;