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