Request.pm

filter: Statementstmt 94.3% 218 / 231 Branchbran 82.7% 48 / 58 Conditioncond 91.3% 21 / 23 MC/DCmcdc 84.6% 11 / 13 Subroutinesub 94.8% 37 / 39 Podpod 92.8% 13 / 14 totaltotal 92.0% 348 / 378 CC 47 SCAR 13.6
CC
47
Cov
92%
__ANON__
11.3
path
6.0
__ANON__
5.0
CRAP 3.9
« Response.pm ↑ Summary Request/Types.pm »
1 package Web::Request;
2 24 our $AUTHORITY = 'cpan:PTC';
3 24 $Web::Request::VERSION = '0.12';
4 24 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # ABSTRACT: common request class for web frameworks
6  
7 24 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 24 ▶use HTTP::Body ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 24 ▶use HTTP::Headers ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 24 ▶use HTTP::Message::PSGI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 24 ▶use Module::Runtime ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12 24 ▶use Stream::Buffered ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13 24 ▶use URI ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 24 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 24 has env => (
18 traits => ['Hash'],
19 is => 'ro',
20 isa => 'HashRef',
21 required => 1,
22 handles => {
23 address => [ get => 'REMOTE_ADDR' ],
24 remote_host => [ get => 'REMOTE_HOST' ],
25 protocol => [ get => 'SERVER_PROTOCOL' ],
26 method => [ get => 'REQUEST_METHOD' ],
27 port => [ get => 'SERVER_PORT' ],
28 request_uri => [ get => 'REQUEST_URI' ],
29 path_info => [ get => 'PATH_INFO' ],
30 script_name => [ get => 'SCRIPT_NAME' ],
31 scheme => [ get => 'psgi.url_scheme' ],
32 _input => [ get => 'psgi.input' ],
33 content_length => [ get => 'CONTENT_LENGTH' ],
34 content_type => [ get => 'CONTENT_TYPE' ],
35 session => [ get => 'psgix.session' ],
36 session_options => [ get => 'psgix.session.options' ],
37 logger => [ get => 'psgix.logger' ],
38 },
39 );
40  
41 has _base_uri => (
42 is => 'ro',
43 isa => 'Str',
44 lazy => 1,
45 35 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
46 35 my $self = shift;
47  
48 35 my $env = $self->env;
49  
50 35 ▶ my $scheme = $self->scheme || "http";
Condition100%2/2
$self->scheme || 'http'
l!l
1619
MC/DC100%1/1
$self->scheme || 'http'
$self->scheme
Truth table100%2/2
A
A= $self->scheme
Aresult
11
00
51 35 my $server = $self->host;
52 35 ▶ my $path = $self->script_name || '/';
Condition100%2/2
$self->script_name || '/'
l!l
1124
MC/DC100%1/1
$self->script_name || '/'
$self->script_name
Truth table100%2/2
A
A= $self->script_name
Aresult
11
00
53  
54 35 return "${scheme}://${server}${path}";
55 },
56 24 );
57  
58 has base_uri => (
59 is => 'ro',
60 isa => 'URI',
61 lazy => 1,
62 9 ▶ default => sub { URI->new(shift->_base_uri)->canonical },
Subroutine __ANON__: called CC=1 SCAR=0.0
63 24 );
64  
65 has uri => (
66 is => 'ro',
67 isa => 'URI',
68 lazy => 1,
69 26 ▶ default => sub {
Subroutine __ANON__: called CC=5 SCAR=16.1
70 26 my $self = shift;
71  
72 26 my $base = $self->_base_uri;
73  
74 # We have to escape back PATH_INFO in case they include stuff
75 # like ? or # so that the URI parser won't be tricked. However
76 # we should preserve '/' since encoding them into %2f doesn't
77 # make sense. This means when a request like /foo%2fbar comes
78 # in, we recognize it as /foo/bar which is not ideal, but that's
79 # how the PSGI PATH_INFO spec goes and we can't do anything
80 # about it. See PSGI::FAQ for details.
81 26 my $path_escape_class = q{^/;:@&=A-Za-z0-9\$_.+!*'(),-};
82  
83 26 ▶ my $path = URI::Escape::uri_escape(
Condition100%2/2
$self->path_info || ''
l!l
1016
MC/DC100%1/1
$self->path_info || ''
$self->path_info
Truth table100%2/2
A
A= $self->path_info
Aresult
11
00
84 $self->path_info || '',
85 $path_escape_class
86 );
87 $path .= '?' . $self->env->{QUERY_STRING}
88 if defined $self->env->{QUERY_STRING}
89 26 ▶ && $self->env->{QUERY_STRING} ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->env->{"QUERY_STRING"} and $self->env->{"QUERY_STRING"} ne "" 13 13 26
Condition100%3/3
defined $self->env->{"QUERY_STRING"} and $self->env->{"QUERY_STRING"} ne ""
!ll&&!rl&&r
11213
MC/DC100%2/2
defined $self->env->{"QUERY_STRING"} and $self->env->{"QUERY_STRING"} ne ""
defined $self->env->{"QUERY_STRING"} $self->env->{"QUERY_STRING"} ne ""
Truth table100%3/3
A and B
A= defined $self->env->{"QUERY_STRING"}B= $self->env->{"QUERY_STRING"} ne ""
ABresult
0X0
100
111
90  
91 26 ▶ $base =~ s!/$!! if $path =~ m!^/!;
Branch: 1 branch
BranchTrueFalseTotal
if $path =~ m[^/] 10 16 26
92  
93 26 return URI->new($base . $path)->canonical;
94 },
95 24 );
96  
97 has headers => (
98 is => 'ro',
99 isa => 'HTTP::Headers',
100 lazy => 1,
101 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 my $self = shift;
103 0 my $env = $self->env;
104 return HTTP::Headers->new(
105 map {
106 0 (my $field = $_) =~ s/^HTTPS?_//;
107 0 $field => $env->{$_}
108 } grep {
109 0 /^(?:HTTP|CONTENT)/i
110 } keys %$env
111 );
112 },
113 24 handles => ['header', 'content_encoding', 'referer', 'user_agent'],
114 );
115  
116 has cookies => (
117 is => 'ro',
118 isa => 'HashRef',
119 lazy => 1,
120 3 ▶ default => sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
121 3 my $self = shift;
122  
123 3 my $cookie_str = $self->env->{HTTP_COOKIE};
124 3 ▶ return {} unless defined $cookie_str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cookie_str 1 2 3
125  
126 2 my %results;
127 2 for my $pair (grep { /=/ } split /[;,] ?/, $cookie_str) {
128 7 $pair =~ s/^\s+|\s+$//g;
129 my ($key, $value) = map {
130 7 URI::Escape::uri_unescape($_)
131 } split(/=/, $pair, 2);
132 # XXX $self->decode too?
133 7 ▶ $results{$key} = $value unless exists $results{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $results{$key} 6 1 7
134 }
135  
136 2 return \%results;
137 },
138 24 );
139  
140 24 has _http_body => (
141 is => 'rw',
142 isa => 'HTTP::Body',
143 );
144  
145 has _parsed_body => (
146 traits => ['Hash'],
147 is => 'ro',
148 isa => 'HashRef',
149 lazy => 1,
150 19 ▶ default => sub {
Subroutine __ANON__: called CC=11 SCAR=24.2
151 19 my $self = shift;
152  
153 19 my $ct = $self->content_type;
154 19 my $cl = $self->content_length;
155 19 ▶ if (!$ct && !$cl) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($ct or $cl) 8 11 19
Condition66%2/3
$ct or $cl
l!l&&r!l&&!r
1108
MC/DC50%1/2
$ct or $cl
$ct $cl
Truth table66%2/3
A or B
A= $ctB= $cl
ABresult
1X1
011
000
156 8 ▶ if (!$self->env->{'psgix.input.buffered'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->env->{"psgix.input.buffered"}) 8 0 8
157 8 $self->env->{'psgix.input.buffered'} = 1;
158 8 $self->env->{'psgi.input'} = Stream::Buffered->new(0)->rewind;
159 }
160 return {
161 8 body => {},
162 uploads => {},
163 };
164 }
165  
166 11 my $body = HTTP::Body->new($ct, $cl);
167 # automatically clean up, but wait until the request object is gone
168 11 $body->cleanup(1);
169 11 $self->_http_body($body);
170  
171 11 my $input = $self->_input;
172  
173 11 my $buffer;
174 11 ▶ if ($self->env->{'psgix.input.buffered'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->env->{'psgix.input.buffered'}) { } 0 11 11
175 0 $input->seek(0, 0);
176 }
177 else {
178 11 $buffer = Stream::Buffered->new($cl);
179 }
180  
181 11 my $spin = 0;
182 11 ▶ while ($cl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cl) 2022 10 2032
183 2022 ▶ $input->read(my $chunk, $cl < 8192 ? $cl : 8192);
Branch: 1 branch
BranchTrueFalseTotal
$cl < 8192 ? : 2013 9 2022
184 2022 my $read = length($chunk);
185 2022 $cl -= $read;
186 2022 $body->add($chunk);
187 2022 ▶ $buffer->print($chunk) if $buffer;
Branch: 1 branch
BranchTrueFalseTotal
if $buffer 2022 0 2022
188  
189 2022 ▶ if ($read == 0 && $spin++ > 2000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read == 0 and $spin++ > 2000) 1 2021 2022
Condition100%3/3
$read == 0 and $spin++ > 2000
!ll&&!rl&&r
2020011
MC/DC100%2/2
$read == 0 and $spin++ > 2000
$read == 0 $spin++ > 2000
Truth table100%3/3
A and B
A= $read == 0B= $spin++ > 2000
ABresult
0X0
100
111
190 1 confess "Bad Content-Length ($cl bytes remaining)";
191 }
192 }
193  
194 10 ▶ if ($buffer) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buffer) { } 10 0 10
195 10 $self->env->{'psgix.input.buffered'} = 1;
196 10 $self->env->{'psgi.input'} = $buffer->rewind;
197 }
198 else {
199 0 $input->seek(0, 0);
200 }
201  
202 return {
203 10 body => $body->param,
204 uploads => $body->upload,
205 }
206 },
207 24 handles => {
208 _body => [ get => 'body' ],
209 _uploads => [ get => 'uploads' ],
210 },
211 );
212  
213 has query_parameters => (
214 is => 'ro',
215 isa => 'HashRef[Str]',
216 lazy => 1,
217 clearer => '_clear_query_parameters',
218 26 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
219 26 my $self = shift;
220  
221 my %params = (
222 $self->uri->query_form,
223 26 (map { $_ => '' } $self->uri->query_keywords),
224 );
225 26 for my $value (values %params) {
226 17 ▶ $value = ''
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 1 16 17
227 if !defined $value;
228 }
229 return {
230 26 map { $self->_decode($_) } %params
231 };
232 },
233 24 );
234  
235 has all_query_parameters => (
236 is => 'ro',
237 isa => 'HashRef[ArrayRef[Str]]',
238 lazy => 1,
239 clearer => '_clear_all_query_parameters',
240 6 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
241 6 my $self = shift;
242  
243 6 my @params = $self->uri->query_form;
244 6 my $ret = {};
245  
246 6 ▶ while (my ($k, $v) = splice @params, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@params, 0, 2)) 8 6 14
247 8 $k = $self->_decode($k);
248 8 ▶ push @{ $ret->{$k} ||= [] }, $self->_decode($v);
Condition100%2/2
$ret->{$k} ||= []
l!l
26
MC/DC100%1/1
$ret->{$k} ||= []
$ret->{$k}
Truth table100%2/2
A
A= $ret->{$k}
Aresult
11
00
249 }
250  
251 6 return $ret;
252 },
253 24 );
254  
255 has body_parameters => (
256 is => 'ro',
257 isa => 'HashRef[Str]',
258 lazy => 1,
259 clearer => '_clear_body_parameters',
260 13 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
261 13 my $self = shift;
262  
263 13 my $body = $self->_body;
264  
265 12 my $ret = {};
266 12 for my $key (keys %$body) {
267 7 my $val = $body->{$key};
268 7 $key = $self->_decode($key);
269 7 ▶ $ret->{$key} = $self->_decode(ref($val) ? $val->[-1] : $val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 1 6 7
270 }
271  
272 12 return $ret;
273 },
274 24 );
275  
276 has all_body_parameters => (
277 is => 'ro',
278 isa => 'HashRef[ArrayRef[Str]]',
279 lazy => 1,
280 clearer => '_clear_all_body_parameters',
281 6 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
282 6 my $self = shift;
283  
284 6 my $body = $self->_body;
285  
286 6 my $ret = {};
287 6 for my $key (keys %$body) {
288 5 my $val = $body->{$key};
289 5 $key = $self->_decode($key);
290 $ret->{$key} = ref($val)
291 5 ▶ ? [ map { $self->_decode($_) } @$val ]
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 1 4 5
292 : [ $self->_decode($val) ];
293 }
294  
295 6 return $ret;
296 },
297 24 );
298  
299 has uploads => (
300 is => 'ro',
301 isa => 'HashRef[Web::Request::Upload]',
302 lazy => 1,
303 3 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
304 3 my $self = shift;
305  
306 3 my $uploads = $self->_uploads;
307  
308 3 my $ret = {};
309 3 for my $key (keys %$uploads) {
310 7 my $val = $uploads->{$key};
311 7 ▶ $ret->{$key} = ref($val) eq 'ARRAY'
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 3 4 7
312 ? $self->_new_upload($val->[-1])
313 : $self->_new_upload($val);
314 }
315  
316 3 return $ret;
317 },
318 24 );
319  
320 has all_uploads => (
321 is => 'ro',
322 isa => 'HashRef[ArrayRef[Web::Request::Upload]]',
323 lazy => 1,
324 1 ▶ default => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
325 1 my $self = shift;
326  
327 1 my $uploads = $self->_uploads;
328  
329 1 my $ret = {};
330 1 for my $key (keys %$uploads) {
331 4 my $val = $uploads->{$key};
332 $ret->{$key} = ref($val) eq 'ARRAY'
333 4 ▶ ? [ map { $self->_new_upload($_) } @$val ]
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 2 2 4
334 : [ $self->_new_upload($val) ];
335 }
336  
337 1 return $ret;
338 },
339 24 );
340  
341 24 has _encoding_obj => (
342 is => 'rw',
343 isa => 'Object', # no idea what this should be
344 clearer => '_clear_encoding_obj',
345 predicate => 'has_encoding',
346 );
347  
348 71 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=7.4
Pod undocumented
349 71 my $self = shift;
350 71 my ($params) = @_;
351 71 ▶ if (defined $params->{encoding}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params->{'encoding'}) { } 0 71 71
352 0 $self->encoding($params->{encoding});
353 }
354 else {
355 71 $self->encoding($self->default_encoding);
356 }
357 }
358  
359 71 ▶sub new_from_env {
Subroutine new_from_env: called CC=1 SCAR=0.0
Pod documented
360 71 my $class = shift;
361 71 my ($env) = @_;
362  
363 71 return $class->new(env => $env);
364 }
365  
366 1 ▶sub new_from_request {
Subroutine new_from_request: called CC=1 SCAR=0.0
Pod documented
367 1 my $class = shift;
368 1 my ($req) = @_;
369  
370 1 return $class->new_from_env(HTTP::Message::PSGI::req_to_psgi($req));
371 }
372  
373 32 ▶sub new_response {
Subroutine new_response: called CC=2 SCAR=6.9
Pod documented
374 32 my $self = shift;
375  
376 32 Module::Runtime::use_package_optimistically($self->response_class);
377 32 my $res = $self->response_class->new(@_);
378 32 ▶ $res->_encoding_obj($self->_encoding_obj)
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_encoding 31 1 32
379 if $self->has_encoding;
380 32 return $res;
381 }
382  
383 13 ▶sub _new_upload {
Subroutine _new_upload: called CC=1 SCAR=0.0
384 13 my $self = shift;
385  
386 13 Module::Runtime::use_package_optimistically($self->upload_class);
387 13 $self->upload_class->new(@_);
388 }
389  
390 44 ▶sub host {
Subroutine host: called CC=4 SCAR=13.9
Pod documented
391 44 my $self = shift;
392  
393 44 my $env = $self->env;
394 44 my $host = $env->{HTTP_HOST};
395 $host = ($env->{SERVER_NAME} || '') . ':'
396 44 ▶ . ($env->{SERVER_PORT} || 80)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 11 33 44
Condition100%4/4
$env->{'SERVER_NAME'} || ''
l!l
65
$env->{'SERVER_PORT'} || 80
l!l
74
MC/DC100%1/1
$env->{'SERVER_NAME'} || ''
$env->{'SERVER_NAME'}
MC/DC100%1/1
$env->{'SERVER_PORT'} || 80
$env->{'SERVER_PORT'}
Truth table100%2/2
A
A= $env->{'SERVER_NAME'}
Aresult
11
00
Truth table100%2/2
A
A= $env->{'SERVER_PORT'}
Aresult
11
00
397 unless defined $host;
398  
399 44 return $host;
400 }
401  
402 0 ▶sub path {
Subroutine path: not called CC=2 SCAR=17.9
Pod documented
403 0 my $self = shift;
404  
405 0 my $path = $self->path_info;
406 0 ▶ return $path if length($path);
Branch: 1 branch
BranchTrueFalseTotal
if length $path 0 0 0
407 0 return '/';
408 }
409  
410 14 ▶sub parameters {
Subroutine parameters: called CC=1 SCAR=0.0
Pod documented
411 14 my $self = shift;
412  
413 return {
414 14 %{ $self->query_parameters },
415 14 %{ $self->body_parameters },
416 };
417 }
418  
419 3 ▶sub all_parameters {
Subroutine all_parameters: called CC=3 SCAR=11.0
Pod documented
420 3 my $self = shift;
421  
422 3 my $ret = { %{ $self->all_query_parameters } };
423 3 my $body_parameters = $self->all_body_parameters;
424  
425 3 for my $key (keys %$body_parameters) {
426 2 ▶ push @{ $ret->{$key} ||= [] }, @{ $body_parameters->{$key} };
Condition50%1/2
$ret->{$key} ||= []
l!l
02
MC/DC0%0/1
$ret->{$key} ||= []
$ret->{$key}
Truth table50%1/2
A
A= $ret->{$key}
Aresult
11
00
427 }
428  
429 3 return $ret;
430 }
431  
432 3 ▶sub param {
Subroutine param: called CC=1 SCAR=0.0
Pod documented
433 3 my $self = shift;
434 3 my ($key) = @_;
435  
436 3 $self->parameters->{$key};
437 }
438  
439 11 ▶sub content {
Subroutine content: called CC=4 SCAR=13.9
Pod documented
440 11 my $self = shift;
441  
442 11 ▶ unless ($self->env->{'psgix.input.buffered'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->env->{"psgix.input.buffered"}) 5 6 11
443 # the builder for this attribute also sets up psgi.input
444 5 $self->_parsed_body;
445 }
446  
447 11 ▶ my $fh = $self->_input or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $self->_input or return "" 11 0 11
448 11 ▶ my $cl = $self->content_length or return '';
Branch: 1 branch
BranchTrueFalseTotal
my $cl = $self->content_length or return "" 9 2 11
449  
450 9 $fh->seek(0, 0); # just in case middleware/apps read it without seeking back
451  
452 9 $fh->read(my $content, $cl, 0);
453 9 $fh->seek(0, 0);
454  
455 9 return $self->_decode($content);
456 }
457  
458 84 ▶sub _decode {
Subroutine _decode: called CC=2 SCAR=7.0
459 84 my $self = shift;
460 84 my ($content) = @_;
461 84 ▶ return $content unless $self->has_encoding;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_encoding 0 84 84
462 84 return $self->_encoding_obj->decode($content);
463 }
464  
465 87 ▶sub encoding {
Subroutine encoding: called CC=4 SCAR=13.9
Pod documented
466 87 my $self = shift;
467  
468 87 ▶ if (@_ > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 0) 87 0 87
469 87 my ($encoding) = @_;
470 87 $self->_clear_encoded_data;
471 87 ▶ if (defined($encoding)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoding) { } 86 1 87
472 86 $self->_encoding_obj(Encode::find_encoding($encoding));
473 }
474 else {
475 1 $self->_clear_encoding_obj;
476 }
477 }
478  
479 87 ▶ return $self->_encoding_obj ? $self->_encoding_obj->name : undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_encoding_obj ? : 86 1 87
480 }
481  
482 87 ▶sub _clear_encoded_data {
Subroutine _clear_encoded_data: called CC=1 SCAR=0.0
483 87 my $self = shift;
484 87 $self->_clear_encoding_obj;
485 87 $self->_clear_query_parameters;
486 87 $self->_clear_all_query_parameters;
487 87 $self->_clear_body_parameters;
488 87 $self->_clear_all_body_parameters;
489 }
490  
491 64 ▶sub response_class { 'Web::Response' }
Subroutine response_class: called CC=1 SCAR=0.0
Pod documented
492 26 ▶sub upload_class { 'Web::Request::Upload' }
Subroutine upload_class: called CC=1 SCAR=0.0
Pod documented
493 71 ▶sub default_encoding { 'iso8859-1' }
Subroutine default_encoding: called CC=1 SCAR=0.0
Pod documented
494  
495 24 __PACKAGE__->meta->make_immutable;
496 24 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
497  
498  
499  
500 24 1;
501  
502 __END__
« Response.pm ↑ Summary Request/Types.pm »