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