forked from oakserver/oak
-
Notifications
You must be signed in to change notification settings - Fork 0
/
content_disposition.ts
143 lines (130 loc) · 3.95 KB
/
content_disposition.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/**
* Adapted directly from content-disposition.js at
* https://github.com/Rob--W/open-in-browser/blob/master/extension/content-disposition.js
* which is licensed as:
*
* (c) 2017 Rob Wu <[email protected]> (https://robwu.nl)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
import { toParamRegExp, unquote } from "./headers.ts";
let needsEncodingFixup = false;
function fixupEncoding(value: string): string {
if (needsEncodingFixup && /[\x80-\xff]/.test(value)) {
value = textDecode("utf-8", value);
if (needsEncodingFixup) {
value = textDecode("iso-8859-1", value);
}
}
return value;
}
const FILENAME_STAR_REGEX = toParamRegExp("filename\\*", "i");
const FILENAME_START_ITER_REGEX = toParamRegExp(
"filename\\*((?!0\\d)\\d+)(\\*?)",
"ig",
);
const FILENAME_REGEX = toParamRegExp("filename", "i");
function rfc2047decode(value: string): string {
// deno-lint-ignore no-control-regex
if (!value.startsWith("=?") || /[\x00-\x19\x80-\xff]/.test(value)) {
return value;
}
return value.replace(
/=\?([\w-]*)\?([QqBb])\?((?:[^?]|\?(?!=))*)\?=/g,
(_: string, charset: string, encoding: string, text: string) => {
if (encoding === "q" || encoding === "Q") {
text = text.replace(/_/g, " ");
text = text.replace(
/=([0-9a-fA-F]{2})/g,
(_, hex) => String.fromCharCode(parseInt(hex, 16)),
);
return textDecode(charset, text);
}
try {
text = atob(text);
// deno-lint-ignore no-empty
} catch {}
return textDecode(charset, text);
},
);
}
function rfc2231getParam(header: string): string {
const matches: [string, string][] = [];
let match: RegExpExecArray | null;
while ((match = FILENAME_START_ITER_REGEX.exec(header))) {
const [, ns, quote, part] = match;
const n = parseInt(ns, 10);
if (n in matches) {
if (n === 0) {
break;
}
continue;
}
matches[n] = [quote, part];
}
const parts: string[] = [];
for (let n = 0; n < matches.length; ++n) {
if (!(n in matches)) {
break;
}
let [quote, part] = matches[n];
part = unquote(part);
if (quote) {
part = unescape(part);
if (n === 0) {
part = rfc5987decode(part);
}
}
parts.push(part);
}
return parts.join("");
}
function rfc5987decode(value: string): string {
const encodingEnd = value.indexOf(`'`);
if (encodingEnd === -1) {
return value;
}
const encoding = value.slice(0, encodingEnd);
const langValue = value.slice(encodingEnd + 1);
return textDecode(encoding, langValue.replace(/^[^']*'/, ""));
}
function textDecode(encoding: string, value: string): string {
if (encoding) {
try {
const decoder = new TextDecoder(encoding, { fatal: true });
const bytes = Array.from(value, (c) => c.charCodeAt(0));
if (bytes.every((code) => code <= 0xFF)) {
value = decoder.decode(new Uint8Array(bytes));
needsEncodingFixup = false;
}
// deno-lint-ignore no-empty
} catch {}
}
return value;
}
export function getFilename(header: string): string {
needsEncodingFixup = true;
// filename*=ext-value ("ext-value" from RFC 5987, referenced by RFC 6266).
let matches = FILENAME_STAR_REGEX.exec(header);
if (matches) {
const [, filename] = matches;
return fixupEncoding(
rfc2047decode(rfc5987decode(unescape(unquote(filename)))),
);
}
// Continuations (RFC 2231 section 3, referenced by RFC 5987 section 3.1).
// filename*n*=part
// filename*n=part
const filename = rfc2231getParam(header);
if (filename) {
return fixupEncoding(rfc2047decode(filename));
}
// filename=value (RFC 5987, section 4.1).
matches = FILENAME_REGEX.exec(header);
if (matches) {
const [, filename] = matches;
return fixupEncoding(rfc2047decode(unquote(filename)));
}
return "";
}