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 be the matching part of a regexp action. otherwise it
165 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 like param method.
179 $value = $c->request->param('foo');
180 @values = $c->request->param('foo');
181 @params = $c->request->param;
189 return keys %{ $self->parameters };
196 unless ( exists $self->parameters->{$param} ) {
197 return wantarray ? () : undef;
200 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
202 ? @{ $self->parameters->{$param} }
203 : $self->parameters->{$param}->[0];
207 ? ( $self->parameters->{$param} )
208 : $self->parameters->{$param};
214 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
216 next unless defined $field;
218 if ( exists $self->parameters->{$field} ) {
219 for ( $self->parameters->{$field} ) {
220 $_ = [$_] unless ref($_) eq "ARRAY";
225 $self->parameters->{$field} = $value;
233 Shortcut for $req->parameters.
235 =item $req->parameters
237 Returns a reference to a hash containing parameters. Values can
238 be either a scalar or a arrayref containing scalars.
240 print $c->request->parameters->{field};
241 print $c->request->parameters->{field}->[0];
247 print $c->request->path;
251 Contains the protocol.
255 Shortcut to $req->headers->referer. Referring page.
259 Contains a boolean whether the communciation is secure.
263 Returns a reference to an array containing regex snippets.
265 my @snippets = @{ $c->request->snippets };
269 A convenient method to $req->uploads.
271 $upload = $c->request->upload('field');
272 @uploads = $c->request->upload('field');
273 @fields = $c->request->upload;
275 for my $upload ( $c->request->upload('field') ) {
276 print $upload->filename;
285 return keys %{ $self->uploads };
292 unless ( exists $self->uploads->{$upload} ) {
293 return wantarray ? () : undef;
296 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
298 ? @{ $self->uploads->{$upload} }
299 : $self->uploads->{$upload}->[0];
303 ? ( $self->uploads->{$upload} )
304 : $self->uploads->{$upload};
310 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
312 if ( exists $self->uploads->{$field} ) {
313 for ( $self->uploads->{$field} ) {
314 $_ = [$_] unless ref($_) eq "ARRAY";
315 push( @$_, $upload );
319 $self->uploads->{$field} = $upload;
327 Returns a reference to a hash containing uploads. Values can be either a
328 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
330 my $upload = $c->request->uploads->{field};
331 my $upload = $c->request->uploads->{field}->[0];
335 Shortcut for C<< $req->base . $req->path >>.
341 my $path = shift || $self->path || '';
342 return $self->base . $path;
347 Contains the user name of user if authentication check was successful.
349 =item $req->user_agent
351 Shortcut to $req->headers->user_agent. User Agent version string.
357 Sebastian Riedel, C<sri@cpan.org>
358 Marcus Ramberg, C<mramberg@cpan.org>
362 This program is free software, you can redistribute it and/or modify
363 it under the same terms as Perl itself.