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