use native-tls for ureq

Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
This commit is contained in:
Wolfgang Bumiller 2022-02-01 10:35:10 +01:00
parent ce9a84c54f
commit bd2bf045cc
2 changed files with 23 additions and 8 deletions

View File

@ -13,7 +13,6 @@ exclude = [
name = "proxmox_openid" name = "proxmox_openid"
path = "src/lib.rs" path = "src/lib.rs"
[dependencies] [dependencies]
anyhow = "1.0" anyhow = "1.0"
http = "0.2" http = "0.2"
@ -22,7 +21,8 @@ openidconnect = { version = "2.2", default-features = false, features = ["accept
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0" serde_json = "1.0"
thiserror="1.0" thiserror="1.0"
ureq = { version = "2.4", features = ["native-tls", "gzip"] } ureq = { version = "2.4", default-features = false, features = ["native-tls", "gzip"] }
native-tls = "0.2"
url = "2.1" url = "2.1"
proxmox-time = "1" proxmox-time = "1"

View File

@ -1,3 +1,5 @@
use std::sync::Arc;
use http::header::{HeaderMap, HeaderValue, CONTENT_TYPE}; use http::header::{HeaderMap, HeaderValue, CONTENT_TYPE};
use http::method::Method; use http::method::Method;
use http::status::StatusCode; use http::status::StatusCode;
@ -19,26 +21,39 @@ pub enum Error {
/// Non-ureq HTTP error. /// Non-ureq HTTP error.
#[error("HTTP error")] #[error("HTTP error")]
Http(#[from] http::Error), Http(#[from] http::Error),
/// IO error /// IO error
#[error("IO error")] #[error("IO error")]
IO(#[from] std::io::Error), IO(#[from] std::io::Error),
/// Other error.
#[error("Other error: {}", _0)]
Other(String),
/// Error returned by ureq crate. /// Error returned by ureq crate.
// boxed due to https://github.com/algesten/ureq/issues/296 // boxed due to https://github.com/algesten/ureq/issues/296
#[error("ureq request failed")] #[error("ureq request failed")]
Ureq(#[from] Box<ureq::Error>), Ureq(#[from] Box<ureq::Error>),
#[error("TLS error: {0}")]
Tls(#[from] native_tls::Error),
/// Other error.
#[error("Other error: {}", _0)]
Other(String),
}
fn ureq_agent() -> Result<ureq::Agent, Error> {
Ok(ureq::AgentBuilder::new()
.tls_connector(Arc::new(native_tls::TlsConnector::new()?))
.build())
} }
/// ///
/// Synchronous HTTP client for ureq. /// Synchronous HTTP client for ureq.
/// ///
pub fn http_client(request: HttpRequest) -> Result<HttpResponse, Error> { pub fn http_client(request: HttpRequest) -> Result<HttpResponse, Error> {
let mut req = if let Method::POST = request.method { let agent = ureq_agent()?;
ureq::post(&request.url.to_string()) let mut req = if let Method::POST = request.method {
agent.post(&request.url.to_string())
} else { } else {
ureq::get(&request.url.to_string()) agent.get(&request.url.to_string())
}; };
for (name, value) in request.headers { for (name, value) in request.headers {