﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
36447	HttpRequest.get_preferred_type misorders types when more specific accepted types have lower q	Anders Kaseorg	Jake Howard	"Consider this example from [https://www.rfc-editor.org/rfc/rfc9110.html#section-12.5.1-18 RFC 9110 §12.5.1] (adjusted for [https://www.rfc-editor.org/errata/eid7138 erratum 7138]):

  The media type quality factor associated with a given type is determined by finding the media range with the highest precedence that matches the type. For example,
  {{{
  Accept: text/*;q=0.3, text/plain;q=0.7, text/plain;format=flowed,
         text/plain;format=fixed;q=0.4, */*;q=0.5
  }}}
  would cause the following values to be associated:
  ||= Media Type =||= Quality Value =||
  || text/plain;format=flowed || 1 ||
  || text/plain || 0.7 ||
  || text/html || 0.3 ||
  || image/jpeg || 0.5 ||
  || text/plain;format=fixed || 0.4 ||
  || text/html;level=3 || 0.3 ||

Django’s `HttpRequest.get_preferred_type` fails to match this behavior.

{{{
>>> from django.conf import settings
>>> from django.http import HttpRequest
>>> settings.configure()
>>> request = HttpRequest()
>>> request.META[""HTTP_ACCEPT""] = ""text/*;q=0.3, text/plain;q=0.7, text/plain;format=flowed, text/plain;format=fixed;q=0.4, */*;q=0.5""
>>> request.get_preferred_type(['text/plain', 'text/plain;format=fixed']) # expected text/plain (0.7 > 0.4)
'text/plain;format=fixed'
>>> request.get_preferred_type(['text/html', 'image/jpeg']) # expected image/jpeg (0.3 < 0.5)
'text/html'
>>> request.get_preferred_type(['image/jpeg', 'text/html']) # expected image/jpeg (0.5 > 0.3)
'text/html'
>>> request.get_preferred_type(['image/jpeg', 'text/plain;format=fixed']) # expected image/jpeg (0.5 > 0.4)
'text/plain;format=fixed'
>>> request.get_preferred_type(['image/jpeg', 'text/html;level=3']) # expected image/jpeg (0.5 > 0.3)
'text/html;level=3'
>>> request.get_preferred_type(['text/plain;format=fixed', 'text/plain']) # expected text/plain (0.4 < 0.7)
'text/plain;format=fixed'
>>> request.get_preferred_type(['text/plain;format=fixed', 'image/jpeg']) # expected image/jpeg (0.4 < 0.5)
'text/plain;format=fixed'
>>> request.get_preferred_type(['text/html;level=3', 'image/jpeg']) # expected image/jpeg (0.3 < 0.5)
'text/html;level=3'
}}}"	Bug	assigned	HTTP handling	5.2	Release blocker		preferred media type	Anders Kaseorg Rodrigo Vieira Jake Howard	Accepted	1	0	0	0	0	0
