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