]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/http_getter/http_getter.pl
http_getter reimplemented from scratch
[helm.git] / helm / http_getter / http_getter.pl
index 884bbc307d97fd553b1417b32cd740980b8285bf..ccc8a82b81f6deece6e8161c0cd85e313b715bc5 100755 (executable)
@@ -16,7 +16,10 @@ use HTTP::Request;
 use LWP::UserAgent;
 use DB_File;
 
-my $cont = "";
+#CSC: mancano i controlli sulle condizioni di errore di molte funzioni
+#CSC: ==> non e' robusto
+#CSC: altra roba da sistemare segnata con CSC
+
 my $d = new HTTP::Daemon LocalPort => 8081;
 tie(%map, 'DB_File', $uris_dbm.".db", O_RDONLY, 0664);
 print "Please contact me at: <URL:", $d->url, ">\n";
@@ -29,256 +32,103 @@ while (my $c = $d->accept) {
     while (my $r = $c->get_request) {
         #CSC: mancano i controlli di sicurezza
         
-        $cont = "";
-        my $cicuri = $r->url; 
-        $cicuri =~ s/^[^?]*\?url=(.*)/$1/;
-        print "*".$r->url."\n";
+        my $inputuri = $r->url; 
+        $inputuri =~ s/^[^?]*\?uri=(.*)/$1/;
+        print "\nRequest: ".$r->url."\n\n";
         my $http_method = $r->method;
         my $http_path = $r->url->path;
-        if ($http_method eq 'GET' and $http_path eq "/get") {
-            my $filename = $cicuri;
-            $filename =~ s/cic:(.*)/$1/;
-            $filename =~ s/theory:(.*)/$1/;
-            $filename = $helm_dir.$filename.".xml";
-            my $resolved = $map{$cicuri};
-            print "$cicuri ==> $resolved ($filename)\n";
-            if (stat($filename)) {
-               print "Using local copy\n";
-               open(FD, $filename);
-               while(<FD>) { $cont .= $_; }
-               close(FD);
-               my $res = new HTTP::Response;
-               $res->content($cont);
-               $c->send_response($res);
-            } else {
-               print "Downloading\n";
-               $ua = LWP::UserAgent->new;
-               $request = HTTP::Request->new(GET => "$resolved");
-               $response = $ua->request($request, \&callback);
-               
-               print "Storing file\n";
-               mkdirs($filename);
-               open(FD, ">".$filename);
-               print FD $cont;
-               close(FD);
 
-               my $res = new HTTP::Response;
-               $res->content($cont);
-               $c->send_response($res);
-            }
-        } elsif ($http_method eq 'GET' and $http_path eq "/annotate") {
-            my $do_annotate = ($cicuri =~ /\.ann$/);
-            my $target_to_annotate = $cicuri;
-            $target_to_annotate =~ s/(.*)\.ann$/$1/ if $do_annotate;
-            my $filename = $cicuri;
-            $filename =~ s/cic:(.*)/$1/;
-            $filename =~ s/theory:(.*)/$1/;
-            my $filename_target = $helm_dir.$filename if $do_annotate;
-            $filename = $helm_dir.$filename.".xml";
-            $filename_target =~ s/(.*)\.ann$/$1.xml/ if $do_annotate;
-            my $resolved = $map{$cicuri};
-            my $resolved_target = $map{$target_to_annotate} if $do_annotate;
-            if ($do_annotate) {
-               print "($cicuri, $target_to_annotate) ==> ($resolved + $resolved_target) ($filename)\n";
-            } else {
-               print "$cicuri ==> $resolved ($filename)\n";
-            }
-
-            # Retrieves the annotation
-
-            if (stat($filename)) {
-               print "Using local copy for the annotation\n";
-               open(FD, $filename);
-               while(<FD>) { $cont .= $_; }
-               close(FD);
-            } else {
-               print "Downloading the annotation\n";
-               $ua = LWP::UserAgent->new;
-               $request = HTTP::Request->new(GET => "$resolved");
-               $response = $ua->request($request, \&callback);
-               
-               print "Storing file for the annotation\n";
-               mkdirs($filename);
-               open(FD, ">".$filename);
-               print FD $cont;
-               close(FD);
-            }
-            my $annotation = $cont;
+        if ($http_method eq 'GET' and $http_path eq "/getciconly") {
+            # finds the uri, url and filename
+            my $cicuri = $inputuri;
 
-            # Retrieves the target to annotate
+            my $cicfilename = $cicuri;
+            $cicfilename =~ s/cic:(.*)/$1/;
+            $cicfilename =~ s/theory:(.*)/$1/;
+            $cicfilename = $helm_dir.$cicfilename.".xml";
 
-            $cont = "";
-            if ($do_annotate) {
-               if (stat($filename_target)) {
-                  print "Using local copy for the file to annotate\n";
-                  open(FD, $filename_target);
-                  while(<FD>) { $cont .= $_; }
-                  close(FD);
-               } else {
-                  print "Downloading the file to annotate\n";
-                  $ua = LWP::UserAgent->new;
-                  $request = HTTP::Request->new(GET => "$resolved_target");
-                  $response = $ua->request($request, \&callback);
-               
-                  print "Storing file for the file to annotate\n";
-                  mkdirs($filename_target);
-                  open(FD, ">".$filename_target);
-                  print FD $cont;
-                  close(FD);
-               }
-            }
-            my $target = $cont;
+            my $cicurl   = $map{$cicuri};
 
-            # Merging the annotation and the target
+            print_request("cic",$cicuri,$cicurl,$cicfilename);
 
-            $target =~ s/<\?xml [^?]*\?>//sg;
-            $target =~ s/<!DOCTYPE [^>]*>//sg;
-            $annotation =~ s/<\?xml [^?]*\?>//sg;
-            $annotation =~ s/<!DOCTYPE [^>]*>//sg;
-            my $merged = <<EOT;
-<?xml version="1.0" encoding="UTF-8"?>
-<cicxml uri="$target_to_annotate">
-$target
-$annotation
-</cicxml>
-EOT
+            # Retrieves the file
+            my $ciccontent = download(0,"cic",$cicurl,$cicfilename);
 
             # Answering the client
-
-            my $res = new HTTP::Response;
-            $res->content($merged);
-            $c->send_response($res);
-        } elsif ($http_method eq 'GET' and $http_path eq "/getwithtypes") {
-            my $mode;
-            my $do_annotate;
-            if ($cicuri =~ /\.types$/) {
-               $do_annotate = 1;
-               $mode = "types";
-            } elsif ($cicuri =~ /\.ann$/) {
-               $do_annotate = 1;
-               $mode = "ann";
+            answer($c,$ciccontent);
+        } elsif ($http_method eq 'GET' and $http_path eq "/get") {
+            # finds the uris, urls and filenames
+            my $cicuri = $inputuri,
+               $typesuri = $inputuri,
+               $annuri = $inputuri;
+            my $annsuffix;
+            if ($inputuri =~ /\.types$/) {
+               $cicuri    =~ s/(.*)\.types$/$1/;
+               undef($annuri);
+            } elsif ($inputuri =~ /\.types\.ann$/) {
+               $cicuri    =~ s/(.*)\.types\.ann$/$1/;
+               $typesuri  =~ s/(.*)\.ann$/$1/;
+               $annsuffix = ".types.ann";
+            } elsif ($inputuri =~ /\.ann$/) {
+               $cicuri  =~ s/(.*)\.ann$/$1/;
+               undef($typesuri);
+               $annsuffix = ".ann";
             } else {
-               $do_annotate = 0;
-            }
-            my $target_to_annotate = $cicuri;
-            if ($mode eq "types") {
-               $target_to_annotate =~ s/(.*)\.types$/$1/;
-            } elsif ($mode eq "ann") {
-               $target_to_annotate =~ s/(.*)\.ann$/$1/;
-            }
-            my $filename = $cicuri;
-            $filename =~ s/cic:(.*)/$1/;
-            $filename =~ s/theory:(.*)/$1/;
-            my $filename_target = $helm_dir.$filename if $do_annotate;
-            $filename = $helm_dir.$filename.".xml";
-            if ($mode eq "types") {
-               $filename_target =~ s/(.*)\.types$/$1.xml/;
-            } elsif ($mode eq "ann") {
-               $filename_target =~ s/(.*)\.ann$/$1.xml/;
-            }
-            my $resolved = $map{$cicuri};
-            my $resolved_target = $map{$target_to_annotate} if $do_annotate;
-            if ($do_annotate) {
-               print "GETWITHTYPES!!\n" if ($mode eq "types");
-               print "GETWITHANN!!\n" if ($mode eq "ann");
-               print "($cicuri, $target_to_annotate) ==> ($resolved + $resolved_target) ($filename)\n";
-             } else {
-               print "$cicuri ==> $resolved ($filename)\n";
+               undef($typesuri);
+               undef($annuri);
             }
 
-            # Retrieves the annotation
+            my $cicfilename = $cicuri;
+            $cicfilename =~ s/cic:(.*)/$1/;
+            $cicfilename =~ s/theory:(.*)/$1/;
+            $cicfilename = $helm_dir.$cicfilename;
 
-            if (stat($filename)) {
-               print "Using local copy for the types\n" if ($mode eq "types");
-               print "Using local copy for the ann\n" if ($mode eq "ann");
-               open(FD, $filename);
-               while(<FD>) { $cont .= $_; }
-               close(FD);
-            } else {
-               print "Downloading the types\n" if ($mode eq "types");
-               print "Downloading the ann\n" if ($mode eq "ann");
-               $ua = LWP::UserAgent->new;
-               $request = HTTP::Request->new(GET => "$resolved");
-               $response = $ua->request($request, \&callback);
-               
-               print "Storing file for the types\n" if ($mode eq "types");
-               print "Storing file for the ann\n" if ($mode eq "ann");
-               mkdirs($filename);
-               open(FD, ">".$filename);
-               print FD $cont;
-               close(FD);
-            }
-            my $annotation = $cont;
+            my $typesfilename = $cicfilename.".types.xml"     if $typesuri;
+            my $annfilename  = $cicfilename.$annsuffix.".xml" if $annuri;
+            $cicfilename .= ".xml";
 
-            # Retrieves the target to annotate
+            my $cicurl   = $map{$cicuri};
+            my $typesurl = $map{$typesuri} if $typesuri;
+            my $annurl   = $map{$annuri}  if $annuri;
 
-            $cont = "";
-            my $target;
-            if ($do_annotate) {
-               if (stat($filename_target)) {
-                  print "Using local copy for the file to type\n";
-                  open(FD, $filename_target);
-                  while(<FD>) { $cont .= $_; }
-                  close(FD);
-               } else {
-                  print "Downloading the file to type\n";
-                  $ua = LWP::UserAgent->new;
-                  $request = HTTP::Request->new(GET => "$resolved_target");
-                  $response = $ua->request($request, \&callback);
-               
-                  print "Storing file for the file to type\n";
-                  mkdirs($filename_target);
-                  open(FD, ">".$filename_target);
-                  print FD $cont;
-                  close(FD);
-               }
-               $target = $cont;
-            } else {
-               $target = $annotation;
-               $annotation = "";
-            }
+            print_request("cic",$cicuri,$cicurl,$cicfilename);
+            print_request("types",$typesuri,$typesurl,$typesfilename)
+             if ($typesuri);
+            print_request("ann",$annuri,$annurl,$annfilename)
+             if ($annuri);
 
-            # Merging the annotation and the target
+            # Retrieves the files
+
+            my $ciccontent = download(1,"cic",$cicurl,$cicfilename);
+            my $typescontent =
+             download(1,"types",$typesurl,$typesfilename) if ($typesuri);
+            my $anncontent =
+             download(1,"ann",$annurl,$annfilename) if ($annuri);
+
+            # Merging the files together
 
-            $target =~ s/<\?xml [^?]*\?>//sg;
-            $target =~ s/<!DOCTYPE [^>]*>//sg;
-            $annotation =~ s/<\?xml [^?]*\?>//sg;
-            $annotation =~ s/<!DOCTYPE [^>]*>//sg;
-            my $element, $endelement; 
-            if ($mode eq "types") {
-               $element = "<ALLTYPES>";
-               $endelement = "</ALLTYPES>";
-            } elsif ($mode eq "ann") {
-               $element = "";
-               $endelement = "";
-            }
             my $merged = <<EOT;
 <?xml version="1.0" encoding="UTF-8"?>
-<cicxml uri="$target_to_annotate">
-$target
-$element
-$annotation
-$endelement
+<cicxml uri="$cicuri">
+$ciccontent
+$typescontent
+$anncontent
 </cicxml>
 EOT
 
             # Answering the client
-
-            my $res = new HTTP::Response;
-            $res->content($merged);
-            $c->send_response($res);
+            answer($c,$merged);
          } elsif ($http_method eq 'GET' and $http_path eq "/getdtd") {
-            my $filename = $cicuri;
+            my $filename = $inputuri;
             $filename = $dtd_dir."/".$filename;
-            print "DTD: $cicuri ==> ($filename)\n";
+            print "DTD: $inputuri ==> ($filename)\n";
             if (stat($filename)) {
                print "Using local copy\n";
                open(FD, $filename);
+               $cont = "";
                while(<FD>) { $cont .= $_; }
                close(FD);
-               my $res = new HTTP::Response;
-               $res->content($cont);
-               $c->send_response($res);
+               answer($c,$cont);
             } else {
                die "Could not find DTD!";
             }
@@ -289,15 +139,14 @@ EOT
             $quoted_html_link =~ s/>/&gt;/g;
             $quoted_html_link =~ s/'/&apos;/g;
             $quoted_html_link =~ s/"/&quot;/g;
-            print "Configuration requested, returned #$quoted_html_link#\n";
+            print "\nConfiguration requested, returned #$quoted_html_link#\n";
            $cont = "<?xml version=\"1.0\"?><html_link>$quoted_html_link</html_link>";
-            my $res = new HTTP::Response;
-            $res->content($cont);
-            $c->send_response($res);
+            answer($c,$cont);
         } else {
-            print "INVALID REQUEST!!!!!\n";
+            print "\nINVALID REQUEST!!!!!\n";
             $c->send_error(RC_FORBIDDEN)
         }
+        print "\nRequest solved: ".$r->url."\n\n";
     }
     $c->close;
     undef($c);
@@ -308,23 +157,67 @@ EOT
 
 #================================
 
-sub callback
-{
- my ($data) = @_;
- $cont .= $data;
-}
 
-# Does not raise errors if could not create dirs/files
-
-# Too much powerful: creates even /home, /home/users/, ...
+#CSC: Too much powerful: creates even /home, /home/users/, ...
+#CSC: Does not raise errors if could not create dirs/files
 sub mkdirs
 {
  my ($pathname) = @_;
  my @dirs = split /\//,$pathname;
  my $tmp;
  foreach $dir (@dirs) {
-  $tmp = ((defined($tmp)) ?  $tmp = $tmp."\/".$dir : "");
+  $tmp = ((defined($tmp)) ?  $tmp."\/".$dir : "");
   mkdir($tmp,0777);
  }
  rmdir($tmp);
 }
+
+sub print_request
+{
+ my ($str,$uri,$url,$filename) = @_;
+ print $str."uri: $uri\n";
+ print $str."url: $url\n";
+ print $str."filename: $filename\n\n";
+}
+
+sub callback
+{
+ my ($data) = @_;
+ $cont .= $data;
+}
+
+sub download
+{
+ my ($remove_headers,$str,$url,$filename) = @_;
+ $cont = ""; # modified by side-effect by the callback function
+ if (stat($filename)) {
+    print "Using local copy for the $str file\n";
+    open(FD, $filename);
+    while(<FD>) { $cont .= $_; }
+    close(FD);
+ } else {
+    print "Downloading the $str file\n";
+    $ua = LWP::UserAgent->new;
+    $request = HTTP::Request->new(GET => "$url");
+    $response = $ua->request($request, \&callback);
+               
+    print "Storing the $str file\n";
+    mkdirs($filename);
+    open(FD, ">".$filename);
+    print FD $cont;
+    close(FD);
+ }
+ if ($remove_headers) {
+    $cont =~ s/<\?xml [^?]*\?>//sg;
+    $cont =~ s/<!DOCTYPE [^>]*>//sg;
+ }
+ return $cont;
+}
+
+sub answer
+{
+ my ($c,$cont) = @_;
+ my $res = new HTTP::Response;
+ $res->content($cont);
+ $c->send_response($res);
+}