From 3244f4334d430b66dd1507f1d853f19ed9f597ac Mon Sep 17 00:00:00 2001 From: Sarah Shader Date: Fri, 15 Mar 2024 13:31:36 -0400 Subject: [PATCH] Support non-ASCII headers returned by fetch (#23510) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit As an example, wordpress seems to use the `X-Olaf` header set to `⛄️` which would cause `fetch` to fail. GitOrigin-RevId: b8f82419d87882ab6e77dfd446e214af9ca602bb --- crates/isolate/src/http.rs | 6 ++++-- npm-packages/udf-runtime/src/23_response.ts | 2 +- npm-packages/udf-tests/convex/fetch.ts | 10 ++++++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/crates/isolate/src/http.rs b/crates/isolate/src/http.rs index 1a5e2aee..19ad0eca 100644 --- a/crates/isolate/src/http.rs +++ b/crates/isolate/src/http.rs @@ -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. diff --git a/npm-packages/udf-runtime/src/23_response.ts b/npm-packages/udf-runtime/src/23_response.ts index 3fe6ba91..eec566da 100644 --- a/npm-packages/udf-runtime/src/23_response.ts +++ b/npm-packages/udf-runtime/src/23_response.ts @@ -271,7 +271,7 @@ export class Response { statusText: this.statusText, url: this._url, }; - return `Request ${inspect(properties)}`; + return `Response ${inspect(properties)}`; } // --------------------------------------------------------------- diff --git a/npm-packages/udf-tests/convex/fetch.ts b/npm-packages/udf-tests/convex/fetch.ts index 0a16932b..f13ec846 100644 --- a/npm-packages/udf-tests/convex/fetch.ts +++ b/npm-packages/udf-tests/convex/fetch.ts @@ -111,6 +111,7 @@ export default action(async () => { fetchResponseStreamIsLockedWhileReading, fetchResponseStreamIsLockedWhileReadingBlob, fetchForbidden, + fetchOlaf, }); }); @@ -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"); +}