Commit | Line | Data |
fc7ec1d9 |
1 | package Catalyst::Request; |
2 | |
b4ca0ee8 |
3 | use IO::Socket qw[AF_INET inet_aton]; |
bd917b94 |
4 | use Carp; |
fc42a730 |
5 | use utf8; |
de19de2e |
6 | use URI::http; |
7 | use URI::https; |
e669e88a |
8 | use URI::QueryParam; |
6680c772 |
9 | use HTTP::Headers; |
b4ca0ee8 |
10 | |
059c085b |
11 | use Moose; |
12 | |
6802c884 |
13 | use namespace::clean -except => 'meta'; |
14 | |
b99ff5d8 |
15 | with 'MooseX::Emulate::Class::Accessor::Fast'; |
16 | |
faa02805 |
17 | has env => (is => 'ro', writer => '_set_env'); |
18 | |
19 | has _read_position => ( is => 'rw', default => 0 ); |
20 | has _read_length => ( is => 'ro', |
21 | default => sub { |
22 | my $self = shift; |
23 | $self->header('Content-Length') || 0; |
24 | }, |
25 | lazy => 1, |
26 | ); |
27 | |
5fb12dbb |
28 | has action => (is => 'rw'); |
29 | has address => (is => 'rw'); |
30 | has arguments => (is => 'rw', default => sub { [] }); |
31 | has cookies => (is => 'rw', default => sub { {} }); |
32 | has query_keywords => (is => 'rw'); |
33 | has match => (is => 'rw'); |
34 | has method => (is => 'rw'); |
35 | has protocol => (is => 'rw'); |
059c085b |
36 | has query_parameters => (is => 'rw', default => sub { {} }); |
5fb12dbb |
37 | has secure => (is => 'rw', default => 0); |
38 | has captures => (is => 'rw', default => sub { [] }); |
6cb9e383 |
39 | has uri => (is => 'rw', predicate => 'has_uri'); |
8026359e |
40 | has remote_user => (is => 'rw'); |
5fb12dbb |
41 | has headers => ( |
e5ecd5bc |
42 | is => 'rw', |
059c085b |
43 | isa => 'HTTP::Headers', |
44 | handles => [qw(content_encoding content_length content_type header referer user_agent)], |
6680c772 |
45 | default => sub { HTTP::Headers->new() }, |
46 | required => 1, |
47 | lazy => 1, |
059c085b |
48 | ); |
49 | |
50 | has _context => ( |
51 | is => 'rw', |
52 | weak_ref => 1, |
02570318 |
53 | clearer => '_clear_context', |
059c085b |
54 | ); |
55 | |
f083854e |
56 | # Amount of data to read from input on each pass |
57 | our $CHUNKSIZE = 64 * 1024; |
58 | |
59 | sub read { |
60 | my ($self, $maxlength) = @_; |
61 | my $remaining = $self->_read_length - $self->_read_position; |
62 | $maxlength ||= $CHUNKSIZE; |
63 | |
64 | # Are we done reading? |
65 | if ( $remaining <= 0 ) { |
66 | return; |
67 | } |
68 | |
69 | my $readlen = ( $remaining > $maxlength ) ? $maxlength : $remaining; |
70 | my $rc = $self->read_chunk( my $buffer, $readlen ); |
71 | if ( defined $rc ) { |
72 | if (0 == $rc) { # Nothing more to read even though Content-Length |
73 | # said there should be. |
74 | return; |
75 | } |
76 | $self->_read_position( $self->_read_position + $rc ); |
77 | return $buffer; |
78 | } |
79 | else { |
80 | Catalyst::Exception->throw( |
81 | message => "Unknown error reading input: $!" ); |
82 | } |
83 | } |
84 | |
87f50436 |
85 | sub read_chunk { |
86 | my $self = shift; |
87 | return $self->env->{'psgi.input'}->read(@_); |
88 | } |
89 | |
059c085b |
90 | has body_parameters => ( |
5fb12dbb |
91 | is => 'rw', |
92 | required => 1, |
93 | lazy => 1, |
94 | default => sub { {} }, |
fc7ec1d9 |
95 | ); |
96 | |
059c085b |
97 | has uploads => ( |
5fb12dbb |
98 | is => 'rw', |
99 | required => 1, |
5fb12dbb |
100 | default => sub { {} }, |
059c085b |
101 | ); |
102 | |
059c085b |
103 | has parameters => ( |
1cbdfa9b |
104 | is => 'rw', |
105 | lazy => 1, |
106 | builder => 'prepare_parameters', |
059c085b |
107 | ); |
108 | |
341620d5 |
109 | # TODO: |
110 | # - Can we lose the before modifiers which just call prepare_body ? |
111 | # they are wasteful, slow us down and feel cluttery. |
112 | |
113 | # Can we make _body an attribute, have the rest of |
114 | # these lazy build from there and kill all the direct hash access |
115 | # in Catalyst.pm and Engine.pm? |
116 | |
1cbdfa9b |
117 | sub prepare_parameters { |
118 | my ( $self ) = @_; |
119 | |
398f13db |
120 | $self->prepare_body; |
1cbdfa9b |
121 | my $parameters = {}; |
122 | my $body_parameters = $self->body_parameters; |
123 | my $query_parameters = $self->query_parameters; |
124 | # We copy, no references |
125 | foreach my $name (keys %$query_parameters) { |
126 | my $param = $query_parameters->{$name}; |
127 | $parameters->{$name} = ref $param eq 'ARRAY' ? [ @$param ] : $param; |
128 | } |
129 | |
130 | # Merge query and body parameters |
131 | foreach my $name (keys %$body_parameters) { |
132 | my $param = $body_parameters->{$name}; |
133 | my @values = ref $param eq 'ARRAY' ? @$param : ($param); |
134 | if ( my $existing = $parameters->{$name} ) { |
135 | unshift(@values, (ref $existing eq 'ARRAY' ? @$existing : $existing)); |
136 | } |
137 | $parameters->{$name} = @values > 1 ? \@values : $values[0]; |
138 | } |
139 | $parameters; |
140 | } |
141 | |
398f13db |
142 | before body_parameters => sub { |
143 | my ($self) = @_; |
144 | $self->prepare_body; |
145 | $self->prepare_body_parameters; |
146 | }; |
147 | |
148 | =head2 $self->prepare_body() |
149 | |
150 | sets up the L<Catalyst::Request> object body using L<HTTP::Body> |
151 | |
152 | =cut |
153 | |
154 | has _uploadtmp => ( |
155 | is => 'ro', |
156 | predicate => '_has_uploadtmp', |
157 | ); |
158 | |
159 | sub prepare_body { |
160 | my ( $self ) = @_; |
161 | |
162 | if ( my $length = $self->_read_length ) { |
163 | unless ( $self->_body ) { |
164 | my $type = $self->header('Content-Type'); |
165 | $self->_body(HTTP::Body->new( $type, $length )); |
166 | $self->_body->cleanup(1); # Make extra sure! |
167 | $self->_body->tmpdir( $self->_uploadtmp ) |
168 | if $self->_has_uploadtmp; |
169 | } |
170 | |
171 | # Check for definedness as you could read '0' |
172 | while ( defined ( my $buffer = $self->read() ) ) { |
173 | $self->prepare_body_chunk($buffer); |
174 | } |
175 | |
176 | # paranoia against wrong Content-Length header |
177 | my $remaining = $length - $self->_read_position; |
178 | if ( $remaining > 0 ) { |
179 | Catalyst::Exception->throw( |
180 | "Wrong Content-Length value: $length" ); |
181 | } |
182 | } |
183 | else { |
184 | # Defined but will cause all body code to be skipped |
185 | $self->_body(0); |
186 | } |
187 | } |
188 | |
189 | =head2 $self->prepare_body_chunk() |
190 | |
191 | Add a chunk to the request body. |
192 | |
193 | =cut |
194 | |
195 | sub prepare_body_chunk { |
196 | my ( $self, $chunk ) = @_; |
197 | |
198 | $self->_body->add($chunk); |
199 | } |
200 | |
201 | =head2 $self->prepare_body_parameters() |
202 | |
203 | Sets up parameters from body. |
204 | |
205 | =cut |
206 | |
207 | sub prepare_body_parameters { |
208 | my ( $self ) = @_; |
209 | |
210 | return unless $self->_body; |
211 | |
212 | $self->{body_parameters} = $self->_body->param; # FIXME!! Recursion here. |
213 | } |
341620d5 |
214 | |
2f498a7e |
215 | sub prepare_connection { |
216 | my ($self) = @_; |
217 | |
218 | my $env = $self->env; |
219 | |
220 | $self->address( $env->{REMOTE_ADDR} ); |
221 | $self->hostname( $env->{REMOTE_HOST} ) |
222 | if exists $env->{REMOTE_HOST}; |
223 | $self->protocol( $env->{SERVER_PROTOCOL} ); |
224 | $self->remote_user( $env->{REMOTE_USER} ); |
225 | $self->method( $env->{REQUEST_METHOD} ); |
226 | $self->secure( $env->{'psgi.url_scheme'} eq 'https' ? 1 : 0 ); |
227 | } |
228 | |
229 | # XXX - FIXME - method is here now, move this crap... |
e99ec2dc |
230 | around parameters => sub { |
231 | my ($orig, $self, $params) = @_; |
232 | if ($params) { |
233 | if ( !ref $params ) { |
234 | $self->_context->log->warn( |
235 | "Attempt to retrieve '$params' with req->params(), " . |
236 | "you probably meant to call req->param('$params')" |
237 | ); |
238 | $params = undef; |
239 | } |
240 | return $self->$orig($params); |
241 | } |
242 | $self->$orig(); |
059c085b |
243 | }; |
244 | |
245 | has base => ( |
5fb12dbb |
246 | is => 'rw', |
247 | required => 1, |
248 | lazy => 1, |
249 | default => sub { |
059c085b |
250 | my $self = shift; |
6cb9e383 |
251 | return $self->path if $self->has_uri; |
059c085b |
252 | }, |
253 | ); |
254 | |
069355da |
255 | has _body => ( |
0f56bbcf |
256 | is => 'rw', clearer => '_clear_body', predicate => '_has_body', |
059c085b |
257 | ); |
610bc6ec |
258 | # Eugh, ugly. Should just be able to rename accessor methods to 'body' |
b0ad47c1 |
259 | # and provide a custom reader.. |
610bc6ec |
260 | sub body { |
261 | my $self = shift; |
398f13db |
262 | $self->prepare_body(); |
14c057aa |
263 | croak 'body is a reader' if scalar @_; |
610bc6ec |
264 | return blessed $self->_body ? $self->_body->body : $self->_body; |
265 | } |
059c085b |
266 | |
267 | has hostname => ( |
268 | is => 'rw', |
269 | required => 1, |
270 | lazy => 1, |
271 | default => sub { |
272 | my ($self) = @_; |
9fb936e5 |
273 | gethostbyaddr( inet_aton( $self->address ), AF_INET ) || $self->address |
059c085b |
274 | }, |
275 | ); |
276 | |
02570318 |
277 | has _path => ( is => 'rw', predicate => '_has_path', clearer => '_clear_path' ); |
278 | |
8026359e |
279 | # XXX: Deprecated in docs ages ago (2006), deprecated with warning in 5.8000 due |
280 | # to confusion between Engines and Plugin::Authentication. Remove in 5.8100? |
281 | has user => (is => 'rw'); |
282 | |
059c085b |
283 | sub args { shift->arguments(@_) } |
284 | sub body_params { shift->body_parameters(@_) } |
285 | sub input { shift->body(@_) } |
286 | sub params { shift->parameters(@_) } |
287 | sub query_params { shift->query_parameters(@_) } |
288 | sub path_info { shift->path(@_) } |
289 | sub snippets { shift->captures(@_) } |
f7e4e231 |
290 | |
965f3e35 |
291 | =for stopwords param params |
292 | |
fc7ec1d9 |
293 | =head1 NAME |
294 | |
3e19f4f6 |
295 | Catalyst::Request - provides information about the current client request |
fc7ec1d9 |
296 | |
297 | =head1 SYNOPSIS |
298 | |
b22c6668 |
299 | $req = $c->request; |
300 | $req->action; |
301 | $req->address; |
b22c6668 |
302 | $req->arguments; |
3e19f4f6 |
303 | $req->args; |
b22c6668 |
304 | $req->base; |
06e1b616 |
305 | $req->body; |
fbcc39ad |
306 | $req->body_parameters; |
b5176d9e |
307 | $req->content_encoding; |
308 | $req->content_length; |
309 | $req->content_type; |
b77e7869 |
310 | $req->cookie; |
b22c6668 |
311 | $req->cookies; |
b5176d9e |
312 | $req->header; |
b22c6668 |
313 | $req->headers; |
314 | $req->hostname; |
61bacdcc |
315 | $req->input; |
3b4d1251 |
316 | $req->query_keywords; |
b22c6668 |
317 | $req->match; |
318 | $req->method; |
e7c0c583 |
319 | $req->param; |
e7c0c583 |
320 | $req->parameters; |
3e19f4f6 |
321 | $req->params; |
b22c6668 |
322 | $req->path; |
bfde09a2 |
323 | $req->protocol; |
fbcc39ad |
324 | $req->query_parameters; |
325 | $req->read; |
b5176d9e |
326 | $req->referer; |
bfde09a2 |
327 | $req->secure; |
2982e768 |
328 | $req->captures; # previously knows as snippets |
e7c0c583 |
329 | $req->upload; |
b22c6668 |
330 | $req->uploads; |
77d12cae |
331 | $req->uri; |
7ce7ca2e |
332 | $req->user; |
66294129 |
333 | $req->user_agent; |
b22c6668 |
334 | |
3e22baa5 |
335 | See also L<Catalyst>, L<Catalyst::Request::Upload>. |
fc7ec1d9 |
336 | |
337 | =head1 DESCRIPTION |
338 | |
3e19f4f6 |
339 | This is the Catalyst Request class, which provides an interface to data for the |
340 | current client request. The request object is prepared by L<Catalyst::Engine>, |
341 | thus hiding the details of the particular engine implementation. |
b22c6668 |
342 | |
343 | =head1 METHODS |
fc7ec1d9 |
344 | |
b5ecfcf0 |
345 | =head2 $req->action |
fc7ec1d9 |
346 | |
aae8d418 |
347 | [DEPRECATED] Returns the name of the requested action. |
348 | |
349 | |
350 | Use C<< $c->action >> instead (which returns a |
351 | L<Catalyst::Action|Catalyst::Action> object). |
fc7ec1d9 |
352 | |
b5ecfcf0 |
353 | =head2 $req->address |
0556eb49 |
354 | |
3e19f4f6 |
355 | Returns the IP address of the client. |
61b1e958 |
356 | |
b5ecfcf0 |
357 | =head2 $req->arguments |
61b1e958 |
358 | |
b22c6668 |
359 | Returns a reference to an array containing the arguments. |
fc7ec1d9 |
360 | |
361 | print $c->request->arguments->[0]; |
362 | |
c436c1e8 |
363 | For example, if your action was |
364 | |
7d7519a4 |
365 | package MyApp::Controller::Foo; |
85d9fce6 |
366 | |
367 | sub moose : Local { |
368 | ... |
369 | } |
c436c1e8 |
370 | |
3e19f4f6 |
371 | and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah> |
c436c1e8 |
372 | would be the first and only argument. |
373 | |
6d920953 |
374 | Arguments get automatically URI-unescaped for you. |
8f58057d |
375 | |
b5ecfcf0 |
376 | =head2 $req->args |
3e19f4f6 |
377 | |
01011731 |
378 | Shortcut for L</arguments>. |
3e19f4f6 |
379 | |
b5ecfcf0 |
380 | =head2 $req->base |
fc7ec1d9 |
381 | |
328f225e |
382 | Contains the URI base. This will always have a trailing slash. Note that the |
f4dda4a8 |
383 | URI scheme (e.g., http vs. https) must be determined through heuristics; |
328f225e |
384 | depending on your server configuration, it may be incorrect. See $req->secure |
385 | for more info. |
c436c1e8 |
386 | |
3e19f4f6 |
387 | If your application was queried with the URI |
388 | C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>. |
fc7ec1d9 |
389 | |
b5ecfcf0 |
390 | =head2 $req->body |
06e1b616 |
391 | |
843871cf |
392 | Returns the message body of the request, as returned by L<HTTP::Body>: a string, |
393 | unless Content-Type is C<application/x-www-form-urlencoded>, C<text/xml>, or |
394 | C<multipart/form-data>, in which case a L<File::Temp> object is returned. |
e060fe05 |
395 | |
b5ecfcf0 |
396 | =head2 $req->body_parameters |
fbcc39ad |
397 | |
3e19f4f6 |
398 | Returns a reference to a hash containing body (POST) parameters. Values can |
fbcc39ad |
399 | be either a scalar or an arrayref containing scalars. |
400 | |
401 | print $c->request->body_parameters->{field}; |
402 | print $c->request->body_parameters->{field}->[0]; |
c436c1e8 |
403 | |
d631b5f9 |
404 | These are the parameters from the POST part of the request, if any. |
e5ecd5bc |
405 | |
b5ecfcf0 |
406 | =head2 $req->body_params |
fbcc39ad |
407 | |
3e19f4f6 |
408 | Shortcut for body_parameters. |
fbcc39ad |
409 | |
b5ecfcf0 |
410 | =head2 $req->content_encoding |
b5176d9e |
411 | |
3e19f4f6 |
412 | Shortcut for $req->headers->content_encoding. |
b5176d9e |
413 | |
b5ecfcf0 |
414 | =head2 $req->content_length |
b5176d9e |
415 | |
3e19f4f6 |
416 | Shortcut for $req->headers->content_length. |
b5176d9e |
417 | |
b5ecfcf0 |
418 | =head2 $req->content_type |
b5176d9e |
419 | |
3e19f4f6 |
420 | Shortcut for $req->headers->content_type. |
b5176d9e |
421 | |
b5ecfcf0 |
422 | =head2 $req->cookie |
3ad654e0 |
423 | |
3e19f4f6 |
424 | A convenient method to access $req->cookies. |
3ad654e0 |
425 | |
426 | $cookie = $c->request->cookie('name'); |
427 | @cookies = $c->request->cookie; |
428 | |
429 | =cut |
430 | |
431 | sub cookie { |
432 | my $self = shift; |
433 | |
434 | if ( @_ == 0 ) { |
b77e7869 |
435 | return keys %{ $self->cookies }; |
3ad654e0 |
436 | } |
437 | |
438 | if ( @_ == 1 ) { |
439 | |
440 | my $name = shift; |
441 | |
b77e7869 |
442 | unless ( exists $self->cookies->{$name} ) { |
3ad654e0 |
443 | return undef; |
444 | } |
fbcc39ad |
445 | |
b77e7869 |
446 | return $self->cookies->{$name}; |
3ad654e0 |
447 | } |
448 | } |
449 | |
b5ecfcf0 |
450 | =head2 $req->cookies |
fc7ec1d9 |
451 | |
b22c6668 |
452 | Returns a reference to a hash containing the cookies. |
fc7ec1d9 |
453 | |
454 | print $c->request->cookies->{mycookie}->value; |
455 | |
7e743798 |
456 | The cookies in the hash are indexed by name, and the values are L<CGI::Simple::Cookie> |
c436c1e8 |
457 | objects. |
458 | |
b5ecfcf0 |
459 | =head2 $req->header |
b5176d9e |
460 | |
3e19f4f6 |
461 | Shortcut for $req->headers->header. |
b5176d9e |
462 | |
b5ecfcf0 |
463 | =head2 $req->headers |
fc7ec1d9 |
464 | |
3e19f4f6 |
465 | Returns an L<HTTP::Headers> object containing the headers for the current request. |
fc7ec1d9 |
466 | |
467 | print $c->request->headers->header('X-Catalyst'); |
468 | |
b5ecfcf0 |
469 | =head2 $req->hostname |
0556eb49 |
470 | |
178dca5f |
471 | Returns the hostname of the client. Use C<< $req->uri->host >> to get the hostname of the server. |
e5ecd5bc |
472 | |
b5ecfcf0 |
473 | =head2 $req->input |
61bacdcc |
474 | |
3e19f4f6 |
475 | Alias for $req->body. |
61bacdcc |
476 | |
3b4d1251 |
477 | =head2 $req->query_keywords |
478 | |
479 | Contains the keywords portion of a query string, when no '=' signs are |
480 | present. |
481 | |
482 | http://localhost/path?some+keywords |
b0ad47c1 |
483 | |
3b4d1251 |
484 | $c->request->query_keywords will contain 'some keywords' |
485 | |
b5ecfcf0 |
486 | =head2 $req->match |
fc7ec1d9 |
487 | |
3e19f4f6 |
488 | This contains the matching part of a Regex action. Otherwise |
2c83fd5a |
489 | it returns the same as 'action', except for default actions, |
490 | which return an empty string. |
fc7ec1d9 |
491 | |
b5ecfcf0 |
492 | =head2 $req->method |
b5176d9e |
493 | |
494 | Contains the request method (C<GET>, C<POST>, C<HEAD>, etc). |
495 | |
b5ecfcf0 |
496 | =head2 $req->param |
e7c0c583 |
497 | |
b0ad47c1 |
498 | Returns GET and POST parameters with a CGI.pm-compatible param method. This |
3e19f4f6 |
499 | is an alternative method for accessing parameters in $c->req->parameters. |
e7c0c583 |
500 | |
a82c2894 |
501 | $value = $c->request->param( 'foo' ); |
502 | @values = $c->request->param( 'foo' ); |
e7c0c583 |
503 | @params = $c->request->param; |
504 | |
3e705254 |
505 | Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple |
a82c2894 |
506 | arguments to this method, like this: |
507 | |
85d9fce6 |
508 | $c->request->param( 'foo', 'bar', 'gorch', 'quxx' ); |
a82c2894 |
509 | |
510 | will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and |
511 | C<quxx>. Previously this would have added C<bar> as another value to C<foo> |
3e19f4f6 |
512 | (creating it if it didn't exist before), and C<quxx> as another value for |
513 | C<gorch>. |
a82c2894 |
514 | |
83312afd |
515 | B<NOTE> this is considered a legacy interface and care should be taken when |
516 | using it. C<< scalar $c->req->param( 'foo' ) >> will return only the first |
517 | C<foo> param even if multiple are present; C<< $c->req->param( 'foo' ) >> will |
518 | return a list of as many are present, which can have unexpected consequences |
519 | when writing code of the form: |
520 | |
521 | $foo->bar( |
522 | a => 'b', |
523 | baz => $c->req->param( 'baz' ), |
524 | ); |
525 | |
526 | If multiple C<baz> parameters are provided this code might corrupt data or |
527 | cause a hash initialization error. For a more straightforward interface see |
528 | C<< $c->req->parameters >>. |
529 | |
e7c0c583 |
530 | =cut |
531 | |
532 | sub param { |
533 | my $self = shift; |
534 | |
535 | if ( @_ == 0 ) { |
536 | return keys %{ $self->parameters }; |
537 | } |
538 | |
bfde09a2 |
539 | if ( @_ == 1 ) { |
e7c0c583 |
540 | |
bfde09a2 |
541 | my $param = shift; |
6bd2b72c |
542 | |
bfde09a2 |
543 | unless ( exists $self->parameters->{$param} ) { |
544 | return wantarray ? () : undef; |
545 | } |
546 | |
547 | if ( ref $self->parameters->{$param} eq 'ARRAY' ) { |
548 | return (wantarray) |
549 | ? @{ $self->parameters->{$param} } |
550 | : $self->parameters->{$param}->[0]; |
551 | } |
552 | else { |
553 | return (wantarray) |
554 | ? ( $self->parameters->{$param} ) |
555 | : $self->parameters->{$param}; |
556 | } |
d7945f32 |
557 | } |
a82c2894 |
558 | elsif ( @_ > 1 ) { |
559 | my $field = shift; |
560 | $self->parameters->{$field} = [@_]; |
d7945f32 |
561 | } |
e7c0c583 |
562 | } |
b5176d9e |
563 | |
b5ecfcf0 |
564 | =head2 $req->parameters |
61b1e958 |
565 | |
3e19f4f6 |
566 | Returns a reference to a hash containing GET and POST parameters. Values can |
d08ced28 |
567 | be either a scalar or an arrayref containing scalars. |
fc7ec1d9 |
568 | |
e7c0c583 |
569 | print $c->request->parameters->{field}; |
570 | print $c->request->parameters->{field}->[0]; |
fc7ec1d9 |
571 | |
c436c1e8 |
572 | This is the combination of C<query_parameters> and C<body_parameters>. |
573 | |
b5ecfcf0 |
574 | =head2 $req->params |
3e19f4f6 |
575 | |
576 | Shortcut for $req->parameters. |
577 | |
b5ecfcf0 |
578 | =head2 $req->path |
fc7ec1d9 |
579 | |
3e19f4f6 |
580 | Returns the path, i.e. the part of the URI after $req->base, for the current request. |
fc7ec1d9 |
581 | |
be6801fa |
582 | http://localhost/path/foo |
583 | |
584 | $c->request->path will contain 'path/foo' |
585 | |
b5ecfcf0 |
586 | =head2 $req->path_info |
fbcc39ad |
587 | |
10011c19 |
588 | Alias for path, added for compatibility with L<CGI>. |
fbcc39ad |
589 | |
590 | =cut |
591 | |
592 | sub path { |
02fb5d78 |
593 | my ( $self, @params ) = @_; |
4f5ebacd |
594 | |
02fb5d78 |
595 | if (@params) { |
596 | $self->uri->path(@params); |
02570318 |
597 | $self->_clear_path; |
fbcc39ad |
598 | } |
02570318 |
599 | elsif ( $self->_has_path ) { |
600 | return $self->_path; |
e561386f |
601 | } |
02fb5d78 |
602 | else { |
603 | my $path = $self->uri->path; |
604 | my $location = $self->base->path; |
605 | $path =~ s/^(\Q$location\E)?//; |
606 | $path =~ s/^\///; |
02570318 |
607 | $self->_path($path); |
fbcc39ad |
608 | |
02fb5d78 |
609 | return $path; |
610 | } |
fbcc39ad |
611 | } |
612 | |
b5ecfcf0 |
613 | =head2 $req->protocol |
bfde09a2 |
614 | |
3e19f4f6 |
615 | Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request. |
bfde09a2 |
616 | |
b5ecfcf0 |
617 | =head2 $req->query_parameters |
fbcc39ad |
618 | |
def54ce2 |
619 | =head2 $req->query_params |
620 | |
3e19f4f6 |
621 | Returns a reference to a hash containing query string (GET) parameters. Values can |
fbcc39ad |
622 | be either a scalar or an arrayref containing scalars. |
623 | |
624 | print $c->request->query_parameters->{field}; |
625 | print $c->request->query_parameters->{field}->[0]; |
b0ad47c1 |
626 | |
b5ecfcf0 |
627 | =head2 $req->read( [$maxlength] ) |
fbcc39ad |
628 | |
3e19f4f6 |
629 | Reads a chunk of data from the request body. This method is intended to be |
630 | used in a while loop, reading $maxlength bytes on every call. $maxlength |
fbcc39ad |
631 | defaults to the size of the request if not specified. |
632 | |
87f50436 |
633 | =head2 $req->read_chunk(\$buff, $max) |
634 | |
635 | Reads a chunk.. |
636 | |
9779c885 |
637 | You have to set MyApp->config(parse_on_demand => 1) to use this directly. |
fbcc39ad |
638 | |
b5ecfcf0 |
639 | =head2 $req->referer |
fc7ec1d9 |
640 | |
3e19f4f6 |
641 | Shortcut for $req->headers->referer. Returns the referring page. |
fc7ec1d9 |
642 | |
b5ecfcf0 |
643 | =head2 $req->secure |
bfde09a2 |
644 | |
328f225e |
645 | Returns true or false, indicating whether the connection is secure |
f4dda4a8 |
646 | (https). Note that the URI scheme (e.g., http vs. https) must be determined |
ae7da8f5 |
647 | through heuristics, and therefore the reliability of $req->secure will depend |
328f225e |
648 | on your server configuration. If you are serving secure pages on the standard |
649 | SSL port (443) and/or setting the HTTPS environment variable, $req->secure |
650 | should be valid. |
bfde09a2 |
651 | |
2982e768 |
652 | =head2 $req->captures |
653 | |
5c6a56e0 |
654 | Returns a reference to an array containing captured args from chained |
655 | actions or regex captures. |
fc7ec1d9 |
656 | |
2982e768 |
657 | my @captures = @{ $c->request->captures }; |
658 | |
659 | =head2 $req->snippets |
fc7ec1d9 |
660 | |
10011c19 |
661 | C<captures> used to be called snippets. This is still available for backwards |
2982e768 |
662 | compatibility, but is considered deprecated. |
fc7ec1d9 |
663 | |
b5ecfcf0 |
664 | =head2 $req->upload |
e7c0c583 |
665 | |
3e19f4f6 |
666 | A convenient method to access $req->uploads. |
e7c0c583 |
667 | |
668 | $upload = $c->request->upload('field'); |
669 | @uploads = $c->request->upload('field'); |
670 | @fields = $c->request->upload; |
bfde09a2 |
671 | |
e7c0c583 |
672 | for my $upload ( $c->request->upload('field') ) { |
146554c5 |
673 | print $upload->filename; |
e7c0c583 |
674 | } |
675 | |
676 | =cut |
677 | |
678 | sub upload { |
679 | my $self = shift; |
680 | |
681 | if ( @_ == 0 ) { |
682 | return keys %{ $self->uploads }; |
683 | } |
684 | |
bfde09a2 |
685 | if ( @_ == 1 ) { |
e7c0c583 |
686 | |
bfde09a2 |
687 | my $upload = shift; |
688 | |
689 | unless ( exists $self->uploads->{$upload} ) { |
690 | return wantarray ? () : undef; |
691 | } |
6bd2b72c |
692 | |
bfde09a2 |
693 | if ( ref $self->uploads->{$upload} eq 'ARRAY' ) { |
694 | return (wantarray) |
695 | ? @{ $self->uploads->{$upload} } |
696 | : $self->uploads->{$upload}->[0]; |
697 | } |
698 | else { |
699 | return (wantarray) |
fbcc39ad |
700 | ? ( $self->uploads->{$upload} ) |
701 | : $self->uploads->{$upload}; |
bfde09a2 |
702 | } |
d7945f32 |
703 | } |
bfde09a2 |
704 | |
a4f5c51e |
705 | if ( @_ > 1 ) { |
bfde09a2 |
706 | |
707 | while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) { |
708 | |
709 | if ( exists $self->uploads->{$field} ) { |
710 | for ( $self->uploads->{$field} ) { |
711 | $_ = [$_] unless ref($_) eq "ARRAY"; |
712 | push( @$_, $upload ); |
713 | } |
714 | } |
715 | else { |
716 | $self->uploads->{$field} = $upload; |
717 | } |
718 | } |
e7c0c583 |
719 | } |
720 | } |
721 | |
b5ecfcf0 |
722 | =head2 $req->uploads |
fc7ec1d9 |
723 | |
bfde09a2 |
724 | Returns a reference to a hash containing uploads. Values can be either a |
b0ad47c1 |
725 | L<Catalyst::Request::Upload> object, or an arrayref of |
84e7aa89 |
726 | L<Catalyst::Request::Upload> objects. |
e7c0c583 |
727 | |
728 | my $upload = $c->request->uploads->{field}; |
729 | my $upload = $c->request->uploads->{field}->[0]; |
730 | |
b5ecfcf0 |
731 | =head2 $req->uri |
fbcc39ad |
732 | |
d26ee0d0 |
733 | Returns a L<URI> object for the current request. Stringifies to the URI text. |
fbcc39ad |
734 | |
a375a206 |
735 | =head2 $req->mangle_params( { key => 'value' }, $appendmode); |
bd917b94 |
736 | |
a375a206 |
737 | Returns a hashref of parameters stemming from the current request's params, |
738 | plus the ones supplied. Keys for which no current param exists will be |
739 | added, keys with undefined values will be removed and keys with existing |
740 | params will be replaced. Note that you can supply a true value as the final |
741 | argument to change behavior with regards to existing parameters, appending |
742 | values rather than replacing them. |
743 | |
744 | A quick example: |
745 | |
746 | # URI query params foo=1 |
747 | my $hashref = $req->mangle_params({ foo => 2 }); |
748 | # Result is query params of foo=2 |
749 | |
750 | versus append mode: |
751 | |
752 | # URI query params foo=1 |
753 | my $hashref = $req->mangle_params({ foo => 2 }, 1); |
754 | # Result is query params of foo=1&foo=2 |
755 | |
756 | This is the code behind C<uri_with>. |
bd917b94 |
757 | |
758 | =cut |
759 | |
a375a206 |
760 | sub mangle_params { |
761 | my ($self, $args, $append) = @_; |
b0ad47c1 |
762 | |
a375a206 |
763 | carp('No arguments passed to mangle_params()') unless $args; |
fbb513f7 |
764 | |
2f381252 |
765 | foreach my $value ( values %$args ) { |
d0f0fcf6 |
766 | next unless defined $value; |
fbb513f7 |
767 | for ( ref $value eq 'ARRAY' ? @$value : $value ) { |
768 | $_ = "$_"; |
7066a4d5 |
769 | utf8::encode( $_ ) if utf8::is_utf8($_); |
fc42a730 |
770 | } |
fc42a730 |
771 | }; |
b0ad47c1 |
772 | |
a375a206 |
773 | my %params = %{ $self->uri->query_form_hash }; |
774 | foreach my $key (keys %{ $args }) { |
775 | my $val = $args->{$key}; |
776 | if(defined($val)) { |
777 | |
778 | if($append && exists($params{$key})) { |
779 | |
780 | # This little bit of heaven handles appending a new value onto |
781 | # an existing one regardless if the existing value is an array |
782 | # or not, and regardless if the new value is an array or not |
783 | $params{$key} = [ |
784 | ref($params{$key}) eq 'ARRAY' ? @{ $params{$key} } : $params{$key}, |
785 | ref($val) eq 'ARRAY' ? @{ $val } : $val |
786 | ]; |
787 | |
788 | } else { |
789 | $params{$key} = $val; |
790 | } |
791 | } else { |
792 | |
793 | # If the param wasn't defined then we delete it. |
794 | delete($params{$key}); |
795 | } |
796 | } |
797 | |
798 | |
799 | return \%params; |
800 | } |
801 | |
802 | =head2 $req->uri_with( { key => 'value' } ); |
803 | |
804 | Returns a rewritten URI object for the current request. Key/value pairs |
805 | passed in will override existing parameters. You can remove an existing |
806 | parameter by passing in an undef value. Unmodified pairs will be |
807 | preserved. |
808 | |
809 | You may also pass an optional second parameter that puts C<uri_with> into |
810 | append mode: |
811 | |
812 | $req->uri_with( { key => 'value' }, { mode => 'append' } ); |
9779c885 |
813 | |
a375a206 |
814 | See C<mangle_params> for an explanation of this behavior. |
815 | |
816 | =cut |
817 | |
818 | sub uri_with { |
819 | my( $self, $args, $behavior) = @_; |
820 | |
821 | carp( 'No arguments passed to uri_with()' ) unless $args; |
822 | |
823 | my $append = 0; |
824 | if((ref($behavior) eq 'HASH') && defined($behavior->{mode}) && ($behavior->{mode} eq 'append')) { |
825 | $append = 1; |
826 | } |
827 | |
828 | my $params = $self->mangle_params($args, $append); |
829 | |
830 | my $uri = $self->uri->clone; |
831 | $uri->query_form($params); |
2f381252 |
832 | |
bd917b94 |
833 | return $uri; |
834 | } |
835 | |
8026359e |
836 | =head2 $req->remote_user |
837 | |
838 | Returns the value of the C<REMOTE_USER> environment variable. |
7ce7ca2e |
839 | |
b5ecfcf0 |
840 | =head2 $req->user_agent |
b5176d9e |
841 | |
3e19f4f6 |
842 | Shortcut to $req->headers->user_agent. Returns the user agent (browser) |
843 | version string. |
b5176d9e |
844 | |
059c085b |
845 | =head2 meta |
846 | |
847 | Provided by Moose |
848 | |
3e19f4f6 |
849 | =head1 AUTHORS |
fc7ec1d9 |
850 | |
2f381252 |
851 | Catalyst Contributors, see Catalyst.pm |
fc7ec1d9 |
852 | |
853 | =head1 COPYRIGHT |
854 | |
536bee89 |
855 | This library is free software. You can redistribute it and/or modify |
61b1e958 |
856 | it under the same terms as Perl itself. |
fc7ec1d9 |
857 | |
858 | =cut |
859 | |
e5ecd5bc |
860 | __PACKAGE__->meta->make_immutable; |
861 | |
fc7ec1d9 |
862 | 1; |