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 = "@DEFAULT_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";
36 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
37 require $HELM_LIB_PATH;
46 #CSC: mancano i controlli sulle condizioni di errore di molte funzioni
47 #CSC: ==> non e' robusto
48 #CSC: altra roba da sistemare segnata con CSC
50 my $d = new HTTP::Daemon LocalPort => 8081;
51 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
52 print "Please contact me at: <URL:", $d->url, ">\n";
53 print "helm_dir: $helm_dir\n";
54 print "dtd_dir: $dtd_dir\n";
55 print "urls_of_uris.db: $uris_dbm.db\n";
56 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
57 $SIG{USR1} = \&update; # sent by the child to make the parent update
58 while (my $c = $d->accept) {
60 while (my $r = $c->get_request) {
61 #CSC: mancano i controlli di sicurezza
63 my $inputuri = $r->url;
64 $inputuri =~ s/^[^?]*\?uri=(.*)/$1/;
65 print "\nRequest: ".$r->url."\n\n";
66 my $http_method = $r->method;
67 my $http_path = $r->url->path;
69 if ($http_method eq 'GET' and $http_path eq "/getciconly") {
70 # finds the uri, url and filename
71 my $cicuri = $inputuri;
73 my $cicfilename = $cicuri;
74 $cicfilename =~ s/cic:(.*)/$1/;
75 $cicfilename =~ s/theory:(.*)/$1/;
76 # $cicfilename = $helm_dir.$cicfilename.".xml";
78 my $cicurl = $map{$cicuri};
80 if ($cicurl =~ /\.xml$/) { # non gzipped file
82 } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
83 $extension = ".xml.gz";
84 } else { # error: unknown extension
85 die "unexpected extension in url: $cicurl, might be '.xml' or '.xml.gz'";
87 $cicfilename = $helm_dir.$cicfilename.$extension;
89 #my $cicurl = $map{$cicuri};
91 if (!defined($cicurl)) {
92 print "\nNOT FOUND!!!!!\n";
93 $c->send_error(RC_NOT_FOUND)
95 print_request("cic",$cicuri,$cicurl,$cicfilename);
98 my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
100 # Answering the client
101 answer($c,$ciccontent);
103 } elsif ($http_method eq 'GET' and $http_path eq "/get") {
104 # finds the uris, urls and filenames
105 my $cicuri = $inputuri,
106 $typesuri = $inputuri,
109 if ($inputuri =~ /\.types$/) {
110 $cicuri =~ s/(.*)\.types$/$1/;
112 } elsif ($inputuri =~ /\.types\.ann$/) {
113 $cicuri =~ s/(.*)\.types\.ann$/$1/;
114 $typesuri =~ s/(.*)\.ann$/$1/;
115 $annsuffix = ".types.ann";
116 } elsif ($inputuri =~ /\.ann$/) {
117 $cicuri =~ s/(.*)\.ann$/$1/;
125 my $cicfilename = $cicuri;
126 $cicfilename =~ s/cic:(.*)/$1/;
127 $cicfilename =~ s/theory:(.*)/$1/;
128 $cicfilename = $helm_dir.$cicfilename;
130 # my $typesfilename = $cicfilename.".types.xml" if $typesuri;
131 # my $annfilename = $cicfilename.$annsuffix.".xml" if $annuri;
132 # $cicfilename .= ".xml";
135 my $cicurl = $map{$cicuri};
136 my $typesurl = $map{$typesuri} if (defined($typesuri));
137 my $annurl = $map{$annuri} if (defined($annuri));
138 my ($cicext, $typesext, $annext);
139 if ($cicurl =~ /\.xml$/) { # normal file
141 } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
144 die "unexpected extension in url: $cicurl; might be '.xml' or '.xml.gz'";
146 if (defined($typesuri)) { # extension selection for types file
147 if ($typesurl =~ /\.xml$/) { # normal file
148 $typesext = ".types.xml";
149 } elsif ($typesurl =~ /\.xml\.gz$/) { # gzipped file
150 $typesext = ".types.xml.gz";
152 die "unexpected extension in url: $typesurl; might be '.xml' or '.xml.gz'";
155 if (defined($annuri)) { # extension selection for annotation file
156 if ($annurl =~ /\.xml$/) { # normal file
158 } elsif ($annurl =~ /\.xml\.gz$/) { # gzipped file
161 die "unexpected extension in url: $annurl might be '.xml' or '.xml.gz'";
164 my $typesfilename = $cicfilename.$typesext if $typesuri;
165 my $annfilename = $cicfilename.$annsuffix.$annext if $annuri;
166 $cicfilename .= $cicext;
170 if (!defined($cicurl) ||
171 (!defined($typesurl) && $typesuri) ||
172 (!defined($annuri) && $annuri))
174 print "\nNOT FOUND!!!!!\n";
175 $c->send_error(RC_NOT_FOUND)
177 print_request("cic",$cicuri,$cicurl,$cicfilename);
178 print_request("types",$typesuri,$typesurl,$typesfilename)
180 print_request("ann",$annuri,$annurl,$annfilename)
183 # Retrieves the files
185 my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
187 download(1,"types",$typesurl,$typesfilename) if ($typesuri);
189 download(1,"ann",$annurl,$annfilename) if ($annuri);
191 # Merging the files together
194 <?xml version="1.0" encoding="UTF-8"?>
195 <cicxml uri="$cicuri">
202 # Answering the client
205 } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
206 my $filename = $inputuri;
207 $filename = $dtd_dir."/".$filename;
208 print "DTD: $inputuri ==> ($filename)\n";
209 if (stat($filename)) {
210 print "Using local copy\n";
213 while(<FD>) { $cont .= $_; }
217 die "Could not find DTD!";
219 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
220 my $quoted_html_link = $html_link;
221 $quoted_html_link =~ s/&/&/g;
222 $quoted_html_link =~ s/</</g;
223 $quoted_html_link =~ s/>/>/g;
224 $quoted_html_link =~ s/'/'/g;
225 $quoted_html_link =~ s/"/"/g;
226 print "\nConfiguration requested, returned #$quoted_html_link#\n";
227 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
229 } elsif ($http_method eq 'GET' and $http_path eq "/update") {
230 print "Update requested...";
232 kill(USR1,getppid());
234 answer($c,"<html><body><h1>Update done</h1></body></html>");
236 print "\nINVALID REQUEST!!!!!\n";
237 $c->send_error(RC_FORBIDDEN)
239 print "\nRequest solved: ".$r->url."\n\n";
243 print "\nCONNECTION CLOSED\n\n";
248 #================================
251 #CSC: Too much powerful: creates even /home, /home/users/, ...
252 #CSC: Does not raise errors if could not create dirs/files
256 my @dirs = split /\//,$pathname;
258 foreach $dir (@dirs) {
259 $tmp = ((defined($tmp)) ? $tmp."\/".$dir : "");
267 my ($str,$uri,$url,$filename) = @_;
268 print $str."uri: $uri\n";
269 print $str."url: $url\n";
270 print $str."filename: $filename\n\n";
281 my ($remove_headers,$str,$url,$filename) = @_;
285 my $mode; # retrieve mode: "normal" (.xml) or "gzipped" (.xml.gz)
286 if ($filename =~ /\.xml$/) { # set retrieve mode
288 } elsif ($filename =~ /\.xml\.gz$/) {
291 die "Unsupported download extension, might be '.gz' or '.xml'\n";
294 $cont = ""; # modified by side-effect by the callback function
295 if (stat($filename)) { # we already have local copy of requested file
296 print "Using local copy for the $str file\n";
298 if ($mode eq "gzipped") { # deflating cached file
299 print "deflating local file ...\n";
300 $gz = gzopen($filename, "r") or die "Cannot open gzip'ed file $filename: $gzerrno";
301 while ( $gz->gzread($buffer) > 0 ) {
304 die "Error while reading : $gzerrno\n" if $gzerrno != Z_STREAM_END ;
306 } elsif ($mode eq "normal") { # return cached file
308 while(<FD>) { $cont .= $_; }
311 die "Internal error: unexpected mode: $mode, might be 'normal' or 'gzipped'";
314 } else { # download file from net
315 print "Downloading the $str file\n"; # download file
316 $ua = LWP::UserAgent->new;
317 $request = HTTP::Request->new(GET => "$url");
318 $response = $ua->request($request, \&callback);
320 print "Storing the $str file\n";
322 open(FD, ">".$filename);
326 if ($mode eq "gzipped") { # deflate gzipped retrieved file
327 print "deflating just retrieved file ...\n";
328 $cont = ""; # reset $cont, cause $cont actually contain gzipped data
329 $gz = gzopen($filename, "r") or die "Cannot open gzip'ed file $filename: $gzerrno";
330 while ( $gz->gzread($buffer) > 0 ) {
333 die "Error while reading : $gzerrno\n" if $gzerrno != Z_STREAM_END ;
335 # now $cont contain deflated, clear text data
339 if ($remove_headers) {
340 $cont =~ s/<\?xml [^?]*\?>//sg;
341 $cont =~ s/<!DOCTYPE [^>]*>//sg;
349 my $res = new HTTP::Response;
350 $res->content($cont);
351 $c->send_response($res);
356 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);