blib/lib/Mojolicious/Plugin/XRD.pm

filter: Statementstmt 43.7% 42 / 96 Branchbran 16.1% 11 / 68 Conditioncond 34.3% 11 / 32 MC/DCmcdc 10.0% 2 / 20 Subroutinesub 69.2% 9 / 13 Podpod 100% 1 / 1 totaltotal 33.0% 76 / 230 CC 47 SCAR 56.1
CC
47
Cov
33%
_get_xrd
600.1
__ANON__
56.0
__ANON__
20.0
CRAP 274.0
↑ Summary
1 package Mojolicious::Plugin::XRD;
2 2 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use Mojo::Util qw/quote deprecated/;
Subroutine BEGIN: called CC=1 SCAR=0.0
4
5 2 our $VERSION = '0.24';
6
7 # Todo: Support
8 # $self->reply->xrd( $xrd => {
9 # resource => 'acct:akron@sojolicious.example',
10 # expires => (30 * 24 * 60 * 60),
11 # cache => ...,
12 # chi => ...
13 # });
14 #
15 # - Add Acceptance for XRD and JRD and JSON as a header
16
17 # UserAgent name
18 2 my $UA_NAME = __PACKAGE__ . ' v' . $VERSION;
19
20 # UserAgent maximum redirects
21 2 my $UA_MAX_REDIRECTS = 10;
22
23 # UserAgent connect timeout
24 2 my $UA_CONNECT_TIMEOUT = 7;
25
26
27 # Register Plugin
28 2 ▶sub register {
Subroutine register: called CC=3 SCAR=11.1
Pod documented
29 2 my ($plugin, $mojo) = @_;
30
31 # Add types
32 2 for ($mojo->types) {
33 2 $_->type(jrd => 'application/jrd+json');
34 2 $_->type(xrd => 'application/xrd+xml');
35 };
36
37 14 ▶ my $reply_xrd = sub {
Subroutine __ANON__: called CC=8 SCAR=21.1
38 14 my ($c, $xrd, $res) = @_;
39
40 # Define xrd or jrd
41 14 ▶ unless ($c->stash('format')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c->stash("format")) 14 0 14
42 14 ▶ $c->stash('format' => (
Condition100%2/2
scalar $c->param('_format') || scalar $c->param('format')
l!l
410
MC/DC100%1/1
scalar $c->param('_format') || scalar $c->param('format')
scalar $c->param('_format')
Truth table100%2/2
A
A= scalar $c->param('_format')
Aresult
11
00
43 scalar $c->param('_format') || scalar $c->param('format')
44 ));
45 };
46
47 # Add CORS header
48 14 $c->res->headers->header(
49 'Access-Control-Allow-Origin' => '*'
50 );
51
52 14 my $status = 200;
53
54 # Not found
55 14 ▶ if (!defined $xrd || !ref($xrd)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $xrd && ref $xrd) { } 6 8 14
elsif ($c->param('rel')) { } 6 2 8
Condition66%2/3
defined $xrd && ref $xrd
!ll&&!rl&&r
608
MC/DC50%1/2
defined $xrd && ref $xrd
defined $xrd ref $xrd
Truth table66%2/3
A && B
A= defined $xrdB= ref $xrd
ABresult
0X0
100
111
56 6 $status = 404;
57 6 $xrd = $c->helpers->new_xrd;
58 6 ▶ $xrd->subject("$res") if $res;
Branch: 1 branch
BranchTrueFalseTotal
if $res 6 0 6
59 }
60
61 # rel parameter
62 elsif ($c->param('rel')) {
63
64 # Clone and filter relations
65 6 $xrd = $xrd->filter_rel( $c->every_param('rel') );
66 };
67
68 14 ▶ my $head_data = $c->req->method eq 'HEAD' ? '' : undef;
Branch: 1 branch
BranchTrueFalseTotal
$c->req->method eq 'HEAD' ? : 7 7 14
69
70 # content negotiation
71 return $c->respond_to(
72
73 # JSON request
74 2 ▶ json => sub { $c->render(
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition66%2/3
$head_data // $xrd->to_json
l!l&&r!l&&!r
110
MC/DC0%0/2
$head_data // $xrd->to_json
$head_data $xrd->to_json
Truth table66%2/3
A // B
A= $head_dataB= $xrd->to_json
ABresult
1X1
011
000
75 status => $status,
76 data => $head_data // $xrd->to_json,
77 format => 'json'
78 )},
79
80 # JRD request
81 2 ▶ jrd => sub { $c->render(
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition66%2/3
$head_data // $xrd->to_json
l!l&&r!l&&!r
110
MC/DC0%0/2
$head_data // $xrd->to_json
$head_data $xrd->to_json
Truth table66%2/3
A // B
A= $head_dataB= $xrd->to_json
ABresult
1X1
011
000
82 status => $status,
83 data => $head_data // $xrd->to_json,
84 format => 'jrd'
85 )},
86
87 # XML default
88 10 ▶ any => sub { $c->render(
Subroutine __ANON__: called CC=2 SCAR=7.2
Condition66%2/3
$head_data // $xrd->to_pretty_xml
l!l&&r!l&&!r
550
MC/DC0%0/2
$head_data // $xrd->to_pretty_xml
$head_data $xrd->to_pretty_xml
Truth table66%2/3
A // B
A= $head_dataB= $xrd->to_pretty_xml
ABresult
1X1
011
000
89 status => $status,
90 data => $head_data // $xrd->to_pretty_xml,
91 format => 'xrd'
92 )}
93 14 );
94 2 };
95
96 # Add DEPRECATED 'render_xrd' helper
97 $mojo->helper(
98 8 ▶ render_xrd => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
99 8 deprecated 'render_xrd is deprecated in favor of reply->xrd';
100 8 $reply_xrd->(@_)
101 }
102 2 );
103
104 # Add 'reply->xrd' helper
105 2 $mojo->helper( 'reply.xrd' => $reply_xrd);
106
107 # Add 'get_xrd' helper
108 2 $mojo->helper( get_xrd => \&_get_xrd );
109
110 # Add 'new_xrd' helper
111 2 ▶ unless (exists $mojo->renderer->helpers->{'new_xrd'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $mojo->renderer->helpers->{"new_xrd"}) 2 0 2
112 2 $mojo->plugin('XML::Loy' => {
113 new_xrd => [-XRD]
114 });
115 };
116 };
117
118 # Get XRD document
119 1 ▶sub _get_xrd {
Subroutine _get_xrd: called CC=26 SCAR=64.0
120 1 my $c = shift;
121 1 my $resource = Mojo::URL->new( shift );
122
123 # Trim tail
124 1 ▶ pop while @_ && !defined $_[-1];
Branch: 1 branch
BranchTrueFalseTotal
@_ and not defined $_[-1] and do { pop() } 0 1 1
Condition33%1/3
@_ and not defined $_[-1]
!ll&&!rl&&r
010
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
125
126 # No valid resource
127 1 ▶ return unless $resource->host;
Branch: 1 branch
BranchTrueFalseTotal
unless $resource->host 1 0 1
128
129 0 my $header = {};
130 0 ▶ if ($_[0] && ref $_[0] && ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] and ref $_[0] and ref $_[0] eq "HASH") 0 0 0
Condition0%0/6
$_[0] and ref $_[0] and ref $_[0] eq "HASH"
!ll&&!rl&&r
000
$_[0] and ref $_[0]
!ll&&!rl&&r
000
MC/DC0%0/3
$_[0] and ref $_[0] and ref $_[0] eq "HASH"
$_[0] ref $_[0] ref $_[0] eq "HASH"
Truth table0%0/4
A and B and C
A= $_[0]B= ref $_[0]C= ref $_[0] eq "HASH"
ABCresult
0XX0
10X0
1100
1111
131 0 $header = shift;
132 };
133
134 # Check if security is forced
135 0 my $prot = $resource->protocol;
136 0 my $secure;
137 0 ▶ $secure = 1 if $prot && $prot eq 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $prot and $prot eq "https" 0 0 0
Condition0%0/3
$prot and $prot eq "https"
!ll&&!rl&&r
000
MC/DC0%0/2
$prot and $prot eq "https"
$prot $prot eq "https"
Truth table0%0/3
A and B
A= $protB= $prot eq "https"
ABresult
0X0
100
111
138
139 # Get callback
140 0 ▶ my $cb = pop if ref($_[-1]) && ref($_[-1]) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[-1] and ref $_[-1] eq "CODE" 0 0 0
Condition0%0/3
ref $_[-1] and ref $_[-1] eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[-1] and ref $_[-1] eq "CODE"
ref $_[-1] ref $_[-1] eq "CODE"
Truth table0%0/3
A and B
A= ref $_[-1]B= ref $_[-1] eq "CODE"
ABresult
0X0
100
111
141
142 # Build relations parameter
143 0 my $rel;
144 0 ▶ $rel = shift if $_[0] && ref $_[0] eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and ref $_[0] eq "ARRAY" 0 0 0
Condition0%0/3
$_[0] and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and ref $_[0] eq "ARRAY"
$_[0] ref $_[0] eq "ARRAY"
Truth table0%0/3
A and B
A= $_[0]B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
145
146 # Get secure user agent
147 0 ▶ my $ua = Mojo::UserAgent->new(
Branch: 1 branch
BranchTrueFalseTotal
$secure ? : 0 0 0
148 name => $UA_NAME,
149 max_redirects => ($secure ? 0 : $UA_MAX_REDIRECTS),
150 connect_timeout => $UA_CONNECT_TIMEOUT
151 );
152
153 0 my $xrd;
154
155 # Set to secure, if not defined
156 0 ▶ $resource->scheme('https') unless $resource->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $resource->scheme 0 0 0
157
158 # Get helpers proxy object
159 0 my $h = $c->helpers;
160
161 # Is blocking
162 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
163
164 # Fetch Host-Meta XRD - first try ssl
165 0 my $tx = $ua->get($resource => $header);
166 0 my $xrd_res;
167
168 # Transaction was not successful
169 0 ▶ return unless $xrd_res = $tx->success;
Branch: 1 branch
BranchTrueFalseTotal
unless $xrd_res = $tx->success 0 0 0
170
171 0 ▶ unless ($xrd_res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($xrd_res->is_success) 0 0 0
172
173 # Only support secure retrieval
174 0 ▶ return if $secure;
Branch: 1 branch
BranchTrueFalseTotal
if $secure 0 0 0
175
176 # Was already insecure
177 0 ▶ return if $resource->protocol eq 'http';
Branch: 1 branch
BranchTrueFalseTotal
if $resource->protocol eq "http" 0 0 0
178
179 # Make request insecure
180 0 $resource->scheme('http');
181
182 # Update insecure max_redirects;
183 0 $ua->max_redirects($UA_MAX_REDIRECTS);
184
185 # Then try insecure
186 0 $tx = $ua->get($resource => $header);
187
188 # Transaction was not successful
189 0 ▶ return unless $xrd_res = $tx->success;
Branch: 1 branch
BranchTrueFalseTotal
unless $xrd_res = $tx->success 0 0 0
190
191 # Retrieval was successful
192 0 ▶ return unless $xrd_res->is_success;
Branch: 1 branch
BranchTrueFalseTotal
unless $xrd_res->is_success 0 0 0
193 };
194
195 # Parse xrd document
196 0 ▶ $xrd = $h->new_xrd($xrd_res->body) or return;
Branch: 1 branch
BranchTrueFalseTotal
$xrd = $h->new_xrd($xrd_res->body) or return 0 0 0
197
198 # Filter relations
199 0 ▶ $xrd = $xrd->filter_rel($rel) if $rel;
Branch: 1 branch
BranchTrueFalseTotal
if $rel 0 0 0
200
201 # Return xrd
202 0 ▶ return ($xrd, $xrd_res->headers->clone) if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
203 0 return $xrd;
204 };
205
206 # Non-blocking
207 # Create delay for https with or without redirection
208 my $delay = Mojo::IOLoop->delay(
209 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
210 0 my $delay = shift;
211
212 # Get with https - possibly without redirects
213 0 $ua->get($resource => $header => $delay->begin);
214 },
215 0 ▶ sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
216 0 my ($delay, $tx) = @_;
217
218 # Get response
219 0 ▶ if (my $xrd_res = $tx->success) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $xrd_res = $tx->success) { } 0 0 0
220
221 # Fine
222 0 ▶ if ($xrd_res->is_success) {
Branch: 1 branch
BranchTrueFalseTotal
if ($xrd_res->is_success) 0 0 0
223
224 # Parse xrd document
225 0 ▶ $xrd = $h->new_xrd($xrd_res->body) or return $cb->(undef);
Branch: 1 branch
BranchTrueFalseTotal
$xrd = $h->new_xrd($xrd_res->body) or return &$cb(undef) 0 0 0
226
227 # Filter relations
228 0 ▶ $xrd = $xrd->filter_rel($rel) if $rel;
Branch: 1 branch
BranchTrueFalseTotal
if $rel 0 0 0
229
230 # Send to callback
231 0 return $cb->($xrd, $xrd_res->headers->clone);
232 };
233
234 # Only support secure retrieval
235 0 ▶ return $cb->(undef) if $secure;
Branch: 1 branch
BranchTrueFalseTotal
if $secure 0 0 0
236 }
237
238 # Fail
239 else {
240 0 return $cb->(undef);
241 };
242
243 # Was already insecure
244 0 ▶ return if $resource->protocol eq 'http';
Branch: 1 branch
BranchTrueFalseTotal
if $resource->protocol eq "http" 0 0 0
245
246 # Try http with redirects
247 $delay->steps(
248 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
249 0 my $delay = shift;
250
251 0 $resource->scheme('http');
252
253 # Get with http and redirects
254 0 $ua->max_redirects($UA_MAX_REDIRECTS);
255 0 $ua->get($resource => $header => $delay->begin );
256 },
257 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
258 0 my $delay = shift;
259
260 # Transaction was successful
261 0 ▶ if (my $xrd_res = pop->success) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $xrd_res = (pop())->success) 0 0 0
262
263 # Parse xrd document
264 0 ▶ $xrd = $h->new_xrd($xrd_res->body) or return $cb->(undef);
Branch: 1 branch
BranchTrueFalseTotal
$xrd = $h->new_xrd($xrd_res->body) or return &$cb(undef) 0 0 0
265
266 # Filter relations
267 0 ▶ $xrd = $xrd->filter_rel($rel) if $rel;
Branch: 1 branch
BranchTrueFalseTotal
if $rel 0 0 0
268
269 # Send to callback
270 0 return $cb->($xrd, $xrd_res->headers->clone);
271 };
272
273 # Fail
274 0 return $cb->(undef);
275 0 });
276 }
277 0 );
278
279 # Wait if IOLoop is not running
280 0 ▶ $delay->wait unless Mojo::IOLoop->is_running;
Branch: 1 branch
BranchTrueFalseTotal
unless "Mojo::IOLoop"->is_running 0 0 0
281 0 return;
282 };
283
284
285 2 1;
286
287
288 __END__
↑ Summary