public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox 0/4] http: sync: user-agent improvements
@ 2023-03-03 11:39 Fabian Grünbichler
  2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 1/4] http: sync: set user-agent via ureq agent Fabian Grünbichler
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Fabian Grünbichler @ 2023-03-03 11:39 UTC (permalink / raw)
  To: pbs-devel

stumbled upon these while testing the ureq fix linked in the first patch.

Fabian Grünbichler (4):
  http: sync: set user-agent via ureq agent
  http: sync: remove redundant calls for setting User-Agent
  http: sync: derive default user-agent from crate version
  http: sync: drop unused &self parameter

 proxmox-http/src/client/sync.rs | 55 ++++++++++++---------------------
 1 file changed, 20 insertions(+), 35 deletions(-)

-- 
2.30.2





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

* [pbs-devel] [PATCH proxmox 1/4] http: sync: set user-agent via ureq agent
  2023-03-03 11:39 [pbs-devel] [PATCH proxmox 0/4] http: sync: user-agent improvements Fabian Grünbichler
@ 2023-03-03 11:39 ` Fabian Grünbichler
  2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 2/4] http: sync: remove redundant calls for setting User-Agent Fabian Grünbichler
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Fabian Grünbichler @ 2023-03-03 11:39 UTC (permalink / raw)
  To: pbs-devel

this allows us to slim down our code, and once
https://github.com/algesten/ureq/pull/597 is merged upstream (and/or we update
to a version containing the fix) it also means the custom user agent is used
for requests to the proxy host, if one is configured.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
---
 proxmox-http/src/client/sync.rs | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/proxmox-http/src/client/sync.rs b/proxmox-http/src/client/sync.rs
index 41b9c79..9465d8e 100644
--- a/proxmox-http/src/client/sync.rs
+++ b/proxmox-http/src/client/sync.rs
@@ -22,6 +22,14 @@ impl Client {
 
     fn agent(&self) -> Result<ureq::Agent, Error> {
         let mut builder = ureq::AgentBuilder::new();
+
+        builder = builder.user_agent(
+            self.options
+                .user_agent
+                .as_deref()
+                .unwrap_or(DEFAULT_USER_AGENT_STRING),
+        );
+
         if let Some(proxy_config) = &self.options.proxy_config {
             builder = builder.proxy(ureq::Proxy::new(proxy_config.to_proxy_string()?)?);
         }
-- 
2.30.2





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

* [pbs-devel] [PATCH proxmox 2/4] http: sync: remove redundant calls for setting User-Agent
  2023-03-03 11:39 [pbs-devel] [PATCH proxmox 0/4] http: sync: user-agent improvements Fabian Grünbichler
  2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 1/4] http: sync: set user-agent via ureq agent Fabian Grünbichler
@ 2023-03-03 11:39 ` Fabian Grünbichler
  2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 3/4] http: sync: derive default user-agent from crate version Fabian Grünbichler
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 7+ messages in thread
From: Fabian Grünbichler @ 2023-03-03 11:39 UTC (permalink / raw)
  To: pbs-devel

the requests are all created via the agent that already contains the user
agent, so this internal helper isn't needed anymore.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
---
AFAICT at least, there shouldn't be anything relying on the agent being present
as manually added header..

 proxmox-http/src/client/sync.rs | 16 ----------------
 1 file changed, 16 deletions(-)

diff --git a/proxmox-http/src/client/sync.rs b/proxmox-http/src/client/sync.rs
index 9465d8e..228f839 100644
--- a/proxmox-http/src/client/sync.rs
+++ b/proxmox-http/src/client/sync.rs
@@ -37,19 +37,7 @@ impl Client {
         Ok(builder.build())
     }
 
