Skip to content

get_encoding_from_headers fails if charset name not specified #6646

Open
@batterseapower

Description

requests.utils.get_encoding_from_headers assumes that the charset parameter always specifies a name. In very rare cases a server can send a malformed content-type header which does not specify a name. In these cases, requests should probably just treat it as if no charset had been specified.

Expected Result

requests.utils.get_encoding_from_headers({'content-type': 'text/html; charset'}) == 'ISO-8859-1'

Actual Result

File ~/opt/anaconda3/2023.03/envs/mamba/envs/py3/lib/python3.9/site-packages/requests/utils.py:553, in get_encoding_from_headers(headers)
    550 content_type, params = _parse_content_type_header(content_type)
    552 if "charset" in params:
--> 553     return params["charset"].strip("'\"")
    555 if "text" in content_type:
    556     return "ISO-8859-1"

AttributeError: 'bool' object has no attribute 'strip'

System Information

{
  "chardet": {
    "version": "4.0.0"
  },
  "charset_normalizer": {
    "version": "2.0.4"
  },
  "cryptography": {
    "version": "41.0.3"
  },
  "idna": {
    "version": "3.4"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.9.15"
  },
  "platform": {
    "release": "5.14.0-284.11.1.el9_2.x86_64",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "1010116f",
    "version": "23.2.0"
  },
  "requests": {
    "version": "2.31.0"
  },
  "system_ssl": {
    "version": "1010117f"
  },
  "urllib3": {
    "version": "1.26.18"
  },
  "using_charset_normalizer": false,
  "using_pyopenssl": true
}

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions