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