From 32f2ffd9791ffed90427921ef89e1d6af1c69680 Mon Sep 17 00:00:00 2001 From: Lann Martin Date: Thu, 19 Sep 2024 09:38:57 -0400 Subject: [PATCH] Remove non-ascii header values instead of failing Signed-off-by: Lann Martin --- crates/factor-outbound-http/src/spin.rs | 49 ++++++++++--------------- 1 file changed, 19 insertions(+), 30 deletions(-) diff --git a/crates/factor-outbound-http/src/spin.rs b/crates/factor-outbound-http/src/spin.rs index f4d2e71b6..51d173e1a 100644 --- a/crates/factor-outbound-http/src/spin.rs +++ b/crates/factor-outbound-http/src/spin.rs @@ -148,21 +148,7 @@ fn hyper_method(m: Method) -> http::Method { async fn response_from_hyper(mut resp: crate::Response) -> Result { let status = resp.status().as_u16(); - let headers = resp - .headers() - .into_iter() - .map(|(key, val)| { - Ok(( - key.to_string(), - val.to_str() - .map_err(|_| { - tracing::error!("Non-ascii response header {key} = {val:?}"); - HttpError::RuntimeError - })? - .to_string(), - )) - }) - .collect::, _>>()?; + let headers = headers_from_map(resp.headers()); let body = resp .body_mut() @@ -205,21 +191,7 @@ fn log_reqwest_error(err: reqwest::Error) -> HttpError { async fn response_from_reqwest(res: reqwest::Response) -> Result { let status = res.status().as_u16(); - let headers = res - .headers() - .into_iter() - .map(|(key, val)| { - Ok(( - key.to_string(), - val.to_str() - .map_err(|_| { - tracing::error!("Non-ascii response header {key} = {val:?}"); - HttpError::RuntimeError - })? - .to_string(), - )) - }) - .collect::, _>>()?; + let headers = headers_from_map(res.headers()); let body = res .bytes() @@ -233,3 +205,20 @@ async fn response_from_reqwest(res: reqwest::Response) -> Result Vec<(String, String)> { + map.iter() + .filter_map(|(key, val)| { + Some(( + key.to_string(), + val.to_str() + .ok() + .or_else(|| { + tracing::warn!("Non-ascii response header value for {key}"); + None + })? + .to_string(), + )) + }) + .collect() +}