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 AD1228536 for ; Fri, 3 Mar 2023 18:30:03 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 95B521A3ED for ; Fri, 3 Mar 2023 18:30:03 +0100 (CET) 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 for ; Fri, 3 Mar 2023 18:30:02 +0100 (CET) Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1]) by proxmox-new.maurer-it.com (Proxmox) with ESMTP id BF363463C7 for ; Fri, 3 Mar 2023 18:30:01 +0100 (CET) From: Max Carrara To: pve-devel@lists.proxmox.com Date: Fri, 3 Mar 2023 18:29:48 +0100 Message-Id: <20230303172951.197711-2-m.carrara@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20230303172951.197711-1-m.carrara@proxmox.com> References: <20230303172951.197711-1-m.carrara@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.045 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% 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 Subject: [pve-devel] [PATCH v2 http-server 1/4] anyevent: move header processing into separate subroutine 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: Fri, 03 Mar 2023 17:30:03 -0000 The code concerned with processing the request's header in `unshift_read_header` is moved into the new `process_header` subroutine. If `process_header` doesn't return early, it returns `1` for further control flow purposes. Some minor things are formatted or renamed for readability's sake. Signed-off-by: Max Carrara --- src/PVE/APIServer/AnyEvent.pm | 93 ++++++++++++++++++++--------------- 1 file changed, 53 insertions(+), 40 deletions(-) diff --git a/src/PVE/APIServer/AnyEvent.pm b/src/PVE/APIServer/AnyEvent.pm index 3cd77fa..294c035 100644 --- a/src/PVE/APIServer/AnyEvent.pm +++ b/src/PVE/APIServer/AnyEvent.pm @@ -1320,51 +1320,12 @@ sub unshift_read_header { $r->push_header($state->{key}, $state->{val}) if $state->{key}; - if (!$known_methods->{$method}) { - my $resp = HTTP::Response->new(HTTP_NOT_IMPLEMENTED, "method '$method' not available"); - $self->response($reqstate, $resp); - return; - } - - my $conn = $r->header('Connection'); - my $accept_enc = $r->header('Accept-Encoding'); - $reqstate->{accept_gzip} = ($accept_enc && $accept_enc =~ m/gzip/) ? 1 : 0; - - if ($conn) { - $reqstate->{keep_alive} = 0 if $conn =~ m/close/oi; - } else { - if ($reqstate->{proto}->{ver} < 1001) { - $reqstate->{keep_alive} = 0; - } - } - - my $te = $r->header('Transfer-Encoding'); - if ($te && lc($te) eq 'chunked') { - # Handle chunked transfer encoding - $self->error($reqstate, 501, "chunked transfer encoding not supported"); - return; - } elsif ($te) { - $self->error($reqstate, 501, "Unknown transfer encoding '$te'"); - return; - } - - my $pveclientip = $r->header('PVEClientIP'); my $base_uri = $self->{base_uri}; - # fixme: how can we make PVEClientIP header trusted? - if ($self->{trusted_env} && $pveclientip) { - $reqstate->{peer_host} = $pveclientip; - } else { - $r->header('PVEClientIP', $reqstate->{peer_host}); - } - my $len = $r->header('Content-Length'); - my $host_header = $r->header('Host'); - if (my $rpcenv = $self->{rpcenv}) { - $rpcenv->set_request_host($host_header); - } + $self->process_header($reqstate) or return; # header processing complete - authenticate now my $auth = {}; @@ -1518,6 +1479,58 @@ sub unshift_read_header { }); }; +sub process_header { + my ($self, $reqstate) = @_; + + my $request = $reqstate->{request}; + + my $path = uri_unescape($request->uri->path()); + my $method = $request->method(); + + if (!$known_methods->{$method}) { + my $resp = HTTP::Response->new(HTTP_NOT_IMPLEMENTED, "method '$method' not available"); + $self->response($reqstate, $resp); + return; + } + + my $conn = $request->header('Connection'); + my $accept_enc = $request->header('Accept-Encoding'); + $reqstate->{accept_gzip} = ($accept_enc && $accept_enc =~ m/gzip/) ? 1 : 0; + + if ($conn) { + $reqstate->{keep_alive} = 0 if $conn =~ m/close/oi; + } else { + if ($reqstate->{proto}->{ver} < 1001) { + $reqstate->{keep_alive} = 0; + } + } + + my $te = $request->header('Transfer-Encoding'); + if ($te && lc($te) eq 'chunked') { + # Handle chunked transfer encoding + $self->error($reqstate, 501, "chunked transfer encoding not supported"); + return; + } elsif ($te) { + $self->error($reqstate, 501, "Unknown transfer encoding '$te'"); + return; + } + + my $pveclientip = $request->header('PVEClientIP'); + + # fixme: how can we make PVEClientIP header trusted? + if ($self->{trusted_env} && $pveclientip) { + $reqstate->{peer_host} = $pveclientip; + } else { + $request->header('PVEClientIP', $reqstate->{peer_host}); + } + + if (my $rpcenv = $self->{rpcenv}) { + $rpcenv->set_request_host($request->header('Host')); + } + + return 1; +} + sub push_request_header { my ($self, $reqstate) = @_; -- 2.30.2