line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::TagHelpers::NoCaching; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
372864
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
13
|
|
4
|
2
|
|
|
2
|
|
314
|
use Mojo::URL; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
14
|
|
5
|
2
|
|
|
2
|
|
65
|
use File::Spec; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
41
|
|
6
|
2
|
|
|
2
|
|
10
|
use Cwd; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
2755
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub register { |
11
|
2
|
|
|
2
|
1
|
164
|
my ($plugin, $app, $cfg) = @_; |
12
|
|
|
|
|
|
|
|
13
|
2
|
100
|
|
|
|
13
|
$cfg->{key} = 'nc' unless defined $cfg->{key}; |
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
|
|
17
|
$plugin->{url2path} = {}; |
16
|
2
|
|
|
|
|
5
|
$plugin->{path2key} = {}; |
17
|
2
|
|
|
|
|
4
|
$plugin->{cfg} = $cfg; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$app->helper(stylesheet_nc => sub { |
20
|
6
|
|
|
6
|
|
130569
|
my $self = shift; |
21
|
|
|
|
|
|
|
|
22
|
6
|
50
|
|
|
|
31
|
if (@_ % 2) { |
23
|
|
|
|
|
|
|
# this is css url |
24
|
6
|
|
|
|
|
27
|
my $href = $plugin->_nc_href($self, shift); |
25
|
6
|
|
|
|
|
1691
|
unshift @_, $href; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
6
|
|
|
|
|
67
|
$self->stylesheet(@_); |
29
|
2
|
|
|
|
|
19
|
}); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$app->helper(javascript_nc => sub { |
32
|
5
|
|
|
5
|
|
71427
|
my $self = shift; |
33
|
|
|
|
|
|
|
|
34
|
5
|
50
|
|
|
|
27
|
if (@_ % 2) { |
35
|
|
|
|
|
|
|
# this is script url |
36
|
5
|
|
|
|
|
25
|
my $href = $plugin->_nc_href($self, shift); |
37
|
5
|
|
|
|
|
844
|
unshift @_, $href; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
5
|
|
|
|
|
49
|
$self->javascript(@_); |
41
|
2
|
|
|
|
|
218
|
}); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$app->helper(image_nc => sub { |
44
|
4
|
|
|
4
|
|
4792
|
my $self = shift; |
45
|
|
|
|
|
|
|
|
46
|
4
|
|
|
|
|
13
|
my $href = $plugin->_nc_href($self, shift); |
47
|
4
|
|
|
|
|
244
|
unshift @_, $href; |
48
|
|
|
|
|
|
|
|
49
|
4
|
|
|
|
|
288
|
$self->image(@_); |
50
|
2
|
|
|
|
|
187
|
}); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _href2absolute { |
54
|
15
|
|
|
15
|
|
23
|
my ($controller, $href) = @_; |
55
|
|
|
|
|
|
|
|
56
|
15
|
|
|
|
|
48
|
$href =~ s/\?.+//; # query params |
57
|
15
|
100
|
|
|
|
73
|
if ($href =~ m!^(?:/|[a-z]+://)!i) { |
58
|
|
|
|
|
|
|
# absolute |
59
|
6
|
|
|
|
|
20
|
return $href; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
9
|
|
|
|
|
35
|
my $path = $controller->req->url->path; |
63
|
9
|
|
|
|
|
1014
|
$path =~ s![^/]+$!!; |
64
|
9
|
50
|
|
|
|
818
|
$path = '/' if $path eq ''; |
65
|
9
|
|
|
|
|
159
|
$href = $path . $href; |
66
|
|
|
|
|
|
|
|
67
|
9
|
|
|
|
|
220
|
return $href; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub _href2filepath { |
71
|
12
|
|
|
12
|
|
22
|
my ($controller, $href) = @_; |
72
|
|
|
|
|
|
|
|
73
|
12
|
50
|
|
|
|
43
|
if ($href =~ m!^[a-z]+://!i) { |
74
|
0
|
|
|
|
|
0
|
my $url = Mojo::URL->new($href); |
75
|
0
|
|
|
|
|
0
|
my $c_url = $controller->req->url->to_abs; |
76
|
|
|
|
|
|
|
|
77
|
0
|
0
|
|
|
|
0
|
if ($url->host ne $c_url->host) { |
78
|
|
|
|
|
|
|
# external url |
79
|
0
|
|
|
|
|
0
|
return; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
0
|
|
|
|
|
0
|
$href = $url->path; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
12
|
|
|
|
|
273
|
my $static =$controller->app->static; |
86
|
12
|
100
|
|
|
|
456
|
my $asset = $static->file($href) |
87
|
|
|
|
|
|
|
or return; |
88
|
|
|
|
|
|
|
|
89
|
8
|
50
|
|
|
|
1427
|
$asset->is_file |
90
|
|
|
|
|
|
|
or return; |
91
|
|
|
|
|
|
|
|
92
|
8
|
|
50
|
|
|
231
|
my $path = File::Spec->canonpath(Cwd::realpath($asset->path)||return); |
93
|
8
|
|
|
|
|
1001
|
my $ok; |
94
|
8
|
|
|
|
|
141
|
for my $p (@{$static->paths}) { |
|
8
|
|
|
|
|
192
|
|
95
|
8
|
100
|
|
|
|
88
|
$ok = index($path, $p) == 0 |
96
|
|
|
|
|
|
|
and last; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
# check is found file is inside public directory |
99
|
8
|
100
|
|
|
|
26
|
$ok or return; |
100
|
|
|
|
|
|
|
|
101
|
7
|
|
|
|
|
45
|
return $path; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _nc_key { |
105
|
7
|
|
|
7
|
|
14
|
my ($self, $path) = @_; |
106
|
7
|
|
|
|
|
309
|
return (stat($path))[9]; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _nc_href { |
110
|
15
|
|
|
15
|
|
32
|
my ($self, $controller, $href) = @_; |
111
|
|
|
|
|
|
|
|
112
|
15
|
|
|
|
|
42
|
my $abs_href = _href2absolute($controller, $href); |
113
|
|
|
|
|
|
|
|
114
|
15
|
100
|
|
|
|
60
|
unless (exists $self->{url2path}{$abs_href}) { |
115
|
12
|
|
|
|
|
37
|
$self->{url2path}{$abs_href} = _href2filepath($controller, $abs_href); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
15
|
100
|
|
|
|
1196
|
my $path = $self->{url2path}{$abs_href} |
119
|
|
|
|
|
|
|
or return $href; |
120
|
|
|
|
|
|
|
|
121
|
8
|
100
|
|
|
|
32
|
unless (exists $self->{path2key}{$path}) { |
122
|
7
|
|
|
|
|
39
|
$self->{path2key}{$path} = $self->_nc_key($path); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
8
|
50
|
|
|
|
37
|
my $key = $self->{path2key}{$path} |
126
|
|
|
|
|
|
|
or return $href; |
127
|
|
|
|
|
|
|
|
128
|
8
|
100
|
|
|
|
38
|
$href .= index($href, '?') == -1 ? '?' : '&'; |
129
|
8
|
|
|
|
|
31
|
$href .= $self->{cfg}{key} . '=' . $key; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# fix for https://github.com/kraih/mojo/issues/565 |
132
|
8
|
|
|
|
|
57
|
return Mojo::URL->new($href); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
1; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
__END__ |