UNPKG

6.07 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2019 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * Javascript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.getFilenameFromContentDispositionHeader = getFilenameFromContentDispositionHeader;
28
29function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); }
30
31function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); }
32
33function _iterableToArrayLimit(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
34
35function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
36
37function getFilenameFromContentDispositionHeader(contentDisposition) {
38 var needsEncodingFixup = true;
39 var tmp = toParamRegExp('filename\\*', 'i').exec(contentDisposition);
40
41 if (tmp) {
42 tmp = tmp[1];
43 var filename = rfc2616unquote(tmp);
44 filename = unescape(filename);
45 filename = rfc5987decode(filename);
46 filename = rfc2047decode(filename);
47 return fixupEncoding(filename);
48 }
49
50 tmp = rfc2231getparam(contentDisposition);
51
52 if (tmp) {
53 var _filename = rfc2047decode(tmp);
54
55 return fixupEncoding(_filename);
56 }
57
58 tmp = toParamRegExp('filename', 'i').exec(contentDisposition);
59
60 if (tmp) {
61 tmp = tmp[1];
62
63 var _filename2 = rfc2616unquote(tmp);
64
65 _filename2 = rfc2047decode(_filename2);
66 return fixupEncoding(_filename2);
67 }
68
69 function toParamRegExp(attributePattern, flags) {
70 return new RegExp('(?:^|;)\\s*' + attributePattern + '\\s*=\\s*' + '(' + '[^";\\s][^;\\s]*' + '|' + '"(?:[^"\\\\]|\\\\"?)+"?' + ')', flags);
71 }
72
73 function textdecode(encoding, value) {
74 if (encoding) {
75 if (!/^[\x00-\xFF]+$/.test(value)) {
76 return value;
77 }
78
79 try {
80 var decoder = new TextDecoder(encoding, {
81 fatal: true
82 });
83 var bytes = Array.from(value, function (ch) {
84 return ch.charCodeAt(0) & 0xFF;
85 });
86 value = decoder.decode(new Uint8Array(bytes));
87 needsEncodingFixup = false;
88 } catch (e) {
89 if (/^utf-?8$/i.test(encoding)) {
90 try {
91 value = decodeURIComponent(escape(value));
92 needsEncodingFixup = false;
93 } catch (err) {}
94 }
95 }
96 }
97
98 return value;
99 }
100
101 function fixupEncoding(value) {
102 if (needsEncodingFixup && /[\x80-\xff]/.test(value)) {
103 value = textdecode('utf-8', value);
104
105 if (needsEncodingFixup) {
106 value = textdecode('iso-8859-1', value);
107 }
108 }
109
110 return value;
111 }
112
113 function rfc2231getparam(contentDisposition) {
114 var matches = [],
115 match;
116 var iter = toParamRegExp('filename\\*((?!0\\d)\\d+)(\\*?)', 'ig');
117
118 while ((match = iter.exec(contentDisposition)) !== null) {
119 var _match = match,
120 _match2 = _slicedToArray(_match, 4),
121 n = _match2[1],
122 quot = _match2[2],
123 part = _match2[3];
124
125 n = parseInt(n, 10);
126
127 if (n in matches) {
128 if (n === 0) {
129 break;
130 }
131
132 continue;
133 }
134
135 matches[n] = [quot, part];
136 }
137
138 var parts = [];
139
140 for (var n = 0; n < matches.length; ++n) {
141 if (!(n in matches)) {
142 break;
143 }
144
145 var _matches$n = _slicedToArray(matches[n], 2),
146 quot = _matches$n[0],
147 part = _matches$n[1];
148
149 part = rfc2616unquote(part);
150
151 if (quot) {
152 part = unescape(part);
153
154 if (n === 0) {
155 part = rfc5987decode(part);
156 }
157 }
158
159 parts.push(part);
160 }
161
162 return parts.join('');
163 }
164
165 function rfc2616unquote(value) {
166 if (value.startsWith('"')) {
167 var parts = value.slice(1).split('\\"');
168
169 for (var i = 0; i < parts.length; ++i) {
170 var quotindex = parts[i].indexOf('"');
171
172 if (quotindex !== -1) {
173 parts[i] = parts[i].slice(0, quotindex);
174 parts.length = i + 1;
175 }
176
177 parts[i] = parts[i].replace(/\\(.)/g, '$1');
178 }
179
180 value = parts.join('"');
181 }
182
183 return value;
184 }
185
186 function rfc5987decode(extvalue) {
187 var encodingend = extvalue.indexOf('\'');
188
189 if (encodingend === -1) {
190 return extvalue;
191 }
192
193 var encoding = extvalue.slice(0, encodingend);
194 var langvalue = extvalue.slice(encodingend + 1);
195 var value = langvalue.replace(/^[^']*'/, '');
196 return textdecode(encoding, value);
197 }
198
199 function rfc2047decode(value) {
200 if (!value.startsWith('=?') || /[\x00-\x19\x80-\xff]/.test(value)) {
201 return value;
202 }
203
204 return value.replace(/=\?([\w-]*)\?([QqBb])\?((?:[^?]|\?(?!=))*)\?=/g, function (_, charset, encoding, text) {
205 if (encoding === 'q' || encoding === 'Q') {
206 text = text.replace(/_/g, ' ');
207 text = text.replace(/=([0-9a-fA-F]{2})/g, function (_, hex) {
208 return String.fromCharCode(parseInt(hex, 16));
209 });
210 return textdecode(charset, text);
211 }
212
213 try {
214 text = atob(text);
215 } catch (e) {}
216
217 return textdecode(charset, text);
218 });
219 }
220
221 return '';
222}
\No newline at end of file