blib/lib/Mojolicious/Plugin/WebFinger.pm

filter: Statementstmt 53.4% 85 / 159 Branchbran 24.2% 31 / 128 Conditioncond 37.1% 29 / 78 MC/DCmcdc 0.0% 0 / 45 Subroutinesub 60.0% 9 / 15 Podpod 100% 1 / 1 totaltotal 36.3% 155 / 426 CC 93 SCAR 64.3
CC
93
Cov
40%
_fetch_webfinger
1336.8
__ANON__
24.8
__ANON__
20.0
CRAP 620.6
↑ Summary
1 package Mojolicious::Plugin::WebFinger;
2 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Mojo::Util 'url_escape';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mojo::URL;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # Todo:
7 # - Make callback non-blocking aware
8 # - Support 307 Temporary Redirect as described in the spec
9 # - Support simple startup defintion, like
10 # plugin WebFinger => {
11 # 'akron@sojolicious' => {
12 # describedby => {
13 # type => 'application/rdf+xml',
14 # href => 'http://sojolicio.us/akron.foaf'
15 # }
16 # }
17 # };
18  
19 1 our $VERSION = '0.13';
20  
21  
22 1 my $WK_PATH = '/.well-known/webfinger';
23  
24 # Register Plugin
25 1 ▶sub register {
Subroutine register: called CC=6 SCAR=19.3
Pod documented
26 1 my ($plugin, $mojo, $param) = @_;
27  
28 # Plugin parameter
29 1 ▶ $param ||= {};
Condition50%1/2
$param ||= {}
l!l
10
MC/DC0%0/1
$param ||= {}
$param
Truth table50%1/2
A
A= $param
Aresult
11
00
30  
31 # Load parameter from Config file
32 1 ▶ if (my $config_param = $mojo->config('WebFinger')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $config_param = $mojo->config("WebFinger")) 0 1 1
33 0 $param = { %$param, %$config_param };
34 };
35  
36 # Load HostMeta if not already loaded.
37 # This automatically loads XRD,
38 # Util::Endpoint and Util::Callback plugins.
39 1 ▶ unless (exists $mojo->renderer->helpers->{hostmeta}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $mojo->renderer->helpers->{"hostmeta"}) 1 0 1
40 1 $mojo->plugin('HostMeta');
41 };
42  
43 # Check for 'prepare_webfinger' and 'fetch_webfinger' callback
44 1 $mojo->callback(
45 [qw/fetch_webfinger prepare_webfinger/],
46 $param,
47 -once
48 );
49  
50 # Get seconds to expiration
51 1 my $seconds = (60 * 60 * 24 * 10);
52 1 ▶ if ($param->{expires} && $param->{expires} =~ /^\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"expires"} and $param->{"expires"} =~ /^\d+$/u) 0 1 1
Condition33%1/3
$param->{"expires"} and $param->{"expires"} =~ /^\d+$/u
!ll&&!rl&&r
100
MC/DC0%0/2
$param->{"expires"} and $param->{"expires"} =~ /^\d+$/u
$param->{"expires"} $param->{"expires"} =~ /^\d+$/u
Truth table33%1/3
A and B
A= $param->{"expires"}B= $param->{"expires"} =~ /^\d+$/u
ABresult
0X0
100
111
53 0 $seconds = delete $param->{expires};
54 };
55  
56 # Establish WebFinger Route
57 1 my $wfr = $mojo->routes->any($WK_PATH);
58  
59 # Establish endpoint
60 1 $wfr->endpoint(
61 webfinger => {
62 query => [
63 'resource' => '{uri}',
64 'rel' => '{rel?}',
65 'format' => '{format?}'
66 ]
67 });
68  
69 # Response to webfinger request
70 $wfr->to(
71 5 ▶ cb => sub {
Subroutine __ANON__: called CC=17 SCAR=32.1
72 5 my $c = shift;
73  
74 # Check for security
75 5 ▶ if ($param->{secure} && !$c->req->is_secure) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param->{"secure"} and not $c->req->is_secure) 0 5 5
Condition33%1/3
$param->{"secure"} and not $c->req->is_secure
!ll&&!rl&&r
500
MC/DC0%0/2
$param->{"secure"} and not $c->req->is_secure
$param->{"secure"} not $c->req->is_secure
Truth table33%1/3
A and not(B)
A= $param->{"secure"}B= not $c->req->is_secure
ABresult
0X0
100
111
76  
77 # Bad request - only https allowed!
78 0 return $c->render(status => 400);
79 };
80  
81 # Get resource parameter
82 5 my $res = $c->param('resource');
83  
84 # Delete invalid parameters
85 5 ▶ if (!$res || $res eq '{uri}') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $res or $res eq "{uri}") 0 5 5
Condition33%1/3
not $res or $res eq "{uri}"
l!l&&r!l&&!r
005
MC/DC0%0/2
not $res or $res eq "{uri}"
not $res $res eq "{uri}"
Truth table33%1/3
not(A) or B
A= not $resB= $res eq "{uri}"
ABresult
1X1
011
000
86  
87 # Bad request - no resource defined
88 0 return $c->render(status => 400);
89 };
90  
91 # Set standard format
92 5 ▶ unless ($c->stash('format') || scalar $c->param('_format') || scalar $c->param('format')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->stash("format") or scalar $c->param("_format") or scalar $c->param("format")) 3 2 5
Condition75%3/4
$c->stash("format") or scalar $c->param("_format") or scalar $c->param("format")
l!l
23
$c->stash("format") or scalar $c->param("_format")
l!l
05
MC/DC0%0/1
$c->stash("format") or scalar $c->param("_format") or scalar $c->param("format")
$c->stash("format")
Truth table50%1/2
A
A= $c->stash("format")
Aresult
11
00
93 3 $c->stash(format => 'jrd');
94 };
95  
96 # Normalize the resource
97 5 my ($acct, $host, $nres) = _normalize_resource($c, $res);
98  
99 # Set host to local
100 5 ▶ $host ||= $c->req->url->base->host || 'localhost';
Condition60%3/5
$host ||= $c->req->url->base->host || "localhost"
l!l&&r!l&&!r
320
$c->req->url->base->host || 'localhost'
l!l
20
MC/DC0%0/2
$host ||= $c->req->url->base->host || "localhost"
$host $c->req->url->base->host
Truth table66%2/3
A ||= B
A= $hostB= $c->req->url->base->host
ABresult
1X1
011
000
101  
102 # Bad request - no resource defined
103 5 ▶ return $c->render(status => 400) unless $nres;
Branch: 1 branch
BranchTrueFalseTotal
unless $nres 0 5 5
104  
105 # Check for 'prepare_webfinger' callback
106 5 ▶ if ($c->callback(prepare_webfinger => $nres)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->callback("prepare_webfinger", $nres)) 3 2 5
107  
108 # The response body is already rendered
109 3 ▶ return if $c->res->body;
Branch: 1 branch
BranchTrueFalseTotal
if $c->res->body 0 3 3
110  
111 # Create new xrd document
112 3 my $xrd = _serve_webfinger($c, $acct, $nres, $res);
113  
114 # Seconds given
115 3 ▶ if ($xrd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xrd) 3 0 3
116  
117 3 my $expires;
118 3 ▶ unless ($expires = $xrd->expires && $seconds) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($expires = $xrd->expires && $seconds) 3 0 3
Condition33%1/3
$xrd->expires && $seconds
!ll&&!rl&&r
300
MC/DC0%0/2
$xrd->expires && $seconds
$xrd->expires $seconds
Truth table33%1/3
A && B
A= $xrd->expiresB= $seconds
ABresult
0X0
100
111
119 3 $expires = $xrd->expires( time + $seconds);
120 };
121  
122 # Expires set
123 3 ▶ if ($expires) {
Branch: 1 branch
BranchTrueFalseTotal
if ($expires) 3 0 3
124  
125 # Set cache control
126 3 my $headers = $c->res->headers;
127 3 $headers->cache_control(
128 "public, max-age=$seconds"
129 );
130  
131 # Set expires header
132 3 $headers->expires( $xrd->expires );
133 };
134 };
135  
136 # Server xrd document
137 3 return $c->reply->xrd($xrd, $res);
138 };
139  
140 # No valid xrd document is existing for this resource
141 2 return $c->reply->xrd(undef, $res);
142 }
143 1 );
144  
145 # Add Route to Host-Meta - exactly once
146 $mojo->hook(
147 1 ▶ prepare_hostmeta => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
148 1 my ($c, $hostmeta) = @_;
149  
150 # Add JRD link
151 1 $hostmeta->link(lrdd => {
152 type => 'application/jrd+json',
153 template => $c->endpoint(
154 webfinger => {
155 '?' => undef
156 }
157 )
158 });
159  
160 # Add XRD link
161 1 $hostmeta->link(lrdd => {
162 type => 'application/xrd+xml',
163 template => $c->endpoint(
164 webfinger => {
165 format => 'xrd',
166 '?' => undef
167 }
168 )
169 });
170 1 });
171  
172 # webfinger helper
173 1 $mojo->helper(
174 webfinger => \&_fetch_webfinger
175 );
176 };
177  
178  
179 # Fetch webfinger resource
180 2 ▶sub _fetch_webfinger {
Subroutine _fetch_webfinger: called CC=49 SCAR=72.0
181 2 my $c = shift;
182  
183 2 my ($acct, $res, $nres, $host);
184  
185  
186 # Request with host information
187 2 ▶ if ($_[1] && !ref($_[1]) && index($_[1], '-') != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] and not ref $_[1] and index($_[1], '-') != 0) { } 0 2 2
Condition33%2/6
$_[1] and not ref $_[1] and index($_[1], "-") != 0
!ll&&!rl&&r
200
$_[1] and not ref $_[1]
!ll&&!rl&&r
200
MC/DC0%0/3
$_[1] and not ref $_[1] and index($_[1], "-") != 0
$_[1] not ref $_[1] index($_[1], "-") != 0
Truth table25%1/4
A and not(B) and C
A= $_[1]B= not ref $_[1]C= index($_[1], "-") != 0
ABCresult
0XX0
10X0
1100
1111
188 0 $host = shift;
189 0 $nres = shift;
190 }
191  
192 # Get host information from resource
193 else {
194 2 $res = shift;
195 2 ($acct, $host, $nres) = _normalize_resource($c, $res);
196 };
197  
198 # Trim tail
199 2 ▶ pop while @_ && !defined $_[-1];
Branch: 1 branch
BranchTrueFalseTotal
@_ and not defined $_[-1] and do { pop() } 0 2 2
Condition33%1/3
@_ and not defined $_[-1]
!ll&&!rl&&r
200
MC/DC0%0/2
@_ and not defined $_[-1]
@_ not defined $_[-1]
Truth table33%1/3
A and not(B)
A= @_B= not defined $_[-1]
ABresult
0X0
100
111
200  
201 # Get flags
202 2 my %flag;
203 2 ▶ while (defined $_[-1] && index($_[-1], '-') == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $_[-1] and index($_[-1], "-") == 0) 0 2 2
Condition33%1/3
defined $_[-1] and index($_[-1], "-") == 0
!ll&&!rl&&r
200
MC/DC0%0/2
defined $_[-1] and index($_[-1], "-") == 0
defined $_[-1] index($_[-1], "-") == 0
Truth table33%1/3
A and B
A= defined $_[-1]B= index($_[-1], "-") == 0
ABresult
0X0
100
111
204 0 $flag{ pop() } = 1;
205 };
206  
207 # Optimize flags for known services
208 2 ▶ if ($host && $host =~ /(?:gmail|yahoo|mozilla)\.(?:com|org|net)$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($host and $host =~ /(?:gmail|yahoo|mozilla)\.(?:com|org|net)$/iu) 0 2 2
Condition66%2/3
$host and $host =~ /(?:gmail|yahoo|mozilla)\.(?:com|org|net)$/iu
!ll&&!rl&&r
110
MC/DC0%0/2
$host and $host =~ /(?:gmail|yahoo|mozilla)\.(?:com|org|net)$/iu
$host $host =~ /(?:gmail|yahoo|mozilla)\.(?:com|org|net)$/iu
Truth table66%2/3
A and B
A= $hostB= $host =~ /(?:gmail|yahoo|mozilla)\.(?:com|org|net)$/iu
ABresult
0X0
100
111
209 0 ▶ $flag{-old} = 1 unless $flag{-modern};
Branch: 1 branch
BranchTrueFalseTotal
unless $flag{"-modern"} 0 0 0
210 };
211  
212 # Get callback
213 2 ▶ my $cb = defined $_[-1] && ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $_[-1] && ref $_[-1] eq 'CODE' ? : 0 2 2
Condition33%1/3
defined $_[-1] and ref $_[-1] eq "CODE"
!ll&&!rl&&r
200
MC/DC0%0/2
defined $_[-1] and ref $_[-1] eq "CODE"
defined $_[-1] ref $_[-1] eq "CODE"
Truth table33%1/3
A and B
A= defined $_[-1]B= ref $_[-1] eq "CODE"
ABresult
0X0
100
111
214  
215 # Get header information for requests
216 2 my $header = {};
217 2 ▶ if ($_[0] && ref $_[0] && ref($_[0]) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] and ref $_[0] and ref $_[0] eq "HASH") 0 2 2
Condition33%2/6
$_[0] and ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
200
$_[0] and ref $_[0]
!ll&&!rl&&r
200
MC/DC0%0/3
$_[0] and ref $_[0] and ref $_[0] eq "HASH"
$_[0] ref $_[0] ref $_[0] eq "HASH"
Truth table25%1/4
A and B and C
A= $_[0]B= ref $_[0]C= ref $_[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
218 0 $header = shift;
219 };
220  
221 # Get relation information
222 2 my $rel = shift;
223  
224 # If local, serve local
225 2 ▶ if (!$host ||
Branch: 1 branch
BranchTrueFalseTotal
if (not $host or $host eq ($c->req->url->base->host || "localhost")) 2 0 2
Condition60%3/5
not $host or $host eq ($c->req->url->base->host || "localhost")
l!l&&r!l&&!r
110
$c->req->url->base->host || 'localhost'
l!l
10
MC/DC0%0/2
not $host or $host eq ($c->req->url->base->host || "localhost")
not $host $host eq ($c->req->url->base->host || "localhost")
MC/DC0%0/1
$c->req->url->base->host || 'localhost'
$c->req->url->base->host
Truth table66%2/3
not(A) or B
A= not $hostB= $host eq ($c->req->url->base->host || "localhost")
ABresult
1X1
011
000
Truth table50%1/2
A
A= $c->req->url->base->host
Aresult
11
00
226 ($host eq ($c->req->url->base->host || 'localhost'))) {
227  
228 2 ▶ if ($c->callback(prepare_webfinger => $nres)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c->callback('prepare_webfinger', $nres)) { } 2 0 2
229  
230 # Serve local xrd document
231 2 my $xrd = _serve_webfinger($c, $acct, $nres, $res);
232  
233 # Return values
234 2 ▶ return $cb ? $cb->($xrd, Mojo::Headers->new) : (
Branch: 2 branches
BranchTrueFalseTotal
$cb ? : 0 2 2
wantarray ? : 0 2 2
235 wantarray ? ($xrd, Mojo::Headers->new) : $xrd
236 );
237 }
238 else {
239 0 ▶ return $cb ? $cb->() : undef;
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
240 }
241 };
242  
243 # Check cache
244 0 my ($xrd, $headers) = $c->callback(
245 fetch_webfinger => ($host, $nres, $header)
246 );
247  
248 # Store unchanged normalized resource
249 0 $res = $nres;
250  
251 # Delete resource
252 0 $nres =~ s/^acct://;
253  
254 # xrd document exists
255 0 ▶ if ($xrd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xrd) 0 0 0
256  
257 # Filter relations
258 0 ▶ $xrd = $xrd->filter_rel( $rel ) if $rel;
Branch: 1 branch
BranchTrueFalseTotal
if $rel 0 0 0
259  
260 # Set headers to default
261 0 ▶ $headers ||= Mojo::Headers->new if $cb || wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if $cb or wantarray 0 0 0
Condition0%0/6
$cb or wantarray
l!l&&r!l&&!r
000
$headers ||= "Mojo::Headers"->new
l!l&&r!l&&!r
000
MC/DC0%0/2
$cb or wantarray
$cb wantarray
MC/DC0%0/2
$headers ||= "Mojo::Headers"->new
$headers "Mojo::Headers"->new
Truth table0%0/3
A or B
A= $cbB= wantarray
ABresult
1X1
011
000
Truth table0%0/3
A ||= B
A= $headersB= "Mojo::Headers"->new
ABresult
1X1
011
000
262  
263 # Return cached webfinger document
264 # Return values
265 0 ▶ return $cb ? $cb->($xrd, $headers) : (
Branch: 2 branches
BranchTrueFalseTotal
$cb ? : 0 0 0
wantarray ? : 0 0 0
266 wantarray ? ($xrd, $headers) : $xrd
267 );
268 };
269  
270 # Not found
271 0 ▶ return ($cb ? $cb->() : undef) unless $host && $res;
Branch: 2 branches
BranchTrueFalseTotal
unless $host and $res 0 0 0
$cb ? : 0 0 0
Condition0%0/3
$host and $res
!ll&&!rl&&r
000
MC/DC0%0/2
$host and $res
$host $res
Truth table0%0/3
A and B
A= $hostB= $res
ABresult
0X0
100
111
272  
273 # Set secure value
274 0 my $secure;
275 0 ▶ if (exists $flag{-secure} || exists $flag{-modern}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $flag{"-secure"} or exists $flag{"-modern"}) 0 0 0
Condition0%0/3
exists $flag{"-secure"} or exists $flag{"-modern"}
l!l&&r!l&&!r
000
MC/DC0%0/2
exists $flag{"-secure"} or exists $flag{"-modern"}
exists $flag{"-secure"} exists $flag{"-modern"}
Truth table0%0/3
A or B
A= exists $flag{"-secure"}B= exists $flag{"-modern"}
ABresult
1X1
011
000
276 0 $secure = 1;
277 };
278  
279 # Modern webfinger path
280 0 my $path = '//' . $host . $WK_PATH . '?resource=' . url_escape $nres;
281 0 ▶ $path = 'https:' . $path if $secure;
Branch: 1 branch
BranchTrueFalseTotal
if $secure 0 0 0
282  
283 # Non-blocking
284 0 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) 0 0 0
285  
286 # Initialize delay array
287 0 my @delay;
288  
289 # If modern is allowed
290 0 ▶ unless (exists $flag{-old}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $flag{"-old"}) 0 0 0
291  
292 # push to delay array
293 push(
294 @delay,
295  
296 # Step 1
297 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
298 0 my $delay = shift;
299  
300 # Retrieve from modern path
301 0 $c->get_xrd(
302 $path => $header => $delay->begin
303 );
304 },
305  
306 # Step 2
307 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
308 0 my ($delay, $xrd, $headers) = @_;
309  
310 # Document found
311 0 ▶ if ($xrd) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xrd) 0 0 0
312  
313 # Hook for caching
314 0 $c->app->plugins->emit_hook(
315 after_fetching_webfinger => (
316 $c, $host, $res, $xrd, $headers
317 ));
318  
319 # Filter based on relations
320 0 ▶ $xrd = $xrd->filter_rel($rel) if $rel;
Branch: 1 branch
BranchTrueFalseTotal
if $rel 0 0 0
321  
322 # Successful
323 0 return $cb->($xrd, $headers);
324 };
325  
326 # No more discovery
327 0 ▶ return $cb->() if exists $flag{-modern};
Branch: 1 branch
BranchTrueFalseTotal
if exists $flag{"-modern"} 0 0 0
328  
329 # Next step
330 0 $delay->begin->();
331 0 });
332 };
333  
334 # Old Host-Meta discovery
335 push(
336 @delay,
337  
338 # Step 3
339 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
340 0 my $delay = shift;
341  
342 0 my @param = (
343 $host,
344 $header,
345 ['lrdd'],
346 $delay->begin(0,1)
347 );
348  
349 0 ▶ push @param, '-secure' if $secure;
Branch: 1 branch
BranchTrueFalseTotal
if $secure 0 0 0
350  
351 # Host-Meta with lrdd
352 0 $c->hostmeta( @param );
353 },
354  
355 # Step 4
356 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
357 # Host-Meta document
358 0 my ($delay, $xrd) = @_;
359  
360 # Host-Meta is expired
361 0 ▶ return $cb->() if !$xrd || $xrd->expired;
Branch: 1 branch
BranchTrueFalseTotal
if not $xrd or $xrd->expired 0 0 0
Condition0%0/3
not $xrd or $xrd->expired
l!l&&r!l&&!r
000
MC/DC0%0/2
not $xrd or $xrd->expired
not $xrd $xrd->expired
Truth table0%0/3
not(A) or B
A= not $xrdB= $xrd->expired
ABresult
1X1
011
000
362  
363 # Prepare lrdd
364 0 ▶ my $template = _get_lrdd($xrd) or return $cb->();
Branch: 1 branch
BranchTrueFalseTotal
my $template = _get_lrdd($xrd) or return &$cb() 0 0 0
365  
366 # Interpolate template
367 0 my $lrdd = $c->endpoint($template => {
368 uri => $nres,
369 '?' => undef
370 });
371  
372 # Get lrdd
373 0 $c->get_xrd($lrdd => $header => $delay->begin(0,1))
374 },
375  
376 # Step 5
377 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
378 0 my $delay = shift;
379 0 my ($xrd, $headers) = @_;
380  
381 # No lrdd xrd document found
382 0 ▶ return $cb->() unless $xrd;
Branch: 1 branch
BranchTrueFalseTotal
unless $xrd 0 0 0
383  
384 # Hook for caching
385 0 $c->app->plugins->emit_hook(
386 after_fetching_webfinger => (
387 $c, $host, $res, $xrd, $headers
388 ));
389  
390 # Filter based on relations
391 0 ▶ $xrd = $xrd->filter_rel($rel) if $rel;
Branch: 1 branch
BranchTrueFalseTotal
if $rel 0 0 0
392  
393 # Successful
394 0 return $cb->($xrd, $headers);
395 0 });
396  
397 # Create delay
398 0 my $delay = Mojo::IOLoop->delay(@delay);
399  
400 # Start IOLoop if not running
401 0 ▶ $delay->wait unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 0 0 0
402  
403 0 return;
404 };
405  
406 # Blocking
407 # Modern discovery
408 0 ▶ unless (exists $flag{-old}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $flag{"-old"}) 0 0 0
409  
410 # Retrieve from modern path
411 0 ($xrd, $headers) = $c->get_xrd($path => $header);
412 };
413  
414 # Not found yet
415 0 ▶ unless ($xrd) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xrd) 0 0 0
416  
417 # No further discovery
418 0 ▶ return if exists $flag{-modern};
Branch: 1 branch
BranchTrueFalseTotal
if exists $flag{"-modern"} 0 0 0
419  
420 # Host-Meta and lrdd
421 0 ▶ $xrd = $c->hostmeta(
Branch: 2 branches
BranchTrueFalseTotal
$xrd = $c->hostmeta($host, $header, ["lrdd"], $secure ? "-secure" : undef) or return 0 0 0
$secure ? : 0 0 0
422 $host,
423 $header,
424 ['lrdd'],
425 ($secure ? '-secure' : undef)
426 ) or return;
427  
428 # Todo: support header expiration date
429 0 ▶ return if $xrd->expired;
Branch: 1 branch
BranchTrueFalseTotal
if $xrd->expired 0 0 0
430  
431 # Find 'lrdd' link
432 0 ▶ my $template = _get_lrdd($xrd) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $template = _get_lrdd($xrd) or return 0 0 0
433  
434 # Interpolate template
435 0 my $lrdd = $c->endpoint(
436 $template => {
437 uri => $nres,
438 '?' => undef
439 });
440  
441 # Retrieve based on lrdd
442 0 ▶ ($xrd, $headers) = $c->get_xrd($lrdd => $header) or return;
Branch: 1 branch
BranchTrueFalseTotal
() = $c->get_xrd($lrdd, $header) or return 0 0 0
443 };
444  
445 # Hook for caching
446 0 $c->app->plugins->emit_hook(
447 after_fetching_webfinger => (
448 $c, $host, $res, $xrd, $headers
449 ));
450  
451 # Filter based on relations
452 0 ▶ $xrd = $xrd->filter_rel($rel) if $rel;
Branch: 1 branch
BranchTrueFalseTotal
if $rel 0 0 0
453  
454 # Return
455 0 ▶ return wantarray ? ($xrd, $headers) : $xrd;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
456 };
457  
458  
459 # Serve webfinger
460 5 ▶sub _serve_webfinger {
Subroutine _serve_webfinger: called CC=5 SCAR=16.6
461 5 my $c = shift;
462 5 my ($acct, $nres, $res) = @_;
463  
464 # No normalized resource
465 5 ▶ return unless $nres;
Branch: 1 branch
BranchTrueFalseTotal
unless $nres 0 5 5
466  
467 # No resource given
468 5 ▶ $res ||= $nres;
Condition33%1/3
$res ||= $nres
l!l&&r!l&&!r
500
MC/DC0%0/2
$res ||= $nres
$res $nres
Truth table33%1/3
A ||= B
A= $resB= $nres
ABresult
1X1
011
000
469  
470 # Create new XRD document
471 5 my $xrd = $c->new_xrd;
472  
473 # Set Subject
474 5 $xrd->subject($res);
475  
476 # Set Alias
477 5 ▶ $xrd->alias($nres) if $res ne $nres;
Branch: 1 branch
BranchTrueFalseTotal
if $res ne $nres 2 3 5
478  
479 # Run hook
480 5 $c->app->plugins->emit_hook(
481 before_serving_webfinger => ($c, $nres, $xrd)
482 );
483  
484 # Filter relations
485 5 ▶ $xrd = $xrd->filter_rel($c->every_param('rel')) if $c->param('rel');
Branch: 1 branch
BranchTrueFalseTotal
if $c->param("rel") 0 5 5
486  
487 # Return webfinger document
488 5 return $xrd;
489 };
490  
491  
492 # Normalize resource
493 7 ▶sub _normalize_resource {
Subroutine _normalize_resource: called CC=9 SCAR=28.5
494 7 my ($c, $res) = @_;
495 7 ▶ return unless $res;
Branch: 1 branch
BranchTrueFalseTotal
unless $res 0 7 7
496  
497 # Resource is qualified
498 7 ▶ if (index($res, 'acct:') != 0 and $res =~ /^[^:]+:/) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($res, "acct:") != 0 and $res =~ /^[^:]+:/u) 0 7 7
Condition66%2/3
index($res, "acct:") != 0 and $res =~ /^[^:]+:/u
!ll&&!rl&&r
340
MC/DC0%0/2
index($res, "acct:") != 0 and $res =~ /^[^:]+:/u
index($res, "acct:") != 0 $res =~ /^[^:]+:/u
Truth table66%2/3
A and B
A= index($res, "acct:") != 0B= $res =~ /^[^:]+:/u
ABresult
0X0
100
111
499  
500 0 ▶ return $res unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 0 0 0
501  
502 # Check host
503 0 my $url = Mojo::URL->new($res);
504  
505 # Get host information
506 0 my $host = $url->host;
507  
508 # Return array
509 0 ▶ return (undef, $host, $res) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
510 };
511  
512 # Delete scheme if exists
513 7 $res =~ s/^acct://i;
514  
515 # Split user from domain
516 7 my ($acct, $host) = split '@', lc $res;
517  
518 # Create norm writing
519 7 my $norm = 'acct:' . $acct . '@';
520  
521 # Use request host if no host is given
522 7 ▶ $norm .= ($host || $c->req->url->base->host || 'localhost');
Condition60%3/5
$host || $c->req->url->base->host || 'localhost'
l!l
70
$host or $c->req->url->base->host
l!l&&r!l&&!r
430
MC/DC0%0/2
$host || $c->req->url->base->host || 'localhost'
$host $c->req->url->base->host
Truth table66%2/3
A or B
A= $hostB= $c->req->url->base->host
ABresult
1X1
011
000
523  
524 7 ▶ return wantarray ? ($acct, $host, $norm) : $norm;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 0 7
525 };
526  
527  
528 # Get lrdd
529 0 ▶sub _get_lrdd {
Subroutine _get_lrdd: not called CC=3 SCAR=24.8
530 0 my $xrd = shift;
531  
532 # Find 'lrdd' link
533 0 ▶ my $lrdd = $xrd->link('lrdd') or return;
Branch: 1 branch
BranchTrueFalseTotal
my $lrdd = $xrd->link("lrdd") or return 0 0 0
534  
535 # Get template
536 0 ▶ $lrdd->attr('template') or return;
Branch: 1 branch
BranchTrueFalseTotal
$lrdd->attr("template") or return 0 0 0
537 };
538  
539  
540 1 1;
541  
542  
543 __END__
↑ Summary