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; |
54 |
> |
my $origurl=shift; |
55 |
> |
my $file=""; |
56 |
|
|
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 ($urlstring, $type, $urltypehandler, $base, $version) |
63 |
< |
=$self->_parseurl($url); # get a unique url string |
64 |
< |
my $rv=""; |
63 |
< |
$rv=$self->getto($url,$self->{cache}->filename($urlstring)); |
64 |
< |
|
65 |
< |
# now register it in the cache if successful |
66 |
< |
if ( $rv ne "" ) { |
67 |
< |
print "Storing ".$urlstring.",".$rv."\n"; |
68 |
< |
$self->{cache}->store($urlstring, $rv); |
62 |
> |
$file=$self->{cache}->file($fullurl); |
63 |
> |
if ( $file eq "" ) { |
64 |
> |
($fullurl,$file)=$self->download($origurl, @_); |
65 |
|
} |
66 |
< |
return $rv; |
66 |
> |
return ($fullurl, $file); |
67 |
|
} |
68 |
|
|
69 |
< |
sub getto ($@) { |
69 |
> |
sub download { |
70 |
|
my $self=shift; |
71 |
< |
my $origurl=shift; |
76 |
< |
my $dirname=shift; |
71 |
> |
my $origurl=shift; |
72 |
|
|
73 |
< |
my $rest; |
74 |
< |
my $type; |
75 |
< |
my $version; |
76 |
< |
my $rv=""; |
77 |
< |
|
83 |
< |
# Process the URL string |
84 |
< |
my ($urlstring, $type, $urltypehandler, $base, $version) |
85 |
< |
=$self->_parseurl($origurl); |
73 |
> |
# Process the URL string |
74 |
> |
my $url=URL::URLclass->new($origurl); |
75 |
> |
my $type=$url->type(); |
76 |
> |
$urltypehandler=$self->_typehandler($type); |
77 |
> |
$url->expandurl($self->currentbase($type)); |
78 |
|
|
79 |
+ |
# Generate a location name if not provided |
80 |
+ |
$nocache=1; |
81 |
+ |
if ( @_ ) { |
82 |
+ |
$location=shift; |
83 |
+ |
$nocache=0; # dont cache if downloaded to an external location |
84 |
+ |
} |
85 |
+ |
else { |
86 |
+ |
$location=$self->{cache}->filename($url->url()); |
87 |
+ |
} |
88 |
+ |
# -- get the file from the appropriate handler |
89 |
|
if ( defined $urltypehandler ) { |
90 |
|
# Call the download module |
91 |
< |
$rv=eval{$urltypehandler->get($base.(($version ne "")?"\?".$version:""), $dirname); }; |
92 |
< |
} |
91 |
> |
$file=eval{$urltypehandler->get($url, $location)}; |
92 |
> |
} |
93 |
|
|
94 |
< |
# Check the return type |
95 |
< |
if ( defined $rv ) { |
96 |
< |
# if ( $rv!~/^\// ) { |
97 |
< |
# $rv=$dirname."/".$rv; |
98 |
< |
# } |
97 |
< |
} |
98 |
< |
return $rv; |
94 |
> |
# now register it in the cache if successful |
95 |
> |
if ( $file && $nocache) { |
96 |
> |
$self->{cache}->store($url->url(), $location); |
97 |
> |
} |
98 |
> |
return ($url->url(), $file); |
99 |
|
} |
100 |
|
|
101 |
|
sub setbase { |
104 |
|
my @args=@_; |
105 |
|
my $oref; |
106 |
|
|
107 |
< |
# Check type is supported |
108 |
< |
if ( ! exists $self->{urlmodules}{$type} ) { |
109 |
< |
print "URLhandler error: Unsupported type $type\n"; |
110 |
< |
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 |
< |
} |
107 |
> |
$self->checktype($type); |
108 |
> |
# make a new base object |
109 |
> |
my $base=URL::URLbase->new(@_); |
110 |
> |
push @{$self->{"basestack"}{$type}}, $base; |
111 |
|
} |
112 |
|
|
113 |
|
sub unsetbase { |
115 |
|
my $type=shift; |
116 |
|
my $oref; |
117 |
|
|
118 |
< |
# Check type is supported |
119 |
< |
if ( ! exists $self->{urlmodules}{$type} ) { |
120 |
< |
print "URLhandler error: Unsupported type $type\n"; |
121 |
< |
return 1; |
122 |
< |
} |
123 |
< |
else { |
124 |
< |
# pop off the stack and call the unset base method |
125 |
< |
if ( $#{$self->{urlostack}{$type}} >=0 ) { |
126 |
< |
$oref=pop @{$self->{urlostack}{$type}}; |
127 |
< |
$oref->unsetbase(); |
128 |
< |
undef $oref; |
129 |
< |
} |
130 |
< |
else { |
138 |
< |
print "URLhandler error: Unable to unset type $type\n"; |
139 |
< |
return 1; |
140 |
< |
} |
141 |
< |
} |
118 |
> |
$self->checktype($type); |
119 |
> |
# pop off the stack and call the unset base method |
120 |
> |
if ( $#{$self->{basestack}{$type}} >=0 ) { |
121 |
> |
my $base=pop @{$self->{basestack}{$type}}; |
122 |
> |
undef $base; |
123 |
> |
} |
124 |
> |
else { |
125 |
> |
die "URLhandler error: Unable to unset type $type\n"; |
126 |
> |
} |
127 |
> |
# remove the stack if its empty |
128 |
> |
if ( $#{$self->{basestack}{$type}} == -1 ) { |
129 |
> |
delete $self->{basestack}{$type}; |
130 |
> |
} |
131 |
|
} |
132 |
|
|
133 |
< |
# -------------------- Support Routines (private Methods) ------------- |
133 |
> |
sub currentbase { |
134 |
> |
my $self=shift; |
135 |
> |
my $type=shift; |
136 |
> |
my $rv; |
137 |
|
|
138 |
< |
# |
139 |
< |
# Process the URL string into its component parts |
140 |
< |
# |
141 |
< |
sub _parseurl { |
138 |
> |
if ( exists $self->{basestack}{$type} ) { |
139 |
> |
$rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}]; |
140 |
> |
} |
141 |
> |
else { |
142 |
> |
$rv=$self->{dummybase}; |
143 |
> |
} |
144 |
> |
return $rv; |
145 |
> |
} |
146 |
> |
|
147 |
> |
sub checktype($type) { |
148 |
|
my $self=shift; |
149 |
< |
my $origurl=shift; |
152 |
< |
chomp $origurl; |
149 |
> |
my $type=shift; |
150 |
|
|
151 |
< |
my ($type,$rest,$cmdstring)=URLUtilities::parseURL($origurl); |
152 |
< |
my $urlstring=""; |
153 |
< |
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; |
151 |
> |
# Check type is supported |
152 |
> |
if ( ! exists $self->{urlmodules}{$type} ) { |
153 |
> |
die "URLhandler error: Unsupported type $type\n"; |
154 |
|
} |
155 |
< |
else { |
156 |
< |
if ( $#{$self->{urlostack}{$type}} < 0 ) { |
157 |
< |
print "URLhandler : base not set for type $type \n"; |
158 |
< |
die; |
159 |
< |
} |
160 |
< |
else { |
161 |
< |
# ---- type is supported |
162 |
< |
$urltypehandler= |
163 |
< |
${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}]; |
164 |
< |
$base=$urltypehandler->baseurl(); |
165 |
< |
} |
166 |
< |
} |
167 |
< |
|
168 |
< |
my $urlbase=($base ne ""?$base."/":"").$rest; |
169 |
< |
$urlstring=$type.":".$urlbase.($cmdstring ne ""?"?".$cmdstring:""); |
180 |
< |
return $urlstring, $type, $urltypehandler, $urlbase, $cmdstring; |
155 |
> |
} |
156 |
> |
|
157 |
> |
sub _typehandler { |
158 |
> |
my $self=shift; |
159 |
> |
my $type=shift; |
160 |
> |
|
161 |
> |
$self->checktype($type); |
162 |
> |
|
163 |
> |
# instantiate only if it dosnt already exist; |
164 |
> |
if ( exists $self->{'urlobjs'}{$type} ) { |
165 |
> |
$self->{'urlobjs'}{$type}; |
166 |
> |
} |
167 |
> |
else { |
168 |
> |
$self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new(); |
169 |
> |
} |
170 |
|
} |