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;
66 This is the Catalyst Request class, which provides a set of accessors to the
67 request data. The request object is prepared by the specialized Catalyst
68 Engine module thus hiding the details of the particular engine implementation.
77 Contains the requested action.
79 print $c->request->action;
83 Contains the remote address.
85 print $c->request->address
89 Shortcut for arguments
93 Returns a reference to an array containing the arguments.
95 print $c->request->arguments->[0];
99 Contains the url base. This will always have a trailing slash.
103 Contains the message body of the request unless Content-Type is
104 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
106 print $c->request->body
108 =item $req->content_encoding
110 Shortcut to $req->headers->content_encoding
112 =item $req->content_length
114 Shortcut to $req->headers->content_length
116 =item $req->content_type
118 Shortcut to $req->headers->content_type
122 A convenient method to $req->cookies.
124 $cookie = $c->request->cookie('name');
125 @cookies = $c->request->cookie;
133 return keys %{ $self->cookie };
140 unless ( exists $self->cookie->{$name} ) {
144 return $self->cookie->{$name};
150 Returns a reference to a hash containing the cookies.
152 print $c->request->cookies->{mycookie}->value;
156 Shortcut to $req->headers->header
160 Returns an L<HTTP::Headers> object containing the headers.
162 print $c->request->headers->header('X-Catalyst');
166 Lookup the current users DNS hostname.
168 print $c->request->hostname
175 if ( @_ == 0 && not $self->{hostname} ) {
176 $self->{hostname} = gethostbyaddr( inet_aton( $self->address ), AF_INET );
180 $self->{hostname} = shift;
183 return $self->{hostname};
188 Shortcut for $req->body.
192 This contains the matching part of a regexp action. Otherwise
193 it returns the same as 'action'.
195 print $c->request->match;
199 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
201 print $c->request->method;
205 Get request parameters with a CGI.pm-compatible param method. This
206 is a method for accessing parameters in $c->req->parameters.
208 $value = $c->request->param('foo');
209 @values = $c->request->param('foo');
210 @params = $c->request->param;
218 return keys %{ $self->parameters };
225 unless ( exists $self->parameters->{$param} ) {
226 return wantarray ? () : undef;
229 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
231 ? @{ $self->parameters->{$param} }
232 : $self->parameters->{$param}->[0];
236 ? ( $self->parameters->{$param} )
237 : $self->parameters->{$param};
243 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
245 next unless defined $field;
247 if ( exists $self->parameters->{$field} ) {
248 for ( $self->parameters->{$field} ) {
249 $_ = [$_] unless ref($_) eq "ARRAY";
254 $self->parameters->{$field} = $value;
262 Shortcut for $req->parameters.
264 =item $req->parameters
266 Returns a reference to a hash containing parameters. Values can
267 be either a scalar or a arrayref containing scalars.
269 print $c->request->parameters->{field};
270 print $c->request->parameters->{field}->[0];
276 print $c->request->path;
280 Contains the protocol.
284 Shortcut to $req->headers->referer. Referring page.
288 Contains a boolean whether the communciation is secure.
292 Returns a reference to an array containing regex snippets.
294 my @snippets = @{ $c->request->snippets };
298 A convenient method to $req->uploads.
300 $upload = $c->request->upload('field');
301 @uploads = $c->request->upload('field');
302 @fields = $c->request->upload;
304 for my $upload ( $c->request->upload('field') ) {
305 print $upload->filename;
314 return keys %{ $self->uploads };
321 unless ( exists $self->uploads->{$upload} ) {
322 return wantarray ? () : undef;
325 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
327 ? @{ $self->uploads->{$upload} }
328 : $self->uploads->{$upload}->[0];
332 ? ( $self->uploads->{$upload} )
333 : $self->uploads->{$upload};
339 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
341 if ( exists $self->uploads->{$field} ) {
342 for ( $self->uploads->{$field} ) {
343 $_ = [$_] unless ref($_) eq "ARRAY";
344 push( @$_, $upload );
348 $self->uploads->{$field} = $upload;
356 Returns a reference to a hash containing uploads. Values can be either a
357 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
359 my $upload = $c->request->uploads->{field};
360 my $upload = $c->request->uploads->{field}->[0];
364 Shortcut for C<< $req->base . $req->path >>.
370 my $path = shift || $self->path || '';
371 return $self->base . $path;
376 Contains the user name of user if authentication check was successful.
378 =item $req->user_agent
380 Shortcut to $req->headers->user_agent. User Agent version string.
386 Sebastian Riedel, C<sri@cpan.org>
387 Marcus Ramberg, C<mramberg@cpan.org>
391 This program is free software, you can redistribute it and/or modify
392 it under the same terms as Perl itself.