use LWP::UserAgent;
use DB_File;
use Compress::Zlib;
+use CGI;
use URI::Escape;
#CSC: mancano i controlli sulle condizioni di errore di molte funzioni
my $http_method = $r->method;
my $http_path = $r->url->path;
my $http_query = uri_unescape($r->url->query);
+ my $cgi = new CGI($http_query);
print "\nUnescaped query: ".$http_query."\n";
answer($c,"<html><body><h1>Update done</h1></body></html>");
} elsif ($http_method eq 'GET' and $http_path eq "/ls") {
# send back keys that begin with a given uri
- my $baseuri = $http_query;
- $baseuri =~ s/^.*baseuri=(.*)&.*$/$1/;
+ my ($uritype,$uripattern,$uriprefix);
+ my $baseuri = $cgi->param('baseuri');
chop $baseuri if ($baseuri =~ /.*\/$/); # remove trailing "/"
- my $outype = $http_query; # output type, might be 'txt' or 'xml'
- $outype =~ s/^.*&type=(.*)$/$1/;
- if (($outype ne 'txt') and ($outype ne 'xml')) { # invalid out type
- print "Invalid output type specified: $outype\n";
- answer($c,"<html><body><h1>Invalid output type, may be ".
- "\"txt\" or \"xml\"</h1></body></html>");
- } else { # valid output type
- print "BASEURI $baseuri, TYPE $outype\n";
- my $key;
- $cont = "";
- $cont .= "<urilist>\n" if ($outype eq "xml");
- foreach (keys(%map)) { # search for uri that begin with $baseuri
- if ($_ =~ /^$baseuri\//) {
- $cont .= "<uri>" if ($outype eq "xml");
- $cont .= $_;
- $cont .= "\n" if ($outype eq "txt");
- $cont .= "</uri>\n" if ($outype eq "xml");
- }
+ my $outype = $cgi->param('format'); # output type, might be 'txt' or 'xml'
+ $uripattern = $baseuri;
+ $uripattern =~ s/^.*:(.*)/$1/;
+ if ($baseuri =~ /^cic:/) {
+ $uritype = "cic";
+ } elsif ($baseuri =~ /^theory:/) {
+ $uritype = "theory";
+ } elsif ($baseuri =~ /^\*:/) {
+ $uritype = "any";
+ } else {
+ $uritype = "invalid";
+ }
+ if ($uritype ne "invalid") { # uri is valid
+ if (($outype ne 'txt') and ($outype ne 'xml')) { # invalid out type
+ print "Invalid output type specified: $outype\n";
+ answer($c,"<html><body><h1>Invalid output type, may be ".
+ "\"txt\" or \"xml\"</h1></body></html>");
+ } else { # valid output
+ print "BASEURI $baseuri, FORMAT $outype\n";
+ $cont = finduris($uritype,$uripattern,$outype);
+ answer($c,$cont);
}
- $cont .= "</urilist>" if ($outype eq "xml");
- answer($c,$cont);
+ } else { # invalid uri
+ print "Invalid uri: $baseuri, may begin with 'cic:', ".
+ "'theory:' or '*:'\n";
+ answer($c,"<html><body><h1>Invalid uri , may begin with ".
+ "\"cic:\", \"theory:\" or \"*:\"</h1></body></html>");
}
} elsif ($http_method eq 'GET' and $http_path eq "/version") {
print "Version requested!";
#================================
+sub finduris { # find uris for cic and theory trees generation
+ my ($uritype,$uripattern,$format) = @_;
+ my $content = "";
+ my ($uri,$localpart,$dirname);
+ my (@itemz,@already_pushed_dir);
+
+ print "FINDURIS, uritype: $uritype, uripattern: $uripattern, ".
+ "format: $format\n\n";
+
+ if (($uritype eq "cic") or ($uritype eq "theory")) {
+ # get info only of one type: cic or theory
+ foreach (keys(%map)) { # select matching uris
+ $uri = $_;
+ if ($uri =~ /^$uritype:$uripattern\//) {
+ $localpart = $uri;
+ $localpart =~ s/^$uritype:$uripattern\/(.*)/$1/;
+ if ($localpart =~ /^[^\/]*$/) { # no slash, i.e. no dir
+ push @itemz, "object," . $localpart;
+ } else { # exists at least one slash, i.e. a dir
+ ($dirname) = split (/\//, $localpart);
+ push @itemz, "dir," . $dirname;
+ #print "LOCALPART $localpart, DIRNAME $dirname\n"; #DEBUG
+ }
+ }
+ }
+ } elsif ($uritype eq "any") { # get info for both cic and theory
+ foreach (keys(%map)) {
+ $uri = $_;
+ }
+ } else {
+ die "getter internal error: unsupported uritype: \"$uritype\"";
+ }
+ @itemz = sort @itemz; # sort itemz and remove duplicates
+ my $lastitem = "";
+ if ($format eq "txt") { #now generate output
+ foreach (@itemz) {
+ next if ($_ eq $last);
+ $content .= ($_ . "\n");
+ $last = $_;
+ }
+ } elsif ($format eq "xml") {
+ $content .= "<ls>\n";
+ foreach (@itemz) {
+ next if ($_ eq $last);
+ $last = $_;
+ ($itemtype,$itemdata) = split(/,/, $_);
+ if ($itemtype eq "object") {
+ $content .= "<object type=\"$uritype\">$itemdata</object>\n";
+ } elsif ($itemtype eq "dir") {
+ $content .= "<section>$itemdata</section>\n";
+ }
+ }
+ $content .= "</ls>\n"
+ } else { # may not enter this branch
+ die "Getter internal error: invalid format received by finduris sub";
+ }
+ return $content;
+}
#CSC: Too much powerful: creates even /home, /home/users/, ...
#CSC: Does not raise errors if could not create dirs/files