proxmox/proxmox-http/src/uri.rs
Wolfgang Bumiller c9b4a4f39b uri: drop anyhow::Error and improve docs
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
2021-05-17 11:25:42 +02:00

18 lines
643 B
Rust

//! URI Related helpers, such as `build_authority`
use http::uri::{Authority, InvalidUri};
// Build an [`Authority`](http::uri::Authority) from a combination of `host` and `port`, ensuring that
// IPv6 addresses are enclosed in brackets.
pub fn build_authority(host: &str, port: u16) -> Result<Authority, InvalidUri> {
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)
}