Handle HTTPError being partially initialized due to the error. Fixes #76386 (#76421)

pull/76478/head
Matt Martz 3 years ago committed by GitHub
parent 82ff7efa94
commit eca97a19a3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,4 @@
bugfixes:
- urls/uri - Address case where ``HTTPError`` isn't fully initialized due to
the error, and is missing certain methods and attributes
(https://github.com/ansible/ansible/issues/76386)

@ -1839,6 +1839,11 @@ def fetch_url(module, url, data=None, headers=None, method=None,
except urllib_error.HTTPError as e:
r = e
try:
if e.fp is None:
# Certain HTTPError objects may not have the ability to call ``.read()`` on Python 3
# This is not handled gracefully in Python 3, and instead an exception is raised from
# tempfile, due to ``urllib.response.addinfourl`` not being initialized
raise AttributeError
body = e.read()
except AttributeError:
body = ''

@ -697,7 +697,9 @@ def main():
filename = get_response_filename(r) or 'index.html'
dest = os.path.join(dest, filename)
if r:
if r and r.fp is not None:
# r may be None for some errors
# r.fp may be None depending on the error, which means there are no headers either
content_type, main_type, sub_type, content_encoding = parse_content_type(r)
else:
content_type = 'application/octet-stream'
@ -710,7 +712,7 @@ def main():
if maybe_output:
try:
if PY3 and r.closed:
if PY3 and (r.fp is None or r.closed):
raise TypeError
content = r.read()
except (AttributeError, TypeError):

@ -247,6 +247,16 @@
headers:
Cookie: "fake=fake_value"
- name: test digest auth failure
uri:
url: 'https://{{ httpbin_host }}/digest-auth/auth/user/passwd'
user: user
password: wrong
headers:
Cookie: "fake=fake_value"
register: result
failed_when: result.status != 401
- name: test unredirected_headers
uri:
url: 'https://{{ httpbin_host }}/redirect-to?status_code=301&url=/basic-auth/user/passwd'

Loading…
Cancel
Save