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 - 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.
111 my ( $self, $base ) = @_;
113 return $self->{base} unless $base;
115 $self->{base} = $base;
117 # set the value in path for backwards-compat
122 return $self->{base};
127 Contains the message body of the request unless Content-Type is
128 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
130 print $c->request->body
135 my ( $self, $body ) = @_;
136 $self->{_context}->prepare_body;
137 return $self->{_body}->body;
140 =item $req->body_parameters
142 Returns a reference to a hash containing body parameters. Values can
143 be either a scalar or an arrayref containing scalars.
145 print $c->request->body_parameters->{field};
146 print $c->request->body_parameters->{field}->[0];
148 =item $req->body_params
150 An alias for body_parameters.
154 sub body_parameters {
155 my ( $self, $params ) = @_;
156 $self->{_context}->prepare_body;
157 $self->{body_parameters} = $params if $params;
158 return $self->{body_parameters};
161 =item $req->content_encoding
163 Shortcut to $req->headers->content_encoding
165 =item $req->content_length
167 Shortcut to $req->headers->content_length
169 =item $req->content_type
171 Shortcut to $req->headers->content_type
175 A convenient method to $req->cookies.
177 $cookie = $c->request->cookie('name');
178 @cookies = $c->request->cookie;
186 return keys %{ $self->cookies };
193 unless ( exists $self->cookies->{$name} ) {
197 return $self->cookies->{$name};
203 Returns a reference to a hash containing the cookies.
205 print $c->request->cookies->{mycookie}->value;
209 Shortcut to $req->headers->header
213 Returns an L<HTTP::Headers> object containing the headers.
215 print $c->request->headers->header('X-Catalyst');
219 Lookup the current users DNS hostname.
221 print $c->request->hostname
228 if ( @_ == 0 && not $self->{hostname} ) {
230 gethostbyaddr( inet_aton( $self->address ), AF_INET );
234 $self->{hostname} = shift;
237 return $self->{hostname};
242 Shortcut for $req->body.
246 This contains the matching part of a regexp action. Otherwise
247 it returns the same as 'action'.
249 print $c->request->match;
253 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
255 print $c->request->method;
259 Get request parameters with a CGI.pm-compatible param method. This
260 is a method for accessing parameters in $c->req->parameters.
262 $value = $c->request->param('foo');
263 @values = $c->request->param('foo');
264 @params = $c->request->param;
272 return keys %{ $self->parameters };
279 unless ( exists $self->parameters->{$param} ) {
280 return wantarray ? () : undef;
283 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
285 ? @{ $self->parameters->{$param} }
286 : $self->parameters->{$param}->[0];
290 ? ( $self->parameters->{$param} )
291 : $self->parameters->{$param};
297 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
299 next unless defined $field;
301 if ( exists $self->parameters->{$field} ) {
302 for ( $self->parameters->{$field} ) {
303 $_ = [$_] unless ref($_) eq "ARRAY";
308 $self->parameters->{$field} = $value;
316 Shortcut for $req->parameters.
318 =item $req->parameters
320 Returns a reference to a hash containing parameters. Values can
321 be either a scalar or an arrayref containing scalars.
323 print $c->request->parameters->{field};
324 print $c->request->parameters->{field}->[0];
329 my ( $self, $params ) = @_;
330 $self->{_context}->prepare_body;
331 $self->{parameters} = $params if $params;
332 return $self->{parameters};
339 print $c->request->path;
341 =item $req->path_info
343 alias for path, added for compability with L<CGI>
348 my ( $self, $params ) = @_;
351 $self->uri->path($params);
354 return $self->{path} if $self->{path};
357 my $path = $self->uri->path;
358 my $location = $self->base->path;
359 $path =~ s/^(\Q$location\E)?//;
360 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
362 $self->{path} = $path;
369 Contains the protocol.
371 =item $req->query_parameters
373 Returns a reference to a hash containing query parameters. Values can
374 be either a scalar or an arrayref containing scalars.
376 print $c->request->query_parameters->{field};
377 print $c->request->query_parameters->{field}->[0];
379 =item $req->read( [$maxlength] )
381 Read a chunk of data from the request body. This method is designed to be
382 used in a while loop, reading $maxlength bytes on every call. $maxlength
383 defaults to the size of the request if not specified.
385 You have to set MyApp->config->{parse_on_demand} to use this directly.
389 sub read { shift->{_context}->read(@_); }
393 Shortcut to $req->headers->referer. Referring page.
397 Contains a boolean denoting whether the communication is secure.
401 Returns a reference to an array containing regex snippets.
403 my @snippets = @{ $c->request->snippets };
407 A convenient method to $req->uploads.
409 $upload = $c->request->upload('field');
410 @uploads = $c->request->upload('field');
411 @fields = $c->request->upload;
413 for my $upload ( $c->request->upload('field') ) {
414 print $upload->filename;
423 return keys %{ $self->uploads };
430 unless ( exists $self->uploads->{$upload} ) {
431 return wantarray ? () : undef;
434 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
436 ? @{ $self->uploads->{$upload} }
437 : $self->uploads->{$upload}->[0];
441 ? ( $self->uploads->{$upload} )
442 : $self->uploads->{$upload};
448 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
450 if ( exists $self->uploads->{$field} ) {
451 for ( $self->uploads->{$field} ) {
452 $_ = [$_] unless ref($_) eq "ARRAY";
453 push( @$_, $upload );
457 $self->uploads->{$field} = $upload;
465 Returns a reference to a hash containing uploads. Values can be either a
466 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
468 my $upload = $c->request->uploads->{field};
469 my $upload = $c->request->uploads->{field}->[0];
474 my ( $self, $uploads ) = @_;
475 $self->{_context}->prepare_body;
476 $self->{uploads} = $uploads if $uploads;
477 return $self->{uploads};
482 Returns a URI object for the request.
486 Contains the user name of user if authentication check was successful.
488 =item $req->user_agent
490 Shortcut to $req->headers->user_agent. User Agent version string.
496 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.