| 1 | package HTTP::AcceptLanguage; | ||||||||||||||||||||||||||||
| 2 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 6 | ▶ | use 5.008_005; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 6 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 6 | our $MATCH_PRIORITY_0_01_STYLE; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 6 | my $LANGUAGE_RANGE = qr/(?:[A-Za-z0-9]{1,8}(?:-[A-Za-z0-9]{1,8})*|\*)/; | |||||||||||||||||||||||||||
| 10 | 6 | my $QVALUE = qr/(?:0(?:\.[0-9]{0,3})?|1(?:\.0{0,3})?)/; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 101 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 13 | 101 | my($class, $header) = @_; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 101 | my @parsed_header; | |||||||||||||||||||||||||||
| 16 | 101 | ▶ | if ($header) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 17 | 87 | @parsed_header = $class->_parse($header); | |||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | bless { | ||||||||||||||||||||||||||||
| 21 | 101 | header => $header, | |||||||||||||||||||||||||||
| 22 | parsed_header => \@parsed_header, | ||||||||||||||||||||||||||||
| 23 | }, $class; | ||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 87 | ▶ | sub _parse { | ||||||||||||||||||||||||||
|
Subroutine
_parse: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 27 | 87 | my($class, $header) = @_; | |||||||||||||||||||||||||||
| 28 | 87 | $header =~ s/\s//g; #loose | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 87 | my @elements; | |||||||||||||||||||||||||||
| 31 | my %high_qualities; | ||||||||||||||||||||||||||||
| 32 | 87 | for my $element (split /,+/, $header) { | |||||||||||||||||||||||||||
| 33 | 167 | my($language, $quality) = $element =~ /\A($LANGUAGE_RANGE)(?:;q=($QVALUE))?\z/; | |||||||||||||||||||||||||||
| 34 | 167 | ▶ | $quality = 1 unless defined $quality; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | 167 | ▶ | next unless $language && $quality > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$language and $quality > 0
MC/DC100%2/2
$language and $quality > 0
$language
$quality > 0
Truth table100%3/3
A and B
A= $languageB= $quality > 0
| |||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 153 | my($primary) = split /-/, $language; | |||||||||||||||||||||||||||
| 38 | 153 | push @elements, { | |||||||||||||||||||||||||||
| 39 | language => $language, | ||||||||||||||||||||||||||||
| 40 | language_primary_lc => lc($primary), | ||||||||||||||||||||||||||||
| 41 | language_lc => lc($language), | ||||||||||||||||||||||||||||
| 42 | quality => $quality, | ||||||||||||||||||||||||||||
| 43 | }; | ||||||||||||||||||||||||||||
| 44 | 153 | ▶ | if ((not exists $high_qualities{$language}) || $quality > $high_qualities{$language}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
not exists $high_qualities{$language} or $quality > $high_qualities{$language}
MC/DC100%2/2
not exists $high_qualities{$language} or $quality > $high_qualities{$language}
not exists $high_qualities{$language}
$quality > $high_qualities{$language}
Truth table100%3/3
not(A) or B
A= not exists $high_qualities{$language}B= $quality > $high_qualities{$language}
| |||||||||||||||||||||||||||||
| 45 | 146 | $high_qualities{$language} = $quality; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # RFC2616: The language quality factor assigned to a language-tag by the Accept-Language field is the quality value of the longest language- range in the field that matches the language-tag. | ||||||||||||||||||||||||||||
| 50 | grep { | ||||||||||||||||||||||||||||
| 51 | 87 | my $language = $_->{language}; | |||||||||||||||||||||||||||
| 52 | $high_qualities{$language} ? ( | ||||||||||||||||||||||||||||
| 53 | 153 | ▶ | $high_qualities{$language} == $_->{quality} ? delete $high_qualities{$language} : 0 | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 54 | ) : 0; | ||||||||||||||||||||||||||||
| 55 | } @elements; | ||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 19 | ▶ | sub languages { | ||||||||||||||||||||||||||
|
Subroutine
languages: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 19 | my $self = shift; | |||||||||||||||||||||||||||
| 60 | 19 | ▶ | $self->{languages} ||= do { | ||||||||||||||||||||||||||
|
Condition33%1/3
$self->{"languages"} ||= do {
BEGIN {
$^H{'sort'} = '0';
}
my(@languages) = map({$_->{"language"};} (sort {$HTTP::AcceptLanguage::b->{"quality"} <=> $HTTP::AcceptLanguage::a->{"quality"};} @{$$self{"parsed_header"};}));
\@languages
}
MC/DC0%0/2
$self->{"languages"} ||= do {
BEGIN {
$^H{'sort'} = '0';
}
my(@languages) = map({$_->{"language"};} (sort {$HTTP::AcceptLanguage::b->{"quality"} <=> $HTTP::AcceptLanguage::a->{"quality"};} @{$$self{"parsed_header"};}));
\@languages
}
$self->{"languages"}
do {
BEGIN {
$^H{'sort'} = '0';
}
my(@languages) = map({$_->{"language"};} (sort {$HTTP::AcceptLanguage::b->{"quality"} <=> $HTTP::AcceptLanguage::a->{"quality"};} @{$$self{"parsed_header"};}));
\@languages
}
Truth table33%1/3
A ||= B
A= $self->{"languages"}B= do {
BEGIN {
$^H{'sort'} = '0';
}
my(@languages) = map({$_->{"language"};} (sort {$HTTP::AcceptLanguage::b->{"quality"} <=> $HTTP::AcceptLanguage::a->{"quality"};} @{$$self{"parsed_header"};}));
\@languages
}
| |||||||||||||||||||||||||||||
| 61 | 6 | ▶ | use sort 'stable'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 19 | my @languages = map { $_->{language} } sort { $b->{quality} <=> $a->{quality} } @{ $self->{parsed_header} }; | |||||||||||||||||||||||||||
| 63 | 19 | \@languages; | |||||||||||||||||||||||||||
| 64 | }; | ||||||||||||||||||||||||||||
| 65 | 19 | @{ $self->{languages} }; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 84 | ▶ | sub match { | ||||||||||||||||||||||||||
|
Subroutine
match: called CC=17 SCAR=28.3
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 84 | my($self, @languages) = @_; | |||||||||||||||||||||||||||
| 70 | my @normlized_languages = map { | ||||||||||||||||||||||||||||
| 71 | 84 | ▶ | $_ ? ( +{ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | tag => $_, | ||||||||||||||||||||||||||||
| 73 | tag_lc => lc($_), | ||||||||||||||||||||||||||||
| 74 | } ) : () | ||||||||||||||||||||||||||||
| 75 | } @languages; | ||||||||||||||||||||||||||||
| 76 | 84 | ▶ | return undef unless scalar(@normlized_languages); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 72 | ▶ | unless (scalar(@{ $self->{parsed_header} })) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | # RFC2616: SHOULD assume that all languages are equally acceptable. If an Accept-Language header is present, then all languages which are assigned a quality factor greater than 0 are acceptable. | ||||||||||||||||||||||||||||
| 80 | 4 | return $normlized_languages[0]->{tag}; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 68 | ▶ | $self->{sorted_parsed_header} ||= do { | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"sorted_parsed_header"} ||= do {
BEGIN {
$^H{'sort'} = '0';
}
[(sort {$HTTP::AcceptLanguage::b->{"quality"} <=> $HTTP::AcceptLanguage::a->{"quality"};} @{$$self{"parsed_header"};})]
}
MC/DC0%0/2
$self->{"sorted_parsed_header"} ||= do {
BEGIN {
$^H{'sort'} = '0';
}
[(sort {$HTTP::AcceptLanguage::b->{"quality"} <=> $HTTP::AcceptLanguage::a->{"quality"};} @{$$self{"parsed_header"};})]
}
$self->{"sorted_parsed_header"}
do {
BEGIN {
$^H{'sort'} = '0';
}
[(sort {$HTTP::AcceptLanguage::b->{"quality"} <=> $HTTP::AcceptLanguage::a->{"quality"};} @{$$self{"parsed_header"};})]
}
Truth table66%2/3
A ||= B
A= $self->{"sorted_parsed_header"}B= do {
BEGIN {
$^H{'sort'} = '0';
}
[(sort {$HTTP::AcceptLanguage::b->{"quality"} <=> $HTTP::AcceptLanguage::a->{"quality"};} @{$$self{"parsed_header"};})]
}
| |||||||||||||||||||||||||||||
| 84 | 6 | ▶ | use sort 'stable'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 85 | 66 | [ sort { $b->{quality} <=> $a->{quality} } @{ $self->{parsed_header} } ]; | |||||||||||||||||||||||||||
| 86 | }; | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | # If language-quality has the same value, is a priority order of the $self->{sorted_parsed_header}. | ||||||||||||||||||||||||||||
| 89 | # If you set $MATCH_PRIORITY_0_01_STYLE=1, takes is a priority order of the @languages | ||||||||||||||||||||||||||||
| 90 | 68 | ▶ | if ($MATCH_PRIORITY_0_01_STYLE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 33 | my %header_tags; | |||||||||||||||||||||||||||
| 92 | my %header_primary_tags; | ||||||||||||||||||||||||||||
| 93 | 62 | ▶ | my $detect_langguage = sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 94 | 62 | ▶ | if (scalar(%header_tags)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 95 | # RFC give priority to full match. | ||||||||||||||||||||||||||||
| 96 | 29 | for my $tag (@normlized_languages) { | |||||||||||||||||||||||||||
| 97 | 40 | ▶ | return $tag->{tag} if $header_tags{$tag->{tag_lc}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | 4 | for my $tag (@normlized_languages) { | |||||||||||||||||||||||||||
| 100 | 4 | ▶ | return $tag->{tag} if $header_primary_tags{$tag->{tag_lc}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | 33 | }; | |||||||||||||||||||||||||||
| 104 | 33 | my $current_quality = 0; | |||||||||||||||||||||||||||
| 105 | 33 | for my $language (@{ $self->{sorted_parsed_header} }) { | |||||||||||||||||||||||||||
| 106 | 54 | ▶ | if ($current_quality != $language->{quality}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | # check of the last quality languages | ||||||||||||||||||||||||||||
| 108 | 39 | my $ret = $detect_langguage->(); | |||||||||||||||||||||||||||
| 109 | 39 | ▶ | return $ret if $ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | # cleanup | ||||||||||||||||||||||||||||
| 112 | 33 | $current_quality = $language->{quality}; | |||||||||||||||||||||||||||
| 113 | 33 | %header_tags = (); | |||||||||||||||||||||||||||
| 114 | 33 | %header_primary_tags = (); | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # wildcard | ||||||||||||||||||||||||||||
| 118 | 48 | ▶ | return $normlized_languages[0]->{tag} if $language->{language} eq '*'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 44 | $header_tags{$language->{language_lc}} = 1; | |||||||||||||||||||||||||||
| 121 | 44 | $header_primary_tags{$language->{language_primary_lc}} = 1; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 23 | my $ret = $detect_langguage->(); | |||||||||||||||||||||||||||
| 125 | 23 | ▶ | return $ret if $ret; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | } else { | ||||||||||||||||||||||||||||
| 127 | # 0.02 or more | ||||||||||||||||||||||||||||
| 128 | 35 | for my $language (@{ $self->{sorted_parsed_header} }) { | |||||||||||||||||||||||||||
| 129 | # wildcard | ||||||||||||||||||||||||||||
| 130 | 37 | ▶ | return $normlized_languages[0]->{tag} if $language->{language} eq '*'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # RFC give priority to full match. | ||||||||||||||||||||||||||||
| 133 | 34 | for my $tag (@normlized_languages) { | |||||||||||||||||||||||||||
| 134 | 56 | ▶ | return $tag->{tag} if $language->{language_lc} eq $tag->{tag_lc}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 10 | for my $tag (@normlized_languages) { | |||||||||||||||||||||||||||
| 137 | 14 | ▶ | return $tag->{tag} if $language->{language_primary_lc} eq $tag->{tag_lc}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 4 | return undef; # not matched | |||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | 6 | 1; | |||||||||||||||||||||||||||
| 146 | __END__ | ||||||||||||||||||||||||||||