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.
82 Returns the requested action as a L<Catalyst::Action> object.
86 Returns the IP address of the client.
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 =item $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 =item $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 =item $req->content_encoding
171 Shortcut for $req->headers->content_encoding.
173 =item $req->content_length
175 Shortcut for $req->headers->content_length.
177 =item $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');
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'.
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 =item $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;
334 $self->{parameters} = $params if $params;
335 return $self->{parameters};
340 Returns the path, i.e. the part of the URI after $req->base, for the current request.
342 =item $req->path_info
344 Alias for path, added for compability with L<CGI>.
349 my ( $self, $params ) = @_;
352 $self->uri->path($params);
355 return $self->{path} if $self->{path};
358 my $path = $self->uri->path;
359 my $location = $self->base->path;
360 $path =~ s/^(\Q$location\E)?//;
361 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
363 $self->{path} = $path;
370 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
372 =item $req->query_parameters
374 Returns a reference to a hash containing query string (GET) parameters. Values can
375 be either a scalar or an arrayref containing scalars.
377 print $c->request->query_parameters->{field};
378 print $c->request->query_parameters->{field}->[0];
380 =item $req->read( [$maxlength] )
382 Reads a chunk of data from the request body. This method is intended to be
383 used in a while loop, reading $maxlength bytes on every call. $maxlength
384 defaults to the size of the request if not specified.
386 You have to set MyApp->config->{parse_on_demand} to use this directly.
390 sub read { shift->{_context}->read(@_); }
394 Shortcut for $req->headers->referer. Returns the referring page.
398 Returns true or false, indicating whether the connection is secure (https).
402 Returns a reference to an array containing regex snippets.
404 my @snippets = @{ $c->request->snippets };
408 A convenient method to access $req->uploads.
410 $upload = $c->request->upload('field');
411 @uploads = $c->request->upload('field');
412 @fields = $c->request->upload;
414 for my $upload ( $c->request->upload('field') ) {
415 print $upload->filename;
424 return keys %{ $self->uploads };
431 unless ( exists $self->uploads->{$upload} ) {
432 return wantarray ? () : undef;
435 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
437 ? @{ $self->uploads->{$upload} }
438 : $self->uploads->{$upload}->[0];
442 ? ( $self->uploads->{$upload} )
443 : $self->uploads->{$upload};
449 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
451 if ( exists $self->uploads->{$field} ) {
452 for ( $self->uploads->{$field} ) {
453 $_ = [$_] unless ref($_) eq "ARRAY";
454 push( @$_, $upload );
458 $self->uploads->{$field} = $upload;
466 Returns a reference to a hash containing uploads. Values can be either a
467 hashref or a arrayref containing L<Catalyst::Request::Upload> objects.
469 my $upload = $c->request->uploads->{field};
470 my $upload = $c->request->uploads->{field}->[0];
475 my ( $self, $uploads ) = @_;
476 $self->{_context}->prepare_body;
477 $self->{uploads} = $uploads if $uploads;
478 return $self->{uploads};
483 Returns a URI object for the current request. Stringifies to the URI text.
487 Returns the currently logged in user. Deprecated. The method recommended for
488 newer plugins is $c->user.
490 =item $req->user_agent
492 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
499 Sebastian Riedel, C<sri@cpan.org>
501 Marcus Ramberg, C<mramberg@cpan.org>
505 This program is free software, you can redistribute it and/or modify
506 it under the same terms as Perl itself.