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 |
> |
# 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(type,variablehash) : set the url defaults for a specific url type |
9 |
|
# arguments are dependent on type |
10 |
|
# unsetbase(type) : deactivate a previously set base |
11 |
+ |
# currentbase(type) : return the current base for the given type |
12 |
|
# |
13 |
|
# ---------------------------------------------------------------------- |
14 |
|
# returns file location - or crashes out |
18 |
|
package URL::URLhandler; |
19 |
|
require 5.004; |
20 |
|
use Utilities::AddDir; |
21 |
< |
use URL::URLUtilities; |
22 |
< |
use URL::URL_base; |
21 |
> |
use URL::URLcache; |
22 |
> |
use URL::URLclass; |
23 |
> |
use URL::URLbase; |
24 |
|
use Carp; |
25 |
|
|
26 |
|
sub new { |
33 |
|
} |
34 |
|
|
35 |
|
sub init { |
33 |
– |
use URL::URLcache; |
36 |
|
use Utilities::AddDir; |
37 |
|
my $self=shift; |
38 |
|
my $cache=shift; |
39 |
|
$self->{cache}=$cache; |
40 |
+ |
$self->{dummybase}=URL::URLbase->new({}); |
41 |
|
$self->{cachestore}=$self->{cache}->filestore(); |
42 |
|
use URL::URL_cvs; |
43 |
|
use URL::URL_cvsfile; |
54 |
|
$self->setbase("filed", {}); # Base file as default |
55 |
|
} |
56 |
|
|
54 |
– |
# |
57 |
|
sub get { |
58 |
|
my $self=shift; |
57 |
– |
my $url=shift; |
58 |
– |
|
59 |
– |
|
60 |
– |
my ($urlstring, $type, $urltypehandler, $base, $version) |
61 |
– |
=$self->_urlexpand($url); # get a unique url string |
62 |
– |
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 |
– |
$self->{cache}->store($urlstring, $rv); |
68 |
– |
} |
69 |
– |
return $rv; |
70 |
– |
} |
71 |
– |
|
72 |
– |
sub getto ($@) { |
73 |
– |
my $self=shift; |
59 |
|
my $origurl=shift; |
75 |
– |
my $dirname=shift; |
76 |
– |
|
77 |
– |
my $rv=""; |
60 |
|
|
61 |
|
# Process the URL string |
62 |
< |
my ($type,$rest,$version)=URLUtilities::parseURL($origurl); |
63 |
< |
my ($urltypehandler, $base)=$self->_typecheck($type); |
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 |
|
|
75 |
+ |
# -- get the file form the appropriate handler |
76 |
|
if ( defined $urltypehandler ) { |
77 |
|
# Call the download module |
78 |
< |
$rv=eval{$urltypehandler->get($rest.(($version ne "")?"\?".$version:""), $dirname); }; |
79 |
< |
} |
80 |
< |
return $rv; |
78 |
> |
$file=eval{$urltypehandler->get($url, $location)}; |
79 |
> |
} |
80 |
> |
|
81 |
> |
# now register it in the cache if successful |
82 |
> |
if ( $file ) { |
83 |
> |
$self->{cache}->store($fullurl, $rv); |
84 |
> |
} |
85 |
> |
return ($fullurl, $file); |
86 |
|
} |
87 |
|
|
88 |
|
sub setbase { |
91 |
|
my @args=@_; |
92 |
|
my $oref; |
93 |
|
|
94 |
< |
# Check type is supported |
95 |
< |
if ( ! exists $self->{urlmodules}{$type} ) { |
96 |
< |
print "URLhandler error: Unsupported type $type\n"; |
97 |
< |
return 1; |
100 |
< |
} |
101 |
< |
else { |
102 |
< |
# A new URL object - pushed onto the stack |
103 |
< |
$oref=eval{$self->{urlmodules}{$type}}->new(); |
104 |
< |
push @{$self->{urlostack}{$type}}, $oref; |
105 |
< |
$oref->setbase(@args); |
106 |
< |
} |
94 |
> |
$self->checktype($type); |
95 |
> |
# make a new base object |
96 |
> |
my $base=URL::URLbase->new(@_); |
97 |
> |
push @{$self->{basestack}{$type}}, $base; |
98 |
|
} |
99 |
|
|
100 |
|
sub unsetbase { |
102 |
|
my $type=shift; |
103 |
|
my $oref; |
104 |
|
|
105 |
< |
# Check type is supported |
105 |
> |
$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 |
> |
} |
114 |
> |
} |
115 |
> |
|
116 |
> |
sub currentbase { |
117 |
> |
my $self=shift; |
118 |
> |
my $type=shift; |
119 |
> |
my $rv; |
120 |
> |
|
121 |
> |
if ( exists $self->{basestack}{$type} ) { |
122 |
> |
$rv=$self->{basestack}{$type}[$#$self->{basestack}{$type}]; |
123 |
> |
} |
124 |
> |
else { |
125 |
> |
$rv=$self->{dummybase}; |
126 |
> |
} |
127 |
> |
return $rv; |
128 |
> |
} |
129 |
> |
|
130 |
> |
sub checktype($type) { |
131 |
> |
my $self=shift; |
132 |
> |
my $type=shift; |
133 |
> |
|
134 |
> |
# Check type is supported |
135 |
|
if ( ! exists $self->{urlmodules}{$type} ) { |
136 |
< |
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 |
< |
} |
136 |
> |
die "URLhandler error: Unsupported type $type\n"; |
137 |
|
} |
138 |
|
} |
139 |
|
|
140 |
< |
# -------------------- Support Routines (private Methods) ------------- |
134 |
< |
|
135 |
< |
# |
136 |
< |
# Process the URL string into its component parts |
137 |
< |
# |
138 |
< |
sub _typecheck { |
140 |
> |
sub _typehandler { |
141 |
|
my $self=shift; |
142 |
|
my $type=shift; |
143 |
|
|
144 |
< |
my $base=""; |
144 |
> |
$self->checktype($type); |
145 |
|
|
146 |
< |
# check type are supported |
147 |
< |
if ( ! ( exists $self->{'urlmodules'}{$type}) ) { |
148 |
< |
print "URLhandler: Unsupported type $type\n"; |
147 |
< |
die; |
148 |
< |
} |
149 |
< |
else { |
150 |
< |
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; |
146 |
> |
# instantiate only if it dosnt already exist; |
147 |
> |
(exists $self->{urlobjs}{$type})?$self->{urlobjs}{$type} |
148 |
> |
:$self->{urlobjs}{$type}=$self->{urlmodules}{$type}->new(); |
149 |
|
} |