ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/URL/URLhandler.pm
Revision: 1.2
Committed: Mon Aug 23 15:54:10 1999 UTC (25 years, 8 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.1: +31 -208 lines
Log Message:
BAsic functionality tested

File Contents

# User Rev Content
1 williamc 1.1 # url handler -> returns the location of the file
2     #
3     # Interface
4     # ---------
5     # new() :
6     # new(cachedir) : A new urlhandler with a defined default cahce directory
7     # get(url) : download from the specified url to the default cache
8 williamc 1.2 # get(url,location) : download to the specified directory
9 williamc 1.1 # setbase(type,@args) : set the url defaults for a specific url type
10     # arguments are dependent on type
11     # http:
12     # file:
13     # cvs: servername,servertype [ ,user,passkey ]
14     # label:
15     # unsetbase(type) : deactivate a previously set base
16     #
17     # ----------------------------------------------------------------------
18     # returns file location - or crashes out
19     # can pass a file name for the item to be stored as
20     # if not then stores in a default cache.
21    
22     package URLhandler;
23     require 5.004;
24     use Utilities::AddDir;
25     use Carp;
26    
27     sub new {
28     my $class=shift;
29     my $cache=shift;
30     $self={};
31     bless $self, $class;
32     $self->init($cache);
33     return $self;
34     }
35    
36     sub init {
37     use Utilities::AddDir;
38     my $self=shift;
39     my $cache=shift;
40     if (! defined $cache ) { $cache="/tmp/SCRAMcache" }; # default cache
41     AddDir::adddir($cache);
42     $self->{cache}=$cache;
43 williamc 1.2 use URL::URL_cvs;
44     use URL::URL_file;
45     $self->{urlmodules}={
46     'cvs' => 'URL_cvs',
47     'file' => 'URL_file'
48     };
49 williamc 1.1 $self->{filebase}="";
50 williamc 1.2 $self->setbase("file", {}); # Base file as default
51 williamc 1.1 }
52    
53     sub get ($@) {
54     my $self=shift;
55     my $origurl=shift;
56     my $filename=shift;
57     my $rest;
58     my $type;
59     my $url;
60     my $version;
61    
62     if ( ! defined $filename ) {
63 williamc 1.2 $filename=$self->{cache};
64 williamc 1.1 }
65     chomp $origurl;
66     # get our version info from the url (after last ??)
67     ( $url, $version) = split /\?\?/, $origurl;
68     if ( $url=~/:/ ) {
69     ($type,$rest) = split /:/, $url;
70     }
71     else {
72     $type='label';
73     $rest=$url.":".$version;
74 williamc 1.2 }
75     if ( ! ( exists $self->{'urlmodules'}{$type}) ) {
76 williamc 1.1 print "URLhandler: Unsupported type $type\n";
77     carp;
78     }
79 williamc 1.2 else {
80     eval{${$self->{urlostack}{$type}}[$#{$self->{urlostack}{$type}}]}->
81     get($rest, $filename);
82     }
83 williamc 1.1 }
84    
85     sub setbase {
86     my $self=shift;
87     my $type=shift;
88     my @args=@_;
89 williamc 1.2 my $oref;
90 williamc 1.1
91     # Check type is supported
92 williamc 1.2 if ( ! exists $self->{urlmodules}{$type} ) {
93 williamc 1.1 print "URLhandler error: Unsupported type $type\n";
94     return 1;
95     }
96     else {
97 williamc 1.2 # A new URL object - pushed onto the stack
98     $oref=eval{$self->{urlmodules}{$type}}->new();
99     push @{$self->{urlostack}{$type}}, $oref;
100     $oref->setbase(@args);
101 williamc 1.1 }
102     }
103    
104     sub unsetbase {
105     my $self=shift;
106     my $type=shift;
107 williamc 1.2 my $oref;
108 williamc 1.1
109     # Check type is supported
110 williamc 1.2 if ( ! exists $self->{urlmodules}{$type} ) {
111 williamc 1.1 print "URLhandler error: Unsupported type $type\n";
112     return 1;
113     }
114     else {
115 williamc 1.2 # pop off the stack and call the unset base method
116     if ( $#{$self->{urlostack}{$type}} >=0 ) {
117     $oref=pop @{$self->{urlostack}{$type}};
118     $oref->unsetbase();
119     undef $oref;
120 williamc 1.1 }
121 williamc 1.2 else {
122     print "URLhandler error: Unable to unset type $type\n";
123     return 1;
124 williamc 1.1 }
125     }
126     }
127    
128     # ------------------------ General Support Routines ----------------------------
129    
130     sub cachefilename {
131     my $self=shift;
132     use File::Basename;
133     use Utilities::AddDir;
134     my $filebase=dirname($rest);
135     $cache="/tmp/williamc/urlhandler$$";
136     adddir($cache);
137     $filename=$cache."/".$filebase;
138     }
139