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
9 require $HELM_CONFIGURATION_PATH;
20 my $d = new HTTP::Daemon LocalPort => 8081;
21 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
22 print "Please contact me at: <URL:", $d->url, ">\n";
23 print "helm_dir: $helm_dir\n";
24 print "urls_of_uris.db: $uris_dbm.db\n";
25 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
26 while (my $c = $d->accept) {
28 while (my $r = $c->get_request) {
29 #CSC: mancano i controlli di sicurezza
33 $cicuri =~ s/^[^?]*\?url=(.*)/$1/;
34 print "*".$r->url."\n";
35 my $http_method = $r->method;
36 my $http_path = $r->url->path;
37 if ($http_method eq 'GET' and $http_path eq "/get") {
38 my $filename = $cicuri;
39 $filename =~ s/cic:(.*)/$1/;
40 $filename =~ s/theory:(.*)/$1/;
41 $filename = $helm_dir.$filename.".xml";
42 my $resolved = $map{$cicuri};
43 print "$cicuri ==> $resolved ($filename)\n";
44 if (stat($filename)) {
45 print "Using local copy\n";
47 while(<FD>) { $cont .= $_; }
49 my $res = new HTTP::Response;
51 $c->send_response($res);
53 print "Downloading\n";
54 $ua = LWP::UserAgent->new;
55 $request = HTTP::Request->new(GET => "$resolved");
56 $response = $ua->request($request, \&callback);
58 print "Storing file\n";
60 open(FD, ">".$filename);
64 my $res = new HTTP::Response;
66 $c->send_response($res);
68 } elsif ($http_method eq 'GET' and $http_path eq "/annotate") {
69 my $do_annotate = ($cicuri =~ /\.ann$/);
70 my $target_to_annotate = $cicuri;
71 $target_to_annotate =~ s/(.*)\.ann$/$1/ if $do_annotate;
72 my $filename = $cicuri;
73 $filename =~ s/cic:(.*)/$1/;
74 $filename =~ s/theory:(.*)/$1/;
75 my $filename_target = $helm_dir.$filename if $do_annotate;
76 $filename = $helm_dir.$filename.".xml";
77 $filename_target =~ s/(.*)\.ann$/$1.xml/ if $do_annotate;
78 my $resolved = $map{$cicuri};
79 my $resolved_target = $map{$target_to_annotate} if $do_annotate;
81 print "($cicuri, $target_to_annotate) ==> ($resolved + $resolved_target) ($filename)\n";
83 print "$cicuri ==> $resolved ($filename)\n";
86 # Retrieves the annotation
88 if (stat($filename)) {
89 print "Using local copy for the annotation\n";
91 while(<FD>) { $cont .= $_; }
94 print "Downloading the annotation\n";
95 $ua = LWP::UserAgent->new;
96 $request = HTTP::Request->new(GET => "$resolved");
97 $response = $ua->request($request, \&callback);
99 print "Storing file for the annotation\n";
101 open(FD, ">".$filename);
105 my $annotation = $cont;
107 # Retrieves the target to annotate
111 if (stat($filename_target)) {
112 print "Using local copy for the file to annotate\n";
113 open(FD, $filename_target);
114 while(<FD>) { $cont .= $_; }
117 print "Downloading the file to annotate\n";
118 $ua = LWP::UserAgent->new;
119 $request = HTTP::Request->new(GET => "$resolved_target");
120 $response = $ua->request($request, \&callback);
122 print "Storing file for the file to annotate\n";
123 mkdirs($filename_target);
124 open(FD, ">".$filename_target);
131 # Merging the annotation and the target
133 $target =~ s/<\?xml [^?]*\?>//sg;
134 $target =~ s/<!DOCTYPE [^>]*>//sg;
135 $annotation =~ s/<\?xml [^?]*\?>//sg;
136 $annotation =~ s/<!DOCTYPE [^>]*>//sg;
138 <?xml version="1.0" encoding="UTF-8"?>
139 <cicxml uri="$target_to_annotate">
145 # Answering the client
147 my $res = new HTTP::Response;
148 $res->content($merged);
149 $c->send_response($res);
150 } elsif ($http_method eq 'GET' and $http_path eq "/getwithtypes") {
153 if ($cicuri =~ /\.types$/) {
156 } elsif ($cicuri =~ /\.ann$/) {
162 my $target_to_annotate = $cicuri;
163 if ($mode eq "types") {
164 $target_to_annotate =~ s/(.*)\.types$/$1/;
165 } elsif ($mode eq "ann") {
166 $target_to_annotate =~ s/(.*)\.ann$/$1/;
168 my $filename = $cicuri;
169 $filename =~ s/cic:(.*)/$1/;
170 $filename =~ s/theory:(.*)/$1/;
171 my $filename_target = $helm_dir.$filename if $do_annotate;
172 $filename = $helm_dir.$filename.".xml";
173 if ($mode eq "types") {
174 $filename_target =~ s/(.*)\.types$/$1.xml/;
175 } elsif ($mode eq "ann") {
176 $filename_target =~ s/(.*)\.ann$/$1.xml/;
178 my $resolved = $map{$cicuri};
179 my $resolved_target = $map{$target_to_annotate} if $do_annotate;
181 print "GETWITHTYPES!!\n" if ($mode eq "types");
182 print "GETWITHANN!!\n" if ($mode eq "ann");
183 print "($cicuri, $target_to_annotate) ==> ($resolved + $resolved_target) ($filename)\n";
185 print "$cicuri ==> $resolved ($filename)\n";
188 # Retrieves the annotation
190 if (stat($filename)) {
191 print "Using local copy for the types\n" if ($mode eq "types");
192 print "Using local copy for the ann\n" if ($mode eq "ann");
194 while(<FD>) { $cont .= $_; }
197 print "Downloading the types\n" if ($mode eq "types");
198 print "Downloading the ann\n" if ($mode eq "ann");
199 $ua = LWP::UserAgent->new;
200 $request = HTTP::Request->new(GET => "$resolved");
201 $response = $ua->request($request, \&callback);
203 print "Storing file for the types\n" if ($mode eq "types");
204 print "Storing file for the ann\n" if ($mode eq "ann");
206 open(FD, ">".$filename);
210 my $annotation = $cont;
212 # Retrieves the target to annotate
217 if (stat($filename_target)) {
218 print "Using local copy for the file to type\n";
219 open(FD, $filename_target);
220 while(<FD>) { $cont .= $_; }
223 print "Downloading the file to type\n";
224 $ua = LWP::UserAgent->new;
225 $request = HTTP::Request->new(GET => "$resolved_target");
226 $response = $ua->request($request, \&callback);
228 print "Storing file for the file to type\n";
229 mkdirs($filename_target);
230 open(FD, ">".$filename_target);
236 $target = $annotation;
240 # Merging the annotation and the target
242 $target =~ s/<\?xml [^?]*\?>//sg;
243 $target =~ s/<!DOCTYPE [^>]*>//sg;
244 $annotation =~ s/<\?xml [^?]*\?>//sg;
245 $annotation =~ s/<!DOCTYPE [^>]*>//sg;
246 my $element, $endelement;
247 if ($mode eq "types") {
248 $element = "<ALLTYPES>";
249 $endelement = "</ALLTYPES>";
250 } elsif ($mode eq "ann") {
255 <?xml version="1.0" encoding="UTF-8"?>
256 <cicxml uri="$target_to_annotate">
264 # Answering the client
266 my $res = new HTTP::Response;
267 $res->content($merged);
268 $c->send_response($res);
269 } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
270 my $filename = $cicuri;
271 $filename = $helm_dir."/dtd/".$filename;
272 print "DTD: $cicuri ==> ($filename)\n";
273 if (stat($filename)) {
274 print "Using local copy\n";
276 while(<FD>) { $cont .= $_; }
278 my $res = new HTTP::Response;
279 $res->content($cont);
280 $c->send_response($res);
282 die "Could not find DTD!";
284 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
285 my $quoted_html_link = $html_link;
286 $quoted_html_link =~ s/&/&/g;
287 $quoted_html_link =~ s/</</g;
288 $quoted_html_link =~ s/>/>/g;
289 $quoted_html_link =~ s/'/'/g;
290 $quoted_html_link =~ s/"/"/g;
291 print "Configuration requested, returned #$quoted_html_link#\n";
292 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
293 my $res = new HTTP::Response;
294 $res->content($cont);
295 $c->send_response($res);
297 print "INVALID REQUEST!!!!!\n";
298 $c->send_error(RC_FORBIDDEN)
303 print "\nCONNECTION CLOSED\n\n";
308 #================================
316 # Does not raise errors if could not create dirs/files
318 # Too much powerful: creates even /home, /home/users/, ...
322 my @dirs = split /\//,$pathname;
324 foreach $dir (@dirs) {
325 $tmp = ((defined($tmp)) ? $tmp = $tmp."\/".$dir : "");