[pbs-devel] [PATCH proxmox-backup v2] client/http_client: add necessary brackets

Dominik Csapak d.csapak at proxmox.com
Tue May 4 15:01:38 CEST 2021


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 at proxmox.com>
---
changes from v1:
* move the actual mapping to the request building functions

 src/client/http_client.rs | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/src/client/http_client.rs b/src/client/http_client.rs
index 76ab0391..ab7bda48 100644
--- a/src/client/http_client.rs
+++ b/src/client/http_client.rs
@@ -273,6 +273,16 @@ fn load_ticket_info(prefix: &str, server: &str, userid: &Userid) -> Option<(Stri
     }
 }
 
+fn map_ipv6(server: &str) -> String {
+    let bytes = server.as_bytes();
+    let len = bytes.len();
+    if len > 3 && bytes.contains(&b':') && bytes[0] != b'[' && bytes[len-1] != b']' {
+        format!("[{}]", server)
+    } else {
+        server.to_string()
+    }
+}
+
 impl HttpClient {
     pub fn new(
         server: &str,
@@ -615,7 +625,8 @@ impl HttpClient {
     ) -> Result<Value, Error> {
 
         let path = path.trim_matches('/');
-        let mut url = format!("https://{}:{}/{}", &self.server, self.port, path);
+        let server = map_ipv6(&self.server);
+        let mut url = format!("https://{}:{}/{}", server, self.port, path);
 
         if let Some(data) = data {
             let query = tools::json_object_to_query(data).unwrap();
@@ -758,6 +769,7 @@ impl HttpClient {
 
     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 server = map_ipv6(server);
         let url: Uri = format!("https://{}:{}/{}", server, port, path).parse()?;
 
         if let Some(data) = data {
@@ -970,6 +982,7 @@ impl H2Client {
         let path = path.trim_matches('/');
 
         let content_type = content_type.unwrap_or("application/x-www-form-urlencoded");
+        let server = map_ipv6(server);
 
         if let Some(param) = param {
             let query = tools::json_object_to_query(param)?;
-- 
2.20.1






More information about the pbs-devel mailing list