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 $uri = $self->uri;
167 if ( scalar $self->param ) {
169 foreach my $arg ( sort keys %{ $self->params } ) {
170 if ( ref $self->params->{$arg} ) {
171 my $list = $self->params->{$arg};
172 push @params, map { "$arg=" . $_ } sort @{$list};
174 push @params, "$arg=" . $self->params->{$arg};
177 $full_uri .= '?' . join( '&', @params );
184 Shortcut to $req->headers->header
188 Returns an L<HTTP::Headers> object containing the headers.
190 print $c->request->headers->header('X-Catalyst');
194 Lookup the current users DNS hostname.
196 print $c->request->hostname
203 if ( @_ == 0 && not $self->{hostname} ) {
204 $self->{hostname} = gethostbyaddr( inet_aton( $self->address ), AF_INET );
208 $self->{hostname} = shift;
211 return $self->{hostname};
216 Shortcut for $req->body.
220 This contains the matching part of a regexp action. Otherwise
221 it returns the same as 'action'.
223 print $c->request->match;
227 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
229 print $c->request->method;
233 Get request parameters with a CGI.pm-compatible param method. This
234 is a method for accessing parameters in $c->req->parameters.
236 $value = $c->request->param('foo');
237 @values = $c->request->param('foo');
238 @params = $c->request->param;
246 return keys %{ $self->parameters };
253 unless ( exists $self->parameters->{$param} ) {
254 return wantarray ? () : undef;
257 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
259 ? @{ $self->parameters->{$param} }
260 : $self->parameters->{$param}->[0];
264 ? ( $self->parameters->{$param} )
265 : $self->parameters->{$param};
271 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
273 next unless defined $field;
275 if ( exists $self->parameters->{$field} ) {
276 for ( $self->parameters->{$field} ) {
277 $_ = [$_] unless ref($_) eq "ARRAY";
282 $self->parameters->{$field} = $value;
290 Shortcut for $req->parameters.
292 =item $req->parameters
294 Returns a reference to a hash containing parameters. Values can
295 be either a scalar or an arrayref containing scalars.
297 print $c->request->parameters->{field};
298 print $c->request->parameters->{field}->[0];
304 print $c->request->path;
308 Contains the protocol.
312 Shortcut to $req->headers->referer. Referring page.
316 Contains a boolean whether the communciation is secure.
320 Returns a reference to an array containing regex snippets.
322 my @snippets = @{ $c->request->snippets };
326 A convenient method to $req->uploads.
328 $upload = $c->request->upload('field');
329 @uploads = $c->request->upload('field');
330 @fields = $c->request->upload;
332 for my $upload ( $c->request->upload('field') ) {
333 print $upload->filename;
342 return keys %{ $self->uploads };
349 unless ( exists $self->uploads->{$upload} ) {
350 return wantarray ? () : undef;
353 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
355 ? @{ $self->uploads->{$upload} }
356 : $self->uploads->{$upload}->[0];
360 ? ( $self->uploads->{$upload} )
361 : $self->uploads->{$upload};
367 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
369 if ( exists $self->uploads->{$field} ) {
370 for ( $self->uploads->{$field} ) {
371 $_ = [$_] unless ref($_) eq "ARRAY";
372 push( @$_, $upload );
376 $self->uploads->{$field} = $upload;
384 Returns a reference to a hash containing uploads. Values can be either a
385 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
387 my $upload = $c->request->uploads->{field};
388 my $upload = $c->request->uploads->{field}->[0];
392 Shortcut for C<< $req->base . $req->path >>.
398 my $path = shift || $self->path || '';
399 return $self->base . $path;
404 Contains the user name of user if authentication check was successful.
406 =item $req->user_agent
408 Shortcut to $req->headers->user_agent. User Agent version string.
414 Sebastian Riedel, C<sri@cpan.org>
415 Marcus Ramberg, C<mramberg@cpan.org>
419 This program is free software, you can redistribute it and/or modify
420 it under the same terms as Perl itself.