// Ignore anything after the invalid second filename*0.
if(n===0){
break;
}
continue;
}
matches[n]=[quot,part];
}
letparts=[];
for(letn=0;n<matches.length;++n){
if(!(ninmatches)){
// Numbers must be consecutive. Truncate when there is a hole.
break;
}
let[quot,part]=matches[n];
part=rfc2616unquote(part);
if(quot){
part=unescape(part);
if(n===0){
part=rfc5987decode(part);
}
}
parts.push(part);
}
returnparts.join('');
}
functionrfc2616unquote(value){
if(value.charAt(0)==='"'){
letparts=value.slice(1).split('\\"');
// Find the first unescaped " and terminate there.
for(leti=0;i<parts.length;++i){
letquotindex=parts[i].indexOf('"');
if(quotindex!==-1){
parts[i]=parts[i].slice(0,quotindex);
parts.length=i+1;// Truncates and stop the iteration.
}
parts[i]=parts[i].replace(/\\(.)/g,'$1');
}
value=parts.join('"');
}
returnvalue;
}
functionrfc5987decode(extvalue){
// Decodes "ext-value" from RFC 5987.
letencodingend=extvalue.indexOf('\'');
if(encodingend===-1){
// Some servers send "filename*=" without encoding'language' prefix,
// e.g. in https://github.com/Rob--W/open-in-browser/issues/26
// Let's accept the value like Firefox (57) (Chrome 62 rejects it).
returnextvalue;
}
letencoding=extvalue.slice(0,encodingend);
letlangvalue=extvalue.slice(encodingend+1);
// Ignore language (RFC 5987 section 3.2.1, and RFC 6266 section 4.1 ).
letvalue=langvalue.replace(/^[^']*'/,'');
returntextdecode(encoding,value);
}
functionrfc2047decode(value){
// RFC 2047-decode the result. Firefox tried to drop support for it, but
// backed out because some servers use it - https://bugzil.la/875615
// Firefox's condition for decoding is here: https://searchfox.org/mozilla-central/rev/4a590a5a15e35d88a3b23dd6ac3c471cf85b04a8/netwerk/mime/nsMIMEHeaderParamImpl.cpp#742-748
// We are more strict and only recognize RFC 2047-encoding if the value
// starts with "=?", since then it is likely that the full value is
// RFC 2047-encoded.
// Firefox also decodes words even where RFC 2047 section 5 states:
// "An 'encoded-word' MUST NOT appear within a 'quoted-string'."