Changed default match to use path instead of result
[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) {
4f5ebacd 334 $self->uri->path($params);
fbcc39ad 335 }
336
4f5ebacd 337 my $path = $self->uri->path;
fbcc39ad 338 my $location = $self->base->path;
339 $path =~ s/^(\Q$location\E)?//;
340 $path =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
341 $path =~ s/^\///;
4f5ebacd 342
fbcc39ad 343 return $path;
344}
345
bfde09a2 346=item $req->protocol
347
348Contains the protocol.
349
fbcc39ad 350=item $req->query_parameters
351
352Returns a reference to a hash containing query parameters. Values can
353be either a scalar or an arrayref containing scalars.
354
355 print $c->request->query_parameters->{field};
356 print $c->request->query_parameters->{field}->[0];
357
358=item $req->read( [$maxlength] )
359
360Read a chunk of data from the request body. This method is designed to be
361used in a while loop, reading $maxlength bytes on every call. $maxlength
362defaults to the size of the request if not specified.
363
364You have to set MyApp->config->{parse_on_demand} to use this directly.
365
366=cut
367
368sub read { shift->{_context}->read(@_); }
369
b5176d9e 370=item $req->referer
fc7ec1d9 371
61b1e958 372Shortcut to $req->headers->referer. Referring page.
fc7ec1d9 373
bfde09a2 374=item $req->secure
375
26e73131 376Contains a boolean denoting whether the communication is secure.
bfde09a2 377
b22c6668 378=item $req->snippets
fc7ec1d9 379
b22c6668 380Returns a reference to an array containing regex snippets.
fc7ec1d9 381
382 my @snippets = @{ $c->request->snippets };
383
e7c0c583 384=item $req->upload
385
386A convenient method to $req->uploads.
387
388 $upload = $c->request->upload('field');
389 @uploads = $c->request->upload('field');
390 @fields = $c->request->upload;
bfde09a2 391
e7c0c583 392 for my $upload ( $c->request->upload('field') ) {
146554c5 393 print $upload->filename;
e7c0c583 394 }
395
396=cut
397
398sub upload {
399 my $self = shift;
400
401 if ( @_ == 0 ) {
402 return keys %{ $self->uploads };
403 }
404
bfde09a2 405 if ( @_ == 1 ) {
e7c0c583 406
bfde09a2 407 my $upload = shift;
408
409 unless ( exists $self->uploads->{$upload} ) {
410 return wantarray ? () : undef;
411 }
6bd2b72c 412
bfde09a2 413 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
414 return (wantarray)
415 ? @{ $self->uploads->{$upload} }
416 : $self->uploads->{$upload}->[0];
417 }
418 else {
419 return (wantarray)
fbcc39ad 420 ? ( $self->uploads->{$upload} )
421 : $self->uploads->{$upload};
bfde09a2 422 }
d7945f32 423 }
bfde09a2 424
a4f5c51e 425 if ( @_ > 1 ) {
bfde09a2 426
427 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
428
429 if ( exists $self->uploads->{$field} ) {
430 for ( $self->uploads->{$field} ) {
431 $_ = [$_] unless ref($_) eq "ARRAY";
432 push( @$_, $upload );
433 }
434 }
435 else {
436 $self->uploads->{$field} = $upload;
437 }
438 }
e7c0c583 439 }
440}
441
b22c6668 442=item $req->uploads
fc7ec1d9 443
bfde09a2 444Returns a reference to a hash containing uploads. Values can be either a
146554c5 445hashref or a arrayref containing C<Catalyst::Request::Upload> objects.
e7c0c583 446
447 my $upload = $c->request->uploads->{field};
448 my $upload = $c->request->uploads->{field}->[0];
449
77d12cae 450=cut
451
fbcc39ad 452sub uploads {
453 my ( $self, $uploads ) = @_;
454 $self->{_context}->prepare_body;
455 $self->{uploads} = $uploads if $uploads;
456 return $self->{uploads};
77d12cae 457}
458
fbcc39ad 459=item $req->uri
460
461Returns a URI object for the request.
462
66294129 463=item $req->user
464
465Contains the user name of user if authentication check was successful.
466
b5176d9e 467=item $req->user_agent
468
61b1e958 469Shortcut to $req->headers->user_agent. User Agent version string.
b5176d9e 470
b22c6668 471=back
472
fc7ec1d9 473=head1 AUTHOR
474
475Sebastian Riedel, C<sri@cpan.org>
61b1e958 476Marcus Ramberg, C<mramberg@cpan.org>
fc7ec1d9 477
478=head1 COPYRIGHT
479
e7c0c583 480This program is free software, you can redistribute it and/or modify
61b1e958 481it under the same terms as Perl itself.
fc7ec1d9 482
483=cut
484
4851;