Skip to content
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

Merged
merged 7 commits into from
Apr 10, 2018
Merged

Conversation

sivel
Copy link
Member

@sivel sivel commented Dec 11, 2017

SUMMARY

...and make it easier for users to use cookies by providing a pre-built string

FIxes #33325

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

uri

ANSIBLE VERSION
devel
ADDITIONAL INFORMATION

@ansibot ansibot added affects_2.5 This issue/PR affects Ansible v2.5 bugfix_pull_request needs_triage Needs a first human triage before being processed. support:core This issue/PR relates to code supported by the Ansible Engineering Team. ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Dec 11, 2017
@mkrizek mkrizek removed the needs_triage Needs a first human triage before being processed. label Dec 15, 2017
@ansibot ansibot removed the ci_verified Changes made in this PR are causing tests to fail. label Dec 21, 2017
@ansibot ansibot added stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Dec 31, 2017
@mattclay
Copy link
Member

mattclay commented Jan 3, 2018

CI failure unrelated to PR. Restarting CI.

@ansibot ansibot added needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Jan 3, 2018
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Jan 11, 2018
@ansibot ansibot added bug This issue/PR relates to a bug. and removed bugfix_pull_request labels Mar 2, 2018
@sivel sivel added this to the 2.7.0 milestone Mar 2, 2018
@ansible ansible deleted a comment from ansibot Mar 27, 2018
@ansible ansible deleted a comment from ansibot Mar 27, 2018
@ansibot
Copy link
Contributor

ansibot commented Mar 28, 2018

@ansibot ansibot added net_tools Net-tools category test This PR relates to tests. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Mar 28, 2018
@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Apr 9, 2018
@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Apr 9, 2018
@ansibot ansibot added needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. and removed needs_ci This PR requires CI testing to be performed. Please close and re-open this PR to trigger CI. labels Apr 9, 2018
@@ -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()))
Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member Author

@sivel sivel Apr 9, 2018

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.

_h[_k] = ', '.join((_h[_k], v))
else:
_h[_k] = v
info.update(_h)
Copy link
Member

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.

# 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix spelling of duplicate.

else:
_h[_k] = v
info.update(_h)
except AttributeError:
Copy link
Member

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?

@ansibot ansibot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Apr 9, 2018
@sivel
Copy link
Member Author

sivel commented Apr 9, 2018

@mattclay most comments addressed in a recent commit. I added a comment to the untouched line around concern of backwards incompat.

@ansibot ansibot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. label Apr 10, 2018
@sivel sivel merged commit 450cfa8 into ansible:devel Apr 10, 2018
@bersace
Copy link

bersace commented Jun 14, 2018

Hi,

Kudo @sivel. Do you have some input on multiple cookies in uri modules ? Unlike what's stated in PR title, the diff seems to modify get_url module. Am I wrong ? The integration test playbook is explicit. Thanks.

Regards,
Étienne

felixfontein added a commit to felixfontein/ansible that referenced this pull request Sep 19, 2018
felixfontein added a commit to felixfontein/ansible that referenced this pull request Sep 28, 2018
ilicmilan pushed a commit to ilicmilan/ansible that referenced this pull request Nov 7, 2018
* 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.
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.5 This issue/PR affects Ansible v2.5 bug This issue/PR relates to a bug. net_tools Net-tools category support:core This issue/PR relates to code supported by the Ansible Engineering Team. test This PR relates to tests.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

uri module doesn’t handle multiple cookies with same name
5 participants