]> matita.cs.unibo.it Git - helm.git/blob - helm/http_getter/http_getter.pl.in
Reindented some parts
[helm.git] / helm / http_getter / http_getter.pl.in
1 #!@PERL_BINARY@
2
3 # Copyright (C) 2000, HELM Team.
4
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.
8
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.
13
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.
18
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.
22
23 # For details, see the HELM World-Wide-Web page,
24 # http://cs.unibo.it/helm/.
25
26 # First of all, let's load HELM configuration
27 use Env;
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";
33 } else {
34    $HELM_LIB_PATH = $DEFAULT_HELM_LIB_DIR."/configuration.pl";
35 }
36
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"}));
40
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";
47 }
48 # </ZACK>
49
50 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
51 require $HELM_LIB_PATH;
52
53 use HTTP::Daemon;
54 use HTTP::Status;
55 use HTTP::Request;
56 use LWP::UserAgent;
57 use DB_File;
58 use Compress::Zlib;
59
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
63
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";
71
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) {
75  if (fork() == 0) {
76     while (my $r = $c->get_request) {
77         #CSC: mancano i controlli di sicurezza
78         
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;
84
85         if ($http_method eq 'GET' and $http_path eq "/getciconly") {
86             # finds the uri, url and filename
87             my $cicuri = $inputuri;
88
89             my $cicfilename = $cicuri;
90             $cicfilename =~ s/cic:(.*)/$1/;
91             $cicfilename =~ s/theory:(.*)/$1/;
92
93             my $cicurl   = $map{$cicuri};
94             my $extension;
95             if ($cicurl =~ /\.xml$/) { # non gzipped file
96               $extension = ".xml";
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'".
101               "or '.xml.gz'";
102             }
103             $cicfilename = $helm_dir.$cicfilename.$extension;
104
105             if (!defined($cicurl)) {
106              print "\nNOT FOUND!!!!!\n";
107              $c->send_error(RC_NOT_FOUND)
108             } else {
109                print_request("cic",$cicuri,$cicurl,$cicfilename);
110
111                # Retrieves the file
112                my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
113
114                # Answering the client
115                answer($c,$ciccontent);
116             }
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,
121                $annuri = $inputuri;
122             my $annsuffix;
123             if ($inputuri =~ /\.types$/) {
124                $cicuri    =~ s/(.*)\.types$/$1/;
125                undef($annuri);
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/;
132                undef($typesuri);
133                $annsuffix = ".ann";
134             } else {
135                undef($typesuri);
136                undef($annuri);
137             }
138
139             my $cicfilename = $cicuri;
140             $cicfilename =~ s/cic:(.*)/$1/;
141             $cicfilename =~ s/theory:(.*)/$1/;
142             $cicfilename = $helm_dir.$cicfilename;
143
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
149              $cicext = ".xml";
150             } elsif ($cicurl =~ /\.xml\.gz$/) { # gzipped file
151              $cicext = ".xml.gz";
152             } else {
153              die "unexpected extension in url: $cicurl;".
154              "might be '.xml' or '.xml.gz'";
155             }
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";
161              } else {
162               die "unexpected extension in url: $typesurl;".
163               "might be '.xml' or '.xml.gz'";
164              }
165             }
166             if (defined($annuri)) { # extension selection for annotation file
167              if ($annurl =~ /\.xml$/) { # normal file
168               $annext = ".xml";
169              } elsif ($annurl =~ /\.xml\.gz$/) { # gzipped file
170               $annext = ".xml.gz";
171              } else {
172               die "unexpected extension in url: $annurl".
173               "might be '.xml' or '.xml.gz'";
174              }
175             }
176             my $typesfilename = $cicfilename.$typesext if $typesuri;
177             my $annfilename  = $cicfilename.$annsuffix.$annext if $annuri;
178             $cicfilename .= $cicext;
179
180             if (!defined($cicurl) ||
181                (!defined($typesurl) && $typesuri) ||
182                (!defined($annuri) && $annuri))
183             {
184              print "\nNOT FOUND!!!!!\n";
185              $c->send_error(RC_NOT_FOUND)
186             } else {
187                print_request("cic",$cicuri,$cicurl,$cicfilename);
188                print_request("types",$typesuri,$typesurl,$typesfilename)
189                 if ($typesuri);
190                print_request("ann",$annuri,$annurl,$annfilename)
191                 if ($annuri);
192  
193                # Retrieves the files
194
195                my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
196                my $typescontent =
197                 download(1,"types",$typesurl,$typesfilename) if ($typesuri);
198                my $anncontent =
199                 download(1,"ann",$annurl,$annfilename) if ($annuri);
200  
201                # Merging the files together
202  
203                my $merged = <<EOT;
204 <?xml version="1.0" encoding="UTF-8"?>
205 <cicxml uri="$cicuri">
206 $ciccontent
207 $typescontent
208 $anncontent
209 </cicxml>
210 EOT
211
212                # Answering the client
213                answer($c,$merged);
214             }
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";
222                $cont = "";
223                while(<FD>) { $cont .= $_; }
224                close(FD);
225                answer($c,$cont);
226             } else {
227                die "Could not find DTD!";
228             }
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";
236                $cont = "";
237                while(<FD>) { $cont .= $_; }
238                close(FD);
239                answer($c,$cont);
240             } else {
241                die "Could not find XSLT!";
242             }
243         } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
244             my $quoted_html_link = $html_link;
245             $quoted_html_link =~ s/&/&amp;/g;
246             $quoted_html_link =~ s/</&lt;/g;
247             $quoted_html_link =~ s/>/&gt;/g;
248             $quoted_html_link =~ s/'/&apos;/g;
249             $quoted_html_link =~ s/"/&quot;/g;
250             print "\nConfiguration requested, returned #$quoted_html_link#\n";
251             $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
252             answer($c,$cont);
253         } elsif ($http_method eq 'GET' and $http_path eq "/update") {
254            print "Update requested...";
255            update();
256            kill(USR1,getppid());
257            print " done\n";
258            answer($c,"<html><body><h1>Update done</h1></body></html>");
259         } else {
260             print "\nINVALID REQUEST!!!!!\n";
261             $c->send_error(RC_FORBIDDEN)
262         }
263         print "\nRequest solved: ".$r->url."\n\n";
264     }
265     $c->close;
266     undef($c);
267     print "\nCONNECTION CLOSED\n\n";
268     exit;
269   } # fork
270 }
271
272 #================================
273
274
275 #CSC: Too much powerful: creates even /home, /home/users/, ...
276 #CSC: Does not raise errors if could not create dirs/files
277 sub mkdirs
278 {
279  my ($pathname) = @_;
280  my @dirs = split /\//,$pathname;
281  my $tmp;
282  foreach $dir (@dirs) {
283   $tmp = ((defined($tmp)) ?  $tmp."\/".$dir : "");
284   mkdir($tmp,0777);
285  }
286  rmdir($tmp);
287 }
288
289 sub print_request
290 {
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";
295 }
296
297 sub callback
298 {
299  my ($data) = @_;
300  $cont .= $data;
301 }
302
303 sub gunzip { # gunzip a file and return the deflated content
304         my ($filename) = @_;
305
306         my ($gz, $buffer, $cont);
307
308         print "deflating $filename ...\n";
309         $gz = gzopen($filename, "r")
310                 or die "Cannot open gzip'ed file $filename: $gzerrno";
311         $cont = "";
312         while ( $gz->gzread($buffer) > 0 ) {
313                 $cont .= $buffer;
314         }
315         die "Error while reading : $gzerrno\n" if $gzerrno != Z_STREAM_END ;
316         $gz->gzclose();
317
318         return $cont;
319 }
320
321 sub gzip {      # gzip the content argument and save it to filename argument
322         my ($cont, $filename) = @_;
323
324         my ($gz, $cont);
325
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" ;
329         $gz->gzclose();
330 }
331
332 sub download
333 {
334  my ($remove_headers,$str,$url,$filename) = @_;
335  my ($gz, $buffer);
336
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";
342  } else {
343          die "Unsupported download extension, might be '.gz' or '.xml'\n";
344  }
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
348
349  my $localfname="";
350  if (stat($basefname)) {
351         $localfname=$basefname;
352  } elsif (stat($basefname.".gz")) {
353         $localfname=$basefname.".gz";
354  }
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 .= $_; }
363    close(FD);
364   } else { # error
365    die "Internal error: unexpected file name $localfname,"
366    ."must end with '.gz' or '.xml.gz'\n";
367   }
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);
373                
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";
378  mkdirs($filename);
379  open(FD, ">".$filename.".tmp") or die "Cannot open $filename.tmp\n";
380  print FD $cont;
381  close(FD);
382
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
394    # and update $cont
395   open(FD, "> $basefname") or die "cannot open $basefname\n";
396   $cont = gunzip($filename.".tmp");
397   print FD $cont;
398   close(FD);
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
404  } else {
405   die "Internal error, unsopported cachemode, resourcetype couple\n";
406  }
407  # $cont now contained uncompressed data
408
409  }
410  if ($remove_headers) {
411     $cont =~ s/<\?xml [^?]*\?>//sg;
412     $cont =~ s/<!DOCTYPE [^>]*>//sg;
413  }
414  return $cont;
415 }
416
417 sub answer
418 {
419  my ($c,$cont) = @_;
420  my $res = new HTTP::Response;
421  $res->content($cont);
422  $c->send_response($res);
423 }
424
425 sub update {
426  untie %map;
427  tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
428 }