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) server-digest SHA256) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id C62BE7776F for ; Wed, 28 Apr 2021 12:04:07 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id B2D8E9F75 for ; Wed, 28 Apr 2021 12:04:07 +0200 (CEST) Received: from elsa.proxmox.com (unknown [94.136.29.99]) by firstgate.proxmox.com (Proxmox) with ESMTP id 8EB6F9F68 for ; Wed, 28 Apr 2021 12:04:06 +0200 (CEST) Received: by elsa.proxmox.com (Postfix, from userid 0) id 7F579AEB223; Wed, 28 Apr 2021 12:04:06 +0200 (CEST) From: Dietmar Maurer To: pbs-devel@lists.proxmox.com Date: Wed, 28 Apr 2021 12:03:58 +0200 Message-Id: <20210428100358.25850-2-dietmar@proxmox.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210428100358.25850-1-dietmar@proxmox.com> References: <20210428100358.25850-1-dietmar@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 1 AWL -0.136 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 Subject: [pbs-devel] [PATCH proxmox-backup 2/2] tools/http: make user agent configurable 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, 28 Apr 2021 10:04:07 -0000 --- src/tools/http.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/tools/http.rs b/src/tools/http.rs index a4299fb4..8656e685 100644 --- a/src/tools/http.rs +++ b/src/tools/http.rs @@ -103,6 +103,7 @@ impl ProxyConfig { pub struct SimpleHttp { client: Client, proxy_authorization: Option, // Proxy-Authorization header value + user_agent: Option, } impl SimpleHttp { @@ -129,7 +130,12 @@ impl SimpleHttp { https.set_proxy(proxy_config); } let client = Client::builder().build(https); - Self { client, proxy_authorization} + Self { client, proxy_authorization, user_agent: None } + } + + pub fn set_user_agent(&mut self, user_agent: &str) -> Result<(), Error> { + self.user_agent = Some(user_agent.to_owned()); + Ok(()) } fn add_proxy_headers(&self, request: &mut Request) -> Result<(), Error> { @@ -147,11 +153,13 @@ impl SimpleHttp { } pub async fn request(&self, mut request: Request) -> Result, Error> { + let user_agent = if let Some(ref user_agent) = self.user_agent { + HeaderValue::from_str(&user_agent)? + } else { + HeaderValue::from_str(Self::DEFAULT_USER_AGENT_STRING)? + }; - request.headers_mut().insert( - hyper::header::USER_AGENT, - HeaderValue::from_str(Self::DEFAULT_USER_AGENT_STRING)?, - ); + request.headers_mut().insert(hyper::header::USER_AGENT, user_agent); self.add_proxy_headers(&mut request)?; -- 2.20.1