From 487c0469c6ec81b47ac4209364dc2e5e672e949a Mon Sep 17 00:00:00 2001 From: Cieons <109508894+cieons@users.noreply.github.com> Date: Fri, 13 Sep 2024 23:10:45 +0800 Subject: [PATCH] fix(route): people charset decoding (#16734) (#16743) --- lib/routes/people/index.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/routes/people/index.ts b/lib/routes/people/index.ts index 48c352385..9543a452a 100644 --- a/lib/routes/people/index.ts +++ b/lib/routes/people/index.ts @@ -32,8 +32,12 @@ async function handler(ctx) { responseType: 'buffer', }); - const encoding = site === 'www' ? 'gbk' : 'utf-8'; - const decodedResponse = iconv.decode(response, encoding); + // not seen Content-Type in response headers + // try to parse charset from meta tag + let decodedResponse = iconv.decode(response, 'utf-8'); + const parsedCharset = decodedResponse.match(/]+)["']?/i); + const encoding = parsedCharset ? parsedCharset[1].toLowerCase() : 'utf-8'; + decodedResponse = encoding === 'utf-8' ? decodedResponse : iconv.decode(response, encoding); const $ = load(decodedResponse); $('em').remove();