mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-23 05:10:22 -07:00
Handle duplicate headers in the uri module (#33792)
* Handle duplicate headers, and make it easier for users to use cookies, by providing a pre-built string * Ensure proper cookie ordering, make key plural * Add note about cookie sort order * Add tests for duplicate headers and cookies_string * Extend tests, normalize headers between py2 and py3 * Add some notes in test code * Don't use AttributeError, use six.PY3. Use better names.
This commit is contained in:
parent
f1082af73f
commit
450cfa8776
3 changed files with 55 additions and 2 deletions
|
@ -51,6 +51,9 @@ except ImportError:
|
|||
import ansible.module_utils.six.moves.http_cookiejar as cookiejar
|
||||
import ansible.module_utils.six.moves.urllib.request as urllib_request
|
||||
import ansible.module_utils.six.moves.urllib.error as urllib_error
|
||||
|
||||
from ansible.module_utils.six import PY3
|
||||
|
||||
from ansible.module_utils.basic import get_distribution
|
||||
from ansible.module_utils._text import to_bytes, to_native, to_text
|
||||
|
||||
|
@ -1049,11 +1052,33 @@ def fetch_url(module, url, data=None, headers=None, method=None,
|
|||
url_password=password, http_agent=http_agent, force_basic_auth=force_basic_auth,
|
||||
follow_redirects=follow_redirects, client_cert=client_cert,
|
||||
client_key=client_key, cookies=cookies)
|
||||
info.update(r.info())
|
||||
# Lowercase keys, to conform to py2 behavior, so that py3 and py2 are predictable
|
||||
info.update(dict((k.lower(), v) for k, v in r.info().items()))
|
||||
|
||||
# Don't be lossy, append header values for duplicate headers
|
||||
# In Py2 there is nothing that needs done, py2 does this for us
|
||||
if PY3:
|
||||
temp_headers = {}
|
||||
for name, value in r.headers.items():
|
||||
# The same as above, lower case keys to match py2 behavior, and create more consistent results
|
||||
name = name.lower()
|
||||
if name in temp_headers:
|
||||
temp_headers[name] = ', '.join((temp_headers[name], value))
|
||||
else:
|
||||
temp_headers[name] = value
|
||||
info.update(temp_headers)
|
||||
|
||||
# parse the cookies into a nice dictionary
|
||||
cookie_list = []
|
||||
cookie_dict = dict()
|
||||
# Python sorts cookies in order of most specific (ie. longest) path first. See ``CookieJar._cookie_attrs``
|
||||
# Cookies with the same path are reversed from response order.
|
||||
# This code makes no assumptions about that, and accepts the order given by python
|
||||
for cookie in cookies:
|
||||
cookie_dict[cookie.name] = cookie.value
|
||||
cookie_list.append((cookie.name, cookie.value))
|
||||
info['cookies_string'] = '; '.join('%s=%s' % c for c in cookie_list)
|
||||
|
||||
info['cookies'] = cookie_dict
|
||||
# finally update the result with a message about the fetch
|
||||
info.update(dict(msg="OK (%s bytes)" % r.headers.get('Content-Length', 'unknown'), url=r.geturl(), status=r.code))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue