]> matita.cs.unibo.it Git - helm.git/blob - helm/http_getter/http_getter.pl.in
added LICENSE
[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_LIBRARY_DIR = $ENV{"HELM_LIBRARY_DIR"};
29 # this should be the only fixed constant
30 my $DEFAULT_HELM_LIBRARY_DIR = "@DEFAULT_HELM_LIBRARY_DIR@";
31 if (defined ($HELM_LIBRARY_DIR)) {
32    $HELM_LIBRARY_PATH = $HELM_LIBRARY_DIR."/configuration.pl";
33 } else {
34    $HELM_LIBRARY_PATH = $DEFAULT_HELM_LIBRARY_DIR."/configuration.pl";
35 }
36 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
37 require $HELM_LIBRARY_PATH;
38
39 use HTTP::Daemon;
40 use HTTP::Status;
41 use HTTP::Request;
42 use LWP::UserAgent;
43 use DB_File;
44
45 #CSC: mancano i controlli sulle condizioni di errore di molte funzioni
46 #CSC: ==> non e' robusto
47 #CSC: altra roba da sistemare segnata con CSC
48
49 my $d = new HTTP::Daemon LocalPort => 8081;
50 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
51 print "Please contact me at: <URL:", $d->url, ">\n";
52 print "helm_dir: $helm_dir\n";
53 print "dtd_dir: $dtd_dir\n";
54 print "urls_of_uris.db: $uris_dbm.db\n";
55 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
56 $SIG{USR1} = \&update; # sent by the child to make the parent update
57 while (my $c = $d->accept) {
58  if (fork() == 0) {
59     while (my $r = $c->get_request) {
60         #CSC: mancano i controlli di sicurezza
61         
62         my $inputuri = $r->url; 
63         $inputuri =~ s/^[^?]*\?uri=(.*)/$1/;
64         print "\nRequest: ".$r->url."\n\n";
65         my $http_method = $r->method;
66         my $http_path = $r->url->path;
67
68         if ($http_method eq 'GET' and $http_path eq "/getciconly") {
69             # finds the uri, url and filename
70             my $cicuri = $inputuri;
71
72             my $cicfilename = $cicuri;
73             $cicfilename =~ s/cic:(.*)/$1/;
74             $cicfilename =~ s/theory:(.*)/$1/;
75             $cicfilename = $helm_dir.$cicfilename.".xml";
76
77             my $cicurl   = $map{$cicuri};
78             if (!defined($cicurl)) {
79              print "\nNOT FOUND!!!!!\n";
80              $c->send_error(RC_NOT_FOUND)
81             } else {
82                print_request("cic",$cicuri,$cicurl,$cicfilename);
83
84                # Retrieves the file
85                my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
86
87                # Answering the client
88                answer($c,$ciccontent);
89             }
90         } elsif ($http_method eq 'GET' and $http_path eq "/get") {
91             # finds the uris, urls and filenames
92             my $cicuri = $inputuri,
93                $typesuri = $inputuri,
94                $annuri = $inputuri;
95             my $annsuffix;
96             if ($inputuri =~ /\.types$/) {
97                $cicuri    =~ s/(.*)\.types$/$1/;
98                undef($annuri);
99             } elsif ($inputuri =~ /\.types\.ann$/) {
100                $cicuri    =~ s/(.*)\.types\.ann$/$1/;
101                $typesuri  =~ s/(.*)\.ann$/$1/;
102                $annsuffix = ".types.ann";
103             } elsif ($inputuri =~ /\.ann$/) {
104                $cicuri  =~ s/(.*)\.ann$/$1/;
105                undef($typesuri);
106                $annsuffix = ".ann";
107             } else {
108                undef($typesuri);
109                undef($annuri);
110             }
111
112             my $cicfilename = $cicuri;
113             $cicfilename =~ s/cic:(.*)/$1/;
114             $cicfilename =~ s/theory:(.*)/$1/;
115             $cicfilename = $helm_dir.$cicfilename;
116
117             my $typesfilename = $cicfilename.".types.xml"     if $typesuri;
118             my $annfilename  = $cicfilename.$annsuffix.".xml" if $annuri;
119             $cicfilename .= ".xml";
120
121             my $cicurl   = $map{$cicuri};
122             my $typesurl = $map{$typesuri} if $typesuri;
123             my $annurl   = $map{$annuri}  if $annuri;
124
125             if (!defined($cicurl) ||
126                (!defined($typesurl) && $typesuri) ||
127                (!defined($annuri) && $annuri))
128             {
129              print "\nNOT FOUND!!!!!\n";
130              $c->send_error(RC_NOT_FOUND)
131             } else {
132                print_request("cic",$cicuri,$cicurl,$cicfilename);
133                print_request("types",$typesuri,$typesurl,$typesfilename)
134                 if ($typesuri);
135                print_request("ann",$annuri,$annurl,$annfilename)
136                 if ($annuri);
137  
138                # Retrieves the files
139
140                my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
141                my $typescontent =
142                 download(1,"types",$typesurl,$typesfilename) if ($typesuri);
143                my $anncontent =
144                 download(1,"ann",$annurl,$annfilename) if ($annuri);
145  
146                # Merging the files together
147  
148                my $merged = <<EOT;
149 <?xml version="1.0" encoding="UTF-8"?>
150 <cicxml uri="$cicuri">
151 $ciccontent
152 $typescontent
153 $anncontent
154 </cicxml>
155 EOT
156
157                # Answering the client
158                answer($c,$merged);
159             }
160          } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
161             my $filename = $inputuri;
162             $filename = $dtd_dir."/".$filename;
163             print "DTD: $inputuri ==> ($filename)\n";
164             if (stat($filename)) {
165                print "Using local copy\n";
166                open(FD, $filename);
167                $cont = "";
168                while(<FD>) { $cont .= $_; }
169                close(FD);
170                answer($c,$cont);
171             } else {
172                die "Could not find DTD!";
173             }
174         } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
175             my $quoted_html_link = $html_link;
176             $quoted_html_link =~ s/&/&amp;/g;
177             $quoted_html_link =~ s/</&lt;/g;
178             $quoted_html_link =~ s/>/&gt;/g;
179             $quoted_html_link =~ s/'/&apos;/g;
180             $quoted_html_link =~ s/"/&quot;/g;
181             print "\nConfiguration requested, returned #$quoted_html_link#\n";
182             $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
183             answer($c,$cont);
184         } elsif ($http_method eq 'GET' and $http_path eq "/update") {
185            print "Update requested...";
186            update();
187            kill(USR1,getppid());
188            print " done\n";
189            answer($c,"<html><body><h1>Update done</h1></body></html>");
190         } else {
191             print "\nINVALID REQUEST!!!!!\n";
192             $c->send_error(RC_FORBIDDEN)
193         }
194         print "\nRequest solved: ".$r->url."\n\n";
195     }
196     $c->close;
197     undef($c);
198     print "\nCONNECTION CLOSED\n\n";
199     exit;
200   } # fork
201 }
202
203 #================================
204
205
206 #CSC: Too much powerful: creates even /home, /home/users/, ...
207 #CSC: Does not raise errors if could not create dirs/files
208 sub mkdirs
209 {
210  my ($pathname) = @_;
211  my @dirs = split /\//,$pathname;
212  my $tmp;
213  foreach $dir (@dirs) {
214   $tmp = ((defined($tmp)) ?  $tmp."\/".$dir : "");
215   mkdir($tmp,0777);
216  }
217  rmdir($tmp);
218 }
219
220 sub print_request
221 {
222  my ($str,$uri,$url,$filename) = @_;
223  print $str."uri: $uri\n";
224  print $str."url: $url\n";
225  print $str."filename: $filename\n\n";
226 }
227
228 sub callback
229 {
230  my ($data) = @_;
231  $cont .= $data;
232 }
233
234 sub download
235 {
236  my ($remove_headers,$str,$url,$filename) = @_;
237  $cont = ""; # modified by side-effect by the callback function
238  if (stat($filename)) {
239     print "Using local copy for the $str file\n";
240     open(FD, $filename);
241     while(<FD>) { $cont .= $_; }
242     close(FD);
243  } else {
244     print "Downloading the $str file\n";
245     $ua = LWP::UserAgent->new;
246     $request = HTTP::Request->new(GET => "$url");
247     $response = $ua->request($request, \&callback);
248                
249     print "Storing the $str file\n";
250     mkdirs($filename);
251     open(FD, ">".$filename);
252     print FD $cont;
253     close(FD);
254  }
255  if ($remove_headers) {
256     $cont =~ s/<\?xml [^?]*\?>//sg;
257     $cont =~ s/<!DOCTYPE [^>]*>//sg;
258  }
259  return $cont;
260 }
261
262 sub answer
263 {
264  my ($c,$cont) = @_;
265  my $res = new HTTP::Response;
266  $res->content($cont);
267  $c->send_response($res);
268 }
269
270 sub update {
271  untie %map;
272  tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
273 }