| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | die "RelativePathNotXRIAuthority for passed service or type\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$service or $type
MC/DC0%0/2
$service or $type
$service
$type
Truth table0%0/3
A or B
A= $serviceB= $type
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | die "URIAuthorityNotXRIAuthority for passed service or type\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$service or $type
MC/DC0%0/2
$service or $type
$service
$type
Truth table0%0/3
A or B
A= $serviceB= $type
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 99 | 1 | ▶ | if (! defined $url) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 0 | die "UnknownAuthority: $subseg\n"; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 1 | ▶ | while ( $subseg = shift @$authRef ) { # divider | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
$local and not $desLocalAccess =~ m[\/$]
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[\/$]
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 2 | ▶ | die "XRIRootsNotFound: Can't find $roots\n" unless -r $roots; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||