Skip to content

Commit

Permalink
Use loop for acceptParams (#6066)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey authored and jonchurch committed Nov 14, 2024
1 parent 59fc270 commit 09089d5
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,33 @@ exports.contentDisposition = deprecate.function(contentDisposition,
*/

function acceptParams (str) {
var parts = str.split(/ *; */);
var ret = { value: parts[0], quality: 1, params: {} }
var length = str.length;
var colonIndex = str.indexOf(';');
var index = colonIndex === -1 ? length : colonIndex;
var ret = { value: str.slice(0, index).trim(), quality: 1, params: {} };

for (var i = 1; i < parts.length; ++i) {
var pms = parts[i].split(/ *= */);
if ('q' === pms[0]) {
ret.quality = parseFloat(pms[1]);
while (index < length) {
var splitIndex = str.indexOf('=', index);
if (splitIndex === -1) break;

var colonIndex = str.indexOf(';', index);
var endIndex = colonIndex === -1 ? length : colonIndex;

if (splitIndex > endIndex) {
index = str.lastIndexOf(';', splitIndex - 1) + 1;
continue;
}

var key = str.slice(index, splitIndex).trim();
var value = str.slice(splitIndex + 1, endIndex).trim();

if (key === 'q') {
ret.quality = parseFloat(value);
} else {
ret.params[pms[0]] = pms[1];
ret.params[key] = value;
}

index = endIndex + 1;
}

return ret;
Expand Down

0 comments on commit 09089d5

Please sign in to comment.