1 package Catalyst::Request;
4 use base 'Class::Accessor::Fast';
6 use IO::Socket qw[AF_INET inet_aton];
8 __PACKAGE__->mk_accessors(
9 qw/action address arguments cookies headers match method
10 protocol query_parameters secure snippets uri user/
14 *body_params = \&body_parameters;
16 *params = \¶meters;
17 *query_params = \&query_parameters;
20 sub content_encoding { shift->headers->content_encoding(@_) }
21 sub content_length { shift->headers->content_length(@_) }
22 sub content_type { shift->headers->content_type(@_) }
23 sub header { shift->headers->header(@_) }
24 sub referer { shift->headers->referer(@_) }
25 sub user_agent { shift->headers->user_agent(@_) }
29 Catalyst::Request - provides information about the current client request
40 $req->body_parameters;
41 $req->content_encoding;
57 $req->query_parameters;
72 This is the Catalyst Request class, which provides an interface to data for the
73 current client request. The request object is prepared by L<Catalyst::Engine>,
74 thus hiding the details of the particular engine implementation.
80 Returns the requested action as a L<Catalyst::Action> object.
84 Returns the IP address of the client.
86 =head2 $req->arguments
88 Returns a reference to an array containing the arguments.
90 print $c->request->arguments->[0];
92 For example, if your action was
94 package MyApp::C::Foo;
100 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
101 would be the first and only argument.
105 Shortcut for arguments.
109 Contains the URI base. This will always have a trailing slash.
111 If your application was queried with the URI
112 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
117 my ( $self, $base ) = @_;
119 return $self->{base} unless $base;
121 $self->{base} = $base;
123 # set the value in path for backwards-compat
128 return $self->{base};
133 Returns the message body of the request, unless Content-Type is
134 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
139 my ( $self, $body ) = @_;
140 $self->{_context}->prepare_body;
141 return $self->{_body}->body;
144 =head2 $req->body_parameters
146 Returns a reference to a hash containing body (POST) parameters. Values can
147 be either a scalar or an arrayref containing scalars.
149 print $c->request->body_parameters->{field};
150 print $c->request->body_parameters->{field}->[0];
152 These are the parameters from the POST part of the request, if any.
154 =head2 $req->body_params
156 Shortcut for body_parameters.
160 sub body_parameters {
161 my ( $self, $params ) = @_;
162 $self->{_context}->prepare_body;
163 $self->{body_parameters} = $params if $params;
164 return $self->{body_parameters};
167 =head2 $req->content_encoding
169 Shortcut for $req->headers->content_encoding.
171 =head2 $req->content_length
173 Shortcut for $req->headers->content_length.
175 =head2 $req->content_type
177 Shortcut for $req->headers->content_type.
181 A convenient method to access $req->cookies.
183 $cookie = $c->request->cookie('name');
184 @cookies = $c->request->cookie;
192 return keys %{ $self->cookies };
199 unless ( exists $self->cookies->{$name} ) {
203 return $self->cookies->{$name};
209 Returns a reference to a hash containing the cookies.
211 print $c->request->cookies->{mycookie}->value;
213 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
218 Shortcut for $req->headers->header.
222 Returns an L<HTTP::Headers> object containing the headers for the current request.
224 print $c->request->headers->header('X-Catalyst');
226 =head2 $req->hostname
228 Returns the hostname of the client.
235 if ( @_ == 0 && not $self->{hostname} ) {
237 gethostbyaddr( inet_aton( $self->address ), AF_INET );
241 $self->{hostname} = shift;
244 return $self->{hostname};
249 Alias for $req->body.
253 This contains the matching part of a Regex action. Otherwise
254 it returns the same as 'action'.
258 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
262 Returns GET and POST parameters with a CGI.pm-compatible param method. This
263 is an alternative method for accessing parameters in $c->req->parameters.
265 $value = $c->request->param( 'foo' );
266 @values = $c->request->param( 'foo' );
267 @params = $c->request->param;
269 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
270 arguments to this method, like this:
272 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
274 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
275 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
276 (creating it if it didn't exist before), and C<quxx> as another value for
285 return keys %{ $self->parameters };
292 unless ( exists $self->parameters->{$param} ) {
293 return wantarray ? () : undef;
296 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
298 ? @{ $self->parameters->{$param} }
299 : $self->parameters->{$param}->[0];
303 ? ( $self->parameters->{$param} )
304 : $self->parameters->{$param};
309 $self->parameters->{$field} = [@_];
313 =head2 $req->parameters
315 Returns a reference to a hash containing GET and POST parameters. Values can
316 be either a scalar or an arrayref containing scalars.
318 print $c->request->parameters->{field};
319 print $c->request->parameters->{field}->[0];
321 This is the combination of C<query_parameters> and C<body_parameters>.
325 Shortcut for $req->parameters.
330 my ( $self, $params ) = @_;
331 $self->{_context}->prepare_body;
332 $self->{parameters} = $params if $params;
333 return $self->{parameters};
338 Returns the path, i.e. the part of the URI after $req->base, for the current request.
340 =head2 $req->path_info
342 Alias for path, added for compability with L<CGI>.
347 my ( $self, $params ) = @_;
350 $self->uri->path($params);
353 return $self->{path} if $self->{path};
356 my $path = $self->uri->path;
357 my $location = $self->base->path;
358 $path =~ s/^(\Q$location\E)?//;
359 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
361 $self->{path} = $path;
366 =head2 $req->protocol
368 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
370 =head2 $req->query_parameters
372 Returns a reference to a hash containing query string (GET) parameters. Values can
373 be either a scalar or an arrayref containing scalars.
375 print $c->request->query_parameters->{field};
376 print $c->request->query_parameters->{field}->[0];
378 =head2 $req->read( [$maxlength] )
380 Reads a chunk of data from the request body. This method is intended to be
381 used in a while loop, reading $maxlength bytes on every call. $maxlength
382 defaults to the size of the request if not specified.
384 You have to set MyApp->config->{parse_on_demand} to use this directly.
388 sub read { shift->{_context}->read(@_); }
392 Shortcut for $req->headers->referer. Returns the referring page.
396 Returns true or false, indicating whether the connection is secure (https).
398 =head2 $req->snippets
400 Returns a reference to an array containing regex snippets.
402 my @snippets = @{ $c->request->snippets };
406 A convenient method to access $req->uploads.
408 $upload = $c->request->upload('field');
409 @uploads = $c->request->upload('field');
410 @fields = $c->request->upload;
412 for my $upload ( $c->request->upload('field') ) {
413 print $upload->filename;
422 return keys %{ $self->uploads };
429 unless ( exists $self->uploads->{$upload} ) {
430 return wantarray ? () : undef;
433 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
435 ? @{ $self->uploads->{$upload} }
436 : $self->uploads->{$upload}->[0];
440 ? ( $self->uploads->{$upload} )
441 : $self->uploads->{$upload};
447 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
449 if ( exists $self->uploads->{$field} ) {
450 for ( $self->uploads->{$field} ) {
451 $_ = [$_] unless ref($_) eq "ARRAY";
452 push( @$_, $upload );
456 $self->uploads->{$field} = $upload;
464 Returns a reference to a hash containing uploads. Values can be either a
465 hashref or a arrayref containing L<Catalyst::Request::Upload> objects.
467 my $upload = $c->request->uploads->{field};
468 my $upload = $c->request->uploads->{field}->[0];
473 my ( $self, $uploads ) = @_;
474 $self->{_context}->prepare_body;
475 $self->{uploads} = $uploads if $uploads;
476 return $self->{uploads};
481 Returns a URI object for the current request. Stringifies to the URI text.
485 Returns the currently logged in user. Deprecated. The method recommended for
486 newer plugins is $c->user.
488 =head2 $req->user_agent
490 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
495 Sebastian Riedel, C<sri@cpan.org>
497 Marcus Ramberg, C<mramberg@cpan.org>
501 This program is free software, you can redistribute it and/or modify
502 it under the same terms as Perl itself.