From f305be95833ccc77edc136561dcc9ebb5ccb090d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= Date: Fri, 14 May 2021 15:44:43 +0200 Subject: [PATCH] http: takeover build_authority helper from proxmox_backup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabian Grünbichler --- proxmox-http/Cargo.toml | 3 ++- proxmox-http/src/http/helpers.rs | 15 +++++++++++++++ proxmox-http/src/http/mod.rs | 2 ++ 3 files changed, 19 insertions(+), 1 deletion(-) create mode 100644 proxmox-http/src/http/helpers.rs diff --git a/proxmox-http/Cargo.toml b/proxmox-http/Cargo.toml index f1f53da8..6b2d8db8 100644 --- a/proxmox-http/Cargo.toml +++ b/proxmox-http/Cargo.toml @@ -15,6 +15,7 @@ exclude = [ "debian" ] anyhow = "1.0" base64 = { version = "0.12", optional = true } futures = { version = "0.3", optional = true } +http = { version = "0.2", optional = true } hyper = { version = "0.14", features = [ "full" ], optional = true } openssl = { version = "0.10", optional = true } tokio = { version = "1.0", features = [], optional = true } @@ -26,5 +27,5 @@ proxmox = { path = "../proxmox", optional = true, version = "0.11.3", default-fe default = [] client = [ "http-helpers" ] -http-helpers = [ "hyper", "tokio/io-util", "tokio-openssl" ] +http-helpers = [ "http", "hyper", "tokio/io-util", "tokio-openssl" ] websocket = [ "base64", "futures", "hyper", "openssl", "proxmox/tokio", "tokio/io-util", "tokio/sync" ] diff --git a/proxmox-http/src/http/helpers.rs b/proxmox-http/src/http/helpers.rs new file mode 100644 index 00000000..3f663d22 --- /dev/null +++ b/proxmox-http/src/http/helpers.rs @@ -0,0 +1,15 @@ +use anyhow::Error; + +use http::uri::Authority; + +// Build a http::uri::Authority ("host:port"), use '[..]' around IPv6 addresses +pub fn build_authority(host: &str, port: u16) -> Result { + let bytes = host.as_bytes(); + let len = bytes.len(); + let authority = if len > 3 && bytes.contains(&b':') && bytes[0] != b'[' && bytes[len-1] != b']' { + format!("[{}]:{}", host, port).parse()? + } else { + format!("{}:{}", host, port).parse()? + }; + Ok(authority) +} diff --git a/proxmox-http/src/http/mod.rs b/proxmox-http/src/http/mod.rs index 09fa42fb..49602465 100644 --- a/proxmox-http/src/http/mod.rs +++ b/proxmox-http/src/http/mod.rs @@ -1,3 +1,5 @@ mod wrapper; pub use wrapper::MaybeTlsStream; + +pub mod helpers;