requests icon indicating copy to clipboard operation
requests copied to clipboard

get_encoding_from_headers is case-sensitive to media type tokens (RFC 9110 §8.3.1)

Open Parveshkumar2211 opened this issue 2 weeks ago • 1 comments

Summary

requests.utils.get_encoding_from_headers is case-sensitive with respect to the media type token because _parse_content_type_header does not normalize content_type to lower case.

RFC 9110 §8.3.1 (HTTP Semantics) explicitly specifies:

"The type, subtype, and parameter names of media types are case-insensitive." (See also RFC 2045 §5.1 and RFC 7231 §3.1.1.1).

When an HTTP response contains uppercase or mixed-case media types without an explicit charset (e.g., Content-Type: TEXT/PLAIN or Content-Type: APPLICATION/JSON), get_encoding_from_headers fails to match "text" or "application/json", returning None.

When Response.encoding is None, Response.text falls back to heuristic encoding detection (apparent_encoding via chardet / charset_normalizer), which frequently misidentifies Latin-1 byte sequences (e.g., guessing windows-1255) and produces corrupted mojibake instead of the RFC-mandated ISO-8859-1 fallback.

Expected Result

get_encoding_from_headers should normalize the media type token to lower case (or perform a case-insensitive check):

  • Content-Type: TEXT/PLAIN -> 'ISO-8859-1'
  • Content-Type: Text/Plain -> 'ISO-8859-1'
  • Content-Type: APPLICATION/JSON -> 'utf-8'

Actual Result

get_encoding_from_headers returns None for uppercase or mixed-case media types:

  • Content-Type: TEXT/PLAIN -> None
  • Content-Type: Text/Plain -> None
  • Content-Type: APPLICATION/JSON -> None

Reproduction Steps

from requests.structures import CaseInsensitiveDict
from requests.utils import get_encoding_from_headers
import requests

# 1. Direct utility demonstration
h_lower = CaseInsensitiveDict({"content-type": "text/plain"})
h_upper = CaseInsensitiveDict({"content-type": "TEXT/PLAIN"})
h_json_upper = CaseInsensitiveDict({"content-type": "APPLICATION/JSON"})

print("text/plain:", get_encoding_from_headers(h_lower))        # 'ISO-8859-1'
print("TEXT/PLAIN:", get_encoding_from_headers(h_upper))        # None (expected 'ISO-8859-1')
print("APPLICATION/JSON:", get_encoding_from_headers(h_json_upper)) # None (expected 'utf-8')

# 2. Impact on Response.text decoding with Latin-1 bytes
content = b"\xe0\xe9\xee\xf4\xfb" * 10  # French accented characters in ISO-8859-1

r_lower = requests.Response()
r_lower.headers = h_lower
r_lower.encoding = get_encoding_from_headers(r_lower.headers)
r_lower._content = content

r_upper = requests.Response()
r_upper.headers = h_upper
r_upper.encoding = get_encoding_from_headers(r_upper.headers)
r_upper._content = content

print("Lowercase text:", repr(r_lower.text[:20]))  # 'àéîôûàéîôûàéîôûàéîôû' (correct)
print("Uppercase text:", repr(r_upper.text[:20]))  # 'אימפאימפאימפאימפ' (mojibake: apparent_encoding guessed windows-1255)

System Information

{
  "chardet": {
    "version": "5.2.0"
  },
  "charset_normalizer": {
    "version": "3.4.4"
  },
  "cryptography": {
    "version": "46.0.3"
  },
  "idna": {
    "version": "3.11"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.13.9"
  },
  "platform": {
    "release": "25.6.0",
    "system": "Darwin"
  },
  "pyOpenSSL": {
    "openssl_version": "30000120",
    "version": "25.3.0"
  },
  "requests": {
    "version": "2.32.5"
  },
  "system_ssl": {
    "version": "30000120"
  },
  "urllib3": {
    "version": "2.5.0"
  },
  "using_charset_normalizer": false,
  "using_pyopenssl": true
}

Parveshkumar2211 avatar Nov 14 '26 05:11 Parveshkumar2211

I have a tested fix for this on a branch: Sun-Wukhan/requests fix/encoding-case-insensitive-media-type. get_encoding_from_headers now lowercases the media type before the text/json checks, so TEXT/PLAIN and APPLICATION/JSON resolve correctly, while an explicit charset is still returned exactly as sent. Tests are in tests/test_utils.py. PR creation is restricted to collaborators on this repo, so I cannot open a PR directly; happy for a maintainer to pull the branch or advise another route.

Sun-Wukhan avatar Nov 14 '26 16:11 Sun-Wukhan