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 $styles_dir = $ENV{"HELM_STYLE_DIR"} if (defined ($ENV{"HELM_STYLE_DIR"}));
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 'normal' or 'gzipped'\n";
47 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
48 require $HELM_LIB_PATH;
57 #CSC: mancano i controlli sulle condizioni di errore di molte funzioni
58 #CSC: ==> non e' robusto
59 #CSC: altra roba da sistemare segnata con CSC
61 my $d = new HTTP::Daemon LocalPort => 8081;
62 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
63 print "Please contact me at: <URL:", $d->url, ">\n";
64 print "helm_dir: $helm_dir\n";
65 print "dtd_dir: $dtd_dir\n";
66 print "urls_of_uris.db: $uris_dbm.db\n";
67 print "cache mode: $cachemode\n";
69 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
70 $SIG{USR1} = \&update; # sent by the child to make the parent update
71 while (my $c = $d->accept) {
73 while (my $r = $c->get_request) {
74 #CSC: mancano i controlli di sicurezza
76 my $inputuri = $r->url;
77 $inputuri =~ s/^[^?]*\?uri=(.*)/$1/;
78 print "\nRequest: ".$r->url."\n\n";
79 my $http_method = $r->method;
80 my $http_path = $r->url->path;
82 if ($http_method eq 'GET' and $http_path eq "/getciconly") {
83 # finds the uri, url and filename
84 my $cicuri = $inputuri;
86 my $cicfilename = $cicuri;
87 $cicfilename =~ s/cic:(.*)/$1/;
88 $cicfilename =~ s/theory:(.*)/$1/;
89 # $cicfilename = $helm_dir.$cicfilename.".xml";
91 my $cicurl = $map{$cicuri};
93 if ($cicurl =~ /\.xml$/) { # non gzipped file
95 } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
96 $extension = ".xml.gz";
97 } else { # error: unknown extension
98 die "unexpected extension in url: $cicurl, might be '.xml' or '.xml.gz'";
100 $cicfilename = $helm_dir.$cicfilename.$extension;
102 #my $cicurl = $map{$cicuri};
104 if (!defined($cicurl)) {
105 print "\nNOT FOUND!!!!!\n";
106 $c->send_error(RC_NOT_FOUND)
108 print_request("cic",$cicuri,$cicurl,$cicfilename);
111 my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
113 # Answering the client
114 answer($c,$ciccontent);
116 } elsif ($http_method eq 'GET' and $http_path eq "/get") {
117 # finds the uris, urls and filenames
118 my $cicuri = $inputuri,
119 $typesuri = $inputuri,
122 if ($inputuri =~ /\.types$/) {
123 $cicuri =~ s/(.*)\.types$/$1/;
125 } elsif ($inputuri =~ /\.types\.ann$/) {
126 $cicuri =~ s/(.*)\.types\.ann$/$1/;
127 $typesuri =~ s/(.*)\.ann$/$1/;
128 $annsuffix = ".types.ann";
129 } elsif ($inputuri =~ /\.ann$/) {
130 $cicuri =~ s/(.*)\.ann$/$1/;
138 my $cicfilename = $cicuri;
139 $cicfilename =~ s/cic:(.*)/$1/;
140 $cicfilename =~ s/theory:(.*)/$1/;
141 $cicfilename = $helm_dir.$cicfilename;
143 # my $typesfilename = $cicfilename.".types.xml" if $typesuri;
144 # my $annfilename = $cicfilename.$annsuffix.".xml" if $annuri;
145 # $cicfilename .= ".xml";
148 my $cicurl = $map{$cicuri};
149 my $typesurl = $map{$typesuri} if (defined($typesuri));
150 my $annurl = $map{$annuri} if (defined($annuri));
151 my ($cicext, $typesext, $annext);
152 if ($cicurl =~ /\.xml$/) { # normal file
154 } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
157 die "unexpected extension in url: $cicurl; might be '.xml' or '.xml.gz'";
159 if (defined($typesuri)) { # extension selection for types file
160 if ($typesurl =~ /\.xml$/) { # normal file
161 $typesext = ".types.xml";
162 } elsif ($typesurl =~ /\.xml\.gz$/) { # gzipped file
163 $typesext = ".types.xml.gz";
165 die "unexpected extension in url: $typesurl; might be '.xml' or '.xml.gz'";
168 if (defined($annuri)) { # extension selection for annotation file
169 if ($annurl =~ /\.xml$/) { # normal file
171 } elsif ($annurl =~ /\.xml\.gz$/) { # gzipped file
174 die "unexpected extension in url: $annurl might be '.xml' or '.xml.gz'";
177 my $typesfilename = $cicfilename.$typesext if $typesuri;
178 my $annfilename = $cicfilename.$annsuffix.$annext if $annuri;
179 $cicfilename .= $cicext;
183 if (!defined($cicurl) ||
184 (!defined($typesurl) && $typesuri) ||
185 (!defined($annuri) && $annuri))
187 print "\nNOT FOUND!!!!!\n";
188 $c->send_error(RC_NOT_FOUND)
190 print_request("cic",$cicuri,$cicurl,$cicfilename);
191 print_request("types",$typesuri,$typesurl,$typesfilename)
193 print_request("ann",$annuri,$annurl,$annfilename)
196 # Retrieves the files
198 my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
200 download(1,"types",$typesurl,$typesfilename) if ($typesuri);
202 download(1,"ann",$annurl,$annfilename) if ($annuri);
204 # Merging the files together
207 <?xml version="1.0" encoding="UTF-8"?>
208 <cicxml uri="$cicuri">
215 # Answering the client
218 } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
219 my $filename = $inputuri;
220 $filename = $dtd_dir."/".$filename;
221 print "DTD: $inputuri ==> ($filename)\n";
222 if (stat($filename)) {
223 print "Using local copy\n";
224 open(FD, $filename) or die "Cannot open $filename\n";
226 while(<FD>) { $cont .= $_; }
230 die "Could not find DTD!";
232 } elsif ($http_method eq 'GET' and $http_path eq "/getxslt") {
233 my $filename = $inputuri;
234 $filename = $styles_dir."/".$filename;
235 print "XSLT: $inputuri ==> ($filename)\n";
236 if (stat($filename)) {
237 print "Using local copy\n";
238 open(FD, $filename) or die "Cannot open $filename\n";
240 while(<FD>) { $cont .= $_; }
244 die "Could not find XSLT!";
246 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
247 my $quoted_html_link = $html_link;
248 $quoted_html_link =~ s/&/&/g;
249 $quoted_html_link =~ s/</</g;
250 $quoted_html_link =~ s/>/>/g;
251 $quoted_html_link =~ s/'/'/g;
252 $quoted_html_link =~ s/"/"/g;
253 print "\nConfiguration requested, returned #$quoted_html_link#\n";
254 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
256 } elsif ($http_method eq 'GET' and $http_path eq "/update") {
257 print "Update requested...";
259 kill(USR1,getppid());
261 answer($c,"<html><body><h1>Update done</h1></body></html>");
263 print "\nINVALID REQUEST!!!!!\n";
264 $c->send_error(RC_FORBIDDEN)
266 print "\nRequest solved: ".$r->url."\n\n";
270 print "\nCONNECTION CLOSED\n\n";
275 #================================
278 #CSC: Too much powerful: creates even /home, /home/users/, ...
279 #CSC: Does not raise errors if could not create dirs/files
283 my @dirs = split /\//,$pathname;
285 foreach $dir (@dirs) {
286 $tmp = ((defined($tmp)) ? $tmp."\/".$dir : "");
294 my ($str,$uri,$url,$filename) = @_;
295 print $str."uri: $uri\n";
296 print $str."url: $url\n";
297 print $str."filename: $filename\n\n";
306 sub gunzip { # gunzip a file and return the deflated content
309 my ($gz, $buffer, $cont);
311 print "deflating $filename ...\n";
312 $gz = gzopen($filename, "r") or die "Cannot open gzip'ed file $filename: $gzerrno";
314 while ( $gz->gzread($buffer) > 0 ) {
317 die "Error while reading : $gzerrno\n" if $gzerrno != Z_STREAM_END ;
323 sub gzip { # gzip the content argument and save it to filename argument
324 my ($cont, $filename) = @_;
328 $gz = gzopen($filename, "w") or die "Cannot gzopen for writing file $filename: $gzerrno";
329 $gz->gzwrite($cont) or die "error writing: $gzerrno\n" ;
335 my ($remove_headers,$str,$url,$filename) = @_;
339 my $resourcetype; # retrieve mode: "normal" (.xml) or "gzipped" (.xml.gz)
340 if ($filename =~ /\.xml$/) { # set retrieve mode
341 $resourcetype = "normal";
342 } elsif ($filename =~ /\.xml\.gz$/) {
343 $resourcetype = "gzipped";
345 die "Unsupported download extension, might be '.gz' or '.xml'\n";
347 my $basefname = $filename;
348 $basefname =~ s/\.gz$//; # get base resource name removing trailing .gz
350 $cont = ""; # modified by side-effect by the callback function
353 if (stat($basefname)) {
354 $localfname=$basefname;
355 } elsif (stat($basefname.".gz")) {
356 $localfname=$basefname.".gz";
358 if ($localfname ne "") { # we already have local copy of requested file
359 # check both possible cache entry: gzipped or normal
360 print "Using local copy for the $str file\n";
362 if ($localfname =~ /\.xml\.gz$/) { # deflating cached file and return it
363 $cont = gunzip($localfname);
364 } elsif ($localfname =~ /\.xml$/) { # just return cached file
365 open(FD, $localfname) or die "Cannot open $localfname";
366 while(<FD>) { $cont .= $_; }
369 die "Internal error: unexpected file name $localfname, must end with '.gz' or '.xml.gz'\n";
372 } else { # download file from net
373 print "Downloading the $str file\n"; # download file
374 $ua = LWP::UserAgent->new;
375 $request = HTTP::Request->new(GET => "$url");
376 $response = $ua->request($request, \&callback);
378 # cache retrieved file to disk
379 # <ZACK/> TODO: inefficent, I haven't yet undestood how to deflate in memory gzipped file,
380 # without call "gzopen"
382 print "Storing the $str file\n";
384 open(FD, ">".$filename.".tmp") or die "Cannot open $filename.tmp\n";
388 # handle cache conversion normal->gzipped or gzipped->normal as user choice
389 if (($cachemode eq 'normal') and ($resourcetype eq 'normal')) { # cache the file as is
390 rename "$filename.tmp", $filename;
391 } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'gzipped')) { # cache the file as is
392 # and update the $cont variabile with deflated content
393 rename "$filename.tmp", $filename;
394 $cont = gunzip($filename);
395 } elsif (($cachemode eq 'normal') and ($resourcetype eq 'gzipped')) { # deflate cache entry
397 open(FD, "> $basefname") or die "cannot open $basefname\n";
398 $cont = gunzip($filename.".tmp");
401 unlink "$filename.tmp"; # delete old gzipped file
402 } elsif (($cachemode eq 'gzipped') and ($resourcetype eq 'normal')) { # compress cache entry
403 gzip($cont, $basefname.".gz");
404 unlink "$filename.tmp"; # delete old uncompressed file
406 die "Internal error, unsopported cachemode, resourcetype couple\n";
408 # $cont now contained uncompressed data
412 if ($remove_headers) {
413 $cont =~ s/<\?xml [^?]*\?>//sg;
414 $cont =~ s/<!DOCTYPE [^>]*>//sg;
422 my $res = new HTTP::Response;
423 $res->content($cont);
424 $c->send_response($res);
429 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);