Skip to content

Commit

Permalink
Add charset parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
wearrrrr committed Dec 2, 2024
1 parent e5dff7f commit 0f3b3cc
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/worker/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,12 +314,25 @@ async function rewriteBody(
destination: RequestDestination,
workertype: string,
cookieStore: CookieStore
): Promise<BodyType> {
): Promise<BodyType> {;

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;
}
Expand Down

0 comments on commit 0f3b3cc

Please sign in to comment.