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/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 sub action { shift->{_context}->action(@_) }
90 Returns the IP address of the client.
94 Returns a reference to an array containing the arguments.
96 print $c->request->arguments->[0];
98 For example, if your action was
100 package MyApp::C::Foo;
106 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
107 would be the first and only argument.
111 Shortcut for arguments.
115 Contains the URI base. This will always have a trailing slash.
117 If your application was queried with the URI
118 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
123 my ( $self, $base ) = @_;
125 return $self->{base} unless $base;
127 $self->{base} = $base;
129 # set the value in path for backwards-compat
134 return $self->{base};
139 Returns the message body of the request, unless Content-Type is
140 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
145 my ( $self, $body ) = @_;
146 $self->{_context}->prepare_body;
147 return $self->{_body}->body;
150 =item $req->body_parameters
152 Returns a reference to a hash containing body (POST) parameters. Values can
153 be either a scalar or an arrayref containing scalars.
155 print $c->request->body_parameters->{field};
156 print $c->request->body_parameters->{field}->[0];
158 These are the parameters from the POST part of the request, if any.
160 =item $req->body_params
162 Shortcut for body_parameters.
166 sub body_parameters {
167 my ( $self, $params ) = @_;
168 $self->{_context}->prepare_body;
169 $self->{body_parameters} = $params if $params;
170 return $self->{body_parameters};
173 =item $req->content_encoding
175 Shortcut for $req->headers->content_encoding.
177 =item $req->content_length
179 Shortcut for $req->headers->content_length.
181 =item $req->content_type
183 Shortcut for $req->headers->content_type.
187 A convenient method to access $req->cookies.
189 $cookie = $c->request->cookie('name');
190 @cookies = $c->request->cookie;
198 return keys %{ $self->cookies };
205 unless ( exists $self->cookies->{$name} ) {
209 return $self->cookies->{$name};
215 Returns a reference to a hash containing the cookies.
217 print $c->request->cookies->{mycookie}->value;
219 The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
224 Shortcut for $req->headers->header.
228 Returns an L<HTTP::Headers> object containing the headers for the current request.
230 print $c->request->headers->header('X-Catalyst');
234 Returns the hostname of the client.
241 if ( @_ == 0 && not $self->{hostname} ) {
243 gethostbyaddr( inet_aton( $self->address ), AF_INET );
247 $self->{hostname} = shift;
250 return $self->{hostname};
255 Alias for $req->body.
259 This contains the matching part of a Regex action. Otherwise
260 it returns the same as 'action'.
264 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
268 Returns GET and POST parameters with a CGI.pm-compatible param method. This
269 is an alternative method for accessing parameters in $c->req->parameters.
271 $value = $c->request->param( 'foo' );
272 @values = $c->request->param( 'foo' );
273 @params = $c->request->param;
275 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
276 arguments to this method, like this:
278 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
280 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
281 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
282 (creating it if it didn't exist before), and C<quxx> as another value for
291 return keys %{ $self->parameters };
298 unless ( exists $self->parameters->{$param} ) {
299 return wantarray ? () : undef;
302 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
304 ? @{ $self->parameters->{$param} }
305 : $self->parameters->{$param}->[0];
309 ? ( $self->parameters->{$param} )
310 : $self->parameters->{$param};
315 $self->parameters->{$field} = [@_];
319 =item $req->parameters
321 Returns a reference to a hash containing GET and POST parameters. Values can
322 be either a scalar or an arrayref containing scalars.
324 print $c->request->parameters->{field};
325 print $c->request->parameters->{field}->[0];
327 This is the combination of C<query_parameters> and C<body_parameters>.
331 Shortcut for $req->parameters.
336 my ( $self, $params ) = @_;
337 $self->{_context}->prepare_body;
338 $self->{parameters} = $params if $params;
339 return $self->{parameters};
344 Returns the path, i.e. the part of the URI after $req->base, for the current request.
346 =item $req->path_info
348 Alias for path, added for compability with L<CGI>.
353 my ( $self, $params ) = @_;
356 $self->uri->path($params);
359 return $self->{path} if $self->{path};
362 my $path = $self->uri->path;
363 my $location = $self->base->path;
364 $path =~ s/^(\Q$location\E)?//;
365 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
367 $self->{path} = $path;
374 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
376 =item $req->query_parameters
378 Returns a reference to a hash containing query string (GET) parameters. Values can
379 be either a scalar or an arrayref containing scalars.
381 print $c->request->query_parameters->{field};
382 print $c->request->query_parameters->{field}->[0];
384 =item $req->read( [$maxlength] )
386 Reads a chunk of data from the request body. This method is intended to be
387 used in a while loop, reading $maxlength bytes on every call. $maxlength
388 defaults to the size of the request if not specified.
390 You have to set MyApp->config->{parse_on_demand} to use this directly.
394 sub read { shift->{_context}->read(@_); }
398 Shortcut for $req->headers->referer. Returns the referring page.
402 Returns true or false, indicating whether the connection is secure (https).
406 Returns a reference to an array containing regex snippets.
408 my @snippets = @{ $c->request->snippets };
412 A convenient method to access $req->uploads.
414 $upload = $c->request->upload('field');
415 @uploads = $c->request->upload('field');
416 @fields = $c->request->upload;
418 for my $upload ( $c->request->upload('field') ) {
419 print $upload->filename;
428 return keys %{ $self->uploads };
435 unless ( exists $self->uploads->{$upload} ) {
436 return wantarray ? () : undef;
439 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
441 ? @{ $self->uploads->{$upload} }
442 : $self->uploads->{$upload}->[0];
446 ? ( $self->uploads->{$upload} )
447 : $self->uploads->{$upload};
453 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
455 if ( exists $self->uploads->{$field} ) {
456 for ( $self->uploads->{$field} ) {
457 $_ = [$_] unless ref($_) eq "ARRAY";
458 push( @$_, $upload );
462 $self->uploads->{$field} = $upload;
470 Returns a reference to a hash containing uploads. Values can be either a
471 hashref or a arrayref containing L<Catalyst::Request::Upload> objects.
473 my $upload = $c->request->uploads->{field};
474 my $upload = $c->request->uploads->{field}->[0];
479 my ( $self, $uploads ) = @_;
480 $self->{_context}->prepare_body;
481 $self->{uploads} = $uploads if $uploads;
482 return $self->{uploads};
487 Returns a URI object for the current request. Stringifies to the URI text.
491 Returns the currently logged in user. Deprecated. The method recommended for
492 newer plugins is $c->user.
494 =item $req->user_agent
496 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
503 Sebastian Riedel, C<sri@cpan.org>
505 Marcus Ramberg, C<mramberg@cpan.org>
509 This program is free software, you can redistribute it and/or modify
510 it under the same terms as Perl itself.