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