LS/Locator.pm untested

filter: Statementstmt 0.0% 0 / 109 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 185 CC 20 SCAR 42.0
CC
20
Cov
0%
resolveAuthority
156.0
loadLocalMappingFile
56.0
new
12.0
CRAP 66.5
1 # ====================================================================
2 # Copyright (c) 2002,2003 IBM Corporation
3 # All rights reserved. This program and the accompanying materials
4 # are made available under the terms of the Common Public License v1.0
5 # which accompanies this distribution, and is available at
6 # http://www.opensource.org/licenses/cpl.php
7 #
8 # =====================================================================
9 package LS::Locator;
10  
11 use strict;
12 use warnings;
13  
14 use vars qw( $METHODS );
15  
16 use Net::DNS::Resolver;
17  
18 use LS;
19 use LS::ID;
20 use LS::Authority;
21  
22 use base 'LS::Base';
23  
24  
25 sub BEGIN {
26  
27 $METHODS = [
28 'authorityCache',
29 'localMappingFile',
30 'cacheAuthorities',
31 ];
32
33 LS::makeAccessorMethods($METHODS, __PACKAGE__);
34 }
35  
36  
37 #
38 # new( %options ) - Creates a new LS::Locator that searches DNS for an LSID's authority.
39 #
40 # This method accepts parameters in the form of a hash. The following hash keys are
41 # used:
42 #
43 # local - Specifies the local filename and path that LS::Locator will use to
44 # short ciruit DNS.
45 #
46 # nocache - If set to true, then DNS queries for authorities will not be cached.
47 #
48 #
49 # Returns: An LS::Locator object if successful,
50 # undef if there is a failure
51 #
52 sub new {
53
54 my $self = shift;
55 my (%params) = @_;
56
57 unless (ref $self) {
58  
59 $self = bless {}, $self;
60  
61 $self->authorityCache( {} );
62 }
63
64 my $file = ( $params{'local'} || $params{'localMappingFile'} || 'authorities');
65 $self->localMappingFile( $file );
66 $self->loadLocalMappingFile();
67 $self->cacheAuthorities(1);
68
69 $self->cacheAuthorities($params{'nocache'})
70 if(exists($params{'nocache'}));
71
72 return $self;
73 }
74  
75  
76 #
77 # cacheAuthorities( $bool ) - Returns whether or not Authority queries will be cached.
78 #
79 # Returns: If Authority queries _WILL_ be cached, 1 is returned
80 # otherwise, a false value is returned
81 #
82  
83  
84 #
85 # localMappingFile( [$filename] )
86 # Gets/Sets the local filename to use for the static
87 # authority -> endpoint mappings
88 #
89 # Parameters:
90 # If the filename parameter is specifed, the local filename is set to that
91 # parameter.
92 #
93 # Returns:
94 # If the parameter is left unspecified the current local filename
95 # is used.
96 #
97  
98  
99 #
100 # clearCache( ) - Empties the Authority query cache
101 #
102 sub clearCache {
103  
104 my $self = shift;
105
106 $self->authorityCache( {} );
107  
108 # Reload the authorities file
109 $self->loadLocalMappingFile();
110 }
111  
112  
113 #
114 # clear_cache( ) - Synonym for clearCache
115 #
116 sub clear_cache {
117  
118 my $self = shift;
119 return $self->clearCache();
120 }
121  
122  
123 sub loadLocalMappingFile {
124
125 my $self = shift;
126  
127 # Can only initialize the cache if there is a filename set
128 unless($self->localMappingFile()) {
129
130 return undef;
131 }
132  
133 # Load the local authorities file
134 unless(open(FILE, $self->localMappingFile()) ) {
135
136 return undef;
137 }
138
139 local ($/)= "\n";
140 while (<FILE>) {
141
142 # Trim whitespace
143 s/^\s+|\s+$//g;
144  
145 # Ignore comments
146 next if(/^#/);
147  
148 my ($name, $location) = split(/\s/, $_, 2);
149 next unless($name);
150  
151 my ($host, $port, $path) = $location =~ m|^\s*(.*?):(.*?)(/(.*))?$|;
152 next unless($host && $port);
153  
154 my $authority = LS::Authority->new_by_hostname($host, $port, $path);
155 next unless($authority);
156  
157 $self->authorityCache()->{ lc($name) } = $authority;
158 }
159  
160 close(FILE);
161 }
162  
163  
164 #
165 # resolveAuthority( $lsid ) - Resolves the authority for the specified LSID. This method will
166 # search DNS for the hostname and then retrieve the SRV resource
167 # records for the authority.
168 #
169 # Returns: An LS::Authority object if successful,
170 # undef if an error occurs, check errorString for more information
171 #
172 sub resolveAuthority {
173  
174 my $self = shift;
175 my ($id) = @_;
176  
177 my $authority_id;
178  
179 unless(UNIVERSAL::isa($id, 'LS::ID')) {
180  
181 my $lsid = LS::ID->new($id);
182
183 if ($lsid) {
184
185 $authority_id = $lsid->authority();
186 }
187 elsif ($id =~ /^lsidauth:([A-Za-z0-9][\w\(\)\+\,\-\.\=\@\;\$\"\_\!\*\']*)$/i) {
188 $authority_id = $1;
189 }
190 else {
191
192 $self->recordError( 'Cannot resolve invalid LSID: ' . $id );
193 $self->addStackTrace() ;
194
195 return undef;
196 }
197 }
198  
199 $authority_id = $id->authority();
200 unless ($authority_id) {
201  
202 $self->recordError( 'Cannot resolve authority, invalid LSID' );
203 $self->addStackTrace() ;
204  
205 return undef;
206 }
207  
208 my $cached = $self->authorityCache()->{ $authority_id };
209  
210 return $cached
211 if($cached);
212  
213 # Use DNS to resolve the authority ID via an SRV record
214 my $resolver = Net::DNS::Resolver->new();
215 unless($resolver) {
216  
217 $self->recordError( 'Unable to create Net::DNS::Resolver object' );
218 $self->addStackTrace();
219  
220 return undef;
221 }
222  
223 # Do the DNS lookup
224 my $query = $resolver->search('_lsid._tcp.' . $authority_id, 'SRV'); # $query is a Net::DNS::Packet object
225 my $authority;
226 if ($query) {
227  
228 $self->recordError( 'Unable to resolve authority to SRV record: ' .
229 ($resolver->errorstring() || 'No details available from $resolver') );
230  
231 $self->addStackTrace();
232 my $rr = ($query->answer())[0]; # rr is a Net::DNS::RR::SRV object
233  
234 # Create an authority interface from the answer
235 $authority = LS::Authority->new_by_hostname($rr->target(), $rr->port());
236  
237 # return undef; # I want to temporarily over-ride this and allow http://domain/authority to be allowed!
238 } else { # okay, if all else fails, is there a presumptive authority at http://example.org:80/authority ?
239 $query = $resolver->search($authority_id); # $query is a Net::DNS::Packet object
240 if ($query){
241 use LWP::Simple;
242 my ($content_type, $document_length, $modified_time, $expires, $server) = head("http://$authority_id/authority");
243 return undef unless $document_length; # if nothing responds then finally give up and return undef
244 }
245 # if we are here, then something must have responded at http://$authority_id:80/authority
246 # so lets hope that its an LSID resolver!
247 $authority = LS::Authority->new_by_hostname($authority_id, '80');
248 }
249 unless(UNIVERSAL::isa($authority, 'LS::Authority')) {
250
251 $self->recordError('Unable to create new authority from host information');
252 $self->addStackTrace();
253
254 return undef;
255 }
256  
257 $self->authorityCache()->{$authority_id} = $authority
258 if($self->cacheAuthorities());
259  
260 return $authority;
261 }
262
263  
264 #
265 # resolve_authority - Synonym for resolveAuthority.
266 #
267 sub resolve_authority {
268  
269 my $self = shift;
270 return $self->resolveAuthority(@_);
271 }
272  
273  
274  
275  
276 1;
277  
278  
279 __END__