From ab5d5b39f6dfd3eb1a4daf947844e2240c17bcd7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20Gr=C3=BCnbichler?= Date: Thu, 4 Aug 2022 09:52:45 +0200 Subject: [PATCH] http: move SimpleHttpOptions to http-helpers feature MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit and rename it to HttpOptions, since it's not specific to the "Simple" client at all. this is a breaking change. Signed-off-by: Fabian Grünbichler --- proxmox-http/src/client/mod.rs | 1 - proxmox-http/src/client/simple.rs | 38 ++++++++-------------------- proxmox-http/src/http_options.rs | 24 ++++++++++++++++++ proxmox-http/src/lib.rs | 5 ++++ proxmox-metrics/src/influxdb/http.rs | 7 ++--- 5 files changed, 43 insertions(+), 32 deletions(-) create mode 100644 proxmox-http/src/http_options.rs diff --git a/proxmox-http/src/client/mod.rs b/proxmox-http/src/client/mod.rs index e578be2d..f56a6778 100644 --- a/proxmox-http/src/client/mod.rs +++ b/proxmox-http/src/client/mod.rs @@ -13,6 +13,5 @@ pub use connector::HttpsConnector; mod simple; pub use simple::SimpleHttp; -pub use simple::SimpleHttpOptions; pub mod tls; diff --git a/proxmox-http/src/client/simple.rs b/proxmox-http/src/client/simple.rs index ee0461bc..9e333c2f 100644 --- a/proxmox-http/src/client/simple.rs +++ b/proxmox-http/src/client/simple.rs @@ -1,57 +1,39 @@ use anyhow::{bail, format_err, Error}; use std::collections::HashMap; +#[cfg(all(feature = "client-trait", feature = "proxmox-async"))] +use std::str::FromStr; + use futures::*; +#[cfg(all(feature = "client-trait", feature = "proxmox-async"))] +use http::header::HeaderName; use http::{HeaderValue, Request, Response}; use hyper::client::{Client, HttpConnector}; use hyper::Body; use openssl::ssl::{SslConnector, SslMethod}; use crate::client::HttpsConnector; -use crate::proxy_config::ProxyConfig; - -/// Options for a SimpleHttp client. -#[derive(Default)] -pub struct SimpleHttpOptions { - /// Proxy configuration - pub proxy_config: Option, - /// `User-Agent` header value, defaults to `proxmox-simple-http-client/0.1` - pub user_agent: Option, - /// TCP keepalive time, defaults to 7200 - pub tcp_keepalive: Option, -} - -impl SimpleHttpOptions { - fn get_proxy_authorization(&self) -> Option { - if let Some(ref proxy_config) = self.proxy_config { - if !proxy_config.force_connect { - return proxy_config.authorization.clone(); - } - } - - None - } -} +use crate::HttpOptions; /// Asyncrounous HTTP client implementation pub struct SimpleHttp { client: Client, - options: SimpleHttpOptions, + options: HttpOptions, } impl SimpleHttp { pub const DEFAULT_USER_AGENT_STRING: &'static str = "proxmox-simple-http-client/0.1"; pub fn new() -> Self { - Self::with_options(SimpleHttpOptions::default()) + Self::with_options(HttpOptions::default()) } - pub fn with_options(options: SimpleHttpOptions) -> Self { + pub fn with_options(options: HttpOptions) -> Self { let ssl_connector = SslConnector::builder(SslMethod::tls()).unwrap().build(); Self::with_ssl_connector(ssl_connector, options) } - pub fn with_ssl_connector(ssl_connector: SslConnector, options: SimpleHttpOptions) -> Self { + pub fn with_ssl_connector(ssl_connector: SslConnector, options: HttpOptions) -> Self { let connector = HttpConnector::new(); let mut https = HttpsConnector::with_connector( connector, diff --git a/proxmox-http/src/http_options.rs b/proxmox-http/src/http_options.rs new file mode 100644 index 00000000..1124aa26 --- /dev/null +++ b/proxmox-http/src/http_options.rs @@ -0,0 +1,24 @@ +use crate::ProxyConfig; + +/// Options for an HTTP client. +#[derive(Default)] +pub struct HttpOptions { + /// Proxy configuration + pub proxy_config: Option, + /// `User-Agent` header value + pub user_agent: Option, + /// TCP keepalive time, defaults to 7200 + pub tcp_keepalive: Option, +} + +impl HttpOptions { + pub fn get_proxy_authorization(&self) -> Option { + if let Some(ref proxy_config) = self.proxy_config { + if !proxy_config.force_connect { + return proxy_config.authorization.clone(); + } + } + + None + } +} diff --git a/proxmox-http/src/lib.rs b/proxmox-http/src/lib.rs index 55c5d346..f32cfec0 100644 --- a/proxmox-http/src/lib.rs +++ b/proxmox-http/src/lib.rs @@ -11,6 +11,11 @@ pub mod proxy_config; #[cfg(feature = "http-helpers")] pub use proxy_config::ProxyConfig; +#[cfg(feature = "http-helpers")] +mod http_options; +#[cfg(feature = "http-helpers")] +pub use http_options::HttpOptions; + #[cfg(feature = "client")] pub mod client; diff --git a/proxmox-metrics/src/influxdb/http.rs b/proxmox-metrics/src/influxdb/http.rs index 26c2f221..352b5f65 100644 --- a/proxmox-metrics/src/influxdb/http.rs +++ b/proxmox-metrics/src/influxdb/http.rs @@ -3,9 +3,10 @@ use std::sync::Arc; use anyhow::{bail, Error}; use hyper::Body; use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode}; +use proxmox_http::HttpOptions; use tokio::sync::mpsc; -use proxmox_http::client::{SimpleHttp, SimpleHttpOptions}; +use proxmox_http::client::SimpleHttp; use crate::influxdb::utils; use crate::{Metrics, MetricsData}; @@ -76,11 +77,11 @@ impl InfluxDbHttp { channel: mpsc::Receiver>, ) -> Result { let client = if verify_tls { - SimpleHttp::with_options(SimpleHttpOptions::default()) + SimpleHttp::with_options(HttpOptions::default()) } else { let mut ssl_connector = SslConnector::builder(SslMethod::tls()).unwrap(); ssl_connector.set_verify(SslVerifyMode::NONE); - SimpleHttp::with_ssl_connector(ssl_connector.build(), SimpleHttpOptions::default()) + SimpleHttp::with_ssl_connector(ssl_connector.build(), HttpOptions::default()) }; let uri: http::uri::Uri = uri.parse()?;