3 # Copyright (C) 2000, HELM Team.
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.
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.
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.
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.
23 # For details, see the HELM World-Wide-Web page,
24 # http://cs.unibo.it/helm/.
28 my $VERSION = "@VERSION@";
32 my $cgi_dir = "@HELM_CGI_DIR@";
35 # First of all, let's load HELM configuration
37 my $HELM_LIB_DIR = $ENV{"HELM_LIB_DIR"};
38 # this should be the only fixed constant
39 my $DEFAULT_HELM_LIB_DIR = "@HELM_LIB_DIR@";
40 if (defined ($HELM_LIB_DIR)) {
41 $HELM_LIB_PATH = $HELM_LIB_DIR."/configuration.pl";
43 $HELM_LIB_PATH = $DEFAULT_HELM_LIB_DIR."/configuration.pl";
46 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm, $indexname
47 require $HELM_LIB_PATH;
49 # TEMP: TODO put these vars in configuration file configuration.xml
51 my $cachemode = $ENV{'HTTP_GETTER_CACHE_MODE'} ||
53 if (($cachemode ne 'gzipped') and ($cachemode ne 'normal')) {
54 die "Invalid HTTP_GETTER_CACHE_MODE environment variable, must be".
55 "'normal' or 'gzipped'\n";
57 my $helm_rdf_dir = $ENV{'HTTP_GETTER_RDF_DIR'} ||
58 "/usr/local/helm/rdf_library";
59 my $rdf_dbm = $ENV{'HTTP_GETTER_RDF_DBM'} ||
60 "/usr/local/helm/rdf_urls_of_uris";
61 my $rdf_indexname = $ENV{'HTTP_GETTER_RDF_INDEXNAME'} ||
63 # </move_to_conf_file>
65 # Let's override the configuration file
66 $style_dir = $ENV{"HELM_STYLE_DIR"} if (defined ($ENV{"HELM_STYLE_DIR"}));
67 $dtd_dir = $ENV{"HELM_DTD_DIR"} if (defined ($ENV{"HELM_DTD_DIR"}));
78 #CSC: mancano i controlli sulle condizioni di errore di molte funzioni
79 #CSC: ==> non e' robusto
80 #CSC: altra roba da sistemare segnata con CSC
82 my $d = new HTTP::Daemon LocalPort => 8081;
83 my $myownurl = $d->url;
85 # Let's patch the returned URL
86 $myownurl =~ s/\/$//; # chop the final slash
87 my $myownport = $myownurl;
88 $myownport =~ s/http:\/\/(.*):(.*)/$2/;
89 $myownurl =~ s/http:\/\/(.*):(.*)/$1/;
90 ($myownurl) = gethostbyname($myownurl);
91 $myownurl = "http://".$myownurl.":".$myownport;
93 tie(%map, 'DB_File', $uris_dbm.".db", O_RDWR, 0664); # open dbs
94 tie(%rdf_map, 'DB_File', $rdf_dbm.".db", O_RDWR, 0664);
97 print "HTTP Getter $VERSION\n"; # print hello information
98 print "Please contact me at: <URL:", $myownurl."/", ">\n";
100 print "helm_dir: $helm_dir\n";
101 print "helm_rdf_dir: $helm_rdf_dir\n";
102 print "style_dir: $style_dir\n";
103 print "dtd_dir: $dtd_dir\n";
104 print "urls_of_uris db: $uris_dbm.db\n";
105 print "rdf db: $rdf_dbm.db\n";
106 print "cache mode: $cachemode\n";
107 print "indexname: $indexname\n";
108 print "rdf_indexname: $rdf_indexname\n";
111 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
112 $SIG{USR1} = \&update; # sent by the child to make the parent update
114 while (my $c = $d->accept) {
116 while (my $r = $c->get_request) {
117 #CSC: mancano i controlli di sicurezza
119 my $http_method = $r->method;
120 my $http_path = $r->url->path;
121 my $http_query = uri_unescape($r->url->query);
122 my $cgi = new CGI("$http_query");
123 my $inputuri = $cgi->param('uri');
124 print "\nRequest: ".$r->url."\n\n";
126 print "\nUnescaped query: ".$http_query."\n";
128 # "getxml" works with rdf uris
129 if ($http_method eq 'GET' and $http_path eq "/getxml") {
130 # finds the uri, url and filename
132 my $answerformat = $cgi->param('format');
133 my $patch_dtd = $cgi->param('patch_dtd');
134 $answerformat = "" if (not defined($answerformat));
135 $patch_dtd = "yes" if (not defined($patch_dtd));
136 if (($answerformat ne "gz") and ($answerformat ne "normal")
137 and ($answerformat ne "")) {
138 die "Wrong output format: $answerformat, must be 'normal' ".
141 if (($patch_dtd ne "yes") and ($patch_dtd ne "no")
142 and ($patch_dtd ne "")) {
143 die "Wrong param, patch_dtd must be 'yes' or 'no'\n";
146 my $filename = $inputuri;
148 if (not isRdfUri($inputuri)) { # standad cic: or theory: uri
149 $filename =~ s/^cic:(.*)/$1/;
150 $filename =~ s/^theory:(.*)/$1/;
152 # The "helm:rdf/<<...>>//cic:" prefix is kept, but quoted
153 # (so that a "helm:rdf/<<...>>//cic:" dir is created
155 $filename =~ s/^(.*)\/\/cic:(.*)/$2/;
156 $filename =~ s/^(.*)\/\/theory:(.*)/$2/;
157 $prefix =~ s/\Q$filename\E//;
160 $filename = $prefix.$filename;
163 my $url = resolve ($inputuri); # resolve uri in url
164 if (not defined($url)) { # uri not found in uri2url map
165 die "NOT FOUND: uri \"$inputuri\" can't be resolved\n";
166 $c->send_error(RC_NOT_FOUND)
167 } else { # uri found and mapped to url
168 my $extension; # file extension
169 if ($url =~ /\.xml$/) { # non gzipped file
171 } elsif ($url =~ /\.xml\.gz$/) { # gzipped file
172 $extension = ".xml.gz";
173 } else { # error: unknown extension
174 die "unexpected extension in url: $url, might be '.xml'".
178 if (not isRdfUri ($inputuri)) { # save in uri std cache dir
179 $filename = $helm_dir.$filename.$extension;
180 } else { # save in rdf metadata cache dir
181 $filename = $helm_rdf_dir."/".$filename.$extension;
184 print_request($inputuri,$url,$filename);
187 my $ciccontent = download($patch_dtd,$url,$filename,$answerformat);
189 if ($answerformat eq "normal") { # answer the client in text/xml
190 answer($c,$ciccontent,"text/xml","");
191 } else { # answer the client in text/xml, gzip encoding
192 answer($c,$ciccontent,"text/xml","x-gzip");
196 # "/register" does not work with rdf uris
197 } elsif ($http_method eq 'GET' and $http_path eq "/register") {
198 my $inputurl = $cgi->param('url');
199 print "Register requested...\n";
200 $map{$inputuri}=$inputurl;
202 # Now let's clean the cache
203 my $cicfilename = $inputuri;
204 $cicfilename =~ s/cic:(.*)/$1/;
205 $cicfilename =~ s/theory:(.*)/$1/;
207 print "Unlinking ".$helm_dir.$cicfilename.".xml[.gz]\n";
208 unlink ($helm_dir.$cicfilename.".xml");
209 unlink ($helm_dir.$cicfilename.".xml.gz");
211 kill(USR1,getppid()); # signal changes to parent
214 html_nice_answer($c,"Register done");
216 # "/resolve" works with rdf uri
217 } elsif ($http_method eq 'GET' and $http_path eq "/resolve") {
218 #my $outputurl = $map{$inputuri};
219 my $outputurl = resolve($inputuri);
220 $outputurl = "" if (not defined($outputurl));
221 $cont = "<?xml version=\"1.0\" ?>\n\n";
222 if ($outputurl eq "") {
223 $cont .= "<unresolved />\n";
225 $cont .= "<url value=\"$outputurl\" />\n";
227 answer($c,$cont,"text/xml","");
229 } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
230 my $filename = $inputuri;
231 $filename = $dtd_dir."/".$filename;
232 print "DTD: $inputuri ==> ($filename)\n";
233 if (stat($filename)) {
234 print "Using local copy\n";
235 open(FD, $filename) or die "Cannot open $filename\n";
238 # Vary bad heuristic for substituion of absolute URLs
240 s/ENTITY (.*) SYSTEM\s+"/ENTITY $1 SYSTEM "$myownurl\/getdtd?uri=/g;
244 answer($c,$cont,"text/xml","");
246 die "Could not find DTD!";
249 } elsif ($http_method eq 'GET' and $http_path eq "/getstyleconf") {
250 my $filename = $inputuri;
251 $filename = $style_dir."/config/".$filename;
252 if (stat($filename)) {
253 open(FD, $filename) or die "Cannot open $filename\n";
256 s/DOCTYPE (.*) SYSTEM\s+"/DOCTYPE $1 SYSTEM "$myownurl\/getstyleconf?uri=/g;
260 answer($c,$cont,"text/plain","");
262 die "Could not find Style Configuration File!";
265 } elsif ($http_method eq 'GET' and $http_path eq "/getxslt") {
266 my $filename = $inputuri;
267 $filename = $style_dir."/".$filename;
268 print "XSLT: $inputuri ==> ($filename)\n";
269 if (stat($filename)) {
270 print "Using local copy\n";
271 open(FD, $filename) or die "Cannot open $filename\n";
274 # Vary bad heuristic for substituion of absolute URLs
276 s/xsl:import\s+href="/xsl:import href="$myownurl\/getxslt?uri=/g ;
277 s/xsl:include\s+href="/xsl:include href="$myownurl\/getxslt?uri=/g ;
281 answer($c,$cont,"text/xml","");
283 die "Could not find XSLT!";
286 # "/update" works with rdf uri
287 } elsif ($http_method eq 'GET' and $http_path eq "/update") {
288 # rebuild urls_of_uris db and rdf uris db
289 print "Update requested...\n";
291 kill(USR1,getppid()); # signal changes to parent
293 html_nice_answer($c,"Update done");
295 # "/ls" does not work with rdf uris
296 } elsif ($http_method eq 'GET' and $http_path eq "/ls") {
297 # send back keys that begin with a given uri
298 my ($uritype,$uripattern,$uriprefix);
299 my $baseuri = $cgi->param('baseuri');
300 chop $baseuri if ($baseuri =~ /.*\/$/); # remove trailing "/"
301 # output type, might be 'txt' or 'xml':
302 my $outype = $cgi->param('format');
303 $uripattern = $baseuri;
304 $uripattern =~ s/^.*:(.*)/$1/;
305 if ($baseuri =~ /^cic:/) {
307 } elsif ($baseuri =~ /^theory:/) {
310 $uritype = "invalid";
312 if ($uritype ne "invalid") { # uri is valid
313 if (($outype ne 'txt') and ($outype ne 'xml')) { # invalid out type
314 print "Invalid output type specified: $outype\n";
315 html_nice_answer($c,"Invalid output type, must be ".
317 } else { # valid output
318 print "BASEURI $baseuri, FORMAT $outype\n";
319 $cont = finduris($uritype,$uripattern,$outype);
320 if ($outype eq 'txt') {
321 answer($c,$cont,"text/plain","");
322 } elsif ($outype eq 'xml') {
323 answer($c,$cont,"text/xml","");
325 die "Internal error, exiting!";
328 } else { # invalid uri
329 print "Invalid uri: $baseuri, may begin with 'cic:', ".
330 "'theory:' or '*:'\n";
331 html_nice_answer($c,"Invalid uri , must begin with ".
332 "'cic:' or 'theory:'");
335 } elsif ($http_method eq 'GET' and $http_path eq "/help") {
337 print "Help requested!\n";
338 html_nice_answer($c,"HTTP Getter Version: $VERSION");
340 } elsif ($http_method eq 'GET' and $http_path =~ /\.cgi$/) {
342 print "CGI requested!\n";
343 if ($http_path !~ /^\/[^\/]*\.cgi$/) {
344 html_nice_answer($c,"Invalid CGI name: $http_path, ".
345 "you can't request CGI that contain a slash in the name\n");
346 } elsif (stat "$cgi_dir"."$http_path") {
347 if (not -x "$cgi_dir/$http_path") {
348 html_nice_answer($c,"CGI $http_path found but not ".
350 } else { # exec CGI and anser back its output
353 $ENV{'QUERY_STRING'} = $http_query;
354 my $cgi_output = `$cgi_dir/$http_path`;
355 answer($c,$cgi_output,"","");
359 html_nice_answer($c,"CGI '$http_path' not found ".
360 "in CGI dir '$cgi_dir'");
363 } else { # unsupported request
365 print "INVALID REQUEST!!!!!\n";
366 print "(PATH: ",$http_path,", ";
367 print "QUERY: ",$http_query,")\n";
368 $c->send_error(RC_FORBIDDEN)
371 print "\nRequest solved: ".$r->url."\n\n";
376 print "\nCONNECTION CLOSED\n\n";
381 #================================
383 sub isRdfUri { # return true if the uri is an rdf uri, false otherwise
385 # helm:rdf/cic:www.cs.unibo.it/helm/rdf/foo_schema//cic:\
386 # /Coq/Init/Logic/True_rec.con.types
388 # the format is "helm:rdf/<metadata_tree>:<metadata_scheme>//<xml_file_uri>"
391 if ($uri =~ /^helm:rdf(.*):(.*)\/\/(.*)/) {
398 sub resolve { # resolve an uri in a url, work both with standard cic: or theory:
400 print "RESOLVE subroutine\n";
402 print "GIVEN URI: \"$uri\"\n";
403 if (isRdfUri ($uri)) { # rdf uri, resolve using rdf db
404 print "IS A RDF URI\n";
405 print "I WILL RETURN '$rdf_map{$uri}'\n";
406 return ($rdf_map{$uri});
407 } else { # standard cic: or theory: uri, resolve using std uri db
408 print "IS NOT A RDF URI\n";
409 print "I WILL RETURN '$map{$uri}'\n";
414 sub finduris { # find uris for cic and theory trees generation
415 my ($uritype,$uripattern,$format) = @_;
417 my ($uri,$localpart,$basepart,$dirname,$suffix,$flags,$key);
418 my (@itemz,@already_pushed_dir);
419 my (%objects,%dirs); # map uris to suffixes' flags
420 my $debug=1; # for debug
422 print "FINDURIS, uritype: $uritype, uripattern: $uripattern, ".
423 "format: $format\n\n" if defined($debug);
425 if (($uritype eq "cic") or ($uritype eq "theory")) {
426 # get info only of one type: cic or theory
427 foreach (keys(%map)) { # select matching uris
429 if ($uri =~ /^$uritype:$uripattern(\/|$|\.)/) {
430 if ($uri =~ /^$uritype:$uripattern\//) { # directory match
432 $localpart =~ s/^$uritype:$uripattern\/(.*)/$1/;
433 } elsif ($uri =~ /^$uritype:$uripattern($|\.)/) { # file match
435 $localpart =~ s/^.*\/([^\/]*)/$1/;
437 die "Internal error, seems that requested match is none of ".
438 "directory match or file match";
440 print "LOCALPART: $localpart\n" if defined($debug);
442 if ($localpart =~ /^[^\/]*$/) { # no slash, an OBJECT
443 $basepart = $localpart;
444 $basepart =~ s/^([^.]*\.[^.]*)(\.types)?(\.ann)?/$1/;
445 # remove exts .types or
447 $flags = $objects{$basepart}; # get old flags
448 if ($localpart =~ /\.ann$/) {
449 $flags = add_flag("ann","YES",$flags);
451 $flags = add_flag("ann","NO",$flags);
453 if ($localpart =~ /\.types$/) {
454 $flags = add_flag("types","YES",$flags);
455 } elsif ($localpart =~ /\.types\.ann$/) {
456 $flags = add_flag("types","ANN",$flags);
458 $flags = add_flag("types","NO",$flags);
460 $objects{$basepart} = $flags; # save new flags
461 } else { # exists at least one slash, a DIR
462 ($dirname) = split (/\//, $localpart);
463 $dirs{$dirname} = ""; # no flags requirement for dir
468 die "getter internal error: unsupported uritype: \"$uritype\"";
470 # now generate OUTPUT:
471 # output will be saved in $content
472 if ($format eq "txt") { # TXT output
473 foreach $key (sort(keys %dirs)) {
474 $content .= "dir, " . $key . "\n";
476 foreach $key (sort(keys %objects)) {
477 $content .= "object, $key, " . $objects{$key} . "\n";
479 } elsif ($format eq "xml") { # XML output
480 $content .= '<?xml version="1.0" encoding="ISO-8859-1"?>' . "\n";
481 $content .= "<!DOCTYPE ls SYSTEM ";
482 $content .= "\"$myownurl/getdtd?uri=ls.dtd\">" . "\n\n";
483 $content .= "<ls>\n";
484 foreach $key (sort(keys %dirs)) {
485 $content .= "\t<section>$key</section>\n";
487 foreach $key (sort(keys %objects)) {
488 $content .= "\t<object name=\"$key\">\n";
489 $flags = $objects{$key};
490 $flags =~ s/^<(.*)>$/$1/;
491 my ($annflag,$typesflag) = split /,/,$flags;
492 $content .= "\t\t<ann value=\"$annflag\" />\n";
493 $content .= "\t\t<types value=\"$typesflag\" />\n";
494 $content .= "\t</object>\n";
496 $content .= "</ls>\n";
497 } else { # may not enter this branch
498 die "Getter internal error: invalid format received by finduris sub";
504 # manage string like: "<ann_flag,type_flag>"
505 # "ann_flag" may be one of "ann_YES", "ann_NO"
506 # "type_flag" may be one of "types_NO", "types_YES", "types_ANN"
507 # when adding a flag the max between the current flag and the new flag
508 # is taken, the orders are ann_NO < ann_YES and types_NO < types_YES <
510 my ($flagtype,$newflag,$str) = @_;
511 $str = "<,>" if ($str eq "");
512 ($str =~ s/^<(.*,.*)>$/$1/) or die "Internal error: ".
513 "wrong string format for flag adding in $str";
514 my ($annflag,$typeflag) = split /,/,$str;
515 if ($flagtype eq "ann") { # ANN flag handling
516 if ($newflag eq "YES") {
518 } elsif ($newflag eq "NO") {
519 $annflag = "NO" unless ($annflag eq "YES");
521 die "Internal error: annflag must be \"YES\" or \"NO\"";
523 } elsif ($flagtype eq "types") { # TYPES flag handling
524 if ($newflag eq "ANN") {
526 } elsif ($newflag eq "YES") {
527 $typeflag = "YES" unless ($typeflag eq "ANN");
528 } elsif ($newflag eq "NO") {
530 unless (($typeflag eq "ANN") or ($typeflag eq "YES"));
532 die "Internal error: typeflag must be \"YES\", \"NO\" or \"ANN\"";
535 die "Internal error: unsupported flagtype \"$flagtype\"";
537 $str = "<$annflag,$typeflag>";
540 #CSC: Too much powerful: creates even /home, /home/users/, ...
541 #CSC: Does not raise errors if could not create dirs/files
545 my @dirs = split /\//,$pathname;
547 foreach $dir (@dirs) {
548 $tmp = ((defined($tmp)) ? $tmp."\/".$dir : "");
556 my ($uri,$url,$filename) = @_;
557 print "<download_request>\n";
560 print "filename: $filename\n\n";
561 print "</download_request>\n";
570 sub gunzip { # gunzip a file and return the deflated content
573 my ($gz, $buffer, $cont);
575 print "deflating $filename ...\n";
576 $gz = gzopen($filename, "r")
577 or die "Cannot open gzip'ed file $filename: $gzerrno";
579 while ( $gz->gzread($buffer) > 0 ) {
582 die "Error while reading : $gzerrno\n" if $gzerrno != Z_STREAM_END ;
588 sub gzip { # gzip the content argument and save it to filename argument
589 my ($cont, $filename) = @_;
590 #my $debug=1; # debug only
592 print "gzopening $filename ...\n" if (defined($debug));
593 my $gz = gzopen($filename, "w")
594 or die "Cannot gzopen for writing file $filename: $gzerrno";
595 print "gzwriting ...\n" if (defined($debug));
596 $gz->gzwrite($cont) or die "error writing: $gzerrno, exiting!\n";
597 print "gzclosing ...\n" if (defined($debug));
602 my ($patch_dtd,$url,$filename,$format) = @_;
605 # print "DOWNLOAD subs receives url: \"$url\"\n";
606 # print "DOWNLOAD subs receives filename: \"$filename\"\n";
608 my $resourcetype; # retrieve mode: "normal" (.xml) or "gzipped" (.xml.gz)
609 if ($filename =~ /\.xml$/) { # set retrieve mode
610 $resourcetype = "normal";
611 } elsif ($filename =~ /\.xml\.gz$/) {
612 $resourcetype = "gzipped";
614 die "Unsupported download extension, might be '.gz' or '.xml'\n";
616 my $basefname = $filename;
617 $basefname =~ s/\.gz$//; # get base resource name removing trailing .gz
618 $cont = ""; # modified by side-effect by the callback function
621 if (stat($basefname)) {
622 $localfname=$basefname;
623 } elsif (stat($basefname.".gz")) {
624 $localfname=$basefname.".gz";
626 if ($localfname ne "") { # we already have local copy of requested file
627 # check both possible cache entry: gzipped or normal
628 print "Using local copy.\n";
629 if ($localfname =~ /\.xml\.gz$/) { # deflating cached file and return it
630 $cont = gunzip($localfname);
631 } elsif ($localfname =~ /\.xml$/) { # just return cached file
632 open(FD, $localfname) or die "Cannot open $localfname";
633 while(<FD>) { $cont .= $_; }
636 die "Internal error: unexpected file name $localfname,"
637 ."must end with '.gz' or '.xml.gz'\n";
639 } else { # download file from net
640 print "Downloading file ...\n"; # download file
641 my $ua = LWP::UserAgent->new;
642 my $request = HTTP::Request->new(GET => "$url");
643 my $response = $ua->request($request, \&callback);
645 # cache retrieved file to disk
646 # <ZACK/> TODO: inefficent, I haven't yet undestood how to deflate
647 # in memory gzipped file, without call "gzopen"
648 print "Storing file ...\n";
649 # print "Making dirs ...\n";
651 # print "Opening tmp file for writing ...\n";
652 open(FD, ">".$filename.".tmp") or die "Cannot open $filename.tmp\n";
653 # print "Writing on tmp file ...\n";
655 # print "Closing tmp file ...\n";
658 # handle cache conversion normal->gzipped or gzipped->normal as user choice
659 print "cachemode:$cachemode, resourcetype:$resourcetype\n"
660 if (defined($debug));
661 if (($cachemode eq 'normal') and ($resourcetype eq 'normal')) {
662 # cache the file as is
663 rename "$filename.tmp", $filename;
664 } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'gzipped')) {
665 # cache the file as is
666 # and update the $cont variabile with deflated content
667 rename "$filename.tmp", $filename;
668 $cont = gunzip($filename);
669 } elsif (($cachemode eq 'normal') and ($resourcetype eq 'gzipped')) {
670 # deflate cache entry
672 open(FD, "> $basefname") or die "cannot open $basefname\n";
673 $cont = gunzip($filename.".tmp");
676 unlink "$filename.tmp"; # delete old gzipped file
677 } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'normal')) {
678 # compress cache entry
679 print "gzipping ...\n" if (defined($debug));
680 gzip($cont, $basefname.".gz");
681 unlink "$filename.tmp"; # delete old uncompressed file
683 die "Internal error, unsopported cachemode, resourcetype couple\n";
685 # $cont now contained uncompressed data
687 if ($patch_dtd eq "yes") {
688 $cont =~ s/DOCTYPE (.*) SYSTEM\s+"http:\/\/www.cs.unibo.it\/helm\/dtd\//DOCTYPE $1 SYSTEM "$myownurl\/getdtd?uri=/g;
690 if ($format eq "gz") {
691 gzip($cont,"$basefname.tmp");
692 open (TMP, "< $basefname.tmp")
693 or die "Can't open tempfile: $filename.tmp, exiting!\n";
699 unlink ($basefname . ".tmp") or
700 die "cannot unlink temporary file: $basefname.tmp\n";
707 # answer the client setting content, Content-Type and Content-Enconding
709 my ($c,$cont,$contype,$contenc) = @_;
710 my $res = new HTTP::Response;
711 $res->content($cont);
712 $res->push_header("Content-Type" => $contype)
713 unless ($contype eq "");
714 $res->push_header("Content-Encoding" => $contenc)
715 unless ($contenc eq "");
716 $res->push_header("Cache-Control" => "no-cache");
717 $res->push_header("Pragma" => "no-cache");
718 $res->push_header("Expires" => "0");
719 $c->send_response($res);
722 sub html_nice_answer {
723 # answer the client whith a nice html document
724 my ($c,$content) = @_;
725 $content = "<html><body><h1>$content</h1></body></html>";
726 answer($c,$content,"text/html","");
730 #retrieve a file from an url and write it to a temp dir
731 #used for retrieve resource index from servers
733 my ($prefix, $URL) = @_;
734 my $ua = LWP::UserAgent->new;
735 my $request = HTTP::Request->new(GET => "$URL");
736 my $response = $ua->request($request, \&callback);
737 my ($filename) = reverse (split "/", $URL); # get filename part of the URL
738 open (TEMP, "> $prefix/$filename")
739 || die "Cannot open temporary file: $prefix/$filename\n";
747 tie(%map, 'DB_File', $uris_dbm.".db", O_RDWR, 0664);
749 tie(%rdf_map, 'DB_File', $rdf_dbm.".db", O_RDWR, 0664);
753 # rebuild dbs fetching resource indexes from servers.
754 # Rebuild urls_of_uris db (1) and rdf db (2)
756 $server, $idxfile, $rdf_idxfile, $uri, $url, $comp, $line,
758 %urls_of_uris, %rdf # local proxy of $map and $rdf_map
763 foreach my $file ($uris_dbm, $rdf_dbm) { # remove old db file
764 if (stat $file.".db") { # remove if exists
765 unlink ($file.".db") or die "can't unlink old db file: $file.db\n";
768 tie(%urls_of_uris, 'DB_File', $uris_dbm.".db", O_RDWR|O_CREAT, 0664);
769 tie(%rdf, 'DB_File', $rdf_dbm.".db", O_RDWR|O_CREAT, 0664);
771 open (SRVS, "< $servers_file") or
772 die "cannot open servers file: $servers_file\n";
773 @servers = <SRVS>; # read all servers
776 while ($server = pop @servers) { # cicle on servers in _reverse_ order
777 print "processing server: $server ...\n";
779 helm_wget($tmp_dir, $server."/".$indexname); # get index
780 helm_wget($tmp_dir, $server."/".$rdf_indexname); # get rdf index
781 $idxfile = $tmp_dir."/".$indexname;
782 $rdf_idxfile = $tmp_dir."/".$rdf_indexname;
784 open (INDEX, "< $idxfile") or # (1) REBUILD URLS_OF_URIS DB
785 die "cannot open temporary index file: $idxfile\n";
786 while ($line = <INDEX>) { # parse index and add entry to urls_of_uris db
788 ($uri,$comp) = split /[ \t]+/, $line;
791 $url = $uri . ".xml" . ".gz";
793 $url = $uri . ".xml";
795 $url =~ s/cic:/$server/;
796 $url =~ s/theory:/$server/;
797 $urls_of_uris{$uri} = $url;
800 open (RDF_INDEX, "< $rdf_idxfile") or # (2) REBUILD RDF DB
801 die "cannot open temporary rdf index file: $rdf_idxfile\n";
802 while ($line = <RDF_INDEX>) { # parse index and add entry to rdf db
804 ($uri,$comp) = split /[ \t]+/, $line; # comp is "gz" or nothing
807 $url = $uri . ".xml" . ".gz";
809 $url = $uri . ".xml";
811 $url =~ s/^helm:rdf(.*)\/\/cic:/$server/;
812 $url =~ s/^helm:rdf(.*)\/\/theory:/$server/;
816 close INDEX; # close indexes
818 die "cannot unlink temporary file: $idxfile\n" # remove temp files
819 if (unlink $idxfile) != 1;
820 die "cannot unlink temporary file: $rdf_idxfile\n"
821 if (unlink $rdf_idxfile) != 1;
825 untie(%urls_of_uris); # untie local proxies
827 tie(%map, 'DB_File', $uris_dbm.".db", O_RDWR, 0664); # retie global ones
828 tie(%rdf_map, 'DB_File', $rdf_dbm.".db", O_RDWR, 0664);
831 # vim modline: do not remove!