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/.
26 my $VERSION = "@VERSION@";
28 # First of all, let's load HELM configuration
30 my $HELM_LIB_DIR = $ENV{"HELM_LIB_DIR"};
31 # this should be the only fixed constant
32 my $DEFAULT_HELM_LIB_DIR = "@HELM_LIB_DIR@";
33 if (defined ($HELM_LIB_DIR)) {
34 $HELM_LIB_PATH = $HELM_LIB_DIR."/configuration.pl";
36 $HELM_LIB_PATH = $DEFAULT_HELM_LIB_DIR."/configuration.pl";
39 # <ZACK>: TODO temporary, move this setting to configuration file
40 # set the cache mode, may be "gzipped" or "normal"
41 my $cachemode = $ENV{'HTTP_GETTER_CACHE_MODE'} || 'gzipped';
42 if (($cachemode ne 'gzipped') and ($cachemode ne 'normal')) {
43 die "Invalid HTTP_GETTER_CACHE_MODE environment variable, must be".
44 "'normal' or 'gzipped'\n";
48 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
49 require $HELM_LIB_PATH;
51 # Let's override the configuration file
52 $style_dir = $ENV{"HELM_STYLE_DIR"} if (defined ($ENV{"HELM_STYLE_DIR"}));
53 $dtd_dir = $ENV{"HELM_DTD_DIR"} if (defined ($ENV{"HELM_DTD_DIR"}));
62 #CSC: mancano i controlli sulle condizioni di errore di molte funzioni
63 #CSC: ==> non e' robusto
64 #CSC: altra roba da sistemare segnata con CSC
66 my $d = new HTTP::Daemon LocalPort => 8081;
67 my $myownurl = $d->url;
69 # Let's patch the returned URL
70 $myownurl =~ s/\/$//; # chop the final slash
71 my $myownport = $myownurl;
72 $myownport =~ s/http:\/\/(.*):(.*)/$2/;
73 $myownurl =~ s/http:\/\/(.*):(.*)/$1/;
74 ($myownurl) = gethostbyname($myownurl);
75 $myownurl = "http://".$myownurl.":".$myownport;
77 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
78 print "Please contact me at: <URL:", $myownurl."/", ">\n";
79 print "helm_dir: $helm_dir\n";
80 print "style_dir: $style_dir\n";
81 print "dtd_dir: $dtd_dir\n";
82 print "urls_of_uris.db: $uris_dbm.db\n";
83 print "cache mode: $cachemode\n";
85 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
86 $SIG{USR1} = \&update; # sent by the child to make the parent update
87 while (my $c = $d->accept) {
89 while (my $r = $c->get_request) {
90 #CSC: mancano i controlli di sicurezza
92 my $inputuri = $r->url;
93 $inputuri =~ s/^[^?]*\?uri=(.*)/$1/;
94 print "\nRequest: ".$r->url."\n\n";
95 my $http_method = $r->method;
96 my $http_path = $r->url->path;
98 if ($http_method eq 'GET' and $http_path eq "/getciconly") {
99 # finds the uri, url and filename
100 my $cicuri = $inputuri;
102 my $cicfilename = $cicuri;
103 $cicfilename =~ s/cic:(.*)/$1/;
104 $cicfilename =~ s/theory:(.*)/$1/;
106 my $cicurl = $map{$cicuri};
108 if ($cicurl =~ /\.xml$/) { # non gzipped file
110 } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
111 $extension = ".xml.gz";
112 } else { # error: unknown extension
113 die "unexpected extension in url: $cicurl, might be '.xml'".
116 $cicfilename = $helm_dir.$cicfilename.$extension;
118 if (!defined($cicurl)) {
119 print "\nNOT FOUND!!!!!\n";
120 $c->send_error(RC_NOT_FOUND)
122 print_request("cic",$cicuri,$cicurl,$cicfilename);
125 my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
127 # Answering the client
128 answer($c,$ciccontent);
130 } elsif ($http_method eq 'GET' and $http_path eq "/get") {
131 # finds the uris, urls and filenames
132 my $cicuri = $inputuri,
133 $typesuri = $inputuri,
136 if ($inputuri =~ /\.types$/) {
137 $cicuri =~ s/(.*)\.types$/$1/;
139 } elsif ($inputuri =~ /\.types\.ann$/) {
140 $cicuri =~ s/(.*)\.types\.ann$/$1/;
141 $typesuri =~ s/(.*)\.ann$/$1/;
142 $annsuffix = ".types.ann";
143 } elsif ($inputuri =~ /\.ann$/) {
144 $cicuri =~ s/(.*)\.ann$/$1/;
152 my $cicfilename = $cicuri;
153 $cicfilename =~ s/cic:(.*)/$1/;
154 $cicfilename =~ s/theory:(.*)/$1/;
155 $cicfilename = $helm_dir.$cicfilename;
157 my $cicurl = $map{$cicuri};
158 my $typesurl = $map{$typesuri} if (defined($typesuri));
159 my $annurl = $map{$annuri} if (defined($annuri));
160 my ($cicext, $typesext, $annext);
161 if ($cicurl =~ /\.xml$/) { # normal file
163 } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
166 die "unexpected extension in url: $cicurl;".
167 "might be '.xml' or '.xml.gz'";
169 if (defined($typesuri)) { # extension selection for types file
170 if ($typesurl =~ /\.xml$/) { # normal file
171 $typesext = ".types.xml";
172 } elsif ($typesurl =~ /\.xml\.gz$/) { # gzipped file
173 $typesext = ".types.xml.gz";
175 die "unexpected extension in url: $typesurl;".
176 "might be '.xml' or '.xml.gz'";
179 if (defined($annuri)) { # extension selection for annotation file
180 if ($annurl =~ /\.xml$/) { # normal file
182 } elsif ($annurl =~ /\.xml\.gz$/) { # gzipped file
185 die "unexpected extension in url: $annurl".
186 "might be '.xml' or '.xml.gz'";
189 my $typesfilename = $cicfilename.$typesext if $typesuri;
190 my $annfilename = $cicfilename.$annsuffix.$annext if $annuri;
191 $cicfilename .= $cicext;
193 if (!defined($cicurl) ||
194 (!defined($typesurl) && $typesuri) ||
195 (!defined($annuri) && $annuri))
197 print "\nNOT FOUND!!!!!\n";
198 $c->send_error(RC_NOT_FOUND)
200 print_request("cic",$cicuri,$cicurl,$cicfilename);
201 print_request("types",$typesuri,$typesurl,$typesfilename)
203 print_request("ann",$annuri,$annurl,$annfilename)
206 # Retrieves the files
208 my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
210 download(1,"types",$typesurl,$typesfilename) if ($typesuri);
212 download(1,"ann",$annurl,$annfilename) if ($annuri);
214 # Merging the files together
217 <?xml version="1.0" encoding="UTF-8"?>
218 <cicxml uri="$cicuri">
225 # Answering the client
228 } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
229 my $filename = $inputuri;
230 $filename = $dtd_dir."/".$filename;
231 print "DTD: $inputuri ==> ($filename)\n";
232 if (stat($filename)) {
233 print "Using local copy\n";
234 open(FD, $filename) or die "Cannot open $filename\n";
237 # Vary bad heuristic for substituion of absolute URLs
239 s/ENTITY (.*) SYSTEM\s+"/ENTITY $1 SYSTEM "$myownurl\/getdtd?uri=/g;
245 die "Could not find DTD!";
247 } elsif ($http_method eq 'GET' and $http_path eq "/getstyleconf") {
248 my $filename = $inputuri;
249 $filename = $style_dir."/config/".$filename;
250 if (stat($filename)) {
251 open(FD, $filename) or die "Cannot open $filename\n";
254 s/DOCTYPE (.*) SYSTEM\s+"/DOCTYPE $1 SYSTEM "$myownurl\/getstyleconf?uri=/g;
260 die "Could not find Style Configuration File!";
262 } elsif ($http_method eq 'GET' and $http_path eq "/getxslt") {
263 my $filename = $inputuri;
264 $filename = $style_dir."/".$filename;
265 print "XSLT: $inputuri ==> ($filename)\n";
266 if (stat($filename)) {
267 print "Using local copy\n";
268 open(FD, $filename) or die "Cannot open $filename\n";
271 # Vary bad heuristic for substituion of absolute URLs
273 s/xsl:import\s+href="/xsl:import href="$myownurl\/getxslt?uri=/g ;
274 s/xsl:include\s+href="/xsl:include href="$myownurl\/getxslt?uri=/g ;
280 die "Could not find XSLT!";
282 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
283 my $quoted_html_link = $html_link;
284 $quoted_html_link =~ s/&/&/g;
285 $quoted_html_link =~ s/</</g;
286 $quoted_html_link =~ s/>/>/g;
287 $quoted_html_link =~ s/'/'/g;
288 $quoted_html_link =~ s/"/"/g;
289 print "\nConfiguration requested, returned #$quoted_html_link#\n";
290 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
292 } elsif ($http_method eq 'GET' and $http_path eq "/update") {
293 print "Update requested...\n";
295 kill(USR1,getppid()); # signal changes to parent
297 answer($c,"<html><body><h1>Update done</h1></body></html>");
298 } elsif ($http_method eq 'GET' and $http_path eq "/version") {
299 print "Version requested!";
300 answer($c,"<html><body><h1>HTTP Getter Version ".$VERSION."</h1></body></html>");
302 print "\nINVALID REQUEST!!!!!\n";
303 $c->send_error(RC_FORBIDDEN)
305 print "\nRequest solved: ".$r->url."\n\n";
309 print "\nCONNECTION CLOSED\n\n";
314 #================================
317 #CSC: Too much powerful: creates even /home, /home/users/, ...
318 #CSC: Does not raise errors if could not create dirs/files
322 my @dirs = split /\//,$pathname;
324 foreach $dir (@dirs) {
325 $tmp = ((defined($tmp)) ? $tmp."\/".$dir : "");
333 my ($str,$uri,$url,$filename) = @_;
334 print $str."uri: $uri\n";
335 print $str."url: $url\n";
336 print $str."filename: $filename\n\n";
345 sub gunzip { # gunzip a file and return the deflated content
348 my ($gz, $buffer, $cont);
350 print "deflating $filename ...\n";
351 $gz = gzopen($filename, "r")
352 or die "Cannot open gzip'ed file $filename: $gzerrno";
354 while ( $gz->gzread($buffer) > 0 ) {
357 die "Error while reading : $gzerrno\n" if $gzerrno != Z_STREAM_END ;
363 sub gzip { # gzip the content argument and save it to filename argument
364 my ($cont, $filename) = @_;
368 $gz = gzopen($filename, "w")
369 or die "Cannot gzopen for writing file $filename: $gzerrno";
370 $gz->gzwrite($cont) or die "error writing: $gzerrno\n" ;
376 my ($remove_headers,$str,$url,$filename) = @_;
379 my $resourcetype; # retrieve mode: "normal" (.xml) or "gzipped" (.xml.gz)
380 if ($filename =~ /\.xml$/) { # set retrieve mode
381 $resourcetype = "normal";
382 } elsif ($filename =~ /\.xml\.gz$/) {
383 $resourcetype = "gzipped";
385 die "Unsupported download extension, might be '.gz' or '.xml'\n";
387 my $basefname = $filename;
388 $basefname =~ s/\.gz$//; # get base resource name removing trailing .gz
389 $cont = ""; # modified by side-effect by the callback function
392 if (stat($basefname)) {
393 $localfname=$basefname;
394 } elsif (stat($basefname.".gz")) {
395 $localfname=$basefname.".gz";
397 if ($localfname ne "") { # we already have local copy of requested file
398 # check both possible cache entry: gzipped or normal
399 print "Using local copy for the $str file\n";
400 if ($localfname =~ /\.xml\.gz$/) { # deflating cached file and return it
401 $cont = gunzip($localfname);
402 } elsif ($localfname =~ /\.xml$/) { # just return cached file
403 open(FD, $localfname) or die "Cannot open $localfname";
404 while(<FD>) { $cont .= $_; }
407 die "Internal error: unexpected file name $localfname,"
408 ."must end with '.gz' or '.xml.gz'\n";
410 } else { # download file from net
411 print "Downloading the $str file\n"; # download file
412 $ua = LWP::UserAgent->new;
413 $request = HTTP::Request->new(GET => "$url");
414 $response = $ua->request($request, \&callback);
416 # cache retrieved file to disk
417 # <ZACK/> TODO: inefficent, I haven't yet undestood how to deflate
418 # in memory gzipped file, without call "gzopen"
419 print "Storing the $str file\n";
421 open(FD, ">".$filename.".tmp") or die "Cannot open $filename.tmp\n";
425 # handle cache conversion normal->gzipped or gzipped->normal as user choice
426 if (($cachemode eq 'normal') and ($resourcetype eq 'normal')) {
427 # cache the file as is
428 rename "$filename.tmp", $filename;
429 } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'gzipped')) {
430 # cache the file as is
431 # and update the $cont variabile with deflated content
432 rename "$filename.tmp", $filename;
433 $cont = gunzip($filename);
434 } elsif (($cachemode eq 'normal') and ($resourcetype eq 'gzipped')) {
435 # deflate cache entry
437 open(FD, "> $basefname") or die "cannot open $basefname\n";
438 $cont = gunzip($filename.".tmp");
441 unlink "$filename.tmp"; # delete old gzipped file
442 } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'normal')) {
443 # compress cache entry
444 gzip($cont, $basefname.".gz");
445 unlink "$filename.tmp"; # delete old uncompressed file
447 die "Internal error, unsopported cachemode, resourcetype couple\n";
449 # $cont now contained uncompressed data
452 if ($remove_headers) {
453 $cont =~ s/<\?xml [^?]*\?>//sg;
454 $cont =~ s/<!DOCTYPE [^>]*>//sg;
462 my $res = new HTTP::Response;
463 $res->content($cont);
464 $c->send_response($res);
468 #ported from helm_wget executable, TODO: rewrite without wget.
469 #retrieve a file from an url and write it to a temp dir
470 #used for retrieve resource indexe from servers
471 my ($prefix, $URL) = @_;
472 if ($URL =~ /^file:\//) {
473 $URL =~ s/^file:\///;
474 my $command = "mkdir -p $prefix ; cp $URL $prefix";
476 system($command) == 0
477 or die "\"$command\" error";
479 my $command = "wget -c -P $prefix $URL";
480 system($command) == 0
481 or die "\"$command\" error";
487 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
490 sub mk_urls_of_uris {
491 #rebuild $uris_dbm.db fetching resource indexes from servers
493 $server, $idxfile, $uri, $url, $comp, $line,
499 if (stat $uris_dbm.".db") { # remove old db file
500 unlink($uris_dbm.".db") or
501 die "cannot unlink old db file: $uris_dbm.db\n";
503 tie(%urls_of_uris, 'DB_File', $uris_dbm.".db", O_RDWR|O_CREAT, 0664);
505 open (SRVS, "< $servers_file") or
506 die "cannot open servers file: $servers_file\n";
509 while ($server = pop @servers) { #cicle on servers in reverse order
510 print "processing server: $server ...\n";
512 $debugserver = "http://dalamar.krynn.it/helm"; # FOR DEBUG ONLY: REMOVE ME !!
513 helm_wget($tmp_dir, $server."/".$indexname); #get index
514 $idxfile = $tmp_dir."/".$indexname;
515 open (INDEX, "< $idxfile") or
516 die "cannot open temporary index file: $idxfile\n";
517 while ($line = <INDEX>) { #parse index and add entry to urls_of_uris
519 ($uri,$comp) = split /[ \t]+/, $line;
522 $url = $uri . ".xml" . ".gz";
524 $url = $uri . ".xml";
526 $url =~ s/cic:/$server/;
527 $url =~ s/theory:/$server/;
528 $urls_of_uris{$uri} = $url;
531 die "cannot unlink temporary file: $idxfile\n"
532 if (unlink $idxfile) != 1;
535 untie(%urls_of_uris);
536 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);