public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox-backup 1/2] tools/http: set USER_AGENT inside request
@ 2021-04-28 10:03 Dietmar Maurer
  2021-04-28 10:03 ` [pbs-devel] [PATCH proxmox-backup 2/2] tools/http: make user agent configurable Dietmar Maurer
  0 siblings, 1 reply; 3+ messages in thread
From: Dietmar Maurer @ 2021-04-28 10:03 UTC (permalink / raw)
  To: pbs-devel

---
 src/tools/http.rs | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/tools/http.rs b/src/tools/http.rs
index f4ccefc9..a4299fb4 100644
--- a/src/tools/http.rs
+++ b/src/tools/http.rs
@@ -107,6 +107,8 @@ pub struct SimpleHttp {
 
 impl SimpleHttp {
 
+    pub const DEFAULT_USER_AGENT_STRING: &'static str = "proxmox-backup-client/1.0";
+
     pub fn new(proxy_config: Option<ProxyConfig>) -> Self {
         let ssl_connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
         Self::with_ssl_connector(ssl_connector, proxy_config)
@@ -145,7 +147,14 @@ impl SimpleHttp {
     }
 
     pub async fn request(&self, mut request: Request<Body>) -> Result<Response<Body>, Error> {
+
+        request.headers_mut().insert(
+            hyper::header::USER_AGENT,
+            HeaderValue::from_str(Self::DEFAULT_USER_AGENT_STRING)?,
+        );
+
         self.add_proxy_headers(&mut request)?;
+
         self.client.request(request)
             .map_err(Error::from)
             .await
@@ -168,7 +177,6 @@ impl SimpleHttp {
         let request = Request::builder()
             .method("POST")
             .uri(uri)
-            .header("User-Agent", "proxmox-backup-client/1.0")
             .header(hyper::header::CONTENT_TYPE, content_type)
             .body(body)?;
 
@@ -183,8 +191,7 @@ impl SimpleHttp {
 
         let mut request = Request::builder()
             .method("GET")
-            .uri(uri)
-            .header("User-Agent", "proxmox-backup-client/1.0");
+            .uri(uri);
 
         if let Some(hs) = extra_headers {
             for (h, v) in hs.iter() {
-- 
2.20.1




^ permalink raw reply	[flat|nested] 3+ messages in thread

* [pbs-devel] [PATCH proxmox-backup 2/2] tools/http: make user agent configurable
  2021-04-28 10:03 [pbs-devel] [PATCH proxmox-backup 1/2] tools/http: set USER_AGENT inside request Dietmar Maurer
@ 2021-04-28 10:03 ` Dietmar Maurer
  2021-04-28 14:23   ` Wolfgang Bumiller
  0 siblings, 1 reply; 3+ messages in thread
From: Dietmar Maurer @ 2021-04-28 10:03 UTC (permalink / raw)
  To: pbs-devel

---
 src/tools/http.rs | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/src/tools/http.rs b/src/tools/http.rs
index a4299fb4..8656e685 100644
--- a/src/tools/http.rs
+++ b/src/tools/http.rs
@@ -103,6 +103,7 @@ impl ProxyConfig {
 pub struct SimpleHttp {
     client: Client<HttpsConnector, Body>,
     proxy_authorization: Option<String>, // Proxy-Authorization header value
+    user_agent: Option<String>,
 }
 
 impl SimpleHttp {
@@ -129,7 +130,12 @@ impl SimpleHttp {
             https.set_proxy(proxy_config);
         }
         let client = Client::builder().build(https);
-        Self { client, proxy_authorization}
+        Self { client, proxy_authorization, user_agent: None }
+    }
+
+    pub fn set_user_agent(&mut self, user_agent: &str) -> Result<(), Error> {
+        self.user_agent = Some(user_agent.to_owned());
+        Ok(())
     }
 
     fn add_proxy_headers(&self, request: &mut Request<Body>) -> Result<(), Error> {
@@ -147,11 +153,13 @@ impl SimpleHttp {
     }
 
     pub async fn request(&self, mut request: Request<Body>) -> Result<Response<Body>, Error> {
+        let user_agent = if let Some(ref user_agent) = self.user_agent {
+            HeaderValue::from_str(&user_agent)?
+        } else {
+            HeaderValue::from_str(Self::DEFAULT_USER_AGENT_STRING)?
+        };
 
-        request.headers_mut().insert(
-            hyper::header::USER_AGENT,
-            HeaderValue::from_str(Self::DEFAULT_USER_AGENT_STRING)?,
-        );
+        request.headers_mut().insert(hyper::header::USER_AGENT, user_agent);
 
         self.add_proxy_headers(&mut request)?;
 
-- 
2.20.1




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [pbs-devel] [PATCH proxmox-backup 2/2] tools/http: make user agent configurable
  2021-04-28 10:03 ` [pbs-devel] [PATCH proxmox-backup 2/2] tools/http: make user agent configurable Dietmar Maurer
