line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Pcore::Util::URI::Web2; |
2
|
|
|
|
|
|
|
|
3
|
3
|
|
|
3
|
|
1473
|
use Pcore -role; |
|
3
|
|
|
|
|
5
|
|
|
3
|
|
|
|
|
23
|
|
4
|
3
|
|
|
3
|
|
34
|
use Pcore::Util::Text qw[decode_utf8]; |
|
3
|
|
|
|
|
8
|
|
|
3
|
|
|
|
|
28
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
has _web2_data => ( is => 'lazy', isa => Maybe [ArrayRef], init_arg => undef ); |
7
|
|
|
|
|
|
|
has web2_domain => ( is => 'lazy', isa => Maybe [Str], init_arg => undef ); |
8
|
|
|
|
|
|
|
has web2_id => ( is => 'lazy', isa => Maybe [Str], init_arg => undef ); |
9
|
|
|
|
|
|
|
has is_web2 => ( is => 'lazy', isa => Bool, init_arg => undef ); |
10
|
|
|
|
|
|
|
has web2_canon => ( is => 'lazy', isa => Maybe [Str], init_arg => undef ); # subdomain.domain.tld, domain.tld/path/, without scheme |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
our $WEB2_CFG = P->cfg->load( $ENV->share->get('/data/web2.ini') ); |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $WEB2_HOST_RE; |
15
|
|
|
|
|
|
|
our $WEB2_RE; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub _web2_compile { |
18
|
1
|
|
|
1
|
|
3
|
my @re; |
19
|
|
|
|
|
|
|
|
20
|
1
|
|
|
|
|
23
|
for my $host ( sort keys $WEB2_CFG->%* ) { |
21
|
34
|
100
|
|
|
|
306
|
$WEB2_RE->{$host} = qr[$WEB2_CFG->{$host}->{re}]smi if $WEB2_CFG->{$host}->{re}; |
22
|
|
|
|
|
|
|
|
23
|
34
|
100
|
|
|
|
64
|
if ( $host =~ /[.]/sm ) { |
24
|
33
|
|
|
|
|
60
|
push @re, quotemeta $host; |
25
|
|
|
|
|
|
|
} |
26
|
|
|
|
|
|
|
else { |
27
|
1
|
|
|
|
|
3
|
push @re, $host . '[.][[:alpha:].]{2,6}'; |
28
|
|
|
|
|
|
|
} |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
1
|
|
|
|
|
10
|
my $re = join q[|], @re; |
32
|
|
|
|
|
|
|
|
33
|
1
|
|
|
1
|
|
94
|
$WEB2_HOST_RE = qr[($re)\z]smi; |
|
1
|
|
|
|
|
8
|
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
11
|
|
34
|
|
|
|
|
|
|
|
35
|
1
|
|
|
|
|
31391
|
return; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
0
|
|
|
0
|
0
|
0
|
sub web2_cfg ($self) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
39
|
0
|
|
|
|
|
0
|
return $WEB2_CFG; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
0
|
0
|
0
|
sub web2_load_default_cfg ( $self, $cfg, $merge = 1 ) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
43
|
0
|
|
|
|
|
0
|
$WEB2_CFG = P->cfg->load( $ENV->share->get('/data/web2.ini') ); |
44
|
|
|
|
|
|
|
|
45
|
0
|
|
|
|
|
0
|
undef $WEB2_HOST_RE; |
46
|
0
|
|
|
|
|
0
|
undef $WEB2_RE; |
47
|
|
|
|
|
|
|
|
48
|
0
|
|
|
|
|
0
|
return; |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
|
51
|
0
|
|
|
0
|
0
|
0
|
sub web2_load_cfg ( $self, $cfg, $merge = 1 ) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
52
|
0
|
0
|
|
|
|
0
|
$WEB2_CFG = {} if !$merge; |
53
|
|
|
|
|
|
|
|
54
|
0
|
|
|
|
|
0
|
for my $host ( keys $cfg->%* ) { |
55
|
0
|
|
|
|
|
0
|
$WEB2_CFG->{$host}->%* = $cfg->{$host}->%*; |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
0
|
undef $WEB2_HOST_RE; |
59
|
0
|
|
|
|
|
0
|
undef $WEB2_RE; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
0
|
return; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
18
|
|
|
18
|
|
124
|
sub _build__web2_data ($self) { |
|
18
|
|
|
|
|
23
|
|
|
18
|
|
|
|
|
20
|
|
65
|
18
|
100
|
|
|
|
41
|
_web2_compile() if !$WEB2_HOST_RE; |
66
|
|
|
|
|
|
|
|
67
|
18
|
|
|
|
|
27
|
my $res; |
68
|
|
|
|
|
|
|
|
69
|
18
|
100
|
|
|
|
277
|
if ( $self->host->canon =~ $WEB2_HOST_RE ) { |
70
|
16
|
|
|
|
|
39
|
my $web2_domain = $1; |
71
|
|
|
|
|
|
|
|
72
|
16
|
100
|
|
|
|
48
|
my $web2_id = exists $WEB2_CFG->{$web2_domain} ? $web2_domain : $web2_domain =~ s/[.].+\z//smr; |
73
|
|
|
|
|
|
|
|
74
|
16
|
100
|
|
|
|
197
|
if ( $WEB2_CFG->{$web2_id}->{path} ) { |
|
|
100
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
# path-based web2 url must not contain subdomain and must have nont empty path |
77
|
5
|
100
|
66
|
|
|
70
|
if ( $self->host->canon eq $web2_domain && $self->path =~ m[\A(/[^/]+)/?]sm ) { |
78
|
3
|
|
|
|
|
14
|
$res = [ $web2_id, $web2_domain, $web2_domain . $1 . q[/] ]; |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
elsif ( $self->host->canon =~ /([^.]+[.]\Q$web2_domain\E)\z/sm ) { |
82
|
5
|
|
|
|
|
165
|
$res = [ $web2_id, $web2_domain, $1 ]; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
} |
85
|
|
|
|
|
|
|
|
86
|
18
|
|
|
|
|
450
|
return $res; |
87
|
|
|
|
|
|
|
} |
88
|
|
|
|
|
|
|
|
89
|
0
|
|
|
0
|
|
0
|
sub _build_web2_domain ($self) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
90
|
0
|
0
|
|
|
|
0
|
if ( my $web2_data = $self->_web2_data ) { |
91
|
0
|
|
|
|
|
0
|
return $web2_data->[1]; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
else { |
94
|
0
|
|
|
|
|
0
|
return; |
95
|
|
|
|
|
|
|
} |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
0
|
|
|
0
|
|
0
|
sub _build_web2_id ($self) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
99
|
0
|
0
|
|
|
|
0
|
if ( my $web2_data = $self->_web2_data ) { |
100
|
0
|
|
|
|
|
0
|
return $web2_data->[0]; |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
else { |
103
|
0
|
|
|
|
|
0
|
return; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
|
107
|
18
|
|
|
18
|
|
552
|
sub _build_is_web2 ($self) { |
|
18
|
|
|
|
|
22
|
|
|
18
|
|
|
|
|
20
|
|
108
|
18
|
100
|
|
|
|
234
|
if ( my $web2_data = $self->_web2_data ) { |
109
|
8
|
|
|
|
|
151
|
return 1; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
else { |
112
|
10
|
|
|
|
|
183
|
return 0; |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
0
|
|
|
0
|
|
0
|
sub _build_web2_canon ($self) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
117
|
0
|
0
|
|
|
|
0
|
if ( my $web2_data = $self->_web2_data ) { |
118
|
0
|
|
|
|
|
0
|
return $web2_data->[2]; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
else { |
121
|
0
|
|
|
|
|
0
|
return; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
# NOTE http request must be performed with recursion enabled |
126
|
0
|
|
|
0
|
0
|
0
|
sub web2_check_available ( $self, $http_res ) { |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
127
|
0
|
0
|
|
|
|
0
|
return undef if !$self->is_web2; ## no critic qw[Subroutines::ProhibitExplicitReturnUndef] |
128
|
|
|
|
|
|
|
|
129
|
0
|
0
|
|
|
|
0
|
return undef if !$http_res->body; ## no critic qw[Subroutines::ProhibitExplicitReturnUndef] |
130
|
|
|
|
|
|
|
|
131
|
0
|
|
|
|
|
0
|
my $web2_id = $self->web2_id; |
132
|
|
|
|
|
|
|
|
133
|
0
|
|
|
|
|
0
|
my $cfg = $WEB2_CFG->{$web2_id}; |
134
|
|
|
|
|
|
|
|
135
|
0
|
0
|
0
|
|
|
0
|
if ( $cfg->{status} && $http_res->status == $cfg->{status} ) { return 1 } |
|
0
|
|
|
|
|
0
|
|
136
|
|
|
|
|
|
|
|
137
|
0
|
0
|
|
|
|
0
|
if ( $WEB2_RE->{$web2_id} ) { |
138
|
0
|
|
|
|
|
0
|
eval { decode_utf8 $http_res->body->$* }; |
|
0
|
|
|
|
|
0
|
|
139
|
|
|
|
|
|
|
|
140
|
0
|
0
|
|
|
|
0
|
return 1 if $http_res->body->$* =~ $WEB2_RE->{$web2_id}; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
0
|
return 0; |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
1; |
147
|
|
|
|
|
|
|
## -----SOURCE FILTER LOG BEGIN----- |
148
|
|
|
|
|
|
|
## |
149
|
|
|
|
|
|
|
## PerlCritic profile "pcore-script" policy violations: |
150
|
|
|
|
|
|
|
## +------+----------------------+----------------------------------------------------------------------------------------------------------------+ |
151
|
|
|
|
|
|
|
## | Sev. | Lines | Policy | |
152
|
|
|
|
|
|
|
## |======+======================+================================================================================================================| |
153
|
|
|
|
|
|
|
## | 3 | 138 | ErrorHandling::RequireCheckingReturnValueOfEval - Return value of eval not tested | |
154
|
|
|
|
|
|
|
## +------+----------------------+----------------------------------------------------------------------------------------------------------------+ |
155
|
|
|
|
|
|
|
## |
156
|
|
|
|
|
|
|
## -----SOURCE FILTER LOG END----- |
157
|
|
|
|
|
|
|
__END__ |
158
|
|
|
|
|
|
|
=pod |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
=encoding utf8 |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head1 NAME |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Pcore::Util::URI::Web2 |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 SYNOPSIS |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head1 DESCRIPTION |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
=head1 METHODS |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head1 SEE ALSO |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=cut |