1 package Catalyst::Request;
4 use base 'Class::Accessor::Fast';
6 use IO::Socket qw[AF_INET inet_aton];
13 __PACKAGE__->mk_accessors(
14 qw/action address arguments cookies headers query_keywords match method
15 protocol query_parameters secure captures uri user/
19 *body_params = \&body_parameters;
21 *params = \¶meters;
22 *query_params = \&query_parameters;
24 *snippets = \&captures;
26 sub content_encoding { shift->headers->content_encoding(@_) }
27 sub content_length { shift->headers->content_length(@_) }
28 sub content_type { shift->headers->content_type(@_) }
29 sub header { shift->headers->header(@_) }
30 sub referer { shift->headers->referer(@_) }
31 sub user_agent { shift->headers->user_agent(@_) }
35 Catalyst::Request - provides information about the current client request
46 $req->body_parameters;
47 $req->content_encoding;
64 $req->query_parameters;
68 $req->captures; # previously knows as snippets
75 See also L<Catalyst>, L<Catalyst::Request::Upload>.
79 This is the Catalyst Request class, which provides an interface to data for the
80 current client request. The request object is prepared by L<Catalyst::Engine>,
81 thus hiding the details of the particular engine implementation.
87 [DEPRECATED] Returns the name of the requested action.
90 Use C<< $c->action >> instead (which returns a
91 L<Catalyst::Action|Catalyst::Action> object).
95 Returns the IP address of the client.
97 =head2 $req->arguments
99 Returns a reference to an array containing the arguments.
101 print $c->request->arguments->[0];
103 For example, if your action was
105 package MyApp::C::Foo;
111 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
112 would be the first and only argument.
116 Shortcut for arguments.
120 Contains the URI base. This will always have a trailing slash.
122 If your application was queried with the URI
123 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
128 my ( $self, $base ) = @_;
130 return $self->{base} unless $base;
132 $self->{base} = $base;
134 # set the value in path for backwards-compat
139 return $self->{base};
144 Returns the message body of the request, unless Content-Type is
145 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
151 $self->{_context}->prepare_body;
153 return unless $self->{_body};
155 return $self->{_body}->body;
158 =head2 $req->body_parameters
160 Returns a reference to a hash containing body (POST) parameters. Values can
161 be either a scalar or an arrayref containing scalars.
163 print $c->request->body_parameters->{field};
164 print $c->request->body_parameters->{field}->[0];
166 These are the parameters from the POST part of the request, if any.
168 =head2 $req->body_params
170 Shortcut for body_parameters.
174 sub body_parameters {
175 my ( $self, $params ) = @_;
176 $self->{_context}->prepare_body;
177 $self->{body_parameters} = $params if $params;
178 return $self->{body_parameters};
181 =head2 $req->content_encoding
183 Shortcut for $req->headers->content_encoding.
185 =head2 $req->content_length
187 Shortcut for $req->headers->content_length.
189 =head2 $req->content_type
191 Shortcut for $req->headers->content_type.
195 A convenient method to access $req->cookies.
197 $cookie = $c->request->cookie('name');
198 @cookies = $c->request->cookie;
206 return keys %{ $self->cookies };
213 unless ( exists $self->cookies->{$name} ) {
217 return $self->cookies->{$name};
223 Returns a reference to a hash containing the cookies.
225 print $c->request->cookies->{mycookie}->value;
227 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
232 Shortcut for $req->headers->header.
236 Returns an L<HTTP::Headers> object containing the headers for the current request.
238 print $c->request->headers->header('X-Catalyst');
240 =head2 $req->hostname
242 Returns the hostname of the client.
249 if ( @_ == 0 && not $self->{hostname} ) {
251 gethostbyaddr( inet_aton( $self->address ), AF_INET );
255 $self->{hostname} = shift;
258 return $self->{hostname};
263 Alias for $req->body.
265 =head2 $req->query_keywords
267 Contains the keywords portion of a query string, when no '=' signs are
270 http://localhost/path?some+keywords
272 $c->request->query_keywords will contain 'some keywords'
276 This contains the matching part of a Regex action. Otherwise
277 it returns the same as 'action', except for default actions,
278 which return an empty string.
282 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
286 Returns GET and POST parameters with a CGI.pm-compatible param method. This
287 is an alternative method for accessing parameters in $c->req->parameters.
289 $value = $c->request->param( 'foo' );
290 @values = $c->request->param( 'foo' );
291 @params = $c->request->param;
293 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
294 arguments to this method, like this:
296 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
298 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
299 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
300 (creating it if it didn't exist before), and C<quxx> as another value for
309 return keys %{ $self->parameters };
316 unless ( exists $self->parameters->{$param} ) {
317 return wantarray ? () : undef;
320 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
322 ? @{ $self->parameters->{$param} }
323 : $self->parameters->{$param}->[0];
327 ? ( $self->parameters->{$param} )
328 : $self->parameters->{$param};
333 $self->parameters->{$field} = [@_];
337 =head2 $req->parameters
339 Returns a reference to a hash containing GET and POST parameters. Values can
340 be either a scalar or an arrayref containing scalars.
342 print $c->request->parameters->{field};
343 print $c->request->parameters->{field}->[0];
345 This is the combination of C<query_parameters> and C<body_parameters>.
349 Shortcut for $req->parameters.
354 my ( $self, $params ) = @_;
355 $self->{_context}->prepare_body;
358 $self->{parameters} = $params;
361 $self->{_context}->log->warn(
362 "Attempt to retrieve '$params' with req->params(), " .
363 "you probably meant to call req->param('$params')" );
366 return $self->{parameters};
371 Returns the path, i.e. the part of the URI after $req->base, for the current request.
373 =head2 $req->path_info
375 Alias for path, added for compability with L<CGI>.
380 my ( $self, @params ) = @_;
383 $self->uri->path(@params);
386 elsif ( defined( my $path = $self->{path} ) ) {
390 my $path = $self->uri->path;
391 my $location = $self->base->path;
392 $path =~ s/^(\Q$location\E)?//;
394 $self->{path} = $path;
400 =head2 $req->protocol
402 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
404 =head2 $req->query_parameters
406 Returns a reference to a hash containing query string (GET) parameters. Values can
407 be either a scalar or an arrayref containing scalars.
409 print $c->request->query_parameters->{field};
410 print $c->request->query_parameters->{field}->[0];
412 =head2 $req->read( [$maxlength] )
414 Reads a chunk of data from the request body. This method is intended to be
415 used in a while loop, reading $maxlength bytes on every call. $maxlength
416 defaults to the size of the request if not specified.
418 You have to set MyApp->config->{parse_on_demand} to use this directly.
422 sub read { shift->{_context}->read(@_); }
426 Shortcut for $req->headers->referer. Returns the referring page.
430 Returns true or false, indicating whether the connection is secure (https).
432 =head2 $req->captures
434 Returns a reference to an array containing regex captures.
436 my @captures = @{ $c->request->captures };
438 =head2 $req->snippets
440 C<captures> used to be called snippets. This is still available for backwoards
441 compatibility, but is considered deprecated.
445 A convenient method to access $req->uploads.
447 $upload = $c->request->upload('field');
448 @uploads = $c->request->upload('field');
449 @fields = $c->request->upload;
451 for my $upload ( $c->request->upload('field') ) {
452 print $upload->filename;
461 return keys %{ $self->uploads };
468 unless ( exists $self->uploads->{$upload} ) {
469 return wantarray ? () : undef;
472 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
474 ? @{ $self->uploads->{$upload} }
475 : $self->uploads->{$upload}->[0];
479 ? ( $self->uploads->{$upload} )
480 : $self->uploads->{$upload};
486 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
488 if ( exists $self->uploads->{$field} ) {
489 for ( $self->uploads->{$field} ) {
490 $_ = [$_] unless ref($_) eq "ARRAY";
491 push( @$_, $upload );
495 $self->uploads->{$field} = $upload;
503 Returns a reference to a hash containing uploads. Values can be either a
504 L<Catalyst::Request::Upload> object, or an arrayref of
505 L<Catalyst::Request::Upload> objects.
507 my $upload = $c->request->uploads->{field};
508 my $upload = $c->request->uploads->{field}->[0];
513 my ( $self, $uploads ) = @_;
514 $self->{_context}->prepare_body;
515 $self->{uploads} = $uploads if $uploads;
516 return $self->{uploads};
521 Returns a URI object for the current request. Stringifies to the URI text.
523 =head2 $req->uri_with( { key => 'value' } );
525 Returns a rewritten URI object for the current request. Key/value pairs
526 passed in will override existing parameters. Unmodified pairs will be
532 my( $self, $args ) = @_;
534 carp( 'No arguments passed to uri_with()' ) unless $args;
536 for my $value ( values %$args ) {
537 next unless defined $value;
538 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
544 my $uri = $self->uri->clone;
547 %{ $uri->query_form_hash },
555 Returns the currently logged in user. Deprecated. The method recommended for
556 newer plugins is $c->user.
558 =head2 $req->user_agent
560 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
565 Sebastian Riedel, C<sri@cpan.org>
567 Marcus Ramberg, C<mramberg@cpan.org>
571 This program is free software, you can redistribute it and/or modify
572 it under the same terms as Perl itself.