django-rest-framework icon indicating copy to clipboard operation
django-rest-framework copied to clipboard

Incorrect Accept header prioritizing with parameters

Open halfstrik opened this issue 6 years ago • 2 comments

Checklist

  • [x] I have verified that that issue exists against the master branch of Django REST framework.
  • [x] I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • [x] This is not a usage question. (Those should be directed to the discussion group instead.)
  • [x] This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • [x] I have reduced the issue to the simplest possible case.
  • [ ] I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Description

Google Chrome accept headers get prioritized in incorrect order. Here are headers my Chrome provides and how order_by_precedence prioritizes them:

>>> from rest_framework.utils.mediatypes import order_by_precedence
>>> accepts = ['text/html', 'application/xhtml+xml', 'application/xml;q=0.9', 'image/webp', 'image/apng', '*/*;q=0.8', 'application/signed-exchange;v=b3;q=0.9']                                                                                              
>>> order_by_precedence(accepts)
[{'application/signed-exchange;v=b3;q=0.9'}, {'application/xml;q=0.9', 'application/xhtml+xml', 'text/html', 'image/apng', 'image/webp'}, {'*/*;q=0.8'}]

This is happening b.c order_by_precedence prioritizes the highest if header has a parameter that is not "q". In my example, even though application/signed-exchange;v=b3;q=0.9 has parameter v=b3 I do not think it should be prioritized at the top (at least since it also has q=0.9). See https://github.com/encode/django-rest-framework/blob/master/rest_framework/utils/mediatypes.py#L71-L77

As a result of this instead of showing HTML page with BrowsableAPI - Chrome downloads a file (uses different Renderer with less priority).

Same page works fine in all other browsers.

Steps to reproduce

Execute code in the example above.

Expected behavior

The first set returned by order_by_precedence should be empty leaving 'text/html' to be the most prioritized.

Actual behavior

The first set contains application/signed-exchange;v=b3;q=0.9 - making it the most prioritized.

Suggested solution

If header has q parameter never prioritize it higher than headers without it. Please let me know your thoughts, I can come up with a PR later.

halfstrik avatar May 20 '20 20:05 halfstrik

From https://www.django-rest-framework.org/api-guide/content-negotiation/:

Note: "q" values are not taken into account by REST framework when determining preference. The use of "q" values negatively impacts caching, and in the author's opinion they are an unnecessary and overcomplicated approach to content negotiation.

This is a valid approach as the HTTP spec deliberately underspecifies how a server should weight server-based preferences against client-based preferences.

I believe this means what you're seeing is by design.

seawolf42 avatar Jun 17 '20 02:06 seawolf42

@seawolf42 , got it! Thank you! It make sense, let's close this! We will try to work around this in a different way. Or may be Google Chrome will fix it's headers in the next versions.

halfstrik avatar Jun 18 '20 19:06 halfstrik