2 |
|
# Interface |
3 |
|
# --------- |
4 |
|
# new(cache) : A new urlhandler with a defined default cahce directory |
5 |
< |
# get(url) : download from the specified url to the default cache |
6 |
< |
# getto(url,dirlocation): download to the specified directory - no cache registry |
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 |
|
# ---------------------------------------------------------------------- |
12 |
– |
# returns file location - or crashes out |
13 |
– |
# can pass a file name for the item to be stored as |
14 |
– |
# if not then stores in a default cache. |
15 |
|
|
16 |
|
package URL::URLhandler; |
17 |
|
require 5.004; |
18 |
|
use Utilities::AddDir; |
19 |
< |
use URL::URLUtilities; |
20 |
< |
use URL::URL_base; |
19 |
> |
use URL::URLcache; |
20 |
> |
use URL::URLclass; |
21 |
> |
use URL::URLbase; |
22 |
|
use Carp; |
23 |
|
|
24 |
|
sub new { |
31 |
|
} |
32 |
|
|
33 |
|
sub init { |
33 |
– |
use URL::URLcache; |
34 |
|
use Utilities::AddDir; |
35 |
|
my $self=shift; |
36 |
|
my $cache=shift; |
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; |
47 |
|
'file' => 'URL::URL_file', |
48 |
|
'filed' => 'URL::URL_filed' |
49 |
|
}; |
49 |
– |
$self->{filebase}=""; |
50 |
– |
$self->setbase("file", {}); # Base file as default |
51 |
– |
$self->setbase("filed", {}); # Base file as default |
50 |
|
} |
51 |
|
|
54 |
– |
# |
52 |
|
sub get { |
53 |
|
my $self=shift; |
54 |
< |
my $url=shift; |
55 |
< |
|
54 |
> |
my $origurl=shift; |
55 |
> |
my $file=""; |
56 |
|
|
57 |
< |
my ($urlstring, $type, $urltypehandler, $base, $version) |
58 |
< |
=$self->_parseurl($url); # get a unique url string |
59 |
< |
my $rv=""; |
60 |
< |
$rv=$self->getto($url,$self->{cache}->filename($urlstring)); |
61 |
< |
|
62 |
< |
# now register it in the cache if successful |
63 |
< |
if ( $rv ne "" ) { |
64 |
< |
print "Storing ".$urlstring.",".$rv."\n"; |
65 |
< |
$self->{cache}->store($urlstring, $rv); |
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 |
> |
my $location=$self->{cache}->file($fullurl); |
63 |
> |
if ( $location ne "" ) { |
64 |
> |
$file=$self->{cache}->file($location); |
65 |
> |
#print " Already defined file --".$location." -- ".$file."\n"; |
66 |
|
} |
67 |
< |
return $rv; |
67 |
> |
else { |
68 |
> |
($fullurl,$file)=$self->download($origurl, @_); |
69 |
> |
} |
70 |
> |
return ($fullurl, $file); |
71 |
|
} |
72 |
|
|
73 |
< |
sub getto ($@) { |
73 |
> |
sub download { |
74 |
|
my $self=shift; |
75 |
< |
my $origurl=shift; |
76 |
< |
my $dirname=shift; |
75 |
> |
my $origurl=shift; |
76 |
|
|
77 |
< |
my $rest; |
78 |
< |
my $type; |
79 |
< |
my $version; |
80 |
< |
my $rv=""; |
81 |
< |
|
82 |
< |
# Process the URL string |
84 |
< |
my ($urlstring, $type, $urltypehandler, $base, $version) |
85 |
< |
=$self->_parseurl($origurl); |
77 |
> |
print "downloading $origurl\n"; |
78 |
> |
# Process the URL string |
79 |
> |
my $url=URL::URLclass->new($origurl); |
80 |
> |
my $type=$url->type(); |
81 |
> |
$urltypehandler=$self->_typehandler($type); |
82 |
> |
$url->expandurl($self->currentbase($type)); |
83 |
|
|
84 |
+ |
# Generate a location name if not provided |
85 |
+ |
if ( @_ ) { |
86 |
+ |
$location=shift; |
87 |
+ |
} |
88 |
+ |
else { |
89 |
+ |
$location=$self->{cache}->filename($url->url()); |
90 |
+ |
} |
91 |
+ |
# -- get the file from the appropriate handler |
92 |
|
if ( defined $urltypehandler ) { |
93 |
|
# Call the download module |
94 |
< |
$rv=eval{$urltypehandler->get($base.(($version ne "")?"\?".$version:""), $dirname); }; |
95 |
< |
} |
94 |
> |
$file=eval{$urltypehandler->get($url, $location)}; |
95 |
> |
} |
96 |
|
|
97 |
< |
# Check the return type |
98 |
< |
if ( defined $rv ) { |
99 |
< |
# if ( $rv!~/^\// ) { |
100 |
< |
# $rv=$dirname."/".$rv; |
101 |
< |
# } |
102 |
< |
} |
98 |
< |
return $rv; |
97 |
> |
# now register it in the cache if successful |
98 |
> |
if ( $file ) { |
99 |
> |
$self->{cache}->store($url->url(), $location); |
100 |
> |
$self->{cache}->store($location, $file); |
101 |
> |
} |
102 |
> |
return ($url->url(), $file); |
103 |
|
} |
104 |
|
|
105 |
|
sub setbase { |
108 |
|
my @args=@_; |
109 |
|
my $oref; |
110 |
|
|
111 |
< |
# Check type is supported |
112 |
< |
if ( ! exists $self->{urlmodules}{$type} ) { |
113 |
< |
print "URLhandler error: Unsupported type $type\n"; |
114 |
< |
return 1; |
111 |
< |
} |
112 |
< |
else { |
113 |
< |
# A new URL object - pushed onto the stack |
114 |
< |
$oref=eval{$self->{urlmodules}{$type}}->new(); |
115 |
< |
push @{$self->{urlostack}{$type}}, $oref; |
116 |
< |
$oref->setbase(@args); |
117 |
< |
} |
111 |
> |
$self->checktype($type); |
112 |
> |
# make a new base object |
113 |
> |
my $base=URL::URLbase->new(@_); |
114 |
> |
push @{$self->{"basestack"}{$type}}, $base; |
115 |
|
} |
116 |
|
|
117 |
|
sub unsetbase { |
119 |
|
my $type=shift; |
120 |
|
my $oref; |
121 |
|
|
122 |
< |
# Check type is supported |
123 |
< |
if ( ! exists $self->{urlmodules}{$type} ) { |
124 |
< |
print "URLhandler error: Unsupported type $type\n"; |
125 |
< |
return 1; |
126 |
< |
} |
127 |
< |
else { |
128 |
< |
# pop off the stack and call the unset base method |
129 |
< |
if ( $#{$self->{urlostack}{$type}} >=0 ) { |
130 |
< |
$oref=pop @{$self->{urlostack}{$type}}; |
131 |
< |
$oref->unsetbase(); |
132 |
< |
undef $oref; |
133 |
< |
} |
134 |
< |
else { |
138 |
< |
print "URLhandler error: Unable to unset type $type\n"; |
139 |
< |
return 1; |
140 |
< |
} |
141 |
< |
} |
122 |
> |
$self->checktype($type); |
123 |
> |
# pop off the stack and call the unset base method |
124 |
> |
if ( $#{$self->{basestack}{$type}} >=0 ) { |
125 |
> |
my $base=pop @{$self->{basestack}{$type}}; |
126 |
> |
undef $base; |
127 |
> |
} |
128 |
> |
else { |
129 |
> |
die "URLhandler error: Unable to unset type $type\n"; |
130 |
> |
} |
131 |
> |
# remove the stack if its empty |
132 |
> |
if ( $#{$self->{basestack}{$type}} == -1 ) { |
133 |
> |
delete $self->{basestack}{$type}; |
134 |
> |
} |
135 |
|
} |
136 |
|
|
137 |
< |
# -------------------- Support Routines (private Methods) ------------- |
137 |
> |
sub currentbase { |
138 |
> |
my $self=shift; |
139 |
> |
my $type=shift; |
140 |
> |
my $rv; |
141 |
|
|
142 |
< |
# |
143 |
< |
# Process the URL string into its component parts |
144 |
< |
# |
145 |
< |
sub _parseurl { |
142 |
> |
if ( exists $self->{basestack}{$type} ) { |
143 |
> |
$rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}]; |
144 |
> |
} |
145 |
> |
else { |
146 |
> |
$rv=$self->{dummybase}; |
147 |
> |
} |
148 |
> |
return $rv; |
149 |
> |
} |
150 |
> |
|
151 |
> |
sub checktype($type) { |
152 |
|
my $self=shift; |
153 |
< |
my $origurl=shift; |
152 |
< |
chomp $origurl; |
153 |
> |
my $type=shift; |
154 |
|
|
155 |
< |
my ($type,$rest,$cmdstring)=URLUtilities::parseURL($origurl); |
156 |
< |
my $urlstring=""; |
157 |
< |
my $url; |
157 |
< |
|
158 |
< |
$base=""; |
159 |
< |
|
160 |
< |
# check type are supported |
161 |
< |
if ( ! ( exists $self->{'urlmodules'}{$type}) ) { |
162 |
< |
print "URLhandler: Unsupported type $type\n"; |
163 |
< |
die; |
155 |
> |
# Check type is supported |
156 |
> |
if ( ! exists $self->{urlmodules}{$type} ) { |
157 |
> |
die "URLhandler error: Unsupported type $type\n"; |
158 |
|
} |
159 |
< |
else { |
160 |
< |
if ( $#{$self->{urlostack}{$type}} < 0 ) { |
161 |
< |
print "URLhandler : base not set for type $type \n"; |
162 |
< |
die; |
163 |
< |
} |
164 |
< |
else { |
165 |
< |
# ---- type is supported |
166 |
< |
$urltypehandler= |
167 |
< |
${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}]; |
168 |
< |
$base=$urltypehandler->baseurl(); |
169 |
< |
} |
170 |
< |
} |
171 |
< |
|
172 |
< |
my $urlbase=($base ne ""?$base."/":"").$rest; |
173 |
< |
$urlstring=$type.":".$urlbase.($cmdstring ne ""?"?".$cmdstring:""); |
180 |
< |
return $urlstring, $type, $urltypehandler, $urlbase, $cmdstring; |
159 |
> |
} |
160 |
> |
|
161 |
> |
sub _typehandler { |
162 |
> |
my $self=shift; |
163 |
> |
my $type=shift; |
164 |
> |
|
165 |
> |
$self->checktype($type); |
166 |
> |
|
167 |
> |
# instantiate only if it dosnt already exist; |
168 |
> |
if ( exists $self->{'urlobjs'}{$type} ) { |
169 |
> |
$self->{'urlobjs'}{$type}; |
170 |
> |
} |
171 |
> |
else { |
172 |
> |
$self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new(); |
173 |
> |
} |
174 |
|
} |