all lists on lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox-backup v2] client/http_client: add necessary brackets
@ 2021-05-04 13:01 Dominik Csapak
  0 siblings, 0 replies; only message in thread
From: Dominik Csapak @ 2021-05-04 13:01 UTC (permalink / raw)
  To: pbs-devel

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>
---
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





^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-05-04 13:02 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-04 13:01 [pbs-devel] [PATCH proxmox-backup v2] client/http_client: add necessary brackets Dominik Csapak

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal