3 # next require defines: $helm_dir, $html_link
5 # require "/usr/lib/helm/configuration.pl";
6 require "/home/cadet/sacerdot/local/lib/helm/configuration.pl";
14 my $d = new HTTP::Daemon LocalPort => 8081;
15 tie(%map, 'DB_File', 'urls_of_uris.db', O_RDONLY, 0664);
16 print "Please contact me at: <URL:", $d->url, ">\n";
17 print "helm_dir: $helm_dir\n";
18 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
19 while (my $c = $d->accept) {
21 while (my $r = $c->get_request) {
22 #CSC: mancano i controlli di sicurezza
26 $cicuri =~ s/^[^?]*\?url=(.*)/$1/;
27 print "*".$r->url."\n";
28 my $http_method = $r->method;
29 my $http_path = $r->url->path;
30 if ($http_method eq 'GET' and $http_path eq "/get") {
31 my $filename = $cicuri;
32 $filename =~ s/cic:(.*)/$1/;
33 $filename =~ s/theory:(.*)/$1/;
34 $filename = $helm_dir.$filename.".xml";
35 my $resolved = $map{$cicuri};
36 print "$cicuri ==> $resolved ($filename)\n";
37 if (stat($filename)) {
38 print "Using local copy\n";
40 while(<FD>) { $cont .= $_; }
42 my $res = new HTTP::Response;
44 $c->send_response($res);
46 print "Downloading\n";
47 $ua = LWP::UserAgent->new;
48 $request = HTTP::Request->new(GET => "$resolved");
49 $response = $ua->request($request, \&callback);
51 print "Storing file\n";
56 my $res = new HTTP::Response;
58 $c->send_response($res);
60 } elsif ($http_method eq 'GET' and $http_path eq "/annotate") {
61 my $do_annotate = ($cicuri =~ /\.ann$/);
62 my $target_to_annotate = $cicuri;
63 $target_to_annotate =~ s/(.*)\.ann$/$1/ if $do_annotate;
64 my $filename = $cicuri;
65 $filename =~ s/cic:(.*)/$1/;
66 $filename =~ s/theory:(.*)/$1/;
67 my $filename_target = $helm_dir.$filename if $do_annotate;
68 $filename = $helm_dir.$filename.".xml";
69 $filename_target =~ s/(.*)\.ann$/$1.xml/ if $do_annotate;
70 my $resolved = $map{$cicuri};
71 my $resolved_target = $map{$target_to_annotate} if $do_annotate;
73 print "($cicuri, $target_to_annotate) ==> ($resolved + $resolved_target) ($filename)\n";
75 print "$cicuri ==> $resolved ($filename)\n";
78 # Retrieves the annotation
80 if (stat($filename)) {
81 print "Using local copy for the annotation\n";
83 while(<FD>) { $cont .= $_; }
86 print "Downloading the annotation\n";
87 $ua = LWP::UserAgent->new;
88 $request = HTTP::Request->new(GET => "$resolved");
89 $response = $ua->request($request, \&callback);
91 print "Storing file for the annotation\n";
96 my $annotation = $cont;
98 # Retrieves the target to annotate
102 if (stat($filename_target)) {
103 print "Using local copy for the file to annotate\n";
104 open(FD, $filename_target);
105 while(<FD>) { $cont .= $_; }
108 print "Downloading the file to annotate\n";
109 $ua = LWP::UserAgent->new;
110 $request = HTTP::Request->new(GET => "$resolved_target");
111 $response = $ua->request($request, \&callback);
113 print "Storing file for the file to annotate\n";
114 open(FD, $filename_target);
121 # Merging the annotation and the target
123 $target =~ s/<\?xml [^?]*\?>//sg;
124 $target =~ s/<!DOCTYPE [^>]*>//sg;
125 $annotation =~ s/<\?xml [^?]*\?>//sg;
126 $annotation =~ s/<!DOCTYPE [^>]*>//sg;
128 <?xml version="1.0" encoding="UTF-8"?>
129 <cicxml uri="$target_to_annotate">
135 # Answering the client
137 my $res = new HTTP::Response;
138 $res->content($merged);
139 $c->send_response($res);
140 } elsif ($http_method eq 'GET' and $http_path eq "/getwithtypes") {
141 my $do_annotate = ($cicuri =~ /\.types$/);
142 my $target_to_annotate = $cicuri;
143 $target_to_annotate =~ s/(.*)\.types$/$1/ if $do_annotate;
144 my $filename = $cicuri;
145 $filename =~ s/cic:(.*)/$1/;
146 $filename =~ s/theory:(.*)/$1/;
147 my $filename_target = $helm_dir.$filename if $do_annotate;
148 $filename = $helm_dir.$filename.".xml";
149 $filename_target =~ s/(.*)\.types$/$1.xml/ if $do_annotate;
150 my $resolved = $map{$cicuri};
151 my $resolved_target = $map{$target_to_annotate} if $do_annotate;
153 print "GETWITHTYPES!!\n";
154 print "($cicuri, $target_to_annotate) ==> ($resolved + $resolved_target) ($filename)\n";
156 print "$cicuri ==> $resolved ($filename)\n";
159 # Retrieves the annotation
161 if (stat($filename)) {
162 print "Using local copy for the types\n";
164 while(<FD>) { $cont .= $_; }
167 print "Downloading the types\n";
168 $ua = LWP::UserAgent->new;
169 $request = HTTP::Request->new(GET => "$resolved");
170 $response = $ua->request($request, \&callback);
172 print "Storing file for the types\n";
177 my $annotation = $cont;
179 # Retrieves the target to annotate
184 if (stat($filename_target)) {
185 print "Using local copy for the file to type\n";
186 open(FD, $filename_target);
187 while(<FD>) { $cont .= $_; }
190 print "Downloading the file to type\n";
191 $ua = LWP::UserAgent->new;
192 $request = HTTP::Request->new(GET => "$resolved_target");
193 $response = $ua->request($request, \&callback);
195 print "Storing file for the file to type\n";
196 open(FD, $filename_target);
202 $target = $annotation;
206 # Merging the annotation and the target
208 $target =~ s/<\?xml [^?]*\?>//sg;
209 $target =~ s/<!DOCTYPE [^>]*>//sg;
210 $annotation =~ s/<\?xml [^?]*\?>//sg;
211 $annotation =~ s/<!DOCTYPE [^>]*>//sg;
213 <?xml version="1.0" encoding="UTF-8"?>
214 <cicxml uri="$target_to_annotate">
222 # Answering the client
224 my $res = new HTTP::Response;
225 $res->content($merged);
226 $c->send_response($res);
227 } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
228 my $filename = $cicuri;
229 $filename = $helm_dir."/dtd/".$filename;
230 print "DTD: $cicuri ==> ($filename)\n";
231 if (stat($filename)) {
232 print "Using local copy\n";
234 while(<FD>) { $cont .= $_; }
236 my $res = new HTTP::Response;
237 $res->content($cont);
238 $c->send_response($res);
240 die "Could not find DTD!";
242 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
243 my $quoted_html_link = $html_link;
244 $quoted_html_link =~ s/&/&/g;
245 $quoted_html_link =~ s/</</g;
246 $quoted_html_link =~ s/>/>/g;
247 $quoted_html_link =~ s/'/'/g;
248 $quoted_html_link =~ s/"/"/g;
249 print "Configuration requested, returned #$quoted_html_link#\n";
250 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
251 my $res = new HTTP::Response;
252 $res->content($cont);
253 $c->send_response($res);
255 print "INVALID REQUEST!!!!!\n";
256 $c->send_error(RC_FORBIDDEN)
261 print "\nCONNECTION CLOSED\n\n";
266 #================================