Uniform/Utils.pm

filter: Statementstmt 100% 48 / 48 Branchbran 90.9% 20 / 22 Conditioncond 66.6% 6 / 9 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 91.4% 86 / 94 CC 16 SCAR 20.8
CC
16
Cov
94%
normalize_http_headers
12.2
parse_size_limit
5.0
BEGIN
1.0
CRAP 8.0
↑ Summary Uniform/Exceptions.pm »
1 package Uniform::Utils;
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Uniform::Exceptions; # PULL IN THE EXCEPTION ENGINE
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 3 our $VERSION = '1.02';
10 3 our @EXPORT_OK = qw(normalize_http_headers parse_size_limit);
11  
12 # Shared production-grade normalization engine utilized by all Uniform distributions
13 2 ▶sub normalize_http_headers {
Subroutine normalize_http_headers: called CC=12 SCAR=25.0
Pod documented
14 2 my ($hash) = @_;
15  
16 2 ▶ unless (ref($hash) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $hash eq "HASH") 1 1 2
17 1 Uniform::Exceptions->throw(
18 type => 'TypeError',
19 message => 'Normalization input context must be a strict HASH reference',
20 attribute => 'hash_input',
21 );
22 }
23  
24 1 my (%normalized, %priority);
25  
26 1 for my $key (keys %$hash) {
27 5 ▶ next unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 5 5
28 5 my $val = $hash->{$key};
29  
30 5 my $norm = lc "$key";
31 5 $norm =~ s/^\s+|\s+$//g;
32  
33 5 my $is_http_env = $norm =~ s/^http[-_]//;
34 5 ▶ my $rank = ($is_http_env ? 0 : 2) + ($norm =~ /-/ ? 1 : 0);
Branch: 2 branches
BranchTrueFalseTotal
$is_http_env ? : 1 4 5
$norm =~ /-/ ? : 3 2 5
35  
36 5 $norm =~ tr/_/-/;
37 5 $norm =~ s/-+/-/g;
38  
39 # Guard clause: Ignore unrelated, malformed, or hostile names.
40 # This prevents a bogus key from colliding with valid metadata after normalization.
41 5 ▶ next unless $norm =~ /\Ahx-[a-z0-9]+(?:-[a-z0-9]+)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $norm =~ /\Ahx-[a-z0-9]+(?:-[a-z0-9]+)*\z/ 1 4 5
42  
43 # Some adapters preserve duplicate headers as an array reference.
44 # HTTP specs define these fields as single values, so the last defined value wins.
45 4 ▶ if (ref($val) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY") 1 3 4
46 1 ▶ ($val) = reverse grep { defined && !ref } @$val;
Condition33%1/3
defined $_ && !ref($_)
!ll&&!rl&&r
002
MC/DC0%0/2
defined $_ && !ref($_)
defined $_ !ref($_)
Truth table33%1/3
A && not(B)
A= defined $_B= !ref($_)
ABresult
0X0
100
111
47 }
48 4 ▶ next if ref $val;
Branch: 1 branch
BranchTrueFalseTotal
if ref $val 0 4 4
49  
50 # Prioritize real HTTP header spellings over CGI/PSGI environment spellings
51 # if an adapter accidentally supplies both down the pipeline.
52 4 ▶ next if exists($priority{$norm}) && $priority{$norm} > $rank;
Branch: 1 branch
BranchTrueFalseTotal
if exists $priority{$norm} and $priority{$norm} > $rank 1 3 4
Condition66%2/3
exists $priority{$norm} and $priority{$norm} > $rank
!ll&&!rl&&r
301
MC/DC50%1/2
exists $priority{$norm} and $priority{$norm} > $rank
exists $priority{$norm} $priority{$norm} > $rank
Truth table66%2/3
A and B
A= exists $priority{$norm}B= $priority{$norm} > $rank
ABresult
0X0
100
111
53  
54 3 $normalized{$norm} = $val;
55 3 $priority{$norm} = $rank;
56 }
57  
58 1 return \%normalized;
59 }
60  
61 # Shared human-readable size string parser utilized by any Uniform distribution
62 # that needs to compare a byte count against a configured limit (upload size
63 # caps, request body size caps, etc).
64 7 ▶sub parse_size_limit {
Subroutine parse_size_limit: called CC=5 SCAR=16.1
Pod documented
65 7 my ($limit) = @_;
66  
67 7 ▶ unless (defined $limit && length $limit) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $limit and length $limit) 2 5 7
Condition100%3/3
defined $limit and length $limit
!ll&&!rl&&r
115
MC/DC100%2/2
defined $limit and length $limit
defined $limit length $limit
Truth table100%3/3
A and B
A= defined $limitB= length $limit
ABresult
0X0
100
111
68 2 Uniform::Exceptions->throw(
69 type => 'ValidationError',
70 message => 'parse_size_limit requires a defined, non-empty size string (e.g. "2M") or a plain byte count',
71 attribute => 'limit',
72 );
73 }
74  
75 5 my %multiplier = ( K => 1024, M => 1024**2, G => 1024**3 );
76 5 my ($num, $unit) = $limit =~ /^([\d.]+)\s*([KMG]?)$/i;
77  
78 5 ▶ unless (defined $num) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $num) 1 4 5
79 1 Uniform::Exceptions->throw(
80 type => 'ValidationError',
81 message => "parse_size_limit received an unparsable size string: '$limit'",
82 attribute => 'limit',
83 );
84 }
85  
86 4 ▶ return $unit ? $num * $multiplier{uc $unit} : $num;
Branch: 1 branch
BranchTrueFalseTotal
$unit ? : 3 1 4
87 }
88  
89 3 1;
90  
91 __END__
↑ Summary Uniform/Exceptions.pm »