Skip to content

Commit

Permalink
Remove non-ascii header values instead of failing
Browse files Browse the repository at this point in the history
Signed-off-by: Lann Martin <[email protected]>
  • Loading branch information
lann committed Sep 19, 2024
1 parent 1e3d971 commit 32f2ffd
Showing 1 changed file with 19 additions and 30 deletions.
49 changes: 19 additions & 30 deletions crates/factor-outbound-http/src/spin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,7 @@ fn hyper_method(m: Method) -> http::Method {
async fn response_from_hyper(mut resp: crate::Response) -> Result<Response, HttpError> {
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::<Result<Vec<_>, _>>()?;
let headers = headers_from_map(resp.headers());

let body = resp
.body_mut()
Expand Down Expand Up @@ -205,21 +191,7 @@ fn log_reqwest_error(err: reqwest::Error) -> HttpError {
async fn response_from_reqwest(res: reqwest::Response) -> Result<Response, HttpError> {
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::<Result<Vec<_>, _>>()?;
let headers = headers_from_map(res.headers());

let body = res
.bytes()
Expand All @@ -233,3 +205,20 @@ async fn response_from_reqwest(res: reqwest::Response) -> Result<Response, HttpE
body: Some(body),
})
}

fn headers_from_map(map: &http::HeaderMap) -> 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()
}

0 comments on commit 32f2ffd

Please sign in to comment.