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"}));
63 #CSC: mancano i controlli sulle condizioni di errore di molte funzioni
64 #CSC: ==> non e' robusto
65 #CSC: altra roba da sistemare segnata con CSC
67 my $d = new HTTP::Daemon LocalPort => 8081;
68 my $myownurl = $d->url;
70 # Let's patch the returned URL
71 $myownurl =~ s/\/$//; # chop the final slash
72 my $myownport = $myownurl;
73 $myownport =~ s/http:\/\/(.*):(.*)/$2/;
74 $myownurl =~ s/http:\/\/(.*):(.*)/$1/;
75 ($myownurl) = gethostbyname($myownurl);
76 $myownurl = "http://".$myownurl.":".$myownport;
78 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
79 print "Please contact me at: <URL:", $myownurl."/", ">\n";
80 print "helm_dir: $helm_dir\n";
81 print "style_dir: $style_dir\n";
82 print "dtd_dir: $dtd_dir\n";
83 print "urls_of_uris.db: $uris_dbm.db\n";
84 print "cache mode: $cachemode\n";
86 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
87 $SIG{USR1} = \&update; # sent by the child to make the parent update
88 while (my $c = $d->accept) {
90 while (my $r = $c->get_request) {
91 #CSC: mancano i controlli di sicurezza
93 my $inputuri = uri_unescape($r->url);
94 $inputuri =~ s/^[^?]*\?uri=(.*)/$1/;
95 print "\nRequest: ".$r->url."\n\n";
96 my $http_method = $r->method;
97 my $http_path = $r->url->path;
98 my $http_query = uri_unescape($r->url->query);
100 print "\nUnescaped query: ".$http_query."\n";
102 if ($http_method eq 'GET' and $http_path eq "/getciconly") {
103 # finds the uri, url and filename
104 my $cicuri = $inputuri;
106 my $cicfilename = $cicuri;
107 $cicfilename =~ s/cic:(.*)/$1/;
108 $cicfilename =~ s/theory:(.*)/$1/;
110 my $cicurl = $map{$cicuri};
112 if ($cicurl =~ /\.xml$/) { # non gzipped file
114 } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
115 $extension = ".xml.gz";
116 } else { # error: unknown extension
117 die "unexpected extension in url: $cicurl, might be '.xml'".
120 $cicfilename = $helm_dir.$cicfilename.$extension;
122 if (!defined($cicurl)) {
123 print "\nNOT FOUND!!!!!\n";
124 $c->send_error(RC_NOT_FOUND)
126 print_request("cic",$cicuri,$cicurl,$cicfilename);
129 my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
131 # Answering the client
132 answer($c,$ciccontent);
134 } elsif ($http_method eq 'GET' and $http_path eq "/get") {
135 # finds the uris, urls and filenames
136 my $cicuri = $inputuri,
137 $typesuri = $inputuri,
140 if ($inputuri =~ /\.types$/) {
141 $cicuri =~ s/(.*)\.types$/$1/;
143 } elsif ($inputuri =~ /\.types\.ann$/) {
144 $cicuri =~ s/(.*)\.types\.ann$/$1/;
145 $typesuri =~ s/(.*)\.ann$/$1/;
146 $annsuffix = ".types.ann";
147 } elsif ($inputuri =~ /\.ann$/) {
148 $cicuri =~ s/(.*)\.ann$/$1/;
156 my $cicfilename = $cicuri;
157 $cicfilename =~ s/cic:(.*)/$1/;
158 $cicfilename =~ s/theory:(.*)/$1/;
159 $cicfilename = $helm_dir.$cicfilename;
161 my $cicurl = $map{$cicuri};
162 my $typesurl = $map{$typesuri} if (defined($typesuri));
163 my $annurl = $map{$annuri} if (defined($annuri));
164 my ($cicext, $typesext, $annext);
165 if ($cicurl =~ /\.xml$/) { # normal file
167 } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
170 die "unexpected extension in url: $cicurl;".
171 "might be '.xml' or '.xml.gz'";
173 if (defined($typesuri)) { # extension selection for types file
174 if ($typesurl =~ /\.xml$/) { # normal file
175 $typesext = ".types.xml";
176 } elsif ($typesurl =~ /\.xml\.gz$/) { # gzipped file
177 $typesext = ".types.xml.gz";
179 die "unexpected extension in url: $typesurl;".
180 "might be '.xml' or '.xml.gz'";
183 if (defined($annuri)) { # extension selection for annotation file
184 if ($annurl =~ /\.xml$/) { # normal file
186 } elsif ($annurl =~ /\.xml\.gz$/) { # gzipped file
189 die "unexpected extension in url: $annurl".
190 "might be '.xml' or '.xml.gz'";
193 my $typesfilename = $cicfilename.$typesext if $typesuri;
194 my $annfilename = $cicfilename.$annsuffix.$annext if $annuri;
195 $cicfilename .= $cicext;
197 if (!defined($cicurl) ||
198 (!defined($typesurl) && $typesuri) ||
199 (!defined($annuri) && $annuri))
201 print "\nNOT FOUND!!!!!\n";
202 $c->send_error(RC_NOT_FOUND)
204 print_request("cic",$cicuri,$cicurl,$cicfilename);
205 print_request("types",$typesuri,$typesurl,$typesfilename)
207 print_request("ann",$annuri,$annurl,$annfilename)
210 # Retrieves the files
212 my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
214 download(1,"types",$typesurl,$typesfilename) if ($typesuri);
216 download(1,"ann",$annurl,$annfilename) if ($annuri);
218 # Merging the files together
221 <?xml version="1.0" encoding="UTF-8"?>
222 <cicxml uri="$cicuri">
229 # Answering the client
232 } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
233 my $filename = $inputuri;
234 $filename = $dtd_dir."/".$filename;
235 print "DTD: $inputuri ==> ($filename)\n";
236 if (stat($filename)) {
237 print "Using local copy\n";
238 open(FD, $filename) or die "Cannot open $filename\n";
241 # Vary bad heuristic for substituion of absolute URLs
243 s/ENTITY (.*) SYSTEM\s+"/ENTITY $1 SYSTEM "$myownurl\/getdtd?uri=/g;
249 die "Could not find DTD!";
251 } elsif ($http_method eq 'GET' and $http_path eq "/getstyleconf") {
252 my $filename = $inputuri;
253 $filename = $style_dir."/config/".$filename;
254 if (stat($filename)) {
255 open(FD, $filename) or die "Cannot open $filename\n";
258 s/DOCTYPE (.*) SYSTEM\s+"/DOCTYPE $1 SYSTEM "$myownurl\/getstyleconf?uri=/g;
264 die "Could not find Style Configuration File!";
266 } elsif ($http_method eq 'GET' and $http_path eq "/getxslt") {
267 my $filename = $inputuri;
268 $filename = $style_dir."/".$filename;
269 print "XSLT: $inputuri ==> ($filename)\n";
270 if (stat($filename)) {
271 print "Using local copy\n";
272 open(FD, $filename) or die "Cannot open $filename\n";
275 # Vary bad heuristic for substituion of absolute URLs
277 s/xsl:import\s+href="/xsl:import href="$myownurl\/getxslt?uri=/g ;
278 s/xsl:include\s+href="/xsl:include href="$myownurl\/getxslt?uri=/g ;
284 die "Could not find XSLT!";
286 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
287 my $quoted_html_link = $html_link;
288 $quoted_html_link =~ s/&/&/g;
289 $quoted_html_link =~ s/</</g;
290 $quoted_html_link =~ s/>/>/g;
291 $quoted_html_link =~ s/'/'/g;
292 $quoted_html_link =~ s/"/"/g;
293 print "\nConfiguration requested, returned #$quoted_html_link#\n";
294 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
296 } elsif ($http_method eq 'GET' and $http_path eq "/update") {
297 # rebuild urls_of_uris.db
298 print "Update requested...\n";
300 kill(USR1,getppid()); # signal changes to parent
302 answer($c,"<html><body><h1>Update done</h1></body></html>");
303 } elsif ($http_method eq 'GET' and $http_path eq "/ls") {
304 # send back keys that begin with a given uri
305 my $baseuri = $http_query;
306 $baseuri =~ s/^.*baseuri=(.*)&.*$/$1/;
307 chop $baseuri if ($baseuri =~ /.*\/$/); # remove trailing "/"
308 my $outype = $http_query; # output type, might be 'txt' or 'xml'
309 $outype =~ s/^.*&type=(.*)$/$1/;
310 if (($outype ne 'txt') and ($outype ne 'xml')) { # invalid out type
311 print "Invalid output type specified: $outype\n";
312 answer($c,"<html><body><h1>Invalid output type, may be ".
313 "\"txt\" or \"xml\"</h1></body></html>");
314 } else { # valid output type
315 print "BASEURI $baseuri, TYPE $outype\n";
318 $cont .= "<urilist>\n" if ($outype eq "xml");
319 foreach (keys(%map)) { # search for uri that begin with $baseuri
320 if ($_ =~ /^$baseuri\//) {
321 $cont .= "<uri>" if ($outype eq "xml");
323 $cont .= "\n" if ($outype eq "txt");
324 $cont .= "</uri>\n" if ($outype eq "xml");
327 $cont .= "</urilist>" if ($outype eq "xml");
330 } elsif ($http_method eq 'GET' and $http_path eq "/version") {
331 print "Version requested!";
332 answer($c,"<html><body><h1>HTTP Getter Version ".
333 $VERSION."</h1></body></html>");
336 print "INVALID REQUEST!!!!!\n";
337 print "(PATH: ",$http_path,", ";
338 print "QUERY: ",$http_query,")\n";
339 $c->send_error(RC_FORBIDDEN)
341 print "\nRequest solved: ".$r->url."\n\n";
345 print "\nCONNECTION CLOSED\n\n";
350 #================================
353 #CSC: Too much powerful: creates even /home, /home/users/, ...
354 #CSC: Does not raise errors if could not create dirs/files
358 my @dirs = split /\//,$pathname;
360 foreach $dir (@dirs) {
361 $tmp = ((defined($tmp)) ? $tmp."\/".$dir : "");
369 my ($str,$uri,$url,$filename) = @_;
370 print $str."uri: $uri\n";
371 print $str."url: $url\n";
372 print $str."filename: $filename\n\n";
381 sub gunzip { # gunzip a file and return the deflated content
384 my ($gz, $buffer, $cont);
386 print "deflating $filename ...\n";
387 $gz = gzopen($filename, "r")
388 or die "Cannot open gzip'ed file $filename: $gzerrno";
390 while ( $gz->gzread($buffer) > 0 ) {
393 die "Error while reading : $gzerrno\n" if $gzerrno != Z_STREAM_END ;
399 sub gzip { # gzip the content argument and save it to filename argument
400 my ($cont, $filename) = @_;
404 $gz = gzopen($filename, "w")
405 or die "Cannot gzopen for writing file $filename: $gzerrno";
406 $gz->gzwrite($cont) or die "error writing: $gzerrno\n" ;
412 my ($remove_headers,$str,$url,$filename) = @_;
415 my $resourcetype; # retrieve mode: "normal" (.xml) or "gzipped" (.xml.gz)
416 if ($filename =~ /\.xml$/) { # set retrieve mode
417 $resourcetype = "normal";
418 } elsif ($filename =~ /\.xml\.gz$/) {
419 $resourcetype = "gzipped";
421 die "Unsupported download extension, might be '.gz' or '.xml'\n";
423 my $basefname = $filename;
424 $basefname =~ s/\.gz$//; # get base resource name removing trailing .gz
425 $cont = ""; # modified by side-effect by the callback function
428 if (stat($basefname)) {
429 $localfname=$basefname;
430 } elsif (stat($basefname.".gz")) {
431 $localfname=$basefname.".gz";
433 if ($localfname ne "") { # we already have local copy of requested file
434 # check both possible cache entry: gzipped or normal
435 print "Using local copy for the $str file\n";
436 if ($localfname =~ /\.xml\.gz$/) { # deflating cached file and return it
437 $cont = gunzip($localfname);
438 } elsif ($localfname =~ /\.xml$/) { # just return cached file
439 open(FD, $localfname) or die "Cannot open $localfname";
440 while(<FD>) { $cont .= $_; }
443 die "Internal error: unexpected file name $localfname,"
444 ."must end with '.gz' or '.xml.gz'\n";
446 } else { # download file from net
447 print "Downloading the $str file\n"; # download file
448 my $ua = LWP::UserAgent->new;
449 my $request = HTTP::Request->new(GET => "$url");
450 my $response = $ua->request($request, \&callback);
452 # cache retrieved file to disk
453 # <ZACK/> TODO: inefficent, I haven't yet undestood how to deflate
454 # in memory gzipped file, without call "gzopen"
455 print "Storing the $str file\n";
457 open(FD, ">".$filename.".tmp") or die "Cannot open $filename.tmp\n";
461 # handle cache conversion normal->gzipped or gzipped->normal as user choice
462 if (($cachemode eq 'normal') and ($resourcetype eq 'normal')) {
463 # cache the file as is
464 rename "$filename.tmp", $filename;
465 } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'gzipped')) {
466 # cache the file as is
467 # and update the $cont variabile with deflated content
468 rename "$filename.tmp", $filename;
469 $cont = gunzip($filename);
470 } elsif (($cachemode eq 'normal') and ($resourcetype eq 'gzipped')) {
471 # deflate cache entry
473 open(FD, "> $basefname") or die "cannot open $basefname\n";
474 $cont = gunzip($filename.".tmp");
477 unlink "$filename.tmp"; # delete old gzipped file
478 } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'normal')) {
479 # compress cache entry
480 gzip($cont, $basefname.".gz");
481 unlink "$filename.tmp"; # delete old uncompressed file
483 die "Internal error, unsopported cachemode, resourcetype couple\n";
485 # $cont now contained uncompressed data
488 if ($remove_headers) {
489 $cont =~ s/<\?xml [^?]*\?>//sg;
490 $cont =~ s/<!DOCTYPE [^>]*>//sg;
492 $cont =~ s/DOCTYPE (.*) SYSTEM\s+"http:\/\/www.cs.unibo.it\/helm\/dtd\//DOCTYPE $1 SYSTEM "$myownurl\/getdtd?uri=/g;
500 my $res = new HTTP::Response;
501 $res->content($cont);
502 $c->send_response($res);
506 #retrieve a file from an url and write it to a temp dir
507 #used for retrieve resource index from servers
509 my ($prefix, $URL) = @_;
510 my $ua = LWP::UserAgent->new;
511 my $request = HTTP::Request->new(GET => "$URL");
512 my $response = $ua->request($request, \&callback);
513 my ($filename) = reverse (split "/", $URL); # get filename part of the URL
514 open (TEMP, "> $prefix/$filename")
515 || die "Cannot open temporary file: $prefix/$filename\n";
522 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
525 sub mk_urls_of_uris {
526 #rebuild $uris_dbm.db fetching resource indexes from servers
528 $server, $idxfile, $uri, $url, $comp, $line,
534 if (stat $uris_dbm.".db") { # remove old db file
535 unlink($uris_dbm.".db") or
536 die "cannot unlink old db file: $uris_dbm.db\n";
538 tie(%urls_of_uris, 'DB_File', $uris_dbm.".db", O_RDWR|O_CREAT, 0664);
540 open (SRVS, "< $servers_file") or
541 die "cannot open servers file: $servers_file\n";
544 while ($server = pop @servers) { #cicle on servers in reverse order
545 print "processing server: $server ...\n";
547 helm_wget($tmp_dir, $server."/".$indexname); #get index
548 $idxfile = $tmp_dir."/".$indexname;
549 open (INDEX, "< $idxfile") or
550 die "cannot open temporary index file: $idxfile\n";
551 while ($line = <INDEX>) { #parse index and add entry to urls_of_uris
553 ($uri,$comp) = split /[ \t]+/, $line;
556 $url = $uri . ".xml" . ".gz";
558 $url = $uri . ".xml";
560 $url =~ s/cic:/$server/;
561 $url =~ s/theory:/$server/;
562 $urls_of_uris{$uri} = $url;
565 die "cannot unlink temporary file: $idxfile\n"
566 if (unlink $idxfile) != 1;
569 untie(%urls_of_uris);
570 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);