Skip to content


Subversion checkout URL

You can clone with
Download ZIP


BUG - CHttpRequest - the sort function for preferred languages and content types does not take into account the original item order #2429

Rupert-RR opened this Issue · 1 comment

4 participants


I have only just realised this, and for some reason I never came across a case where it was an issue before.
The sorting of the Accept and Accept-Language headers is done using the native usort function with a custom compare function. I had assumed that this would preserve the original order when items compared as equal, however it turns out that the usort function is not stable

The cmp_function doesn't keep the original order for elements comparing as equal

This means that lists of items with identical 'q' values will not necessarily be ordered correctly (earlier elements should have priority over later ones).

I will submit a pull request with a fix for this, and to make the getPreferredLanguages function more like its accept types counterpart (returning an array map, including the calculated 'q' values rather than just the language code, so that the content negotiation can use these 'q' values).

The original issues were #1673 for the Accept-Language parsing and #2131 and #2132 for the Accept header parsing.

@Rupert-RR Rupert-RR referenced this issue from a commit in Rupert-RR/yii
Rupert-RR Fixes issue #2429 by adding the class methods
parseAcceptLanguagesHeader() and compareAcceptLanguages as the
equivalents of the existing parseAcceptHeader() and
compareAcceptTypes(), and the instance method getAcceptLanguages() as
the equivalent of the existing getAcceptTypes().
Modified the parseAcceptHeader() method to include the original array
position in the resulting accept types array map.
Modified the compareAcceptTypes() method to take into account the
original position of the items when comparing is equal on all other
Modified the existing getPreferredLanguages() method to function in the
same way as the getPreferredAcceptTypes() method, and modified the
getPreferredLanguage() method to correctly interpret the resulting array
of array maps (rather than array of language strings as previously), so
that these changes should be transparent to anyone using this function.
Updated the unit tests for the parseAcceptHeader() and
compareAcceptTypes() methods and added tests for the new
parseAcceptLanguagesHeader() and compareAcceptLanguages() methods.
@cebe cebe was assigned

This seems to be fixed.

@samdark samdark closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.