1 |
– |
# url handler -> returns the location of the file |
1 |
|
# Interface |
2 |
|
# --------- |
3 |
|
# new(cache) : A new urlhandler with a defined default cahce directory |
4 |
< |
# get(url) : download from the specified url to the default cache |
5 |
< |
# getto(url,dirlocation): download to the specified directory - no cache registry |
6 |
< |
# setbase(type,variablehash) : set the url defaults for a specific url type |
7 |
< |
# arguments are dependent on type |
4 |
> |
# download(url,[location]) : as get but always download |
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(urlstring) : set a base url type - return the url object |
9 |
|
# unsetbase(type) : deactivate a previously set base |
10 |
+ |
# currentbase(type) : return the current base for the given type |
11 |
+ |
# expandurl(urlstring) : return the base expanded URLclass of the given string |
12 |
|
# |
13 |
|
# ---------------------------------------------------------------------- |
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. |
14 |
|
|
15 |
|
package URL::URLhandler; |
16 |
|
require 5.004; |
17 |
|
use Utilities::AddDir; |
18 |
< |
use URL::URLUtilities; |
19 |
< |
use URL::URL_base; |
18 |
> |
use URL::URLcache; |
19 |
> |
use URL::URLclass; |
20 |
|
use Carp; |
21 |
|
|
22 |
|
sub new { |
29 |
|
} |
30 |
|
|
31 |
|
sub init { |
33 |
– |
use URL::URLcache; |
32 |
|
use Utilities::AddDir; |
33 |
|
my $self=shift; |
34 |
|
my $cache=shift; |
35 |
|
$self->{cache}=$cache; |
36 |
|
$self->{cachestore}=$self->{cache}->filestore(); |
37 |
|
use URL::URL_cvs; |
40 |
– |
use URL::URL_cvsfile; |
38 |
|
use URL::URL_file; |
39 |
< |
use URL::URL_filed; |
39 |
> |
use URL::URL_test; |
40 |
|
$self->{urlmodules}={ |
44 |
– |
'cvsfile' => 'URL::URL_cvsfile', |
41 |
|
'cvs' => 'URL::URL_cvs', |
42 |
|
'file' => 'URL::URL_file', |
43 |
< |
'filed' => 'URL::URL_filed' |
43 |
> |
'test' => 'URL::URL_test' |
44 |
|
}; |
49 |
– |
$self->{filebase}=""; |
50 |
– |
$self->setbase("file", {}); # Base file as default |
51 |
– |
$self->setbase("filed", {}); # Base file as default |
45 |
|
} |
46 |
|
|
54 |
– |
# |
47 |
|
sub get { |
48 |
|
my $self=shift; |
49 |
< |
my $url=shift; |
50 |
< |
|
49 |
> |
my $origurl=shift; |
50 |
> |
my $file=""; |
51 |
|
|
52 |
< |
my ($urlstring, $type, $urltypehandler, $base, $version) |
53 |
< |
=$self->_urlexpand($url); # get a unique url string |
54 |
< |
my $rv=""; |
55 |
< |
$rv=$self->getto($url,$self->{cache}->filename($urlstring)); |
56 |
< |
|
57 |
< |
# now register it in the cache if successful |
58 |
< |
if ( $rv ne "" ) { |
59 |
< |
$self->{cache}->store($urlstring, $rv); |
52 |
> |
my $url=URL::URLclass->new($origurl); |
53 |
> |
my $type=$url->type(); |
54 |
> |
$url->merge($self->currentbase($type)); |
55 |
> |
my $fullurl=$url->url(); |
56 |
> |
|
57 |
> |
$file=$self->{cache}->file($fullurl); |
58 |
> |
if ( $file eq "" ) { |
59 |
> |
($fullurl,$file)=$self->download($origurl, @_); |
60 |
|
} |
61 |
< |
return $rv; |
61 |
> |
return ($fullurl, $file); |
62 |
|
} |
63 |
|
|
64 |
< |
sub getto ($@) { |
64 |
> |
sub download { |
65 |
|
my $self=shift; |
66 |
< |
my $origurl=shift; |
75 |
< |
my $dirname=shift; |
76 |
< |
|
77 |
< |
my $rv=""; |
66 |
> |
my $origurl=shift; |
67 |
|
|
68 |
< |
# Process the URL string |
69 |
< |
my ($type,$rest,$version)=URLUtilities::parseURL($origurl); |
70 |
< |
my ($urltypehandler, $base)=$self->_typecheck($type); |
68 |
> |
# Process the URL string |
69 |
> |
my $url=URL::URLclass->new($origurl); |
70 |
> |
my $type=$url->type(); |
71 |
> |
$urltypehandler=$self->_typehandler($type); |
72 |
> |
$url->merge($self->currentbase($type)); |
73 |
> |
print "Attempting download of ".$url->url()."\n"; |
74 |
|
|
75 |
+ |
# Generate a location name if not provided |
76 |
+ |
my $nocache=1; |
77 |
+ |
if ( @_ ) { |
78 |
+ |
$location=shift; |
79 |
+ |
$nocache=0; # dont cache if downloaded to an external location |
80 |
+ |
} |
81 |
+ |
else { |
82 |
+ |
$location=$self->{cache}->filename($url->url()); |
83 |
+ |
} |
84 |
+ |
# -- get the file from the appropriate handler |
85 |
|
if ( defined $urltypehandler ) { |
86 |
|
# Call the download module |
87 |
< |
$rv=eval{$urltypehandler->get($rest.(($version ne "")?"\?".$version:""), $dirname); }; |
88 |
< |
} |
89 |
< |
return $rv; |
87 |
> |
$file=eval{$urltypehandler->get($url, $location)}; |
88 |
> |
} |
89 |
> |
|
90 |
> |
# now register it in the cache if successful |
91 |
> |
if ( $file && $nocache) { |
92 |
> |
#$self->{cache}->store($url->url(), $location); |
93 |
> |
$self->{cache}->store($url->url(), $file); |
94 |
> |
} |
95 |
> |
return ($url->url(), $file); |
96 |
> |
} |
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 |
|
} |
107 |
|
|
108 |
|
sub setbase { |
109 |
|
my $self=shift; |
110 |
< |
my $type=shift; |
111 |
< |
my @args=@_; |
110 |
> |
my $partialurl=shift; |
111 |
> |
|
112 |
> |
my $base=URL::URLclass->new($partialurl); |
113 |
> |
my $type=$base->type(); |
114 |
> |
$self->checktype($type); |
115 |
> |
# make a new base-url object |
116 |
> |
push @{$self->{"basestack"}{$type}}, $base; |
117 |
> |
return $base; |
118 |
> |
} |
119 |
> |
|
120 |
> |
sub unsetbase { |
121 |
> |
my $self=shift; |
122 |
> |
my $type=shift; |
123 |
|
my $oref; |
124 |
|
|
125 |
< |
# Check type is supported |
126 |
< |
if ( ! exists $self->{urlmodules}{$type} ) { |
127 |
< |
print "URLhandler error: Unsupported type $type\n"; |
128 |
< |
return 1; |
125 |
> |
$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 |
< |
# A new URL object - pushed onto the stack |
133 |
< |
$oref=eval{$self->{urlmodules}{$type}}->new(); |
134 |
< |
push @{$self->{urlostack}{$type}}, $oref; |
135 |
< |
$oref->setbase(@args); |
132 |
> |
die "URLhandler error: Unable to unset type $type\n"; |
133 |
> |
} |
134 |
> |
# remove the stack if its empty |
135 |
> |
if ( $#{$self->{basestack}{$type}} == -1 ) { |
136 |
> |
delete $self->{basestack}{$type}; |
137 |
|
} |
138 |
|
} |
139 |
|
|
140 |
< |
sub unsetbase { |
140 |
> |
sub currentbase { |
141 |
|
my $self=shift; |
142 |
< |
my $type=shift; |
143 |
< |
my $oref; |
142 |
> |
my $type=shift; |
143 |
> |
my $rv; |
144 |
> |
|
145 |
> |
if ( exists $self->{basestack}{$type} ) { |
146 |
> |
$rv=${$self->{basestack}{$type}}[$#{$self->{basestack}{$type}}]; |
147 |
> |
} |
148 |
> |
else { |
149 |
> |
$rv=undef; |
150 |
> |
} |
151 |
> |
return $rv; |
152 |
> |
} |
153 |
> |
|
154 |
> |
sub checktype($type) { |
155 |
> |
my $self=shift; |
156 |
> |
my $type=shift; |
157 |
|
|
158 |
< |
# Check type is supported |
158 |
> |
# Check type is supported |
159 |
|
if ( ! exists $self->{urlmodules}{$type} ) { |
160 |
< |
print "URLhandler error: Unsupported type $type\n"; |
117 |
< |
return 1; |
118 |
< |
} |
119 |
< |
else { |
120 |
< |
# pop off the stack and call the unset base method |
121 |
< |
if ( $#{$self->{urlostack}{$type}} >=0 ) { |
122 |
< |
$oref=pop @{$self->{urlostack}{$type}}; |
123 |
< |
$oref->unsetbase(); |
124 |
< |
undef $oref; |
125 |
< |
} |
126 |
< |
else { |
127 |
< |
print "URLhandler error: Unable to unset type $type\n"; |
128 |
< |
return 1; |
129 |
< |
} |
160 |
> |
die "URLhandler error: Unsupported type $type\n"; |
161 |
|
} |
162 |
|
} |
163 |
|
|
164 |
< |
# -------------------- Support Routines (private Methods) ------------- |
134 |
< |
|
135 |
< |
# |
136 |
< |
# Process the URL string into its component parts |
137 |
< |
# |
138 |
< |
sub _typecheck { |
164 |
> |
sub _typehandler { |
165 |
|
my $self=shift; |
166 |
|
my $type=shift; |
167 |
|
|
168 |
< |
my $base=""; |
168 |
> |
$self->checktype($type); |
169 |
|
|
170 |
< |
# check type are supported |
171 |
< |
if ( ! ( exists $self->{'urlmodules'}{$type}) ) { |
172 |
< |
print "URLhandler: Unsupported type $type\n"; |
173 |
< |
die; |
174 |
< |
} |
175 |
< |
else { |
176 |
< |
if ( $#{$self->{urlostack}{$type}} < 0 ) { |
151 |
< |
print "URLhandler : base not set for type $type \n"; |
152 |
< |
die; |
153 |
< |
} |
154 |
< |
else { |
155 |
< |
# ---- type is supported |
156 |
< |
$urltypehandler= |
157 |
< |
${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}]; |
158 |
< |
$base=$urltypehandler->baseurl(); |
159 |
< |
} |
160 |
< |
} |
161 |
< |
|
162 |
< |
return $urltypehandler, $base; |
163 |
< |
} |
164 |
< |
|
165 |
< |
sub _urlexpand { |
166 |
< |
my $self=shift; |
167 |
< |
my $url=shift; |
168 |
< |
|
169 |
< |
my ($type,$rest,$cmds)=URLUtilities::parseURL($url); |
170 |
< |
my ($urltypehandler, $base)=$self->_typecheck($type); |
171 |
< |
|
172 |
< |
my $expurl=$type.":".($base ne ""?$base."/":"").$rest. |
173 |
< |
($cmds ne ""?"\?".$cmds:""); |
174 |
< |
return $expurl; |
170 |
> |
# instantiate only if it dosnt already exist; |
171 |
> |
if ( exists $self->{'urlobjs'}{$type} ) { |
172 |
> |
$self->{'urlobjs'}{$type}; |
173 |
> |
} |
174 |
> |
else { |
175 |
> |
$self->{'urlobjs'}{$type}=$self->{urlmodules}{$type}->new(); |
176 |
> |
} |
177 |
|
} |