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 base 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 - Catalyst Request Class
41 $req->body_parameters;
42 $req->content_encoding;
58 $req->query_parameters;
73 This is the Catalyst Request class, which provides a set of accessors to the
74 request data. The request object is prepared by the specialized Catalyst
75 Engine module thus hiding the details of the particular engine implementation.
84 Contains the requested action.
86 print $c->request->action;
90 Contains the remote address.
92 print $c->request->address
96 Shortcut for arguments
100 Returns a reference to an array containing the arguments.
102 print $c->request->arguments->[0];
106 Contains the url base. This will always have a trailing slash.
110 Contains the message body of the request unless Content-Type is
111 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
113 print $c->request->body
118 my ( $self, $body ) = @_;
119 $self->{_context}->prepare_body;
120 return $self->{_body}->body;
123 =item $req->body_parameters
125 Returns a reference to a hash containing body parameters. Values can
126 be either a scalar or an arrayref containing scalars.
128 print $c->request->body_parameters->{field};
129 print $c->request->body_parameters->{field}->[0];
131 =item $req->body_params
133 An alias for body_parameters.
137 sub body_parameters {
138 my ( $self, $params ) = @_;
139 $self->{_context}->prepare_body;
140 $self->{body_parameters} = $params if $params;
141 return $self->{body_parameters};
144 =item $req->content_encoding
146 Shortcut to $req->headers->content_encoding
148 =item $req->content_length
150 Shortcut to $req->headers->content_length
152 =item $req->content_type
154 Shortcut to $req->headers->content_type
158 A convenient method to $req->cookies.
160 $cookie = $c->request->cookie('name');
161 @cookies = $c->request->cookie;
169 return keys %{ $self->cookies };
176 unless ( exists $self->cookies->{$name} ) {
180 return $self->cookies->{$name};
186 Returns a reference to a hash containing the cookies.
188 print $c->request->cookies->{mycookie}->value;
192 Shortcut to $req->headers->header
196 Returns an L<HTTP::Headers> object containing the headers.
198 print $c->request->headers->header('X-Catalyst');
202 Lookup the current users DNS hostname.
204 print $c->request->hostname
211 if ( @_ == 0 && not $self->{hostname} ) {
213 gethostbyaddr( inet_aton( $self->address ), AF_INET );
217 $self->{hostname} = shift;
220 return $self->{hostname};
225 Shortcut for $req->body.
229 This contains the matching part of a regexp action. Otherwise
230 it returns the same as 'action'.
232 print $c->request->match;
236 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
238 print $c->request->method;
242 Get request parameters with a CGI.pm-compatible param method. This
243 is a method for accessing parameters in $c->req->parameters.
245 $value = $c->request->param('foo');
246 @values = $c->request->param('foo');
247 @params = $c->request->param;
255 return keys %{ $self->parameters };
262 unless ( exists $self->parameters->{$param} ) {
263 return wantarray ? () : undef;
266 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
268 ? @{ $self->parameters->{$param} }
269 : $self->parameters->{$param}->[0];
273 ? ( $self->parameters->{$param} )
274 : $self->parameters->{$param};
280 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
282 next unless defined $field;
284 if ( exists $self->parameters->{$field} ) {
285 for ( $self->parameters->{$field} ) {
286 $_ = [$_] unless ref($_) eq "ARRAY";
291 $self->parameters->{$field} = $value;
299 Shortcut for $req->parameters.
301 =item $req->parameters
303 Returns a reference to a hash containing parameters. Values can
304 be either a scalar or an arrayref containing scalars.
306 print $c->request->parameters->{field};
307 print $c->request->parameters->{field}->[0];
312 my ( $self, $params ) = @_;
313 $self->{_context}->prepare_body;
314 $self->{parameters} = $params if $params;
315 return $self->{parameters};
322 print $c->request->path;
324 =item $req->path_info
326 alias for path, added for compability with L<CGI>
331 my ( $self, $params ) = @_;
334 $self->uri->path($params);
337 my $path = $self->uri->path;
338 my $location = $self->base->path;
339 $path =~ s/^(\Q$location\E)?//;
340 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
348 Contains the protocol.
350 =item $req->query_parameters
352 Returns a reference to a hash containing query parameters. Values can
353 be either a scalar or an arrayref containing scalars.
355 print $c->request->query_parameters->{field};
356 print $c->request->query_parameters->{field}->[0];
358 =item $req->read( [$maxlength] )
360 Read a chunk of data from the request body. This method is designed to be
361 used in a while loop, reading $maxlength bytes on every call. $maxlength
362 defaults to the size of the request if not specified.
364 You have to set MyApp->config->{parse_on_demand} to use this directly.
368 sub read { shift->{_context}->read(@_); }
372 Shortcut to $req->headers->referer. Referring page.
376 Contains a boolean denoting whether the communication is secure.
380 Returns a reference to an array containing regex snippets.
382 my @snippets = @{ $c->request->snippets };
386 A convenient method to $req->uploads.
388 $upload = $c->request->upload('field');
389 @uploads = $c->request->upload('field');
390 @fields = $c->request->upload;
392 for my $upload ( $c->request->upload('field') ) {
393 print $upload->filename;
402 return keys %{ $self->uploads };
409 unless ( exists $self->uploads->{$upload} ) {
410 return wantarray ? () : undef;
413 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
415 ? @{ $self->uploads->{$upload} }
416 : $self->uploads->{$upload}->[0];
420 ? ( $self->uploads->{$upload} )
421 : $self->uploads->{$upload};
427 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
429 if ( exists $self->uploads->{$field} ) {
430 for ( $self->uploads->{$field} ) {
431 $_ = [$_] unless ref($_) eq "ARRAY";
432 push( @$_, $upload );
436 $self->uploads->{$field} = $upload;
444 Returns a reference to a hash containing uploads. Values can be either a
445 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
447 my $upload = $c->request->uploads->{field};
448 my $upload = $c->request->uploads->{field}->[0];
453 my ( $self, $uploads ) = @_;
454 $self->{_context}->prepare_body;
455 $self->{uploads} = $uploads if $uploads;
456 return $self->{uploads};
461 Returns a URI object for the request.
465 Contains the user name of user if authentication check was successful.
467 =item $req->user_agent
469 Shortcut to $req->headers->user_agent. User Agent version string.
475 Sebastian Riedel, C<sri@cpan.org>
476 Marcus Ramberg, C<mramberg@cpan.org>
480 This program is free software, you can redistribute it and/or modify
481 it under the same terms as Perl itself.