Added links to each plugin in Plugins.pod
[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
61bacdcc 8 method parameters path snippets uploads/
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;
b5176d9e 50 $req->referer;
b22c6668 51 $req->snippets;
e7c0c583 52 $req->upload;
b22c6668 53 $req->uploads;
b5176d9e 54 $req->user_agent
b22c6668 55
56See also L<Catalyst>.
fc7ec1d9 57
58=head1 DESCRIPTION
59
b22c6668 60This is the Catalyst Request class, which provides a set of accessors to the
61request data. The request object is prepared by the specialized Catalyst
62Engine module thus hiding the details of the particular engine implementation.
63
64
65=head1 METHODS
fc7ec1d9 66
b22c6668 67=over 4
fc7ec1d9 68
b22c6668 69=item $req->action
fc7ec1d9 70
61b1e958 71Contains the requested action.
fc7ec1d9 72
73 print $c->request->action;
74
b22c6668 75=item $req->address
0556eb49 76
77Contains the remote address.
78
79 print $c->request->address
80
b22c6668 81=item $req->args
82
61b1e958 83Shortcut for arguments
84
85=item $req->arguments
86
b22c6668 87Returns a reference to an array containing the arguments.
fc7ec1d9 88
89 print $c->request->arguments->[0];
90
b22c6668 91=item $req->base
fc7ec1d9 92
61b1e958 93Contains the url base. This will always have a trailing slash.
fc7ec1d9 94
06e1b616 95=item $req->body
96
e060fe05 97Contains the message body of the request unless Content-Type is
98C<application/x-www-form-urlencoded> or C<multipart/form-data>.
99
100 print $c->request->body
06e1b616 101
b5176d9e 102=item $req->content_encoding
103
104Shortcut to $req->headers->content_encoding
105
106=item $req->content_length
107
108Shortcut to $req->headers->content_length
109
110=item $req->content_type
111
112Shortcut to $req->headers->content_type
113
b22c6668 114=item $req->cookies
fc7ec1d9 115
b22c6668 116Returns a reference to a hash containing the cookies.
fc7ec1d9 117
118 print $c->request->cookies->{mycookie}->value;
119
b5176d9e 120=item $req->header
121
122Shortcut to $req->headers->header
123
b22c6668 124=item $req->headers
fc7ec1d9 125
b22c6668 126Returns an L<HTTP::Headers> object containing the headers.
fc7ec1d9 127
128 print $c->request->headers->header('X-Catalyst');
129
b22c6668 130=item $req->hostname
0556eb49 131
61b1e958 132Contains the hostname of the remote user.
0556eb49 133
134 print $c->request->hostname
135
61bacdcc 136=item $req->input
137
e060fe05 138Shortcut for $req->body.
61bacdcc 139
b22c6668 140=item $req->match
fc7ec1d9 141
e7c0c583 142This contains be the matching part of a regexp action. otherwise it
61b1e958 143returns the same as 'action'.
fc7ec1d9 144
145 print $c->request->match;
146
b5176d9e 147=item $req->method
148
149Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
150
e7c0c583 151 print $c->request->method;
152
153=item $req->param
154
155Get request parameters with a CGI.pm like param method.
156
157 $value = $c->request->param('foo');
158 @values = $c->request->param('foo');
159 @params = $c->request->param;
160
161=cut
162
163sub param {
164 my $self = shift;
165
166 if ( @_ == 0 ) {
167 return keys %{ $self->parameters };
168 }
169
6bd2b72c 170 if ( @_ == 1 and ref( $_[0] ) eq 'ARRAY' ) {
e7c0c583 171
6bd2b72c 172 while ( my ( $field, $value ) = splice( @{ $_[0] }, 0, 2 ) ) {
e7c0c583 173
6bd2b72c 174 if ( exists $self->parameters->{$field} ) {
175 for ( $self->parameters->{$field} ) {
176 $_ = [$_] unless ref($_) eq "ARRAY";
177 push( @$_, $value );
178 }
179 }
180 else {
181 $self->parameters->{$field} = $value;
182 }
183 }
e7c0c583 184 }
6bd2b72c 185
186 if ( @_ == 1 ) {
187
188 my $param = shift;
189
190 unless ( exists $self->parameters->{$param} ) {
191 return wantarray ? () : undef;
192 }
193
194 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
195 return (wantarray)
196 ? @{ $self->parameters->{$param} }
197 : $self->parameters->{$param}->[0];
198 }
199 else {
200 return (wantarray)
201 ? ( $self->parameters->{$param} )
202 : $self->parameters->{$param};
203 }
204 }
e7c0c583 205}
b5176d9e 206
b22c6668 207=item $req->params
fc7ec1d9 208
61b1e958 209Shortcut for $req->parameters.
210
211=item $req->parameters
212
e7c0c583 213Returns a reference to a hash containing parameters. Values can
214be either a scalar or a arrayref containing scalars.
fc7ec1d9 215
e7c0c583 216 print $c->request->parameters->{field};
217 print $c->request->parameters->{field}->[0];
fc7ec1d9 218
b22c6668 219=item $req->path
fc7ec1d9 220
221Contains the path.
222
223 print $c->request->path;
224
b5176d9e 225=item $req->referer
fc7ec1d9 226
61b1e958 227Shortcut to $req->headers->referer. Referring page.
fc7ec1d9 228
b22c6668 229=item $req->snippets
fc7ec1d9 230
b22c6668 231Returns a reference to an array containing regex snippets.
fc7ec1d9 232
233 my @snippets = @{ $c->request->snippets };
234
e7c0c583 235=item $req->upload
236
237A convenient method to $req->uploads.
238
239 $upload = $c->request->upload('field');
240 @uploads = $c->request->upload('field');
241 @fields = $c->request->upload;
6bd2b72c 242
e7c0c583 243 for my $upload ( $c->request->upload('field') ) {
146554c5 244 print $upload->filename;
e7c0c583 245 }
246
247=cut
248
249sub upload {
250 my $self = shift;
251
252 if ( @_ == 0 ) {
253 return keys %{ $self->uploads };
254 }
255
6bd2b72c 256 if ( @_ == 1 and ref( $_[0] ) eq 'ARRAY' ) {
e7c0c583 257
6bd2b72c 258 while ( my ( $field, $upload ) = splice( @{ $_[0] }, 0, 2 ) ) {
e7c0c583 259
6bd2b72c 260 if ( exists $self->uploads->{$field} ) {
261 for ( $self->uploads->{$field} ) {
262 $_ = [$_] unless ref($_) eq "ARRAY";
263 push( @$_, $upload );
264 }
265 }
266 else {
267 $self->uploads->{$field} = $upload;
268 }
269 }
e7c0c583 270 }
6bd2b72c 271
272 if ( @_ == 1 ) {
273
274 my $upload = shift;
275
276 unless ( exists $self->uploads->{$upload} ) {
277 return wantarray ? () : undef;
278 }
279
280 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
281 return (wantarray)
282 ? @{ $self->uploads->{$upload} }
283 : $self->uploads->{$upload}->[0];
284 }
285 else {
286 return (wantarray)
287 ? ( $self->uploads->{$upload} )
288 : $self->uploads->{$upload};
289 }
e7c0c583 290 }
291}
292
b22c6668 293=item $req->uploads
fc7ec1d9 294
6bd2b72c 295Returns a reference to a hash containing uploads. Values can be either a
146554c5 296hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
e7c0c583 297
298 my $upload = $c->request->uploads->{field};
299 my $upload = $c->request->uploads->{field}->[0];
300
b5176d9e 301=item $req->user_agent
302
61b1e958 303Shortcut to $req->headers->user_agent. User Agent version string.
b5176d9e 304
b22c6668 305=back
306
fc7ec1d9 307=head1 AUTHOR
308
309Sebastian Riedel, C<sri@cpan.org>
61b1e958 310Marcus Ramberg, C<mramberg@cpan.org>
fc7ec1d9 311
312=head1 COPYRIGHT
313
e7c0c583 314This program is free software, you can redistribute it and/or modify
61b1e958 315it under the same terms as Perl itself.
fc7ec1d9 316
317=cut
318
3191;