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 D7C14799BA for ; Wed, 5 May 2021 11:10:02 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id CC556128E0 for ; Wed, 5 May 2021 11:09:32 +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 05D0E128D2 for ; Wed, 5 May 2021 11:09:32 +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 D48C346277 for ; Wed, 5 May 2021 11:09:31 +0200 (CEST) To: pbs-devel@lists.proxmox.com References: <20210505083654.16303-1-dietmar@proxmox.com> From: Dietmar Maurer Message-ID: <5a96465c-fcb0-5a56-ae90-25922c1ea9ca@proxmox.com> Date: Wed, 5 May 2021 11:09:30 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.10.0 MIME-Version: 1.0 In-Reply-To: <20210505083654.16303-1-dietmar@proxmox.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-SPAM-LEVEL: Spam detection results: 0 AWL 0.250 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. [fe80::1] WEIRD_PORT 0.001 Uses non-standard port number for HTTP Subject: [pbs-devel] applied: [PATCH proxmox-backup v3] client/http_client: add necessary brackets 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, 05 May 2021 09:10:02 -0000 applied On 5/5/21 10:36 AM, Dietmar Maurer wrote: > if we are given a 'naked' ipv6 without square brackets around it, > we need to add them ourselves, since the address is ambigious otherwise > when we add the port. > > e.g. giving 'fe80::1' as address we arrive at the url (with the default port) > 'https://fe80::1:8007/' > > Signed-off-by: Dominik Csapak > Signed-off-by: Dietmar Maurer > --- > > changes from v2: > * add build_uri() method which returns an Uri > * try to avoid unnecessary allocation (result in some code duplication) > > changes from v1: > * move the actual mapping to the request building functions > > src/client/http_client.rs | 106 +++++++++++++++++++++----------------- > 1 file changed, 58 insertions(+), 48 deletions(-) > > diff --git a/src/client/http_client.rs b/src/client/http_client.rs > index 76ab0391..f2fada23 100644 > --- a/src/client/http_client.rs > +++ b/src/client/http_client.rs > @@ -273,6 +273,26 @@ fn load_ticket_info(prefix: &str, server: &str, userid: &Userid) -> Option<(Stri > } > } > > +fn build_uri(server: &str, port: u16, path: &str, query: Option) -> Result { > + let path = path.trim_matches('/'); > + let bytes = server.as_bytes(); > + let len = bytes.len(); > + let uri = if len > 3 && bytes.contains(&b':') && bytes[0] != b'[' && bytes[len-1] != b']' { > + if let Some(query) = query { > + format!("https://[{}]:{}/{}?{}", server, port, path, query) > + } else { > + format!("https://[{}]:{}/{}", server, port, path) > + } > + } else { > + if let Some(query) = query { > + format!("https://{}:{}/{}?{}", server, port, path, query) > + } else { > + format!("https://{}:{}/{}", server, port, path) > + } > + }; > + Ok(uri.parse()?) > +} > + > impl HttpClient { > pub fn new( > server: &str, > @@ -614,16 +634,11 @@ impl HttpClient { > data: Option, > ) -> Result { > > - let path = path.trim_matches('/'); > - let mut url = format!("https://{}:{}/{}", &self.server, self.port, path); > - > - if let Some(data) = data { > - let query = tools::json_object_to_query(data).unwrap(); > - url.push('?'); > - url.push_str(&query); > - } > - > - let url: Uri = url.parse().unwrap(); > + let query = match data { > + Some(data) => Some(tools::json_object_to_query(data)?), > + None => None, > + }; > + let url = build_uri(&self.server, self.port, path, query)?; > > let req = Request::builder() > .method("POST") > @@ -757,39 +772,38 @@ impl HttpClient { > } > > pub fn request_builder(server: &str, port: u16, method: &str, path: &str, data: Option) -> Result, Error> { > - let path = path.trim_matches('/'); > - let url: Uri = format!("https://{}:{}/{}", server, port, path).parse()?; > - > if let Some(data) = data { > if method == "POST" { > + let url = build_uri(server, port, path, None)?; > let request = Request::builder() > .method(method) > .uri(url) > .header("User-Agent", "proxmox-backup-client/1.0") > .header(hyper::header::CONTENT_TYPE, "application/json") > .body(Body::from(data.to_string()))?; > - return Ok(request); > + Ok(request) > } else { > let query = tools::json_object_to_query(data)?; > - let url: Uri = format!("https://{}:{}/{}?{}", server, port, path, query).parse()?; > + let url = build_uri(server, port, path, Some(query))?; > let request = Request::builder() > .method(method) > .uri(url) > .header("User-Agent", "proxmox-backup-client/1.0") > .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded") > .body(Body::empty())?; > - return Ok(request); > + Ok(request) > } > - } > - > - let request = Request::builder() > - .method(method) > - .uri(url) > - .header("User-Agent", "proxmox-backup-client/1.0") > - .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded") > - .body(Body::empty())?; > + } else { > + let url = build_uri(server, port, path, None)?; > + let request = Request::builder() > + .method(method) > + .uri(url) > + .header("User-Agent", "proxmox-backup-client/1.0") > + .header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded") > + .body(Body::empty())?; > > - Ok(request) > + Ok(request) > + } > } > } > > @@ -970,29 +984,25 @@ impl H2Client { > let path = path.trim_matches('/'); > > let content_type = content_type.unwrap_or("application/x-www-form-urlencoded"); > + let query = match param { > + Some(param) => { > + let query = tools::json_object_to_query(param)?; > + // We detected problem with hyper around 6000 characters - so we try to keep on the safe side > + if query.len() > 4096 { > + bail!("h2 query data too large ({} bytes) - please encode data inside body", query.len()); > + } > + Some(query) > + } > + None => None, > + }; > > - if let Some(param) = param { > - let query = tools::json_object_to_query(param)?; > - // We detected problem with hyper around 6000 characters - seo we try to keep on the safe side > - if query.len() > 4096 { bail!("h2 query data too large ({} bytes) - please encode data inside body", query.len()); } > - let url: Uri = format!("https://{}:8007/{}?{}", server, path, query).parse()?; > - let request = Request::builder() > - .method(method) > - .uri(url) > - .header("User-Agent", "proxmox-backup-client/1.0") > - .header(hyper::header::CONTENT_TYPE, content_type) > - .body(())?; > - Ok(request) > - } else { > - let url: Uri = format!("https://{}:8007/{}", server, path).parse()?; > - let request = Request::builder() > - .method(method) > - .uri(url) > - .header("User-Agent", "proxmox-backup-client/1.0") > - .header(hyper::header::CONTENT_TYPE, content_type) > - .body(())?; > - > - Ok(request) > - } > + let url = build_uri(server, 8007, path, query)?; > + let request = Request::builder() > + .method(method) > + .uri(url) > + .header("User-Agent", "proxmox-backup-client/1.0") > + .header(hyper::header::CONTENT_TYPE, content_type) > + .body(())?; > + Ok(request) > } > }