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 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
66 print "Please contact me at: <URL:", $d->url, ">\n";
67 print "helm_dir: $helm_dir\n";
68 print "dtd_dir: $dtd_dir\n";
69 print "urls_of_uris.db: $uris_dbm.db\n";
70 print "cache mode: $cachemode\n";
72 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
73 $SIG{USR1} = \&update; # sent by the child to make the parent update
74 while (my $c = $d->accept) {
76 while (my $r = $c->get_request) {
77 #CSC: mancano i controlli di sicurezza
79 my $inputuri = $r->url;
80 $inputuri =~ s/^[^?]*\?uri=(.*)/$1/;
81 print "\nRequest: ".$r->url."\n\n";
82 my $http_method = $r->method;
83 my $http_path = $r->url->path;
85 if ($http_method eq 'GET' and $http_path eq "/getciconly") {
86 # finds the uri, url and filename
87 my $cicuri = $inputuri;
89 my $cicfilename = $cicuri;
90 $cicfilename =~ s/cic:(.*)/$1/;
91 $cicfilename =~ s/theory:(.*)/$1/;
93 my $cicurl = $map{$cicuri};
95 if ($cicurl =~ /\.xml$/) { # non gzipped file
97 } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
98 $extension = ".xml.gz";
99 } else { # error: unknown extension
100 die "unexpected extension in url: $cicurl, might be '.xml'".
103 $cicfilename = $helm_dir.$cicfilename.$extension;
105 if (!defined($cicurl)) {
106 print "\nNOT FOUND!!!!!\n";
107 $c->send_error(RC_NOT_FOUND)
109 print_request("cic",$cicuri,$cicurl,$cicfilename);
112 my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
114 # Answering the client
115 answer($c,$ciccontent);
117 } elsif ($http_method eq 'GET' and $http_path eq "/get") {
118 # finds the uris, urls and filenames
119 my $cicuri = $inputuri,
120 $typesuri = $inputuri,
123 if ($inputuri =~ /\.types$/) {
124 $cicuri =~ s/(.*)\.types$/$1/;
126 } elsif ($inputuri =~ /\.types\.ann$/) {
127 $cicuri =~ s/(.*)\.types\.ann$/$1/;
128 $typesuri =~ s/(.*)\.ann$/$1/;
129 $annsuffix = ".types.ann";
130 } elsif ($inputuri =~ /\.ann$/) {
131 $cicuri =~ s/(.*)\.ann$/$1/;
139 my $cicfilename = $cicuri;
140 $cicfilename =~ s/cic:(.*)/$1/;
141 $cicfilename =~ s/theory:(.*)/$1/;
142 $cicfilename = $helm_dir.$cicfilename;
144 my $cicurl = $map{$cicuri};
145 my $typesurl = $map{$typesuri} if (defined($typesuri));
146 my $annurl = $map{$annuri} if (defined($annuri));
147 my ($cicext, $typesext, $annext);
148 if ($cicurl =~ /\.xml$/) { # normal file
150 } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
153 die "unexpected extension in url: $cicurl;".
154 "might be '.xml' or '.xml.gz'";
156 if (defined($typesuri)) { # extension selection for types file
157 if ($typesurl =~ /\.xml$/) { # normal file
158 $typesext = ".types.xml";
159 } elsif ($typesurl =~ /\.xml\.gz$/) { # gzipped file
160 $typesext = ".types.xml.gz";
162 die "unexpected extension in url: $typesurl;".
163 "might be '.xml' or '.xml.gz'";
166 if (defined($annuri)) { # extension selection for annotation file
167 if ($annurl =~ /\.xml$/) { # normal file
169 } elsif ($annurl =~ /\.xml\.gz$/) { # gzipped file
172 die "unexpected extension in url: $annurl".
173 "might be '.xml' or '.xml.gz'";
176 my $typesfilename = $cicfilename.$typesext if $typesuri;
177 my $annfilename = $cicfilename.$annsuffix.$annext if $annuri;
178 $cicfilename .= $cicext;
180 if (!defined($cicurl) ||
181 (!defined($typesurl) && $typesuri) ||
182 (!defined($annuri) && $annuri))
184 print "\nNOT FOUND!!!!!\n";
185 $c->send_error(RC_NOT_FOUND)
187 print_request("cic",$cicuri,$cicurl,$cicfilename);
188 print_request("types",$typesuri,$typesurl,$typesfilename)
190 print_request("ann",$annuri,$annurl,$annfilename)
193 # Retrieves the files
195 my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
197 download(1,"types",$typesurl,$typesfilename) if ($typesuri);
199 download(1,"ann",$annurl,$annfilename) if ($annuri);
201 # Merging the files together
204 <?xml version="1.0" encoding="UTF-8"?>
205 <cicxml uri="$cicuri">
212 # Answering the client
215 } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
216 my $filename = $inputuri;
217 $filename = $dtd_dir."/".$filename;
218 print "DTD: $inputuri ==> ($filename)\n";
219 if (stat($filename)) {
220 print "Using local copy\n";
221 open(FD, $filename) or die "Cannot open $filename\n";
223 while(<FD>) { $cont .= $_; }
227 die "Could not find DTD!";
229 } elsif ($http_method eq 'GET' and $http_path eq "/getxslt") {
230 my $filename = $inputuri;
231 $filename = $styles_dir."/".$filename;
232 print "XSLT: $inputuri ==> ($filename)\n";
233 if (stat($filename)) {
234 print "Using local copy\n";
235 open(FD, $filename) or die "Cannot open $filename\n";
237 while(<FD>) { $cont .= $_; }
241 die "Could not find XSLT!";
243 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
244 my $quoted_html_link = $html_link;
245 $quoted_html_link =~ s/&/&/g;
246 $quoted_html_link =~ s/</</g;
247 $quoted_html_link =~ s/>/>/g;
248 $quoted_html_link =~ s/'/'/g;
249 $quoted_html_link =~ s/"/"/g;
250 print "\nConfiguration requested, returned #$quoted_html_link#\n";
251 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
253 } elsif ($http_method eq 'GET' and $http_path eq "/update") {
254 print "Update requested...";
256 kill(USR1,getppid());
258 answer($c,"<html><body><h1>Update done</h1></body></html>");
260 print "\nINVALID REQUEST!!!!!\n";
261 $c->send_error(RC_FORBIDDEN)
263 print "\nRequest solved: ".$r->url."\n\n";
267 print "\nCONNECTION CLOSED\n\n";
272 #================================
275 #CSC: Too much powerful: creates even /home, /home/users/, ...
276 #CSC: Does not raise errors if could not create dirs/files
280 my @dirs = split /\//,$pathname;
282 foreach $dir (@dirs) {
283 $tmp = ((defined($tmp)) ? $tmp."\/".$dir : "");
291 my ($str,$uri,$url,$filename) = @_;
292 print $str."uri: $uri\n";
293 print $str."url: $url\n";
294 print $str."filename: $filename\n\n";
303 sub gunzip { # gunzip a file and return the deflated content
306 my ($gz, $buffer, $cont);
308 print "deflating $filename ...\n";
309 $gz = gzopen($filename, "r")
310 or die "Cannot open gzip'ed file $filename: $gzerrno";
312 while ( $gz->gzread($buffer) > 0 ) {
315 die "Error while reading : $gzerrno\n" if $gzerrno != Z_STREAM_END ;
321 sub gzip { # gzip the content argument and save it to filename argument
322 my ($cont, $filename) = @_;
326 $gz = gzopen($filename, "w")
327 or die "Cannot gzopen for writing file $filename: $gzerrno";
328 $gz->gzwrite($cont) or die "error writing: $gzerrno\n" ;
334 my ($remove_headers,$str,$url,$filename) = @_;
337 my $resourcetype; # retrieve mode: "normal" (.xml) or "gzipped" (.xml.gz)
338 if ($filename =~ /\.xml$/) { # set retrieve mode
339 $resourcetype = "normal";
340 } elsif ($filename =~ /\.xml\.gz$/) {
341 $resourcetype = "gzipped";
343 die "Unsupported download extension, might be '.gz' or '.xml'\n";
345 my $basefname = $filename;
346 $basefname =~ s/\.gz$//; # get base resource name removing trailing .gz
347 $cont = ""; # modified by side-effect by the callback function
350 if (stat($basefname)) {
351 $localfname=$basefname;
352 } elsif (stat($basefname.".gz")) {
353 $localfname=$basefname.".gz";
355 if ($localfname ne "") { # we already have local copy of requested file
356 # check both possible cache entry: gzipped or normal
357 print "Using local copy for the $str file\n";
358 if ($localfname =~ /\.xml\.gz$/) { # deflating cached file and return it
359 $cont = gunzip($localfname);
360 } elsif ($localfname =~ /\.xml$/) { # just return cached file
361 open(FD, $localfname) or die "Cannot open $localfname";
362 while(<FD>) { $cont .= $_; }
365 die "Internal error: unexpected file name $localfname,"
366 ."must end with '.gz' or '.xml.gz'\n";
368 } else { # download file from net
369 print "Downloading the $str file\n"; # download file
370 $ua = LWP::UserAgent->new;
371 $request = HTTP::Request->new(GET => "$url");
372 $response = $ua->request($request, \&callback);
374 # cache retrieved file to disk
375 # <ZACK/> TODO: inefficent, I haven't yet undestood how to deflate
376 # in memory gzipped file, without call "gzopen"
377 print "Storing the $str file\n";
379 open(FD, ">".$filename.".tmp") or die "Cannot open $filename.tmp\n";
383 # handle cache conversion normal->gzipped or gzipped->normal as user choice
384 if (($cachemode eq 'normal') and ($resourcetype eq 'normal')) {
385 # cache the file as is
386 rename "$filename.tmp", $filename;
387 } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'gzipped')) {
388 # cache the file as is
389 # and update the $cont variabile with deflated content
390 rename "$filename.tmp", $filename;
391 $cont = gunzip($filename);
392 } elsif (($cachemode eq 'normal') and ($resourcetype eq 'gzipped')) {
393 # deflate cache entry
395 open(FD, "> $basefname") or die "cannot open $basefname\n";
396 $cont = gunzip($filename.".tmp");
399 unlink "$filename.tmp"; # delete old gzipped file
400 } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'normal')) {
401 # compress cache entry
402 gzip($cont, $basefname.".gz");
403 unlink "$filename.tmp"; # delete old uncompressed file
405 die "Internal error, unsopported cachemode, resourcetype couple\n";
407 # $cont now contained uncompressed data
410 if ($remove_headers) {
411 $cont =~ s/<\?xml [^?]*\?>//sg;
412 $cont =~ s/<!DOCTYPE [^>]*>//sg;
420 my $res = new HTTP::Response;
421 $res->content($cont);
422 $c->send_response($res);
427 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);