This commit is contained in:
2026-10-19 00:03:48 +07:00
parent a58f0866d3
commit f74b2a8a41
8 changed files with 290 additions and 28 deletions
+10 -3
View File
@@ -1,13 +1,20 @@
export function byteStringResponseHeaders(headers: Record<string, string[]> = {}) {
export function byteStringResponseHeaders(headers: Record<string, string | string[]> = {}) {
return Object.fromEntries(Object.entries(headers).map(([name, values]) => [
name,
values.map((value) => value.replace(/[^\x00-\xff]+/gu, encodeURIComponent)),
(Array.isArray(values) ? values : [values]).map((value) => value.replace(/[^\x00-\xff]+/gu, encodeURIComponent)),
]))
}
export function prepareFetchHeaders(target: Electron.Session) {
// Electron net.fetch throws outside its promise when a response header contains Unicode.
target.webRequest.onHeadersReceived((details, callback) => {
callback({ responseHeaders: byteStringResponseHeaders(details.responseHeaders) })
callback({
responseHeaders: byteStringResponseHeaders(details.responseHeaders),
statusLine: asciiStatusLine(details.statusLine),
})
})
}
export function asciiStatusLine(line: string) {
return /[^\x20-\x7e]/.test(line) ? line.replace(/^(HTTP\/\S+\s+\d{3}).*$/, '$1 ') : line
}