﻿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
37542	Unhandled ValueError from malformed tokens in HttpRequest.accepted_types	Anisayak Mitra	Anisayak Mitra	"=== Problem Description ===
In django/http/request.py, HttpRequest.accepted_types parses the HTTP Accept header by tokenizing the header and instantiating MediaType(token) for each token.

When a client sends an Accept header containing tokens that trigger a ValueError during parameter parsing in parse_header_parameters (for example, tokens exceeding MAX_HEADER_LENGTH or invalid RFC 2231 encoded parameter values), MediaType(token) raises ValueError.

Currently, this ValueError is unhandled inside HttpRequest.accepted_types. As a result, incoming requests containing malformed media tokens in the Accept header crash with an unhandled exception (HTTP 500 Internal Server Error) instead of gracefully discarding the invalid token.

=== Steps to Reproduce ===
{{{
#!python
from django.test import RequestFactory

rf = RequestFactory()
request = rf.get(""/"", HTTP_ACCEPT=""text/html, "" + ""a"" * 1000)
# Raises unhandled ValueError: token is too long
print(request.accepted_types)
}}}

=== Expected Behavior ===
HttpRequest.accepted_types should catch ValueError during token parsing and discard invalid tokens, allowing valid media types in the header (e.g. text/html) to be processed normally. Similarly, HttpRequest.accepted_type and MediaType.match should return None and False respectively when encountering malformed tokens.

=== Solution & Pull Request ===
A patch with complete test coverage has been submitted:
Pull Request: https://github.com/django/django/pull/22342

All 126 tests in requests_tests (including 27 in requests_tests.test_accept_header) pass."	Bug	assigned	HTTP handling	6.1	Normal		accepted_types, MediaType, ValueError	Anisayak Mitra	Unreviewed	1	0	0	0	0	0
