http: takeover build_authority helper from proxmox_backup

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
This commit is contained in:
Fabian Grünbichler 2021-05-14 15:44:43 +02:00 committed by Dietmar Maurer
parent 6c5028512b
commit f305be9583
3 changed files with 19 additions and 1 deletions

View File

@ -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" ]

View File

@ -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)
}

View File

@ -1,3 +1,5 @@
mod wrapper;
pub use wrapper::MaybeTlsStream;
pub mod helpers;