public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH manager] pvesh: decode streamed responses properly
Date: Tue, 28 Mar 2023 15:01:39 +0200	[thread overview]
Message-ID: <20230328130139.510340-1-c.heiss@proxmox.com> (raw)

This allows to use `pvesh` on endpoints like /nodes/{node}/journal,
which return streamed (and possibly gzip'd) responses.

Currently, e.g. `pvesh get /nodes/localhost/journal --lastentries 10`
fails with:

  gzip: stdout: Broken pipe
  got hash object, but result schema specified array!

(.. and similar messages for other output formats.)

This is due the API call returning a "download" object, which contains
(among some other things) a file handle to read the response from.

With this patch, the response from such endpoints is now correctly
read/displayed. Only handles combinations of `Content-Encoding` ==
'gzip' and either 'text/plain' or 'application/json' as `Content-Type`.
As far as I could see (aka. grep for it), only these combinations are
used currently.

Tested this with all four output formats 'text', 'json', 'json-pretty'
and 'yaml', as well as "cross-node" in a local test cluster.

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
 PVE/CLI/pvesh.pm | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/PVE/CLI/pvesh.pm b/PVE/CLI/pvesh.pm
index 9acf292a..85cdaf8e 100755
--- a/PVE/CLI/pvesh.pm
+++ b/PVE/CLI/pvesh.pm
@@ -15,6 +15,7 @@ use PVE::CLIHandler;
 use PVE::API2Tools;
 use PVE::API2;
 use JSON;
+use IO::Uncompress::Gunzip qw(gunzip);
 
 use base qw(PVE::CLIHandler);
 
@@ -281,6 +282,28 @@ my $cond_add_standard_output_properties = sub {
     return PVE::RESTHandler::add_standard_output_properties($props, $keys);
 };
 
+my $handle_streamed_response = sub {
+    my ($download) = @_;
+    my ($fh, $encoding, $type) = $download->@{'fh', 'content-encoding', 'content-type'};
+
+    local $/;
+    my $data = <$fh>;
+
+    if (defined($encoding)) {
+	die "unknown 'content-encoding' $encoding\n" if $encoding ne 'gzip';
+	my $out;
+	gunzip(\$data => \$out);
+	$data = $out;
+    }
+
+    if (defined($type) && not $type =~ qw!^text/plain!) {
+	die "unknown 'content-type' $type\n" if not $type =~ qw!^application/json!;
+	$data = decode_json($data)->{data};
+    }
+
+    return $data;
+};
+
 sub call_api_method {
     my ($cmd, $param) = @_;
 
@@ -310,6 +333,9 @@ sub call_api_method {
 	}
 
 	$data = $handler->handle($info, $param);
+
+	$data = &$handle_streamed_response($data->{download})
+	    if ref($data) eq 'HASH' && defined($data->{download});
     }
 
     return if $opt_nooutput || $stdopts->{quiet};
-- 
2.39.2





                 reply	other threads:[~2023-03-28 13:01 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230328130139.510340-1-c.heiss@proxmox.com \
    --to=c.heiss@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal