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 base cookies handle headers match method
10 protocol query_parameters secure snippets uri user/
14 *body_params = \&body_parameters;
16 *params = \¶meters;
17 *query_params = \&query_parameters;
20 sub content_encoding { shift->headers->content_encoding(@_) }
21 sub content_length { shift->headers->content_length(@_) }
22 sub content_type { shift->headers->content_type(@_) }
23 sub header { shift->headers->header(@_) }
24 sub referer { shift->headers->referer(@_) }
25 sub user_agent { shift->headers->user_agent(@_) }
29 Catalyst::Request - Catalyst Request Class
41 $req->body_parameters;
42 $req->content_encoding;
59 $req->query_parameters;
74 This is the Catalyst Request class, which provides a set of accessors to the
75 request data. The request object is prepared by the specialized Catalyst
76 Engine module thus hiding the details of the particular engine implementation.
85 Contains the requested action.
87 print $c->request->action;
91 Contains the remote address.
93 print $c->request->address
97 Shortcut for arguments
101 Returns a reference to an array containing the arguments.
103 print $c->request->arguments->[0];
107 Contains the url base. This will always have a trailing slash.
111 Contains the message body of the request unless Content-Type is
112 C<application/x-www-form-urlencoded> or C<multipart/form-data>.
114 print $c->request->body
119 my ( $self, $body ) = @_;
120 $self->{_context}->prepare_body;
121 return $self->{_body}->body;
124 =item $req->body_parameters
126 Returns a reference to a hash containing body parameters. Values can
127 be either a scalar or an arrayref containing scalars.
129 print $c->request->body_parameters->{field};
130 print $c->request->body_parameters->{field}->[0];
132 =item $req->body_params
134 An alias for body_parameters.
138 sub body_parameters {
139 my ( $self, $params ) = @_;
140 $self->{_context}->prepare_body;
141 $self->{body_parameters} = $params if $params;
142 return $self->{body_parameters};
145 =item $req->content_encoding
147 Shortcut to $req->headers->content_encoding
149 =item $req->content_length
151 Shortcut to $req->headers->content_length
153 =item $req->content_type
155 Shortcut to $req->headers->content_type
159 A convenient method to $req->cookies.
161 $cookie = $c->request->cookie('name');
162 @cookies = $c->request->cookie;
170 return keys %{ $self->cookies };
177 unless ( exists $self->cookies->{$name} ) {
181 return $self->cookies->{$name};
187 Returns a reference to a hash containing the cookies.
189 print $c->request->cookies->{mycookie}->value;
197 Shortcut to $req->headers->header
201 Returns an L<HTTP::Headers> object containing the headers.
203 print $c->request->headers->header('X-Catalyst');
207 Lookup the current users DNS hostname.
209 print $c->request->hostname
216 if ( @_ == 0 && not $self->{hostname} ) {
218 gethostbyaddr( inet_aton( $self->address ), AF_INET );
222 $self->{hostname} = shift;
225 return $self->{hostname};
230 Shortcut for $req->body.
234 This contains the matching part of a regexp action. Otherwise
235 it returns the same as 'action'.
237 print $c->request->match;
241 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
243 print $c->request->method;
247 Get request parameters with a CGI.pm-compatible param method. This
248 is a method for accessing parameters in $c->req->parameters.
250 $value = $c->request->param('foo');
251 @values = $c->request->param('foo');
252 @params = $c->request->param;
260 return keys %{ $self->parameters };
267 unless ( exists $self->parameters->{$param} ) {
268 return wantarray ? () : undef;
271 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
273 ? @{ $self->parameters->{$param} }
274 : $self->parameters->{$param}->[0];
278 ? ( $self->parameters->{$param} )
279 : $self->parameters->{$param};
285 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
287 next unless defined $field;
289 if ( exists $self->parameters->{$field} ) {
290 for ( $self->parameters->{$field} ) {
291 $_ = [$_] unless ref($_) eq "ARRAY";
296 $self->parameters->{$field} = $value;
304 Shortcut for $req->parameters.
306 =item $req->parameters
308 Returns a reference to a hash containing parameters. Values can
309 be either a scalar or an arrayref containing scalars.
311 print $c->request->parameters->{field};
312 print $c->request->parameters->{field}->[0];
317 my ( $self, $params ) = @_;
318 $self->{_context}->prepare_body;
319 $self->{parameters} = $params if $params;
320 return $self->{parameters};
327 print $c->request->path;
329 =item $req->path_info
331 alias for path, added for compability with L<CGI>
336 my ( $self, $params ) = @_;
339 # base must always have a trailing slash
340 $params .= '/' unless ( $params =~ /\/$/ );
341 $self->uri->path( $params );
344 my $path = $self->uri->path;
345 my $location = $self->base->path;
346 $path =~ s/^(\Q$location\E)?//;
347 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
355 Contains the protocol.
357 =item $req->query_parameters
359 Returns a reference to a hash containing query parameters. Values can
360 be either a scalar or an arrayref containing scalars.
362 print $c->request->query_parameters->{field};
363 print $c->request->query_parameters->{field}->[0];
365 =item $req->read( [$maxlength] )
367 Read a chunk of data from the request body. This method is designed to be
368 used in a while loop, reading $maxlength bytes on every call. $maxlength
369 defaults to the size of the request if not specified.
371 You have to set MyApp->config->{parse_on_demand} to use this directly.
375 sub read { shift->{_context}->read(@_); }
379 Shortcut to $req->headers->referer. Referring page.
383 Contains a boolean whether the communciation is secure.
387 Returns a reference to an array containing regex snippets.
389 my @snippets = @{ $c->request->snippets };
393 A convenient method to $req->uploads.
395 $upload = $c->request->upload('field');
396 @uploads = $c->request->upload('field');
397 @fields = $c->request->upload;
399 for my $upload ( $c->request->upload('field') ) {
400 print $upload->filename;
409 return keys %{ $self->uploads };
416 unless ( exists $self->uploads->{$upload} ) {
417 return wantarray ? () : undef;
420 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
422 ? @{ $self->uploads->{$upload} }
423 : $self->uploads->{$upload}->[0];
427 ? ( $self->uploads->{$upload} )
428 : $self->uploads->{$upload};
434 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
436 if ( exists $self->uploads->{$field} ) {
437 for ( $self->uploads->{$field} ) {
438 $_ = [$_] unless ref($_) eq "ARRAY";
439 push( @$_, $upload );
443 $self->uploads->{$field} = $upload;
451 Returns a reference to a hash containing uploads. Values can be either a
452 hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
454 my $upload = $c->request->uploads->{field};
455 my $upload = $c->request->uploads->{field}->[0];
460 my ( $self, $uploads ) = @_;
461 $self->{_context}->prepare_body;
462 $self->{uploads} = $uploads if $uploads;
463 return $self->{uploads};
468 Returns a URI object for the request.
472 Contains the user name of user if authentication check was successful.
474 =item $req->user_agent
476 Shortcut to $req->headers->user_agent. User Agent version string.
482 Sebastian Riedel, C<sri@cpan.org>
483 Marcus Ramberg, C<mramberg@cpan.org>
487 This program is free software, you can redistribute it and/or modify
488 it under the same terms as Perl itself.