From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id 9DE4975DB6; Thu, 22 Apr 2021 17:35:39 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 374641EBF9; Thu, 22 Apr 2021 17:35:14 +0200 (CEST) Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com [94.136.29.106]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by firstgate.proxmox.com (Proxmox) with ESMTPS id F40EE1EB3A; Thu, 22 Apr 2021 17:35:05 +0200 (CEST) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id CEAA84200F; Thu, 22 Apr 2021 17:35:05 +0200 (CEST) From: Stefan Reiter To: pve-devel@lists.proxmox.com, pbs-devel@lists.proxmox.com Date: Thu, 22 Apr 2021 17:34:53 +0200 Message-Id: <20210422153457.12265-10-s.reiter@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210422153457.12265-1-s.reiter@proxmox.com> References: <20210422153457.12265-1-s.reiter@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.025 Adjusted score from AWL reputation of From: address KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [anyevent.pm] Subject: [pve-devel] [PATCH v2 http-server 09/13] allow stream download from path and over pvedaemon-proxy X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 22 Apr 2021 15:35:40 -0000 Allow specifying a filepath for stream=1 instead of either a path or fh with stream=1. With this in place, we can also just return the path to the proxy in case we want to stream a response back, and let it read from the file itself. This way, the pvedaemon is cut out of the transfer pipe. Signed-off-by: Stefan Reiter --- v2: * unchanged PVE/APIServer/AnyEvent.pm | 40 +++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/PVE/APIServer/AnyEvent.pm b/PVE/APIServer/AnyEvent.pm index f3db0e3..0654bd4 100644 --- a/PVE/APIServer/AnyEvent.pm +++ b/PVE/APIServer/AnyEvent.pm @@ -410,9 +410,33 @@ sub send_file_start { my $mime; if (ref($download) eq 'HASH') { - $fh = $download->{fh}; $mime = $download->{'content-type'}; + if ($download->{path} && $download->{stream} && + $reqstate->{request}->header('PVEDisableProxy')) + { + # avoid double stream from a file, let the proxy handle it + die "internal error: file proxy streaming only available for pvedaemon\n" + if !$self->{trusted_env}; + my $header = HTTP::Headers->new( + pvestreamfile => $download->{path}, + Content_Type => $mime, + ); + # we need some data so Content-Length gets set correctly and + # the proxy doesn't wait for more data - place a canary + my $resp = HTTP::Response->new(200, "OK", $header, "error canary"); + $self->response($reqstate, $resp); + return; + } + + if (!($fh = $download->{fh})) { + my $path = $download->{path}; + die "internal error: {download} returned but neither fh not path given\n" + if !$path; + sysopen($fh, "$path", O_NONBLOCK | O_RDONLY) + or die "open stream path '$path' for reading failed: $!\n"; + } + if ($download->{stream}) { my $header = HTTP::Headers->new(Content_Type => $mime); my $resp = HTTP::Response->new(200, "OK", $header); @@ -750,6 +774,7 @@ sub proxy_request { eval { my $code = delete $hdr->{Status}; my $msg = delete $hdr->{Reason}; + my $stream = delete $hdr->{pvestreamfile}; delete $hdr->{URL}; delete $hdr->{HTTPVersion}; my $header = HTTP::Headers->new(%$hdr); @@ -757,9 +782,16 @@ sub proxy_request { $location =~ s|^http://localhost:85||; $header->header(Location => $location); } - my $resp = HTTP::Response->new($code, $msg, $header, $body); - # Note: disable compression, because body is already compressed - $self->response($reqstate, $resp, undef, 1); + if ($stream) { + sysopen(my $fh, "$stream", O_NONBLOCK | O_RDONLY) + or die "open stream path '$stream' for forwarding failed: $!\n"; + my $resp = HTTP::Response->new($code, $msg, $header, undef); + $self->response($reqstate, $resp, undef, 1, 0, $fh); + } else { + my $resp = HTTP::Response->new($code, $msg, $header, $body); + # Note: disable compression, because body is already compressed + $self->response($reqstate, $resp, undef, 1); + } }; warn $@ if $@; }); -- 2.20.1