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 E9200751C2 for ; Wed, 21 Apr 2021 13:17:08 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id E67E9DC89 for ; Wed, 21 Apr 2021 13:17:08 +0200 (CEST) Received: from elsa.proxmox.com (unknown [94.136.29.99]) by firstgate.proxmox.com (Proxmox) with ESMTP id 983E4DC67 for ; Wed, 21 Apr 2021 13:17:04 +0200 (CEST) Received: by elsa.proxmox.com (Postfix, from userid 0) id 83C07AEB09C; Wed, 21 Apr 2021 13:17:04 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Wed, 21 Apr 2021 13:17:01 +0200 Message-Id: <20210421111702.19095-5-dietmar@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210421111702.19095-1-dietmar@proxmox.com> References: <20210421111702.19095-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 1 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 RDNS_NONE 1.274 Delivered to internal network by a host with no rDNS 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. [http.rs] Subject: [pbs-devel] [RFC proxmox-backup 4/5] HttpsConnector: code cleanup X-BeenThere: pbs-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Backup Server development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Apr 2021 11:17:08 -0000 --- src/tools/http.rs | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/src/tools/http.rs b/src/tools/http.rs index f19d6527..a0dbfd01 100644 --- a/src/tools/http.rs +++ b/src/tools/http.rs @@ -3,6 +3,7 @@ use std::task::{Context, Poll}; use std::os::unix::io::AsRawFd; use std::collections::HashMap; use std::pin::Pin; +use std::sync::Arc; use hyper::{Uri, Body}; use hyper::client::{Client, HttpConnector}; @@ -103,17 +104,16 @@ impl SimpleHttp { #[derive(Clone)] pub struct HttpsConnector { - http: HttpConnector, - ssl_connector: std::sync::Arc, + connector: HttpConnector, + ssl_connector: Arc, } impl HttpsConnector { - pub fn with_connector(mut http: HttpConnector, ssl_connector: SslConnector) -> Self { - http.enforce_http(false); - + pub fn with_connector(mut connector: HttpConnector, ssl_connector: SslConnector) -> Self { + connector.enforce_http(false); Self { - http, - ssl_connector: std::sync::Arc::new(ssl_connector), + connector, + ssl_connector: Arc::new(ssl_connector), } } } @@ -130,22 +130,20 @@ impl hyper::service::Service for HttpsConnector { } fn call(&mut self, dst: Uri) -> Self::Future { - let mut this = self.clone(); - async move { - let is_https = dst - .scheme() - .ok_or_else(|| format_err!("missing URL scheme"))? - == "https"; - - let host = dst - .host() - .ok_or_else(|| format_err!("missing hostname in destination url?"))? - .to_string(); + let mut connector = self.connector.clone(); + let ssl_connector = Arc::clone(&self.ssl_connector); + let is_https = dst.scheme() == Some(&http::uri::Scheme::HTTPS); + let host = match dst.host() { + Some(host) => host.to_owned(), + None => { + return futures::future::err(format_err!("missing URL scheme")).boxed(); + } + }; - let config = this.ssl_connector.configure(); + async move { + let config = ssl_connector.configure()?; let dst_str = dst.to_string(); // for error messages - let conn = this - .http + let conn = connector .call(dst) .await .map_err(|err| format_err!("error connecting to {} - {}", dst_str, err))?; @@ -153,7 +151,7 @@ impl hyper::service::Service for HttpsConnector { let _ = set_tcp_keepalive(conn.as_raw_fd(), PROXMOX_BACKUP_TCP_KEEPALIVE_TIME); if is_https { - let mut conn: SslStream = SslStream::new(config?.into_ssl(&host)?, conn)?; + let mut conn: SslStream = SslStream::new(config.into_ssl(&host)?, conn)?; Pin::new(&mut conn).connect().await?; Ok(MaybeTlsStream::Secured(conn)) } else { -- 2.20.1