]> git.proxmox.com Git - pmg-api.git/blobdiff - PMG/HTTPServer.pm
wrap and sort + debhelper 10 buildep
[pmg-api.git] / PMG / HTTPServer.pm
index 446d86affd523f373ea73525b30c9cf5644a48e2..6997dd5cc704da27b382b2b5dab169a1bd0360c3 100755 (executable)
@@ -27,7 +27,7 @@ sub new {
     my $class = ref($this) || $this;
 
     my $self = $class->SUPER::new(%args);
-    
+
     $self->{rpcenv} = PMG::RESTEnvironment->init(
        $self->{trusted_env} ? 'priv' : 'pub', atfork =>  sub { $self->atfork_handler() });
 
@@ -96,9 +96,10 @@ sub auth_handler {
 }
 
 sub rest_handler {
-    my ($self, $clientip, $method, $rel_uri, $auth, $params) = @_;
+    my ($self, $clientip, $method, $rel_uri, $auth, $params, $format) = @_;
 
     my $rpcenv = $self->{rpcenv};
+    $rpcenv->set_format($format);
 
     my $resp = {
        status => HTTP_NOT_IMPLEMENTED,
@@ -152,12 +153,22 @@ sub rest_handler {
            }
        }
 
+
+       my $result = $handler->handle($info, $uri_param);
+
        $resp = {
-           data => $handler->handle($info, $uri_param),
            info => $info, # useful to format output
            status => HTTP_OK,
        };
 
+       if ($info->{download}) {
+           die "download methods should have return type 'string' - internal error"
+               if ($info->{returns}->{type} ne 'string');
+           $resp->{download} = $result;
+       } else {
+           $resp->{data} = $result;
+       }
+
        if (my $count = $rpcenv->get_result_attrib('total')) {
            $resp->{total} = $count;
        }
@@ -170,6 +181,7 @@ sub rest_handler {
 
     $rpcenv->set_user(undef); # clear after request
     $rpcenv->set_role(undef); # clear after request
+    $rpcenv->set_format(undef); # clear after request
 
     if ($err) {
        $resp = { info => $info };