1 package Catalyst::Request;
4 use base 'Class::Accessor::Fast';
6 use IO::Socket qw[AF_INET inet_aton];
10 __PACKAGE__->mk_accessors(
11 qw/action address arguments cookies headers match method
12 protocol query_parameters secure snippets uri user/
16 *body_params = \&body_parameters;
18 *params = \¶meters;
19 *query_params = \&query_parameters;
22 sub content_encoding { shift->headers->content_encoding(@_) }
23 sub content_length { shift->headers->content_length(@_) }
24 sub content_type { shift->headers->content_type(@_) }
25 sub header { shift->headers->header(@_) }
26 sub referer { shift->headers->referer(@_) }
27 sub user_agent { shift->headers->user_agent(@_) }
31 Catalyst::Request - provides information about the current client request
42 $req->body_parameters;
43 $req->content_encoding;
59 $req->query_parameters;
74 This is the Catalyst Request class, which provides an interface to data for the
75 current client request. The request object is prepared by L<Catalyst::Engine>,
76 thus hiding the details of the particular engine implementation.
82 Returns the requested action as a L<Catalyst::Action> object.
86 Returns the IP address of the client.
88 =head2 $req->arguments
90 Returns a reference to an array containing the arguments.
92 print $c->request->arguments->[0];
94 For example, if your action was
96 package MyApp::C::Foo;
102 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
103 would be the first and only argument.
107 Shortcut for arguments.
111 Contains the URI base. This will always have a trailing slash.
113 If your application was queried with the URI
114 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
119 my ( $self, $base ) = @_;
121 return $self->{base} unless $base;
123 $self->{base} = $base;
125 # set the value in path for backwards-compat
130 return $self->{base};
135 Returns the message body of the request, unless Content-Type is
136 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
141 my ( $self, $body ) = @_;
142 $self->{_context}->prepare_body;
143 return $self->{_body}->body;
146 =head2 $req->body_parameters
148 Returns a reference to a hash containing body (POST) parameters. Values can
149 be either a scalar or an arrayref containing scalars.
151 print $c->request->body_parameters->{field};
152 print $c->request->body_parameters->{field}->[0];
154 These are the parameters from the POST part of the request, if any.
156 =head2 $req->body_params
158 Shortcut for body_parameters.
162 sub body_parameters {
163 my ( $self, $params ) = @_;
164 $self->{_context}->prepare_body;
165 $self->{body_parameters} = $params if $params;
166 return $self->{body_parameters};
169 =head2 $req->content_encoding
171 Shortcut for $req->headers->content_encoding.
173 =head2 $req->content_length
175 Shortcut for $req->headers->content_length.
177 =head2 $req->content_type
179 Shortcut for $req->headers->content_type.
183 A convenient method to access $req->cookies.
185 $cookie = $c->request->cookie('name');
186 @cookies = $c->request->cookie;
194 return keys %{ $self->cookies };
201 unless ( exists $self->cookies->{$name} ) {
205 return $self->cookies->{$name};
211 Returns a reference to a hash containing the cookies.
213 print $c->request->cookies->{mycookie}->value;
215 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
220 Shortcut for $req->headers->header.
224 Returns an L<HTTP::Headers> object containing the headers for the current request.
226 print $c->request->headers->header('X-Catalyst');
228 =head2 $req->hostname
230 Returns the hostname of the client.
237 if ( @_ == 0 && not $self->{hostname} ) {
239 gethostbyaddr( inet_aton( $self->address ), AF_INET );
243 $self->{hostname} = shift;
246 return $self->{hostname};
251 Alias for $req->body.
255 This contains the matching part of a Regex action. Otherwise
256 it returns the same as 'action', except for default actions,
257 which return an empty string.
261 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
265 Returns GET and POST parameters with a CGI.pm-compatible param method. This
266 is an alternative method for accessing parameters in $c->req->parameters.
268 $value = $c->request->param( 'foo' );
269 @values = $c->request->param( 'foo' );
270 @params = $c->request->param;
272 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
273 arguments to this method, like this:
275 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
277 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
278 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
279 (creating it if it didn't exist before), and C<quxx> as another value for
288 return keys %{ $self->parameters };
295 unless ( exists $self->parameters->{$param} ) {
296 return wantarray ? () : undef;
299 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
301 ? @{ $self->parameters->{$param} }
302 : $self->parameters->{$param}->[0];
306 ? ( $self->parameters->{$param} )
307 : $self->parameters->{$param};
312 $self->parameters->{$field} = [@_];
316 =head2 $req->parameters
318 Returns a reference to a hash containing GET and POST parameters. Values can
319 be either a scalar or an arrayref containing scalars.
321 print $c->request->parameters->{field};
322 print $c->request->parameters->{field}->[0];
324 This is the combination of C<query_parameters> and C<body_parameters>.
328 Shortcut for $req->parameters.
333 my ( $self, $params ) = @_;
334 $self->{_context}->prepare_body;
337 $self->{parameters} = $params;
340 $self->{_context}->log->warn(
341 "Attempt to retrieve '$params' with req->params(), " .
342 "you probably meant to call req->param('$params')" );
345 return $self->{parameters};
350 Returns the path, i.e. the part of the URI after $req->base, for the current request.
352 =head2 $req->path_info
354 Alias for path, added for compability with L<CGI>.
359 my ( $self, $params ) = @_;
362 $self->uri->path($params);
365 return $self->{path} if $self->{path};
368 my $path = $self->uri->path;
369 my $location = $self->base->path;
370 $path =~ s/^(\Q$location\E)?//;
372 $self->{path} = $path;
377 =head2 $req->protocol
379 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
381 =head2 $req->query_parameters
383 Returns a reference to a hash containing query string (GET) parameters. Values can
384 be either a scalar or an arrayref containing scalars.
386 print $c->request->query_parameters->{field};
387 print $c->request->query_parameters->{field}->[0];
389 =head2 $req->read( [$maxlength] )
391 Reads a chunk of data from the request body. This method is intended to be
392 used in a while loop, reading $maxlength bytes on every call. $maxlength
393 defaults to the size of the request if not specified.
395 You have to set MyApp->config->{parse_on_demand} to use this directly.
399 sub read { shift->{_context}->read(@_); }
403 Shortcut for $req->headers->referer. Returns the referring page.
407 Returns true or false, indicating whether the connection is secure (https).
409 =head2 $req->snippets
411 Returns a reference to an array containing regex snippets.
413 my @snippets = @{ $c->request->snippets };
417 A convenient method to access $req->uploads.
419 $upload = $c->request->upload('field');
420 @uploads = $c->request->upload('field');
421 @fields = $c->request->upload;
423 for my $upload ( $c->request->upload('field') ) {
424 print $upload->filename;
433 return keys %{ $self->uploads };
440 unless ( exists $self->uploads->{$upload} ) {
441 return wantarray ? () : undef;
444 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
446 ? @{ $self->uploads->{$upload} }
447 : $self->uploads->{$upload}->[0];
451 ? ( $self->uploads->{$upload} )
452 : $self->uploads->{$upload};
458 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
460 if ( exists $self->uploads->{$field} ) {
461 for ( $self->uploads->{$field} ) {
462 $_ = [$_] unless ref($_) eq "ARRAY";
463 push( @$_, $upload );
467 $self->uploads->{$field} = $upload;
475 Returns a reference to a hash containing uploads. Values can be either a
476 hashref or a arrayref containing L<Catalyst::Request::Upload> objects.
478 my $upload = $c->request->uploads->{field};
479 my $upload = $c->request->uploads->{field}->[0];
484 my ( $self, $uploads ) = @_;
485 $self->{_context}->prepare_body;
486 $self->{uploads} = $uploads if $uploads;
487 return $self->{uploads};
492 Returns a URI object for the current request. Stringifies to the URI text.
494 =head2 $req->uri_with( { key => 'value' } );
496 Returns a rewriten URI object for the current uri. Key/value pairs passed in
497 will override existing parameters. Unmodified pairs will be preserved.
502 my( $self, $args ) = @_;
504 carp( 'No arguments passed to uri_with()' ) unless $args;
506 for my $value ( values %$args ) {
507 my $isa_ref = ref $value;
508 if( $isa_ref and $isa_ref ne 'ARRAY' ) {
509 croak( "Non-array reference ($isa_ref) passed to uri_with()" );
511 utf8::encode( $_ ) for $isa_ref ? @$value : $value ;
513 my $uri = $self->uri->clone;
524 Returns the currently logged in user. Deprecated. The method recommended for
525 newer plugins is $c->user.
527 =head2 $req->user_agent
529 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
534 Sebastian Riedel, C<sri@cpan.org>
536 Marcus Ramberg, C<mramberg@cpan.org>
540 This program is free software, you can redistribute it and/or modify
541 it under the same terms as Perl itself.