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_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";
34 $HELM_LIBRARY_PATH = $DEFAULT_HELM_LIBRARY_DIR."/configuration.pl";
36 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
37 require $HELM_LIBRARY_PATH;
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
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) {
59 while (my $r = $c->get_request) {
60 #CSC: mancano i controlli di sicurezza
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;
68 if ($http_method eq 'GET' and $http_path eq "/getciconly") {
69 # finds the uri, url and filename
70 my $cicuri = $inputuri;
72 my $cicfilename = $cicuri;
73 $cicfilename =~ s/cic:(.*)/$1/;
74 $cicfilename =~ s/theory:(.*)/$1/;
75 $cicfilename = $helm_dir.$cicfilename.".xml";
77 my $cicurl = $map{$cicuri};
78 if (!defined($cicurl)) {
79 print "\nNOT FOUND!!!!!\n";
80 $c->send_error(RC_NOT_FOUND)
82 print_request("cic",$cicuri,$cicurl,$cicfilename);
85 my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
87 # Answering the client
88 answer($c,$ciccontent);
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,
96 if ($inputuri =~ /\.types$/) {
97 $cicuri =~ s/(.*)\.types$/$1/;
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/;
112 my $cicfilename = $cicuri;
113 $cicfilename =~ s/cic:(.*)/$1/;
114 $cicfilename =~ s/theory:(.*)/$1/;
115 $cicfilename = $helm_dir.$cicfilename;
117 my $typesfilename = $cicfilename.".types.xml" if $typesuri;
118 my $annfilename = $cicfilename.$annsuffix.".xml" if $annuri;
119 $cicfilename .= ".xml";
121 my $cicurl = $map{$cicuri};
122 my $typesurl = $map{$typesuri} if $typesuri;
123 my $annurl = $map{$annuri} if $annuri;
125 if (!defined($cicurl) ||
126 (!defined($typesurl) && $typesuri) ||
127 (!defined($annuri) && $annuri))
129 print "\nNOT FOUND!!!!!\n";
130 $c->send_error(RC_NOT_FOUND)
132 print_request("cic",$cicuri,$cicurl,$cicfilename);
133 print_request("types",$typesuri,$typesurl,$typesfilename)
135 print_request("ann",$annuri,$annurl,$annfilename)
138 # Retrieves the files
140 my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
142 download(1,"types",$typesurl,$typesfilename) if ($typesuri);
144 download(1,"ann",$annurl,$annfilename) if ($annuri);
146 # Merging the files together
149 <?xml version="1.0" encoding="UTF-8"?>
150 <cicxml uri="$cicuri">
157 # Answering the client
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";
168 while(<FD>) { $cont .= $_; }
172 die "Could not find DTD!";
174 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
175 my $quoted_html_link = $html_link;
176 $quoted_html_link =~ s/&/&/g;
177 $quoted_html_link =~ s/</</g;
178 $quoted_html_link =~ s/>/>/g;
179 $quoted_html_link =~ s/'/'/g;
180 $quoted_html_link =~ s/"/"/g;
181 print "\nConfiguration requested, returned #$quoted_html_link#\n";
182 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
184 } elsif ($http_method eq 'GET' and $http_path eq "/update") {
185 print "Update requested...";
187 kill(USR1,getppid());
189 answer($c,"<html><body><h1>Update done</h1></body></html>");
191 print "\nINVALID REQUEST!!!!!\n";
192 $c->send_error(RC_FORBIDDEN)
194 print "\nRequest solved: ".$r->url."\n\n";
198 print "\nCONNECTION CLOSED\n\n";
203 #================================
206 #CSC: Too much powerful: creates even /home, /home/users/, ...
207 #CSC: Does not raise errors if could not create dirs/files
211 my @dirs = split /\//,$pathname;
213 foreach $dir (@dirs) {
214 $tmp = ((defined($tmp)) ? $tmp."\/".$dir : "");
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";
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";
241 while(<FD>) { $cont .= $_; }
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);
249 print "Storing the $str file\n";
251 open(FD, ">".$filename);
255 if ($remove_headers) {
256 $cont =~ s/<\?xml [^?]*\?>//sg;
257 $cont =~ s/<!DOCTYPE [^>]*>//sg;
265 my $res = new HTTP::Response;
266 $res->content($cont);
267 $c->send_response($res);
272 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);