-    fn add_user_agent(&self, req: ureq::Request) -> ureq::Request {
-        req.set(
-            "User-Agent",
-            self.options
-                .user_agent
-                .as_deref()
-                .unwrap_or(DEFAULT_USER_AGENT_STRING),
-        )
-    }
-
     fn call(&self, req: ureq::Request) -> Result<ureq::Response, Error> {
-        let req = self.add_user_agent(req);
-
         req.call().map_err(Into::into)
     }
 
@@ -57,8 +45,6 @@ impl Client {
     where
         R: Read,
     {
-        let req = self.add_user_agent(req);
-
         req.send(body).map_err(Into::into)
     }
 
@@ -147,7 +133,6 @@ impl HttpClient<String, String> 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();
 
@@ -195,7 +180,6 @@ impl HttpClient<&[u8], Vec<u8>> 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();
 
-- 
2.30.2





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

* [pbs-devel] [PATCH proxmox 3/4] http: sync: derive default user-agent from crate version
  2023-03-03 11:39 [pbs-devel] [PATCH proxmox 0/4] http: sync: user-agent improvements Fabian Grünbichler
  2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 1/4] http: sync: set user-agent via ureq agent Fabian Grünbichler
  2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 2/4] http: sync: remove redundant calls for setting User-Agent Fabian Grünbichler
@ 2023-03-03 11:39 ` Fabian Grünbichler
  2023-03-07  8:40   ` Wolfgang Bumiller
  2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 4/4] http: sync: drop unused &self parameter Fabian Grünbichler
  2023-03-07  8:31 ` [pbs-devel] applied-series: [PATCH proxmox 0/4] http: sync: user-agent improvements Wolfgang Bumiller
  4 siblings, 1 reply; 7+ messages in thread
From: Fabian Grünbichler @ 2023-03-03 11:39 UTC (permalink / raw)
  To: pbs-devel

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
---
if we want a fall back in case rustc is called manually, not by cargo, we could
also use option_env!..

 proxmox-http/src/client/sync.rs | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/proxmox-http/src/client/sync.rs b/proxmox-http/src/client/sync.rs
index 228f839..3485f84 100644
--- a/proxmox-http/src/client/sync.rs
+++ b/proxmox-http/src/client/sync.rs
@@ -7,8 +7,6 @@ use http::Response;
 use crate::HttpClient;
 use crate::HttpOptions;
 
-pub const DEFAULT_USER_AGENT_STRING: &str = "proxmox-sync-http-client/0.1";
-
 #[derive(Default)]
 /// Blocking HTTP client for usage with [`HttpClient`].
 pub struct Client {
@@ -23,12 +21,10 @@ impl Client {
     fn agent(&self) -> Result<ureq::Agent, Error> {
         let mut builder = ureq::AgentBuilder::new();
 
-        builder = builder.user_agent(
-            self.options
-                .user_agent
-                .as_deref()
-                .unwrap_or(DEFAULT_USER_AGENT_STRING),
-        );
+        builder = builder.user_agent(self.options.user_agent.as_deref().unwrap_or(&format!(
+            "proxmox-sync-http-client/{}",
+            env!("CARGO_PKG_VERSION")
+        )));
 
         if let Some(proxy_config) = &self.options.proxy_config {
             builder = builder.proxy(ureq::Proxy::new(proxy_config.to_proxy_string()?)?);
-- 
2.30.2





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

* [pbs-devel] [PATCH proxmox 4/4] http: sync: drop unused &self parameter
  2023-03-03 11:39 [pbs-devel] [PATCH proxmox 0/4] http: sync: user-agent improvements Fabian Grünbichler
                   ` (2 preceding siblings ...)
  2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 3/4] http: sync: derive default user-agent from crate version Fabian Grünbichler
@ 2023-03-03 11:39 ` Fabian Grünbichler
  2023-03-07  8:31 ` [pbs-devel] applied-series: [PATCH proxmox 0/4] http: sync: user-agent improvements Wolfgang Bumiller
  4 siblings, 0 replies; 7+ messages in thread
From: Fabian Grünbichler @ 2023-03-03 11:39 UTC (permalink / raw)
  To: pbs-devel

