bye bye Class::C3. for good.
[catagits/Catalyst-Runtime.git] / lib / Catalyst / Request.pm
CommitLineData
fc7ec1d9 1package Catalyst::Request;
2
b4ca0ee8 3use IO::Socket qw[AF_INET inet_aton];
bd917b94 4use Carp;
fc42a730 5use utf8;
de19de2e 6use URI::http;
7use URI::https;
e669e88a 8use URI::QueryParam;
6680c772 9use HTTP::Headers;
b4ca0ee8 10
059c085b 11use Moose;
12
5fb12dbb 13has action => (is => 'rw');
14has address => (is => 'rw');
15has arguments => (is => 'rw', default => sub { [] });
16has cookies => (is => 'rw', default => sub { {} });
17has query_keywords => (is => 'rw');
18has match => (is => 'rw');
19has method => (is => 'rw');
20has protocol => (is => 'rw');
059c085b 21has query_parameters => (is => 'rw', default => sub { {} });
5fb12dbb 22has secure => (is => 'rw', default => 0);
23has captures => (is => 'rw', default => sub { [] });
24has uri => (is => 'rw');
25has user => (is => 'rw');
26has headers => (
e5ecd5bc 27 is => 'rw',
059c085b 28 isa => 'HTTP::Headers',
29 handles => [qw(content_encoding content_length content_type header referer user_agent)],
6680c772 30 default => sub { HTTP::Headers->new() },
31 required => 1,
32 lazy => 1,
059c085b 33);
34
0fc2d522 35#Moose ToDo:
36#can we lose the before modifiers which just call prepare_body ?
37#they are wasteful, slow us down and feel cluttery.
38# Can we call prepare_body at BUILD time?
39# Can we make _body an attribute and have the rest of these lazy build from there?
40
059c085b 41has _context => (
42 is => 'rw',
43 weak_ref => 1,
0fc2d522 44 handles => ['read'],
059c085b 45);
46
47has body_parameters => (
5fb12dbb 48 is => 'rw',
49 required => 1,
50 lazy => 1,
51 default => sub { {} },
fc7ec1d9 52);
53
059c085b 54before body_parameters => sub {
55 my ($self) = @_;
56 $self->_context->prepare_body();
57};
58
59has uploads => (
5fb12dbb 60 is => 'rw',
61 required => 1,
62 lazy => 1,
63 default => sub { {} },
059c085b 64);
65
0fc2d522 66# modifier was a noop (groditi)
67# before uploads => sub {
68# my ($self) = @_;
69# #$self->_context->prepare_body;
70# };
059c085b 71
72has parameters => (
73 is => 'rw',
74 required => 1,
75 lazy => 1,
76 default => sub { {} },
77);
78
79before parameters => sub {
80 my ($self, $params) = @_;
6680c772 81 #$self->_context->prepare_body();
059c085b 82 if ( $params && !ref $params ) {
e5ecd5bc 83 $self->_context->log->warn(
059c085b 84 "Attempt to retrieve '$params' with req->params(), " .
85 "you probably meant to call req->param('$params')" );
86 $params = undef;
87 }
88
89};
90
91has base => (
5fb12dbb 92 is => 'rw',
93 required => 1,
94 lazy => 1,
95 default => sub {
059c085b 96 my $self = shift;
5fb12dbb 97 return $self->path if $self->uri;
059c085b 98 },
99);
100
101has body => (
102 is => 'rw'
103);
104
105before body => sub {
106 my ($self) = @_;
107 $self->_context->prepare_body();
108};
109
110has hostname => (
111 is => 'rw',
112 required => 1,
113 lazy => 1,
114 default => sub {
115 my ($self) = @_;
116 gethostbyaddr( inet_aton( $self->address ), AF_INET )
117 },
118);
119
120no Moose;
121
122sub args { shift->arguments(@_) }
123sub body_params { shift->body_parameters(@_) }
124sub input { shift->body(@_) }
125sub params { shift->parameters(@_) }
126sub query_params { shift->query_parameters(@_) }
127sub path_info { shift->path(@_) }
128sub snippets { shift->captures(@_) }
f7e4e231 129
fc7ec1d9 130=head1 NAME
131
3e19f4f6 132Catalyst::Request - provides information about the current client request
fc7ec1d9 133
134=head1 SYNOPSIS
135
b22c6668 136 $req = $c->request;
137 $req->action;
138 $req->address;
b22c6668 139 $req->arguments;
3e19f4f6 140 $req->args;
b22c6668 141 $req->base;
06e1b616 142 $req->body;
fbcc39ad 143 $req->body_parameters;
b5176d9e 144 $req->content_encoding;
145 $req->content_length;
146 $req->content_type;
b77e7869 147 $req->cookie;
b22c6668 148 $req->cookies;
b5176d9e 149 $req->header;
b22c6668 150 $req->headers;
151 $req->hostname;
61bacdcc 152 $req->input;
3b4d1251 153 $req->query_keywords;
b22c6668 154 $req->match;
155 $req->method;
e7c0c583 156 $req->param;
e7c0c583 157 $req->parameters;
3e19f4f6 158 $req->params;
b22c6668 159 $req->path;
bfde09a2 160 $req->protocol;
fbcc39ad 161 $req->query_parameters;
162 $req->read;
b5176d9e 163 $req->referer;
bfde09a2 164 $req->secure;
2982e768 165 $req->captures; # previously knows as snippets
e7c0c583 166 $req->upload;
b22c6668 167 $req->uploads;
77d12cae 168 $req->uri;
7ce7ca2e 169 $req->user;
66294129 170 $req->user_agent;
b22c6668 171
3e22baa5 172See also L<Catalyst>, L<Catalyst::Request::Upload>.
fc7ec1d9 173
174=head1 DESCRIPTION
175
3e19f4f6 176This is the Catalyst Request class, which provides an interface to data for the
177current client request. The request object is prepared by L<Catalyst::Engine>,
178thus hiding the details of the particular engine implementation.
b22c6668 179
180=head1 METHODS
fc7ec1d9 181
b5ecfcf0 182=head2 $req->action
fc7ec1d9 183
aae8d418 184[DEPRECATED] Returns the name of the requested action.
185
186
187Use C<< $c->action >> instead (which returns a
188L<Catalyst::Action|Catalyst::Action> object).
fc7ec1d9 189
b5ecfcf0 190=head2 $req->address
0556eb49 191
3e19f4f6 192Returns the IP address of the client.
61b1e958 193
b5ecfcf0 194=head2 $req->arguments
61b1e958 195
b22c6668 196Returns a reference to an array containing the arguments.
fc7ec1d9 197
198 print $c->request->arguments->[0];
199
c436c1e8 200For example, if your action was
201
85d9fce6 202 package MyApp::C::Foo;
203
204 sub moose : Local {
205 ...
206 }
c436c1e8 207
3e19f4f6 208and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
c436c1e8 209would be the first and only argument.
210
b5ecfcf0 211=head2 $req->args
3e19f4f6 212
213Shortcut for arguments.
214
b5ecfcf0 215=head2 $req->base
fc7ec1d9 216
c436c1e8 217Contains the URI base. This will always have a trailing slash.
218
3e19f4f6 219If your application was queried with the URI
220C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
fc7ec1d9 221
b5ecfcf0 222=head2 $req->body
06e1b616 223
3e19f4f6 224Returns the message body of the request, unless Content-Type is
e060fe05 225C<application/x-www-form-urlencoded> or C<multipart/form-data>.
226
b5ecfcf0 227=head2 $req->body_parameters
fbcc39ad 228
3e19f4f6 229Returns a reference to a hash containing body (POST) parameters. Values can
fbcc39ad 230be either a scalar or an arrayref containing scalars.
231
232 print $c->request->body_parameters->{field};
233 print $c->request->body_parameters->{field}->[0];
c436c1e8 234
d631b5f9 235These are the parameters from the POST part of the request, if any.
e5ecd5bc 236
b5ecfcf0 237=head2 $req->body_params
fbcc39ad 238
3e19f4f6 239Shortcut for body_parameters.
fbcc39ad 240
b5ecfcf0 241=head2 $req->content_encoding
b5176d9e 242
3e19f4f6 243Shortcut for $req->headers->content_encoding.
b5176d9e 244
b5ecfcf0 245=head2 $req->content_length
b5176d9e 246
3e19f4f6 247Shortcut for $req->headers->content_length.
b5176d9e 248
b5ecfcf0 249=head2 $req->content_type
b5176d9e 250
3e19f4f6 251Shortcut for $req->headers->content_type.
b5176d9e 252
b5ecfcf0 253=head2 $req->cookie
3ad654e0 254
3e19f4f6 255A convenient method to access $req->cookies.
3ad654e0 256
257 $cookie = $c->request->cookie('name');
258 @cookies = $c->request->cookie;
259
260=cut
261
262sub cookie {
263 my $self = shift;
264
265 if ( @_ == 0 ) {
b77e7869 266 return keys %{ $self->cookies };
3ad654e0 267 }
268
269 if ( @_ == 1 ) {
270
271 my $name = shift;
272
b77e7869 273 unless ( exists $self->cookies->{$name} ) {
3ad654e0 274 return undef;
275 }
fbcc39ad 276
b77e7869 277 return $self->cookies->{$name};
3ad654e0 278 }
279}
280
b5ecfcf0 281=head2 $req->cookies
fc7ec1d9 282
b22c6668 283Returns a reference to a hash containing the cookies.
fc7ec1d9 284
285 print $c->request->cookies->{mycookie}->value;
286
3e19f4f6 287The cookies in the hash are indexed by name, and the values are L<CGI::Cookie>
c436c1e8 288objects.
289
b5ecfcf0 290=head2 $req->header
b5176d9e 291
3e19f4f6 292Shortcut for $req->headers->header.
b5176d9e 293
b5ecfcf0 294=head2 $req->headers
fc7ec1d9 295
3e19f4f6 296Returns an L<HTTP::Headers> object containing the headers for the current request.
fc7ec1d9 297
298 print $c->request->headers->header('X-Catalyst');
299
b5ecfcf0 300=head2 $req->hostname
0556eb49 301
3e19f4f6 302Returns the hostname of the client.
e5ecd5bc 303
b5ecfcf0 304=head2 $req->input
61bacdcc 305
3e19f4f6 306Alias for $req->body.
61bacdcc 307
3b4d1251 308=head2 $req->query_keywords
309
310Contains the keywords portion of a query string, when no '=' signs are
311present.
312
313 http://localhost/path?some+keywords
ac5c933b 314
3b4d1251 315 $c->request->query_keywords will contain 'some keywords'
316
b5ecfcf0 317=head2 $req->match
fc7ec1d9 318
3e19f4f6 319This contains the matching part of a Regex action. Otherwise
2c83fd5a 320it returns the same as 'action', except for default actions,
321which return an empty string.
fc7ec1d9 322
b5ecfcf0 323=head2 $req->method
b5176d9e 324
325Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
326
b5ecfcf0 327=head2 $req->param
e7c0c583 328
ac5c933b 329Returns GET and POST parameters with a CGI.pm-compatible param method. This
3e19f4f6 330is an alternative method for accessing parameters in $c->req->parameters.
e7c0c583 331
a82c2894 332 $value = $c->request->param( 'foo' );
333 @values = $c->request->param( 'foo' );
e7c0c583 334 @params = $c->request->param;
335
3e705254 336Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
a82c2894 337arguments to this method, like this:
338
85d9fce6 339 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
a82c2894 340
341will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
342C<quxx>. Previously this would have added C<bar> as another value to C<foo>
3e19f4f6 343(creating it if it didn't exist before), and C<quxx> as another value for
344C<gorch>.
a82c2894 345
e7c0c583 346=cut
347
348sub param {
349 my $self = shift;
350
351 if ( @_ == 0 ) {
352 return keys %{ $self->parameters };
353 }
354
bfde09a2 355 if ( @_ == 1 ) {
e7c0c583 356
bfde09a2 357 my $param = shift;
6bd2b72c 358
bfde09a2 359 unless ( exists $self->parameters->{$param} ) {
360 return wantarray ? () : undef;
361 }
362
363 if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
364 return (wantarray)
365 ? @{ $self->parameters->{$param} }
366 : $self->parameters->{$param}->[0];
367 }
368 else {
369 return (wantarray)
370 ? ( $self->parameters->{$param} )
371 : $self->parameters->{$param};
372 }
d7945f32 373 }
a82c2894 374 elsif ( @_ > 1 ) {
375 my $field = shift;
376 $self->parameters->{$field} = [@_];
d7945f32 377 }
e7c0c583 378}
b5176d9e 379
b5ecfcf0 380=head2 $req->parameters
61b1e958 381
3e19f4f6 382Returns a reference to a hash containing GET and POST parameters. Values can
d08ced28 383be either a scalar or an arrayref containing scalars.
fc7ec1d9 384
e7c0c583 385 print $c->request->parameters->{field};
386 print $c->request->parameters->{field}->[0];
fc7ec1d9 387
c436c1e8 388This is the combination of C<query_parameters> and C<body_parameters>.
389
b5ecfcf0 390=head2 $req->params
3e19f4f6 391
392Shortcut for $req->parameters.
393
b5ecfcf0 394=head2 $req->path
fc7ec1d9 395
3e19f4f6 396Returns the path, i.e. the part of the URI after $req->base, for the current request.
fc7ec1d9 397
b5ecfcf0 398=head2 $req->path_info
fbcc39ad 399
3e19f4f6 400Alias for path, added for compability with L<CGI>.
fbcc39ad 401
402=cut
403
404sub path {
02fb5d78 405 my ( $self, @params ) = @_;
4f5ebacd 406
02fb5d78 407 if (@params) {
408 $self->uri->path(@params);
409 undef $self->{path};
fbcc39ad 410 }
02fb5d78 411 elsif ( defined( my $path = $self->{path} ) ) {
412 return $path;
e561386f 413 }
02fb5d78 414 else {
415 my $path = $self->uri->path;
416 my $location = $self->base->path;
417 $path =~ s/^(\Q$location\E)?//;
418 $path =~ s/^\///;
419 $self->{path} = $path;
fbcc39ad 420
02fb5d78 421 return $path;
422 }
fbcc39ad 423}
424
b5ecfcf0 425=head2 $req->protocol
bfde09a2 426
3e19f4f6 427Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
bfde09a2 428
b5ecfcf0 429=head2 $req->query_parameters
fbcc39ad 430
def54ce2 431=head2 $req->query_params
432
3e19f4f6 433Returns a reference to a hash containing query string (GET) parameters. Values can
fbcc39ad 434be either a scalar or an arrayref containing scalars.
435
436 print $c->request->query_parameters->{field};
437 print $c->request->query_parameters->{field}->[0];
ac5c933b 438
b5ecfcf0 439=head2 $req->read( [$maxlength] )
fbcc39ad 440
3e19f4f6 441Reads a chunk of data from the request body. This method is intended to be
442used in a while loop, reading $maxlength bytes on every call. $maxlength
fbcc39ad 443defaults to the size of the request if not specified.
444
445You have to set MyApp->config->{parse_on_demand} to use this directly.
446
b5ecfcf0 447=head2 $req->referer
fc7ec1d9 448
3e19f4f6 449Shortcut for $req->headers->referer. Returns the referring page.
fc7ec1d9 450
b5ecfcf0 451=head2 $req->secure
bfde09a2 452
3e19f4f6 453Returns true or false, indicating whether the connection is secure (https).
bfde09a2 454
2982e768 455=head2 $req->captures
456
457Returns a reference to an array containing regex captures.
fc7ec1d9 458
2982e768 459 my @captures = @{ $c->request->captures };
460
461=head2 $req->snippets
fc7ec1d9 462
2982e768 463C<captures> used to be called snippets. This is still available for backwoards
464compatibility, but is considered deprecated.
fc7ec1d9 465
b5ecfcf0 466=head2 $req->upload
e7c0c583 467
3e19f4f6 468A convenient method to access $req->uploads.
e7c0c583 469
470 $upload = $c->request->upload('field');
471 @uploads = $c->request->upload('field');
472 @fields = $c->request->upload;
bfde09a2 473
e7c0c583 474 for my $upload ( $c->request->upload('field') ) {
146554c5 475 print $upload->filename;
e7c0c583 476 }
477
478=cut
479
480sub upload {
481 my $self = shift;
482
483 if ( @_ == 0 ) {
484 return keys %{ $self->uploads };
485 }
486
bfde09a2 487 if ( @_ == 1 ) {
e7c0c583 488
bfde09a2 489 my $upload = shift;
490
491 unless ( exists $self->uploads->{$upload} ) {
492 return wantarray ? () : undef;
493 }
6bd2b72c 494
bfde09a2 495 if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
496 return (wantarray)
497 ? @{ $self->uploads->{$upload} }
498 : $self->uploads->{$upload}->[0];
499 }
500 else {
501 return (wantarray)
fbcc39ad 502 ? ( $self->uploads->{$upload} )
503 : $self->uploads->{$upload};
bfde09a2 504 }
d7945f32 505 }
bfde09a2 506
a4f5c51e 507 if ( @_ > 1 ) {
bfde09a2 508
509 while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
510
511 if ( exists $self->uploads->{$field} ) {
512 for ( $self->uploads->{$field} ) {
513 $_ = [$_] unless ref($_) eq "ARRAY";
514 push( @$_, $upload );
515 }
516 }
517 else {
518 $self->uploads->{$field} = $upload;
519 }
520 }
e7c0c583 521 }
522}
523
b5ecfcf0 524=head2 $req->uploads
fc7ec1d9 525
bfde09a2 526Returns a reference to a hash containing uploads. Values can be either a
ac5c933b 527L<Catalyst::Request::Upload> object, or an arrayref of
84e7aa89 528L<Catalyst::Request::Upload> objects.
e7c0c583 529
530 my $upload = $c->request->uploads->{field};
531 my $upload = $c->request->uploads->{field}->[0];
532
b5ecfcf0 533=head2 $req->uri
fbcc39ad 534
3e19f4f6 535Returns a URI object for the current request. Stringifies to the URI text.
fbcc39ad 536
bd917b94 537=head2 $req->uri_with( { key => 'value' } );
538
3338e8ce 539Returns a rewritten URI object for the current request. Key/value pairs
540passed in will override existing parameters. Unmodified pairs will be
541preserved.
bd917b94 542
543=cut
544
545sub uri_with {
546 my( $self, $args ) = @_;
ac5c933b 547
bd917b94 548 carp( 'No arguments passed to uri_with()' ) unless $args;
fbb513f7 549
fc42a730 550 for my $value ( values %$args ) {
d0f0fcf6 551 next unless defined $value;
fbb513f7 552 for ( ref $value eq 'ARRAY' ? @$value : $value ) {
553 $_ = "$_";
7066a4d5 554 utf8::encode( $_ ) if utf8::is_utf8($_);
fc42a730 555 }
fc42a730 556 };
ac5c933b 557
bd917b94 558 my $uri = $self->uri->clone;
ac5c933b 559
bd917b94 560 $uri->query_form( {
27d9619a 561 %{ $uri->query_form_hash },
bd917b94 562 %$args
563 } );
564 return $uri;
565}
566
b5ecfcf0 567=head2 $req->user
7ce7ca2e 568
3e19f4f6 569Returns the currently logged in user. Deprecated. The method recommended for
570newer plugins is $c->user.
7ce7ca2e 571
b5ecfcf0 572=head2 $req->user_agent
b5176d9e 573
3e19f4f6 574Shortcut to $req->headers->user_agent. Returns the user agent (browser)
575version string.
b5176d9e 576
059c085b 577=head2 meta
578
579Provided by Moose
580
3e19f4f6 581=head1 AUTHORS
fc7ec1d9 582
583Sebastian Riedel, C<sri@cpan.org>
3e19f4f6 584
61b1e958 585Marcus Ramberg, C<mramberg@cpan.org>
fc7ec1d9 586
587=head1 COPYRIGHT
588
e7c0c583 589This program is free software, you can redistribute it and/or modify
61b1e958 590it under the same terms as Perl itself.
fc7ec1d9 591
592=cut
593
e5ecd5bc 594__PACKAGE__->meta->make_immutable;
595
fc7ec1d9 5961;