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/
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 - Catalyst Request Class
41 $req->body_parameters;
42 $req->content_encoding;
58 $req->query_parameters;
72 This is the Catalyst Request class, which provides a set of accessors to the
73 request data. The request object is prepared by the specialized Catalyst
74 Engine module thus hiding the details of the particular engine implementation.
83 Contains the requested action.
85 print $c->request->action;
89 Contains the remote address.
91 print $c->request->address
95 Shortcut for arguments
99 Returns a reference to an array containing the arguments.
101 print $c->request->arguments->[0];
103 For example, if your action was
105 package MyApp::C::Foo;
111 And the URI for the request was C<http://.../foo/moose/bah> the string C<bah>
112 would be the first and only argument.
116 Contains the URI base. This will always have a trailing slash.
118 If your application was queried with the URI C<http://localhost:3000/some/path>
119 then C<base> is C<http://localhost:3000/>.
124 my ( $self, $base ) = @_;
126 return $self->{base} unless $base;
128 $self->{base} = $base;
130 # set the value in path for backwards-compat
135 return $self->{base};
140 Contains the message body of the request unless Content-Type is
141 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
143 print $c->request->body
148 my ( $self, $body ) = @_;
149 $self->{_context}->prepare_body;
150 return $self->{_body}->body;
153 =item $req->body_parameters
155 Returns a reference to a hash containing body parameters. Values can
156 be either a scalar or an arrayref containing scalars.
158 print $c->request->body_parameters->{field};
159 print $c->request->body_parameters->{field}->[0];
161 These are the parameters from the POST part of the request, if any.
163 =item $req->body_params
165 An alias for body_parameters.
169 sub body_parameters {
170 my ( $self, $params ) = @_;
171 $self->{_context}->prepare_body;
172 $self->{body_parameters} = $params if $params;
173 return $self->{body_parameters};
176 =item $req->content_encoding
178 Shortcut to $req->headers->content_encoding
180 =item $req->content_length
182 Shortcut to $req->headers->content_length
184 =item $req->content_type
186 Shortcut to $req->headers->content_type
190 A convenient method to $req->cookies.
192 $cookie = $c->request->cookie('name');
193 @cookies = $c->request->cookie;
201 return keys %{ $self->cookies };
208 unless ( exists $self->cookies->{$name} ) {
212 return $self->cookies->{$name};
218 Returns a reference to a hash containing the cookies.
220 print $c->request->cookies->{mycookie}->value;
222 The cookies in the hash are indexed by name, and the values are C<CGI::Cookie>
227 Shortcut to $req->headers->header
231 Returns an L<HTTP::Headers> object containing the headers.
233 print $c->request->headers->header('X-Catalyst');
237 Lookup the current users DNS hostname.
239 print $c->request->hostname
246 if ( @_ == 0 && not $self->{hostname} ) {
248 gethostbyaddr( inet_aton( $self->address ), AF_INET );
252 $self->{hostname} = shift;
255 return $self->{hostname};
260 Shortcut for $req->body.
264 This contains the matching part of a regexp action. Otherwise
265 it returns the same as 'action'.
267 print $c->request->match;
271 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
273 print $c->request->method;
277 Get request parameters with a CGI.pm-compatible param method. This
278 is a method for accessing parameters in $c->req->parameters.
280 $value = $c->request->param( 'foo' );
281 @values = $c->request->param( 'foo' );
282 @params = $c->request->param;
284 Like L<CGI>, and B<unlike> previous versions of Catalyst, passing multiple
285 arguments to this method, like this:
287 $c->request( 'foo', 'bar', 'gorch', 'quxx' );
289 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
290 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
291 (creating it if it didn't exist before), and C<quxx> as another value for C<gorch>.
299 return keys %{ $self->parameters };
306 unless ( exists $self->parameters->{$param} ) {
307 return wantarray ? () : undef;
310 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
312 ? @{ $self->parameters->{$param} }
313 : $self->parameters->{$param}->[0];
317 ? ( $self->parameters->{$param} )
318 : $self->parameters->{$param};
323 $self->parameters->{$field} = [@_];
329 Shortcut for $req->parameters.
331 =item $req->parameters
333 Returns a reference to a hash containing parameters. Values can
334 be either a scalar or an arrayref containing scalars.
336 print $c->request->parameters->{field};
337 print $c->request->parameters->{field}->[0];
339 This is the combination of C<query_parameters> and C<body_parameters>.
344 my ( $self, $params ) = @_;
345 $self->{_context}->prepare_body;
346 $self->{parameters} = $params if $params;
347 return $self->{parameters};
354 print $c->request->path;
356 =item $req->path_info
358 alias for path, added for compability with L<CGI>
363 my ( $self, $params ) = @_;
366 $self->uri->path($params);
369 return $self->{path} if $self->{path};
372 my $path = $self->uri->path;
373 my $location = $self->base->path;
374 $path =~ s/^(\Q$location\E)?//;
375 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
377 $self->{path} = $path;
384 Contains the protocol.
386 =item $req->query_parameters
388 Returns a reference to a hash containing query parameters. Values can
389 be either a scalar or an arrayref containing scalars.
391 print $c->request->query_parameters->{field};
392 print $c->request->query_parameters->{field}->[0];
394 These are the parameters from the query string portion of the request's URI, if
397 =item $req->read( [$maxlength] )
399 Read a chunk of data from the request body. This method is designed to be
400 used in a while loop, reading $maxlength bytes on every call. $maxlength
401 defaults to the size of the request if not specified.
403 You have to set MyApp->config->{parse_on_demand} to use this directly.
407 sub read { shift->{_context}->read(@_); }
411 Shortcut to $req->headers->referer. Referring page.
415 Contains a boolean denoting whether the communication is secure.
419 Returns a reference to an array containing regex snippets.
421 my @snippets = @{ $c->request->snippets };
425 A convenient method to $req->uploads.
427 $upload = $c->request->upload('field');
428 @uploads = $c->request->upload('field');
429 @fields = $c->request->upload;
431 for my $upload ( $c->request->upload('field') ) {
432 print $upload->filename;
441 return keys %{ $self->uploads };
448 unless ( exists $self->uploads->{$upload} ) {
449 return wantarray ? () : undef;
452 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
454 ? @{ $self->uploads->{$upload} }
455 : $self->uploads->{$upload}->[0];
459 ? ( $self->uploads->{$upload} )
460 : $self->uploads->{$upload};
466 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
468 if ( exists $self->uploads->{$field} ) {
469 for ( $self->uploads->{$field} ) {
470 $_ = [$_] unless ref($_) eq "ARRAY";
471 push( @$_, $upload );
475 $self->uploads->{$field} = $upload;
483 Returns a reference to a hash containing uploads. Values can be either a
484 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
486 my $upload = $c->request->uploads->{field};
487 my $upload = $c->request->uploads->{field}->[0];
492 my ( $self, $uploads ) = @_;
493 $self->{_context}->prepare_body;
494 $self->{uploads} = $uploads if $uploads;
495 return $self->{uploads};
500 Returns a URI object for the request.
502 =item $req->user_agent
504 Shortcut to $req->headers->user_agent. User Agent version string.
510 Sebastian Riedel, C<sri@cpan.org>
511 Marcus Ramberg, C<mramberg@cpan.org>
515 This program is free software, you can redistribute it and/or modify
516 it under the same terms as Perl itself.