these are just internal helpers, changing their signature is fine.

Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
---
 proxmox-http/src/client/sync.rs | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)

diff --git a/proxmox-http/src/client/sync.rs b/proxmox-http/src/client/sync.rs
index 3485f84..ea081a7 100644
--- a/proxmox-http/src/client/sync.rs
+++ b/proxmox-http/src/client/sync.rs
@@ -33,11 +33,11 @@ impl Client {
         Ok(builder.build())
     }
 
-    fn call(&self, req: ureq::Request) -> Result<ureq::Response, Error> {
+    fn call(req: ureq::Request) -> Result<ureq::Response, Error> {
         req.call().map_err(Into::into)
     }
 
-    fn send<R>(&self, req: ureq::Request, body: R) -> Result<ureq::Response, Error>
+    fn send<R>(req: ureq::Request, body: R) -> Result<ureq::Response, Error>
     where
         R: Read,
     {
@@ -105,7 +105,7 @@ impl HttpClient<String, String> for Client {
         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)
+        Self::call(req).and_then(Self::convert_response_to_string)
     }
 
     fn post(
@@ -119,8 +119,8 @@ impl HttpClient<String, String> for Client {
         let req = Self::add_headers(req, content_type, extra_headers);
 
         match body {
-            Some(body) => self.send(req, body.as_bytes()),
-            None => self.call(req),
+            Some(body) => Self::send(req, body.as_bytes()),
+            None => Self::call(req),
         }
         .and_then(Self::convert_response_to_string)
     }
@@ -138,8 +138,7 @@ impl HttpClient<String, String> for Client {
             }
         }
 
-        self.send(req, request.body().as_bytes())
-            .and_then(Self::convert_response_to_string)
+        Self::send(req, request.body().as_bytes()).and_then(Self::convert_response_to_string)
     }
 }
 
@@ -152,7 +151,7 @@ impl HttpClient<&[u8], Vec<u8>> for Client {
         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)
+        Self::call(req).and_then(Self::convert_response_to_vec)
     }
 
     fn post(
@@ -166,8 +165,8 @@ impl HttpClient<&[u8], Vec<u8>> for Client {
         let req = Self::add_headers(req, content_type, extra_headers);
 
         match body {
-            Some(body) => self.send(req, body),
-            None => self.call(req),
+            Some(body) => Self::send(req, body),
+            None => Self::call(req),
         }
         .and_then(Self::convert_response_to_vec)
     }
@@ -185,8 +184,7 @@ impl HttpClient<&[u8], Vec<u8>> for Client {
             }
         }
 
-        self.send(req, *request.body())
-            .and_then(Self::convert_response_to_vec)
+        Self::send(req, *request.body()).and_then(Self::convert_response_to_vec)
     }
 }
 
@@ -199,7 +197,7 @@ impl HttpClient<Box<dyn Read>, Box<dyn Read>> for Client {
         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)
+        Self::call(req).and_then(Self::convert_response_to_reader)
     }
 
     fn post(
@@ -213,8 +211,8 @@ impl HttpClient<Box<dyn Read>, Box<dyn Read>> for Client {
         let req = Self::add_headers(req, content_type, extra_headers);
 
         match body {
-            Some(body) => self.send(req, body),
-            None => self.call(req),
+            Some(body) => Self::send(req, body),
+            None => Self::call(req),
         }
         .and_then(Self::convert_response_to_reader)
     }
@@ -234,7 +232,6 @@ impl HttpClient<Box<dyn Read>, Box<dyn Read>> for Client {
             }
         }
 
-        self.send(req, Box::new(request.body_mut()))
-            .and_then(Self::convert_response_to_reader)
+        Self::send(req, Box::new(request.body_mut())).and_then(Self::convert_response_to_reader)
     }
 }
-- 
2.30.2





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

