From cabca54d807dbb7fb12b43bfd42b303260b7e094 Mon Sep 17 00:00:00 2001 From: fyears <1142836+fyears@users.noreply.github.com> Date: Tue, 16 Jan 2024 00:54:11 +0800 Subject: [PATCH] universally change webdav header --- src/remoteForWebdav.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/src/remoteForWebdav.ts b/src/remoteForWebdav.ts index 79eaed5..e0b30cd 100644 --- a/src/remoteForWebdav.ts +++ b/src/remoteForWebdav.ts @@ -19,6 +19,15 @@ import type { // ResponseDataDetailed, } from "webdav"; +/** + * https://stackoverflow.com/questions/32850898/how-to-check-if-a-string-has-any-non-iso-8859-1-characters-with-javascript + * @param str + * @returns true if all are iso 8859 1 chars + */ +function onlyAscii(str: string) { + return !/[^\u0000-\u00ff]/g.test(str); +} + // @ts-ignore import { getPatcher } from "webdav/dist/web/index.js"; if (VALID_REQURL) { @@ -53,9 +62,24 @@ if (VALID_REQURL) { contentType = contentType.toLowerCase(); } const rspHeaders = { ...r.headers }; + console.log("rspHeaders"); + console.log(rspHeaders); for (let key in rspHeaders) { if (rspHeaders.hasOwnProperty(key)) { - if (key === "content-disposition" || key === "Content-Disposition") { + // avoid the error: + // Failed to read the 'headers' property from 'ResponseInit': String contains non ISO-8859-1 code point. + // const possibleNonAscii = [ + // "Content-Disposition", + // "X-Accel-Redirect", + // "X-Outfilename", + // "X-Sendfile" + // ]; + // for (const p of possibleNonAscii) { + // if (key === p || key === p.toLowerCase()) { + // rspHeaders[key] = encodeURIComponent(rspHeaders[key]); + // } + // } + if (!onlyAscii(rspHeaders[key])) { rspHeaders[key] = encodeURIComponent(rspHeaders[key]); } }