Handle multiple Content-Type headers correctly (#31238)
* Handle multiple Content-Type headers correctly Avoids situations where mulitple Content-Type headers including charset information can result in errors like ``` LookupError: unknown encoding: UTF-8, text/html ``` * Account for multiple conflicting values for content-type and charset * Add changelog fragment
This commit is contained in:
parent
943b4f5a83
commit
1cf43e5017
2 changed files with 27 additions and 3 deletions
3
changelogs/fragments/31238-uri-multi-content-type.yaml
Normal file
3
changelogs/fragments/31238-uri-multi-content-type.yaml
Normal file
|
@ -0,0 +1,3 @@
|
|||
bugfixes:
|
||||
- uri - Handle multiple Content-Type headers correctly
|
||||
(https://github.com/ansible/ansible/pull/31238)
|
|
@ -654,9 +654,30 @@ def main():
|
|||
# Default content_encoding to try
|
||||
content_encoding = 'utf-8'
|
||||
if 'content_type' in uresp:
|
||||
content_type, params = cgi.parse_header(uresp['content_type'])
|
||||
if 'charset' in params:
|
||||
content_encoding = params['charset']
|
||||
# Handle multiple Content-Type headers
|
||||
charsets = []
|
||||
content_types = []
|
||||
for value in uresp['content_type'].split(','):
|
||||
ct, params = cgi.parse_header(value)
|
||||
if ct not in content_types:
|
||||
content_types.append(ct)
|
||||
if 'charset' in params:
|
||||
if params['charset'] not in charsets:
|
||||
charsets.append(params['charset'])
|
||||
|
||||
if content_types:
|
||||
content_type = content_types[0]
|
||||
if len(content_types) > 1:
|
||||
module.warn(
|
||||
'Received multiple conflicting Content-Type values (%s), using %s' % (', '.join(content_types), content_type)
|
||||
)
|
||||
if charsets:
|
||||
content_encoding = charsets[0]
|
||||
if len(charsets) > 1:
|
||||
module.warn(
|
||||
'Received multiple conflicting charset values (%s), using %s' % (', '.join(charsets), content_encoding)
|
||||
)
|
||||
|
||||
u_content = to_text(content, encoding=content_encoding)
|
||||
if any(candidate in content_type for candidate in JSON_CANDIDATES):
|
||||
try:
|
||||
|
|
Loading…
Reference in a new issue