Skip to content

Commit

Permalink
Support non-ASCII headers returned by fetch (#23510)
Browse files Browse the repository at this point in the history
As an example, wordpress seems to use the `X-Olaf` header set to `⛄️` which would cause `fetch` to fail.

GitOrigin-RevId: b8f82419d87882ab6e77dfd446e214af9ca602bb
  • Loading branch information
sshader authored and Convex, Inc. committed Mar 15, 2024
1 parent 0cf1ecb commit 3244f43
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
6 changes: 4 additions & 2 deletions crates/isolate/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,11 @@ impl HttpResponseV8 {
// None as the HeaderName for headers with multiple values
// (https://docs.rs/http/latest/http/header/struct.HeaderMap.html#method.into_iter)
for (name, value) in &response.headers {
let value_str = value.to_str()?;
// Don't use `value.to_str()` since that does not support non-ASCII headers
let value_bytes = value.as_bytes();
let value_str: String = value_bytes.iter().map(|&c| c as char).collect();
let header_name_str = name.as_str();
header_pairs.push((header_name_str.to_string(), value_str.to_string()));
header_pairs.push((header_name_str.to_string(), value_str));
}
// reqwest does not expose status text sent in HTTP response, so we derive it
// from status code.
Expand Down
2 changes: 1 addition & 1 deletion npm-packages/udf-runtime/src/23_response.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export class Response {
statusText: this.statusText,
url: this._url,
};
return `Request ${inspect(properties)}`;
return `Response ${inspect(properties)}`;
}

// ---------------------------------------------------------------
Expand Down
10 changes: 10 additions & 0 deletions npm-packages/udf-tests/convex/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export default action(async () => {
fetchResponseStreamIsLockedWhileReading,
fetchResponseStreamIsLockedWhileReadingBlob,
fetchForbidden,
fetchOlaf,
});
});

Expand Down Expand Up @@ -1745,3 +1746,12 @@ export const fetchUnendingRequest = action(async () => {
await response.text();
throw new Error(`fetch should not complete`);
});

// Regression test for https://webtechsurvey.com/response-header/x-olaf
async function fetchOlaf() {
const response = await fetch("http://localhost:4545/echo_server", {
method: "POST",
headers: { "X-Olaf": "⛄" },
});
assert.strictEqual(response.headers.get("X-Olaf"), "â\x9B\x84");
}

0 comments on commit 3244f43

Please sign in to comment.