1 package Catalyst::Request;
4 use base 'Class::Accessor::Fast';
6 use IO::Socket qw[AF_INET inet_aton];
11 __PACKAGE__->mk_accessors(
12 qw/action address arguments cookies headers match method
13 protocol query_parameters secure snippets uri user/
17 *body_params = \&body_parameters;
19 *params = \¶meters;
20 *query_params = \&query_parameters;
23 sub content_encoding { shift->headers->content_encoding(@_) }
24 sub content_length { shift->headers->content_length(@_) }
25 sub content_type { shift->headers->content_type(@_) }
26 sub header { shift->headers->header(@_) }
27 sub referer { shift->headers->referer(@_) }
28 sub user_agent { shift->headers->user_agent(@_) }
32 Catalyst::Request - provides information about the current client request
43 $req->body_parameters;
44 $req->content_encoding;
60 $req->query_parameters;
71 See also L<Catalyst>, L<Catalyst::Request::Upload>.
75 This is the Catalyst Request class, which provides an interface to data for the
76 current client request. The request object is prepared by L<Catalyst::Engine>,
77 thus hiding the details of the particular engine implementation.
83 Returns the requested action as a L<Catalyst::Action> object.
87 Returns the IP address of the client.
89 =head2 $req->arguments
91 Returns a reference to an array containing the arguments.
93 print $c->request->arguments->[0];
95 For example, if your action was
97 package MyApp::C::Foo;
103 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
104 would be the first and only argument.
108 Shortcut for arguments.
112 Contains the URI base. This will always have a trailing slash.
114 If your application was queried with the URI
115 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
120 my ( $self, $base ) = @_;
122 return $self->{base} unless $base;
124 $self->{base} = $base;
126 # set the value in path for backwards-compat
131 return $self->{base};
136 Returns the message body of the request, unless Content-Type is
137 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
142 my ( $self, $body ) = @_;
143 $self->{_context}->prepare_body;
144 return $self->{_body}->body;
147 =head2 $req->body_parameters
149 Returns a reference to a hash containing body (POST) parameters. Values can
150 be either a scalar or an arrayref containing scalars.
152 print $c->request->body_parameters->{field};
153 print $c->request->body_parameters->{field}->[0];
155 These are the parameters from the POST part of the request, if any.
157 =head2 $req->body_params
159 Shortcut for body_parameters.
163 sub body_parameters {
164 my ( $self, $params ) = @_;
165 $self->{_context}->prepare_body;
166 $self->{body_parameters} = $params if $params;
167 return $self->{body_parameters};
170 =head2 $req->content_encoding
172 Shortcut for $req->headers->content_encoding.
174 =head2 $req->content_length
176 Shortcut for $req->headers->content_length.
178 =head2 $req->content_type
180 Shortcut for $req->headers->content_type.
184 A convenient method to access $req->cookies.
186 $cookie = $c->request->cookie('name');
187 @cookies = $c->request->cookie;
195 return keys %{ $self->cookies };
202 unless ( exists $self->cookies->{$name} ) {
206 return $self->cookies->{$name};
212 Returns a reference to a hash containing the cookies.
214 print $c->request->cookies->{mycookie}->value;
216 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
221 Shortcut for $req->headers->header.
225 Returns an L<HTTP::Headers> object containing the headers for the current request.
227 print $c->request->headers->header('X-Catalyst');
229 =head2 $req->hostname
231 Returns the hostname of the client.
238 if ( @_ == 0 && not $self->{hostname} ) {
240 gethostbyaddr( inet_aton( $self->address ), AF_INET );
244 $self->{hostname} = shift;
247 return $self->{hostname};
252 Alias for $req->body.
256 This contains the matching part of a Regex action. Otherwise
257 it returns the same as 'action', except for default actions,
258 which return an empty string.
262 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
266 Returns GET and POST parameters with a CGI.pm-compatible param method. This
267 is an alternative method for accessing parameters in $c->req->parameters.
269 $value = $c->request->param( 'foo' );
270 @values = $c->request->param( 'foo' );
271 @params = $c->request->param;
273 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
274 arguments to this method, like this:
276 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
278 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
279 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
280 (creating it if it didn't exist before), and C<quxx> as another value for
289 return keys %{ $self->parameters };
296 unless ( exists $self->parameters->{$param} ) {
297 return wantarray ? () : undef;
300 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
302 ? @{ $self->parameters->{$param} }
303 : $self->parameters->{$param}->[0];
307 ? ( $self->parameters->{$param} )
308 : $self->parameters->{$param};
313 $self->parameters->{$field} = [@_];
317 =head2 $req->parameters
319 Returns a reference to a hash containing GET and POST parameters. Values can
320 be either a scalar or an arrayref containing scalars.
322 print $c->request->parameters->{field};
323 print $c->request->parameters->{field}->[0];
325 This is the combination of C<query_parameters> and C<body_parameters>.
329 Shortcut for $req->parameters.
334 my ( $self, $params ) = @_;
335 $self->{_context}->prepare_body;
338 $self->{parameters} = $params;
341 $self->{_context}->log->warn(
342 "Attempt to retrieve '$params' with req->params(), " .
343 "you probably meant to call req->param('$params')" );
346 return $self->{parameters};
351 Returns the path, i.e. the part of the URI after $req->base, for the current request.
353 =head2 $req->path_info
355 Alias for path, added for compability with L<CGI>.
360 my ( $self, $params ) = @_;
363 $self->uri->path($params);
366 return $self->{path} if $self->{path};
369 my $path = $self->uri->path;
370 my $location = $self->base->path;
371 $path =~ s/^(\Q$location\E)?//;
373 $self->{path} = $path;
378 =head2 $req->protocol
380 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
382 =head2 $req->query_parameters
384 Returns a reference to a hash containing query string (GET) parameters. Values can
385 be either a scalar or an arrayref containing scalars.
387 print $c->request->query_parameters->{field};
388 print $c->request->query_parameters->{field}->[0];
390 =head2 $req->read( [$maxlength] )
392 Reads a chunk of data from the request body. This method is intended to be
393 used in a while loop, reading $maxlength bytes on every call. $maxlength
394 defaults to the size of the request if not specified.
396 You have to set MyApp->config->{parse_on_demand} to use this directly.
400 sub read { shift->{_context}->read(@_); }
404 Shortcut for $req->headers->referer. Returns the referring page.
408 Returns true or false, indicating whether the connection is secure (https).
410 =head2 $req->snippets
412 Returns a reference to an array containing regex snippets.
414 my @snippets = @{ $c->request->snippets };
418 A convenient method to access $req->uploads.
420 $upload = $c->request->upload('field');
421 @uploads = $c->request->upload('field');
422 @fields = $c->request->upload;
424 for my $upload ( $c->request->upload('field') ) {
425 print $upload->filename;
434 return keys %{ $self->uploads };
441 unless ( exists $self->uploads->{$upload} ) {
442 return wantarray ? () : undef;
445 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
447 ? @{ $self->uploads->{$upload} }
448 : $self->uploads->{$upload}->[0];
452 ? ( $self->uploads->{$upload} )
453 : $self->uploads->{$upload};
459 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
461 if ( exists $self->uploads->{$field} ) {
462 for ( $self->uploads->{$field} ) {
463 $_ = [$_] unless ref($_) eq "ARRAY";
464 push( @$_, $upload );
468 $self->uploads->{$field} = $upload;
476 Returns a reference to a hash containing uploads. Values can be either a
477 hashref or a arrayref containing L<Catalyst::Request::Upload> objects.
479 my $upload = $c->request->uploads->{field};
480 my $upload = $c->request->uploads->{field}->[0];
485 my ( $self, $uploads ) = @_;
486 $self->{_context}->prepare_body;
487 $self->{uploads} = $uploads if $uploads;
488 return $self->{uploads};
493 Returns a URI object for the current request. Stringifies to the URI text.
495 =head2 $req->uri_with( { key => 'value' } );
497 Returns a rewriten URI object for the current uri. Key/value pairs passed in
498 will override existing parameters. Unmodified pairs will be preserved.
503 my( $self, $args ) = @_;
505 carp( 'No arguments passed to uri_with()' ) unless $args;
507 for my $value ( values %$args ) {
508 my $isa_ref = ref $value;
509 if( $isa_ref and $isa_ref ne 'ARRAY' ) {
510 croak( "Non-array reference ($isa_ref) passed to uri_with()" );
512 utf8::encode( $_ ) for grep{ defined } $isa_ref ? @$value : $value;
514 my $uri = $self->uri->clone;
517 %{ $uri->query_form_hash },
525 Returns the currently logged in user. Deprecated. The method recommended for
526 newer plugins is $c->user.
528 =head2 $req->user_agent
530 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
535 Sebastian Riedel, C<sri@cpan.org>
537 Marcus Ramberg, C<mramberg@cpan.org>
541 This program is free software, you can redistribute it and/or modify
542 it under the same terms as Perl itself.