]> matita.cs.unibo.it Git - helm.git/blob - helm/http_getter/http_getter.pl.in
bee3315e7583cb9e1a98700559c36fc9eafb34b4
[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 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
37 require $HELM_LIB_PATH;
38
39 use HTTP::Daemon;
40 use HTTP::Status;
41 use HTTP::Request;
42 use LWP::UserAgent;
43 use DB_File;
44 use Compress::Zlib;
45
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
49
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) {
59  if (fork() == 0) {
60     while (my $r = $c->get_request) {
61         #CSC: mancano i controlli di sicurezza
62         
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;
68
69         if ($http_method eq 'GET' and $http_path eq "/getciconly") {
70             # finds the uri, url and filename
71             my $cicuri = $inputuri;
72
73             my $cicfilename = $cicuri;
74             $cicfilename =~ s/cic:(.*)/$1/;
75             $cicfilename =~ s/theory:(.*)/$1/;
76 #            $cicfilename = $helm_dir.$cicfilename.".xml";
77 # <gzip>
78             my $cicurl   = $map{$cicuri};
79                         my $extension;
80                         if ($cicurl =~ /\.xml$/) {      # non gzipped file
81                                         $extension = ".xml";
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'";
86                         }
87             $cicfilename = $helm_dir.$cicfilename.$extension;
88
89             #my $cicurl   = $map{$cicuri};
90 # </gzip>
91             if (!defined($cicurl)) {
92              print "\nNOT FOUND!!!!!\n";
93              $c->send_error(RC_NOT_FOUND)
94             } else {
95                print_request("cic",$cicuri,$cicurl,$cicfilename);
96
97                # Retrieves the file
98                my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
99
100                # Answering the client
101                answer($c,$ciccontent);
102             }
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,
107                $annuri = $inputuri;
108             my $annsuffix;
109             if ($inputuri =~ /\.types$/) {
110                $cicuri    =~ s/(.*)\.types$/$1/;
111                undef($annuri);
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/;
118                undef($typesuri);
119                $annsuffix = ".ann";
120             } else {
121                undef($typesuri);
122                undef($annuri);
123             }
124
125             my $cicfilename = $cicuri;
126             $cicfilename =~ s/cic:(.*)/$1/;
127             $cicfilename =~ s/theory:(.*)/$1/;
128             $cicfilename = $helm_dir.$cicfilename;
129
130 #            my $typesfilename = $cicfilename.".types.xml"     if $typesuri;
131 #            my $annfilename  = $cicfilename.$annsuffix.".xml" if $annuri;
132 #            $cicfilename .= ".xml";
133
134 # <gzip>
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
140                                 $cicext = ".xml";
141                         } elsif ($cicurl =~ /\.xml\.gz$/) {     # gzipped file
142                                 $cicext = ".xml.gz";
143                         } else {
144                                 die "unexpected extension in url: $cicurl; might be '.xml' or '.xml.gz'";
145                         }
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";
151                                 } else {
152                                         die "unexpected extension in url: $typesurl; might be '.xml' or '.xml.gz'";
153                                 }
154                         }
155                         if (defined($annuri)) { # extension selection for annotation file
156                                 if ($annurl =~ /\.xml$/) {      # normal file
157                                         $annext = ".xml";
158                                 } elsif ($annurl =~ /\.xml\.gz$/) {     # gzipped file
159                                         $annext = ".xml.gz";
160                                 } else {
161                                         die "unexpected extension in url: $annurl might be '.xml' or '.xml.gz'";
162                                 }
163                         }
164             my $typesfilename = $cicfilename.$typesext if $typesuri;
165             my $annfilename  = $cicfilename.$annsuffix.$annext if $annuri;
166             $cicfilename .= $cicext;
167 # </gzip>
168
169
170             if (!defined($cicurl) ||
171                (!defined($typesurl) && $typesuri) ||
172                (!defined($annuri) && $annuri))
173             {
174              print "\nNOT FOUND!!!!!\n";
175              $c->send_error(RC_NOT_FOUND)
176             } else {
177                print_request("cic",$cicuri,$cicurl,$cicfilename);
178                print_request("types",$typesuri,$typesurl,$typesfilename)
179                 if ($typesuri);
180                print_request("ann",$annuri,$annurl,$annfilename)
181                 if ($annuri);
182  
183                # Retrieves the files
184
185                my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
186                my $typescontent =
187                 download(1,"types",$typesurl,$typesfilename) if ($typesuri);
188                my $anncontent =
189                 download(1,"ann",$annurl,$annfilename) if ($annuri);
190  
191                # Merging the files together
192  
193                my $merged = <<EOT;
194 <?xml version="1.0" encoding="UTF-8"?>
195 <cicxml uri="$cicuri">
196 $ciccontent
197 $typescontent
198 $anncontent
199 </cicxml>
200 EOT
201
202                # Answering the client
203                answer($c,$merged);
204             }
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";
211                open(FD, $filename);
212                $cont = "";
213                while(<FD>) { $cont .= $_; }
214                close(FD);
215                answer($c,$cont);
216             } else {
217                die "Could not find DTD!";
218             }
219         } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
220             my $quoted_html_link = $html_link;
221             $quoted_html_link =~ s/&/&amp;/g;
222             $quoted_html_link =~ s/</&lt;/g;
223             $quoted_html_link =~ s/>/&gt;/g;
224             $quoted_html_link =~ s/'/&apos;/g;
225             $quoted_html_link =~ s/"/&quot;/g;
226             print "\nConfiguration requested, returned #$quoted_html_link#\n";
227             $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
228             answer($c,$cont);
229         } elsif ($http_method eq 'GET' and $http_path eq "/update") {
230            print "Update requested...";
231            update();
232            kill(USR1,getppid());
233            print " done\n";
234            answer($c,"<html><body><h1>Update done</h1></body></html>");
235         } else {
236             print "\nINVALID REQUEST!!!!!\n";
237             $c->send_error(RC_FORBIDDEN)
238         }
239         print "\nRequest solved: ".$r->url."\n\n";
240     }
241     $c->close;
242     undef($c);
243     print "\nCONNECTION CLOSED\n\n";
244     exit;
245   } # fork
246 }
247
248 #================================
249
250
251 #CSC: Too much powerful: creates even /home, /home/users/, ...
252 #CSC: Does not raise errors if could not create dirs/files
253 sub mkdirs
254 {
255  my ($pathname) = @_;
256  my @dirs = split /\//,$pathname;
257  my $tmp;
258  foreach $dir (@dirs) {
259   $tmp = ((defined($tmp)) ?  $tmp."\/".$dir : "");
260   mkdir($tmp,0777);
261  }
262  rmdir($tmp);
263 }
264
265 sub print_request
266 {
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";
271 }
272
273 sub callback
274 {
275  my ($data) = @_;
276  $cont .= $data;
277 }
278
279 sub download
280 {
281  my ($remove_headers,$str,$url,$filename) = @_;
282 # <gzip>
283  my ($gz, $buffer);
284
285  my $mode;      # retrieve mode: "normal" (.xml) or "gzipped" (.xml.gz)
286  if ($filename =~ /\.xml$/) {   # set retrieve mode
287          $mode = "normal";
288  } elsif ($filename =~ /\.xml\.gz$/) {
289          $mode = "gzipped";
290  } else {
291          die "Unsupported download extension, might be '.gz' or '.xml'\n";
292  }
293 # </gzip>
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";
297 # <gzip>
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 ) {
302                         $cont .= $buffer;
303                 }
304                 die "Error while reading : $gzerrno\n" if $gzerrno != Z_STREAM_END ;
305                 $gz->gzclose();
306         } elsif ($mode eq "normal") {   # return cached file
307                 open(FD, $filename);
308                 while(<FD>) { $cont .= $_; }
309                 close(FD);
310         } else {        # error
311                 die "Internal error: unexpected mode: $mode, might be 'normal' or 'gzipped'";
312         }
313 # </gzip>
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);
319                
320     print "Storing the $str file\n";
321     mkdirs($filename);
322     open(FD, ">".$filename);
323     print FD $cont;
324     close(FD);
325 # <gzip>
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 ) {
331                         $cont .= $buffer;
332                 }
333                 die "Error while reading : $gzerrno\n" if $gzerrno != Z_STREAM_END ;
334                 $gz->gzclose();
335                         # now $cont contain deflated, clear text data
336         }
337 # </gzip>
338  }
339  if ($remove_headers) {
340     $cont =~ s/<\?xml [^?]*\?>//sg;
341     $cont =~ s/<!DOCTYPE [^>]*>//sg;
342  }
343  return $cont;
344 }
345
346 sub answer
347 {
348  my ($c,$cont) = @_;
349  my $res = new HTTP::Response;
350  $res->content($cont);
351  $c->send_response($res);
352 }
353
354 sub update {
355  untie %map;
356  tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
357 }