* [pbs-devel] applied-series: [PATCH proxmox 0/4] http: sync: user-agent improvements
  2023-03-03 11:39 [pbs-devel] [PATCH proxmox 0/4] http: sync: user-agent improvements Fabian Grünbichler
                   ` (3 preceding siblings ...)
  2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 4/4] http: sync: drop unused &self parameter Fabian Grünbichler
@ 2023-03-07  8:31 ` Wolfgang Bumiller
  4 siblings, 0 replies; 7+ messages in thread
From: Wolfgang Bumiller @ 2023-03-07  8:31 UTC (permalink / raw)
  To: Fabian Grünbichler; +Cc: pbs-devel

applied, thanks




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

* Re: [pbs-devel] [PATCH proxmox 3/4] http: sync: derive default user-agent from crate version
  2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 3/4] http: sync: derive default user-agent from crate version Fabian Grünbichler
@ 2023-03-07  8:40   ` Wolfgang Bumiller
  0 siblings, 0 replies; 7+ messages in thread
From: Wolfgang Bumiller @ 2023-03-07  8:40 UTC (permalink / raw)
  To: Fabian Grünbichler; +Cc: pbs-devel

On Fri, Mar 03, 2023 at 12:39:23PM +0100, Fabian Grünbichler wrote:
> Signed-off-by: Fabian Grünbichler <f.gruenbichler@proxmox.com>
> ---
> if we want a fall back in case rustc is called manually, not by cargo, we could
> also use option_env!..
> 
>  proxmox-http/src/client/sync.rs | 12 ++++--------
>  1 file changed, 4 insertions(+), 8 deletions(-)
> 
> diff --git a/proxmox-http/src/client/sync.rs b/proxmox-http/src/client/sync.rs
> index 228f839..3485f84 100644
> --- a/proxmox-http/src/client/sync.rs
> +++ b/proxmox-http/src/client/sync.rs
> @@ -7,8 +7,6 @@ use http::Response;
>  use crate::HttpClient;
>  use crate::HttpOptions;
>  
> -pub const DEFAULT_USER_AGENT_STRING: &str = "proxmox-sync-http-client/0.1";
> -
>  #[derive(Default)]
>  /// Blocking HTTP client for usage with [`HttpClient`].
>  pub struct Client {
> @@ -23,12 +21,10 @@ impl Client {
>      fn agent(&self) -> Result<ureq::Agent, Error> {
>          let mut builder = ureq::AgentBuilder::new();
>  
> -        builder = builder.user_agent(
> -            self.options
> -                .user_agent
> -                .as_deref()
> -                .unwrap_or(DEFAULT_USER_AGENT_STRING),
> -        );
> +        builder = builder.user_agent(self.options.user_agent.as_deref().unwrap_or(&format!(
> +            "proxmox-sync-http-client/{}",
> +            env!("CARGO_PKG_VERSION")
> +        )));

I just realized too late that here this could actually use `concat!()`
instead of `&format!` to avoid the extra allocation.

>  
>          if let Some(proxy_config) = &self.options.proxy_config {
>              builder = builder.proxy(ureq::Proxy::new(proxy_config.to_proxy_string()?)?);
> -- 
> 2.30.2




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

end of thread, other threads:[~2023-03-07  8:41 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-03 11:39 [pbs-devel] [PATCH proxmox 0/4] http: sync: user-agent improvements Fabian Grünbichler
2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 1/4] http: sync: set user-agent via ureq agent Fabian Grünbichler
2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 2/4] http: sync: remove redundant calls for setting User-Agent Fabian Grünbichler
2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 3/4] http: sync: derive default user-agent from crate version Fabian Grünbichler
2023-03-07  8:40   ` Wolfgang Bumiller
2023-03-03 11:39 ` [pbs-devel] [PATCH proxmox 4/4] http: sync: drop unused &self parameter Fabian Grünbichler
2023-03-07  8:31 ` [pbs-devel] applied-series: [PATCH proxmox 0/4] http: sync: user-agent improvements 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