Format/Url.pm

filter: Statementstmt 88.7% 126 / 142 Branchbran 74.4% 64 / 86 Conditioncond 76.1% 16 / 21 MC/DCmcdc 50.0% 6 / 12 Subroutinesub 94.7% 18 / 19 Podpod 100% 6 / 6 totaltotal 82.5% 236 / 286 CC 51 SCAR 25.1
CC
51
Cov
83%
parse_url
23.0
from_url_params
15.0
_bohemy
12.0
CRAP 12.3
« Format/Html.pm ↑ Summary Format.pm »
1 package Aion::Format::Url;
2  
3 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use List::Util qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Encode qw//;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Exporter qw/import/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 our @EXPORT = our @EXPORT_OK = grep {
10 1 ref \$Aion::Format::Url::{$_} eq "GLOB"
11 16 ▶ && *{$Aion::Format::Url::{$_}}{CODE} && !/^(_|(NaN|import)\z)/n
Condition100%5/5
ref \$Aion::Format::Url::{$_} eq 'GLOB' && *{$Aion::Format::Url::{$_};}{'CODE'} && !/^(_|(NaN|import)\z)/nu
!ll&&!rl&&r
646
ref \$Aion::Format::Url::{$_} eq "GLOB" and *{$Aion::Format::Url::{$_};}{"CODE"}
!ll
313
MC/DC100%2/2
ref \$Aion::Format::Url::{$_} eq 'GLOB' && *{$Aion::Format::Url::{$_};}{'CODE'} && !/^(_|(NaN|import)\z)/nu
ref \$Aion::Format::Url::{$_} eq "GLOB" !/^(_|(NaN|import)\z)/nu
Truth table100%3/3
A && not(B)
A= ref \$Aion::Format::Url::{$_} eq "GLOB"B= !/^(_|(NaN|import)\z)/nu
ABresult
0X0
100
111
12 } keys %Aion::Format::Url::;
13  
14  
15 #@category escape url
16  
17 1 ▶use constant UNSAFE_RFC3986 => qr/[^A-Za-z0-9\-\._~]/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # Эскейпит значение
20 20 ▶sub to_url_param(;$) {
Subroutine to_url_param: called CC=3 SCAR=11.0
Pod documented
21 20 ▶ my ($param) = @_ == 0? $_: @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 ? : 11 9 20
22 1 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 20 ▶ $param =~ s/${\ UNSAFE_RFC3986}/$& eq " "? "+": sprintf "%%%02X", ord $&/ge;
Branch: 1 branch
BranchTrueFalseTotal
$& eq ' ' ? : 2 7 9
24 20 $param
25 }
26  
27 # Преобразует в формат url-параметров
28 3 ▶sub to_url_params(;$) {
Subroutine to_url_params: called CC=8 SCAR=20.8
Pod documented
29 3 ▶ my ($param) = @_ == 0? $_: @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 ? : 1 2 3
30  
31 3 my @R;
32 3 my @S = [$param];
33 3 ▶ while(@S) {
Branch: 1 branch
BranchTrueFalseTotal
if (@S) 22 3 25
34 22 my $u = pop @S;
35 22 my ($x, $key) = @$u;
36
37 22 ▶ if(ref $x eq "HASH") {
Branch: 4 branches
BranchTrueFalseTotal
if (ref $x eq 'HASH') { } 5 17 22
elsif (ref $x eq 'ARRAY') { } 4 13 17
elsif (not defined $x) { } 1 12 13
elsif ($x eq 1) { } 5 7 12
38 push @S, defined($key)
39 2 ? (map [$x->{$_}, "$key\[${\to_url_param}]"], sort keys %$x)
40 5 ▶ : (map [$x->{$_}, to_url_param], sort keys %$x)
Branch: 1 branch
BranchTrueFalseTotal
defined $key ? : 2 3 5
41 ;
42 }
43 elsif(ref $x eq "ARRAY") {
44 4 my $i = '';
45 4 push @S, map [$_, "$key\[${\($i++)}]"], @$x;
46 }
47 elsif(!defined $x) {}
48 5 elsif($x eq 1) { unshift @R, $key }
49 else {
50 7 unshift @R, join "=", $key, to_url_param $x;
51 }
52 }
53
54 3 join "&", @R
55 }
56  
57 # Определяет кодировку. В koi8-r и в cp1251 большие и малые буквы как бы поменялись местами, поэтому у правильной кодировки вес будет больше
58 0 ▶sub _bohemy {
Subroutine _bohemy: not called CC=3 SCAR=24.8
59 0 my ($s) = @_;
60 0 my $c = 0;
61 0 ▶ while($s =~ /[а-яё]+/gi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s =~ /[\x{430}-\x{44f}\x{451}]+/giu) 0 0 0
62 0 my $x = $&;
63 0 ▶ if($x =~ /^[А-ЯЁа-яё][а-яё]*$/) { $c += length $x } else { $c -= length $x }
Branch: 1 branch
BranchTrueFalseTotal
if ($x =~ /^[\x{410}-\x{42f}\x{401}\x{430}-\x{44f}\x{451}][\x{430}-\x{44f}\x{451}]*$/u) { } 0 0 0
64 }
65 $c
66 0 }
67  
68 10 ▶sub from_url_param(;$) {
Subroutine from_url_param: called CC=5 SCAR=16.6
Pod documented
69 10 ▶ my ($param) = @_ == 0? $_: @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 ? : 1 9 10
70  
71 10 ▶ utf8::encode($param) if utf8::is_utf8($param);
Branch: 1 branch
BranchTrueFalseTotal
if utf8::is_utf8($param) 1 9 10
72  
73 {
74 1 ▶ no utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
75 1 ▶ use bytes;
Subroutine BEGIN: called CC=1 SCAR=0.0
76 10 $param =~ tr!\+! !;
77 10 $param =~ s!%([\da-f]{2})! chr hex $1 !iage;
78 }
79  
80 10 eval { $param = Encode::decode_utf8($param, Encode::FB_CROAK) };
81  
82 10 ▶ if($@) { # видимо тут кодировка cp1251 или koi8-r
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 10 10
83 0 my $cp = Encode::decode('cp1251', $param);
84 0 my $koi = Encode::decode('koi8-r', $param);
85 # выбираем перекодировку в которой меньше больших букв внутри слова
86 0 ▶ $param = _bohemy($koi) > _bohemy($cp)? $koi: $cp;
Branch: 1 branch
BranchTrueFalseTotal
_bohemy($koi) > _bohemy($cp) ? : 0 0 0
87 }
88  
89 $param
90 10 }
91  
92 6 ▶sub _set_url_param(@) {
Subroutine _set_url_param: called CC=3 SCAR=13.3
93 6 my ($x, $val) = @_;
94 0 ▶ if(ref $$x eq "ARRAY") { push @$$x, $val }
Branch: 2 branches
BranchTrueFalseTotal
if (ref $$x eq 'ARRAY') { } 0 6 6
elsif (ref $$x eq 'HASH') { } 0 6 6
95 0 elsif(ref $$x eq "HASH") { $$x = [$$x, $val] }
96 6 else { $$x = $val }
97 }
98  
99 1 ▶sub from_url_params(;$) {
Subroutine from_url_params: called CC=14 SCAR=27.1
Pod documented
100 1 ▶ my ($params) = @_ == 0? $_: @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 0 ? : 1 0 1
101  
102 1 my %param;
103 my $x;
104 1 my $was_val;
105  
106 1 ▶ while($params =~ /\G (?:
Branch: 1 branch
BranchTrueFalseTotal
if ($params =~ /\G (?: (?:^|&) (?<key1> [^&=\[\]]* ) | \[ (?<key> [^\[\]]* ) \] | (?: = (?<val> [^&]*) ) | . ) /gsux) 15 1 16
107 (?:^|&) (?<key1> [^&=\[\]]* )
108 | \[ (?<key> [^\[\]]* ) \]
109 | (?: = (?<val> [^&]*) )
110 | .
111 ) /gsx) {
112  
113 15 ▶ if(exists $+{key1}) {
Branch: 3 branches
BranchTrueFalseTotal
if (exists $+{'key1'}) { } 5 10 15
elsif (exists $+{'key'}) { } 7 3 10
elsif (exists $+{'val'}) { } 3 0 3
114 5 ▶ _set_url_param $x, 1 unless $was_val;
Branch: 1 branch
BranchTrueFalseTotal
unless $was_val 3 2 5
115 5 $was_val = 0;
116 5 $x = \$param{from_url_param $+{key1}};
117 }
118 elsif(exists $+{key}) {
119 # Добрасываем в массив
120 7 ▶ if($+{key} eq '' || int $+{key} eq $+{key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($+{'key'} eq '' or int $+{'key'} eq $+{'key'}) { } 6 1 7
Condition100%3/3
$+{"key"} eq "" or int $+{"key"} eq $+{"key"}
l!l&&r!l&&!r
331
MC/DC100%2/2
$+{"key"} eq "" or int $+{"key"} eq $+{"key"}
$+{"key"} eq "" int $+{"key"} eq $+{"key"}
Truth table100%3/3
A or B
A= $+{"key"} eq ""B= int $+{"key"} eq $+{"key"}
ABresult
1X1
011
000
121 6 ▶ $$x = [$$x] if ref $$x ne 'ARRAY' && defined $$x;
Branch: 1 branch
BranchTrueFalseTotal
if ref $$x ne "ARRAY" and defined $$x 0 6 6
Condition66%2/3
ref $$x ne "ARRAY" and defined $$x
!ll&&!rl&&r
420
MC/DC0%0/2
ref $$x ne "ARRAY" and defined $$x
ref $$x ne "ARRAY" defined $$x
Truth table66%2/3
A and B
A= ref $$x ne "ARRAY"B= defined $$x
ABresult
0X0
100
111
122 6 $x = \$$x->[$+{key}];
123 }
124 else {
125 # Добавляем в хеш
126 1 my $key = from_url_param $+{key};
127 1 ▶ $$x = {$key => $$x} if ref $$x ne 'HASH' && defined $$x;
Branch: 1 branch
BranchTrueFalseTotal
if ref $$x ne "HASH" and defined $$x 0 1 1
Condition33%1/3
ref $$x ne "HASH" and defined $$x
!ll&&!rl&&r
010
MC/DC0%0/2
ref $$x ne "HASH" and defined $$x
ref $$x ne "HASH" defined $$x
Truth table33%1/3
A and B
A= ref $$x ne "HASH"B= defined $$x
ABresult
0X0
100
111
128 1 $x = \$$x->{$key};
129 }
130 }
131 elsif(exists $+{val}) {
132 3 _set_url_param $x, from_url_param $+{val};
133 3 $was_val = 1;
134 }
135
136 }
137
138 1 ▶ _set_url_param $x, 1 unless $was_val;
Branch: 1 branch
BranchTrueFalseTotal
unless $was_val 0 1 1
139  
140 1 \%param
141 }
142  
143 #@category parse url
144  
145 30 ▶sub _parse_url ($) {
Subroutine _parse_url: called CC=1 SCAR=0.0
146 30 my ($link) = @_;
147 30 $link =~ m!^
148 ( (?<proto> \w+ ) : )?
149 ( //
150 ( (?<user> [^:/?\#\@]* ) :
151 (?<pass> [^/?\#\@]* ) \@ )?
152 (?<domain> [^/?\#]* ) )?
153 ( / (?<path> [^?\#]* ) )?
154 (?<part> [^?\#]+ )?
155 ( \? (?<query> [^\#]* ) )?
156 ( \# (?<hash> .* ) )?
157 \z!xn;
158 30 return %+;
159 }
160  
161 # 1 - set / in each page, if it not file (*.*), or 0 - unset
162 1 ▶use Aion::Env::Etc DIR => (default => 0, key => 'aion.format.url.dir');
Subroutine BEGIN: called CC=1 SCAR=0.0
163 1 ▶use Aion::Env::Etc ONPAGE => (default => "off://off", key => 'aion.format.url.onpage');
Subroutine BEGIN: called CC=1 SCAR=0.0
164  
165 # Парсит и нормализует url
166 15 ▶sub parse_url($;$$) {
Subroutine parse_url: called CC=21 SCAR=31.3
Pod documented
167 15 my ($link, $onpage, $dir) = @_;
168 15 ▶ $onpage //= ONPAGE;
Condition100%2/2
$onpage //= "off://off"
l!l
510
MC/DC100%1/1
$onpage //= "off://off"
$onpage
Truth table100%2/2
A
A= $onpage
Aresult
11
00
169 15 ▶ $dir //= DIR;
Condition50%1/2
$dir //= 0
l!l
015
MC/DC0%0/1
$dir //= 0
$dir
Truth table50%1/2
A
A= $dir
Aresult
11
00
170 15 my $orig = $link;
171  
172 15 my %link = _parse_url $link;
173 15 my %onpage = _parse_url $onpage;
174  
175 15 ▶ if(!exists $link{path}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $link{"path"}) 11 4 15
176 11 ▶ $link{path} = join "", $onpage{path}, $onpage{path} =~ m!/\z!? (): "/", $link{part};
Branch: 1 branch
BranchTrueFalseTotal
$onpage{'path'} =~ m[/\z]u ? : 0 11 11
177 11 delete $link{part};
178 }
179  
180 15 ▶ if(exists $link{proto}) {}
Branch: 2 branches
BranchTrueFalseTotal
if (exists $link{'proto'}) { } 1 14 15
elsif (exists $link{'domain'}) { } 1 13 14
181 elsif(exists $link{domain}) {
182 1 $link{proto} = $onpage{proto};
183 }
184 else {
185 13 $link{proto} = $onpage{proto};
186 13 ▶ $link{user} = $onpage{user} if exists $onpage{user};
Branch: 1 branch
BranchTrueFalseTotal
if exists $onpage{"user"} 0 13 13
187 13 ▶ $link{pass} = $onpage{pass} if exists $onpage{pass};
Branch: 1 branch
BranchTrueFalseTotal
if exists $onpage{"pass"} 0 13 13
188 13 $link{domain} = $onpage{domain};
189 }
190  
191 # нормализуем
192 15 $link{proto} = lc $link{proto};
193 15 $link{domain} = lc $link{domain};
194 15 $link{dom} = $link{domain} =~ s/^www\.//r;
195 15 $link{path} = lc $link{path};
196  
197 15 my @path = split m!/!, $link{path}; my @p;
198  
199 15 for my $p (@path) {
200 18 ▶ if($p eq ".") {}
Branch: 2 branches
BranchTrueFalseTotal
if ($p eq '.') { } 0 18 18
elsif ($p eq '..') { } 0 18 18
201 elsif($p eq "..") {
202 #@p or die "Выход за пределы пути";
203 0 pop @p;
204 }
205 18 else { push @p, $p }
206 }
207  
208 15 @p = grep { $_ ne "" } @p;
209  
210 15 ▶ if(@p) {
Branch: 2 branches
BranchTrueFalseTotal
if (@p) { } 9 6 15
elsif ($dir) { } 0 6 6
211 9 $link{path} = join "/", "", @p;
212 9 ▶ if($link{path} =~ m![^/]*\.[^/]*\z!) {
Branch: 2 branches
BranchTrueFalseTotal
if ($link{'path'} =~ m[[^/]*\.[^/]*\z]u) { } 1 8 9
elsif ($dir) { } 0 8 8
213 1 $link{dir} = $`;
214 1 $link{file} = $&;
215 } elsif($dir) {
216 0 $link{path} = $link{dir} = "$link{path}/";
217 } else {
218 8 $link{dir} = "$link{path}/";
219 }
220 } elsif($dir) {
221 0 $link{path} = "/";
222 6 } else { delete $link{path} }
223  
224 15 $link{orig} = $orig;
225 15 $link{onpage} = $onpage;
226 $link{link} = join "", $link{proto}, "://",
227 exists $link{user} || exists $link{pass}? ($link{user},
228 exists $link{pass}? ":$link{pass}": (), '@'): (),
229 $link{dom},
230 $link{path},
231 length($link{query})? ("?", $link{query}): (),
232 15 ▶ length($link{hash})? ("#", $link{hash}): (),
Branch: 4 branches
BranchTrueFalseTotal
exists $link{'user'} || exists $link{'pass'} ? : 1 14 15
exists $link{'pass'} ? : 1 0 1
length $link{'query'} ? : 3 12 15
length $link{'hash'} ? : 1 14 15
Condition66%2/3
exists $link{"user"} or exists $link{"pass"}
l!l&&r!l&&!r
1014
MC/DC50%1/2
exists $link{"user"} or exists $link{"pass"}
exists $link{"user"} exists $link{"pass"}
Truth table66%2/3
A or B
A= exists $link{"user"}B= exists $link{"pass"}
ABresult
1X1
011
000
233 ;
234  
235 15 return \%link;
236 }
237  
238 # Нормализует url
239 12 ▶sub normalize_url($;$$) {
Subroutine normalize_url: called CC=1 SCAR=0.0
Pod documented
240 parse_url($_[0], $_[1], $_[2])->{link}
241 12 }
242  
243 1 1;
244  
245 __END__
« Format/Html.pm ↑ Summary Format.pm »