Project

General

Profile

« Previous | Next » 

Revision d911bf37

Added by koszko over 1 year ago

fix encoding detection in StreamFilter

View differences:

background/stream_filter.js
35 35
function validate_encoding(charset)
36 36
{
37 37
    try {
38
	new TextDecoder();
38
	new TextDecoder(charset);
39 39
	return charset;
40 40
    } catch(e) {
41 41
	return undefined;
......
44 44

  
45 45
function is_content_type_header(header)
46 46
{
47
    header.name.toLowerCase().trim() === "content-type";
47
    return header.name.toLowerCase().trim() === "content-type";
48 48
}
49 49

  
50 50
const charset_reg = /;\s*charset\s*=\s*([\w-]+)/i;
......
55 55

  
56 56
    for (const header of headers.filter(is_content_type_header)) {
57 57
	const match = charset_reg.exec(header.value);
58
	if (!properties.detected_charset && validate_encoding(match[1]))
58
	if (match && !properties.detected_charset &&
59
	    validate_encoding(match[1]))
59 60
	    properties.detected_charset = match[1];
60 61

  
61 62
	if (/html/i.test(header.value))
......
105 106
function create_decoder(properties, data)
106 107
{
107 108
    let charset = charset_from_BOM(data) || properties.detected_charset;
108
    if (!charset && data.indexOf(0) !== -1) {
109

  
110
    if (charset)
111
	return new TextDecoder(charset);
112

  
113
    if (data.indexOf(0) !== -1) {
109 114
        console.warn("Haketilo: zeroes in bytestream, probable cached encoding mismatch. Trying to decode it as UTF-16.",
110 115
		     properties);
111 116
	return new TextDecoder("utf-16be");

Also available in: Unified diff