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 # First of all, let's load HELM configuration
28 my $HELM_LIB_DIR = $ENV{"HELM_LIB_DIR"};
29 # this should be the only fixed constant
30 my $DEFAULT_HELM_LIB_DIR = "@HELM_LIB_DIR@";
31 if (defined ($HELM_LIB_DIR)) {
32 $HELM_LIB_PATH = $HELM_LIB_DIR."/configuration.pl";
34 $HELM_LIB_PATH = $DEFAULT_HELM_LIB_DIR."/configuration.pl";
37 # Let's override the configuration file
38 $styles_dir = $ENV{"HELM_STYLE_DIR"} if (defined ($ENV{"HELM_STYLE_DIR"}));
39 $dtd_dir = $ENV{"HELM_DTD_DIR"} if (defined ($ENV{"HELM_DTD_DIR"}));
41 # <ZACK>: TODO temporary, move this setting to configuration file
42 # set the cache mode, may be "gzipped" or "normal"
43 my $cachemode = $ENV{'HTTP_GETTER_CACHE_MODE'} || 'gzipped';
44 if (($cachemode ne 'gzipped') and ($cachemode ne 'normal')) {
45 die "Invalid HTTP_GETTER_CACHE_MODE environment variable, must be".
46 "'normal' or 'gzipped'\n";
50 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
51 require $HELM_LIB_PATH;
60 #CSC: mancano i controlli sulle condizioni di errore di molte funzioni
61 #CSC: ==> non e' robusto
62 #CSC: altra roba da sistemare segnata con CSC
64 my $d = new HTTP::Daemon LocalPort => 8081;
65 my $myownurl = $d->url;
67 # Let's patch the returned URL
68 $myownurl =~ s/\/$//; # chop the final slash
69 my $myownport = $myownurl;
70 $myownport =~ s/http:\/\/(.*):(.*)/$2/;
71 $myownurl =~ s/http:\/\/(.*):(.*)/$1/;
72 ($myownurl) = gethostbyname($myownurl);
73 $myownurl = "http://".$myownurl.":".$myownport;
75 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
76 print "Please contact me at: <URL:", $myownurl."/", ">\n";
77 print "helm_dir: $helm_dir\n";
78 print "styles_dir: $styles_dir\n";
79 print "dtd_dir: $dtd_dir\n";
80 print "urls_of_uris.db: $uris_dbm.db\n";
81 print "cache mode: $cachemode\n";
83 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
84 $SIG{USR1} = \&update; # sent by the child to make the parent update
85 while (my $c = $d->accept) {
87 while (my $r = $c->get_request) {
88 #CSC: mancano i controlli di sicurezza
90 my $inputuri = $r->url;
91 $inputuri =~ s/^[^?]*\?uri=(.*)/$1/;
92 print "\nRequest: ".$r->url."\n\n";
93 my $http_method = $r->method;
94 my $http_path = $r->url->path;
96 if ($http_method eq 'GET' and $http_path eq "/getciconly") {
97 # finds the uri, url and filename
98 my $cicuri = $inputuri;
100 my $cicfilename = $cicuri;
101 $cicfilename =~ s/cic:(.*)/$1/;
102 $cicfilename =~ s/theory:(.*)/$1/;
104 my $cicurl = $map{$cicuri};
106 if ($cicurl =~ /\.xml$/) { # non gzipped file
108 } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
109 $extension = ".xml.gz";
110 } else { # error: unknown extension
111 die "unexpected extension in url: $cicurl, might be '.xml'".
114 $cicfilename = $helm_dir.$cicfilename.$extension;
116 if (!defined($cicurl)) {
117 print "\nNOT FOUND!!!!!\n";
118 $c->send_error(RC_NOT_FOUND)
120 print_request("cic",$cicuri,$cicurl,$cicfilename);
123 my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
125 # Answering the client
126 answer($c,$ciccontent);
128 } elsif ($http_method eq 'GET' and $http_path eq "/get") {
129 # finds the uris, urls and filenames
130 my $cicuri = $inputuri,
131 $typesuri = $inputuri,
134 if ($inputuri =~ /\.types$/) {
135 $cicuri =~ s/(.*)\.types$/$1/;
137 } elsif ($inputuri =~ /\.types\.ann$/) {
138 $cicuri =~ s/(.*)\.types\.ann$/$1/;
139 $typesuri =~ s/(.*)\.ann$/$1/;
140 $annsuffix = ".types.ann";
141 } elsif ($inputuri =~ /\.ann$/) {
142 $cicuri =~ s/(.*)\.ann$/$1/;
150 my $cicfilename = $cicuri;
151 $cicfilename =~ s/cic:(.*)/$1/;
152 $cicfilename =~ s/theory:(.*)/$1/;
153 $cicfilename = $helm_dir.$cicfilename;
155 my $cicurl = $map{$cicuri};
156 my $typesurl = $map{$typesuri} if (defined($typesuri));
157 my $annurl = $map{$annuri} if (defined($annuri));
158 my ($cicext, $typesext, $annext);
159 if ($cicurl =~ /\.xml$/) { # normal file
161 } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
164 die "unexpected extension in url: $cicurl;".
165 "might be '.xml' or '.xml.gz'";
167 if (defined($typesuri)) { # extension selection for types file
168 if ($typesurl =~ /\.xml$/) { # normal file
169 $typesext = ".types.xml";
170 } elsif ($typesurl =~ /\.xml\.gz$/) { # gzipped file
171 $typesext = ".types.xml.gz";
173 die "unexpected extension in url: $typesurl;".
174 "might be '.xml' or '.xml.gz'";
177 if (defined($annuri)) { # extension selection for annotation file
178 if ($annurl =~ /\.xml$/) { # normal file
180 } elsif ($annurl =~ /\.xml\.gz$/) { # gzipped file
183 die "unexpected extension in url: $annurl".
184 "might be '.xml' or '.xml.gz'";
187 my $typesfilename = $cicfilename.$typesext if $typesuri;
188 my $annfilename = $cicfilename.$annsuffix.$annext if $annuri;
189 $cicfilename .= $cicext;
191 if (!defined($cicurl) ||
192 (!defined($typesurl) && $typesuri) ||
193 (!defined($annuri) && $annuri))
195 print "\nNOT FOUND!!!!!\n";
196 $c->send_error(RC_NOT_FOUND)
198 print_request("cic",$cicuri,$cicurl,$cicfilename);
199 print_request("types",$typesuri,$typesurl,$typesfilename)
201 print_request("ann",$annuri,$annurl,$annfilename)
204 # Retrieves the files
206 my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
208 download(1,"types",$typesurl,$typesfilename) if ($typesuri);
210 download(1,"ann",$annurl,$annfilename) if ($annuri);
212 # Merging the files together
215 <?xml version="1.0" encoding="UTF-8"?>
216 <cicxml uri="$cicuri">
223 # Answering the client
226 } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
227 my $filename = $inputuri;
228 $filename = $dtd_dir."/".$filename;
229 print "DTD: $inputuri ==> ($filename)\n";
230 if (stat($filename)) {
231 print "Using local copy\n";
232 open(FD, $filename) or die "Cannot open $filename\n";
235 # Vary bad heuristic for substituion of absolute URLs
237 s/ENTITY (.*) SYSTEM\s+"/ENTITY $1 SYSTEM "$myownurl\/getdtd?uri=/g;
243 die "Could not find DTD!";
245 } elsif ($http_method eq 'GET' and $http_path eq "/getxslt") {
246 my $filename = $inputuri;
247 $filename = $styles_dir."/".$filename;
248 print "XSLT: $inputuri ==> ($filename)\n";
249 if (stat($filename)) {
250 print "Using local copy\n";
251 open(FD, $filename) or die "Cannot open $filename\n";
254 # Vary bad heuristic for substituion of absolute URLs
256 s/xsl:import\s+href="/xsl:import href="$myownurl\/getxslt?uri=/g ;
257 s/xsl:include\s+href="/xsl:include href="$myownurl\/getxslt?uri=/g ;
263 die "Could not find XSLT!";
265 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
266 my $quoted_html_link = $html_link;
267 $quoted_html_link =~ s/&/&/g;
268 $quoted_html_link =~ s/</</g;
269 $quoted_html_link =~ s/>/>/g;
270 $quoted_html_link =~ s/'/'/g;
271 $quoted_html_link =~ s/"/"/g;
272 print "\nConfiguration requested, returned #$quoted_html_link#\n";
273 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
275 } elsif ($http_method eq 'GET' and $http_path eq "/update") {
276 print "Update requested...";
278 kill(USR1,getppid());
280 answer($c,"<html><body><h1>Update done</h1></body></html>");
282 print "\nINVALID REQUEST!!!!!\n";
283 $c->send_error(RC_FORBIDDEN)
285 print "\nRequest solved: ".$r->url."\n\n";
289 print "\nCONNECTION CLOSED\n\n";
294 #================================
297 #CSC: Too much powerful: creates even /home, /home/users/, ...
298 #CSC: Does not raise errors if could not create dirs/files
302 my @dirs = split /\//,$pathname;
304 foreach $dir (@dirs) {
305 $tmp = ((defined($tmp)) ? $tmp."\/".$dir : "");
313 my ($str,$uri,$url,$filename) = @_;
314 print $str."uri: $uri\n";
315 print $str."url: $url\n";
316 print $str."filename: $filename\n\n";
325 sub gunzip { # gunzip a file and return the deflated content
328 my ($gz, $buffer, $cont);
330 print "deflating $filename ...\n";
331 $gz = gzopen($filename, "r")
332 or die "Cannot open gzip'ed file $filename: $gzerrno";
334 while ( $gz->gzread($buffer) > 0 ) {
337 die "Error while reading : $gzerrno\n" if $gzerrno != Z_STREAM_END ;
343 sub gzip { # gzip the content argument and save it to filename argument
344 my ($cont, $filename) = @_;
348 $gz = gzopen($filename, "w")
349 or die "Cannot gzopen for writing file $filename: $gzerrno";
350 $gz->gzwrite($cont) or die "error writing: $gzerrno\n" ;
356 my ($remove_headers,$str,$url,$filename) = @_;
359 my $resourcetype; # retrieve mode: "normal" (.xml) or "gzipped" (.xml.gz)
360 if ($filename =~ /\.xml$/) { # set retrieve mode
361 $resourcetype = "normal";
362 } elsif ($filename =~ /\.xml\.gz$/) {
363 $resourcetype = "gzipped";
365 die "Unsupported download extension, might be '.gz' or '.xml'\n";
367 my $basefname = $filename;
368 $basefname =~ s/\.gz$//; # get base resource name removing trailing .gz
369 $cont = ""; # modified by side-effect by the callback function
372 if (stat($basefname)) {
373 $localfname=$basefname;
374 } elsif (stat($basefname.".gz")) {
375 $localfname=$basefname.".gz";
377 if ($localfname ne "") { # we already have local copy of requested file
378 # check both possible cache entry: gzipped or normal
379 print "Using local copy for the $str file\n";
380 if ($localfname =~ /\.xml\.gz$/) { # deflating cached file and return it
381 $cont = gunzip($localfname);
382 } elsif ($localfname =~ /\.xml$/) { # just return cached file
383 open(FD, $localfname) or die "Cannot open $localfname";
384 while(<FD>) { $cont .= $_; }
387 die "Internal error: unexpected file name $localfname,"
388 ."must end with '.gz' or '.xml.gz'\n";
390 } else { # download file from net
391 print "Downloading the $str file\n"; # download file
392 $ua = LWP::UserAgent->new;
393 $request = HTTP::Request->new(GET => "$url");
394 $response = $ua->request($request, \&callback);
396 # cache retrieved file to disk
397 # <ZACK/> TODO: inefficent, I haven't yet undestood how to deflate
398 # in memory gzipped file, without call "gzopen"
399 print "Storing the $str file\n";
401 open(FD, ">".$filename.".tmp") or die "Cannot open $filename.tmp\n";
405 # handle cache conversion normal->gzipped or gzipped->normal as user choice
406 if (($cachemode eq 'normal') and ($resourcetype eq 'normal')) {
407 # cache the file as is
408 rename "$filename.tmp", $filename;
409 } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'gzipped')) {
410 # cache the file as is
411 # and update the $cont variabile with deflated content
412 rename "$filename.tmp", $filename;
413 $cont = gunzip($filename);
414 } elsif (($cachemode eq 'normal') and ($resourcetype eq 'gzipped')) {
415 # deflate cache entry
417 open(FD, "> $basefname") or die "cannot open $basefname\n";
418 $cont = gunzip($filename.".tmp");
421 unlink "$filename.tmp"; # delete old gzipped file
422 } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'normal')) {
423 # compress cache entry
424 gzip($cont, $basefname.".gz");
425 unlink "$filename.tmp"; # delete old uncompressed file
427 die "Internal error, unsopported cachemode, resourcetype couple\n";
429 # $cont now contained uncompressed data
432 if ($remove_headers) {
433 $cont =~ s/<\?xml [^?]*\?>//sg;
434 $cont =~ s/<!DOCTYPE [^>]*>//sg;
442 my $res = new HTTP::Response;
443 $res->content($cont);
444 $c->send_response($res);
449 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);