From 0f3b3cc8ab747c3c98638512dd0be599a4167afa Mon Sep 17 00:00:00 2001 From: wearrrrr Date: Sun, 1 Dec 2024 20:55:25 -0600 Subject: [PATCH] Add charset parsing --- src/worker/fetch.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/worker/fetch.ts b/src/worker/fetch.ts index e5954fe..9fe9d0c 100644 --- a/src/worker/fetch.ts +++ b/src/worker/fetch.ts @@ -314,12 +314,25 @@ async function rewriteBody( destination: RequestDestination, workertype: string, cookieStore: CookieStore -): Promise { +): Promise {; + switch (destination) { case "iframe": case "document": + if (response.headers.get("content-type")?.startsWith("text/html")) { - return rewriteHtml(await response.text(), cookieStore, meta, true); + const buf = await response.arrayBuffer(); + const decode = new TextDecoder("utf-8").decode(buf); + const charsetHeader = response.headers.get("content-type") ; + const charset = charsetHeader?.split("charset=")[1] || decode.match(/charset=([^"]+)/)?.[1] || "utf-8"; + const htmlContent = charset ? new TextDecoder(charset).decode(buf) : decode; + + return rewriteHtml( + htmlContent, + cookieStore, + meta, + true + ); } else { return response.body; }