client/http_client: add necessary brackets
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 <d.csapak@proxmox.com> Signed-off-by: Dietmar Maurer <dietmar@proxmox.com>
This commit is contained in:
parent
ee0c5c8e01
commit
a2daecc25d
|
@ -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<String>) -> Result<Uri, Error> {
|
||||||
|
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 {
|
impl HttpClient {
|
||||||
pub fn new(
|
pub fn new(
|
||||||
server: &str,
|
server: &str,
|
||||||
|
@ -614,16 +634,11 @@ impl HttpClient {
|
||||||
data: Option<Value>,
|
data: Option<Value>,
|
||||||
) -> Result<Value, Error> {
|
) -> Result<Value, Error> {
|
||||||
|
|
||||||
let path = path.trim_matches('/');
|
let query = match data {
|
||||||
let mut url = format!("https://{}:{}/{}", &self.server, self.port, path);
|
Some(data) => Some(tools::json_object_to_query(data)?),
|
||||||
|
None => None,
|
||||||
if let Some(data) = data {
|
};
|
||||||
let query = tools::json_object_to_query(data).unwrap();
|
let url = build_uri(&self.server, self.port, path, query)?;
|
||||||
url.push('?');
|
|
||||||
url.push_str(&query);
|
|
||||||
}
|
|
||||||
|
|
||||||
let url: Uri = url.parse().unwrap();
|
|
||||||
|
|
||||||
let req = Request::builder()
|
let req = Request::builder()
|
||||||
.method("POST")
|
.method("POST")
|
||||||
|
@ -757,39 +772,38 @@ impl HttpClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn request_builder(server: &str, port: u16, method: &str, path: &str, data: Option<Value>) -> Result<Request<Body>, Error> {
|
pub fn request_builder(server: &str, port: u16, method: &str, path: &str, data: Option<Value>) -> Result<Request<Body>, Error> {
|
||||||
let path = path.trim_matches('/');
|
|
||||||
let url: Uri = format!("https://{}:{}/{}", server, port, path).parse()?;
|
|
||||||
|
|
||||||
if let Some(data) = data {
|
if let Some(data) = data {
|
||||||
if method == "POST" {
|
if method == "POST" {
|
||||||
|
let url = build_uri(server, port, path, None)?;
|
||||||
let request = Request::builder()
|
let request = Request::builder()
|
||||||
.method(method)
|
.method(method)
|
||||||
.uri(url)
|
.uri(url)
|
||||||
.header("User-Agent", "proxmox-backup-client/1.0")
|
.header("User-Agent", "proxmox-backup-client/1.0")
|
||||||
.header(hyper::header::CONTENT_TYPE, "application/json")
|
.header(hyper::header::CONTENT_TYPE, "application/json")
|
||||||
.body(Body::from(data.to_string()))?;
|
.body(Body::from(data.to_string()))?;
|
||||||
return Ok(request);
|
Ok(request)
|
||||||
} else {
|
} else {
|
||||||
let query = tools::json_object_to_query(data)?;
|
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()
|
let request = Request::builder()
|
||||||
.method(method)
|
.method(method)
|
||||||
.uri(url)
|
.uri(url)
|
||||||
.header("User-Agent", "proxmox-backup-client/1.0")
|
.header("User-Agent", "proxmox-backup-client/1.0")
|
||||||
.header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
|
.header(hyper::header::CONTENT_TYPE, "application/x-www-form-urlencoded")
|
||||||
.body(Body::empty())?;
|
.body(Body::empty())?;
|
||||||
return Ok(request);
|
Ok(request)
|
||||||
}
|
}
|
||||||
|
} 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)
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -970,29 +984,25 @@ impl H2Client {
|
||||||
let path = path.trim_matches('/');
|
let path = path.trim_matches('/');
|
||||||
|
|
||||||
let content_type = content_type.unwrap_or("application/x-www-form-urlencoded");
|
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 url = build_uri(server, 8007, path, query)?;
|
||||||
let query = tools::json_object_to_query(param)?;
|
let request = Request::builder()
|
||||||
// We detected problem with hyper around 6000 characters - seo we try to keep on the safe side
|
.method(method)
|
||||||
if query.len() > 4096 { bail!("h2 query data too large ({} bytes) - please encode data inside body", query.len()); }
|
.uri(url)
|
||||||
let url: Uri = format!("https://{}:8007/{}?{}", server, path, query).parse()?;
|
.header("User-Agent", "proxmox-backup-client/1.0")
|
||||||
let request = Request::builder()
|
.header(hyper::header::CONTENT_TYPE, content_type)
|
||||||
.method(method)
|
.body(())?;
|
||||||
.uri(url)
|
Ok(request)
|
||||||
.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)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue