1 |
williamc |
1.7 |
# Interface
|
2 |
williamc |
1.1 |
# ---------
|
3 |
williamc |
1.7 |
# new(cache) : A new urlhandler with a defined default cahce directory
|
4 |
williamc |
1.11 |
# download(url,[location]) : as get but always download
|
5 |
williamc |
1.9 |
# 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 |
williamc |
1.15.2.2 |
# setbase(urlstring) : set a base url type - return the url object
|
9 |
williamc |
1.1 |
# unsetbase(type) : deactivate a previously set base
|
10 |
williamc |
1.9 |
# currentbase(type) : return the current base for the given type
|
11 |
williamc |
1.15.2.4 |
# expandurl(urlstring) : return the base expanded URLclass of the given string
|
12 |
williamc |
1.1 |
#
|
13 |
|
|
# ----------------------------------------------------------------------
|
14 |
|
|
|
15 |
williamc |
1.5 |
package URL::URLhandler;
|
16 |
williamc |
1.1 |
require 5.004;
|
17 |
|
|
use Utilities::AddDir;
|
18 |
williamc |
1.9 |
use URL::URLcache;
|
19 |
|
|
use URL::URLclass;
|
20 |
williamc |
1.1 |
use Carp;
|
21 |
|
|
|
22 |
|
|
sub new {
|
23 |
|
|
my $class=shift;
|
24 |
|
|
my $cache=shift;
|
25 |
|
|
$self={};
|
26 |
|
|
bless $self, $class;
|
27 |
|
|
$self->init($cache);
|
28 |
|
|
return $self;
|
29 |
|
|
}
|
30 |
|
|
|
31 |
|
|
sub init {
|
32 |
|
|
use Utilities::AddDir;
|
33 |
|
|
my $self=shift;
|
34 |
|
|
my $cache=shift;
|
35 |
|
|
$self->{cache}=$cache;
|
36 |
williamc |
1.7 |
$self->{cachestore}=$self->{cache}->filestore();
|
37 |
williamc |
1.2 |
use URL::URL_cvs;
|
38 |
|
|
use URL::URL_file;
|
39 |
williamc |
1.13 |
use URL::URL_test;
|
40 |
williamc |
1.2 |
$self->{urlmodules}={
|
41 |
williamc |
1.5 |
'cvs' => 'URL::URL_cvs',
|
42 |
williamc |
1.7 |
'file' => 'URL::URL_file',
|
43 |
williamc |
1.13 |
'test' => 'URL::URL_test'
|
44 |
williamc |
1.2 |
};
|
45 |
williamc |
1.1 |
}
|
46 |
|
|
|
47 |
williamc |
1.7 |
sub get {
|
48 |
williamc |
1.4 |
my $self=shift;
|
49 |
williamc |
1.9 |
my $origurl=shift;
|
50 |
williamc |
1.11 |
my $file="";
|
51 |
williamc |
1.9 |
|
52 |
williamc |
1.11 |
my $url=URL::URLclass->new($origurl);
|
53 |
|
|
my $type=$url->type();
|
54 |
williamc |
1.15 |
$url->merge($self->currentbase($type));
|
55 |
williamc |
1.11 |
my $fullurl=$url->url();
|
56 |
|
|
|
57 |
williamc |
1.12 |
$file=$self->{cache}->file($fullurl);
|
58 |
|
|
if ( $file eq "" ) {
|
59 |
williamc |
1.11 |
($fullurl,$file)=$self->download($origurl, @_);
|
60 |
|
|
}
|
61 |
|
|
return ($fullurl, $file);
|
62 |
|
|
}
|
63 |
|
|
|
64 |
|
|
sub download {
|
65 |
|
|
my $self=shift;
|
66 |
|
|
my $origurl=shift;
|
67 |
|
|
|
68 |
|
|
# Process the URL string
|
69 |
|
|
my $url=URL::URLclass->new($origurl);
|
70 |
williamc |
1.9 |
my $type=$url->type();
|
71 |
|
|
$urltypehandler=$self->_typehandler($type);
|
72 |
williamc |
1.15 |
$url->merge($self->currentbase($type));
|
73 |
williamc |
1.15.2.6 |
#print "Attempting download of ".$url->url()."\n";
|
74 |
williamc |
1.9 |
|
75 |
|
|
# Generate a location name if not provided
|
76 |
williamc |
1.15.2.5 |
my $nocache=1;
|
77 |
williamc |
1.9 |
if ( @_ ) {
|
78 |
|
|
$location=shift;
|
79 |
williamc |
1.12 |
$nocache=0; # dont cache if downloaded to an external location
|
80 |
williamc |
1.9 |
}
|
81 |
|
|
else {
|
82 |
|
|
$location=$self->{cache}->filename($url->url());
|
83 |
|
|
}
|
84 |
williamc |
1.11 |
# -- get the file from the appropriate handler
|
85 |
williamc |
1.9 |
if ( defined $urltypehandler ) {
|
86 |
|
|
# Call the download module
|
87 |
|
|
$file=eval{$urltypehandler->get($url, $location)};
|
88 |
|
|
}
|
89 |
williamc |
1.4 |
|
90 |
williamc |
1.9 |
# now register it in the cache if successful
|
91 |
williamc |
1.12 |
if ( $file && $nocache) {
|
92 |
williamc |
1.15.2.5 |
#$self->{cache}->store($url->url(), $location);
|
93 |
|
|
$self->{cache}->store($url->url(), $file);
|
94 |
williamc |
1.9 |
}
|
95 |
williamc |
1.10 |
return ($url->url(), $file);
|
96 |
williamc |
1.15.2.4 |
}
|
97 |
|
|
|
98 |
|
|
sub expandurl {
|
99 |
|
|
my $self=shift;
|
100 |
|
|
my $urlstring=shift;
|
101 |
|
|
|
102 |
|
|
my $url=URL::URLclass->new($urlstring);
|
103 |
|
|
my $type=$url->type();
|
104 |
|
|
$url->merge($self->currentbase($type));
|
105 |
|
|
return $url;
|
106 |
williamc |
1.4 |
}
|
107 |
|
|
|
108 |
williamc |
1.9 |
sub setbase {
|
109 |
williamc |
1.1 |
my $self=shift;
|
110 |
williamc |
1.15 |
my $partialurl=shift;
|
111 |
williamc |
1.7 |
|
112 |
williamc |
1.15 |
my $base=URL::URLclass->new($partialurl);
|
113 |
|
|
my $type=$base->type();
|
114 |
williamc |
1.9 |
$self->checktype($type);
|
115 |
williamc |
1.15 |
# make a new base-url object
|
116 |
williamc |
1.10 |
push @{$self->{"basestack"}{$type}}, $base;
|
117 |
williamc |
1.15.2.2 |
return $base;
|
118 |
williamc |
1.9 |
}
|
119 |
williamc |
1.1 |
|
120 |
williamc |
1.9 |
sub unsetbase {
|
121 |
|
|
my $self=shift;
|
122 |
|
|
my $type=shift;
|
123 |
|
|
my $oref;
|
124 |
williamc |
1.7 |
|
125 |
williamc |
1.9 |
$self->checktype($type);
|
126 |
|
|
# pop off the stack and call the unset base method
|
127 |
|
|
if ( $#{$self->{basestack}{$type}} >=0 ) {
|
128 |
|
|
my $base=pop @{$self->{basestack}{$type}};
|
129 |
|
|
undef $base;
|
130 |
|
|
}
|
131 |
|
|
else {
|
132 |
|
|
die "URLhandler error: Unable to unset type $type\n";
|
133 |
williamc |
1.4 |
}
|
134 |
williamc |
1.10 |
# remove the stack if its empty
|
135 |
|
|
if ( $#{$self->{basestack}{$type}} == -1 ) {
|
136 |
|
|
delete $self->{basestack}{$type};
|
137 |
|
|
}
|
138 |
williamc |
1.1 |
}
|
139 |
|
|
|
140 |
williamc |
1.9 |
sub currentbase {
|
141 |
williamc |
1.1 |
my $self=shift;
|
142 |
|
|
my $type=shift;
|
143 |
williamc |
1.9 |
my $rv;
|
144 |
williamc |
1.1 |
|
145 |
williamc |
1.9 |
if ( exists $self->{basestack}{$type} ) {
|
146 |
williamc |
1.10 |
$rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}];
|
147 |
williamc |
1.1 |
}
|
148 |
|
|
else {
|
149 |
williamc |
1.15 |
$rv=undef;
|
150 |
williamc |
1.1 |
}
|
151 |
williamc |
1.9 |
return $rv;
|
152 |
williamc |
1.1 |
}
|
153 |
|
|
|
154 |
williamc |
1.9 |
sub checktype($type) {
|
155 |
williamc |
1.1 |
my $self=shift;
|
156 |
williamc |
1.9 |
my $type=shift;
|
157 |
williamc |
1.1 |
|
158 |
williamc |
1.9 |
# Check type is supported
|
159 |
williamc |
1.2 |
if ( ! exists $self->{urlmodules}{$type} ) {
|
160 |
williamc |
1.9 |
die "URLhandler error: Unsupported type $type\n";
|
161 |
williamc |
1.1 |
}
|
162 |
|
|
}
|
163 |
|
|
|
164 |
williamc |
1.9 |
sub _typehandler {
|
165 |
williamc |
1.7 |
my $self=shift;
|
166 |
williamc |
1.8 |
my $type=shift;
|
167 |
williamc |
1.7 |
|
168 |
williamc |
1.9 |
$self->checktype($type);
|
169 |
williamc |
1.7 |
|
170 |
williamc |
1.9 |
# instantiate only if it dosnt already exist;
|
171 |
williamc |
1.10 |
if ( exists $self->{'urlobjs'}{$type} ) {
|
172 |
|
|
$self->{'urlobjs'}{$type};
|
173 |
|
|
}
|
174 |
|
|
else {
|
175 |
|
|
$self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new();
|
176 |
|
|
}
|
177 |
williamc |
1.1 |
}
|