line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::Host; |
2
|
5
|
|
|
5
|
|
3680
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
33
|
|
3
|
5
|
|
|
5
|
|
949
|
use Carp (); |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
61
|
|
4
|
5
|
|
|
5
|
|
19
|
use Mojo::Util (); |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
2849
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.01'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
sub register { |
9
|
27
|
|
|
27
|
1
|
14394
|
my (undef, $app, $config) = @_; |
10
|
|
|
|
|
|
|
|
11
|
27
|
|
|
|
|
56
|
my ($helper, $www) = _parse_and_validate_config($config); |
12
|
12
|
|
|
|
|
18
|
my $sub; |
13
|
12
|
100
|
|
|
|
38
|
if (not defined $www) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
14
|
6
|
|
|
6
|
|
20
|
$sub = sub { $_[0]->req->url->to_abs->host }; |
|
6
|
|
|
|
|
61830
|
|
15
|
|
|
|
|
|
|
} elsif ($www eq 'always') { |
16
|
|
|
|
|
|
|
$sub = sub { |
17
|
4
|
|
|
4
|
|
36872
|
my $host = $_[0]->req->url->to_abs->host; |
18
|
4
|
100
|
|
|
|
817
|
if (index($host, 'www.') != 0) { |
19
|
2
|
|
|
|
|
7
|
$host = "www.$host"; |
20
|
|
|
|
|
|
|
} |
21
|
|
|
|
|
|
|
|
22
|
4
|
|
|
|
|
20
|
return $host; |
23
|
3
|
|
|
|
|
11
|
}; |
24
|
|
|
|
|
|
|
} elsif ($www eq 'never') { |
25
|
|
|
|
|
|
|
$sub = sub { |
26
|
4
|
|
|
4
|
|
35263
|
my $host = $_[0]->req->url->to_abs->host; |
27
|
4
|
100
|
|
|
|
848
|
if (index($host, 'www.') == 0) { |
28
|
2
|
|
|
|
|
7
|
substr $host, 0, 4, ''; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
4
|
|
|
|
|
18
|
return $host; |
32
|
3
|
|
|
|
|
12
|
}; |
33
|
|
|
|
|
|
|
} else { |
34
|
0
|
|
|
|
|
0
|
Carp::croak qq{unknown value provided for www: '$www'}; |
35
|
|
|
|
|
|
|
} |
36
|
|
|
|
|
|
|
|
37
|
12
|
|
|
|
|
61
|
$app->helper($helper => $sub); |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
sub _parse_and_validate_config { |
41
|
27
|
|
|
27
|
|
43
|
my ($config) = @_; |
42
|
|
|
|
|
|
|
|
43
|
27
|
|
|
|
|
36
|
my $helper; |
44
|
27
|
100
|
|
|
|
60
|
if (exists $config->{helper}) { |
45
|
10
|
|
|
|
|
19
|
$helper = delete $config->{helper}; |
46
|
|
|
|
|
|
|
|
47
|
10
|
|
|
|
|
29
|
my $ref = ref $helper; |
48
|
10
|
100
|
|
|
|
47
|
Carp::croak qq{helper must be a string, but was '$ref'} if $ref; |
49
|
8
|
100
|
100
|
|
|
49
|
Carp::croak 'helper must be non-empty' if not defined $helper or $helper eq ''; |
50
|
|
|
|
|
|
|
} else { |
51
|
17
|
|
|
|
|
25
|
$helper = 'host'; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
23
|
|
|
|
|
28
|
my $www; |
55
|
23
|
100
|
|
|
|
51
|
if (exists $config->{www}) { |
56
|
15
|
|
|
|
|
26
|
$www = delete $config->{www}; |
57
|
|
|
|
|
|
|
|
58
|
15
|
|
|
|
|
22
|
my $ref = ref $www; |
59
|
15
|
100
|
|
|
|
46
|
Carp::croak qq{www must be a string, but was '$ref'} if $ref; |
60
|
13
|
100
|
100
|
|
|
66
|
Carp::croak 'www must be non-empty' if not defined $www or $www eq ''; |
61
|
|
|
|
|
|
|
Carp::croak qq{www must be either 'always' or 'never', but was '$www'} |
62
|
11
|
100
|
|
|
|
22
|
unless grep { $www eq $_ } 'always', 'never'; |
|
22
|
|
|
|
|
72
|
|
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
16
|
100
|
|
|
|
37
|
Carp::croak 'unknown keys/values: ' . Mojo::Util::dumper $config if %$config; |
66
|
|
|
|
|
|
|
|
67
|
12
|
|
|
|
|
29
|
return $helper, $www; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
1; |
71
|
|
|
|
|
|
|
__END__ |