jshttp / negotiator

An HTTP content negotiator for Node.js
MIT License
309 stars 33 forks source link

Sorting of equal quality #22

Closed silentjohnny closed 9 years ago

silentjohnny commented 10 years ago

This was an issue I raised before with Express when the sorting of accept headers was still done there. Since v8 uses quicksort (which is not a stable sort) for arrays longer than 10 elements, the sort order of 'equal' elements is not guaranteed to be the same as the original array. If you put this to the test in for example preferred languages, you get the following results:

nl;q=0.5,fr,de,en,it,es,pt,no,se,fi

should sort as

fr,de,en,it,es,pt,no,se,fi,nl

likewise,

nl;q=0.5,fr,de,en,it,es,pt,no,se,fi,ro (which has 11 elements)

should sort as

fr,de,en,it,es,pt,no,se,fi,ro,nl

but instead sorts as

ro,de,en,it,fr,pt,no,se,fi,es,nl

Not sure if this should be a real problem, but I think people assume that the order of the preferred languages in their browser is honored. But of course only if they have more than 10 preferred languages.

Any thoughts on whether this is a problem or not? I see a closed issue from TJ from last year.

federomero commented 10 years ago

I couldn't find anything in the specs saying that the languages that appear first in the Accept-Language header should have higher priority than the rest.

I would consider making this change if there was any indication that this is expected in practice but I haven't found anything so far.

dougwilson commented 10 years ago

To help decide, from RFC 7231 5.3.5:

Note that some recipients treat the order in which language tags are listed as an indication of descending priority, particularly for tags that are assigned equal quality values (no value is the same as q=1). However, this behavior cannot be relied upon. For consistency and to maximize interoperability, many user agents assign each language tag a unique quality value while also listing them in order of decreasing quality. Additional discussion of language priority lists can be found in Section 2.3 of RFC4647.

jonathanong commented 10 years ago

i'd prefer not to make this change and return types as i, the developer, supplies them to negotiator

silentjohnny commented 10 years ago

That would be nice, but this is exactly not what V8 does. Array#sort changes the original order. Which means you can not assume.