lib/Catalyst/Request.pm

filter: Statementstmt 84.7% 277 / 327 Branchbran 70.4% 117 / 166 Conditioncond 43.7% 21 / 48 MC/DCmcdc 16.6% 4 / 24 Subroutinesub 82.3% 42 / 51 Podpod 95.6% 22 / 23 totaltotal 75.5% 483 / 639 CC 109 SCAR 24.8
CC
109
Cov
77%
upload
42.8
prepare_body_parameters
20.4
_build_io_fh
20.0
CRAP 11.9
« lib/Catalyst/Script/Server.pm ↑ Summary lib/Catalyst/Action.pm »
1 package Catalyst::Request;
2  
3 167 ▶use Socket qw( getaddrinfo getnameinfo AI_NUMERICHOST NI_NAMEREQD NIx_NOSERV );
Subroutine BEGIN: called CC=1 SCAR=0.0
4 167 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 167 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 167 ▶use URI::http;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 167 ▶use URI::https;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 167 ▶use URI::QueryParam;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 167 ▶use HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 167 ▶use Stream::Buffered;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 167 ▶use Hash::MultiValue;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 167 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 167 ▶use HTTP::Body;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 167 ▶use Catalyst::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 167 ▶use Catalyst::Request::PartData;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 167 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 167 ▶use namespace::clean -except => 'meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 167 with 'MooseX::Emulate::Class::Accessor::Fast';
21  
22 167 has env => (is => 'ro', writer => '_set_env', predicate => '_has_env');
23 # XXX Deprecated crap here - warn?
24 167 has action => (is => 'rw');
25 # XXX: Deprecated in docs ages ago (2006), deprecated with warning in 5.8000 due
26 # to confusion between Engines and Plugin::Authentication. Remove in 5.8100?
27 167 has user => (is => 'rw');
28 0 ▶sub snippets { shift->captures(@_) }
Subroutine snippets: not called CC=1 SCAR=6.9
Pod undocumented
29  
30 167 has _read_position => (
31 # FIXME: work around Moose bug RT#75367
32 # init_arg => undef,
33 is => 'ro',
34 writer => '_set_read_position',
35 default => 0,
36 );
37 has _read_length => (
38 # FIXME: work around Moose bug RT#75367
39 # init_arg => undef,
40 is => 'ro',
41 933 ▶ default => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
42 933 my $self = shift;
43 933 ▶ $self->header('Content-Length') || 0;
Branch: 1 branch
BranchTrueFalseTotal
$self->header("Content-Length") or 0 51 882 933
44 },
45 167 lazy => 1,
46 );
47  
48 167 has address => (is => 'rw');
49 949 ▶has arguments => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
50 167 has cookies => (is => 'ro', builder => 'prepare_cookies', lazy => 1);
51  
52 933 ▶sub prepare_cookies {
Subroutine prepare_cookies: called CC=2 SCAR=6.9
Pod documented
53 933 my ( $self ) = @_;
54  
55 933 ▶ if ( my $header = $self->header('Cookie') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $header = $self->header("Cookie")) 1 932 933
56 1 return { CGI::Simple::Cookie->parse($header) };
57 }
58 932 {};
59 }
60  
61 167 has query_keywords => (is => 'rw');
62 167 has match => (is => 'rw');
63 167 has method => (is => 'rw');
64 167 has protocol => (is => 'rw');
65 0 ▶has query_parameters => (is => 'rw', lazy=>1, default => sub { shift->_use_hash_multivalue ? Hash::MultiValue->new : +{} });
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
(shift())->_use_hash_multivalue ? : 0 0 0
66 167 has secure => (is => 'rw', default => 0);
67 950 ▶has captures => (is => 'rw', default => sub { [] });
Subroutine __ANON__: called CC=1 SCAR=0.0
68 167 has uri => (is => 'rw', predicate => 'has_uri');
69 167 has remote_user => (is => 'rw');
70 167 has headers => (
71 is => 'rw',
72 isa => 'HTTP::Headers',
73 handles => [qw(content_encoding content_length content_type header referer user_agent)],
74 builder => 'prepare_headers',
75 lazy => 1,
76 );
77  
78 933 ▶sub prepare_headers {
Subroutine prepare_headers: called CC=3 SCAR=11.0
Pod documented
79 933 my ($self) = @_;
80  
81 933 my $env = $self->env;
82 933 my $headers = HTTP::Headers->new();
83  
84 933 for my $header (keys %{ $env }) {
85 22487 ▶ next unless $header =~ /^(HTTP|CONTENT|COOKIE)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $header =~ /^(HTTP|CONTENT|COOKIE)/i 20537 1950 22487
86 1950 (my $field = $header) =~ s/^HTTPS?_//;
87 1950 $field =~ tr/_/-/;
88 1950 $headers->header($field => $env->{$header});
89 }
90 933 return $headers;
91 }
92  
93 167 has _log => (
94 is => 'ro',
95 weak_ref => 1,
96 required => 1,
97 );
98  
99 167 has io_fh => (
100 is=>'ro',
101 predicate=>'_has_io_fh',
102 lazy=>1,
103 builder=>'_build_io_fh');
104  
105 0 ▶sub _build_io_fh {
Subroutine _build_io_fh: not called CC=4 SCAR=30.0
106 0 my $self = shift;
107 return $self->env->{'psgix.io'}
108 || (
109 $self->env->{'net.async.http.server.req'} &&
110 0 ▶ $self->env->{'net.async.http.server.req'}->stream) ## Until I can make ioasync cabal see the value of supportin psgix.io (jnap)
Condition0%0/8
$self->env->{'psgix.io'} || $self->env->{'net.async.http.server.req'} && $self->env->{'net.async.http.server.req'}->stream || die('Your Server does not support psgix.io')
l!l
00
$self->env->{"psgix.io"} or $self->env->{"net.async.http.server.req"} and $self->env->{"net.async.http.server.req"}->stream
l!l&&r!l&&!r
000
$self->env->{"net.async.http.server.req"} and $self->env->{"net.async.http.server.req"}->stream
!ll&&!rl&&r
000
MC/DC0%0/3
$self->env->{'psgix.io'} || $self->env->{'net.async.http.server.req'} && $self->env->{'net.async.http.server.req'}->stream || die('Your Server does not support psgix.io')
$self->env->{"psgix.io"} $self->env->{"net.async.http.server.req"} $self->env->{"net.async.http.server.req"}->stream
Truth table0%0/4
A or B and C
A= $self->env->{"psgix.io"}B= $self->env->{"net.async.http.server.req"}C= $self->env->{"net.async.http.server.req"}->stream
ABCresult
1XX1
0111
00X0
0100
111 || die "Your Server does not support psgix.io";
112 };
113  
114 7 ▶has data_handlers => ( is=>'ro', isa=>'HashRef', default=>sub { +{} } );
Subroutine __ANON__: called CC=1 SCAR=0.0
115  
116 167 has body_data => (
117 is=>'ro',
118 lazy=>1,
119 builder=>'_build_body_data');
120  
121 7 ▶sub _build_body_data {
Subroutine _build_body_data: called CC=7 SCAR=21.1
122 7 my ($self) = @_;
123  
124 # Not sure if these returns should not be exceptions...
125 7 ▶ my $content_type = $self->content_type || return;
Condition50%1/2
$self->content_type || (return)
l!l
70
MC/DC0%0/1
$self->content_type || (return)
$self->content_type
Truth table50%1/2
A
A= $self->content_type
Aresult
11
00
126 7 ▶ return unless ($self->method eq 'POST' || $self->method eq 'PUT' || $self->method eq 'PATCH' || $self->method eq 'DELETE');
Branch: 1 branch
BranchTrueFalseTotal
unless $self->method eq "POST" or $self->method eq "PUT" or $self->method eq "PATCH" or $self->method eq "DELETE" 0 7 7
Condition33%3/9
$self->method eq "POST" or $self->method eq "PUT" or $self->method eq "PATCH" or $self->method eq "DELETE"
l!l&&r!l&&!r
700
$self->method eq "POST" or $self->method eq "PUT" or $self->method eq "PATCH"
l!l&&r!l&&!r
700
$self->method eq "POST" or $self->method eq "PUT"
l!l&&r!l&&!r
700
MC/DC0%0/4
$self->method eq "POST" or $self->method eq "PUT" or $self->method eq "PATCH" or $self->method eq "DELETE"
$self->method eq "POST" $self->method eq "PUT" $self->method eq "PATCH" $self->method eq "DELETE"
Truth table20%1/5
A or B or C or D
A= $self->method eq "POST"B= $self->method eq "PUT"C= $self->method eq "PATCH"D= $self->method eq "DELETE"
ABCDresult
1XXX1
01XX1
001X1
00011
00000
127  
128 14 my ($match) = grep { $content_type =~/$_/i }
129 7 keys(%{$self->data_handlers});
130  
131 7 ▶ if($match) {
Branch: 1 branch
BranchTrueFalseTotal
if ($match) { } 6 1 7
132 6 my $fh = $self->body;
133 6 local $_ = $fh;
134 6 return $self->data_handlers->{$match}->($fh, $self);
135 } else {
136 Catalyst::Exception->throw(
137 sprintf '%s does not have an available data handler. Valid data_handlers are %s.',
138 1 $content_type, join ', ', sort keys %{$self->data_handlers}
139 );
140 }
141 }
142  
143 has _use_hash_multivalue => (
144 is=>'ro',
145 required=>1,
146 946 ▶ default=> sub {0});
Subroutine __ANON__: called CC=1 SCAR=0.0
147  
148 # Amount of data to read from input on each pass
149 167 our $CHUNKSIZE = 64 * 1024;
150  
151 112 ▶sub read {
Subroutine read: called CC=6 SCAR=18.0
Pod documented
152 112 my ($self, $maxlength) = @_;
153 112 my $remaining = $self->_read_length - $self->_read_position;
154 112 ▶ $maxlength ||= $CHUNKSIZE;
Condition66%2/3
$maxlength ||= $Catalyst::Request::CHUNKSIZE
l!l&&r!l&&!r
121000
MC/DC0%0/2
$maxlength ||= $Catalyst::Request::CHUNKSIZE
$maxlength $Catalyst::Request::CHUNKSIZE
Truth table66%2/3
A ||= B
A= $maxlengthB= $Catalyst::Request::CHUNKSIZE
ABresult
1X1
011
000
155  
156 # Are we done reading?
157 112 ▶ if ( $remaining <= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining <= 0) 49 63 112
158 49 return;
159 }
160  
161 63 ▶ my $readlen = ( $remaining > $maxlength ) ? $maxlength : $remaining;
Branch: 1 branch
BranchTrueFalseTotal
$remaining > $maxlength ? : 12 51 63
162 63 my $rc = $self->read_chunk( my $buffer, $readlen );
163 63 ▶ if ( defined $rc ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $rc) { } 63 0 63
164 63 ▶ if (0 == $rc) { # Nothing more to read even though Content-Length
Branch: 1 branch
BranchTrueFalseTotal
if (0 == $rc) 1 62 63
165 # said there should be.
166 1 return;
167 }
168 62 $self->_set_read_position( $self->_read_position + $rc );
169 62 return $buffer;
170 }
171 else {
172 0 Catalyst::Exception->throw(
173 message => "Unknown error reading input: $!" );
174 }
175 }
176  
177 63 ▶sub read_chunk {
Subroutine read_chunk: called CC=1 SCAR=0.0
Pod documented
178 63 my $self = shift;
179 63 return $self->env->{'psgi.input'}->read(@_);
180 }
181  
182 167 has body_parameters => (
183 is => 'rw',
184 required => 1,
185 lazy => 1,
186 predicate => 'has_body_parameters',
187 builder => 'prepare_body_parameters',
188 );
189  
190 has uploads => (
191 is => 'rw',
192 required => 1,
193 950 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
194 167 );
195  
196 167 has parameters => (
197 is => 'rw',
198 lazy => 1,
199 builder => '_build_parameters',
200 clearer => '_clear_parameters',
201 );
202  
203 # TODO:
204 # - Can we lose the before modifiers which just call prepare_body ?
205 # they are wasteful, slow us down and feel cluttery.
206  
207 # Can we make _body an attribute, have the rest of
208 # these lazy build from there and kill all the direct hash access
209 # in Catalyst.pm and Engine.pm?
210  
211 0 ▶sub prepare_parameters {
Subroutine prepare_parameters: not called CC=1 SCAR=6.9
Pod documented
212 0 my ( $self ) = @_;
213 0 $self->_clear_parameters;
214 0 return $self->parameters;
215 }
216  
217 931 ▶sub _build_parameters {
Subroutine _build_parameters: called CC=9 SCAR=22.0
218 931 my ( $self ) = @_;
219 931 my $parameters = {};
220 931 my $body_parameters = $self->body_parameters;
221 931 my $query_parameters = $self->query_parameters;
222  
223 931 ▶ if($self->_use_hash_multivalue) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_use_hash_multivalue) 4 927 931
224 4 return Hash::MultiValue->new($query_parameters->flatten, $body_parameters->flatten);
225 }
226  
227 # We copy, no references
228 927 foreach my $name (keys %$query_parameters) {
229 118 my $param = $query_parameters->{$name};
230 118 ▶ $parameters->{$name} = ref $param eq 'ARRAY' ? [ @$param ] : $param;
Branch: 1 branch
BranchTrueFalseTotal
ref $param eq 'ARRAY' ? : 12 106 118
231 }
232  
233 # Merge query and body parameters
234 927 foreach my $name (keys %$body_parameters) {
235 54 my $param = $body_parameters->{$name};
236 54 ▶ my @values = ref $param eq 'ARRAY' ? @$param : ($param);
Branch: 1 branch
BranchTrueFalseTotal
ref $param eq 'ARRAY' ? : 4 50 54
237 54 ▶ if ( my $existing = $parameters->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $existing = $parameters->{$name}) 5 49 54
238 5 ▶ unshift(@values, (ref $existing eq 'ARRAY' ? @$existing : $existing));
Branch: 1 branch
BranchTrueFalseTotal
ref $existing eq 'ARRAY' ? : 2 3 5
239 }
240 54 ▶ $parameters->{$name} = @values > 1 ? \@values : $values[0];
Branch: 1 branch
BranchTrueFalseTotal
@values > 1 ? : 7 47 54
241 }
242 927 $parameters;
243 }
244  
245 167 has _uploadtmp => (
246 is => 'ro',
247 predicate => '_has_uploadtmp',
248 );
249  
250 932 ▶sub prepare_body {
Subroutine prepare_body: called CC=12 SCAR=26.6
Pod documented
251 932 my ( $self ) = @_;
252  
253 # If previously applied middleware created the HTTP::Body object, then we
254 # just use that one.
255  
256 932 ▶ if(my $plack_body = $self->_has_env ? $self->env->{'plack.request.http.body'} : undef) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $plack_body = $self->_has_env ? $self->env->{"plack.request.http.body"} : undef) 0 932 932
$self->_has_env ? : 932 0 932
257 0 $self->_body($plack_body);
258 0 $self->_body->cleanup(1);
259 0 return;
260 }
261  
262 # If there is nothing to read, set body to naught and return. This
263 # will cause all body code to be skipped
264  
265 932 ▶ return $self->_body(0) unless my $length = $self->_read_length;
Branch: 1 branch
BranchTrueFalseTotal
unless my $length = $self->_read_length 883 49 932
266  
267 # Unless the body has already been set, create it. Not sure about this
268 # code, how else might it be set, but this was existing logic.
269  
270 49 ▶ unless ($self->_body) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_body) 49 0 49
271 49 my $type = $self->header('Content-Type');
272 49 $self->_body(HTTP::Body->new( $type, $length ));
273 49 $self->_body->cleanup(1);
274  
275 # JNAP: I'm not sure this is doing what we expect, but it also doesn't
276 # seem to be hurting (seems ->_has_uploadtmp is true more than I would
277 # expect.
278  
279 49 ▶ $self->_body->tmpdir( $self->_uploadtmp )
Branch: 1 branch
BranchTrueFalseTotal
if $self->_has_uploadtmp 1 48 49
280 if $self->_has_uploadtmp;
281 }
282  
283 # Ok if we get this far, we have to read psgi.input into the new body
284 # object. Lets play nice with any plack app or other downstream, so
285 # we create a buffer unless one exists.
286  
287 49 my $stream_buffer;
288 49 ▶ if ($self->env->{'psgix.input.buffered'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->env->{'psgix.input.buffered'}) { } 0 49 49
289 # Be paranoid about previous psgi middleware or apps that read the
290 # input but didn't return the buffer to the start.
291 0 $self->env->{'psgi.input'}->seek(0, 0);
292 } else {
293 49 $stream_buffer = Stream::Buffered->new($length);
294 }
295  
296 # Check for definedness as you could read '0'
297 49 ▶ while ( defined ( my $chunk = $self->read() ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $chunk = $self->read)) 51 49 100
298 51 $self->prepare_body_chunk($chunk);
299 51 ▶ next unless $stream_buffer;
Branch: 1 branch
BranchTrueFalseTotal
unless $stream_buffer 0 51 51
300  
301 51 ▶ $stream_buffer->print($chunk)
Branch: 1 branch
BranchTrueFalseTotal
$stream_buffer->print($chunk) or die sprintf("Failed to write %d bytes to psgi.input file: $!", length $chunk) 51 0 51
302 || die sprintf "Failed to write %d bytes to psgi.input file: $!", length( $chunk );
303 }
304  
305 # Ok, we read the body. Lets play nice for any PSGI app down the pipe
306  
307 49 ▶ if ($stream_buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stream_buffer) { } 49 0 49
308 49 $self->env->{'psgix.input.buffered'} = 1;
309 49 $self->env->{'psgi.input'} = $stream_buffer->rewind;
310 } else {
311 0 $self->env->{'psgi.input'}->seek(0, 0); # Reset the buffer for downstream middleware or apps
312 }
313  
314 # paranoia against wrong Content-Length header
315 49 my $remaining = $length - $self->_read_position;
316 49 ▶ if ( $remaining > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($remaining > 0) 1 48 49
317 1 Catalyst::Exception->throw("Wrong Content-Length value: $length" );
318 }
319 }
320  
321 51 ▶sub prepare_body_chunk {
Subroutine prepare_body_chunk: called CC=1 SCAR=0.0
Pod documented
322 51 my ( $self, $chunk ) = @_;
323  
324 51 $self->_body->add($chunk);
325 }
326  
327 932 ▶sub prepare_body_parameters {
Subroutine prepare_body_parameters: called CC=19 SCAR=30.1
Pod documented
328 932 my ( $self, $c ) = @_;
329 932 ▶ return $self->body_parameters if $self->has_body_parameters;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_body_parameters 2 930 932
330 930 ▶ $self->prepare_body if ! $self->_has_body;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_body 1 929 930
331  
332 930 ▶ unless($self->_body) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_body) 882 48 930
333 882 ▶ my $return = $self->_use_hash_multivalue ? Hash::MultiValue->new : {};
Branch: 1 branch
BranchTrueFalseTotal
$self->_use_hash_multivalue ? : 3 879 882
334 882 $self->body_parameters($return);
335 882 return $return;
336 }
337  
338 48 my $params;
339 48 my %part_data = %{$self->_body->part_data};
340 48 ▶ if(scalar %part_data && !$c->config->{skip_complex_post_part_handling}) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar %part_data and not $c->config->{'skip_complex_post_part_handling'}) { } 11 37 48
Condition66%2/3
scalar %part_data and not $c->config->{"skip_complex_post_part_handling"}
!ll&&!rl&&r
37011
MC/DC50%1/2
scalar %part_data and not $c->config->{"skip_complex_post_part_handling"}
scalar %part_data not $c->config->{"skip_complex_post_part_handling"}
Truth table66%2/3
A and not(B)
A= scalar %part_dataB= not $c->config->{"skip_complex_post_part_handling"}
ABresult
0X0
100
111
341 11 foreach my $key (keys %part_data) {
342 21 my $proto_value = $part_data{$key};
343 21 ▶ my ($val, @extra) = (ref($proto_value)||'') eq 'ARRAY' ? @$proto_value : ($proto_value);
Branch: 1 branch
BranchTrueFalseTotal
(ref $proto_value || '') eq 'ARRAY' ? : 1 20 21
Condition50%1/2
ref $proto_value || ''
l!l
210
MC/DC0%0/1
ref $proto_value || ''
ref $proto_value
Truth table50%1/2
A
A= ref $proto_value
Aresult
11
00
344  
345 $key = $c->_handle_param_unicode_decoding($key)
346 21 ▶ if ($c and $c->encoding and !$c->config->{skip_body_param_unicode_decoding});
Branch: 1 branch
BranchTrueFalseTotal
if $c and $c->encoding and not $c->config->{"skip_body_param_unicode_decoding"} 21 0 21
Condition33%2/6
$c and $c->encoding and not $c->config->{"skip_body_param_unicode_decoding"}
!ll&&!rl&&r
0021
$c and $c->encoding
!ll&&!rl&&r
0021
MC/DC0%0/3
$c and $c->encoding and not $c->config->{"skip_body_param_unicode_decoding"}
$c $c->encoding not $c->config->{"skip_body_param_unicode_decoding"}
Truth table25%1/4
A and B and not(C)
A= $cB= $c->encodingC= not $c->config->{"skip_body_param_unicode_decoding"}
ABCresult
0XX0
10X0
1100
1111
347  
348 21 ▶ if(@extra) {
Branch: 1 branch
BranchTrueFalseTotal
if (@extra) { } 1 20 21
349 1 $params->{$key} = [map { Catalyst::Request::PartData->build_from_part_data($c, $_) } ($val,@extra)];
350 } else {
351 20 $params->{$key} = Catalyst::Request::PartData->build_from_part_data($c, $val);
352 }
353 }
354 } else {
355 37 $params = $self->_body->param;
356  
357 # If we have an encoding configured (like UTF-8) in general we expect a client
358 # to POST with the encoding we fufilled the request in. Otherwise don't do any
359 # encoding (good change wide chars could be in HTML entity style llike the old
360 # days -JNAP
361  
362 # so, now that HTTP::Body prepared the body params, we gotta 'walk' the structure
363 # and do any needed decoding.
364  
365 # This only does something if the encoding is set via the encoding param. Remember
366 # this is assuming the client is not bad and responds with what you provided. In
367 # general you can just use utf8 and get away with it.
368 #
369 # I need to see if $c is here since this also doubles as a builder for the object :(
370  
371 37 ▶ if($c and $c->encoding and !$c->config->{skip_body_param_unicode_decoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c and $c->encoding and not $c->config->{"skip_body_param_unicode_decoding"}) 36 1 37
Condition66%4/6
$c and $c->encoding and not $c->config->{"skip_body_param_unicode_decoding"}
!ll&&!rl&&r
1036
$c and $c->encoding
!ll&&!rl&&r
1036
MC/DC33%1/3
$c and $c->encoding and not $c->config->{"skip_body_param_unicode_decoding"}
$c $c->encoding not $c->config->{"skip_body_param_unicode_decoding"}
Truth table50%2/4
A and B and not(C)
A= $cB= $c->encodingC= not $c->config->{"skip_body_param_unicode_decoding"}
ABCresult
0XX0
10X0
1100
1111
372 36 $params = $c->_handle_unicode_decoding($params);
373 }
374 }
375  
376 47 ▶ my $return = $self->_use_hash_multivalue ?
Branch: 1 branch
BranchTrueFalseTotal
$self->_use_hash_multivalue ? : 1 46 47
377 Hash::MultiValue->from_mixed($params) :
378 $params;
379  
380 47 ▶ $self->body_parameters($return) unless $self->has_body_parameters;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_body_parameters 47 0 47
381 47 return $return;
382 }
383  
384 934 ▶sub prepare_connection {
Subroutine prepare_connection: called CC=3 SCAR=11.0
Pod documented
385 934 my ($self) = @_;
386  
387 934 my $env = $self->env;
388  
389 934 $self->address( $env->{REMOTE_ADDR} );
390 $self->hostname( $env->{REMOTE_HOST} )
391 934 ▶ if exists $env->{REMOTE_HOST};
Branch: 1 branch
BranchTrueFalseTotal
if exists $env->{"REMOTE_HOST"} 933 1 934
392 934 $self->protocol( $env->{SERVER_PROTOCOL} );
393 934 $self->remote_user( $env->{REMOTE_USER} );
394 934 $self->method( $env->{REQUEST_METHOD} );
395 934 ▶ $self->secure( $env->{'psgi.url_scheme'} eq 'https' ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$env->{'psgi.url_scheme'} eq 'https' ? : 7 927 934
396 }
397  
398 # XXX - FIXME - method is here now, move this crap...
399 1069 ▶around parameters => sub {
Subroutine __ANON__: called CC=3 SCAR=11.6
400 1069 my ($orig, $self, $params) = @_;
401 1069 ▶ if ($params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params) 1 1068 1069
402 1 ▶ if ( !ref $params ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $params) 0 1 1
403 0 $self->_log->warn(
404 "Attempt to retrieve '$params' with req->params(), " .
405 "you probably meant to call req->param('$params')"
406 );
407 0 $params = undef;
408 }
409 1 return $self->$orig($params);
410 }
411 1068 $self->$orig();
412 167 };
413  
414 has base => (
415 is => 'rw',
416 required => 1,
417 lazy => 1,
418 0 ▶ default => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
419 0 my $self = shift;
420 0 ▶ return $self->path if $self->has_uri;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_uri 0 0 0
421 },
422 167 );
423  
424 167 has _body => (
425 is => 'rw', clearer => '_clear_body', predicate => '_has_body',
426 );
427 # Eugh, ugly. Should just be able to rename accessor methods to 'body'
428 # and provide a custom reader..
429 635 ▶sub body {
Subroutine body: called CC=4 SCAR=14.2
Pod documented
430 635 my $self = shift;
431 635 ▶ $self->prepare_body unless $self->_has_body;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_has_body 0 635 635
432 635 ▶ croak 'body is a reader' if scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 635 635
433 635 ▶ return blessed $self->_body ? $self->_body->body : $self->_body;
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self->_body) ? : 29 606 635
434 }
435  
436 has hostname => (
437 is => 'rw',
438 lazy => 1,
439 0 ▶ default => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
440 0 my ($self) = @_;
441 0 my ( $err, $sockaddr ) = getaddrinfo(
442 $self->address,
443 # no service
444 '',
445 { flags => AI_NUMERICHOST }
446 );
447 0 ▶ if ( $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
448 0 $self->_log->warn("resolve of hostname failed: $err");
449 0 return $self->address;
450 }
451 ( $err, my $hostname ) = getnameinfo(
452 $sockaddr->{addr},
453 0 NI_NAMEREQD,
454 # we are only interested in the hostname, not the servicename
455 NIx_NOSERV
456 );
457 0 ▶ if ( $err ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
458 0 $self->_log->warn("resolve of hostname failed: $err");
459 0 return $self->address;
460 }
461 0 return $hostname;
462 },
463 167 );
464  
465 167 has _path => ( is => 'rw', predicate => '_has_path', clearer => '_clear_path' );
466  
467 15143 ▶sub args { shift->arguments(@_) }
Subroutine args: called CC=1 SCAR=0.0
Pod documented
468 9 ▶sub body_params { shift->body_parameters(@_) }
Subroutine body_params: called CC=1 SCAR=0.0
Pod documented
469 0 ▶sub input { shift->body(@_) }
Subroutine input: not called CC=1 SCAR=6.9
Pod documented
470 25 ▶sub params { shift->parameters(@_) }
Subroutine params: called CC=1 SCAR=0.0
Pod documented
471 7 ▶sub query_params { shift->query_parameters(@_) }
Subroutine query_params: called CC=1 SCAR=0.0
Pod documented
472 0 ▶sub path_info { shift->path(@_) }
Subroutine path_info: not called CC=1 SCAR=6.9
Pod documented
473  
474 =for stopwords param params
475  
476 =head1 NAME
477  
478 Catalyst::Request - provides information about the current client request
479  
480 =head1 SYNOPSIS
481  
482 $req = $c->request;
483 $req->address eq "127.0.0.1";
484 $req->arguments;
485 $req->args;
486 $req->base;
487 $req->body;
488 $req->body_data;
489 $req->body_parameters;
490 $req->content_encoding;
491 $req->content_length;
492 $req->content_type;
493 $req->cookie;
494 $req->cookies;
495 $req->header;
496 $req->headers;
497 $req->hostname;
498 $req->input;
499 $req->query_keywords;
500 $req->match;
501 $req->method;
502 $req->param;
503 $req->parameters;
504 $req->params;
505 $req->path;
506 $req->protocol;
507 $req->query_parameters;
508 $req->read;
509 $req->referer;
510 $req->secure;
511 $req->captures;
512 $req->upload;
513 $req->uploads;
514 $req->uri;
515 $req->user;
516 $req->user_agent;
517 $req->env;
518  
519 See also L<Catalyst>, L<Catalyst::Request::Upload>.
520  
521 =head1 DESCRIPTION
522  
523 This is the Catalyst Request class, which provides an interface to data for the
524 current client request. The request object is prepared by L<Catalyst::Engine>,
525 thus hiding the details of the particular engine implementation.
526  
527 =head1 METHODS
528  
529 =head2 $req->address
530  
531 Returns the IP address of the client.
532  
533 =head2 $req->arguments
534  
535 Returns a reference to an array containing the arguments.
536  
537 print $c->request->arguments->[0];
538  
539 For example, if your action was
540  
541 package MyApp::Controller::Foo;
542  
543 sub moose : Local {
544 ...
545 }
546  
547 and the URI for the request was C<http://.../foo/moose/bah>, the string C<bah>
548 would be the first and only argument.
549  
550 Arguments get automatically URI-unescaped for you.
551  
552 =head2 $req->args
553  
554 Shortcut for L</arguments>.
555  
556 =head2 $req->base
557  
558 Contains the URI base. This will always have a trailing slash. Note that the
559 URI scheme (e.g., http vs. https) must be determined through heuristics;
560 depending on your server configuration, it may be incorrect. See $req->secure
561 for more info.
562  
563 If your application was queried with the URI
564 C<http://localhost:3000/some/path> then C<base> is C<http://localhost:3000/>.
565  
566 =head2 $req->body
567  
568 Returns the message body of the request, as returned by L<HTTP::Body>: a string,
569 unless Content-Type is C<application/x-www-form-urlencoded>, C<text/xml>, or
570 C<multipart/form-data>, in which case a L<File::Temp> object is returned.
571  
572 =head2 $req->body_data
573  
574 Returns a Perl representation of body data that is not classic HTML
575 form data, such as JSON, XML, etc. By default, Catalyst will parse incoming
576 data of the type 'application/json' for POST, PUT, PATCH or DELETE methods,
577 and return access to that data via this method.
578  
579 You may define addition data_handlers via a global configuration
580 setting. See L<Catalyst\DATA HANDLERS> for more information.
581  
582 If the body is malformed in some way (such as undefined or not content that
583 matches the content-type) we raise a L<Catalyst::Exception> with the error
584 text as the message.
585  
586 If the body content type does not match an available data handler, this
587 will also raise an exception.
588  
589 =head2 $req->body_parameters
590  
591 Returns a reference to a hash containing body (POST) parameters. Values can
592 be either a scalar or an arrayref containing scalars.
593  
594 print $c->request->body_parameters->{field};
595 print $c->request->body_parameters->{field}->[0];
596  
597 These are the parameters from the POST part of the request, if any.
598  
599 B<NOTE> If your POST is multipart, but contains non file upload parts (such
600 as an line part with an alternative encoding or content type) we do our best to
601 try and figure out how the value should be presented. If there's a specified character
602 set we will use that to decode rather than the default encoding set by the application.
603 However if there are complex headers and we cannot determine
604 the correct way to extra a meaningful value from the upload, in this case any
605 part like this will be represented as an instance of L<Catalyst::Request::PartData>.
606  
607 Patches and review of this part of the code welcomed.
608  
609 =head2 $req->body_params
610  
611 Shortcut for body_parameters.
612  
613 =head2 $req->content_encoding
614  
615 Shortcut for $req->headers->content_encoding.
616  
617 =head2 $req->content_length
618  
619 Shortcut for $req->headers->content_length.
620  
621 =head2 $req->content_type
622  
623 Shortcut for $req->headers->content_type.
624  
625 =head2 $req->cookie
626  
627 A convenient method to access $req->cookies.
628  
629 $cookie = $c->request->cookie('name');
630 @cookies = $c->request->cookie;
631  
632 =cut
633  
634 0 ▶sub cookie {
Subroutine cookie: not called CC=4 SCAR=30.0
Pod documented
635 0 my $self = shift;
636  
637 0 ▶ if ( @_ == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 0 0 0
638 0 return keys %{ $self->cookies };
639 }
640  
641 0 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 0 0
642  
643 0 my $name = shift;
644  
645 0 ▶ unless ( exists $self->cookies->{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->cookies->{$name}) 0 0 0
646 0 return undef;
647 }
648  
649 0 return $self->cookies->{$name};
650 }
651 }
652  
653 =head2 $req->cookies
654  
655 Returns a reference to a hash containing the cookies.
656  
657 print $c->request->cookies->{mycookie}->value;
658  
659 The cookies in the hash are indexed by name, and the values are L<CGI::Simple::Cookie>
660 objects.
661  
662 =head2 $req->header
663  
664 Shortcut for $req->headers->header.
665  
666 =head2 $req->headers
667  
668 Returns an L<HTTP::Headers> object containing the headers for the current request.
669  
670 print $c->request->headers->header('X-Catalyst');
671  
672 =head2 $req->hostname
673  
674 Returns the hostname of the client. Use C<< $req->uri->host >> to get the hostname of the server.
675  
676 =head2 $req->input
677  
678 Alias for $req->body.
679  
680 =head2 $req->query_keywords
681  
682 Contains the keywords portion of a query string, when no '=' signs are
683 present.
684  
685 http://localhost/path?some+keywords
686  
687 $c->request->query_keywords will contain 'some keywords'
688  
689 =head2 $req->match
690  
691 This contains the matching part of a Regex action. Otherwise
692 it returns the same as 'action', except for default actions,
693 which return an empty string.
694  
695 =head2 $req->method
696  
697 Contains the request method (C<GET>, C<POST>, C<HEAD>, etc).
698  
699 =head2 $req->param
700  
701 Returns GET and POST parameters with a CGI.pm-compatible param method. This
702 is an alternative method for accessing parameters in $c->req->parameters.
703  
704 $value = $c->request->param( 'foo' );
705 @values = $c->request->param( 'foo' );
706 @params = $c->request->param;
707  
708 Like L<CGI>, and B<unlike> earlier versions of Catalyst, passing multiple
709 arguments to this method, like this:
710  
711 $c->request->param( 'foo', 'bar', 'gorch', 'quxx' );
712  
713 will set the parameter C<foo> to the multiple values C<bar>, C<gorch> and
714 C<quxx>. Previously this would have added C<bar> as another value to C<foo>
715 (creating it if it didn't exist before), and C<quxx> as another value for
716 C<gorch>.
717  
718 B<NOTE> this is considered a legacy interface and care should be taken when
719 using it. C<< scalar $c->req->param( 'foo' ) >> will return only the first
720 C<foo> param even if multiple are present; C<< $c->req->param( 'foo' ) >> will
721 return a list of as many are present, which can have unexpected consequences
722 when writing code of the form:
723  
724 $foo->bar(
725 a => 'b',
726 baz => $c->req->param( 'baz' ),
727 );
728  
729 If multiple C<baz> parameters are provided this code might corrupt data or
730 cause a hash initialization error. For a more straightforward interface see
731 C<< $c->req->parameters >>.
732  
733 B<NOTE> Interfaces like this, which are based on L<CGI> and the C<param> method
734 are known to cause demonstrated exploits. It is highly recommended that you
735 avoid using this method, and migrate existing code away from it. Here's a
736 whitepaper of the exploit:
737  
738 L<http://blog.gerv.net/2014/10/new-class-of-vulnerability-in-perl-web-applications/>
739  
740 B<NOTE> Further discussion on IRC indicate that the L<Catalyst> core team from 'back then'
741 were well aware of this hack and this is the main reason we added the new approach to
742 getting parameters in the first place.
743  
744 Basically this is an exploit that takes advantage of how L<\param> will do one thing
745 in scalar context and another thing in list context. This is combined with how Perl
746 chooses to deal with duplicate keys in a hash definition by overwriting the value of
747 existing keys with a new value if the same key shows up again. Generally you will be
748 vulnerable to this exploit if you are using this method in a direct assignment in a
749 hash, such as with a L<DBIx::Class> create statement. For example, if you have
750 parameters like:
751  
752 user?user=123&foo=a&foo=user&foo=456
753  
754 You could end up with extra parameters injected into your method calls:
755  
756 $c->model('User')->create({
757 user => $c->req->param('user'),
758 foo => $c->req->param('foo'),
759 });
760  
761 Which would look like:
762  
763 $c->model('User')->create({
764 user => 123,
765 foo => qw(a user 456),
766 });
767  
768 (or to be absolutely clear if you are not seeing it):
769  
770 $c->model('User')->create({
771 user => 456,
772 foo => 'a',
773 });
774  
775 Possible remediations include scrubbing your parameters with a form validator like
776 L<HTML::FormHandler> or being careful to force scalar context using the scalar
777 keyword:
778  
779 $c->model('User')->create({
780 user => scalar($c->req->param('user')),
781 foo => scalar($c->req->param('foo')),
782 });
783  
784 Upcoming versions of L<Catalyst> will disable this interface by default and require
785 you to positively enable it should you require it for backwards compatibility reasons.
786  
787 =cut
788  
789 14 ▶sub param {
Subroutine param: called CC=11 SCAR=24.4
Pod documented
790 14 my $self = shift;
791  
792 14 ▶ if ( @_ == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 0 14 14
793 0 return keys %{ $self->parameters };
794 }
795  
796 # If anything in @_ is undef, carp about that, and remove it from
797 # the list;
798  
799 14 ▶ my @params = grep { defined($_) ? 1 : do {carp "You called ->params with an undefined value"; 0} } @_;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 16 1 17
800  
801 14 ▶ if ( @params == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@params == 1) { } 12 2 14
elsif (@params > 1) { } 1 1 2
802  
803 12 ▶ defined(my $param = shift @params) ||
Branch: 1 branch
BranchTrueFalseTotal
defined(my $param = shift @params) or carp("You called ->params with an undefined value 2") 12 0 12
804 carp "You called ->params with an undefined value 2";
805  
806 12 ▶ unless ( exists $self->parameters->{$param} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->parameters->{$param}) 2 10 12
807 2 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 2 2
808 }
809  
810 10 ▶ if ( ref $self->parameters->{$param} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->parameters->{$param} eq 'ARRAY') { } 1 9 10
811 return (wantarray)
812 1 ? @{ $self->parameters->{$param} }
813 1 ▶ : $self->parameters->{$param}->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
814 }
815 else {
816 return (wantarray)
817 ? ( $self->parameters->{$param} )
818 9 ▶ : $self->parameters->{$param};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 8 9
819 }
820 }
821 elsif ( @params > 1 ) {
822 1 my $field = shift @params;
823 1 $self->parameters->{$field} = [@params];
824 }
825 }
826  
827 =head2 $req->parameters
828  
829 Returns a reference to a hash containing GET and POST parameters. Values can
830 be either a scalar or an arrayref containing scalars.
831  
832 print $c->request->parameters->{field};
833 print $c->request->parameters->{field}->[0];
834  
835 This is the combination of C<query_parameters> and C<body_parameters>.
836  
837 =head2 $req->params
838  
839 Shortcut for $req->parameters.
840  
841 =head2 $req->path
842  
843 Returns the path, i.e. the part of the URI after $req->base, for the current request.
844  
845 http://localhost/path/foo
846  
847 $c->request->path will contain 'path/foo'
848  
849 =head2 $req->path_info
850  
851 Alias for path, added for compatibility with L<CGI>.
852  
853 =cut
854  
855 2007 ▶sub path {
Subroutine path: called CC=3 SCAR=11.0
Pod documented
856 2007 my ( $self, @params ) = @_;
857  
858 2007 ▶ if (@params) {
Branch: 2 branches
BranchTrueFalseTotal
if (@params) { } 1 2006 2007
elsif ($self->_has_path) { } 1071 935 2006
859 1 $self->uri->path(@params);
860 1 $self->_clear_path;
861 }
862 elsif ( $self->_has_path ) {
863 1071 return $self->_path;
864 }
865 else {
866 935 my $path = $self->uri->path;
867 935 my $location = $self->base->path;
868 935 $path =~ s/^(\Q$location\E)?//;
869 935 $path =~ s/^\///;
870 935 $self->_path($path);
871  
872 935 return $path;
873 }
874 }
875  
876 =head2 $req->protocol
877  
878 Returns the protocol (HTTP/1.0 or HTTP/1.1) used for the current request.
879  
880 =head2 $req->query_parameters
881  
882 =head2 $req->query_params
883  
884 Returns a reference to a hash containing query string (GET) parameters. Values can
885 be either a scalar or an arrayref containing scalars.
886  
887 print $c->request->query_parameters->{field};
888 print $c->request->query_parameters->{field}->[0];
889  
890 =head2 $req->read( [$maxlength] )
891  
892 Reads a chunk of data from the request body. This method is intended to be
893 used in a while loop, reading $maxlength bytes on every call. $maxlength
894 defaults to the size of the request if not specified.
895  
896 =head2 $req->read_chunk(\$buff, $max)
897  
898 Reads a chunk.
899  
900 You have to set MyApp->config(parse_on_demand => 1) to use this directly.
901  
902 =head2 $req->referer
903  
904 Shortcut for $req->headers->referer. Returns the referring page.
905  
906 =head2 $req->secure
907  
908 Returns true or false, indicating whether the connection is secure
909 (https). The reliability of $req->secure may depend on your server
910 configuration; Catalyst relies on PSGI to determine whether or not a
911 request is secure (Catalyst looks at psgi.url_scheme), and different
912 PSGI servers may make this determination in different ways (as by
913 directly passing along information from the server, interpreting any of
914 several HTTP headers, or using heuristics of their own).
915  
916 =head2 $req->captures
917  
918 Returns a reference to an array containing captured args from chained
919 actions or regex captures.
920  
921 my @captures = @{ $c->request->captures };
922  
923 =head2 $req->upload
924  
925 A convenient method to access $req->uploads.
926  
927 $upload = $c->request->upload('field');
928 @uploads = $c->request->upload('field');
929 @fields = $c->request->upload;
930  
931 for my $upload ( $c->request->upload('field') ) {
932 print $upload->filename;
933 }
934  
935 =cut
936  
937 8 ▶sub upload {
Subroutine upload: called CC=13 SCAR=37.6
Pod documented
938 8 my $self = shift;
939  
940 8 ▶ if ( @_ == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) 1 7 8
941 1 return keys %{ $self->uploads };
942 }
943  
944 7 ▶ if ( @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 7 0 7
945  
946 7 my $upload = shift;
947  
948 7 ▶ unless ( exists $self->uploads->{$upload} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->uploads->{$upload}) 1 6 7
949 1 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
950 }
951  
952 6 ▶ if ( ref $self->uploads->{$upload} eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->uploads->{$upload} eq 'ARRAY') { } 0 6 6
953 return (wantarray)
954 0 ? @{ $self->uploads->{$upload} }
955 0 ▶ : $self->uploads->{$upload}->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
956 }
957 else {
958 return (wantarray)
959 ? ( $self->uploads->{$upload} )
960 6 ▶ : $self->uploads->{$upload};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 6 6
961 }
962 }
963  
964 0 ▶ if ( @_ > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 1) 0 0 0
965  
966 0 ▶ while ( my ( $field, $upload ) = splice( @_, 0, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
967  
968 0 ▶ if ( exists $self->uploads->{$field} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->uploads->{$field}) { } 0 0 0
969 0 for ( $self->uploads->{$field} ) {
970 0 ▶ $_ = [$_] unless ref($_) eq "ARRAY";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_ eq "ARRAY" 0 0 0
971 0 push( @$_, $upload );
972 }
973 }
974 else {
975 0 $self->uploads->{$field} = $upload;
976 }
977 }
978 }
979 }
980  
981 =head2 $req->uploads
982  
983 Returns a reference to a hash containing uploads. Values can be either a
984 L<Catalyst::Request::Upload> object, or an arrayref of
985 L<Catalyst::Request::Upload> objects.
986  
987 my $upload = $c->request->uploads->{field};
988 my $upload = $c->request->uploads->{field}->[0];
989  
990 =head2 $req->uri
991  
992 Returns a L<URI> object for the current request. Stringifies to the URI text.
993  
994 =head2 $req->mangle_params( { key => 'value' }, $appendmode);
995  
996 Returns a hashref of parameters stemming from the current request's params,
997 plus the ones supplied. Keys for which no current param exists will be
998 added, keys with undefined values will be removed and keys with existing
999 params will be replaced. Note that you can supply a true value as the final
1000 argument to change behavior with regards to existing parameters, appending
1001 values rather than replacing them.
1002  
1003 A quick example:
1004  
1005 # URI query params foo=1
1006 my $hashref = $req->mangle_params({ foo => 2 });
1007 # Result is query params of foo=2
1008  
1009 versus append mode:
1010  
1011 # URI query params foo=1
1012 my $hashref = $req->mangle_params({ foo => 2 }, 1);
1013 # Result is query params of foo=1&foo=2
1014  
1015 This is the code behind C<uri_with>.
1016  
1017 =cut
1018  
1019 22 ▶sub mangle_params {
Subroutine mangle_params: called CC=12 SCAR=25.0
Pod documented
1020 22 my ($self, $args, $append) = @_;
1021  
1022 22 ▶ carp('No arguments passed to mangle_params()') unless $args;
Branch: 1 branch
BranchTrueFalseTotal
unless $args 0 22 22
1023  
1024 22 foreach my $value ( values %$args ) {
1025 26 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 10 16 26
1026 16 ▶ for ( ref $value eq 'ARRAY' ? @$value : $value ) {
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq 'ARRAY' ? : 2 14 16
1027 18 $_ = "$_";
1028 # utf8::encode($_);
1029 }
1030 };
1031  
1032 22 my %params = %{ $self->uri->query_form_hash };
1033 22 foreach my $key (keys %{ $args }) {
1034 26 my $val = $args->{$key};
1035 26 ▶ if(defined($val)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 16 10 26
1036  
1037 16 ▶ if($append && exists($params{$key})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($append and exists $params{$key}) { } 2 14 16
Condition66%2/3
$append and exists $params{$key}
!ll&&!rl&&r
1402
MC/DC50%1/2
$append and exists $params{$key}
$append exists $params{$key}
Truth table66%2/3
A and B
A= $appendB= exists $params{$key}
ABresult
0X0
100
111
1038  
1039 # This little bit of heaven handles appending a new value onto
1040 # an existing one regardless if the existing value is an array
1041 # or not, and regardless if the new value is an array or not
1042 $params{$key} = [
1043 0 ▶ ref($params{$key}) eq 'ARRAY' ? @{ $params{$key} } : $params{$key},
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 1 1 2
1044 2 ▶ ref($val) eq 'ARRAY' ? @{ $val } : $val
Branch: 1 branch
BranchTrueFalseTotal
ref $params{$key} eq 'ARRAY' ? : 0 2 2
1045 ];
1046  
1047 } else {
1048 14 $params{$key} = $val;
1049 }
1050 } else {
1051  
1052 # If the param wasn't defined then we delete it.
1053 10 delete($params{$key});
1054 }
1055 }
1056  
1057  
1058 22 return \%params;
1059 }
1060  
1061 =head2 $req->uri_with( { key => 'value' } );
1062  
1063 Returns a rewritten URI object for the current request. Key/value pairs
1064 passed in will override existing parameters. You can remove an existing
1065 parameter by passing in an undef value. Unmodified pairs will be
1066 preserved.
1067  
1068 You may also pass an optional second parameter that puts C<uri_with> into
1069 append mode:
1070  
1071 $req->uri_with( { key => 'value' }, { mode => 'append' } );
1072  
1073 See C<mangle_params> for an explanation of this behavior.
1074  
1075 =cut
1076  
1077 22 ▶sub uri_with {
Subroutine uri_with: called CC=5 SCAR=16.3
Pod documented
1078 22 my( $self, $args, $behavior) = @_;
1079  
1080 22 ▶ carp( 'No arguments passed to uri_with()' ) unless $args;
Branch: 1 branch
BranchTrueFalseTotal
unless $args 0 22 22
1081  
1082 22 my $append = 0;
1083 22 ▶ if((ref($behavior) eq 'HASH') && defined($behavior->{mode}) && ($behavior->{mode} eq 'append')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $behavior eq "HASH" and defined $behavior->{"mode"} and $behavior->{"mode"} eq "append") 2 20 22
Condition66%4/6
ref $behavior eq "HASH" and defined $behavior->{"mode"} and $behavior->{"mode"} eq "append"
!ll&&!rl&&r
2002
ref $behavior eq "HASH" and defined $behavior->{"mode"}
!ll&&!rl&&r
2002
MC/DC33%1/3
ref $behavior eq "HASH" and defined $behavior->{"mode"} and $behavior->{"mode"} eq "append"
ref $behavior eq "HASH" defined $behavior->{"mode"} $behavior->{"mode"} eq "append"
Truth table50%2/4
A and B and C
A= ref $behavior eq "HASH"B= defined $behavior->{"mode"}C= $behavior->{"mode"} eq "append"
ABCresult
0XX0
10X0
1100
1111
1084 2 $append = 1;
1085 }
1086  
1087 22 my $params = $self->mangle_params($args, $append);
1088  
1089 22 my $uri = $self->uri->clone;
1090 22 $uri->query_form($params);
1091  
1092 22 return $uri;
1093 }
1094  
1095 =head2 $req->remote_user
1096  
1097 Returns the value of the C<REMOTE_USER> environment variable.
1098  
1099 =head2 $req->user_agent
1100  
1101 Shortcut to $req->headers->user_agent. Returns the user agent (browser)
1102 version string.
1103  
1104 =head2 $req->io_fh
1105  
1106 Returns a psgix.io bidirectional socket, if your server supports one. Used for
1107 when you want to jailbreak out of PSGI and handle bidirectional client server
1108 communication manually, such as when you are using cometd or websockets.
1109  
1110 =head1 SETUP METHODS
1111  
1112 You should never need to call these yourself in application code,
1113 however they are useful if extending Catalyst by applying a request role.
1114  
1115 =head2 $self->prepare_headers()
1116  
1117 Sets up the C<< $res->headers >> accessor.
1118  
1119 =head2 $self->prepare_body()
1120  
1121 Sets up the body using L<HTTP::Body>
1122  
1123 =head2 $self->prepare_body_chunk()
1124  
1125 Add a chunk to the request body.
1126  
1127 =head2 $self->prepare_body_parameters()
1128  
1129 Sets up parameters from body.
1130  
1131 =head2 $self->prepare_cookies()
1132  
1133 Parse cookies from header. Sets up a L<CGI::Simple::Cookie> object.
1134  
1135 =head2 $self->prepare_connection()
1136  
1137 Sets up various fields in the request like the local and remote addresses,
1138 request method, hostname requested etc.
1139  
1140 =head2 $self->prepare_parameters()
1141  
1142 Ensures that the body has been parsed, then builds the parameters, which are
1143 combined from those in the request and those in the body.
1144  
1145 If parameters have already been set will clear the parameters and build them again.
1146  
1147 =head2 $self->env
1148  
1149 Access to the raw PSGI env.
1150  
1151 =head2 meta
1152  
1153 Provided by Moose
1154  
1155 =head1 AUTHORS
1156  
1157 Catalyst Contributors, see Catalyst.pm
1158  
1159 =head1 COPYRIGHT
1160  
1161 This library is free software. You can redistribute it and/or modify
1162 it under the same terms as Perl itself.
1163  
1164 =cut
1165  
1166 167 __PACKAGE__->meta->make_immutable;
1167  
1168 167 1;
« lib/Catalyst/Script/Server.pm ↑ Summary lib/Catalyst/Action.pm »