1 |
williamc |
1.7 |
# url handler -> returns the location of the file
|
2 |
|
|
# Interface
|
3 |
williamc |
1.1 |
# ---------
|
4 |
williamc |
1.7 |
# new(cache) : A new urlhandler with a defined default cahce directory
|
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.4 |
# setbase(type,variablehash) : set the url defaults for a specific url type
|
9 |
williamc |
1.7 |
# arguments are dependent on type
|
10 |
williamc |
1.1 |
# unsetbase(type) : deactivate a previously set base
|
11 |
williamc |
1.9 |
# currentbase(type) : return the current base for the given type
|
12 |
williamc |
1.1 |
#
|
13 |
|
|
# ----------------------------------------------------------------------
|
14 |
|
|
# returns file location - or crashes out
|
15 |
|
|
# can pass a file name for the item to be stored as
|
16 |
|
|
# if not then stores in a default cache.
|
17 |
|
|
|
18 |
williamc |
1.5 |
package URL::URLhandler;
|
19 |
williamc |
1.1 |
require 5.004;
|
20 |
|
|
use Utilities::AddDir;
|
21 |
williamc |
1.9 |
use URL::URLcache;
|
22 |
|
|
use URL::URLclass;
|
23 |
|
|
use URL::URLbase;
|
24 |
williamc |
1.1 |
use Carp;
|
25 |
|
|
|
26 |
|
|
sub new {
|
27 |
|
|
my $class=shift;
|
28 |
|
|
my $cache=shift;
|
29 |
|
|
$self={};
|
30 |
|
|
bless $self, $class;
|
31 |
|
|
$self->init($cache);
|
32 |
|
|
return $self;
|
33 |
|
|
}
|
34 |
|
|
|
35 |
|
|
sub init {
|
36 |
|
|
use Utilities::AddDir;
|
37 |
|
|
my $self=shift;
|
38 |
|
|
my $cache=shift;
|
39 |
|
|
$self->{cache}=$cache;
|
40 |
williamc |
1.9 |
$self->{dummybase}=URL::URLbase->new({});
|
41 |
williamc |
1.7 |
$self->{cachestore}=$self->{cache}->filestore();
|
42 |
williamc |
1.2 |
use URL::URL_cvs;
|
43 |
williamc |
1.5 |
use URL::URL_cvsfile;
|
44 |
williamc |
1.2 |
use URL::URL_file;
|
45 |
williamc |
1.7 |
use URL::URL_filed;
|
46 |
williamc |
1.2 |
$self->{urlmodules}={
|
47 |
williamc |
1.5 |
'cvsfile' => 'URL::URL_cvsfile',
|
48 |
|
|
'cvs' => 'URL::URL_cvs',
|
49 |
williamc |
1.7 |
'file' => 'URL::URL_file',
|
50 |
|
|
'filed' => 'URL::URL_filed'
|
51 |
williamc |
1.2 |
};
|
52 |
williamc |
1.1 |
$self->{filebase}="";
|
53 |
williamc |
1.2 |
$self->setbase("file", {}); # Base file as default
|
54 |
williamc |
1.7 |
$self->setbase("filed", {}); # Base file as default
|
55 |
williamc |
1.1 |
}
|
56 |
|
|
|
57 |
williamc |
1.7 |
sub get {
|
58 |
williamc |
1.4 |
my $self=shift;
|
59 |
williamc |
1.9 |
my $origurl=shift;
|
60 |
|
|
|
61 |
|
|
# Process the URL string
|
62 |
|
|
$url=URL::URLclass->new($origurl);
|
63 |
|
|
my $type=$url->type();
|
64 |
|
|
$urltypehandler=$self->_typehandler($type);
|
65 |
|
|
$url->expandurl($self->currentbase($type));
|
66 |
|
|
|
67 |
|
|
# Generate a location name if not provided
|
68 |
|
|
if ( @_ ) {
|
69 |
|
|
$location=shift;
|
70 |
|
|
}
|
71 |
|
|
else {
|
72 |
|
|
$location=$self->{cache}->filename($url->url());
|
73 |
|
|
}
|
74 |
williamc |
1.7 |
|
75 |
williamc |
1.9 |
# -- get the file form the appropriate handler
|
76 |
|
|
if ( defined $urltypehandler ) {
|
77 |
|
|
# Call the download module
|
78 |
|
|
$file=eval{$urltypehandler->get($url, $location)};
|
79 |
|
|
}
|
80 |
williamc |
1.4 |
|
81 |
williamc |
1.9 |
# now register it in the cache if successful
|
82 |
|
|
if ( $file ) {
|
83 |
|
|
$self->{cache}->store($fullurl, $rv);
|
84 |
|
|
}
|
85 |
|
|
return ($fullurl, $file);
|
86 |
williamc |
1.4 |
}
|
87 |
|
|
|
88 |
williamc |
1.9 |
sub setbase {
|
89 |
williamc |
1.1 |
my $self=shift;
|
90 |
williamc |
1.9 |
my $type=shift;
|
91 |
|
|
my @args=@_;
|
92 |
|
|
my $oref;
|
93 |
williamc |
1.7 |
|
94 |
williamc |
1.9 |
$self->checktype($type);
|
95 |
|
|
# make a new base object
|
96 |
|
|
my $base=URL::URLbase->new(@_);
|
97 |
|
|
push @{$self->{basestack}{$type}}, $base;
|
98 |
|
|
}
|
99 |
williamc |
1.1 |
|
100 |
williamc |
1.9 |
sub unsetbase {
|
101 |
|
|
my $self=shift;
|
102 |
|
|
my $type=shift;
|
103 |
|
|
my $oref;
|
104 |
williamc |
1.7 |
|
105 |
williamc |
1.9 |
$self->checktype($type);
|
106 |
|
|
# pop off the stack and call the unset base method
|
107 |
|
|
if ( $#{$self->{basestack}{$type}} >=0 ) {
|
108 |
|
|
my $base=pop @{$self->{basestack}{$type}};
|
109 |
|
|
undef $base;
|
110 |
|
|
}
|
111 |
|
|
else {
|
112 |
|
|
die "URLhandler error: Unable to unset type $type\n";
|
113 |
williamc |
1.4 |
}
|
114 |
williamc |
1.1 |
}
|
115 |
|
|
|
116 |
williamc |
1.9 |
sub currentbase {
|
117 |
williamc |
1.1 |
my $self=shift;
|
118 |
|
|
my $type=shift;
|
119 |
williamc |
1.9 |
my $rv;
|
120 |
williamc |
1.1 |
|
121 |
williamc |
1.9 |
if ( exists $self->{basestack}{$type} ) {
|
122 |
|
|
$rv=$self->{basestack}{$type}[$#$self->{basestack}{$type}];
|
123 |
williamc |
1.1 |
}
|
124 |
|
|
else {
|
125 |
williamc |
1.9 |
$rv=$self->{dummybase};
|
126 |
williamc |
1.1 |
}
|
127 |
williamc |
1.9 |
return $rv;
|
128 |
williamc |
1.1 |
}
|
129 |
|
|
|
130 |
williamc |
1.9 |
sub checktype($type) {
|
131 |
williamc |
1.1 |
my $self=shift;
|
132 |
williamc |
1.9 |
my $type=shift;
|
133 |
williamc |
1.1 |
|
134 |
williamc |
1.9 |
# Check type is supported
|
135 |
williamc |
1.2 |
if ( ! exists $self->{urlmodules}{$type} ) {
|
136 |
williamc |
1.9 |
die "URLhandler error: Unsupported type $type\n";
|
137 |
williamc |
1.1 |
}
|
138 |
|
|
}
|
139 |
|
|
|
140 |
williamc |
1.9 |
sub _typehandler {
|
141 |
williamc |
1.7 |
my $self=shift;
|
142 |
williamc |
1.8 |
my $type=shift;
|
143 |
williamc |
1.7 |
|
144 |
williamc |
1.9 |
$self->checktype($type);
|
145 |
williamc |
1.7 |
|
146 |
williamc |
1.9 |
# instantiate only if it dosnt already exist;
|
147 |
|
|
(exists $self->{urlobjs}{$type})?$self->{urlobjs}{$type}
|
148 |
|
|
:$self->{urlobjs}{$type}=$self->{urlmodules}{$type}->new();
|
149 |
williamc |
1.1 |
}
|