Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use loop for acceptParams (4.x backport) #6176

Open
wants to merge 2 commits into
base: 4.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions History.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ unreleased

* deps: [email protected]
- Throws an error on invalid path values
* perf: use loop for accceptParams

4.21.1 / 2024-10-08
==========
Expand Down
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
Loading