line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::TagHelpers::NoCaching; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
240774
|
use Mojo::Base 'Mojolicious::Plugin'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
10
|
|
4
|
2
|
|
|
2
|
|
252
|
use Mojo::URL; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
17
|
|
5
|
2
|
|
|
2
|
|
48
|
use File::Spec; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
86
|
|
6
|
2
|
|
|
2
|
|
7
|
use Cwd; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
2042
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $VERSION = '0.05'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
sub register { |
11
|
2
|
|
|
2
|
1
|
77
|
my ($plugin, $app, $cfg) = @_; |
12
|
|
|
|
|
|
|
|
13
|
2
|
100
|
|
|
|
9
|
$cfg->{key} = 'nc' unless defined $cfg->{key}; |
14
|
|
|
|
|
|
|
|
15
|
2
|
|
|
|
|
11
|
$plugin->{url2path} = {}; |
16
|
2
|
|
|
|
|
4
|
$plugin->{path2key} = {}; |
17
|
2
|
|
|
|
|
3
|
$plugin->{cfg} = $cfg; |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
$app->helper(stylesheet_nc => sub { |
20
|
6
|
|
|
6
|
|
101848
|
my $self = shift; |
21
|
|
|
|
|
|
|
|
22
|
6
|
50
|
|
|
|
32
|
if (@_ % 2) { |
23
|
|
|
|
|
|
|
# this is css url |
24
|
6
|
|
|
|
|
21
|
my $href = $plugin->_nc_href($self, shift); |
25
|
6
|
|
|
|
|
975
|
unshift @_, $href; |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
|
28
|
6
|
|
|
|
|
62
|
$self->stylesheet(@_); |
29
|
2
|
|
|
|
|
14
|
}); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
$app->helper(javascript_nc => sub { |
32
|
5
|
|
|
5
|
|
43454
|
my $self = shift; |
33
|
|
|
|
|
|
|
|
34
|
5
|
50
|
|
|
|
24
|
if (@_ % 2) { |
35
|
|
|
|
|
|
|
# this is script url |
36
|
5
|
|
|
|
|
21
|
my $href = $plugin->_nc_href($self, shift); |
37
|
5
|
|
|
|
|
431
|
unshift @_, $href; |
38
|
|
|
|
|
|
|
} |
39
|
|
|
|
|
|
|
|
40
|
5
|
|
|
|
|
49
|
$self->javascript(@_); |
41
|
2
|
|
|
|
|
149
|
}); |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$app->helper(image_nc => sub { |
44
|
4
|
|
|
4
|
|
3305
|
my $self = shift; |
45
|
|
|
|
|
|
|
|
46
|
4
|
|
|
|
|
13
|
my $href = $plugin->_nc_href($self, shift); |
47
|
4
|
|
|
|
|
174
|
unshift @_, $href; |
48
|
|
|
|
|
|
|
|
49
|
4
|
|
|
|
|
53
|
$self->image(@_); |
50
|
2
|
|
|
|
|
108
|
}); |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
sub _href2absolute { |
54
|
15
|
|
|
15
|
|
21
|
my ($controller, $href) = @_; |
55
|
|
|
|
|
|
|
|
56
|
15
|
|
|
|
|
36
|
$href =~ s/\?.+//; # query params |
57
|
15
|
100
|
|
|
|
70
|
if ($href =~ m!^(?:/|[a-z]+://)!i) { |
58
|
|
|
|
|
|
|
# absolute |
59
|
6
|
|
|
|
|
15
|
return $href; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
9
|
|
|
|
|
41
|
my $path = $controller->req->url->path; |
63
|
9
|
|
|
|
|
878
|
$path =~ s![^/]+$!!; |
64
|
9
|
50
|
|
|
|
690
|
$path = '/' if $path eq ''; |
65
|
9
|
|
|
|
|
201
|
$href = $path . $href; |
66
|
|
|
|
|
|
|
|
67
|
9
|
|
|
|
|
150
|
return $href; |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub _href2filepath { |
71
|
12
|
|
|
12
|
|
17
|
my ($controller, $href) = @_; |
72
|
|
|
|
|
|
|
|
73
|
12
|
50
|
|
|
|
37
|
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
|
|
|
|
|
253
|
my $static =$controller->app->static; |
86
|
12
|
100
|
|
|
|
390
|
my $asset = $static->file($href) |
87
|
|
|
|
|
|
|
or return; |
88
|
|
|
|
|
|
|
|
89
|
8
|
50
|
|
|
|
742
|
$asset->is_file |
90
|
|
|
|
|
|
|
or return; |
91
|
|
|
|
|
|
|
|
92
|
8
|
|
50
|
|
|
220
|
my $path = File::Spec->canonpath(Cwd::realpath($asset->path)||return); |
93
|
8
|
|
|
|
|
888
|
my $ok; |
94
|
8
|
|
|
|
|
12
|
for my $p (@{$static->paths}) { |
|
8
|
|
|
|
|
197
|
|
95
|
8
|
100
|
|
|
|
83
|
$ok = index($path, $p) == 0 |
96
|
|
|
|
|
|
|
and last; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
# check is found file is inside public directory |
99
|
8
|
100
|
|
|
|
24
|
$ok or return; |
100
|
|
|
|
|
|
|
|
101
|
7
|
|
|
|
|
38
|
return $path; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub _nc_key { |
105
|
7
|
|
|
7
|
|
12
|
my ($self, $path) = @_; |
106
|
7
|
|
|
|
|
152
|
return (stat($path))[9]; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub _nc_href { |
110
|
15
|
|
|
15
|
|
28
|
my ($self, $controller, $href) = @_; |
111
|
|
|
|
|
|
|
|
112
|
15
|
|
|
|
|
31
|
my $abs_href = _href2absolute($controller, $href); |
113
|
|
|
|
|
|
|
|
114
|
15
|
100
|
|
|
|
59
|
unless (exists $self->{url2path}{$abs_href}) { |
115
|
12
|
|
|
|
|
27
|
$self->{url2path}{$abs_href} = _href2filepath($controller, $abs_href); |
116
|
|
|
|
|
|
|
} |
117
|
|
|
|
|
|
|
|
118
|
15
|
100
|
|
|
|
915
|
my $path = $self->{url2path}{$abs_href} |
119
|
|
|
|
|
|
|
or return $href; |
120
|
|
|
|
|
|
|
|
121
|
8
|
100
|
|
|
|
28
|
unless (exists $self->{path2key}{$path}) { |
122
|
7
|
|
|
|
|
21
|
$self->{path2key}{$path} = $self->_nc_key($path); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
8
|
50
|
|
|
|
32
|
my $key = $self->{path2key}{$path} |
126
|
|
|
|
|
|
|
or return $href; |
127
|
|
|
|
|
|
|
|
128
|
8
|
100
|
|
|
|
36
|
$href .= index($href, '?') == -1 ? '?' : '&'; |
129
|
8
|
|
|
|
|
33
|
$href .= $self->{cfg}{key} . '=' . $key; |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# fix for https://github.com/kraih/mojo/issues/565 |
132
|
8
|
|
|
|
|
45
|
return Mojo::URL->new($href); |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
1; |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
__END__ |