lib/XRI.pm

filter: Statementstmt 66.0% 76 / 115 Branchbran 28.5% 12 / 42 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 80.0% 12 / 15 Podpod 0.0% 0 / 8 totaltotal 51.2% 100 / 195 CC 26 SCAR 32.5
CC
26
Cov
53%
resolveToAuthorityXML
56.5
resolveToLocalAccessURI
21.7
convertToHTTP
6.0
CRAP 25.7
↑ Summary script/xriresolve.pl »
1 # Copyright (C) 2004 Identity Commons. All Rights Reserved.
2 # See LICENSE for licensing details
3  
4 # Author: Fen Labalme <fen@idcommons.net>
5  
6 # TODO: Insert logic about choosing an authority from the current descriptor
7 # TODO: Logic about whether to look at the mapping, etc.
8  
9 3 require 5.6.0;
10  
11 package XRI;
12 3 our $VERSION = '0.2.4';
13  
14 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 3 ▶use XRI::Parse qw( GCS_CHARS );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use XRI::Descriptor;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 3 ▶use XML::Smart;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use LWP::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 3 ▶use Log::Agent;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 logconfig(-prefix => $0);
25  
26 # this gets changed during make install
27 3 my $ROOTS = '/usr/local/lib/perl5/site_perl/5.44.0/XRI/xriroots.xml';
28  
29 3 my $xrins = 'xri:$r.s/XRIDescriptor';
30  
31 3 our %globals = ();
32 3 our %private = ();
33  
34 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
35 1 my $self = shift;
36 1 my $xri = shift;
37 1 my $this = { descriptor=>undef,
38 descriptorXML=>undef,
39 localAccessURL=>undef,
40 xri=>$xri };
41 1 bless $this, $self;
42 }
43  
44 # Converts XRIs with DNS-based Authorities to HTTP URIs and stores the localaccessurl
45 #
46 # FIXME: more escaping needed - also need to check for absoluteness
47 #
48 0 ▶sub convertToHTTP {
Subroutine convertToHTTP: not called CC=2 SCAR=17.9
Pod undocumented
49 0 my $self = shift;
50  
51 $self->{localAccessURL} = "http:" .
52 0 ▶ (( $self->{xri} =~ /^xri:(.*)$/ ) ? $1 : $self->{xri} );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'xri'} =~ /^xri:(.*)$/ ? : 0 0 0
53 }
54  
55 1 ▶sub resolveToAuthorityXML {
Subroutine resolveToAuthorityXML: called CC=15 SCAR=40.4
Pod undocumented
56 1 my $self = shift;
57 1 my ($service, $type) = @_;
58 1 my $XRI = new XRI::Parse $self->{xri}; # FIXME: created two XRI objects...
59 1 my ($authRef, $descriptor, @authURIs);
60 1 my $descXML = undef;
61  
62 # get the authority and local access parts
63 #
64 1 my $authLocal = $XRI->splitAuthLocal;
65  
66 # no authority part -> relative-path
67 #
68 1 ▶ if ( ! defined $XRI->{authority} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $XRI->{"authority"}) 0 1 1
69 0 ▶ die "RelativePathNotXRIAuthority for passed service or type\n"
Branch: 1 branch
BranchTrueFalseTotal
if $service or $type 0 0 0
Condition0%0/3
$service or $type
l!l&&r!l&&!r
000
MC/DC0%0/2
$service or $type
$service $type
Truth table0%0/3
A or B
A= $serviceB= $type
ABresult
1X1
011
000
70 if ($service || $type);
71 0 $self->{localAccessURL} = $authLocal;
72 0 logtrc 'notice', "No XRI Authority - relative-path resolved";
73 0 return;
74 }
75 # if the first segment is '//' convert to HTTP and return
76 #
77 1 ▶ if ( $XRI->{authority} eq '//') {
Branch: 1 branch
BranchTrueFalseTotal
if ($XRI->{"authority"} eq "//") 0 1 1
78 0 ▶ die "URIAuthorityNotXRIAuthority for passed service or type\n"
Branch: 1 branch
BranchTrueFalseTotal
if $service or $type 0 0 0
Condition0%0/3
$service or $type
l!l&&r!l&&!r
000
MC/DC0%0/2
$service or $type
$service $type
Truth table0%0/3
A or B
A= $serviceB= $type
ABresult
1X1
011
000
79 if ($service || $type);
80 0 $self->convertToHTTP;
81 0 logtrc 'notice', "XRI Authority is DNS Based - XRI converted to HTTP";
82 0 return;
83 }
84 1 logtrc 'notice', "Extracted root identifier of %s", $XRI->{authority};
85  
86 # load the roots (global roots could be "precomputed")
87 # FIXME: need mechanism to incrementally add to private roots
88 #
89 1 ▶ readRoots() if ! scalar %globals;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar %XRI::globals 0 1 1
90  
91 1 ($authRef, $self->{localAccessURL}) = @$authLocal;
92 1 my $subseg = shift @$authRef;
93  
94 # get the root authority
95 # HACK: we assume there's only one root AuthorityURI
96 # HACK: we randomly choose the first XRIAuthority URI
97 #
98 1 ▶ my $url = isGCS( $subseg ) ? $globals{ $subseg } : $private{ $subseg };
Branch: 1 branch
BranchTrueFalseTotal
isGCS($subseg) ? : 0 1 1
99 1 ▶ if (! defined $url) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $url) 0 1 1
100 0 die "UnknownAuthority: $subseg\n";
101 }
102 1 ▶ while ( $subseg = shift @$authRef ) { # divider
Branch: 1 branch
BranchTrueFalseTotal
if ($subseg = shift @$authRef) 1 0 1
103 1 $subseg .= shift @$authRef; # segment
104 1 logtrc 'notice', "Resolving authority subsegment %s", $subseg;
105  
106 1 ▶ $url .= '/' unless $url =~ m|/$|;
Branch: 1 branch
BranchTrueFalseTotal
unless $url =~ m[/$] 1 0 1
107 1 $url .= uri_escape($subseg); # FIXME turn spaces into '+', etc...
108  
109 1 logtrc 'notice', "Contacting Naming Authority URL %s", $url;
110 1 $descXML = get $url; # Quick and dirty - should catch exceptions and 404s
111 1 ▶ if ( ! defined $descXML ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $descXML) 1 0 1
112 1 die "NoDescriptorXML for $url\n"; # FIXME
113 }
114 0 logtrc 'notice', "Descriptor for %s is\n%s", $subseg, $descXML;
115  
116 0 $descriptor = XRI::Descriptor->new($descXML);
117  
118 0 ▶ if (! defined $descriptor) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $descriptor) 0 0 0
119 0 die "MalformedXRIDescriptor for $url\n";
120 }
121 0 @authURIs = @{$descriptor->getXRIAuthorityURIs};
122  
123 # is it a bug if there is no XRIAuthority URI in a delegated segment?
124 # we'll accept it for now, and expect to use LocalAccess from here...
125 #
126 0 ▶ last unless scalar @authURIs; # done if no XRI Authority URU
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @authURIs 0 0 0
127  
128 0 $url = $authURIs[0]; # HACK: randomly choose the first URI
129 }
130 0 $self->{descriptor} = $descriptor;
131 0 $self->{descriptorXML} = $descXML;
132 }
133  
134 # Performs basic Authority Resolution
135 # Assumes we are not using a DNS-based authority
136 # Sets the descriptor to the XRI
137 #
138 1 ▶sub resolveToLocalAccessURI {
Subroutine resolveToLocalAccessURI: called CC=5 SCAR=30.8
Pod undocumented
139 1 my $self = shift;
140 1 my ($service, $type) = @_;
141 1 $self->resolveToAuthorityXML($service, $type);
142  
143 0 ▶ return unless $self->{descriptor};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"descriptor"} 0 0 0
144  
145 0 my $local = $self->{localAccessURL};
146 0 my @localAccessElem = $self->{descriptor}->getLocalAccess($service, $type);
147  
148 0 ▶ if ( scalar @localAccessElem ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @localAccessElem) { } 0 0 0
149
150 # HACK: randomly choose the first LocalAccess element and the first URI within it
151 #
152 0 my $desLocalAccess = ${$localAccessElem[0]->uris}[0];
153 0 logtrc( 'notice',
154 "Constructed local access URL from base local access url %s and local XRI part %s",
155 $desLocalAccess, $local );
156 0 ▶ $desLocalAccess .= '/' if $local && $desLocalAccess !~ m|\/$|;
Branch: 1 branch
BranchTrueFalseTotal
if $local and not $desLocalAccess =~ m[\/$] 0 0 0
Condition0%0/3
$local and not $desLocalAccess =~ m[\/$]
!ll&&!rl&&r
000
MC/DC0%0/2
$local and not $desLocalAccess =~ m[\/$]
$local not $desLocalAccess =~ m[\/$]
Truth table0%0/3
A and not(B)
A= $localB= not $desLocalAccess =~ m[\/$]
ABresult
0X0
100
111
157 0 logtrc 'notice', "Local access descriptor is %s", $desLocalAccess;
158 0 $self->{localAccessURL} = $desLocalAccess . XRI::Parse->new($local)->escapeURI;
159 }
160 else {
161 #No local access URL! Probably should raise an excepetion
162 0 $self->{localAccessURL} = undef;
163 0 die "NoLocalAccessFound for XRI $self->{xri}\n";
164 }
165 }
166  
167 0 ▶sub doGet {
Subroutine doGet: not called CC=2 SCAR=17.9
Pod undocumented
168 0 my $self = shift;
169  
170 $self->resolveToLocalAccessURI
171 0 ▶ unless defined $self->{localAccessURL};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"localAccessURL"} 0 0 0
172 0 return get( $self->{localAccessURL} ); # returns the document
173 }
174  
175 0 ▶sub getGetURL {
Subroutine getGetURL: not called CC=2 SCAR=17.9
Pod undocumented
176 0 my $self = shift;
177  
178 $self->resolveToLocalAccessURI
179 0 ▶ unless defined $self->{localAccessURL};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"localAccessURL"} 0 0 0
180 0 return $self->{localAccessURL};
181 }
182  
183 9 ▶sub isGCS {
Subroutine isGCS: called CC=1 SCAR=0.0
Pod undocumented
184 9 my $char = shift;
185  
186 9 return grep { $_ eq $char } @XRI::Parse::GCS_CHARS;
187 }
188  
189 # Read XRI roots file
190 # TODO: implement mechanism to add private roots
191 #
192 2 ▶sub readRoots {
Subroutine readRoots: called CC=5 SCAR=16.1
Pod undocumented
193 2 my $roots = shift;
194  
195 2 ▶ $roots = $ROOTS unless defined $roots;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $roots 0 2 2
196  
197 2 ▶ die "XRIRootsNotFound: Can't find $roots\n" unless -r $roots;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $roots 0 2 2
198  
199 2 my $XML = XML::Smart->new($roots);
200  
201 2 $XML = $XML->cut_root();
202  
203 2 foreach my $descriptor ( @{$XML->{XRIDescriptor}} ) { # missing: ('xmlns','eq',$xrins)
204 8 my $resolved = $descriptor->{Resolved};
205 8 my $authority = $descriptor->{XRIAuthority}{URI};
206 8 ▶ if ( isGCS( $resolved )) {
Branch: 1 branch
BranchTrueFalseTotal
if (isGCS($resolved)) { } 6 2 8
207 6 logtrc 'notice', "Resolved Global %s to %s", $resolved, $authority;
208 6 $XRI::globals{$resolved} = $authority;
209 }
210 else {
211 2 logtrc 'notice', "Resolved Private %s to %s", $resolved, $authority;
212 2 $XRI::private{$resolved} = $authority;
213 }
214 }
215 }
216  
217 3 1;
218 __END__
↑ Summary script/xriresolve.pl »