JSFiddle - React, Tailwind, and code Playground
by nilgundag
JavaScript
/* eslint-disable */
// https://github.com/Rob--W/open-in-browser/blob/master/extension/content-disposition.js
function getFilenameFromContentDispositionHeader(contentDisposition) {
// This parser is designed to be tolerant and accepting of headers that do
// not comply with the standard, but accepted by Firefox.
let needsEncodingFixup = true;
// filename*=ext-value ("ext-value" from RFC 5987, referenced by RFC 6266).
let tmp = toParamRegExp('filename\\*', 'i').exec(contentDisposition);
if (tmp) {
tmp = tmp[1];
let filename = rfc2616unquote(tmp);
filename = unescape(filename);
filename = rfc5987decode(filename);
filename = rfc2047decode(filename);
return fixupEncoding(filename);
}
// Continuations (RFC 2231 section 3, referenced by RFC 5987 section 3.1).
// filename*n*=part
// filename*n=part
tmp = rfc2231getparam(contentDisposition);
if (tmp) {
// RFC 2047, section
let filename = rfc2047decode(tmp);
return fixupEncoding(filename);
}
// filename=value (RFC 5987, section 4.1).
tmp = toParamRegExp('filename', 'i').exec(contentDisposition);
if (tmp) {
tmp = tmp[1];
let filename = rfc2616unquote(tmp);
filename = rfc2047decode(filename);
return fixupEncoding(filename);
}
return '';
function toParamRegExp(attributePattern, flags) {
return new RegExp(
'(?:^|;)\\s*' + attributePattern + '\\s*=\\s*' +
// Captures: value = token | quoted-string
// (RFC 2616, section 3.6 and referenced by RFC 6266 4.1)
'(' +
'[^";\\s][^;\\s]*' +
'|' +
'"(?:[^"\\\\]|\\\\"?)+"?' +
')', flags);
}
function textdecode(encoding, value) {
if (encoding) {
try {
let decoder = new TextDecoder(encoding, { fatal: true });
let bytes = Array.from(value, c => c.charCodeAt(0));
if (bytes.every(code => code <= 0xFF)) {
value = decoder.decode(new Uint8Array(bytes));
needsEncodingFixup = false;
}
...