[pbs-devel] [PATCH proxmox 07/13] http: takeover build_authority helper from proxmox_backup

Fabian Grünbichler f.gruenbichler at proxmox.com
Fri May 14 15:44:43 CEST 2021


Signed-off-by: Fabian Grünbichler <f.gruenbichler at proxmox.com>
---
 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 f1f53da..6b2d8db 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 0000000..3f663d2
--- /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<Authority, Error> {
+    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 09fa42f..4960246 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;
-- 
2.20.1






More information about the pbs-devel mailing list