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 body base cookies headers match method
10 parameters path protocol secure snippets uploads user/
15 *params = \¶meters;
17 sub content_encoding { shift->headers->content_encoding(@_) }
18 sub content_length { shift->headers->content_length(@_) }
19 sub content_type { shift->headers->content_type(@_) }
20 sub header { shift->headers->header(@_) }
21 sub referer { shift->headers->referer(@_) }
22 sub user_agent { shift->headers->user_agent(@_) }
26 Catalyst::Request - Catalyst Request Class
38 $req->content_encoding;
67 This is the Catalyst Request class, which provides a set of accessors to the
68 request data. The request object is prepared by the specialized Catalyst
69 Engine module thus hiding the details of the particular engine implementation.
78 Contains the requested action.
80 print $c->request->action;
84 Contains the remote address.
86 print $c->request->address
90 Shortcut for arguments
94 Returns a reference to an array containing the arguments.
96 print $c->request->arguments->[0];
100 Contains the url base. This will always have a trailing slash.
104 Contains the message body of the request unless Content-Type is
105 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
107 print $c->request->body
109 =item $req->content_encoding
111 Shortcut to $req->headers->content_encoding
113 =item $req->content_length
115 Shortcut to $req->headers->content_length
117 =item $req->content_type
119 Shortcut to $req->headers->content_type
123 A convenient method to $req->cookies.
125 $cookie = $c->request->cookie('name');
126 @cookies = $c->request->cookie;
134 return keys %{ $self->cookies };
141 unless ( exists $self->cookies->{$name} ) {
145 return $self->cookies->{$name};
151 Returns a reference to a hash containing the cookies.
153 print $c->request->cookies->{mycookie}->value;
157 Shortcut to $req->headers->header
161 Returns an L<HTTP::Headers> object containing the headers.
163 print $c->request->headers->header('X-Catalyst');
167 Lookup the current users DNS hostname.
169 print $c->request->hostname
176 if ( @_ == 0 && not $self->{hostname} ) {
177 $self->{hostname} = gethostbyaddr( inet_aton( $self->address ), AF_INET );
181 $self->{hostname} = shift;
184 return $self->{hostname};
189 Shortcut for $req->body.
193 This contains the matching part of a regexp action. Otherwise
194 it returns the same as 'action'.
196 print $c->request->match;
200 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
202 print $c->request->method;
206 Get request parameters with a CGI.pm-compatible param method. This
207 is a method for accessing parameters in $c->req->parameters.
209 $value = $c->request->param('foo');
210 @values = $c->request->param('foo');
211 @params = $c->request->param;
219 return keys %{ $self->parameters };
226 unless ( exists $self->parameters->{$param} ) {
227 return wantarray ? () : undef;
230 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
232 ? @{ $self->parameters->{$param} }
233 : $self->parameters->{$param}->[0];
237 ? ( $self->parameters->{$param} )
238 : $self->parameters->{$param};
244 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
246 next unless defined $field;
248 if ( exists $self->parameters->{$field} ) {
249 for ( $self->parameters->{$field} ) {
250 $_ = [$_] unless ref($_) eq "ARRAY";
255 $self->parameters->{$field} = $value;
263 Shortcut for $req->parameters.
265 =item $req->parameters
267 Returns a reference to a hash containing parameters. Values can
268 be either a scalar or an arrayref containing scalars.
270 print $c->request->parameters->{field};
271 print $c->request->parameters->{field}->[0];
277 print $c->request->path;
281 Contains the protocol.
285 Shortcut to $req->headers->referer. Referring page.
289 Contains a boolean whether the communciation is secure.
293 Returns a reference to an array containing regex snippets.
295 my @snippets = @{ $c->request->snippets };
299 A convenient method to $req->uploads.
301 $upload = $c->request->upload('field');
302 @uploads = $c->request->upload('field');
303 @fields = $c->request->upload;
305 for my $upload ( $c->request->upload('field') ) {
306 print $upload->filename;
315 return keys %{ $self->uploads };
322 unless ( exists $self->uploads->{$upload} ) {
323 return wantarray ? () : undef;
326 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
328 ? @{ $self->uploads->{$upload} }
329 : $self->uploads->{$upload}->[0];
333 ? ( $self->uploads->{$upload} )
334 : $self->uploads->{$upload};
340 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
342 if ( exists $self->uploads->{$field} ) {
343 for ( $self->uploads->{$field} ) {
344 $_ = [$_] unless ref($_) eq "ARRAY";
345 push( @$_, $upload );
349 $self->uploads->{$field} = $upload;
357 Returns a reference to a hash containing uploads. Values can be either a
358 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
360 my $upload = $c->request->uploads->{field};
361 my $upload = $c->request->uploads->{field}->[0];
365 Shortcut for C<< $req->base . $req->path >>.
371 my $path = shift || $self->path || '';
372 return $self->base . $path;
377 Contains the user name of user if authentication check was successful.
379 =item $req->user_agent
381 Shortcut to $req->headers->user_agent. User Agent version string.
387 Sebastian Riedel, C<sri@cpan.org>
388 Marcus Ramberg, C<mramberg@cpan.org>
392 This program is free software, you can redistribute it and/or modify
393 it under the same terms as Perl itself.