summaryrefslogtreecommitdiff
path: root/lib/ansible/module_utils/urls.py
diff options
context:
space:
mode:
authorMatt Martz <matt@sivel.net>2021-07-28 15:58:12 -0500
committerGitHub <noreply@github.com>2021-07-28 15:58:12 -0500
commit4dca539a29aab08ccf6af6b3e8870e5c69150488 (patch)
tree525bdfe62307e47f75b8393ce61caf720af75353 /lib/ansible/module_utils/urls.py
parent767b2f07b00be12b9366655095cf24120d35092e (diff)
downloadansible-4dca539a29aab08ccf6af6b3e8870e5c69150488.tar.gz
Expose `unredirected_headers` to `uri` and `get_url` modules (#75308)
Diffstat (limited to 'lib/ansible/module_utils/urls.py')
-rw-r--r--lib/ansible/module_utils/urls.py17
1 files changed, 11 insertions, 6 deletions
diff --git a/lib/ansible/module_utils/urls.py b/lib/ansible/module_utils/urls.py
index 632387f672..df707a21c2 100644
--- a/lib/ansible/module_utils/urls.py
+++ b/lib/ansible/module_utils/urls.py
@@ -1436,9 +1436,9 @@ class Request:
request.add_header('If-Modified-Since', tstamp)
# user defined headers now, which may override things we've set above
- unredirected_headers = unredirected_headers or []
+ unredirected_headers = [h.lower() for h in (unredirected_headers or [])]
for header in headers:
- if header in unredirected_headers:
+ if header.lower() in unredirected_headers:
request.add_unredirected_header(header, headers[header])
else:
request.add_header(header, headers[header])
@@ -1689,7 +1689,7 @@ def url_argument_spec():
def fetch_url(module, url, data=None, headers=None, method=None,
use_proxy=True, force=False, last_mod_time=None, timeout=10,
- use_gssapi=False, unix_socket=None, ca_path=None, cookies=None):
+ use_gssapi=False, unix_socket=None, ca_path=None, cookies=None, unredirected_headers=None):
"""Sends a request via HTTP(S) or FTP (needs the module as parameter)
:arg module: The AnsibleModule (used to get username, password etc. (s.b.).
@@ -1706,6 +1706,8 @@ def fetch_url(module, url, data=None, headers=None, method=None,
:kwarg unix_socket: (optional) String of file system path to unix socket file to use when establishing
connection to the provided url
:kwarg ca_path: (optional) String of file system path to CA cert bundle to use
+ :kwarg cookies: (optional) CookieJar object to send with the request
+ :kwarg unredirected_headers: (optional) A list of headers to not attach on a redirected request
:returns: A tuple of (**response**, **info**). Use ``response.read()`` to read the data.
The **info** contains the 'status' and other meta data. When a HttpError (status >= 400)
@@ -1758,7 +1760,7 @@ 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, use_gssapi=use_gssapi,
- unix_socket=unix_socket, ca_path=ca_path)
+ unix_socket=unix_socket, ca_path=ca_path, unredirected_headers=unredirected_headers)
# 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()))
@@ -1831,7 +1833,8 @@ def fetch_url(module, url, data=None, headers=None, method=None,
def fetch_file(module, url, data=None, headers=None, method=None,
- use_proxy=True, force=False, last_mod_time=None, timeout=10):
+ use_proxy=True, force=False, last_mod_time=None, timeout=10,
+ unredirected_headers=None):
'''Download and save a file via HTTP(S) or FTP (needs the module as parameter).
This is basically a wrapper around fetch_url().
@@ -1845,6 +1848,7 @@ def fetch_file(module, url, data=None, headers=None, method=None,
:kwarg boolean force: If True: Do not get a cached copy (Default: False)
:kwarg last_mod_time: Default: None
:kwarg int timeout: Default: 10
+ :kwarg unredirected_headers: (optional) A list of headers to not attach on a redirected request
:returns: A string, the path to the downloaded file.
'''
@@ -1854,7 +1858,8 @@ def fetch_file(module, url, data=None, headers=None, method=None,
fetch_temp_file = tempfile.NamedTemporaryFile(dir=module.tmpdir, prefix=file_name, suffix=file_ext, delete=False)
module.add_cleanup_file(fetch_temp_file.name)
try:
- rsp, info = fetch_url(module, url, data, headers, method, use_proxy, force, last_mod_time, timeout)
+ rsp, info = fetch_url(module, url, data, headers, method, use_proxy, force, last_mod_time, timeout,
+ unredirected_headers=unredirected_headers)
if not rsp:
module.fail_json(msg="Failure downloading %s, %s" % (url, info['msg']))
data = rsp.read(bufsize)