]> matita.cs.unibo.it Git - helm.git/blob - helm/http_getter/http_getter.pl.in
Various changes:
[helm.git] / helm / http_getter / http_getter.pl.in
1 #!@PERL_BINARY@
2
3 # Copyright (C) 2000, HELM Team.
4
5 # This file is part of HELM, an Hypertextual, Electronic
6 # Library of Mathematics, developed at the Computer Science
7 # Department, University of Bologna, Italy.
8
9 # HELM is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License
11 # as published by the Free Software Foundation; either version 2
12 # of the License, or (at your option) any later version.
13
14 # HELM is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
18
19 # You should have received a copy of the GNU General Public License
20 # along with HELM; if not, write to the Free Software
21 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
23 # For details, see the HELM World-Wide-Web page,
24 # http://cs.unibo.it/helm/.
25
26 #use strict;
27
28 my $VERSION = "@VERSION@";
29
30 # various variables
31 my ($HELM_LIB_PATH);
32 my (%map);
33
34 # First of all, let's load HELM configuration
35 use Env;
36 my $HELM_LIB_DIR = $ENV{"HELM_LIB_DIR"};
37 # this should be the only fixed constant
38 my $DEFAULT_HELM_LIB_DIR = "@HELM_LIB_DIR@";
39 if (defined ($HELM_LIB_DIR)) {
40    $HELM_LIB_PATH = $HELM_LIB_DIR."/configuration.pl";
41 } else {
42    $HELM_LIB_PATH = $DEFAULT_HELM_LIB_DIR."/configuration.pl";
43 }
44
45 # <ZACK>: TODO temporary, move this setting to configuration file
46 # set the cache mode, may be "gzipped" or "normal"
47 my $cachemode = $ENV{'HTTP_GETTER_CACHE_MODE'} || 'gzipped';
48 if (($cachemode ne 'gzipped') and ($cachemode ne 'normal')) {
49  die "Invalid HTTP_GETTER_CACHE_MODE environment variable, must be".
50  "'normal' or 'gzipped'\n";
51 }
52 # </ZACK>
53
54 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
55 require $HELM_LIB_PATH;
56
57 # Let's override the configuration file
58 $style_dir = $ENV{"HELM_STYLE_DIR"} if (defined ($ENV{"HELM_STYLE_DIR"}));
59 $dtd_dir = $ENV{"HELM_DTD_DIR"} if (defined ($ENV{"HELM_DTD_DIR"}));
60
61 use HTTP::Daemon;
62 use HTTP::Status;
63 use HTTP::Request;
64 use LWP::UserAgent;
65 use DB_File;
66 use Compress::Zlib;
67 use CGI;
68 use URI::Escape;
69
70 #CSC: mancano i controlli sulle condizioni di errore di molte funzioni
71 #CSC: ==> non e' robusto
72 #CSC: altra roba da sistemare segnata con CSC
73
74 my $d = new HTTP::Daemon LocalPort => 8081;
75 my $myownurl = $d->url;
76
77 # Let's patch the returned URL
78 $myownurl =~ s/\/$//; # chop the final slash
79 my $myownport = $myownurl;
80 $myownport =~ s/http:\/\/(.*):(.*)/$2/;
81 $myownurl  =~ s/http:\/\/(.*):(.*)/$1/;
82 ($myownurl) = gethostbyname($myownurl);
83 $myownurl = "http://".$myownurl.":".$myownport;
84
85 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
86 print "Please contact me at: <URL:", $myownurl."/", ">\n";
87 print "helm_dir: $helm_dir\n";
88 print "style_dir: $style_dir\n";
89 print "dtd_dir: $dtd_dir\n";
90 print "urls_of_uris.db: $uris_dbm.db\n";
91 print "cache mode: $cachemode\n";
92
93 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
94 $SIG{USR1} = \&update; # sent by the child to make the parent update
95 while (my $c = $d->accept) {
96  if (fork() == 0) {
97     while (my $r = $c->get_request) {
98         #CSC: mancano i controlli di sicurezza
99         
100         my $http_method = $r->method;
101         my $http_path = $r->url->path;
102         my $http_query = uri_unescape($r->url->query);
103         my $cgi = new CGI("$http_query");
104         my $inputuri = $cgi->param('uri');
105         print "\nRequest: ".$r->url."\n\n";
106
107         print "\nUnescaped query: ".$http_query."\n";
108
109         if ($http_method eq 'GET' and $http_path eq "/getciconly") {
110             # finds the uri, url and filename
111             my $cicuri = $inputuri;
112             my $answerformat = $cgi->param('format');
113             my $patch_dtd = $cgi->param('patch_dtd');
114             $answerformat = "" if (not defined($answerformat));
115             $patch_dtd = "" if (not defined($patch_dtd));
116             if (($answerformat ne "gz") and ($answerformat ne "normal")
117                and ($answerformat ne "")) {
118              die "Wrong output format: $answerformat, must be 'normal' ".
119                  "or 'gz'\n";
120             }
121             if (($patch_dtd ne "yes") and ($patch_dtd ne "no")
122                and ($patch_dtd ne "")) {
123              die "Wrong param, patch_dtd must be 'yes' or 'no'\n";
124             }
125
126             my $cicfilename = $cicuri;
127             $cicfilename =~ s/cic:(.*)/$1/;
128             $cicfilename =~ s/theory:(.*)/$1/;
129
130             my $cicurl   = $map{$cicuri};
131             my $extension;
132             if ($cicurl =~ /\.xml$/) { # non gzipped file
133               $extension = ".xml";
134             } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
135               $extension = ".xml.gz";
136             } else { # error: unknown extension
137              die "unexpected extension in url: $cicurl, might be '.xml'".
138               "or '.xml.gz'";
139             }
140             $cicfilename = $helm_dir.$cicfilename.$extension;
141
142             if (!defined($cicurl)) {
143              print "\nNOT FOUND!!!!!\n";
144              $c->send_error(RC_NOT_FOUND)
145             } else {
146                print_request("cic",$cicuri,$cicurl,$cicfilename);
147
148                # Retrieves the file
149                my $ciccontent = download($patch_dtd,"cic",$cicurl,$cicfilename,$answerformat);
150
151                # Answering the client
152                 answer($c,$ciccontent,$answerformat);
153             }
154         } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
155             my $filename = $inputuri;
156             $filename = $dtd_dir."/".$filename;
157             print "DTD: $inputuri ==> ($filename)\n";
158             if (stat($filename)) {
159                print "Using local copy\n";
160                open(FD, $filename) or die "Cannot open $filename\n";
161                $cont = "";
162                while(<FD>) {
163                 # Vary bad heuristic for substituion of absolute URLs
164                 # for relative ones
165                 s/ENTITY (.*) SYSTEM\s+"/ENTITY $1 SYSTEM "$myownurl\/getdtd?uri=/g;
166                 $cont .= $_;
167                }
168                close(FD);
169                answer($c,$cont,"normal");
170             } else {
171                die "Could not find DTD!";
172             }
173         } elsif ($http_method eq 'GET' and $http_path eq "/getstyleconf") {
174             my $filename = $inputuri;
175             $filename = $style_dir."/config/".$filename;
176             if (stat($filename)) {
177                open(FD, $filename) or die "Cannot open $filename\n";
178                $cont = "";
179                while(<FD>) {
180                 s/DOCTYPE (.*) SYSTEM\s+"/DOCTYPE $1 SYSTEM "$myownurl\/getstyleconf?uri=/g;
181                 $cont .= $_;
182                }
183                close(FD);
184                answer($c,$cont,"normal");
185             } else {
186                die "Could not find Style Configuration File!";
187             }
188         } elsif ($http_method eq 'GET' and $http_path eq "/getxslt") {
189             my $filename = $inputuri;
190             $filename = $style_dir."/".$filename;
191             print "XSLT: $inputuri ==> ($filename)\n";
192             if (stat($filename)) {
193                print "Using local copy\n";
194                open(FD, $filename) or die "Cannot open $filename\n";
195                $cont = "";
196                while(<FD>) {
197                 # Vary bad heuristic for substituion of absolute URLs
198                 # for relative ones
199                 s/xsl:import\s+href="/xsl:import href="$myownurl\/getxslt?uri=/g ;
200                 s/xsl:include\s+href="/xsl:include href="$myownurl\/getxslt?uri=/g ;
201                 $cont .= $_;
202                }
203                close(FD);
204                answer($c,$cont,"normal");
205             } else {
206                die "Could not find XSLT!";
207             }
208         } elsif ($http_method eq 'GET' and $http_path eq "/update") {
209             # rebuild urls_of_uris.db
210            print "Update requested...\n";
211            mk_urls_of_uris();
212            kill(USR1,getppid()); # signal changes to parent
213            print " done\n";
214            answer($c,"<html><body><h1>Update done</h1></body></html>","normal");
215         } elsif ($http_method eq 'GET' and $http_path eq "/ls") {
216             # send back keys that begin with a given uri
217            my ($uritype,$uripattern,$uriprefix);
218            my $baseuri = $cgi->param('baseuri');
219            chop $baseuri if ($baseuri =~ /.*\/$/); # remove trailing "/"
220            my $outype = $cgi->param('format'); # output type, might be 'txt' or 'xml'
221            $uripattern = $baseuri;
222            $uripattern =~ s/^.*:(.*)/$1/;
223            if ($baseuri =~ /^cic:/) {
224             $uritype = "cic";
225            } elsif ($baseuri =~ /^theory:/) {
226             $uritype = "theory";
227            } else {
228             $uritype = "invalid";
229            }
230            if ($uritype ne "invalid") { # uri is valid
231             if (($outype ne 'txt') and ($outype ne 'xml')) { # invalid out type
232              print "Invalid output type specified: $outype\n";
233              answer($c,"<html><body><h1>Invalid output type, may be ".
234               "\"txt\" or \"xml\"</h1></body></html>","normal");
235             } else { # valid output
236              print "BASEURI $baseuri, FORMAT $outype\n";
237              $cont = finduris($uritype,$uripattern,$outype);
238              answer($c,$cont,"normal");
239             }
240            } else { # invalid uri
241             print "Invalid uri: $baseuri, may begin with 'cic:', ".
242              "'theory:' or '*:'\n";
243             answer($c,"<html><body><h1>Invalid uri , may begin with ".
244              "\"cic:\", \"theory:\" or \"*:\"</h1></body></html>","normal");
245            }
246         } elsif ($http_method eq 'GET' and $http_path eq "/help") {
247            print "Help requested!";
248            answer($c,"<html><body><h1>HTTP Getter Version ".
249             $VERSION."</h1></body></html>","normal");
250         } else {
251             print "\n";
252             print "INVALID REQUEST!!!!!\n";
253             print "(PATH: ",$http_path,", ";
254             print "QUERY: ",$http_query,")\n";
255             $c->send_error(RC_FORBIDDEN)
256         }
257         print "\nRequest solved: ".$r->url."\n\n";
258     }
259     $c->close;
260     undef($c);
261     print "\nCONNECTION CLOSED\n\n";
262     exit;
263   } # fork
264 }
265
266 #================================
267
268 sub finduris { # find uris for cic and theory trees generation
269  my ($uritype,$uripattern,$format) = @_;
270  my $content = "";
271  my ($uri,$localpart,$basepart,$dirname,$suffix,$flags,$key);
272  my (@itemz,@already_pushed_dir);
273  my (%objects,%dirs); # map uris to suffixes' flags
274
275  print "FINDURIS, uritype: $uritype, uripattern: $uripattern, ".
276   "format: $format\n\n";
277  
278  if (($uritype eq "cic") or ($uritype eq "theory")) {
279    # get info only of one type: cic or theory
280   foreach (keys(%map)) { # select matching uris
281    $uri = $_;
282    if ($uri =~ /^$uritype:$uripattern\//) {
283     $localpart = $uri;
284     $localpart =~ s/^$uritype:$uripattern\/(.*)/$1/;
285
286     if ($localpart =~ /^[^\/]*$/) { # no slash, an OBJECT
287      $basepart = $localpart;
288      $basepart =~ s/^([^.]*\.[^.]*)(\.types)?(\.ann)?/$1/; # remove exts .types or
289                                               # .types.ann
290      $flags = $objects{$basepart}; # get old flags
291      if ($localpart =~ /\.ann$/) {
292       $flags = add_flag("ann","YES",$flags);
293      } else {
294       $flags = add_flag("ann","NO",$flags);
295      }
296      if ($localpart =~ /\.types$/) {
297       $flags = add_flag("types","YES",$flags);
298      } elsif ($localpart =~ /\.types\.ann$/) {
299       $flags = add_flag("types","ANN",$flags);
300      } else {
301       $flags = add_flag("types","NO",$flags);
302      }
303      $objects{$basepart} = $flags; # save new flags
304     } else { # exists at least one slash, a DIR
305      ($dirname) = split (/\//, $localpart);
306      $dirs{$dirname} = ""; # no flags requirement for dir
307     }
308    }
309   }
310  } else {
311   die "getter internal error: unsupported uritype: \"$uritype\"";
312  }
313                             # now generate OUTPUT:
314                             # output will be saved in $content
315  if ($format eq "txt") { # TXT output
316   foreach $key (sort(keys %dirs)) {
317    $content .= "dir, " . $key . "\n";
318   }
319   foreach $key (sort(keys %objects)) {
320    $content .= "object, $key, " . $objects{$key} . "\n";
321   }
322  } elsif ($format eq "xml") { # XML output
323   $content .= '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n";
324   $content .= '<!DOCTYPE ls SYSTEM ';
325   $content .= "$myownurl/getdtd?uri=ls.dtd>" . "\n\n";
326   $content .= "<ls>\n";
327   foreach $key (sort(keys %dirs)) {
328    $content .= "\t<section>$key</section>\n";
329   }
330   foreach $key (sort(keys %objects)) {
331    $content .= "\t<object name=\"$key\">\n";
332    $flags = $objects{$key};
333    $flags =~ s/^<(.*)>$/$1/;
334    my ($annflag,$typesflag) = split /,/,$flags;
335    $content .= "\t\t<ann value=\"$annflag\" />\n";
336    $content .= "\t\t<types value=\"$typesflag\" />\n";
337    $content .= "\t</object>\n";
338   }
339   $content .= "</ls>\n";
340  } else { # may not enter this branch
341   die "Getter internal error: invalid format received by finduris sub";
342  }
343  return $content;
344 }
345
346 sub add_flag {
347 # manage string like: "<ann_flag,type_flag>"
348 # "ann_flag" may be one of "ann_YES", "ann_NO"
349 # "type_flag" may be one of "types_NO", "types_YES", "types_ANN"
350 # when adding a flag the max between the current flag and the new flag
351 # is taken, the orders are ann_NO < ann_YES and types_NO < types_YES <
352 # types_ANN
353  my ($flagtype,$newflag,$str) = @_;
354  $str = "<,>" if ($str eq "");
355  ($str =~ s/^<(.*,.*)>$/$1/) or die "Internal error: ".
356    "wrong string format for flag adding in $str";
357  my ($annflag,$typeflag) = split /,/,$str;
358  if ($flagtype eq "ann") { # ANN flag handling
359   if ($newflag eq "YES") {
360    $annflag = "YES";
361   } elsif ($newflag eq "NO") {
362    $annflag = "NO" unless ($annflag eq "YES");
363   } else {
364    die "Internal error: annflag must be \"YES\" or \"NO\"";
365   }
366  } elsif ($flagtype eq "types") { # TYPES flag handling
367   if ($newflag eq "ANN") {
368    $typeflag = "ANN";
369   } elsif ($newflag eq "YES") {
370    $typeflag = "YES" unless ($typeflag eq "ANN");
371   } elsif ($newflag eq "NO") {
372    $typeflag = "NO"
373      unless (($typeflag eq "ANN") or ($typeflag eq "YES"));
374   } else {
375    die "Internal error: typeflag must be \"YES\", \"NO\" or \"ANN\"";
376   }
377  } else {
378   die "Internal error: unsupported flagtype \"$flagtype\"";
379  }
380  $str = "<$annflag,$typeflag>";
381 }
382
383 #CSC: Too much powerful: creates even /home, /home/users/, ...
384 #CSC: Does not raise errors if could not create dirs/files
385 sub mkdirs
386 {
387  my ($pathname) = @_;
388  my @dirs = split /\//,$pathname;
389  my $tmp;
390  foreach $dir (@dirs) {
391   $tmp = ((defined($tmp)) ?  $tmp."\/".$dir : "");
392   mkdir($tmp,0777);
393  }
394  rmdir($tmp);
395 }
396
397 sub print_request
398 {
399  my ($str,$uri,$url,$filename) = @_;
400  print $str."uri: $uri\n";
401  print $str."url: $url\n";
402  print $str."filename: $filename\n\n";
403 }
404
405 sub callback
406 {
407  my ($data) = @_;
408  $cont .= $data;
409 }
410
411 sub gunzip { # gunzip a file and return the deflated content
412         my ($filename) = @_;
413
414         my ($gz, $buffer, $cont);
415
416         print "deflating $filename ...\n";
417         $gz = gzopen($filename, "r")
418                 or die "Cannot open gzip'ed file $filename: $gzerrno";
419         $cont = "";
420         while ( $gz->gzread($buffer) > 0 ) {
421                 $cont .= $buffer;
422         }
423         die "Error while reading : $gzerrno\n" if $gzerrno != Z_STREAM_END ;
424         $gz->gzclose();
425
426         return $cont;
427 }
428
429 sub gzip {      # gzip the content argument and save it to filename argument
430         my ($cont, $filename) = @_;
431  #my $debug=1; # debug only
432
433  print "gzopening $filename ...\n" if (defined($debug));
434         my $gz = gzopen($filename, "w")
435                 or die "Cannot gzopen for writing file $filename: $gzerrno";
436  print "gzwriting ...\n" if (defined($debug));
437         $gz->gzwrite($cont) or die "error writing: $gzerrno, exiting!\n";
438  print "gzclosing ...\n" if (defined($debug));
439         $gz->gzclose();
440 }
441
442 sub download {
443  my ($patch_dtd,$str,$url,$filename,$format) = @_;
444  my ($gz, $buffer);
445
446  #my $debug=1; # for DEBUG only
447
448  my $resourcetype;      # retrieve mode: "normal" (.xml) or "gzipped" (.xml.gz)
449  if ($filename =~ /\.xml$/) {   # set retrieve mode
450          $resourcetype = "normal";
451  } elsif ($filename =~ /\.xml\.gz$/) {
452          $resourcetype = "gzipped";
453  } else {
454          die "Unsupported download extension, might be '.gz' or '.xml'\n";
455  }
456  my $basefname = $filename;
457  $basefname =~ s/\.gz$//;       # get base resource name removing trailing .gz
458  $cont = ""; # modified by side-effect by the callback function
459
460  my $localfname="";
461  if (stat($basefname)) {
462         $localfname=$basefname;
463  } elsif (stat($basefname.".gz")) {
464         $localfname=$basefname.".gz";
465  }
466  if ($localfname ne "") { # we already have local copy of requested file
467       # check both possible cache entry: gzipped or normal
468   print "Using local copy for the $str file\n";
469   if ($localfname =~ /\.xml\.gz$/) { # deflating cached file and return it
470    $cont = gunzip($localfname);
471   } elsif ($localfname =~ /\.xml$/) { # just return cached file
472    open(FD, $localfname) or die "Cannot open $localfname";
473    while(<FD>) { $cont .= $_; }
474    close(FD);
475   } else { # error
476    die "Internal error: unexpected file name $localfname,"
477    ."must end with '.gz' or '.xml.gz'\n";
478   }
479  } else { # download file from net
480    print "Downloading the $str file\n"; # download file
481    my $ua = LWP::UserAgent->new;
482    my $request = HTTP::Request->new(GET => "$url");
483    my $response = $ua->request($request, \&callback);
484    
485    # cache retrieved file to disk
486    # <ZACK/> TODO: inefficent, I haven't yet undestood how to deflate
487    #    in memory gzipped file, without call "gzopen"
488    print "Storing the $str file\n";
489    print "Making dirs ...\n" if (defined($debug));
490    mkdirs($filename);
491    print "Opening tmp file for writing ...\n" if (defined($debug));
492    open(FD, ">".$filename.".tmp") or die "Cannot open $filename.tmp\n";
493    print "Writing on tmp file ...\n" if (defined($debug));
494    print FD $cont;
495    print "Closing tmp file ...\n" if (defined($debug));
496    close(FD);
497
498    # handle cache conversion normal->gzipped or gzipped->normal as user choice
499    print "cachemode:$cachemode, resourcetype:$resourcetype\n" if (defined($debug));
500    if (($cachemode eq 'normal') and ($resourcetype eq 'normal')) {
501      # cache the file as is
502     rename "$filename.tmp", $filename; 
503    } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'gzipped')) {
504      # cache the file as is
505      # and update the $cont variabile with deflated content
506     rename "$filename.tmp", $filename; 
507     $cont = gunzip($filename);
508    } elsif (($cachemode eq 'normal') and ($resourcetype eq 'gzipped')) {
509      # deflate cache entry
510      # and update $cont
511     open(FD, "> $basefname") or die "cannot open $basefname\n";
512     $cont = gunzip($filename.".tmp");
513     print FD $cont;
514     close(FD);
515     unlink "$filename.tmp"; # delete old gzipped file
516    } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'normal')) {
517      # compress cache entry
518     print "gzipping ...\n" if (defined($debug));
519     gzip($cont, $basefname.".gz");
520     unlink "$filename.tmp"; # delete old uncompressed file
521    } else {
522     die "Internal error, unsopported cachemode, resourcetype couple\n";
523    }
524    # $cont now contained uncompressed data
525  }
526  if ($patch_dtd eq "yes") {
527     $cont =~ s/DOCTYPE (.*) SYSTEM\s+"http:\/\/www.cs.unibo.it\/helm\/dtd\//DOCTYPE $1 SYSTEM "$myownurl\/getdtd?uri=/g;
528  }
529  if ($format eq "gz") {
530   gzip($cont,"$basefname.tmp");
531   open (TMP, "< $basefname.tmp")
532    or die "Can't open tempfile: $filename.tmp, exiting!\n";
533   $cont = "";
534   while(<TMP>) {
535    $cont .= $_;
536   }
537   close TMP;
538   unlink ($basefname . ".tmp") or
539    die "cannot unlink temporary file: $basefname.tmp\n";
540  }
541
542  return $cont;
543 }
544
545 sub answer {
546  my ($c,$cont,$contype) = @_; # $contype must be "normal" or "gz"
547  my $res = new HTTP::Response;
548  $res->content($cont);
549  $res->push_header("Content-Type" => "text/plain");
550  if ($contype eq "gz") {
551   $res->push_header("Content-Encoding" => "x-gzip");
552  }
553  $c->send_response($res);
554 }
555
556 sub helm_wget {
557 #retrieve a file from an url and write it to a temp dir
558 #used for retrieve resource index from servers
559  $cont = "";
560  my ($prefix, $URL) = @_;
561  my $ua = LWP::UserAgent->new;
562  my $request = HTTP::Request->new(GET => "$URL");
563  my $response = $ua->request($request, \&callback);
564  my ($filename) = reverse (split "/", $URL); # get filename part of the URL
565  open (TEMP, "> $prefix/$filename")
566   || die "Cannot open temporary file: $prefix/$filename\n";
567  print TEMP $cont;
568  close TEMP;
569 }
570
571 sub update {
572  untie %map;
573  tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
574 }
575
576 sub mk_urls_of_uris {
577 #rebuild $uris_dbm.db fetching resource indexes from servers
578  my (
579   $server, $idxfile, $uri, $url, $comp, $line,
580   @servers,
581   %urls_of_uris
582  );
583
584  untie %map;
585  if (stat $uris_dbm.".db") { # remove old db file
586   unlink($uris_dbm.".db") or
587    die "cannot unlink old db file: $uris_dbm.db\n";
588  }
589  tie(%urls_of_uris, 'DB_File', $uris_dbm.".db", O_RDWR|O_CREAT, 0664);
590
591  open (SRVS, "< $servers_file") or
592   die "cannot open servers file: $servers_file\n";
593  @servers = <SRVS>;
594  close (SRVS);
595  while ($server = pop @servers) { #cicle on servers in reverse order
596   print "processing server: $server ...\n";
597   chomp $server;
598   helm_wget($tmp_dir, $server."/".$indexname); #get index
599   $idxfile = $tmp_dir."/".$indexname;
600   open (INDEX, "< $idxfile") or
601    die "cannot open temporary index file: $idxfile\n";
602   while ($line = <INDEX>) { #parse index and add entry to urls_of_uris
603    chomp $line;
604    ($uri,$comp) = split /[ \t]+/, $line;
605              # build url:
606    if ($comp =~ /gz/) { 
607     $url = $uri . ".xml" . ".gz";
608    } else {
609     $url = $uri . ".xml";
610    }
611    $url =~ s/cic:/$server/;
612    $url =~ s/theory:/$server/;
613    $urls_of_uris{$uri} = $url;
614   }
615   close INDEX;
616   die "cannot unlink temporary file: $idxfile\n"
617    if (unlink $idxfile) != 1;
618  }
619
620  untie(%urls_of_uris);
621  tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
622 }
623