Skip to content

Commit 8752dff

Browse files
authored
Merge pull request #2848 from lann/remove-non-ascii-headers
Remove non-ascii header values instead of failing
2 parents 31aa7ec + 32f2ffd commit 8752dff

File tree

1 file changed

+19
-30
lines changed
  • crates/factor-outbound-http/src

1 file changed

+19
-30
lines changed

crates/factor-outbound-http/src/spin.rs

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -148,21 +148,7 @@ fn hyper_method(m: Method) -> http::Method {
148148
async fn response_from_hyper(mut resp: crate::Response) -> Result<Response, HttpError> {
149149
let status = resp.status().as_u16();
150150

151-
let headers = resp
152-
.headers()
153-
.into_iter()
154-
.map(|(key, val)| {
155-
Ok((
156-
key.to_string(),
157-
val.to_str()
158-
.map_err(|_| {
159-
tracing::error!("Non-ascii response header {key} = {val:?}");
160-
HttpError::RuntimeError
161-
})?
162-
.to_string(),
163-
))
164-
})
165-
.collect::<Result<Vec<_>, _>>()?;
151+
let headers = headers_from_map(resp.headers());
166152

167153
let body = resp
168154
.body_mut()
@@ -205,21 +191,7 @@ fn log_reqwest_error(err: reqwest::Error) -> HttpError {
205191
async fn response_from_reqwest(res: reqwest::Response) -> Result<Response, HttpError> {
206192
let status = res.status().as_u16();
207193

208-
let headers = res
209-
.headers()
210-
.into_iter()
211-
.map(|(key, val)| {
212-
Ok((
213-
key.to_string(),
214-
val.to_str()
215-
.map_err(|_| {
216-
tracing::error!("Non-ascii response header {key} = {val:?}");
217-
HttpError::RuntimeError
218-
})?
219-
.to_string(),
220-
))
221-
})
222-
.collect::<Result<Vec<_>, _>>()?;
194+
let headers = headers_from_map(res.headers());
223195

224196
let body = res
225197
.bytes()
@@ -233,3 +205,20 @@ async fn response_from_reqwest(res: reqwest::Response) -> Result<Response, HttpE
233205
body: Some(body),
234206
})
235207
}
208+
209+
fn headers_from_map(map: &http::HeaderMap) -> Vec<(String, String)> {
210+
map.iter()
211+
.filter_map(|(key, val)| {
212+
Some((
213+
key.to_string(),
214+
val.to_str()
215+
.ok()
216+
.or_else(|| {
217+
tracing::warn!("Non-ascii response header value for {key}");
218+
None
219+
})?
220+
.to_string(),
221+
))
222+
})
223+
.collect()
224+
}

0 commit comments

Comments
 (0)