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