line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Mojolicious::Plugin::HostMeta;
|
2
|
1
|
|
|
1
|
|
1511
|
use Mojo::Base 'Mojolicious::Plugin';
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
8
|
|
3
|
1
|
|
|
1
|
|
270
|
use Mojo::Headers;
|
|
1
|
|
|
|
|
10
|
|
|
1
|
|
|
|
|
10
|
|
4
|
1
|
|
|
1
|
|
32
|
use Mojo::Util qw/quote/;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1794
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.24';
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $WK_PATH = '/.well-known/host-meta';
|
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Register plugin
|
12
|
|
|
|
|
|
|
sub register {
|
13
|
1
|
|
|
1
|
1
|
42
|
my ($plugin, $app, $param) = @_;
|
14
|
|
|
|
|
|
|
|
15
|
1
|
|
50
|
|
|
5
|
$param ||= {};
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Load parameter from Config file
|
18
|
1
|
50
|
|
|
|
13
|
if (my $config_param = $app->config('HostMeta')) {
|
19
|
0
|
|
|
|
|
0
|
$param = { %$param, %$config_param };
|
20
|
|
|
|
|
|
|
};
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Get helpers object
|
23
|
1
|
|
|
|
|
24
|
my $helpers = $app->renderer->helpers;
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Load Util-Endpoint/Callback if not already loaded
|
26
|
1
|
|
|
|
|
16
|
foreach (qw/Endpoint Callback/) {
|
27
|
2
|
50
|
|
|
|
3558
|
$app->plugin("Util::$_") unless exists $helpers->{ lc $_ };
|
28
|
|
|
|
|
|
|
};
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# Load XML if not already loaded
|
31
|
1
|
50
|
|
|
|
1540
|
unless (exists $helpers->{new_xrd}) {
|
32
|
1
|
|
|
|
|
5
|
$app->plugin('XRD');
|
33
|
|
|
|
|
|
|
};
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
# Set callbacks on registration
|
36
|
1
|
|
|
|
|
19251
|
$app->callback(fetch_hostmeta => $param);
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Get seconds to expiration
|
39
|
1
|
|
|
|
|
181
|
my $seconds = (60 * 60 * 24 * 10);
|
40
|
1
|
50
|
33
|
|
|
5
|
if ($param->{expires} && $param->{expires} =~ /^\d+$/) {
|
41
|
0
|
|
|
|
|
0
|
$seconds = delete $param->{expires};
|
42
|
|
|
|
|
|
|
};
|
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
# Create new hostmeta document
|
45
|
1
|
|
|
|
|
7
|
my $hostmeta = $app->new_xrd;
|
46
|
1
|
|
|
|
|
362
|
$hostmeta->extension( -HostMeta );
|
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
# Get host information on first request
|
49
|
|
|
|
|
|
|
$app->hook(
|
50
|
|
|
|
|
|
|
prepare_hostmeta =>
|
51
|
|
|
|
|
|
|
sub {
|
52
|
1
|
|
|
1
|
|
13
|
my ($c, $hostmeta) = @_;
|
53
|
1
|
|
|
|
|
3
|
my $host = $c->req->url->to_abs->host;
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
# Add host-information to host-meta
|
56
|
1
|
50
|
|
|
|
316
|
$hostmeta->host( $host ) if $host;
|
57
|
|
|
|
|
|
|
}
|
58
|
1
|
|
|
|
|
1204
|
);
|
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
# Establish 'hostmeta' helper
|
61
|
|
|
|
|
|
|
$app->helper(
|
62
|
|
|
|
|
|
|
hostmeta => sub {
|
63
|
11
|
|
|
11
|
|
82414
|
my $c = shift;
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
# Undefined host name
|
66
|
11
|
100
|
|
|
|
38
|
shift if !defined $_[0];
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
# Host name is provided
|
69
|
11
|
100
|
100
|
|
|
90
|
if (!$_[0] || ref $_[0]) {
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
# Return local hostmeta
|
72
|
9
|
|
|
|
|
36
|
return _serve_hostmeta( $c, $hostmeta, @_ );
|
73
|
|
|
|
|
|
|
};
|
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
# Return discovered hostmeta
|
76
|
2
|
|
|
|
|
9
|
return _fetch_hostmeta( $c, @_ );
|
77
|
1
|
|
|
|
|
25
|
});
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
# Establish /.well-known/host-meta route
|
80
|
1
|
|
|
|
|
108
|
my $route = $app->routes->route( $WK_PATH );
|
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
# Define endpoint
|
83
|
1
|
|
|
|
|
747
|
$route->endpoint('host-meta');
|
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
# Set route callback
|
86
|
|
|
|
|
|
|
$route->to(
|
87
|
|
|
|
|
|
|
cb => sub {
|
88
|
4
|
|
|
4
|
|
89762
|
my $c = shift;
|
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
# Seconds given
|
91
|
4
|
50
|
|
|
|
16
|
if ($seconds) {
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
# Set cache control
|
94
|
4
|
|
|
|
|
16
|
my $headers = $c->res->headers;
|
95
|
4
|
|
|
|
|
88
|
$headers->cache_control(
|
96
|
|
|
|
|
|
|
"public, max-age=$seconds"
|
97
|
|
|
|
|
|
|
);
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
# Set expires element
|
100
|
4
|
|
|
|
|
40
|
$hostmeta->expires( time + $seconds );
|
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
# Set expires header
|
103
|
4
|
|
|
|
|
21568
|
$headers->expires( $hostmeta->expires );
|
104
|
|
|
|
|
|
|
};
|
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
# Serve host-meta document
|
107
|
4
|
|
|
|
|
3505
|
return $c->helpers->reply->xrd(
|
108
|
|
|
|
|
|
|
_serve_hostmeta( $c, $hostmeta )
|
109
|
|
|
|
|
|
|
);
|
110
|
1
|
|
|
|
|
48
|
});
|
111
|
|
|
|
|
|
|
};
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
# Get HostMeta document
|
115
|
|
|
|
|
|
|
sub _fetch_hostmeta {
|
116
|
2
|
|
|
2
|
|
4
|
my $c = shift;
|
117
|
2
|
|
|
|
|
7
|
my $host = lc shift;
|
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
# Trim tail
|
120
|
2
|
|
66
|
|
|
11
|
pop while @_ && !defined $_[-1];
|
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
# Get headers
|
123
|
2
|
|
|
|
|
6
|
my $header = {};
|
124
|
2
|
50
|
66
|
|
|
13
|
if ($_[0] && ref $_[0] && ref($_[0]) eq 'HASH') {
|
|
|
|
66
|
|
|
|
|
125
|
0
|
|
|
|
|
0
|
$header = shift;
|
126
|
|
|
|
|
|
|
};
|
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
# Check if security is forced
|
129
|
2
|
50
|
66
|
|
|
11
|
my $secure = defined $_[-1] && $_[-1] eq '-secure' ? pop : 0;
|
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
# Get callback
|
132
|
2
|
100
|
66
|
|
|
24
|
my $cb = pop if ref($_[-1]) && ref($_[-1]) eq 'CODE';
|
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
# Get host information
|
135
|
2
|
50
|
|
|
|
34
|
unless ($host =~ s!^\s*(?:http(s?)://)?([^/]+)/*\s*$!$2!) {
|
136
|
0
|
|
|
|
|
0
|
return;
|
137
|
|
|
|
|
|
|
};
|
138
|
2
|
50
|
|
|
|
12
|
$secure = 1 if $1;
|
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
# Build relations parameter
|
141
|
2
|
|
|
|
|
4
|
my $rel;
|
142
|
2
|
50
|
33
|
|
|
8
|
$rel = shift if $_[0] && ref($_[0]) eq 'ARRAY';
|
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
# Helpers proxy
|
145
|
2
|
|
|
|
|
8
|
my $h = $c->helpers;
|
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
# Callback for caching
|
148
|
2
|
|
|
|
|
58
|
my ($xrd, $headers) = $h->callback(
|
149
|
|
|
|
|
|
|
fetch_hostmeta => $host
|
150
|
|
|
|
|
|
|
);
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
# HostMeta document was cached
|
153
|
2
|
50
|
|
|
|
5112
|
if ($xrd) {
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
# Filter relations
|
156
|
2
|
50
|
|
|
|
16
|
$xrd = $xrd->filter_rel( $rel ) if $rel;
|
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
# Set headers to default
|
159
|
2
|
100
|
33
|
|
|
18
|
$headers ||= Mojo::Headers->new if $cb || wantarray;
|
|
|
|
66
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
# Return cached hostmeta document
|
162
|
2
|
100
|
|
|
|
33
|
return $cb->( $xrd, $headers ) if $cb;
|
163
|
1
|
50
|
|
|
|
4
|
return ( $xrd, $headers ) if wantarray;
|
164
|
1
|
|
|
|
|
6
|
return $xrd;
|
165
|
|
|
|
|
|
|
};
|
166
|
|
|
|
|
|
|
|
167
|
|
|
|
|
|
|
# Create host-meta path
|
168
|
0
|
|
|
|
|
0
|
my $path = '//' . $host . $WK_PATH;
|
169
|
0
|
0
|
|
|
|
0
|
$path = 'https:' . $path if $secure;
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
# Non-blocking
|
173
|
0
|
0
|
|
|
|
0
|
if ($cb) {
|
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
return $h->get_xrd(
|
176
|
|
|
|
|
|
|
$path => $header => sub {
|
177
|
0
|
|
|
0
|
|
0
|
my ($xrd, $headers) = @_;
|
178
|
0
|
0
|
|
|
|
0
|
if ($xrd) {
|
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
# Add hostmeta extension
|
181
|
0
|
|
|
|
|
0
|
$xrd->extension(-HostMeta);
|
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
# Hook for caching
|
184
|
0
|
|
|
|
|
0
|
$c->app->plugins->emit_hook(
|
185
|
|
|
|
|
|
|
after_fetching_hostmeta => (
|
186
|
|
|
|
|
|
|
$c, $host, $xrd, $headers
|
187
|
|
|
|
|
|
|
)
|
188
|
|
|
|
|
|
|
);
|
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
# Filter based on relations
|
191
|
0
|
0
|
|
|
|
0
|
$xrd = $xrd->filter_rel( $rel ) if $rel;
|
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
# Send to callback
|
194
|
0
|
|
|
|
|
0
|
return $cb->( $xrd, $headers );
|
195
|
|
|
|
|
|
|
};
|
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
# Fail
|
198
|
0
|
|
|
|
|
0
|
return $cb->();
|
199
|
0
|
|
|
|
|
0
|
});
|
200
|
|
|
|
|
|
|
};
|
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
# Blocking
|
203
|
0
|
|
|
|
|
0
|
($xrd, $headers) = $h->get_xrd( $path => $header );
|
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
# No host-meta found
|
206
|
0
|
0
|
|
|
|
0
|
return unless $xrd;
|
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
# Add hostmeta extension
|
209
|
0
|
|
|
|
|
0
|
$xrd->extension( -HostMeta );
|
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
# Hook for caching
|
212
|
0
|
|
|
|
|
0
|
$c->app->plugins->emit_hook(
|
213
|
|
|
|
|
|
|
after_fetching_hostmeta => (
|
214
|
|
|
|
|
|
|
$c, $host, $xrd, $headers
|
215
|
|
|
|
|
|
|
)
|
216
|
|
|
|
|
|
|
);
|
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
# Filter based on relations
|
219
|
0
|
0
|
|
|
|
0
|
$xrd = $xrd->filter_rel( $rel ) if $rel;
|
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
# Return
|
222
|
0
|
0
|
|
|
|
0
|
return ($xrd, $headers) if wantarray;
|
223
|
0
|
|
|
|
|
0
|
return $xrd;
|
224
|
|
|
|
|
|
|
};
|
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
# Run hooks for preparation and serving of hostmeta
|
228
|
|
|
|
|
|
|
sub _serve_hostmeta {
|
229
|
13
|
|
|
13
|
|
142
|
my $c = shift;
|
230
|
13
|
|
|
|
|
24
|
my $xrd = shift;
|
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
# Delete tail
|
233
|
13
|
|
66
|
|
|
58
|
pop while @_ && !defined $_[-1];
|
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
# Ignore security flag
|
236
|
13
|
50
|
66
|
|
|
54
|
pop if defined $_[-1] && $_[-1] eq '-secure';
|
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
# Ignore header information
|
239
|
13
|
50
|
66
|
|
|
50
|
shift if $_[0] && ref($_[0]) && ref($_[0]) eq 'HASH';
|
|
|
|
66
|
|
|
|
|
240
|
|
|
|
|
|
|
|
241
|
|
|
|
|
|
|
# Get callback
|
242
|
13
|
100
|
100
|
|
|
45
|
my $cb = pop if ref($_[-1]) && ref($_[-1]) eq 'CODE';
|
243
|
|
|
|
|
|
|
|
244
|
13
|
|
|
|
|
22
|
my $rel = shift;
|
245
|
|
|
|
|
|
|
|
246
|
13
|
|
|
|
|
48
|
my $plugins = $c->app->plugins;
|
247
|
13
|
|
|
|
|
110
|
my $phm = 'prepare_hostmeta';
|
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
# prepare_hostmeta has subscribers
|
251
|
13
|
100
|
|
|
|
58
|
if ($plugins->has_subscribers( $phm )) {
|
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
# Emit hook for subscribers
|
254
|
2
|
|
|
|
|
18
|
$plugins->emit_hook( $phm => ( $c, $xrd ));
|
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
# Unsubscribe all subscribers
|
257
|
2
|
|
|
|
|
5959
|
foreach (@{ $plugins->subscribers( $phm ) }) {
|
|
2
|
|
|
|
|
10
|
|
258
|
2
|
|
|
|
|
19
|
$plugins->unsubscribe( $phm => $_ );
|
259
|
|
|
|
|
|
|
};
|
260
|
|
|
|
|
|
|
};
|
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
# No further modifications wanted
|
263
|
13
|
100
|
|
|
|
130
|
unless ($plugins->has_subscribers('before_serving_hostmeta')) {
|
264
|
|
|
|
|
|
|
|
265
|
|
|
|
|
|
|
# Filter relations
|
266
|
1
|
50
|
|
|
|
9
|
$xrd = $xrd->filter_rel( $rel ) if $rel;
|
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
# Return document
|
269
|
1
|
50
|
|
|
|
3
|
return $cb->( $xrd ) if $cb;
|
270
|
1
|
|
|
|
|
6
|
return $xrd;
|
271
|
|
|
|
|
|
|
};
|
272
|
|
|
|
|
|
|
|
273
|
|
|
|
|
|
|
# Clone hostmeta reference
|
274
|
12
|
|
|
|
|
91
|
$xrd = $c->helpers->new_xrd( $xrd->to_string );
|
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
# Emit 'before_serving_hostmeta' hook
|
277
|
12
|
|
|
|
|
11256
|
$plugins->emit_hook(
|
278
|
|
|
|
|
|
|
before_serving_hostmeta => (
|
279
|
|
|
|
|
|
|
$c, $xrd
|
280
|
|
|
|
|
|
|
));
|
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
# Filter relations
|
283
|
12
|
100
|
|
|
|
158920
|
$xrd = $xrd->filter_rel( $rel ) if $rel;
|
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
# Return hostmeta clone
|
286
|
12
|
100
|
|
|
|
9886
|
return $cb->( $xrd ) if $cb;
|
287
|
9
|
|
|
|
|
112
|
return $xrd;
|
288
|
|
|
|
|
|
|
};
|
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
1;
|
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
__END__
|