tools/http: set USER_AGENT inside request

This commit is contained in:
Dietmar Maurer 2021-04-28 11:44:51 +02:00
parent 84c8a580b5
commit d52b120905
1 changed files with 10 additions and 3 deletions

View File

@ -107,6 +107,8 @@ pub struct SimpleHttp {
impl SimpleHttp { impl SimpleHttp {
pub const DEFAULT_USER_AGENT_STRING: &'static str = "proxmox-backup-client/1.0";
pub fn new(proxy_config: Option<ProxyConfig>) -> Self { pub fn new(proxy_config: Option<ProxyConfig>) -> Self {
let ssl_connector = SslConnector::builder(SslMethod::tls()).unwrap().build(); let ssl_connector = SslConnector::builder(SslMethod::tls()).unwrap().build();
Self::with_ssl_connector(ssl_connector, proxy_config) 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> { 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.add_proxy_headers(&mut request)?;
self.client.request(request) self.client.request(request)
.map_err(Error::from) .map_err(Error::from)
.await .await
@ -168,7 +177,6 @@ impl SimpleHttp {
let request = Request::builder() let request = Request::builder()
.method("POST") .method("POST")
.uri(uri) .uri(uri)
.header("User-Agent", "proxmox-backup-client/1.0")
.header(hyper::header::CONTENT_TYPE, content_type) .header(hyper::header::CONTENT_TYPE, content_type)
.body(body)?; .body(body)?;
@ -183,8 +191,7 @@ impl SimpleHttp {
let mut request = Request::builder() let mut request = Request::builder()
.method("GET") .method("GET")
.uri(uri) .uri(uri);
.header("User-Agent", "proxmox-backup-client/1.0");
if let Some(hs) = extra_headers { if let Some(hs) = extra_headers {
for (h, v) in hs.iter() { for (h, v) in hs.iter() {