3 # First of all, let's load HELM configuration
5 my $HELM_LIBRARY_DIR = $ENV{"HELM_LIBRARY_DIR"};
6 # this should be the only fixed constant
7 my $DEFAULT_HELM_LIBRARY_DIR = "@DEFAULT_HELM_LIBRARY_DIR@";
8 if (defined ($HELM_LIBRARY_DIR)) {
9 $HELM_LIBRARY_PATH = $HELM_LIBRARY_DIR."/configuration.pl";
11 $HELM_LIBRARY_PATH = $DEFAULT_HELM_LIBRARY_DIR."/configuration.pl";
13 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
14 require $HELM_LIBRARY_PATH;
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
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) {
36 while (my $r = $c->get_request) {
37 #CSC: mancano i controlli di sicurezza
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;
45 if ($http_method eq 'GET' and $http_path eq "/getciconly") {
46 # finds the uri, url and filename
47 my $cicuri = $inputuri;
49 my $cicfilename = $cicuri;
50 $cicfilename =~ s/cic:(.*)/$1/;
51 $cicfilename =~ s/theory:(.*)/$1/;
52 $cicfilename = $helm_dir.$cicfilename.".xml";
54 my $cicurl = $map{$cicuri};
55 if (!defined($cicurl)) {
56 print "\nNOT FOUND!!!!!\n";
57 $c->send_error(RC_NOT_FOUND)
59 print_request("cic",$cicuri,$cicurl,$cicfilename);
62 my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
64 # Answering the client
65 answer($c,$ciccontent);
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,
73 if ($inputuri =~ /\.types$/) {
74 $cicuri =~ s/(.*)\.types$/$1/;
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/;
89 my $cicfilename = $cicuri;
90 $cicfilename =~ s/cic:(.*)/$1/;
91 $cicfilename =~ s/theory:(.*)/$1/;
92 $cicfilename = $helm_dir.$cicfilename;
94 my $typesfilename = $cicfilename.".types.xml" if $typesuri;
95 my $annfilename = $cicfilename.$annsuffix.".xml" if $annuri;
96 $cicfilename .= ".xml";
98 my $cicurl = $map{$cicuri};
99 my $typesurl = $map{$typesuri} if $typesuri;
100 my $annurl = $map{$annuri} if $annuri;
102 if (!defined($cicurl) ||
103 (!defined($typesurl) && $typesuri) ||
104 (!defined($annuri) && $annuri))
106 print "\nNOT FOUND!!!!!\n";
107 $c->send_error(RC_NOT_FOUND)
109 print_request("cic",$cicuri,$cicurl,$cicfilename);
110 print_request("types",$typesuri,$typesurl,$typesfilename)
112 print_request("ann",$annuri,$annurl,$annfilename)
115 # Retrieves the files
117 my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
119 download(1,"types",$typesurl,$typesfilename) if ($typesuri);
121 download(1,"ann",$annurl,$annfilename) if ($annuri);
123 # Merging the files together
126 <?xml version="1.0" encoding="UTF-8"?>
127 <cicxml uri="$cicuri">
134 # Answering the client
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";
145 while(<FD>) { $cont .= $_; }
149 die "Could not find DTD!";
151 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
152 my $quoted_html_link = $html_link;
153 $quoted_html_link =~ s/&/&/g;
154 $quoted_html_link =~ s/</</g;
155 $quoted_html_link =~ s/>/>/g;
156 $quoted_html_link =~ s/'/'/g;
157 $quoted_html_link =~ s/"/"/g;
158 print "\nConfiguration requested, returned #$quoted_html_link#\n";
159 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
161 } elsif ($http_method eq 'GET' and $http_path eq "/update") {
162 print "Update requested...";
164 kill(USR1,getppid());
166 answer($c,"<html><body><h1>Update done</h1></body></html>");
168 print "\nINVALID REQUEST!!!!!\n";
169 $c->send_error(RC_FORBIDDEN)
171 print "\nRequest solved: ".$r->url."\n\n";
175 print "\nCONNECTION CLOSED\n\n";
180 #================================
183 #CSC: Too much powerful: creates even /home, /home/users/, ...
184 #CSC: Does not raise errors if could not create dirs/files
188 my @dirs = split /\//,$pathname;
190 foreach $dir (@dirs) {
191 $tmp = ((defined($tmp)) ? $tmp."\/".$dir : "");
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";
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";
218 while(<FD>) { $cont .= $_; }
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);
226 print "Storing the $str file\n";
228 open(FD, ">".$filename);
232 if ($remove_headers) {
233 $cont =~ s/<\?xml [^?]*\?>//sg;
234 $cont =~ s/<!DOCTYPE [^>]*>//sg;
242 my $res = new HTTP::Response;
243 $res->content($cont);
244 $c->send_response($res);
249 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);