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 Returns a reference to a hash containing the cookies.
124 print $c->request->cookies->{mycookie}->value;
128 Shortcut to $req->headers->header
132 Returns an L<HTTP::Headers> object containing the headers.
134 print $c->request->headers->header('X-Catalyst');
138 Lookup the current users DNS hostname.
140 print $c->request->hostname
147 if ( @_ == 0 && not $self->{hostname} ) {
148 $self->{hostname} = gethostbyaddr( inet_aton( $self->address ), AF_INET );
152 $self->{hostname} = shift;
155 return $self->{hostname};
160 Shortcut for $req->body.
164 This contains the matching part of a regexp action. Otherwise
165 it returns the same as 'action'.
167 print $c->request->match;
171 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
173 print $c->request->method;
177 Get request parameters with a CGI.pm-compatible param method. This
178 is a method for accessing parameters in $c->req->parameters.
180 $value = $c->request->param('foo');
181 @values = $c->request->param('foo');
182 @params = $c->request->param;
190 return keys %{ $self->parameters };
197 unless ( exists $self->parameters->{$param} ) {
198 return wantarray ? () : undef;
201 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
203 ? @{ $self->parameters->{$param} }
204 : $self->parameters->{$param}->[0];
208 ? ( $self->parameters->{$param} )
209 : $self->parameters->{$param};
215 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
217 next unless defined $field;
219 if ( exists $self->parameters->{$field} ) {
220 for ( $self->parameters->{$field} ) {
221 $_ = [$_] unless ref($_) eq "ARRAY";
226 $self->parameters->{$field} = $value;
234 Shortcut for $req->parameters.
236 =item $req->parameters
238 Returns a reference to a hash containing parameters. Values can
239 be either a scalar or a arrayref containing scalars.
241 print $c->request->parameters->{field};
242 print $c->request->parameters->{field}->[0];
248 print $c->request->path;
252 Contains the protocol.
256 Shortcut to $req->headers->referer. Referring page.
260 Contains a boolean whether the communciation is secure.
264 Returns a reference to an array containing regex snippets.
266 my @snippets = @{ $c->request->snippets };
270 A convenient method to $req->uploads.
272 $upload = $c->request->upload('field');
273 @uploads = $c->request->upload('field');
274 @fields = $c->request->upload;
276 for my $upload ( $c->request->upload('field') ) {
277 print $upload->filename;
286 return keys %{ $self->uploads };
293 unless ( exists $self->uploads->{$upload} ) {
294 return wantarray ? () : undef;
297 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
299 ? @{ $self->uploads->{$upload} }
300 : $self->uploads->{$upload}->[0];
304 ? ( $self->uploads->{$upload} )
305 : $self->uploads->{$upload};
311 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
313 if ( exists $self->uploads->{$field} ) {
314 for ( $self->uploads->{$field} ) {
315 $_ = [$_] unless ref($_) eq "ARRAY";
316 push( @$_, $upload );
320 $self->uploads->{$field} = $upload;
328 Returns a reference to a hash containing uploads. Values can be either a
329 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
331 my $upload = $c->request->uploads->{field};
332 my $upload = $c->request->uploads->{field}->[0];
336 Shortcut for C<< $req->base . $req->path >>.
342 my $path = shift || $self->path || '';
343 return $self->base . $path;
348 Contains the user name of user if authentication check was successful.
350 =item $req->user_agent
352 Shortcut to $req->headers->user_agent. User Agent version string.
358 Sebastian Riedel, C<sri@cpan.org>
359 Marcus Ramberg, C<mramberg@cpan.org>
363 This program is free software, you can redistribute it and/or modify
364 it under the same terms as Perl itself.