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', 'urls_of_uris.db', O_RDONLY, 0664);
22 print "Please contact me at: <URL:", $d->url, ">\n";
23 print "helm_dir: $helm_dir\n";
24 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
25 while (my $c = $d->accept) {
27 while (my $r = $c->get_request) {
28 #CSC: mancano i controlli di sicurezza
32 $cicuri =~ s/^[^?]*\?url=(.*)/$1/;
33 print "*".$r->url."\n";
34 my $http_method = $r->method;
35 my $http_path = $r->url->path;
36 if ($http_method eq 'GET' and $http_path eq "/get") {
37 my $filename = $cicuri;
38 $filename =~ s/cic:(.*)/$1/;
39 $filename =~ s/theory:(.*)/$1/;
40 $filename = $helm_dir.$filename.".xml";
41 my $resolved = $map{$cicuri};
42 print "$cicuri ==> $resolved ($filename)\n";
43 if (stat($filename)) {
44 print "Using local copy\n";
46 while(<FD>) { $cont .= $_; }
48 my $res = new HTTP::Response;
50 $c->send_response($res);
52 print "Downloading\n";
53 $ua = LWP::UserAgent->new;
54 $request = HTTP::Request->new(GET => "$resolved");
55 $response = $ua->request($request, \&callback);
57 print "Storing file\n";
62 my $res = new HTTP::Response;
64 $c->send_response($res);
66 } elsif ($http_method eq 'GET' and $http_path eq "/annotate") {
67 my $do_annotate = ($cicuri =~ /\.ann$/);
68 my $target_to_annotate = $cicuri;
69 $target_to_annotate =~ s/(.*)\.ann$/$1/ if $do_annotate;
70 my $filename = $cicuri;
71 $filename =~ s/cic:(.*)/$1/;
72 $filename =~ s/theory:(.*)/$1/;
73 my $filename_target = $helm_dir.$filename if $do_annotate;
74 $filename = $helm_dir.$filename.".xml";
75 $filename_target =~ s/(.*)\.ann$/$1.xml/ if $do_annotate;
76 my $resolved = $map{$cicuri};
77 my $resolved_target = $map{$target_to_annotate} if $do_annotate;
79 print "($cicuri, $target_to_annotate) ==> ($resolved + $resolved_target) ($filename)\n";
81 print "$cicuri ==> $resolved ($filename)\n";
84 # Retrieves the annotation
86 if (stat($filename)) {
87 print "Using local copy for the annotation\n";
89 while(<FD>) { $cont .= $_; }
92 print "Downloading the annotation\n";
93 $ua = LWP::UserAgent->new;
94 $request = HTTP::Request->new(GET => "$resolved");
95 $response = $ua->request($request, \&callback);
97 print "Storing file for the annotation\n";
102 my $annotation = $cont;
104 # Retrieves the target to annotate
108 if (stat($filename_target)) {
109 print "Using local copy for the file to annotate\n";
110 open(FD, $filename_target);
111 while(<FD>) { $cont .= $_; }
114 print "Downloading the file to annotate\n";
115 $ua = LWP::UserAgent->new;
116 $request = HTTP::Request->new(GET => "$resolved_target");
117 $response = $ua->request($request, \&callback);
119 print "Storing file for the file to annotate\n";
120 open(FD, $filename_target);
127 # Merging the annotation and the target
129 $target =~ s/<\?xml [^?]*\?>//sg;
130 $target =~ s/<!DOCTYPE [^>]*>//sg;
131 $annotation =~ s/<\?xml [^?]*\?>//sg;
132 $annotation =~ s/<!DOCTYPE [^>]*>//sg;
134 <?xml version="1.0" encoding="UTF-8"?>
135 <cicxml uri="$target_to_annotate">
141 # Answering the client
143 my $res = new HTTP::Response;
144 $res->content($merged);
145 $c->send_response($res);
146 } elsif ($http_method eq 'GET' and $http_path eq "/getwithtypes") {
147 my $do_annotate = ($cicuri =~ /\.types$/);
148 my $target_to_annotate = $cicuri;
149 $target_to_annotate =~ s/(.*)\.types$/$1/ if $do_annotate;
150 my $filename = $cicuri;
151 $filename =~ s/cic:(.*)/$1/;
152 $filename =~ s/theory:(.*)/$1/;
153 my $filename_target = $helm_dir.$filename if $do_annotate;
154 $filename = $helm_dir.$filename.".xml";
155 $filename_target =~ s/(.*)\.types$/$1.xml/ if $do_annotate;
156 my $resolved = $map{$cicuri};
157 my $resolved_target = $map{$target_to_annotate} if $do_annotate;
159 print "GETWITHTYPES!!\n";
160 print "($cicuri, $target_to_annotate) ==> ($resolved + $resolved_target) ($filename)\n";
162 print "$cicuri ==> $resolved ($filename)\n";
165 # Retrieves the annotation
167 if (stat($filename)) {
168 print "Using local copy for the types\n";
170 while(<FD>) { $cont .= $_; }
173 print "Downloading the types\n";
174 $ua = LWP::UserAgent->new;
175 $request = HTTP::Request->new(GET => "$resolved");
176 $response = $ua->request($request, \&callback);
178 print "Storing file for the types\n";
183 my $annotation = $cont;
185 # Retrieves the target to annotate
190 if (stat($filename_target)) {
191 print "Using local copy for the file to type\n";
192 open(FD, $filename_target);
193 while(<FD>) { $cont .= $_; }
196 print "Downloading the file to type\n";
197 $ua = LWP::UserAgent->new;
198 $request = HTTP::Request->new(GET => "$resolved_target");
199 $response = $ua->request($request, \&callback);
201 print "Storing file for the file to type\n";
202 open(FD, $filename_target);
208 $target = $annotation;
212 # Merging the annotation and the target
214 $target =~ s/<\?xml [^?]*\?>//sg;
215 $target =~ s/<!DOCTYPE [^>]*>//sg;
216 $annotation =~ s/<\?xml [^?]*\?>//sg;
217 $annotation =~ s/<!DOCTYPE [^>]*>//sg;
219 <?xml version="1.0" encoding="UTF-8"?>
220 <cicxml uri="$target_to_annotate">
228 # Answering the client
230 my $res = new HTTP::Response;
231 $res->content($merged);
232 $c->send_response($res);
233 } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
234 my $filename = $cicuri;
235 $filename = $helm_dir."/dtd/".$filename;
236 print "DTD: $cicuri ==> ($filename)\n";
237 if (stat($filename)) {
238 print "Using local copy\n";
240 while(<FD>) { $cont .= $_; }
242 my $res = new HTTP::Response;
243 $res->content($cont);
244 $c->send_response($res);
246 die "Could not find DTD!";
248 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
249 my $quoted_html_link = $html_link;
250 $quoted_html_link =~ s/&/&/g;
251 $quoted_html_link =~ s/</</g;
252 $quoted_html_link =~ s/>/>/g;
253 $quoted_html_link =~ s/'/'/g;
254 $quoted_html_link =~ s/"/"/g;
255 print "Configuration requested, returned #$quoted_html_link#\n";
256 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
257 my $res = new HTTP::Response;
258 $res->content($cont);
259 $c->send_response($res);
261 print "INVALID REQUEST!!!!!\n";
262 $c->send_error(RC_FORBIDDEN)
267 print "\nCONNECTION CLOSED\n\n";
272 #================================