]> 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 b2d58c5542aa52330af2931610d5aa204bf4f001..ef0417ce63991911b5464185981b92bda3aca8da 100755 (executable)
@@ -47,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,
@@ -274,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 {
@@ -691,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();
@@ -748,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});
 
@@ -1714,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).
 
@@ -1740,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 {