Fixed typo, paramaters -> parameters
[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(
e561386f 9 qw/action address arguments cookies headers match method
fbcc39ad 10 protocol query_parameters secure snippets uri user/
fc7ec1d9 11);
12
fbcc39ad 13*args = \&arguments;
14*body_params = \&body_parameters;
15*input = \&body;
16*params = \&parameters;
17*query_params = \&query_parameters;
18*path_info = \&path;
fc7ec1d9 19
f7e4e231 20sub content_encoding { shift->headers->content_encoding(@_) }
fbcc39ad 21sub content_length { shift->headers->content_length(@_) }
22sub content_type { shift->headers->content_type(@_) }
23sub header { shift->headers->header(@_) }
24sub referer { shift->headers->referer(@_) }
25sub user_agent { shift->headers->user_agent(@_) }
f7e4e231 26
fc7ec1d9 27=head1 NAME
28
29Catalyst::Request - Catalyst Request Class
30
31=head1 SYNOPSIS
32
b22c6668 33
34 $req = $c->request;
35 $req->action;
36 $req->address;
37 $req->args;
38 $req->arguments;
39 $req->base;
06e1b616 40 $req->body;
fbcc39ad 41 $req->body_parameters;
b5176d9e 42 $req->content_encoding;
43 $req->content_length;
44 $req->content_type;
b77e7869 45 $req->cookie;
b22c6668 46 $req->cookies;
b5176d9e 47 $req->header;
b22c6668 48 $req->headers;
49 $req->hostname;
61bacdcc 50 $req->input;
b22c6668 51 $req->match;
52 $req->method;
e7c0c583 53 $req->param;
b22c6668 54 $req->params;
e7c0c583 55 $req->parameters;
b22c6668 56 $req->path;
bfde09a2 57 $req->protocol;
fbcc39ad 58 $req->query_parameters;
59 $req->read;
b5176d9e 60 $req->referer;
bfde09a2 61 $req->secure;
b22c6668 62 $req->snippets;
e7c0c583 63 $req->upload;
b22c6668 64 $req->uploads;
77d12cae 65 $req->uri;
66294129 66 $req->user;
67 $req->user_agent;
b22c6668 68
69See also L<Catalyst>.
fc7ec1d9 70
71=head1 DESCRIPTION
72
b22c6668 73This is the Catalyst Request class, which provides a set of accessors to the
74request data. The request object is prepared by the specialized Catalyst
75Engine module thus hiding the details of the particular engine implementation.
76
77
78=head1 METHODS
fc7ec1d9 79
b22c6668 80=over 4
fc7ec1d9 81
b22c6668 82=item $req->action
fc7ec1d9 83
61b1e958 84Contains the requested action.
fc7ec1d9 85
86 print $c->request->action;
87
b22c6668 88=item $req->address
0556eb49 89
90Contains the remote address.
91
92 print $c->request->address
93
b22c6668 94=item $req->args
95
61b1e958 96Shortcut for arguments
97
98=item $req->arguments
99
b22c6668 100Returns a reference to an array containing the arguments.
fc7ec1d9 101
102 print $c->request->arguments->[0];
103
c436c1e8 104For example, if your action was
105
106 package MyApp::C::Foo;
107
108 sub moose : Local {
109 ...
110 }
111
112And the URI for the request was C<http://.../foo/moose/bah> the string C<bah>
113would be the first and only argument.
114
b22c6668 115=item $req->base
fc7ec1d9 116
c436c1e8 117Contains the URI base. This will always have a trailing slash.
118
119If your application was queried with the URI C<http://localhost:3000/some/path>
120then C<base> is C<http://localhost:3000/>.
fc7ec1d9 121
e561386f 122=cut
123
124sub base {
125 my ( $self, $base ) = @_;
126
127 return $self->{base} unless $base;
128
129 $self->{base} = $base;
130
131 # set the value in path for backwards-compat
132 if ( $self->uri ) {
133 $self->path;
134 }
135
136 return $self->{base};
137}
138
06e1b616 139=item $req->body
140
e060fe05 141Contains the message body of the request unless Content-Type is
142C<application/x-www-form-urlencoded> or C<multipart/form-data>.
143
144 print $c->request->body
06e1b616 145
fbcc39ad 146=cut
147
148sub body {
149 my ( $self, $body ) = @_;
150 $self->{_context}->prepare_body;
151 return $self->{_body}->body;
152}
153
154=item $req->body_parameters
155
156Returns a reference to a hash containing body parameters. Values can
157be either a scalar or an arrayref containing scalars.
158
159 print $c->request->body_parameters->{field};
160 print $c->request->body_parameters->{field}->[0];
c436c1e8 161
d631b5f9 162These are the parameters from the POST part of the request, if any.
fbcc39ad 163
164=item $req->body_params
165
166An alias for body_parameters.
167
168=cut
169
170sub body_parameters {
171 my ( $self, $params ) = @_;
172 $self->{_context}->prepare_body;
173 $self->{body_parameters} = $params if $params;
174 return $self->{body_parameters};
175}
176
b5176d9e 177=item $req->content_encoding
178
179Shortcut to $req->headers->content_encoding
180
181=item $req->content_length
182
183Shortcut to $req->headers->content_length
184
185=item $req->content_type
186
187Shortcut to $req->headers->content_type
188
3ad654e0 189=item $req->cookie
190
191A convenient method to $req->cookies.
192
193 $cookie = $c->request->cookie('name');
194 @cookies = $c->request->cookie;
195
196=cut
197
198sub cookie {
199 my $self = shift;
200
201 if ( @_ == 0 ) {
b77e7869 202 return keys %{ $self->cookies };
3ad654e0 203 }
204
205 if ( @_ == 1 ) {
206
207 my $name = shift;
208
b77e7869 209 unless ( exists $self->cookies->{$name} ) {
3ad654e0 210 return undef;
211 }
fbcc39ad 212
b77e7869 213 return $self->cookies->{$name};
3ad654e0 214 }
215}
216
b22c6668 217=item $req->cookies
fc7ec1d9 218
b22c6668 219Returns a reference to a hash containing the cookies.
fc7ec1d9 220
221 print $c->request->cookies->{mycookie}->value;
222
c436c1e8 223The cookies in the hash are indexed by name, and the values are C<CGI::Cookie>
224objects.
225
b5176d9e 226=item $req->header
227
228Shortcut to $req->headers->header
229
b22c6668 230=item $req->headers
fc7ec1d9 231
b22c6668 232Returns an L<HTTP::Headers> object containing the headers.
fc7ec1d9 233
234 print $c->request->headers->header('X-Catalyst');
235
b22c6668 236=item $req->hostname
0556eb49 237
b4ca0ee8 238Lookup the current users DNS hostname.
0556eb49 239
240 print $c->request->hostname
b4ca0ee8 241
242=cut
243
244sub hostname {
245 my $self = shift;
246
a268a011 247 if ( @_ == 0 && not $self->{hostname} ) {
fbcc39ad 248 $self->{hostname} =
249 gethostbyaddr( inet_aton( $self->address ), AF_INET );
b4ca0ee8 250 }
251
a268a011 252 if ( @_ == 1 ) {
253 $self->{hostname} = shift;
b4ca0ee8 254 }
255
256 return $self->{hostname};
257}
0556eb49 258
61bacdcc 259=item $req->input
260
e060fe05 261Shortcut for $req->body.
61bacdcc 262
b22c6668 263=item $req->match
fc7ec1d9 264
c1f33816 265This contains the matching part of a regexp action. Otherwise
266it returns the same as 'action'.
fc7ec1d9 267
268 print $c->request->match;
269
b5176d9e 270=item $req->method
271
272Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
273
e7c0c583 274 print $c->request->method;
275
276=item $req->param
277
2ef2fb0f 278Get request parameters with a CGI.pm-compatible param method. This
279is a method for accessing parameters in $c->req->parameters.
e7c0c583 280
281 $value = $c->request->param('foo');
282 @values = $c->request->param('foo');
283 @params = $c->request->param;
284
285=cut
286
287sub param {
288 my $self = shift;
289
290 if ( @_ == 0 ) {
291 return keys %{ $self->parameters };
292 }
293
bfde09a2 294 if ( @_ == 1 ) {
e7c0c583 295
bfde09a2 296 my $param = shift;
6bd2b72c 297
bfde09a2 298 unless ( exists $self->parameters->{$param} ) {
299 return wantarray ? () : undef;
300 }
301
302 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
303 return (wantarray)
304 ? @{ $self->parameters->{$param} }
305 : $self->parameters->{$param}->[0];
306 }
307 else {
308 return (wantarray)
309 ? ( $self->parameters->{$param} )
310 : $self->parameters->{$param};
311 }
d7945f32 312 }
bfde09a2 313
03222156 314 if ( @_ > 1 ) {
bfde09a2 315
316 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
fbcc39ad 317
a4def412 318 next unless defined $field;
bfde09a2 319
320 if ( exists $self->parameters->{$field} ) {
321 for ( $self->parameters->{$field} ) {
322 $_ = [$_] unless ref($_) eq "ARRAY";
323 push( @$_, $value );
324 }
325 }
326 else {
327 $self->parameters->{$field} = $value;
328 }
329 }
d7945f32 330 }
e7c0c583 331}
b5176d9e 332
b22c6668 333=item $req->params
fc7ec1d9 334
61b1e958 335Shortcut for $req->parameters.
336
337=item $req->parameters
338
e7c0c583 339Returns a reference to a hash containing parameters. Values can
d08ced28 340be either a scalar or an arrayref containing scalars.
fc7ec1d9 341
e7c0c583 342 print $c->request->parameters->{field};
343 print $c->request->parameters->{field}->[0];
fc7ec1d9 344
c436c1e8 345This is the combination of C<query_parameters> and C<body_parameters>.
346
fbcc39ad 347=cut
348
349sub parameters {
350 my ( $self, $params ) = @_;
351 $self->{_context}->prepare_body;
352 $self->{parameters} = $params if $params;
353 return $self->{parameters};
354}
355
b22c6668 356=item $req->path
fc7ec1d9 357
358Contains the path.
359
360 print $c->request->path;
361
fbcc39ad 362=item $req->path_info
363
364alias for path, added for compability with L<CGI>
365
366=cut
367
368sub path {
369 my ( $self, $params ) = @_;
4f5ebacd 370
371 if ($params) {
4f5ebacd 372 $self->uri->path($params);
fbcc39ad 373 }
e561386f 374 else {
375 return $self->{path} if $self->{path};
376 }
fbcc39ad 377
4f5ebacd 378 my $path = $self->uri->path;
fbcc39ad 379 my $location = $self->base->path;
380 $path =~ s/^(\Q$location\E)?//;
381 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
382 $path =~ s/^\///;
e561386f 383 $self->{path} = $path;
4f5ebacd 384
fbcc39ad 385 return $path;
386}
387
bfde09a2 388=item $req->protocol
389
390Contains the protocol.
391
fbcc39ad 392=item $req->query_parameters
393
394Returns a reference to a hash containing query parameters. Values can
395be either a scalar or an arrayref containing scalars.
396
397 print $c->request->query_parameters->{field};
398 print $c->request->query_parameters->{field}->[0];
c436c1e8 399
400These are the parameters from the query string portion of the request's URI, if
401any.
fbcc39ad 402
403=item $req->read( [$maxlength] )
404
405Read a chunk of data from the request body. This method is designed to be
406used in a while loop, reading $maxlength bytes on every call. $maxlength
407defaults to the size of the request if not specified.
408
409You have to set MyApp->config->{parse_on_demand} to use this directly.
410
411=cut
412
413sub read { shift->{_context}->read(@_); }
414
b5176d9e 415=item $req->referer
fc7ec1d9 416
61b1e958 417Shortcut to $req->headers->referer. Referring page.
fc7ec1d9 418
bfde09a2 419=item $req->secure
420
26e73131 421Contains a boolean denoting whether the communication is secure.
bfde09a2 422
b22c6668 423=item $req->snippets
fc7ec1d9 424
b22c6668 425Returns a reference to an array containing regex snippets.
fc7ec1d9 426
427 my @snippets = @{ $c->request->snippets };
428
e7c0c583 429=item $req->upload
430
431A convenient method to $req->uploads.
432
433 $upload = $c->request->upload('field');
434 @uploads = $c->request->upload('field');
435 @fields = $c->request->upload;
bfde09a2 436
e7c0c583 437 for my $upload ( $c->request->upload('field') ) {
146554c5 438 print $upload->filename;
e7c0c583 439 }
440
441=cut
442
443sub upload {
444 my $self = shift;
445
446 if ( @_ == 0 ) {
447 return keys %{ $self->uploads };
448 }
449
bfde09a2 450 if ( @_ == 1 ) {
e7c0c583 451
bfde09a2 452 my $upload = shift;
453
454 unless ( exists $self->uploads->{$upload} ) {
455 return wantarray ? () : undef;
456 }
6bd2b72c 457
bfde09a2 458 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
459 return (wantarray)
460 ? @{ $self->uploads->{$upload} }
461 : $self->uploads->{$upload}->[0];
462 }
463 else {
464 return (wantarray)
fbcc39ad 465 ? ( $self->uploads->{$upload} )
466 : $self->uploads->{$upload};
bfde09a2 467 }
d7945f32 468 }
bfde09a2 469
a4f5c51e 470 if ( @_ > 1 ) {
bfde09a2 471
472 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
473
474 if ( exists $self->uploads->{$field} ) {
475 for ( $self->uploads->{$field} ) {
476 $_ = [$_] unless ref($_) eq "ARRAY";
477 push( @$_, $upload );
478 }
479 }
480 else {
481 $self->uploads->{$field} = $upload;
482 }
483 }
e7c0c583 484 }
485}
486
b22c6668 487=item $req->uploads
fc7ec1d9 488
bfde09a2 489Returns a reference to a hash containing uploads. Values can be either a
146554c5 490hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
e7c0c583 491
492 my $upload = $c->request->uploads->{field};
493 my $upload = $c->request->uploads->{field}->[0];
494
77d12cae 495=cut
496
fbcc39ad 497sub uploads {
498 my ( $self, $uploads ) = @_;
499 $self->{_context}->prepare_body;
500 $self->{uploads} = $uploads if $uploads;
501 return $self->{uploads};
77d12cae 502}
503
fbcc39ad 504=item $req->uri
505
506Returns a URI object for the request.
507
66294129 508=item $req->user
509
510Contains the user name of user if authentication check was successful.
511
b5176d9e 512=item $req->user_agent
513
61b1e958 514Shortcut to $req->headers->user_agent. User Agent version string.
b5176d9e 515
b22c6668 516=back
517
fc7ec1d9 518=head1 AUTHOR
519
520Sebastian Riedel, C<sri@cpan.org>
61b1e958 521Marcus Ramberg, C<mramberg@cpan.org>
fc7ec1d9 522
523=head1 COPYRIGHT
524
e7c0c583 525This program is free software, you can redistribute it and/or modify
61b1e958 526it under the same terms as Perl itself.
fc7ec1d9 527
528=cut
529
5301;