1 |
|
# url handler -> returns the location of the file |
2 |
– |
# |
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 |
7 |
< |
# get(url,location) : download to the specified directory |
8 |
< |
# setbase(type,@args) : set the url defaults for a specific url type |
9 |
< |
# arguments are dependent on type |
10 |
< |
# http: |
12 |
< |
# file: |
13 |
< |
# cvs: servername,servertype [ ,user,passkey ] |
14 |
< |
# label: |
4 |
> |
# new(cache) : A new urlhandler with a defined default cahce directory |
5 |
> |
# download(url,[location]) : as get but always download |
6 |
> |
# get(url,[location]) : download from the specified url to cache or location |
7 |
> |
# return the full url path name incl. any base expansion |
8 |
> |
# and the filename downloaded to |
9 |
> |
# setbase(type,variablehash) : set the url defaults for a specific url type |
10 |
> |
# arguments are dependent on type |
11 |
|
# unsetbase(type) : deactivate a previously set base |
12 |
+ |
# currentbase(type) : return the current base for the given type |
13 |
|
# |
14 |
|
# ---------------------------------------------------------------------- |
18 |
– |
# returns file location - or crashes out |
19 |
– |
# can pass a file name for the item to be stored as |
20 |
– |
# if not then stores in a default cache. |
15 |
|
|
16 |
< |
package URLhandler; |
16 |
> |
package URL::URLhandler; |
17 |
|
require 5.004; |
18 |
|
use Utilities::AddDir; |
19 |
+ |
use URL::URLcache; |
20 |
+ |
use URL::URLclass; |
21 |
+ |
use URL::URLbase; |
22 |
|
use Carp; |
23 |
|
|
24 |
|
sub new { |
34 |
|
use Utilities::AddDir; |
35 |
|
my $self=shift; |
36 |
|
my $cache=shift; |
40 |
– |
if (! defined $cache ) { $cache="/tmp/SCRAMcache" }; # default cache |
41 |
– |
AddDir::adddir($cache); |
37 |
|
$self->{cache}=$cache; |
38 |
+ |
$self->{dummybase}=URL::URLbase->new({}); |
39 |
+ |
$self->{cachestore}=$self->{cache}->filestore(); |
40 |
|
use URL::URL_cvs; |
41 |
+ |
use URL::URL_cvsfile; |
42 |
|
use URL::URL_file; |
43 |
+ |
use URL::URL_filed; |
44 |
|
$self->{urlmodules}={ |
45 |
< |
'cvs' => 'URL_cvs', |
46 |
< |
'file' => 'URL_file' |
45 |
> |
'cvsfile' => 'URL::URL_cvsfile', |
46 |
> |
'cvs' => 'URL::URL_cvs', |
47 |
> |
'file' => 'URL::URL_file', |
48 |
> |
'filed' => 'URL::URL_filed' |
49 |
|
}; |
49 |
– |
$self->{filebase}=""; |
50 |
– |
$self->setbase("file", {}); # Base file as default |
50 |
|
} |
51 |
|
|
52 |
< |
sub get ($@) { |
52 |
> |
sub get { |
53 |
|
my $self=shift; |
54 |
|
my $origurl=shift; |
55 |
< |
my $filename=shift; |
56 |
< |
my $rest; |
57 |
< |
my $type; |
58 |
< |
my $url; |
59 |
< |
my $version; |
60 |
< |
|
61 |
< |
if ( ! defined $filename ) { |
62 |
< |
$filename=$self->{cache}; |
63 |
< |
} |
64 |
< |
chomp $origurl; |
65 |
< |
# get our version info from the url (after last ??) |
66 |
< |
( $url, $version) = split /\?\?/, $origurl; |
67 |
< |
if ( $url=~/:/ ) { |
68 |
< |
($type,$rest) = split /:/, $url; |
69 |
< |
} |
70 |
< |
else { |
71 |
< |
$type='label'; |
72 |
< |
$rest=$url.":".$version; |
73 |
< |
} |
74 |
< |
if ( ! ( exists $self->{'urlmodules'}{$type}) ) { |
75 |
< |
print "URLhandler: Unsupported type $type\n"; |
76 |
< |
carp; |
77 |
< |
} |
55 |
> |
my $file=""; |
56 |
> |
|
57 |
> |
my $url=URL::URLclass->new($origurl); |
58 |
> |
my $type=$url->type(); |
59 |
> |
$url->expandurl($self->currentbase($type)); |
60 |
> |
my $fullurl=$url->url(); |
61 |
> |
|
62 |
> |
$file=$self->{cache}->file($fullurl); |
63 |
> |
if ( $file eq "" ) { |
64 |
> |
($fullurl,$file)=$self->download($origurl, @_); |
65 |
> |
} |
66 |
> |
return ($fullurl, $file); |
67 |
> |
} |
68 |
> |
|
69 |
> |
sub download { |
70 |
> |
my $self=shift; |
71 |
> |
my $origurl=shift; |
72 |
> |
|
73 |
> |
# Process the URL string |
74 |
> |
my $url=URL::URLclass->new($origurl); |
75 |
> |
my $type=$url->type(); |
76 |
> |
$urltypehandler=$self->_typehandler($type); |
77 |
> |
$url->expandurl($self->currentbase($type)); |
78 |
> |
|
79 |
> |
# Generate a location name if not provided |
80 |
> |
$nocache=1; |
81 |
> |
if ( @_ ) { |
82 |
> |
$location=shift; |
83 |
> |
$nocache=0; # dont cache if downloaded to an external location |
84 |
> |
} |
85 |
|
else { |
86 |
< |
eval{${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}]}-> |
81 |
< |
get($rest, $filename); |
86 |
> |
$location=$self->{cache}->filename($url->url()); |
87 |
|
} |
88 |
+ |
# -- get the file from the appropriate handler |
89 |
+ |
if ( defined $urltypehandler ) { |
90 |
+ |
# Call the download module |
91 |
+ |
$file=eval{$urltypehandler->get($url, $location)}; |
92 |
+ |
} |
93 |
+ |
|
94 |
+ |
# now register it in the cache if successful |
95 |
+ |
if ( $file && $nocache) { |
96 |
+ |
$self->{cache}->store($url->url(), $location); |
97 |
+ |
} |
98 |
+ |
return ($url->url(), $file); |
99 |
|
} |
100 |
|
|
101 |
|
sub setbase { |
104 |
|
my @args=@_; |
105 |
|
my $oref; |
106 |
|
|
107 |
< |
# Check type is supported |
108 |
< |
if ( ! exists $self->{urlmodules}{$type} ) { |
109 |
< |
print "URLhandler error: Unsupported type $type\n"; |
110 |
< |
return 1; |
95 |
< |
} |
96 |
< |
else { |
97 |
< |
# A new URL object - pushed onto the stack |
98 |
< |
$oref=eval{$self->{urlmodules}{$type}}->new(); |
99 |
< |
push @{$self->{urlostack}{$type}}, $oref; |
100 |
< |
$oref->setbase(@args); |
101 |
< |
} |
107 |
> |
$self->checktype($type); |
108 |
> |
# make a new base object |
109 |
> |
my $base=URL::URLbase->new(@_); |
110 |
> |
push @{$self->{"basestack"}{$type}}, $base; |
111 |
|
} |
112 |
|
|
113 |
|
sub unsetbase { |
115 |
|
my $type=shift; |
116 |
|
my $oref; |
117 |
|
|
118 |
< |
# Check type is supported |
118 |
> |
$self->checktype($type); |
119 |
> |
# pop off the stack and call the unset base method |
120 |
> |
if ( $#{$self->{basestack}{$type}} >=0 ) { |
121 |
> |
my $base=pop @{$self->{basestack}{$type}}; |
122 |
> |
undef $base; |
123 |
> |
} |
124 |
> |
else { |
125 |
> |
die "URLhandler error: Unable to unset type $type\n"; |
126 |
> |
} |
127 |
> |
# remove the stack if its empty |
128 |
> |
if ( $#{$self->{basestack}{$type}} == -1 ) { |
129 |
> |
delete $self->{basestack}{$type}; |
130 |
> |
} |
131 |
> |
} |
132 |
> |
|
133 |
> |
sub currentbase { |
134 |
> |
my $self=shift; |
135 |
> |
my $type=shift; |
136 |
> |
my $rv; |
137 |
> |
|
138 |
> |
if ( exists $self->{basestack}{$type} ) { |
139 |
> |
$rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}]; |
140 |
> |
} |
141 |
> |
else { |
142 |
> |
$rv=$self->{dummybase}; |
143 |
> |
} |
144 |
> |
return $rv; |
145 |
> |
} |
146 |
> |
|
147 |
> |
sub checktype($type) { |
148 |
> |
my $self=shift; |
149 |
> |
my $type=shift; |
150 |
> |
|
151 |
> |
# Check type is supported |
152 |
|
if ( ! exists $self->{urlmodules}{$type} ) { |
153 |
< |
print "URLhandler error: Unsupported type $type\n"; |
112 |
< |
return 1; |
113 |
< |
} |
114 |
< |
else { |
115 |
< |
# pop off the stack and call the unset base method |
116 |
< |
if ( $#{$self->{urlostack}{$type}} >=0 ) { |
117 |
< |
$oref=pop @{$self->{urlostack}{$type}}; |
118 |
< |
$oref->unsetbase(); |
119 |
< |
undef $oref; |
120 |
< |
} |
121 |
< |
else { |
122 |
< |
print "URLhandler error: Unable to unset type $type\n"; |
123 |
< |
return 1; |
124 |
< |
} |
153 |
> |
die "URLhandler error: Unsupported type $type\n"; |
154 |
|
} |
155 |
|
} |
156 |
|
|
157 |
< |
# ------------------------ General Support Routines ---------------------------- |
157 |
> |
sub _typehandler { |
158 |
> |
my $self=shift; |
159 |
> |
my $type=shift; |
160 |
|
|
161 |
< |
sub cachefilename { |
131 |
< |
my $self=shift; |
132 |
< |
use File::Basename; |
133 |
< |
use Utilities::AddDir; |
134 |
< |
my $filebase=dirname($rest); |
135 |
< |
$cache="/tmp/williamc/urlhandler$$"; |
136 |
< |
adddir($cache); |
137 |
< |
$filename=$cache."/".$filebase; |
138 |
< |
} |
161 |
> |
$self->checktype($type); |
162 |
|
|
163 |
+ |
# instantiate only if it dosnt already exist; |
164 |
+ |
if ( exists $self->{'urlobjs'}{$type} ) { |
165 |
+ |
$self->{'urlobjs'}{$type}; |
166 |
+ |
} |
167 |
+ |
else { |
168 |
+ |
$self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new(); |
169 |
+ |
} |
170 |
+ |
} |