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 |
11 |
< |
# http: |
12 |
< |
# file: |
13 |
< |
# cvs: servername,servertype [ ,user,passkey ] |
14 |
< |
# label: |
4 |
> |
# new(cache) : A new urlhandler with a defined default cahce directory |
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(type,variablehash) : set the url defaults for a specific url type |
9 |
> |
# arguments are dependent on type |
10 |
|
# unsetbase(type) : deactivate a previously set base |
11 |
+ |
# currentbase(type) : return the current base for the given type |
12 |
|
# |
13 |
|
# ---------------------------------------------------------------------- |
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. |
14 |
|
|
15 |
< |
package URLhandler; |
15 |
> |
package URL::URLhandler; |
16 |
|
require 5.004; |
17 |
|
use Utilities::AddDir; |
18 |
+ |
use URL::URLcache; |
19 |
+ |
use URL::URLclass; |
20 |
+ |
use URL::URLbase; |
21 |
|
use Carp; |
22 |
|
|
23 |
|
sub new { |
33 |
|
use Utilities::AddDir; |
34 |
|
my $self=shift; |
35 |
|
my $cache=shift; |
40 |
– |
if (! defined $cache ) { $cache="/tmp/SCRAMcache" }; # default cache |
41 |
– |
AddDir::adddir($cache); |
36 |
|
$self->{cache}=$cache; |
37 |
+ |
$self->{dummybase}=URL::URLbase->new({}); |
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 |
< |
'cvs' => 'URL_cvs', |
45 |
< |
'file' => 'URL_file' |
44 |
> |
'cvsfile' => 'URL::URL_cvsfile', |
45 |
> |
'cvs' => 'URL::URL_cvs', |
46 |
> |
'file' => 'URL::URL_file', |
47 |
> |
'filed' => 'URL::URL_filed' |
48 |
|
}; |
49 |
– |
$self->{filebase}=""; |
50 |
– |
$self->setbase("file", {}); # Base file as default |
49 |
|
} |
50 |
|
|
51 |
< |
sub get ($@) { |
51 |
> |
sub get { |
52 |
|
my $self=shift; |
53 |
|
my $origurl=shift; |
54 |
< |
my $filename=shift; |
55 |
< |
my $rest; |
56 |
< |
my $type; |
57 |
< |
my $url; |
58 |
< |
my $version; |
59 |
< |
|
60 |
< |
if ( ! defined $filename ) { |
61 |
< |
$filename=$self->{cache}; |
62 |
< |
} |
63 |
< |
chomp $origurl; |
64 |
< |
# 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 |
< |
} |
54 |
> |
|
55 |
> |
# Process the URL string |
56 |
> |
$url=URL::URLclass->new($origurl); |
57 |
> |
my $type=$url->type(); |
58 |
> |
$urltypehandler=$self->_typehandler($type); |
59 |
> |
$url->expandurl($self->currentbase($type)); |
60 |
> |
|
61 |
> |
# Generate a location name if not provided |
62 |
> |
if ( @_ ) { |
63 |
> |
$location=shift; |
64 |
> |
} |
65 |
|
else { |
66 |
< |
eval{${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}]}-> |
81 |
< |
get($rest, $filename); |
66 |
> |
$location=$self->{cache}->filename($url->url()); |
67 |
|
} |
68 |
+ |
# -- get the file form the appropriate handler |
69 |
+ |
if ( defined $urltypehandler ) { |
70 |
+ |
# Call the download module |
71 |
+ |
$file=eval{$urltypehandler->get($url, $location)}; |
72 |
+ |
} |
73 |
+ |
|
74 |
+ |
# now register it in the cache if successful |
75 |
+ |
if ( $file ) { |
76 |
+ |
$self->{cache}->store($url->url(), $file); |
77 |
+ |
} |
78 |
+ |
return ($url->url(), $file); |
79 |
|
} |
80 |
|
|
81 |
|
sub setbase { |
84 |
|
my @args=@_; |
85 |
|
my $oref; |
86 |
|
|
87 |
< |
# Check type is supported |
88 |
< |
if ( ! exists $self->{urlmodules}{$type} ) { |
89 |
< |
print "URLhandler error: Unsupported type $type\n"; |
90 |
< |
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 |
< |
} |
87 |
> |
$self->checktype($type); |
88 |
> |
# make a new base object |
89 |
> |
my $base=URL::URLbase->new(@_); |
90 |
> |
push @{$self->{"basestack"}{$type}}, $base; |
91 |
|
} |
92 |
|
|
93 |
|
sub unsetbase { |
95 |
|
my $type=shift; |
96 |
|
my $oref; |
97 |
|
|
98 |
< |
# Check type is supported |
98 |
> |
$self->checktype($type); |
99 |
> |
# pop off the stack and call the unset base method |
100 |
> |
if ( $#{$self->{basestack}{$type}} >=0 ) { |
101 |
> |
my $base=pop @{$self->{basestack}{$type}}; |
102 |
> |
undef $base; |
103 |
> |
} |
104 |
> |
else { |
105 |
> |
die "URLhandler error: Unable to unset type $type\n"; |
106 |
> |
} |
107 |
> |
# remove the stack if its empty |
108 |
> |
if ( $#{$self->{basestack}{$type}} == -1 ) { |
109 |
> |
delete $self->{basestack}{$type}; |
110 |
> |
} |
111 |
> |
} |
112 |
> |
|
113 |
> |
sub currentbase { |
114 |
> |
my $self=shift; |
115 |
> |
my $type=shift; |
116 |
> |
my $rv; |
117 |
> |
|
118 |
> |
if ( exists $self->{basestack}{$type} ) { |
119 |
> |
$rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}]; |
120 |
> |
} |
121 |
> |
else { |
122 |
> |
$rv=$self->{dummybase}; |
123 |
> |
} |
124 |
> |
return $rv; |
125 |
> |
} |
126 |
> |
|
127 |
> |
sub checktype($type) { |
128 |
> |
my $self=shift; |
129 |
> |
my $type=shift; |
130 |
> |
|
131 |
> |
# Check type is supported |
132 |
|
if ( ! exists $self->{urlmodules}{$type} ) { |
133 |
< |
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 |
< |
} |
133 |
> |
die "URLhandler error: Unsupported type $type\n"; |
134 |
|
} |
135 |
|
} |
136 |
|
|
137 |
< |
# ------------------------ General Support Routines ---------------------------- |
137 |
> |
sub _typehandler { |
138 |
> |
my $self=shift; |
139 |
> |
my $type=shift; |
140 |
> |
|
141 |
> |
$self->checktype($type); |
142 |
|
|
143 |
< |
sub cachefilename { |
144 |
< |
my $self=shift; |
145 |
< |
use File::Basename; |
146 |
< |
use Utilities::AddDir; |
147 |
< |
my $filebase=dirname($rest); |
148 |
< |
$cache="/tmp/williamc/urlhandler$$"; |
149 |
< |
adddir($cache); |
137 |
< |
$filename=$cache."/".$filebase; |
143 |
> |
# instantiate only if it dosnt already exist; |
144 |
> |
if ( exists $self->{'urlobjs'}{$type} ) { |
145 |
> |
$self->{'urlobjs'}{$type}; |
146 |
> |
} |
147 |
> |
else { |
148 |
> |
$self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new(); |
149 |
> |
} |
150 |
|
} |
139 |
– |
|