@ 2021-04-28 14:23   ` Wolfgang Bumiller
  0 siblings, 0 replies; 3+ messages in thread
From: Wolfgang Bumiller @ 2021-04-28 14:23 UTC (permalink / raw)
  To: Dietmar Maurer; +Cc: pbs-devel

Do we really need to have more than 1 user agent string in what's
basically a non-exported utility, though?

On Wed, Apr 28, 2021 at 12:03:58PM +0200, Dietmar Maurer wrote:
> ---
>  src/tools/http.rs | 18 +++++++++++++-----
>  1 file changed, 13 insertions(+), 5 deletions(-)
> 
> diff --git a/src/tools/http.rs b/src/tools/http.rs
> index a4299fb4..8656e685 100644
> --- a/src/tools/http.rs
> +++ b/src/tools/http.rs
> @@ -103,6 +103,7 @@ impl ProxyConfig {
>  pub struct SimpleHttp {
>      client: Client<HttpsConnector, Body>,
>      proxy_authorization: Option<String>, // Proxy-Authorization header value
> +    user_agent: Option<String>,
>  }
>  
>  impl SimpleHttp {
> @@ -129,7 +130,12 @@ impl SimpleHttp {
>              https.set_proxy(proxy_config);
>          }
>          let client = Client::builder().build(https);
> -        Self { client, proxy_authorization}
> +        Self { client, proxy_authorization, user_agent: None }
> +    }
> +
> +    pub fn set_user_agent(&mut self, user_agent: &str) -> Result<(), Error> {
> +        self.user_agent = Some(user_agent.to_owned());
> +        Ok(())
>      }
>  
>      fn add_proxy_headers(&self, request: &mut Request<Body>) -> Result<(), Error> {
> @@ -147,11 +153,13 @@ impl SimpleHttp {
>      }
>  
>      pub async fn request(&self, mut request: Request<Body>) -> Result<Response<Body>, Error> {
> +        let user_agent = if let Some(ref user_agent) = self.user_agent {
> +            HeaderValue::from_str(&user_agent)?
> +        } else {
> +            HeaderValue::from_str(Self::DEFAULT_USER_AGENT_STRING)?
> +        };
>  
> -        request.headers_mut().insert(
> -            hyper::header::USER_AGENT,
> -            HeaderValue::from_str(Self::DEFAULT_USER_AGENT_STRING)?,
> -        );
> +        request.headers_mut().insert(hyper::header::USER_AGENT, user_agent);
>  
>          self.add_proxy_headers(&mut request)?;
>  
> -- 
> 2.20.1




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-04-28 14:24 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-28 10:03 [pbs-devel] [PATCH proxmox-backup 1/2] tools/http: set USER_AGENT inside request Dietmar Maurer
2021-04-28 10:03 ` [pbs-devel] [PATCH proxmox-backup 2/2] tools/http: make user agent configurable Dietmar Maurer
2021-04-28 14:23   ` Wolfgang Bumiller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal