blib/lib/Catalyst/Seal/Prepare.pm

filter: Statementstmt 88.9% 145 / 163 Branchbran 54.7% 46 / 84 Conditioncond 38.4% 20 / 52 MC/DCmcdc 3.3% 1 / 30 Subroutinesub 93.3% 14 / 15 Podpod 100% 1 / 1 totaltotal 65.8% 227 / 345 CC 64 SCAR 27.0
CC
64
Cov
71%
_prepare_headers
29.4
_prepare_path
20.3
_prepare_query_parameters
15.8
CRAP 14.9
« blib/lib/Catalyst/Seal/Exceptions.pm ↑ Summary blib/lib/Catalyst/Seal/Dispatch.pm »
1 package Catalyst::Seal::Prepare;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 19 ▶use Catalyst::Seal ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Catalyst::Seal::Guard ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 19 our $VERSION = '0.04';
12  
13 19 my $DONE = 0;
14  
15 398 ▶sub _is_strict_utf8 {
Subroutine _is_strict_utf8: called CC=3 SCAR=11.8
16 398 my ($enc) = @_;
17 398 ▶ return 0 unless $enc;
Branch: 1 branch
BranchTrueFalseTotal
unless $enc 0 398 398
18 398 my $name = eval { $enc->name };
19 398 ▶ return defined $name && $name eq 'utf-8-strict';
Condition33%1/3
defined $name && $name eq 'utf-8-strict'
!ll&&!rl&&r
00398
MC/DC0%0/2
defined $name && $name eq 'utf-8-strict'
defined $name $name eq 'utf-8-strict'
Truth table33%1/3
A && B
A= defined $nameB= $name eq 'utf-8-strict'
ABresult
0X0
100
111
20 }
21  
22 31 ▶sub _handle_param_unicode_decoding {
Subroutine _handle_param_unicode_decoding: called CC=10 SCAR=24.7
23 31 my ( $self, $value, $check ) = @_;
24 31 ▶ return unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 31 31
25 31 ▶ return $value if Scalar::Util::blessed($value);
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($value) 0 31 31
26  
27 31 my $enc = $self->encoding;
28  
29 31 ▶ return $value unless $enc;
Branch: 1 branch
BranchTrueFalseTotal
unless $enc 0 31 31
30  
31 31 ▶ if (_is_strict_utf8($enc)) {
Branch: 1 branch
BranchTrueFalseTotal
if (_is_strict_utf8($enc)) 31 0 31
32 31 my @fast = Catalyst::Seal::_decode_param($value, 0, 1);
33 31 ▶ return wantarray ? @fast : $fast[0] if @fast;
Branch: 2 branches
BranchTrueFalseTotal
if @fast 30 1 31
wantarray ? : 1 29 30
34 }
35  
36 1 ▶ $check ||= $self->_encode_check;
Condition33%1/3
$check ||= $self->_encode_check
l!l&&r!l&&!r
100
MC/DC0%0/2
$check ||= $self->_encode_check
$check $self->_encode_check
Truth table33%1/3
A ||= B
A= $checkB= $self->_encode_check
ABresult
1X1
011
000
37  
38 1 local $@;
39 1 my @out = eval { $enc->decode( $value, $check ) };
40 1 my $err = $@;
41 1 ▶ return wantarray ? @out : $out[0] unless $err;
Branch: 2 branches
BranchTrueFalseTotal
unless $err 0 1 1
wantarray ? : 0 0 0
42  
43 1 return $self->handle_unicode_encoding_exception({
44 param_value => $value,
45 error_msg => $err,
46 encoding_step => 'params',
47 });
48 }
49  
50 19 ▶use constant _FIELD_MAX => 256;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 19 my %FIELD;
53  
54 264 ▶sub _learn_field {
Subroutine _learn_field: called CC=10 SCAR=24.7
55 264 my ($key) = @_;
56  
57 264 ▶ return 0 unless $key =~ /^(HTTP|CONTENT|COOKIE)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^(HTTP|CONTENT|COOKIE)/i 228 36 264
58  
59 36 (my $field = $key) =~ s/^HTTPS?_//;
60 36 $field =~ tr/_/-/;
61  
62 36 require HTTP::Headers;
63 36 my $probe = HTTP::Headers->new;
64 36 my $ok = eval { $probe->header($field => 'probe'); 1 };
65 36 ▶ return 1 unless $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 36 36
66  
67 36 my $std = delete $probe->{'::std_case'};
68 36 my @keys = keys %$probe;
69 36 ▶ return 1 unless @keys == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless @keys == 1 0 36 36
70  
71 36 my $stored = $probe->{ $keys[0] };
72 36 ▶ return 1 if ref $stored;
Branch: 1 branch
BranchTrueFalseTotal
if ref $stored 0 36 36
73 36 ▶ return 1 unless defined $stored && $stored eq 'probe';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $stored and $stored eq "probe" 0 36 36
Condition33%1/3
defined $stored and $stored eq "probe"
!ll&&!rl&&r
0036
MC/DC0%0/2
defined $stored and $stored eq "probe"
defined $stored $stored eq "probe"
Truth table33%1/3
A and B
A= defined $storedB= $stored eq "probe"
ABresult
0X0
100
111
74  
75 36 ▶ return 1 if $std && keys %$std != 1;
Branch: 1 branch
BranchTrueFalseTotal
if $std and keys %$std != 1 0 36 36
Condition33%1/3
$std and keys %$std != 1
!ll&&!rl&&r
3600
MC/DC0%0/2
$std and keys %$std != 1
$std keys %$std != 1
Truth table33%1/3
A and B
A= $stdB= keys %$std != 1
ABresult
0X0
100
111
76 36 ▶ return [ $keys[0], $std ? $std->{ $keys[0] } : undef ];
Branch: 1 branch
BranchTrueFalseTotal
$std ? : 0 36 36
77 }
78  
79 366 ▶sub _prepare_headers {
Subroutine _prepare_headers: called CC=13 SCAR=33.8
80 366 my ($self) = @_;
81  
82 366 my $env = $self->env;
83 366 my $headers = HTTP::Headers->new();
84 366 my %std;
85  
86 366 ▶ my $todo = ref $env eq 'HASH'
Branch: 1 branch
BranchTrueFalseTotal
ref $env eq 'HASH' ? : 366 0 366
87 ? Catalyst::Seal::_build_headers($env, \%FIELD, $headers, \%std)
88 : [ keys %$env ];
89  
90 366 for my $key (@$todo) {
91 264 my $field = $FIELD{$key};
92 264 ▶ unless (defined $field) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field) 264 0 264
93 264 ▶ if (keys(%FIELD) >= _FIELD_MAX) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %FIELD >= 256) { } 0 264 264
94 0 ▶ next unless $key =~ /^(HTTP|CONTENT|COOKIE)/i;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^(HTTP|CONTENT|COOKIE)/i 0 0 0
95 0 $field = 1;
96 }
97 else {
98 264 $field = $FIELD{$key} = _learn_field($key);
99 }
100 }
101 264 ▶ next unless $field;
Branch: 1 branch
BranchTrueFalseTotal
unless $field 228 36 264
102  
103 36 my $value = $env->{$key};
104  
105 36 ▶ if (!ref $field || !defined $value || ref $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $field && defined $value or ref $value) 0 36 36
Condition33%2/6
not ref $field && defined $value or ref $value
l!l&&r!l&&!r
0036
ref $field && defined $value
!ll&&!rl&&r
0036
MC/DC0%0/3
not ref $field && defined $value or ref $value
ref $field defined $value ref $value
Truth table25%1/4
not(A && B) or C
A= ref $fieldB= defined $valueC= ref $value
ABCresult
0XX1
10X1
1111
1100
106 0 (my $name = $key) =~ s/^HTTPS?_//;
107 0 $name =~ tr/_/-/;
108 0 $headers->header($name => $value);
109 0 next;
110 }
111  
112 36 $headers->{ $field->[0] } = $value;
113 36 ▶ $std{ $field->[0] } = $field->[1] if defined $field->[1];
Branch: 1 branch
BranchTrueFalseTotal
if defined $field->[1] 0 36 36
114 }
115  
116 366 ▶ if (%std) {
Branch: 1 branch
BranchTrueFalseTotal
if (%std) 0 366 366
117 0 my $existing = $headers->{'::std_case'};
118 0 ▶ @std{ keys %$existing } = values %$existing if $existing;
Branch: 1 branch
BranchTrueFalseTotal
if $existing 0 0 0
119 0 $headers->{'::std_case'} = \%std;
120 }
121  
122 366 return $headers;
123 }
124  
125 19 my $FAST_CANONICAL = 0;
126  
127 12 ▶sub _probe_canonical {
Subroutine _probe_canonical: called CC=7 SCAR=20.4
128 12 require URI;
129 12 require URI::http;
130  
131 12 for my $str ('http://127.0.0.1/', 'http://example.com:8080/a/b?x=Y',
132 'https://example.com/') {
133 36 my $uri = $str;
134 36 my $obj = bless \$uri, 'URI::http';
135 36 my $out = eval { $obj->canonical };
136 36 ▶ return 0 unless $out && Scalar::Util::refaddr($out) == Scalar::Util::refaddr($obj);
Branch: 1 branch
BranchTrueFalseTotal
unless $out and builtin::refaddr($out) == builtin::refaddr($obj) 0 36 36
Condition33%1/3
$out and builtin::refaddr($out) == builtin::refaddr($obj)
!ll&&!rl&&r
0036
MC/DC0%0/2
$out and builtin::refaddr($out) == builtin::refaddr($obj)
$out builtin::refaddr($out) == builtin::refaddr($obj)
Truth table33%1/3
A and B
A= $outB= builtin::refaddr($out) == builtin::refaddr($obj)
ABresult
0X0
100
111
137 }
138  
139 12 for my $str ('http://EXAMPLE.com/', 'http://example.com:80/', 'http://example.com/%2f') {
140 36 my $uri = $str;
141 36 my $obj = bless \$uri, 'URI::http';
142 36 my $out = eval { $obj->canonical };
143 36 ▶ return 0 unless $out && Scalar::Util::refaddr($out) != Scalar::Util::refaddr($obj);
Branch: 1 branch
BranchTrueFalseTotal
unless $out and builtin::refaddr($out) != builtin::refaddr($obj) 0 36 36
Condition33%1/3
$out and builtin::refaddr($out) != builtin::refaddr($obj)
!ll&&!rl&&r
0036
MC/DC0%0/2
$out and builtin::refaddr($out) != builtin::refaddr($obj)
$out builtin::refaddr($out) != builtin::refaddr($obj)
Truth table33%1/3
A and B
A= $outB= builtin::refaddr($out) != builtin::refaddr($obj)
ABresult
0X0
100
111
144 }
145  
146 12 return 1;
147 }
148  
149 366 ▶sub _prepare_path {
Subroutine _prepare_path: called CC=14 SCAR=30.1
150 366 my ($self, $ctx) = @_;
151  
152 366 my $req = $ctx->request;
153 366 my $env = $req->env;
154  
155 366 ▶ my $scheme = $req->secure ? 'https' : 'http';
Branch: 1 branch
BranchTrueFalseTotal
$req->secure ? : 0 366 366
156 366 ▶ my $host = $env->{HTTP_HOST} || $env->{SERVER_NAME};
Condition33%1/3
$env->{'HTTP_HOST'} || $env->{'SERVER_NAME'}
l!l&&r!l&&!r
36600
MC/DC0%0/2
$env->{'HTTP_HOST'} || $env->{'SERVER_NAME'}
$env->{'HTTP_HOST'} $env->{'SERVER_NAME'}
Truth table33%1/3
A || B
A= $env->{'HTTP_HOST'}B= $env->{'SERVER_NAME'}
ABresult
1X1
011
000
157 366 ▶ my $port = $env->{SERVER_PORT} || 80;
Condition50%1/2
$env->{'SERVER_PORT'} || 80
l!l
3660
MC/DC0%0/1
$env->{'SERVER_PORT'} || 80
$env->{'SERVER_PORT'}
Truth table50%1/2
A
A= $env->{'SERVER_PORT'}
Aresult
11
00
158 366 ▶ my $base_path = $env->{SCRIPT_NAME} || "/";
Condition50%1/2
$env->{'SCRIPT_NAME'} || '/'
l!l
0366
MC/DC0%0/1
$env->{'SCRIPT_NAME'} || '/'
$env->{'SCRIPT_NAME'}
Truth table50%1/2
A
A= $env->{'SCRIPT_NAME'}
Aresult
11
00
159  
160 366 my $path;
161 366 ▶ if (!$ctx->config->{use_request_uri_for_path}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ctx->config->{'use_request_uri_for_path'}) { } 366 0 366
162 366 my $path_info = $env->{PATH_INFO};
163 366 ▶ if ( exists $env->{REDIRECT_URL} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $env->{"REDIRECT_URL"}) 0 366 366
164 0 $base_path = $env->{REDIRECT_URL};
165 0 $base_path =~ s/\Q$path_info\E$//;
166 }
167 366 $path = $base_path . $path_info;
168 366 $path =~ s{^/+}{};
169 366 $path =~ s/([^$URI::uric])/$URI::Escape::escapes{$1}/go;
170 366 $path =~ s/\?/%3F/g;
171 }
172 else {
173 0 my $req_uri = $env->{REQUEST_URI};
174 0 $req_uri =~ s/\?.*$//;
175 0 $path = $req_uri;
176 0 $path =~ s{^/+}{};
177 }
178  
179 366 my $uri_class = "URI::$scheme";
180  
181 366 $host =~ s/:(?:80|443)$//;
182  
183 366 ▶ if ($port !~ /^(?:80|443)$/ && $host !~ /:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($port =~ /^(?:80|443)$/ or $host =~ /:/) 0 366 366
Condition33%1/3
$port =~ /^(?:80|443)$/ or $host =~ /:/
l!l&&r!l&&!r
36600
MC/DC0%0/2
$port =~ /^(?:80|443)$/ or $host =~ /:/
$port =~ /^(?:80|443)$/ $host =~ /:/
Truth table33%1/3
A or B
A= $port =~ /^(?:80|443)$/B= $host =~ /:/
ABresult
1X1
011
000
184 0 $host .= ":$port";
185 }
186  
187 366 ▶ my $query = $env->{QUERY_STRING} ? '?' . $env->{QUERY_STRING} : '';
Branch: 1 branch
BranchTrueFalseTotal
$env->{'QUERY_STRING'} ? : 316 50 366
188 366 my $uri = $scheme . '://' . $host . '/' . $path . $query;
189 366 my $obj = bless \$uri, $uri_class;
190  
191 366 ▶ $req->uri(
Branch: 1 branch
BranchTrueFalseTotal
!($uri =~ /%[0-9A-Fa-f]{2}/) && $uri =~ m[\A[a-z][a-z0-9+.\-]*://([^/?\#]*)] && !($1 =~ /[A-Z:]/) ? : 262 104 366
Condition66%4/6
not $uri =~ /%[0-9A-Fa-f]{2}/ and $uri =~ m[\A[a-z][a-z0-9+.\-]*://([^/?\#]*)] and not $1 =~ /[A-Z:]/
!ll&&!rl&&r
1040262
not $uri =~ /%[0-9A-Fa-f]{2}/ and $uri =~ m[\A[a-z][a-z0-9+.\-]*://([^/?\#]*)]
!ll&&!rl&&r
1040262
MC/DC33%1/3
not $uri =~ /%[0-9A-Fa-f]{2}/ and $uri =~ m[\A[a-z][a-z0-9+.\-]*://([^/?\#]*)] and not $1 =~ /[A-Z:]/
not $uri =~ /%[0-9A-Fa-f]{2}/ $uri =~ m[\A[a-z][a-z0-9+.\-]*://([^/?\#]*)] not $1 =~ /[A-Z:]/
Truth table50%2/4
not(A) and B and not(C)
A= not $uri =~ /%[0-9A-Fa-f]{2}/B= $uri =~ m[\A[a-z][a-z0-9+.\-]*://([^/?\#]*)]C= not $1 =~ /[A-Z:]/
ABCresult
0XX0
10X0
1100
1111
192 ( $uri !~ /%[0-9A-Fa-f]{2}/
193 && $uri =~ m{\A[a-z][a-z0-9+.\-]*://([^/?\#]*)}
194 && $1 !~ /[A-Z:]/ )
195 ? $obj
196 : $obj->canonical
197 );
198  
199 366 ▶ $base_path .= '/' unless $base_path =~ m{/$};
Branch: 1 branch
BranchTrueFalseTotal
unless $base_path =~ m[/$] 0 366 366
200  
201 366 my $base_uri = $scheme . '://' . $host . $base_path;
202  
203 366 $req->base( bless \$base_uri, $uri_class );
204  
205 366 return;
206 }
207  
208 367 ▶sub _prepare_query_parameters {
Subroutine _prepare_query_parameters: called CC=11 SCAR=27.6
209 367 my ($self, $c) = @_;
210  
211 my $stock = $Catalyst::Seal::Guard::ORIGINAL{
212 367 'Catalyst::Engine::prepare_query_parameters'};
213  
214 367 my $req = $c->request;
215 367 my $config = $c->config;
216  
217 goto &$stock if $config->{do_not_decode_query}
218 || $config->{default_query_encoding}
219 || $config->{do_not_check_query_encoding}
220 367 ▶ || $req->_use_hash_multivalue;
Branch: 1 branch
BranchTrueFalseTotal
if $config->{"do_not_decode_query"} or $config->{"default_query_encoding"} or $config->{"do_not_check_query_encoding"} or $req->_use_hash_multivalue 0 367 367
Condition33%3/9
$config->{"do_not_decode_query"} or $config->{"default_query_encoding"} or $config->{"do_not_check_query_encoding"} or $req->_use_hash_multivalue
l!l&&r!l&&!r
00367
$config->{"do_not_decode_query"} or $config->{"default_query_encoding"} or $config->{"do_not_check_query_encoding"}
l!l&&r!l&&!r
00367
$config->{"do_not_decode_query"} or $config->{"default_query_encoding"}
l!l&&r!l&&!r
00367
MC/DC0%0/4
$config->{"do_not_decode_query"} or $config->{"default_query_encoding"} or $config->{"do_not_check_query_encoding"} or $req->_use_hash_multivalue
$config->{"do_not_decode_query"} $config->{"default_query_encoding"} $config->{"do_not_check_query_encoding"} $req->_use_hash_multivalue
Truth table20%1/5
A or B or C or D
A= $config->{"do_not_decode_query"}B= $config->{"default_query_encoding"}C= $config->{"do_not_check_query_encoding"}D= $req->_use_hash_multivalue
ABCDresult
1XXX1
01XX1
001X1
00011
00000
221  
222 367 my $enc = $c->encoding;
223 367 ▶ goto &$stock if $enc && !_is_strict_utf8($enc);
Branch: 1 branch
BranchTrueFalseTotal
if $enc and not _is_strict_utf8($enc) 0 367 367
Condition33%1/3
$enc and not _is_strict_utf8($enc)
!ll&&!rl&&r
03670
MC/DC0%0/2
$enc and not _is_strict_utf8($enc)
$enc not _is_strict_utf8($enc)
Truth table33%1/3
A and not(B)
A= $encB= not _is_strict_utf8($enc)
ABresult
0X0
100
111
224  
225 367 my $env = $req->env;
226 367 ▶ my $qs = exists $env->{QUERY_STRING} ? $env->{QUERY_STRING} : '';
Branch: 1 branch
BranchTrueFalseTotal
exists $env->{'QUERY_STRING'} ? : 367 0 367
227  
228 367 ▶ my ($params, $keywords) = Catalyst::Seal::_parse_query($qs, $enc ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$enc ? : 367 0 367
229 367 ▶ goto &$stock unless $params;
Branch: 1 branch
BranchTrueFalseTotal
unless $params 1 366 367
230  
231 366 ▶ $req->query_keywords($keywords) if defined $keywords;
Branch: 1 branch
BranchTrueFalseTotal
if defined $keywords 0 366 366
232 366 $req->query_parameters($params);
233 366 return;
234 }
235  
236 12 ▶Catalyst::Seal::register_step('prepare' => sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
237 12 ▶ return if $DONE++;
Branch: 1 branch
BranchTrueFalseTotal
if $DONE++ 0 12 12
238  
239 12 require HTTP::Headers;
240 12 require URI;
241 12 require URI::Escape;
242  
243 12 Catalyst::Seal::Guard::replace(
244 'Catalyst::_handle_param_unicode_decoding' => \&_handle_param_unicode_decoding);
245  
246 12 Catalyst::Seal::Guard::replace(
247 'Catalyst::Engine::prepare_query_parameters' => \&_prepare_query_parameters);
248  
249 12 Catalyst::Seal::Guard::replace(
250 'Catalyst::Request::prepare_headers' => \&_prepare_headers);
251  
252 12 ▶ if (_probe_canonical()) {
Branch: 1 branch
BranchTrueFalseTotal
if (_probe_canonical()) { } 12 0 12
253 12 $FAST_CANONICAL = 1;
254 12 Catalyst::Seal::Guard::replace(
255 'Catalyst::Engine::prepare_path' => \&_prepare_path);
256 }
257 else {
258 0 Catalyst::Seal::note(
259 'URI::canonical does not behave the way prepare_path relies on, path not patched');
260 }
261  
262 12 return;
263 19 });
264  
265 0 ▶sub fast_canonical { $FAST_CANONICAL }
Subroutine fast_canonical: not called CC=1 SCAR=6.9
Pod documented
266  
267 19 1;
268  
269 __END__
« blib/lib/Catalyst/Seal/Exceptions.pm ↑ Summary blib/lib/Catalyst/Seal/Dispatch.pm »