]> git.proxmox.com Git - pve-http-server.git/blobdiff - PVE/APIServer/AnyEvent.pm
allow API calls to download file contents.
[pve-http-server.git] / PVE / APIServer / AnyEvent.pm
index 99a97652a03e4c73380d3bf4d2dc1a4b95e17fa3..ef0417ce63991911b5464185981b92bda3aca8da 100755 (executable)
@@ -29,6 +29,7 @@ use AnyEvent::IO;
 use AnyEvent::HTTP;
 use Fcntl ();
 use Compress::Zlib;
+use Encode;
 use PVE::SafeSyslog;
 use PVE::INotify;
 use PVE::Tools;
@@ -46,7 +47,7 @@ use Data::Dumper;
 
 my $limit_max_headers = 30;
 my $limit_max_header_size = 8*1024;
-my $limit_max_post = 16*1024;
+my $limit_max_post = 64*1024;
 
 my $known_methods = {
     GET => 1,
@@ -273,15 +274,19 @@ my $file_extension_info = {
     css   => { ct => 'text/css' },
     html  => { ct => 'text/html' },
     js    => { ct => 'application/javascript' },
+    json  => { ct => 'application/json' },
     png   => { ct => 'image/png' , nocomp => 1 },
     ico   => { ct => 'image/x-icon', nocomp => 1},
     gif   => { ct => 'image/gif', nocomp => 1},
+    svg   => { ct => 'image/svg+xml' },
     jar   => { ct => 'application/java-archive', nocomp => 1},
     woff  => { ct => 'application/font-woff', nocomp => 1},
     woff2 => { ct => 'application/font-woff2', nocomp => 1},
     ttf   => { ct => 'application/font-snft', nocomp => 1},
     pdf   => { ct => 'application/pdf', nocomp => 1},
     epub  => { ct => 'application/epub+zip', nocomp => 1},
+    mp3   => { ct => 'audio/mpeg', nocomp => 1},
+    oga   => { ct => 'audio/ogg', nocomp => 1},
 };
 
 sub send_file_start {
@@ -617,6 +622,7 @@ sub proxy_request {
 }
 
 # return arrays as \0 separated strings (like CGI.pm)
+# assume data is UTF8 encoded
 sub decode_urlencoded {
     my ($data) = @_;
 
@@ -631,6 +637,8 @@ sub decode_urlencoded {
        $v =~s/\+/ /g;
        $v =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr(hex($1))/eg;
 
+       $v = Encode::decode('utf8', $v);
+
        if (defined(my $old = $res->{$k})) {
            $res->{$k} = "$old\0$v";
        } else {
@@ -655,7 +663,7 @@ sub extract_params {
        $params->{$k} = $query_params->{$k};
     }
 
-    return PVE::Tools::decode_utf8_parameters($params);
+    return $params;
 }
 
 sub handle_api2_request {
@@ -687,7 +695,7 @@ sub handle_api2_request {
 
        my $clientip = $reqstate->{peer_host};
 
-       my $res = $self->rest_handler($clientip, $method, $rel_uri, $auth, $params);
+       my $res = $self->rest_handler($clientip, $method, $rel_uri, $auth, $params, $format);
 
        # HACK: see Note 1
        Net::SSLeay::ERR_clear_error();
@@ -744,6 +752,13 @@ sub handle_api2_request {
            $delay = 0 if $delay < 0;
        }
 
+       if (defined(my $filename = $res->{download})) {
+           my $fh = IO::File->new($filename) ||
+               die "unable to open file '$filename' - $!\n";
+           send_file_start($self, $reqstate, $filename);
+           return;
+       }
+
        my ($raw, $ct, $nocomp) = $formatter->($res, $res->{data}, $params, $path,
                                               $auth, $self->{formatter_config});
 
@@ -1710,7 +1725,7 @@ sub auth_handler {
 }
 
 sub rest_handler {
-    my ($self, $clientip, $method, $rel_uri, $auth, $params) = @_;
+    my ($self, $clientip, $method, $rel_uri, $auth, $params, $format) = @_;
 
     # please do not raise exceptions here (always return a result).
 
@@ -1736,6 +1751,9 @@ sub rest_handler {
 
     # to pass the request to the local priviledged daemon use:
     # { proxy => 'localhost' , proxy_params => $params };
+
+    # to download aspecific file use:
+    # { download => "/path/to/file" };
 }
 
 sub check_cert_fingerprint {