mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-23 01:08:28 +02:00
Support non-utf8 charsets
This commit is contained in:
@@ -46,3 +46,8 @@ export function modelsEq(a: Model, b: Model) {
|
|||||||
export function getContentTypeHeader(headers: HttpResponseHeader[]): string | null {
|
export function getContentTypeHeader(headers: HttpResponseHeader[]): string | null {
|
||||||
return headers.find((h) => h.name.toLowerCase() === 'content-type')?.value ?? 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;
|
||||||
|
}
|
||||||
|
|||||||
@@ -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 type { HttpResponse } from '@yaakapp/api';
|
||||||
|
import { getCharsetFromContentType } from './models';
|
||||||
|
|
||||||
export async function getResponseBodyText(response: HttpResponse): Promise<string | null> {
|
export async function getResponseBodyText(response: HttpResponse): Promise<string | null> {
|
||||||
if (response.bodyPath) {
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user