diff --git a/proxmox-http/src/client/simple.rs b/proxmox-http/src/client/simple.rs index 6cf82304..083f1d99 100644 --- a/proxmox-http/src/client/simple.rs +++ b/proxmox-http/src/client/simple.rs @@ -87,6 +87,7 @@ impl Client { uri: &str, body: Option, content_type: Option<&str>, + extra_headers: Option<&HashMap>, ) -> Result, Error> where R: Read, @@ -100,11 +101,18 @@ impl Client { }; let content_type = content_type.unwrap_or("application/json"); - let request = Request::builder() + let mut request = Request::builder() .method("POST") .uri(uri) - .header(hyper::header::CONTENT_TYPE, content_type) - .body(body)?; + .header(hyper::header::CONTENT_TYPE, content_type); + + if let Some(extra_headers) = extra_headers { + for (header, value) in extra_headers { + request = request.header(header, value); + } + } + + let request = request.body(body)?; self.request(request).await } @@ -191,11 +199,12 @@ impl crate::HttpClient for Client { uri: &str, body: Option, content_type: Option<&str>, + extra_headers: Option<&HashMap>, ) -> Result, Error> where R: Read, { - proxmox_async::runtime::block_on(self.post(uri, body, content_type)) + proxmox_async::runtime::block_on(self.post(uri, body, content_type, extra_headers)) } fn request(&self, request: Request) -> Result, Error> { @@ -232,12 +241,14 @@ impl crate::HttpClient for Client { uri: &str, body: Option, content_type: Option<&str>, + extra_headers: Option<&HashMap>, ) -> Result, Error> where R: Read, { proxmox_async::runtime::block_on(async move { - Self::convert_body_to_string(self.post(uri, body, content_type).await).await + Self::convert_body_to_string(self.post(uri, body, content_type, extra_headers).await) + .await }) } diff --git a/proxmox-http/src/client/sync.rs b/proxmox-http/src/client/sync.rs index c6675e4d..7aa3a8e1 100644 --- a/proxmox-http/src/client/sync.rs +++ b/proxmox-http/src/client/sync.rs @@ -67,6 +67,24 @@ impl Client { Ok(builder) } + fn add_headers( + mut req: ureq::Request, + content_type: Option<&str>, + extra_headers: Option<&HashMap>, + ) -> ureq::Request { + if let Some(content_type) = content_type { + req = req.set("Content-Type", content_type); + } + + if let Some(extra_headers) = extra_headers { + for (header, value) in extra_headers { + req = req.set(header, value); + } + } + + req + } + fn convert_response_to_string(res: ureq::Response) -> Result, Error> { let builder = Self::convert_response(&res)?; let body = res.into_string()?; @@ -94,13 +112,8 @@ impl HttpClient for Client { uri: &str, extra_headers: Option<&HashMap>, ) -> Result, Error> { - let mut req = self.agent()?.get(uri); - - if let Some(extra_headers) = extra_headers { - for (header, value) in extra_headers { - req = req.set(header, value); - } - } + let req = self.agent()?.get(uri); + let req = Self::add_headers(req, None, extra_headers); self.call(req).and_then(Self::convert_response_to_string) } @@ -110,14 +123,13 @@ impl HttpClient for Client { uri: &str, body: Option, content_type: Option<&str>, + extra_headers: Option<&HashMap>, ) -> Result, Error> where R: Read, { - let mut req = self.agent()?.post(uri); - if let Some(content_type) = content_type { - req = req.set("Content-Type", content_type); - } + let req = self.agent()?.post(uri); + let req = Self::add_headers(req, content_type, extra_headers); match body { Some(body) => self.send(req, body), @@ -130,6 +142,8 @@ impl HttpClient for Client { let mut req = self .agent()? .request(request.method().as_str(), &request.uri().to_string()); + req = self.add_user_agent(req); + let orig_headers = request.headers(); for header in orig_headers.keys() { @@ -149,13 +163,8 @@ impl HttpClient> for Client { uri: &str, extra_headers: Option<&HashMap>, ) -> Result>, Error> { - let mut req = self.agent()?.get(uri); - - if let Some(extra_headers) = extra_headers { - for (header, value) in extra_headers { - req = req.set(header, value); - } - } + let req = self.agent()?.get(uri); + let req = Self::add_headers(req, None, extra_headers); self.call(req).and_then(Self::convert_response_to_vec) } @@ -165,14 +174,13 @@ impl HttpClient> for Client { uri: &str, body: Option, content_type: Option<&str>, + extra_headers: Option<&HashMap>, ) -> Result>, Error> where R: Read, { - let mut req = self.agent()?.post(uri); - if let Some(content_type) = content_type { - req = req.set("Content-Type", content_type); - } + let req = self.agent()?.post(uri); + let req = Self::add_headers(req, content_type, extra_headers); match body { Some(body) => self.send(req, body), @@ -185,6 +193,8 @@ impl HttpClient> for Client { let mut req = self .agent()? .request(request.method().as_str(), &request.uri().to_string()); + req = self.add_user_agent(req); + let orig_headers = request.headers(); for header in orig_headers.keys() { @@ -204,13 +214,8 @@ impl HttpClient> for Client { uri: &str, extra_headers: Option<&HashMap>, ) -> Result>, Error> { - let mut req = self.agent()?.get(uri); - - if let Some(extra_headers) = extra_headers { - for (header, value) in extra_headers { - req = req.set(header, value); - } - } + let req = self.agent()?.get(uri); + let req = Self::add_headers(req, None, extra_headers); self.call(req).and_then(Self::convert_response_to_reader) } @@ -220,14 +225,13 @@ impl HttpClient> for Client { uri: &str, body: Option, content_type: Option<&str>, + extra_headers: Option<&HashMap>, ) -> Result>, Error> where R: Read, { - let mut req = self.agent()?.post(uri); - if let Some(content_type) = content_type { - req = req.set("Content-Type", content_type); - } + let req = self.agent()?.post(uri); + let req = Self::add_headers(req, content_type, extra_headers); match body { Some(body) => self.send(req, body), diff --git a/proxmox-http/src/client_trait.rs b/proxmox-http/src/client_trait.rs index 7f5a1883..77234821 100644 --- a/proxmox-http/src/client_trait.rs +++ b/proxmox-http/src/client_trait.rs @@ -15,6 +15,7 @@ pub trait HttpClient { uri: &str, body: Option, content_type: Option<&str>, + extra_headers: Option<&HashMap>, ) -> Result, Error> where R: Read; diff --git a/proxmox-subscription/src/check.rs b/proxmox-subscription/src/check.rs index 408b4f26..530f7cdf 100644 --- a/proxmox-subscription/src/check.rs +++ b/proxmox-subscription/src/check.rs @@ -41,6 +41,7 @@ fn register_subscription>( SHOP_URI, Some(&mut query.as_bytes()), Some("application/x-www-form-urlencoded"), + None, )?; let body = response.into_body();