3 # next require defines: $helm_dir, $html_link
4 require "/usr/lib/helm/configuration.pl";
12 my $d = new HTTP::Daemon LocalPort => 8081;
13 tie(%map, 'DB_File', 'urls_of_uris.db', O_RDONLY, 0664);
14 print "Please contact me at: <URL:", $d->url, ">\n";
15 print "helm_dir: $helm_dir\n";
16 $SIG{CHLD} = "IGNORE"; # do not accumulate defunct processes
17 while (my $c = $d->accept) {
19 while (my $r = $c->get_request) {
20 #CSC: mancano i controlli di sicurezza
24 $cicuri =~ s/^[^?]*\?url=(.*)/$1/;
25 print "*".$r->url."\n";
26 my $http_method = $r->method;
27 my $http_path = $r->url->path;
28 if ($http_method eq 'GET' and $http_path eq "/get") {
29 my $filename = $cicuri;
30 $filename =~ s/cic:(.*)/$1/;
31 $filename =~ s/theory:(.*)/$1/;
32 $filename = $helm_dir.$filename.".xml";
33 my $resolved = $map{$cicuri};
34 print "$cicuri ==> $resolved ($filename)\n";
35 if (stat($filename)) {
36 print "Using local copy\n";
38 while(<FD>) { $cont .= $_; }
40 my $res = new HTTP::Response;
42 $c->send_response($res);
44 print "Downloading\n";
45 $ua = LWP::UserAgent->new;
46 $request = HTTP::Request->new(GET => "$resolved");
47 $response = $ua->request($request, \&callback);
49 print "Storing file\n";
54 my $res = new HTTP::Response;
56 $c->send_response($res);
58 } elsif ($http_method eq 'GET' and $http_path eq "/annotate") {
59 my $do_annotate = ($cicuri =~ /\.ann$/);
60 my $target_to_annotate = $cicuri;
61 $target_to_annotate =~ s/(.*)\.ann$/$1/ if $do_annotate;
62 my $filename = $cicuri;
63 $filename =~ s/cic:(.*)/$1/;
64 $filename =~ s/theory:(.*)/$1/;
65 my $filename_target = $helm_dir.$filename if $do_annotate;
66 $filename = $helm_dir.$filename.".xml";
67 $filename_target =~ s/(.*)\.ann$/$1.xml/ if $do_annotate;
68 my $resolved = $map{$cicuri};
69 my $resolved_target = $map{$target_to_annotate} if $do_annotate;
71 print "($cicuri, $target_to_annotate) ==> ($resolved + $resolved_target) ($filename)\n";
72 } elsif ($http_method eq 'GET' and $http_path eq "/getwithtypes") {
73 my $do_annotate = ($cicuri =~ /\.types$/);
74 my $target_to_annotate = $cicuri;
75 $target_to_annotate =~ s/(.*)\.types$/$1/ if $do_annotate;
76 my $filename = $cicuri;
77 $filename =~ s/cic:(.*)/$1/;
78 $filename =~ s/theory:(.*)/$1/;
79 my $filename_target = $helm_dir.$filename if $do_annotate;
80 $filename = $helm_dir.$filename.".xml";
81 $filename_target =~ s/(.*)\.types$/$1.xml/ if $do_annotate;
82 my $resolved = $map{$cicuri};
83 my $resolved_target = $map{$target_to_annotate} if $do_annotate;
85 print "($cicuri, $target_to_annotate) ==> ($resolved + $resolved_target) ($filename)\n";
87 print "$cicuri ==> $resolved ($filename)\n";
90 # Retrieves the annotation
92 if (stat($filename)) {
93 print "Using local copy for the types\n";
95 while(<FD>) { $cont .= $_; }
98 print "Downloading the types\n";
99 $ua = LWP::UserAgent->new;
100 $request = HTTP::Request->new(GET => "$resolved");
101 $response = $ua->request($request, \&callback);
103 print "Storing file for the types\n";
108 my $annotation = $cont;
110 # Retrieves the target to annotate
114 if (stat($filename_target)) {
115 print "Using local copy for the file to type\n";
116 open(FD, $filename_target);
117 while(<FD>) { $cont .= $_; }
120 print "Downloading the file to type\n";
121 $ua = LWP::UserAgent->new;
122 $request = HTTP::Request->new(GET => "$resolved_target");
123 $response = $ua->request($request, \&callback);
125 print "Storing file for the file to type\n";
126 open(FD, $filename_target);
133 # Merging the annotation and the target
135 $target =~ s/<\?xml [^?]*\?>//sg;
136 $target =~ s/<!DOCTYPE [^>]*>//sg;
137 $annotation =~ s/<\?xml [^?]*\?>//sg;
138 $annotation =~ s/<!DOCTYPE [^>]*>//sg;
140 <?xml version="1.0" encoding="UTF-8"?>
141 <cicxml uri="$target_to_annotate">
149 # Answering the client
151 my $res = new HTTP::Response;
152 $res->content($merged);
153 $c->send_response($res);
154 } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
155 my $filename = $cicuri;
156 $filename = $helm_dir."/dtd/".$filename;
157 print "DTD: $cicuri ==> ($filename)\n";
158 if (stat($filename)) {
159 print "Using local copy\n";
161 while(<FD>) { $cont .= $_; }
163 my $res = new HTTP::Response;
164 $res->content($cont);
165 $c->send_response($res);
167 die "Could not find DTD!";
169 } elsif ($http_method eq 'GET' and $http_path eq "/conf") {
170 my $quoted_html_link = $html_link;
171 $quoted_html_link =~ s/&/&/g;
172 $quoted_html_link =~ s/</</g;
173 $quoted_html_link =~ s/>/>/g;
174 $quoted_html_link =~ s/'/'/g;
175 $quoted_html_link =~ s/"/"/g;
176 print "Configuration requested, returned #$quoted_html_link#\n";
177 $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
178 my $res = new HTTP::Response;
179 $res->content($cont);
180 $c->send_response($res);
182 print "INVALID REQUEST!!!!!\n";
183 $c->send_error(RC_FORBIDDEN)
188 print "\nCONNECTION CLOSED\n\n";
193 #================================