Added recursive -r flag to prove example
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request.pm
CommitLineData
fc7ec1d9 1package Catalyst::Request;
2
3use strict;
4use base 'Class::Accessor::Fast';
5
b4ca0ee8 6use IO::Socket qw[AF_INET inet_aton];
7
fc7ec1d9 8__PACKAGE__->mk_accessors(
b4ca0ee8 9 qw/action address arguments body base cookies headers match method
10 parameters path protocol secure snippets uploads user/
fc7ec1d9 11);
12
13*args = \&arguments;
e060fe05 14*input = \&body;
fc7ec1d9 15*params = \&parameters;
16
f7e4e231 17sub content_encoding { shift->headers->content_encoding(@_) }
18sub content_length { shift->headers->content_length(@_) }
19sub content_type { shift->headers->content_type(@_) }
20sub header { shift->headers->header(@_) }
21sub referer { shift->headers->referer(@_) }
22sub user_agent { shift->headers->user_agent(@_) }
23
fc7ec1d9 24=head1 NAME
25
26Catalyst::Request - Catalyst Request Class
27
28=head1 SYNOPSIS
29
b22c6668 30
31 $req = $c->request;
32 $req->action;
33 $req->address;
34 $req->args;
35 $req->arguments;
36 $req->base;
06e1b616 37 $req->body;
b5176d9e 38 $req->content_encoding;
39 $req->content_length;
40 $req->content_type;
b77e7869 41 $req->cookie;
b22c6668 42 $req->cookies;
696e8ae5 43 $req->full_uri;
b5176d9e 44 $req->header;
b22c6668 45 $req->headers;
46 $req->hostname;
61bacdcc 47 $req->input;
b22c6668 48 $req->match;
49 $req->method;
e7c0c583 50 $req->param;
b22c6668 51 $req->params;
e7c0c583 52 $req->parameters;
b22c6668 53 $req->path;
bfde09a2 54 $req->protocol;
b5176d9e 55 $req->referer;
bfde09a2 56 $req->secure;
b22c6668 57 $req->snippets;
e7c0c583 58 $req->upload;
b22c6668 59 $req->uploads;
77d12cae 60 $req->uri;
66294129 61 $req->user;
62 $req->user_agent;
b22c6668 63
64See also L<Catalyst>.
fc7ec1d9 65
66=head1 DESCRIPTION
67
b22c6668 68This is the Catalyst Request class, which provides a set of accessors to the
69request data. The request object is prepared by the specialized Catalyst
70Engine module thus hiding the details of the particular engine implementation.
71
72
73=head1 METHODS
fc7ec1d9 74
b22c6668 75=over 4
fc7ec1d9 76
b22c6668 77=item $req->action
fc7ec1d9 78
61b1e958 79Contains the requested action.
fc7ec1d9 80
81 print $c->request->action;
82
b22c6668 83=item $req->address
0556eb49 84
85Contains the remote address.
86
87 print $c->request->address
88
b22c6668 89=item $req->args
90
61b1e958 91Shortcut for arguments
92
93=item $req->arguments
94
b22c6668 95Returns a reference to an array containing the arguments.
fc7ec1d9 96
97 print $c->request->arguments->[0];
98
b22c6668 99=item $req->base
fc7ec1d9 100
61b1e958 101Contains the url base. This will always have a trailing slash.
fc7ec1d9 102
06e1b616 103=item $req->body
104
e060fe05 105Contains the message body of the request unless Content-Type is
106C<application/x-www-form-urlencoded> or C<multipart/form-data>.
107
108 print $c->request->body
06e1b616 109
b5176d9e 110=item $req->content_encoding
111
112Shortcut to $req->headers->content_encoding
113
114=item $req->content_length
115
116Shortcut to $req->headers->content_length
117
118=item $req->content_type
119
120Shortcut to $req->headers->content_type
121
3ad654e0 122=item $req->cookie
123
124A convenient method to $req->cookies.
125
126 $cookie = $c->request->cookie('name');
127 @cookies = $c->request->cookie;
128
129=cut
130
131sub cookie {
132 my $self = shift;
133
134 if ( @_ == 0 ) {
b77e7869 135 return keys %{ $self->cookies };
3ad654e0 136 }
137
138 if ( @_ == 1 ) {
139
140 my $name = shift;
141
b77e7869 142 unless ( exists $self->cookies->{$name} ) {
3ad654e0 143 return undef;
144 }
145
b77e7869 146 return $self->cookies->{$name};
3ad654e0 147 }
148}
149
b22c6668 150=item $req->cookies
fc7ec1d9 151
b22c6668 152Returns a reference to a hash containing the cookies.
fc7ec1d9 153
154 print $c->request->cookies->{mycookie}->value;
155
696e8ae5 156=item $req->full_uri
157
158Returns the complete URI, with the parameter query string.
159
160=cut
161
162sub full_uri {
163 my $self = shift;
d62d8073 164 my $full_uri = $self->uri;
696e8ae5 165
166 if ( scalar $self->param ) {
167 my @params;
168 foreach my $arg ( sort keys %{ $self->params } ) {
169 if ( ref $self->params->{$arg} ) {
170 my $list = $self->params->{$arg};
171 push @params, map { "$arg=" . $_ } sort @{$list};
172 } else {
173 push @params, "$arg=" . $self->params->{$arg};
174 }
175 }
176 $full_uri .= '?' . join( '&', @params );
177 }
178 return $full_uri;
179}
180
b5176d9e 181=item $req->header
182
183Shortcut to $req->headers->header
184
b22c6668 185=item $req->headers
fc7ec1d9 186
b22c6668 187Returns an L<HTTP::Headers> object containing the headers.
fc7ec1d9 188
189 print $c->request->headers->header('X-Catalyst');
190
b22c6668 191=item $req->hostname
0556eb49 192
b4ca0ee8 193Lookup the current users DNS hostname.
0556eb49 194
195 print $c->request->hostname
b4ca0ee8 196
197=cut
198
199sub hostname {
200 my $self = shift;
201
a268a011 202 if ( @_ == 0 && not $self->{hostname} ) {
203 $self->{hostname} = gethostbyaddr( inet_aton( $self->address ), AF_INET );
b4ca0ee8 204 }
205
a268a011 206 if ( @_ == 1 ) {
207 $self->{hostname} = shift;
b4ca0ee8 208 }
209
210 return $self->{hostname};
211}
0556eb49 212
61bacdcc 213=item $req->input
214
e060fe05 215Shortcut for $req->body.
61bacdcc 216
b22c6668 217=item $req->match
fc7ec1d9 218
c1f33816 219This contains the matching part of a regexp action. Otherwise
220it returns the same as 'action'.
fc7ec1d9 221
222 print $c->request->match;
223
b5176d9e 224=item $req->method
225
226Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
227
e7c0c583 228 print $c->request->method;
229
230=item $req->param
231
2ef2fb0f 232Get request parameters with a CGI.pm-compatible param method. This
233is a method for accessing parameters in $c->req->parameters.
e7c0c583 234
235 $value = $c->request->param('foo');
236 @values = $c->request->param('foo');
237 @params = $c->request->param;
238
239=cut
240
241sub param {
242 my $self = shift;
243
244 if ( @_ == 0 ) {
245 return keys %{ $self->parameters };
246 }
247
bfde09a2 248 if ( @_ == 1 ) {
e7c0c583 249
bfde09a2 250 my $param = shift;
6bd2b72c 251
bfde09a2 252 unless ( exists $self->parameters->{$param} ) {
253 return wantarray ? () : undef;
254 }
255
256 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
257 return (wantarray)
258 ? @{ $self->parameters->{$param} }
259 : $self->parameters->{$param}->[0];
260 }
261 else {
262 return (wantarray)
263 ? ( $self->parameters->{$param} )
264 : $self->parameters->{$param};
265 }
d7945f32 266 }
bfde09a2 267
03222156 268 if ( @_ > 1 ) {
bfde09a2 269
270 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
a4def412 271
272 next unless defined $field;
bfde09a2 273
274 if ( exists $self->parameters->{$field} ) {
275 for ( $self->parameters->{$field} ) {
276 $_ = [$_] unless ref($_) eq "ARRAY";
277 push( @$_, $value );
278 }
279 }
280 else {
281 $self->parameters->{$field} = $value;
282 }
283 }
d7945f32 284 }
e7c0c583 285}
b5176d9e 286
b22c6668 287=item $req->params
fc7ec1d9 288
61b1e958 289Shortcut for $req->parameters.
290
291=item $req->parameters
292
e7c0c583 293Returns a reference to a hash containing parameters. Values can
d08ced28 294be either a scalar or an arrayref containing scalars.
fc7ec1d9 295
e7c0c583 296 print $c->request->parameters->{field};
297 print $c->request->parameters->{field}->[0];
fc7ec1d9 298
b22c6668 299=item $req->path
fc7ec1d9 300
301Contains the path.
302
303 print $c->request->path;
304
bfde09a2 305=item $req->protocol
306
307Contains the protocol.
308
b5176d9e 309=item $req->referer
fc7ec1d9 310
61b1e958 311Shortcut to $req->headers->referer. Referring page.
fc7ec1d9 312
bfde09a2 313=item $req->secure
314
315Contains a boolean whether the communciation is secure.
316
b22c6668 317=item $req->snippets
fc7ec1d9 318
b22c6668 319Returns a reference to an array containing regex snippets.
fc7ec1d9 320
321 my @snippets = @{ $c->request->snippets };
322
e7c0c583 323=item $req->upload
324
325A convenient method to $req->uploads.
326
327 $upload = $c->request->upload('field');
328 @uploads = $c->request->upload('field');
329 @fields = $c->request->upload;
bfde09a2 330
e7c0c583 331 for my $upload ( $c->request->upload('field') ) {
146554c5 332 print $upload->filename;
e7c0c583 333 }
334
335=cut
336
337sub upload {
338 my $self = shift;
339
340 if ( @_ == 0 ) {
341 return keys %{ $self->uploads };
342 }
343
bfde09a2 344 if ( @_ == 1 ) {
e7c0c583 345
bfde09a2 346 my $upload = shift;
347
348 unless ( exists $self->uploads->{$upload} ) {
349 return wantarray ? () : undef;
350 }
6bd2b72c 351
bfde09a2 352 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
353 return (wantarray)
354 ? @{ $self->uploads->{$upload} }
355 : $self->uploads->{$upload}->[0];
356 }
357 else {
358 return (wantarray)
359 ? ( $self->uploads->{$upload} )
360 : $self->uploads->{$upload};
361 }
d7945f32 362 }
bfde09a2 363
a4f5c51e 364 if ( @_ > 1 ) {
bfde09a2 365
366 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
367
368 if ( exists $self->uploads->{$field} ) {
369 for ( $self->uploads->{$field} ) {
370 $_ = [$_] unless ref($_) eq "ARRAY";
371 push( @$_, $upload );
372 }
373 }
374 else {
375 $self->uploads->{$field} = $upload;
376 }
377 }
e7c0c583 378 }
379}
380
b22c6668 381=item $req->uploads
fc7ec1d9 382
bfde09a2 383Returns a reference to a hash containing uploads. Values can be either a
146554c5 384hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
e7c0c583 385
386 my $upload = $c->request->uploads->{field};
387 my $upload = $c->request->uploads->{field}->[0];
388
77d12cae 389=item $req->uri
390
391Shortcut for C<< $req->base . $req->path >>.
392
393=cut
394
395sub uri {
396 my $self = shift;
397 my $path = shift || $self->path || '';
398 return $self->base . $path;
399}
400
66294129 401=item $req->user
402
403Contains the user name of user if authentication check was successful.
404
b5176d9e 405=item $req->user_agent
406
61b1e958 407Shortcut to $req->headers->user_agent. User Agent version string.
b5176d9e 408
b22c6668 409=back
410
fc7ec1d9 411=head1 AUTHOR
412
413Sebastian Riedel, C<sri@cpan.org>
61b1e958 414Marcus Ramberg, C<mramberg@cpan.org>
fc7ec1d9 415
416=head1 COPYRIGHT
417
e7c0c583 418This program is free software, you can redistribute it and/or modify
61b1e958 419it under the same terms as Perl itself.
fc7ec1d9 420
421=cut
422
4231;