public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH http-server v2 2/9] proxy: strip chunked framing headers from decoded responses
Date: Tue,  1 Dec 2026 11:18:57 +0100	[thread overview]
Message-ID: <20261201101913.1885484-3-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261201101913.1885484-1-t.lamprecht@proxmox.com>

AnyEvent::HTTP decodes chunked upstream responses but leaves the
Transfer-Encoding and Trailer headers intact. Forwarding them alongside
the Content-Length of the decoded body causes clients to misparse the
body as chunked. In addition, response() checked Perl truthiness on
$content rather than definedness and length, so a decoded empty body
("") omitted Content-Length on keep-alive 200 OK responses and a "0"
body was dropped altogether.

Strip Transfer-Encoding and Trailer before forwarding, disable
HTTP::Headers underscore translation so an upstream Transfer_Encoding
header cannot be normalized back into Transfer-Encoding, and frame
non-stream responses whenever the status permits a body and $content
is defined (keeping 1xx, 204, and 304 without Content-Length or body,
and omitting Content-Length for file/pipe streams). This is needed once
API2 endpoints stream chunked HTTP/1.1 responses (for example from
pvedaemon through pveproxy). The proxy still buffers the full response
rather than forwarding it incrementally.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---

Changes in v2:
- Scope local $HTTP::Headers::TRANSLATE_UNDERSCORE = 0 to a do block
  around HTTP::Headers->new() so it cannot leak across pipelined
  keep-alive responses while still preventing Transfer_Encoding from
  normalizing into Transfer-Encoding.
- Strip any supplied body on HTTP 205 Reset Content while keeping
  Content-Length: 0 for persistent connections.

 src/PVE/APIServer/AnyEvent.pm | 21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

diff --git a/src/PVE/APIServer/AnyEvent.pm b/src/PVE/APIServer/AnyEvent.pm
index fa4076d..8f77737 100644
--- a/src/PVE/APIServer/AnyEvent.pm
+++ b/src/PVE/APIServer/AnyEvent.pm
@@ -408,8 +408,10 @@ sub response {
         $content = '';
     }
 
-    if ($code =~ /^(1\d\d|[23]04)$/) {
-        # make sure informational, no content and not modified response send no content
+    my $no_body = $code =~ /^(1[0-9][0-9]|[23]04)$/;
+    if ($no_body || $code == 205) {
+        # Informational, 204, 205, and 304 responses must not send a body (205 keeps
+        # Content-Length: 0 for persistent connections).
         $content = "";
     }
 
@@ -434,7 +436,7 @@ sub response {
     $resp->header('Server' => "pve-api-daemon/3.0");
 
     my $content_length;
-    if ($content && !$stream_fh) {
+    if (!$stream_fh && !$no_body && defined($content)) {
 
         $content_length = length($content);
 
@@ -469,7 +471,7 @@ sub response {
     #print "SEND(without content) $res\n" if $self->{debug};
 
     $res .= "\015\012";
-    $res .= $content if $content && !$stream_fh;
+    $res .= $content if !$stream_fh && !$no_body && defined($content) && length($content);
 
     $self->log_request($reqstate, $reqstate->{request});
 
@@ -959,7 +961,16 @@ sub proxy_request {
                     my $stream = delete $hdr->{pvestreamfile};
                     delete $hdr->{URL};
                     delete $hdr->{HTTPVersion};
-                    my $header = HTTP::Headers->new(%$hdr);
+                    # AnyEvent::HTTP already decoded a chunked body but keeps the framing headers.
+                    delete $hdr->{'transfer-encoding'};
+                    delete $hdr->{trailer};
+                    # Keep '_' untranslated only while parsing upstream headers so
+                    # Transfer_Encoding cannot become Transfer-Encoding without affecting
+                    # any pipelined follow-up response on the same connection.
+                    my $header = do {
+                        local $HTTP::Headers::TRANSLATE_UNDERSCORE = 0;
+                        HTTP::Headers->new(%$hdr);
+                    };
                     if (my $location = $header->header('Location')) {
                         $location =~ s|^http://localhost:85||;
                         $header->header(Location => $location);
-- 
2.47.3





  parent reply	other threads:[~2026-12-01 10:20 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-01 10:18 [PATCH http-server,apiclient,manager v2 0/9] async and streaming pull metric export Thomas Lamprecht
2026-12-01 10:18 ` [PATCH http-server v2 1/9] response: support deferred async API2 responses Thomas Lamprecht
2026-12-01 10:18 ` Thomas Lamprecht [this message]
2026-12-01 10:18 ` [PATCH http-server v2 3/9] response: support incremental streaming " Thomas Lamprecht
2026-12-01 10:18 ` [PATCH apiclient v2 4/9] add non-blocking AnyEvent API client Thomas Lamprecht
2026-12-01 10:19 ` [PATCH apiclient v2 5/9] add bounded AnyEvent request pool with backpressure and timeout Thomas Lamprecht
2026-12-01 10:19 ` [PATCH manager v2 6/9] api: metrics export: fetch remote nodes asynchronously Thomas Lamprecht
2026-12-01 10:19 ` [PATCH manager v2 7/9] api: metrics export: stream sorted metrics via incremental k-way merge Thomas Lamprecht
2026-12-01 10:19 ` [PATCH manager v2 8/9] api: metrics export: add unsorted pass-through streaming option Thomas Lamprecht
2026-12-01 10:19 ` [PATCH manager v2 9/9] test: add pull metric export test suite Thomas Lamprecht

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=20261201101913.1885484-3-t.lamprecht@proxmox.com \
    --to=t.lamprecht@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