all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Max Carrara <m.carrara@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v2 http-server 1/4] anyevent: move header processing into separate subroutine
Date: Fri,  3 Mar 2023 18:29:48 +0100	[thread overview]
Message-ID: <20230303172951.197711-2-m.carrara@proxmox.com> (raw)
In-Reply-To: <20230303172951.197711-1-m.carrara@proxmox.com>

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 <m.carrara@proxmox.com>
---
 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





  reply	other threads:[~2023-03-03 17:30 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-03 17:29 [pve-devel] [PATCH v2 http-server 0/4] refactor HTTP request processing Max Carrara
2023-03-03 17:29 ` Max Carrara [this message]
2023-03-03 17:29 ` [pve-devel] [PATCH v2 http-server 2/4] anyevent: move auth and request handling into separate subroutine Max Carrara
2023-03-03 17:29 ` [pve-devel] [PATCH v2 http-server 3/4] fix #4494: anyevent: redirect HTTP to HTTPS Max Carrara
2023-03-03 17:29 ` [pve-devel] [PATCH v2 http-server 4/4] anyevent: fix whitespace Max Carrara
2023-03-07 10:20 ` [pve-devel] applied series: [PATCH v2 http-server 0/4] refactor HTTP request processing Fabian Grünbichler

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=20230303172951.197711-2-m.carrara@proxmox.com \
    --to=m.carrara@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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal