-
Notifications
You must be signed in to change notification settings - Fork 23.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handle duplicate headers in the uri module #33792
Conversation
CI failure unrelated to PR. Restarting CI. |
@@ -1049,11 +1049,36 @@ 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())) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like a bug fix. If that's the case it should go into a separate PR, preferably along with tests.
Also, doesn't this cause potential issues with backwards compatibility?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose it could be considered a bug fix.
While writing tests I noticed that py2 gives lowercased names, while py3 gives capitalization of the name from the response.
I can't see how it would cause a potential backwards compat issue. I went with the py2 default, because all of our modules should support py2. The only way it could cause a backwards compat issue, is if a module was calling fetch_url
and only supported python3. If they supported both py2 and py3, then the code would have to already handle lowercase and mixed case, so it should work as expected.
This normalizes/standardizes return values from this method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, in general, this whole PR is a bug fix. Largely due to difference in behavior of python3 vs python2. The goal is to normalize the functionality.
The only place not a bugfix is the addition of cookies_string
. That was added to ease issues related to the original bug report of missing duplicate headers. However, due to predictability issues reconstructing the string from the headers, I would consider that part of the bugfix as well.
lib/ansible/module_utils/urls.py
Outdated
_h[_k] = ', '.join((_h[_k], v)) | ||
else: | ||
_h[_k] = v | ||
info.update(_h) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use more descriptive variable names for at least _h
and _k
, and probably k
and v
also. I think it's fine to use some single letter variables in list/dict comprehensions or simple loops, but once there is some additional logic I think readability is improved by using longer names.
Also, it should be fine to reassign the lowercase version of k
(or whatever it ends up being) to itself since the original value isn't used anywhere else and the type hasn't changed.
lib/ansible/module_utils/urls.py
Outdated
# 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 duplciate headers |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix spelling of duplicate
.
lib/ansible/module_utils/urls.py
Outdated
else: | ||
_h[_k] = v | ||
info.update(_h) | ||
except AttributeError: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What causes AttributeError
to be raised?
@mattclay most comments addressed in a recent commit. I added a comment to the untouched line around concern of backwards incompat. |
Hi, Kudo @sivel. Regards, |
* 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.
SUMMARY
...and make it easier for users to use cookies by providing a pre-built string
FIxes #33325
ISSUE TYPE
COMPONENT NAME
uri
ANSIBLE VERSION
ADDITIONAL INFORMATION