Support non-utf8 charsets

This commit is contained in:
Gregory Schier
2024-09-05 13:58:06 -07:00
parent 48b288b1a6
commit c663537ca9
2 changed files with 16 additions and 2 deletions

View File

@@ -46,3 +46,8 @@ export function modelsEq(a: Model, b: Model) {
export function getContentTypeHeader(headers: HttpResponseHeader[]): string | null {
return headers.find((h) => h.name.toLowerCase() === 'content-type')?.value ?? null;
}
export function getCharsetFromContentType(headers: HttpResponseHeader[]): string | null {
const contentType = getContentTypeHeader(headers);
return contentType?.match(/charset=([^ ;]+)/)?.[1] ?? null;
}

View File

@@ -1,9 +1,18 @@
import { readFile, readTextFile } from '@tauri-apps/plugin-fs';
import { readFile } from '@tauri-apps/plugin-fs';
import type { HttpResponse } from '@yaakapp/api';
import { getCharsetFromContentType } from './models';
export async function getResponseBodyText(response: HttpResponse): Promise<string | null> {
if (response.bodyPath) {
return await readTextFile(response.bodyPath);
const bytes = await readFile(response.bodyPath);
const charset = getCharsetFromContentType(response.headers);
try {
return new TextDecoder(charset ?? 'utf-8', { fatal: true }).decode(bytes);
} catch (_) {
// Failed to decode as text, so return null
return null;
}
}
return null;
}