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;
68 This is the Catalyst Request class, which provides a set of accessors to the
69 request data. The request object is prepared by the specialized Catalyst
70 Engine module thus hiding the details of the particular engine implementation.
79 Contains the requested action.
81 print $c->request->action;
85 Contains the remote address.
87 print $c->request->address
91 Shortcut for arguments
95 Returns a reference to an array containing the arguments.
97 print $c->request->arguments->[0];
101 Contains the url base. This will always have a trailing slash.
105 Contains the message body of the request unless Content-Type is
106 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
108 print $c->request->body
110 =item $req->content_encoding
112 Shortcut to $req->headers->content_encoding
114 =item $req->content_length
116 Shortcut to $req->headers->content_length
118 =item $req->content_type
120 Shortcut to $req->headers->content_type
124 A convenient method to $req->cookies.
126 $cookie = $c->request->cookie('name');
127 @cookies = $c->request->cookie;
135 return keys %{ $self->cookies };
142 unless ( exists $self->cookies->{$name} ) {
146 return $self->cookies->{$name};
152 Returns a reference to a hash containing the cookies.
154 print $c->request->cookies->{mycookie}->value;
158 Returns the complete URI, with the parameter query string.
164 my $full_uri = $self->uri;
166 if ( scalar $self->param ) {
168 foreach my $arg ( sort keys %{ $self->params } ) {
169 if ( ref $self->params->{$arg} ) {
170 my $list = $self->params->{$arg};
171 push @params, map { "$arg=" . $_ } sort @{$list};
173 push @params, "$arg=" . $self->params->{$arg};
176 $full_uri .= '?' . join( '&', @params );
183 Shortcut to $req->headers->header
187 Returns an L<HTTP::Headers> object containing the headers.
189 print $c->request->headers->header('X-Catalyst');
193 Lookup the current users DNS hostname.
195 print $c->request->hostname
202 if ( @_ == 0 && not $self->{hostname} ) {
203 $self->{hostname} = gethostbyaddr( inet_aton( $self->address ), AF_INET );
207 $self->{hostname} = shift;
210 return $self->{hostname};
215 Shortcut for $req->body.
219 This contains the matching part of a regexp action. Otherwise
220 it returns the same as 'action'.
222 print $c->request->match;
226 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
228 print $c->request->method;
232 Get request parameters with a CGI.pm-compatible param method. This
233 is a method for accessing parameters in $c->req->parameters.
235 $value = $c->request->param('foo');
236 @values = $c->request->param('foo');
237 @params = $c->request->param;
245 return keys %{ $self->parameters };
252 unless ( exists $self->parameters->{$param} ) {
253 return wantarray ? () : undef;
256 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
258 ? @{ $self->parameters->{$param} }
259 : $self->parameters->{$param}->[0];
263 ? ( $self->parameters->{$param} )
264 : $self->parameters->{$param};
270 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
272 next unless defined $field;
274 if ( exists $self->parameters->{$field} ) {
275 for ( $self->parameters->{$field} ) {
276 $_ = [$_] unless ref($_) eq "ARRAY";
281 $self->parameters->{$field} = $value;
289 Shortcut for $req->parameters.
291 =item $req->parameters
293 Returns a reference to a hash containing parameters. Values can
294 be either a scalar or an arrayref containing scalars.
296 print $c->request->parameters->{field};
297 print $c->request->parameters->{field}->[0];
303 print $c->request->path;
307 Contains the protocol.
311 Shortcut to $req->headers->referer. Referring page.
315 Contains a boolean whether the communciation is secure.
319 Returns a reference to an array containing regex snippets.
321 my @snippets = @{ $c->request->snippets };
325 A convenient method to $req->uploads.
327 $upload = $c->request->upload('field');
328 @uploads = $c->request->upload('field');
329 @fields = $c->request->upload;
331 for my $upload ( $c->request->upload('field') ) {
332 print $upload->filename;
341 return keys %{ $self->uploads };
348 unless ( exists $self->uploads->{$upload} ) {
349 return wantarray ? () : undef;
352 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
354 ? @{ $self->uploads->{$upload} }
355 : $self->uploads->{$upload}->[0];
359 ? ( $self->uploads->{$upload} )
360 : $self->uploads->{$upload};
366 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
368 if ( exists $self->uploads->{$field} ) {
369 for ( $self->uploads->{$field} ) {
370 $_ = [$_] unless ref($_) eq "ARRAY";
371 push( @$_, $upload );
375 $self->uploads->{$field} = $upload;
383 Returns a reference to a hash containing uploads. Values can be either a
384 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
386 my $upload = $c->request->uploads->{field};
387 my $upload = $c->request->uploads->{field}->[0];
391 Shortcut for C<< $req->base . $req->path >>.
397 my $path = shift || $self->path || '';
398 return $self->base . $path;
403 Contains the user name of user if authentication check was successful.
405 =item $req->user_agent
407 Shortcut to $req->headers->user_agent. User Agent version string.
413 Sebastian Riedel, C<sri@cpan.org>
414 Marcus Ramberg, C<mramberg@cpan.org>
418 This program is free software, you can redistribute it and/or modify
419 it under the same terms as Perl itself.