1 package Catalyst::Request;
4 use base 'Class::Accessor::Fast';
6 use IO::Socket qw[AF_INET inet_aton];
9 __PACKAGE__->mk_accessors(
10 qw/action address arguments cookies headers match method
11 protocol query_parameters secure snippets uri user/
15 *body_params = \&body_parameters;
17 *params = \¶meters;
18 *query_params = \&query_parameters;
21 sub content_encoding { shift->headers->content_encoding(@_) }
22 sub content_length { shift->headers->content_length(@_) }
23 sub content_type { shift->headers->content_type(@_) }
24 sub header { shift->headers->header(@_) }
25 sub referer { shift->headers->referer(@_) }
26 sub user_agent { shift->headers->user_agent(@_) }
30 Catalyst::Request - provides information about the current client request
41 $req->body_parameters;
42 $req->content_encoding;
58 $req->query_parameters;
73 This is the Catalyst Request class, which provides an interface to data for the
74 current client request. The request object is prepared by L<Catalyst::Engine>,
75 thus hiding the details of the particular engine implementation.
81 Returns the requested action as a L<Catalyst::Action> object.
85 Returns the IP address of the client.
87 =head2 $req->arguments
89 Returns a reference to an array containing the arguments.
91 print $c->request->arguments->[0];
93 For example, if your action was
95 package MyApp::C::Foo;
101 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
102 would be the first and only argument.
106 Shortcut for arguments.
110 Contains the URI base. This will always have a trailing slash.
112 If your application was queried with the URI
113 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
118 my ( $self, $base ) = @_;
120 return $self->{base} unless $base;
122 $self->{base} = $base;
124 # set the value in path for backwards-compat
129 return $self->{base};
134 Returns the message body of the request, unless Content-Type is
135 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
140 my ( $self, $body ) = @_;
141 $self->{_context}->prepare_body;
142 return $self->{_body}->body;
145 =head2 $req->body_parameters
147 Returns a reference to a hash containing body (POST) parameters. Values can
148 be either a scalar or an arrayref containing scalars.
150 print $c->request->body_parameters->{field};
151 print $c->request->body_parameters->{field}->[0];
153 These are the parameters from the POST part of the request, if any.
155 =head2 $req->body_params
157 Shortcut for body_parameters.
161 sub body_parameters {
162 my ( $self, $params ) = @_;
163 $self->{_context}->prepare_body;
164 $self->{body_parameters} = $params if $params;
165 return $self->{body_parameters};
168 =head2 $req->content_encoding
170 Shortcut for $req->headers->content_encoding.
172 =head2 $req->content_length
174 Shortcut for $req->headers->content_length.
176 =head2 $req->content_type
178 Shortcut for $req->headers->content_type.
182 A convenient method to access $req->cookies.
184 $cookie = $c->request->cookie('name');
185 @cookies = $c->request->cookie;
193 return keys %{ $self->cookies };
200 unless ( exists $self->cookies->{$name} ) {
204 return $self->cookies->{$name};
210 Returns a reference to a hash containing the cookies.
212 print $c->request->cookies->{mycookie}->value;
214 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
219 Shortcut for $req->headers->header.
223 Returns an L<HTTP::Headers> object containing the headers for the current request.
225 print $c->request->headers->header('X-Catalyst');
227 =head2 $req->hostname
229 Returns the hostname of the client.
236 if ( @_ == 0 && not $self->{hostname} ) {
238 gethostbyaddr( inet_aton( $self->address ), AF_INET );
242 $self->{hostname} = shift;
245 return $self->{hostname};
250 Alias for $req->body.
254 This contains the matching part of a Regex action. Otherwise
255 it returns the same as 'action', except for default actions,
256 which return an empty string.
260 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
264 Returns GET and POST parameters with a CGI.pm-compatible param method. This
265 is an alternative method for accessing parameters in $c->req->parameters.
267 $value = $c->request->param( 'foo' );
268 @values = $c->request->param( 'foo' );
269 @params = $c->request->param;
271 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
272 arguments to this method, like this:
274 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
276 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
277 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
278 (creating it if it didn't exist before), and C<quxx> as another value for
287 return keys %{ $self->parameters };
294 unless ( exists $self->parameters->{$param} ) {
295 return wantarray ? () : undef;
298 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
300 ? @{ $self->parameters->{$param} }
301 : $self->parameters->{$param}->[0];
305 ? ( $self->parameters->{$param} )
306 : $self->parameters->{$param};
311 $self->parameters->{$field} = [@_];
315 =head2 $req->parameters
317 Returns a reference to a hash containing GET and POST parameters. Values can
318 be either a scalar or an arrayref containing scalars.
320 print $c->request->parameters->{field};
321 print $c->request->parameters->{field}->[0];
323 This is the combination of C<query_parameters> and C<body_parameters>.
327 Shortcut for $req->parameters.
332 my ( $self, $params ) = @_;
333 $self->{_context}->prepare_body;
336 $self->{parameters} = $params;
339 $self->{_context}->log->warn(
340 "Attempt to retrieve '$params' with req->params(), " .
341 "you probably meant to call req->param('$params')" );
344 return $self->{parameters};
349 Returns the path, i.e. the part of the URI after $req->base, for the current request.
351 =head2 $req->path_info
353 Alias for path, added for compability with L<CGI>.
358 my ( $self, $params ) = @_;
361 $self->uri->path($params);
364 return $self->{path} if $self->{path};
367 my $path = $self->uri->path;
368 my $location = $self->base->path;
369 $path =~ s/^(\Q$location\E)?//;
371 $self->{path} = $path;
376 =head2 $req->protocol
378 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
380 =head2 $req->query_parameters
382 Returns a reference to a hash containing query string (GET) parameters. Values can
383 be either a scalar or an arrayref containing scalars.
385 print $c->request->query_parameters->{field};
386 print $c->request->query_parameters->{field}->[0];
388 =head2 $req->read( [$maxlength] )
390 Reads a chunk of data from the request body. This method is intended to be
391 used in a while loop, reading $maxlength bytes on every call. $maxlength
392 defaults to the size of the request if not specified.
394 You have to set MyApp->config->{parse_on_demand} to use this directly.
398 sub read { shift->{_context}->read(@_); }
402 Shortcut for $req->headers->referer. Returns the referring page.
406 Returns true or false, indicating whether the connection is secure (https).
408 =head2 $req->snippets
410 Returns a reference to an array containing regex snippets.
412 my @snippets = @{ $c->request->snippets };
416 A convenient method to access $req->uploads.
418 $upload = $c->request->upload('field');
419 @uploads = $c->request->upload('field');
420 @fields = $c->request->upload;
422 for my $upload ( $c->request->upload('field') ) {
423 print $upload->filename;
432 return keys %{ $self->uploads };
439 unless ( exists $self->uploads->{$upload} ) {
440 return wantarray ? () : undef;
443 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
445 ? @{ $self->uploads->{$upload} }
446 : $self->uploads->{$upload}->[0];
450 ? ( $self->uploads->{$upload} )
451 : $self->uploads->{$upload};
457 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
459 if ( exists $self->uploads->{$field} ) {
460 for ( $self->uploads->{$field} ) {
461 $_ = [$_] unless ref($_) eq "ARRAY";
462 push( @$_, $upload );
466 $self->uploads->{$field} = $upload;
474 Returns a reference to a hash containing uploads. Values can be either a
475 hashref or a arrayref containing L<Catalyst::Request::Upload> objects.
477 my $upload = $c->request->uploads->{field};
478 my $upload = $c->request->uploads->{field}->[0];
483 my ( $self, $uploads ) = @_;
484 $self->{_context}->prepare_body;
485 $self->{uploads} = $uploads if $uploads;
486 return $self->{uploads};
491 Returns a URI object for the current request. Stringifies to the URI text.
493 =head2 $req->uri_with( { key => 'value' } );
495 Returns a rewriten URI object for the current uri. Key/value pairs passed in
496 will override existing parameters. Unmodified pairs will be preserved.
501 my( $self, $args ) = @_;
503 carp( 'No arguments passed to uri_with()' ) unless $args;
505 my $uri = $self->uri->clone;
516 Returns the currently logged in user. Deprecated. The method recommended for
517 newer plugins is $c->user.
519 =head2 $req->user_agent
521 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
526 Sebastian Riedel, C<sri@cpan.org>
528 Marcus Ramberg, C<mramberg@cpan.org>
532 This program is free software, you can redistribute it and/or modify
533 it under the same terms as Perl itself.