To parse HTTP headers, the http.client module uses the email.parser.Parser class, as headers are in the same format. The relevant function for this is _parse_header_lines. Because http.server uses http.client to parse its headers, this function is also responsible for parsing headers on the server side.
However, _parse_header_lines doesn't pass an email policy to email.parser.Parser. The documentation mentions that a policy should always be provided, and that the default policy will change in the future, so it's probably a good idea to change the code to use a specific policy.
The current default policy for email.parser.Parser is email.policy.compat32, which is a bit outdated but provides complete backwards compatibility. Because the documentation says that this won't be the case forever, I suggest that we change _parse_header_lines to use email.policy.default instead. While this is a breaking change, it would greatly extend support for the many extra RFCs that email.policy.default already supports.
One of these RFCs in particular that this change would start supporting is RFC 2047, which includes receiving encoded-word header values and would resolve part of issue #105530.
Note: this is also on discuss.python.org.
Linked PRs
To parse HTTP headers, the
http.clientmodule uses theemail.parser.Parserclass, as headers are in the same format. The relevant function for this is_parse_header_lines. Becausehttp.serveruseshttp.clientto parse its headers, this function is also responsible for parsing headers on the server side.However,
_parse_header_linesdoesn't pass an email policy toemail.parser.Parser. The documentation mentions that a policy should always be provided, and that the default policy will change in the future, so it's probably a good idea to change the code to use a specific policy.The current default policy for
email.parser.Parserisemail.policy.compat32, which is a bit outdated but provides complete backwards compatibility. Because the documentation says that this won't be the case forever, I suggest that we change_parse_header_linesto useemail.policy.defaultinstead. While this is a breaking change, it would greatly extend support for the many extra RFCs thatemail.policy.defaultalready supports.One of these RFCs in particular that this change would start supporting is RFC 2047, which includes receiving encoded-word header values and would resolve part of issue #105530.
Note: this is also on discuss.python.org.
Linked PRs