3 # First of all, let's load HELM configuration
5 my $HELM_CONFIGURATION_PREFIX = $ENV{"HELM_CONFIGURATION_PREFIX"};
6 my $HELM_CONFIGURATION_PATH =
7 $HELM_CONFIGURATION_PREFIX."/local/lib/helm/configuration.pl";
8 # next require defines: $helm_dir, $html_link, $dtd_dir, $uris_dbm
9 require $HELM_CONFIGURATION_PATH;
19 #CSC: mancano i controlli sulle condizioni di errore di molte funzioni
20 #CSC: ==> non e' robusto
21 #CSC: altra roba da sistemare segnata con CSC
23 my $d = new HTTP::Daemon LocalPort => 8081;
24 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
25 print "Please contact me at: <URL:", $d->url, ">\n";
26 print "helm_dir: $helm_dir\n";
27 print "dtd_dir: $dtd_dir\n";
28 print "urls_of_uris.db: $uris_dbm.db\n";
29 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
30 $SIG{USR1} = \&update; # sent by the child to make the parent update
31 while (my $c = $d->accept) {
33 while (my $r = $c->get_request) {
34 #CSC: mancano i controlli di sicurezza
36 my $inputuri = $r->url;
37 $inputuri =~ s/^[^?]*\?uri=(.*)/$1/;
38 print "\nRequest: ".$r->url."\n\n";
39 my $http_method = $r->method;
40 my $http_path = $r->url->path;
42 if ($http_method eq 'GET' and $http_path eq "/getciconly") {
43 # finds the uri, url and filename
44 my $cicuri = $inputuri;
46 my $cicfilename = $cicuri;
47 $cicfilename =~ s/cic:(.*)/$1/;
48 $cicfilename =~ s/theory:(.*)/$1/;
49 $cicfilename = $helm_dir.$cicfilename.".xml";
51 my $cicurl = $map{$cicuri};
52 if (!defined($cicurl)) {
53 print "\nNOT FOUND!!!!!\n";
54 $c->send_error(RC_NOT_FOUND)
56 print_request("cic",$cicuri,$cicurl,$cicfilename);
59 my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
61 # Answering the client
62 answer($c,$ciccontent);
64 } elsif ($http_method eq 'GET' and $http_path eq "/get") {
65 # finds the uris, urls and filenames
66 my $cicuri = $inputuri,
67 $typesuri = $inputuri,
70 if ($inputuri =~ /\.types$/) {
71 $cicuri =~ s/(.*)\.types$/$1/;
73 } elsif ($inputuri =~ /\.types\.ann$/) {
74 $cicuri =~ s/(.*)\.types\.ann$/$1/;
75 $typesuri =~ s/(.*)\.ann$/$1/;
76 $annsuffix = ".types.ann";
77 } elsif ($inputuri =~ /\.ann$/) {
78 $cicuri =~ s/(.*)\.ann$/$1/;
86 my $cicfilename = $cicuri;
87 $cicfilename =~ s/cic:(.*)/$1/;
88 $cicfilename =~ s/theory:(.*)/$1/;
89 $cicfilename = $helm_dir.$cicfilename;
91 my $typesfilename = $cicfilename.".types.xml" if $typesuri;
92 my $annfilename = $cicfilename.$annsuffix.".xml" if $annuri;
93 $cicfilename .= ".xml";
95 my $cicurl = $map{$cicuri};
96 my $typesurl = $map{$typesuri} if $typesuri;
97 my $annurl = $map{$annuri} if $annuri;
99 if (!defined($cicurl) ||
100 (!defined($typesurl) && $typesuri) ||
101 (!defined($annuri) && $annuri))
103 print "\nNOT FOUND!!!!!\n";
104 $c->send_error(RC_NOT_FOUND)
106 print_request("cic",$cicuri,$cicurl,$cicfilename);
107 print_request("types",$typesuri,$typesurl,$typesfilename)
109 print_request("ann",$annuri,$annurl,$annfilename)
112 # Retrieves the files
114 my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
116 download(1,"types",$typesurl,$typesfilename) if ($typesuri);
118 download(1,"ann",$annurl,$annfilename) if ($annuri);
120 # Merging the files together
123 <?xml version="1.0" encoding="UTF-8"?>
124 <cicxml uri="$cicuri">
131 # Answering the client
134 } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
135 my $filename = $inputuri;
136 $filename = $dtd_dir."/".$filename;
137 print "DTD: $inputuri ==> ($filename)\n";
138 if (stat($filename)) {
139 print "Using local copy\n";
142 while(<FD>) { $cont .= $_; }
146 die "Could not find DTD!";
148 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
149 my $quoted_html_link = $html_link;
150 $quoted_html_link =~ s/&/&/g;
151 $quoted_html_link =~ s/</</g;
152 $quoted_html_link =~ s/>/>/g;
153 $quoted_html_link =~ s/'/'/g;
154 $quoted_html_link =~ s/"/"/g;
155 print "\nConfiguration requested, returned #$quoted_html_link#\n";
156 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
158 } elsif ($http_method eq 'GET' and $http_path eq "/update") {
159 print "Update requested...";
161 kill(USR1,getppid());
163 answer($c,"<html><body><h1>Update done</h1></body></html>");
165 print "\nINVALID REQUEST!!!!!\n";
166 $c->send_error(RC_FORBIDDEN)
168 print "\nRequest solved: ".$r->url."\n\n";
172 print "\nCONNECTION CLOSED\n\n";
177 #================================
180 #CSC: Too much powerful: creates even /home, /home/users/, ...
181 #CSC: Does not raise errors if could not create dirs/files
185 my @dirs = split /\//,$pathname;
187 foreach $dir (@dirs) {
188 $tmp = ((defined($tmp)) ? $tmp."\/".$dir : "");
196 my ($str,$uri,$url,$filename) = @_;
197 print $str."uri: $uri\n";
198 print $str."url: $url\n";
199 print $str."filename: $filename\n\n";
210 my ($remove_headers,$str,$url,$filename) = @_;
211 $cont = ""; # modified by side-effect by the callback function
212 if (stat($filename)) {
213 print "Using local copy for the $str file\n";
215 while(<FD>) { $cont .= $_; }
218 print "Downloading the $str file\n";
219 $ua = LWP::UserAgent->new;
220 $request = HTTP::Request->new(GET => "$url");
221 $response = $ua->request($request, \&callback);
223 print "Storing the $str file\n";
225 open(FD, ">".$filename);
229 if ($remove_headers) {
230 $cont =~ s/<\?xml [^?]*\?>//sg;
231 $cont =~ s/<!DOCTYPE [^>]*>//sg;
239 my $res = new HTTP::Response;
240 $res->content($cont);
241 $c->send_response($res);
246 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);