Removed req->handle and res->handle
[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(
4f5ebacd 9 qw/action address arguments base 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
b22c6668 104=item $req->base
fc7ec1d9 105
61b1e958 106Contains the url base. This will always have a trailing slash.
fc7ec1d9 107
06e1b616 108=item $req->body
109
e060fe05 110Contains the message body of the request unless Content-Type is
111C<application/x-www-form-urlencoded> or C<multipart/form-data>.
112
113 print $c->request->body
06e1b616 114
fbcc39ad 115=cut
116
117sub body {
118 my ( $self, $body ) = @_;
119 $self->{_context}->prepare_body;
120 return $self->{_body}->body;
121}
122
123=item $req->body_parameters
124
125Returns a reference to a hash containing body parameters. Values can
126be either a scalar or an arrayref containing scalars.
127
128 print $c->request->body_parameters->{field};
129 print $c->request->body_parameters->{field}->[0];
130
131=item $req->body_params
132
133An alias for body_parameters.
134
135=cut
136
137sub body_parameters {
138 my ( $self, $params ) = @_;
139 $self->{_context}->prepare_body;
140 $self->{body_parameters} = $params if $params;
141 return $self->{body_parameters};
142}
143
b5176d9e 144=item $req->content_encoding
145
146Shortcut to $req->headers->content_encoding
147
148=item $req->content_length
149
150Shortcut to $req->headers->content_length
151
152=item $req->content_type
153
154Shortcut to $req->headers->content_type
155
3ad654e0 156=item $req->cookie
157
158A convenient method to $req->cookies.
159
160 $cookie = $c->request->cookie('name');
161 @cookies = $c->request->cookie;
162
163=cut
164
165sub cookie {
166 my $self = shift;
167
168 if ( @_ == 0 ) {
b77e7869 169 return keys %{ $self->cookies };
3ad654e0 170 }
171
172 if ( @_ == 1 ) {
173
174 my $name = shift;
175
b77e7869 176 unless ( exists $self->cookies->{$name} ) {
3ad654e0 177 return undef;
178 }
fbcc39ad 179
b77e7869 180 return $self->cookies->{$name};
3ad654e0 181 }
182}
183
b22c6668 184=item $req->cookies
fc7ec1d9 185
b22c6668 186Returns a reference to a hash containing the cookies.
fc7ec1d9 187
188 print $c->request->cookies->{mycookie}->value;
189
b5176d9e 190=item $req->header
191
192Shortcut to $req->headers->header
193
b22c6668 194=item $req->headers
fc7ec1d9 195
b22c6668 196Returns an L<HTTP::Headers> object containing the headers.
fc7ec1d9 197
198 print $c->request->headers->header('X-Catalyst');
199
b22c6668 200=item $req->hostname
0556eb49 201
b4ca0ee8 202Lookup the current users DNS hostname.
0556eb49 203
204 print $c->request->hostname
b4ca0ee8 205
206=cut
207
208sub hostname {
209 my $self = shift;
210
a268a011 211 if ( @_ == 0 && not $self->{hostname} ) {
fbcc39ad 212 $self->{hostname} =
213 gethostbyaddr( inet_aton( $self->address ), AF_INET );
b4ca0ee8 214 }
215
a268a011 216 if ( @_ == 1 ) {
217 $self->{hostname} = shift;
b4ca0ee8 218 }
219
220 return $self->{hostname};
221}
0556eb49 222
61bacdcc 223=item $req->input
224
e060fe05 225Shortcut for $req->body.
61bacdcc 226
b22c6668 227=item $req->match
fc7ec1d9 228
c1f33816 229This contains the matching part of a regexp action. Otherwise
230it returns the same as 'action'.
fc7ec1d9 231
232 print $c->request->match;
233
b5176d9e 234=item $req->method
235
236Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
237
e7c0c583 238 print $c->request->method;
239
240=item $req->param
241
2ef2fb0f 242Get request parameters with a CGI.pm-compatible param method. This
243is a method for accessing parameters in $c->req->parameters.
e7c0c583 244
245 $value = $c->request->param('foo');
246 @values = $c->request->param('foo');
247 @params = $c->request->param;
248
249=cut
250
251sub param {
252 my $self = shift;
253
254 if ( @_ == 0 ) {
255 return keys %{ $self->parameters };
256 }
257
bfde09a2 258 if ( @_ == 1 ) {
e7c0c583 259
bfde09a2 260 my $param = shift;
6bd2b72c 261
bfde09a2 262 unless ( exists $self->parameters->{$param} ) {
263 return wantarray ? () : undef;
264 }
265
266 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
267 return (wantarray)
268 ? @{ $self->parameters->{$param} }
269 : $self->parameters->{$param}->[0];
270 }
271 else {
272 return (wantarray)
273 ? ( $self->parameters->{$param} )
274 : $self->parameters->{$param};
275 }
d7945f32 276 }
bfde09a2 277
03222156 278 if ( @_ > 1 ) {
bfde09a2 279
280 while ( my ( $field, $value ) = splice( @_, 0, 2 ) ) {
fbcc39ad 281
a4def412 282 next unless defined $field;
bfde09a2 283
284 if ( exists $self->parameters->{$field} ) {
285 for ( $self->parameters->{$field} ) {
286 $_ = [$_] unless ref($_) eq "ARRAY";
287 push( @$_, $value );
288 }
289 }
290 else {
291 $self->parameters->{$field} = $value;
292 }
293 }
d7945f32 294 }
e7c0c583 295}
b5176d9e 296
b22c6668 297=item $req->params
fc7ec1d9 298
61b1e958 299Shortcut for $req->parameters.
300
301=item $req->parameters
302
e7c0c583 303Returns a reference to a hash containing parameters. Values can
d08ced28 304be either a scalar or an arrayref containing scalars.
fc7ec1d9 305
e7c0c583 306 print $c->request->parameters->{field};
307 print $c->request->parameters->{field}->[0];
fc7ec1d9 308
fbcc39ad 309=cut
310
311sub parameters {
312 my ( $self, $params ) = @_;
313 $self->{_context}->prepare_body;
314 $self->{parameters} = $params if $params;
315 return $self->{parameters};
316}
317
b22c6668 318=item $req->path
fc7ec1d9 319
320Contains the path.
321
322 print $c->request->path;
323
fbcc39ad 324=item $req->path_info
325
326alias for path, added for compability with L<CGI>
327
328=cut
329
330sub path {
331 my ( $self, $params ) = @_;
4f5ebacd 332
333 if ($params) {
334
fbcc39ad 335 # base must always have a trailing slash
336 $params .= '/' unless ( $params =~ /\/$/ );
4f5ebacd 337 $self->uri->path($params);
fbcc39ad 338 }
339
4f5ebacd 340 my $path = $self->uri->path;
fbcc39ad 341 my $location = $self->base->path;
342 $path =~ s/^(\Q$location\E)?//;
343 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
344 $path =~ s/^\///;
4f5ebacd 345
fbcc39ad 346 return $path;
347}
348
bfde09a2 349=item $req->protocol
350
351Contains the protocol.
352
fbcc39ad 353=item $req->query_parameters
354
355Returns a reference to a hash containing query parameters. Values can
356be either a scalar or an arrayref containing scalars.
357
358 print $c->request->query_parameters->{field};
359 print $c->request->query_parameters->{field}->[0];
360
361=item $req->read( [$maxlength] )
362
363Read a chunk of data from the request body. This method is designed to be
364used in a while loop, reading $maxlength bytes on every call. $maxlength
365defaults to the size of the request if not specified.
366
367You have to set MyApp->config->{parse_on_demand} to use this directly.
368
369=cut
370
371sub read { shift->{_context}->read(@_); }
372
b5176d9e 373=item $req->referer
fc7ec1d9 374
61b1e958 375Shortcut to $req->headers->referer. Referring page.
fc7ec1d9 376
bfde09a2 377=item $req->secure
378
26e73131 379Contains a boolean denoting whether the communication is secure.
bfde09a2 380
b22c6668 381=item $req->snippets
fc7ec1d9 382
b22c6668 383Returns a reference to an array containing regex snippets.
fc7ec1d9 384
385 my @snippets = @{ $c->request->snippets };
386
e7c0c583 387=item $req->upload
388
389A convenient method to $req->uploads.
390
391 $upload = $c->request->upload('field');
392 @uploads = $c->request->upload('field');
393 @fields = $c->request->upload;
bfde09a2 394
e7c0c583 395 for my $upload ( $c->request->upload('field') ) {
146554c5 396 print $upload->filename;
e7c0c583 397 }
398
399=cut
400
401sub upload {
402 my $self = shift;
403
404 if ( @_ == 0 ) {
405 return keys %{ $self->uploads };
406 }
407
bfde09a2 408 if ( @_ == 1 ) {
e7c0c583 409
bfde09a2 410 my $upload = shift;
411
412 unless ( exists $self->uploads->{$upload} ) {
413 return wantarray ? () : undef;
414 }
6bd2b72c 415
bfde09a2 416 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
417 return (wantarray)
418 ? @{ $self->uploads->{$upload} }
419 : $self->uploads->{$upload}->[0];
420 }
421 else {
422 return (wantarray)
fbcc39ad 423 ? ( $self->uploads->{$upload} )
424 : $self->uploads->{$upload};
bfde09a2 425 }
d7945f32 426 }
bfde09a2 427
a4f5c51e 428 if ( @_ > 1 ) {
bfde09a2 429
430 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
431
432 if ( exists $self->uploads->{$field} ) {
433 for ( $self->uploads->{$field} ) {
434 $_ = [$_] unless ref($_) eq "ARRAY";
435 push( @$_, $upload );
436 }
437 }
438 else {
439 $self->uploads->{$field} = $upload;
440 }
441 }
e7c0c583 442 }
443}
444
b22c6668 445=item $req->uploads
fc7ec1d9 446
bfde09a2 447Returns a reference to a hash containing uploads. Values can be either a
146554c5 448hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
e7c0c583 449
450 my $upload = $c->request->uploads->{field};
451 my $upload = $c->request->uploads->{field}->[0];
452
77d12cae 453=cut
454
fbcc39ad 455sub uploads {
456 my ( $self, $uploads ) = @_;
457 $self->{_context}->prepare_body;
458 $self->{uploads} = $uploads if $uploads;
459 return $self->{uploads};
77d12cae 460}
461
fbcc39ad 462=item $req->uri
463
464Returns a URI object for the request.
465
66294129 466=item $req->user
467
468Contains the user name of user if authentication check was successful.
469
b5176d9e 470=item $req->user_agent
471
61b1e958 472Shortcut to $req->headers->user_agent. User Agent version string.
b5176d9e 473
b22c6668 474=back
475
fc7ec1d9 476=head1 AUTHOR
477
478Sebastian Riedel, C<sri@cpan.org>
61b1e958 479Marcus Ramberg, C<mramberg@cpan.org>
fc7ec1d9 480
481=head1 COPYRIGHT
482
e7c0c583 483This program is free software, you can redistribute it and/or modify
61b1e958 484it under the same terms as Perl itself.
fc7ec1d9 485
486=cut
487
4881;