1 |
< |
# url handler -> returns the location of thefilu Interface |
1 |
> |
# url handler -> returns the location of the file |
2 |
> |
# Interface |
3 |
|
# --------- |
4 |
< |
# new() : |
5 |
< |
# new(cachedir) : A new urlhandler with a defined default cahce directory |
6 |
< |
# get(url) : download from the specified url to the default cache |
6 |
< |
# get(url,dirlocation) : download to the specified directory |
4 |
> |
# new(cache) : A new urlhandler with a defined default cahce directory |
5 |
> |
# get(url) : download from the specified url to the default cache |
6 |
> |
# getto(url,dirlocation): download to the specified directory - no cache registry |
7 |
|
# setbase(type,variablehash) : set the url defaults for a specific url type |
8 |
< |
# arguments are dependenton type |
8 |
> |
# arguments are dependent on type |
9 |
|
# unsetbase(type) : deactivate a previously set base |
10 |
– |
# setcache(dir) : set the default cache location |
10 |
|
# |
11 |
|
# ---------------------------------------------------------------------- |
12 |
|
# returns file location - or crashes out |
16 |
|
package URL::URLhandler; |
17 |
|
require 5.004; |
18 |
|
use Utilities::AddDir; |
19 |
+ |
use URL::URLUtilities; |
20 |
|
use URL::URL_base; |
21 |
|
use Carp; |
22 |
|
|
30 |
|
} |
31 |
|
|
32 |
|
sub init { |
33 |
+ |
use URL::URLcache; |
34 |
|
use Utilities::AddDir; |
35 |
|
my $self=shift; |
36 |
|
my $cache=shift; |
36 |
– |
if (! defined $cache ) { $cache="/tmp/SCRAMcache" }; # default cache |
37 |
– |
AddDir::adddir($cache); |
37 |
|
$self->{cache}=$cache; |
38 |
+ |
$self->{cachestore}=$self->{cache}->filestore(); |
39 |
|
use URL::URL_cvs; |
40 |
|
use URL::URL_cvsfile; |
41 |
|
use URL::URL_file; |
42 |
+ |
use URL::URL_filed; |
43 |
|
$self->{urlmodules}={ |
44 |
|
'cvsfile' => 'URL::URL_cvsfile', |
45 |
|
'cvs' => 'URL::URL_cvs', |
46 |
< |
'file' => 'URL::URL_file' |
46 |
> |
'file' => 'URL::URL_file', |
47 |
> |
'filed' => 'URL::URL_filed' |
48 |
|
}; |
49 |
|
$self->{filebase}=""; |
50 |
|
$self->setbase("file", {}); # Base file as default |
51 |
+ |
$self->setbase("filed", {}); # Base file as default |
52 |
|
} |
53 |
|
|
54 |
< |
sub setcache { |
54 |
> |
# |
55 |
> |
sub get { |
56 |
|
my $self=shift; |
57 |
< |
my $cache=shift; |
57 |
> |
my $url=shift; |
58 |
|
|
59 |
< |
$self->{cache}=$cache; |
59 |
> |
|
60 |
> |
my ($urlstring, $type, $urltypehandler, $base, $version) |
61 |
> |
=$self->_parseurl($url); # get a unique url string |
62 |
> |
my $rv=""; |
63 |
> |
$rv=$self->getto($url,$self->{cache}->filename($urlstring)); |
64 |
> |
|
65 |
> |
# now register it in the cache if successful |
66 |
> |
if ( $rv ne "" ) { |
67 |
> |
print "Storing ".$urlstring.",".$rv."\n"; |
68 |
> |
$self->{cache}->store($urlstring, $rv); |
69 |
> |
} |
70 |
> |
return $rv; |
71 |
|
} |
72 |
|
|
73 |
< |
sub get ($@) { |
73 |
> |
sub getto ($@) { |
74 |
|
my $self=shift; |
75 |
|
my $origurl=shift; |
76 |
|
my $dirname=shift; |
77 |
+ |
|
78 |
|
my $rest; |
79 |
|
my $type; |
64 |
– |
my $url; |
80 |
|
my $version; |
81 |
|
my $rv=""; |
82 |
|
|
83 |
< |
if ( ! defined $dirname ) { |
84 |
< |
$dirname=$self->{cache}; |
85 |
< |
} |
86 |
< |
chdir $dirname or carp "Unable to Enter Directory $dirname $!\n"; |
87 |
< |
chomp $origurl; |
88 |
< |
# get our version info from the url (after last ??) |
89 |
< |
( $url, $version) = split /\?\?/, $origurl; |
75 |
< |
if ( $url=~/:/ ) { |
76 |
< |
($type,$rest) = split /:/, $url; |
77 |
< |
} |
78 |
< |
else { |
79 |
< |
$type='label'; |
80 |
< |
$rest=$url.":".$version; |
81 |
< |
} |
82 |
< |
if ( ! ( exists $self->{'urlmodules'}{$type}) ) { |
83 |
< |
print "URLhandler: Unsupported type $type\n"; |
84 |
< |
carp; |
85 |
< |
} |
86 |
< |
else { |
87 |
< |
if ( $#{$self->{urlostack}{$type}} < 0 ) { |
88 |
< |
print "URLhandler : base not set for type $type \n"; |
89 |
< |
} |
90 |
< |
else { |
91 |
< |
$rv= |
92 |
< |
eval{${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}]}-> |
93 |
< |
get($rest, $dirname); |
94 |
< |
} |
83 |
> |
# Process the URL string |
84 |
> |
my ($urlstring, $type, $urltypehandler, $base, $version) |
85 |
> |
=$self->_parseurl($origurl); |
86 |
> |
|
87 |
> |
if ( defined $urltypehandler ) { |
88 |
> |
# Call the download module |
89 |
> |
$rv=eval{$urltypehandler->get($base.(($version ne "")?"\?".$version:""), $dirname); }; |
90 |
|
} |
91 |
< |
if ( $rv ne "" ) { |
92 |
< |
$rv=$dirname."/".$rv; |
91 |
> |
|
92 |
> |
# Check the return type |
93 |
> |
if ( defined $rv ) { |
94 |
> |
# if ( $rv!~/^\// ) { |
95 |
> |
# $rv=$dirname."/".$rv; |
96 |
> |
# } |
97 |
|
} |
98 |
|
return $rv; |
99 |
|
} |
141 |
|
} |
142 |
|
} |
143 |
|
|
144 |
< |
# ------------------------ General Support Routines ---------------------------- |
144 |
> |
# -------------------- Support Routines (private Methods) ------------- |
145 |
|
|
146 |
< |
sub cachefilename { |
147 |
< |
my $self=shift; |
148 |
< |
use File::Basename; |
149 |
< |
use Utilities::AddDir; |
150 |
< |
my $filebase=dirname($rest); |
151 |
< |
$cache="/tmp/williamc/urlhandler$$"; |
152 |
< |
adddir($cache); |
153 |
< |
$filename=$cache."/".$filebase; |
154 |
< |
} |
146 |
> |
# |
147 |
> |
# Process the URL string into its component parts |
148 |
> |
# |
149 |
> |
sub _parseurl { |
150 |
> |
my $self=shift; |
151 |
> |
my $origurl=shift; |
152 |
> |
chomp $origurl; |
153 |
> |
|
154 |
> |
my ($type,$rest,$cmdstring)=URLUtilities::parseURL($origurl); |
155 |
> |
my $urlstring=""; |
156 |
> |
my $url; |
157 |
> |
|
158 |
> |
$base=""; |
159 |
|
|
160 |
+ |
# check type are supported |
161 |
+ |
if ( ! ( exists $self->{'urlmodules'}{$type}) ) { |
162 |
+ |
print "URLhandler: Unsupported type $type\n"; |
163 |
+ |
die; |
164 |
+ |
} |
165 |
+ |
else { |
166 |
+ |
if ( $#{$self->{urlostack}{$type}} < 0 ) { |
167 |
+ |
print "URLhandler : base not set for type $type \n"; |
168 |
+ |
die; |
169 |
+ |
} |
170 |
+ |
else { |
171 |
+ |
# ---- type is supported |
172 |
+ |
$urltypehandler= |
173 |
+ |
${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}]; |
174 |
+ |
$base=$urltypehandler->baseurl(); |
175 |
+ |
} |
176 |
+ |
} |
177 |
+ |
|
178 |
+ |
my $urlbase=($base ne ""?$base."/":"").$rest; |
179 |
+ |
$urlstring=$type.":".$urlbase.($cmdstring ne ""?"?".$cmdstring:""); |
180 |
+ |
return $urlstring, $type, $urltypehandler, $urlbase, $cmdstring; |
181 |
+ |
} |