When saving to dest, check if we get redirected, and use the new
location header to set the dest file name if we only provided a target dir. Only save if dest not modified.
This commit is contained in:
parent
d5fdfe6c79
commit
ba55f8ed01
1 changed files with 52 additions and 22 deletions
74
uri
74
uri
|
@ -23,6 +23,7 @@
|
||||||
import shutil
|
import shutil
|
||||||
import tempfile
|
import tempfile
|
||||||
import base64
|
import base64
|
||||||
|
import datetime
|
||||||
try:
|
try:
|
||||||
import json
|
import json
|
||||||
except ImportError:
|
except ImportError:
|
||||||
|
@ -206,7 +207,7 @@ def write_file(module, url, dest, content):
|
||||||
else:
|
else:
|
||||||
if not os.access(os.path.dirname(dest), os.W_OK):
|
if not os.access(os.path.dirname(dest), os.W_OK):
|
||||||
os.remove(tmpsrc)
|
os.remove(tmpsrc)
|
||||||
module.fail_json( msg="Destination %s not writable" % (os.path.dirname(dest)))
|
module.fail_json( msg="Destination dir %s not writable" % (os.path.dirname(dest)))
|
||||||
|
|
||||||
if md5sum_src != md5sum_dest:
|
if md5sum_src != md5sum_dest:
|
||||||
try:
|
try:
|
||||||
|
@ -241,11 +242,44 @@ def uri(module, url, dest, user, password, body, method, headers, redirects, soc
|
||||||
module.fail_json(msg="Both a username and password need to be set.")
|
module.fail_json(msg="Both a username and password need to be set.")
|
||||||
if user is not None and password is not None:
|
if user is not None and password is not None:
|
||||||
h.add_credentials(user, password)
|
h.add_credentials(user, password)
|
||||||
|
|
||||||
|
# is dest is set and is a directory, let's check if we get redirected and
|
||||||
|
# set the filename from that url
|
||||||
|
redirected = False
|
||||||
|
resp_redir = {}
|
||||||
|
r = {}
|
||||||
|
if dest is not None:
|
||||||
|
dest = os.path.expanduser(dest)
|
||||||
|
if os.path.isdir(dest):
|
||||||
|
# first check if we are redirected to a file download
|
||||||
|
h.follow_redirects=False
|
||||||
|
# Try the request
|
||||||
|
try:
|
||||||
|
resp_redir, content_redir = h.request(url, method=method, body=body, headers=headers)
|
||||||
|
# if we are redirected, update the url with the location header,
|
||||||
|
# and update dest with the new url filename
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
if resp_redir['status'] in ["301", "302", "303", "307"]:
|
||||||
|
url = resp_redir['location']
|
||||||
|
redirected = True
|
||||||
|
dest = os.path.join(dest, url_filename(url))
|
||||||
|
# if destination file already exist, only download if file newer
|
||||||
|
if os.path.exists(dest):
|
||||||
|
t = datetime.datetime.utcfromtimestamp(os.path.getmtime(dest))
|
||||||
|
tstamp = t.strftime('%a, %d %b %Y %H:%M:%S +0000')
|
||||||
|
headers['If-Modified-Since'] = tstamp
|
||||||
|
|
||||||
|
# do safe redirects now, including 307
|
||||||
|
h.follow_redirects=True
|
||||||
|
|
||||||
# Make the request, or try to :)
|
# Make the request, or try to :)
|
||||||
try:
|
try:
|
||||||
resp, content = h.request(url, method=method, body=body, headers=headers)
|
resp, content = h.request(url, method=method, body=body, headers=headers)
|
||||||
return resp, content
|
r['redirected'] = redirected
|
||||||
|
r.update(resp_redir)
|
||||||
|
r.update(resp)
|
||||||
|
return r, content, dest
|
||||||
except httplib2.RedirectMissingLocation:
|
except httplib2.RedirectMissingLocation:
|
||||||
module.fail_json(msg="A 3xx redirect response code was provided but no Location: header was provided to point to the new location.")
|
module.fail_json(msg="A 3xx redirect response code was provided but no Location: header was provided to point to the new location.")
|
||||||
except httplib2.RedirectLimit:
|
except httplib2.RedirectLimit:
|
||||||
|
@ -343,26 +377,23 @@ def main():
|
||||||
else:
|
else:
|
||||||
redirects = False
|
redirects = False
|
||||||
|
|
||||||
# If there is a dest, expand it and get the filename if one not explicitly set.
|
|
||||||
if dest is not None:
|
|
||||||
dest = os.path.expanduser(dest)
|
|
||||||
if os.path.isdir(dest):
|
|
||||||
dest = os.path.join(dest, url_filename(url))
|
|
||||||
|
|
||||||
# Make the request
|
# Make the request
|
||||||
resp, content = uri(module, url, dest, user, password, body, method, dict_headers, redirects, socket_timeout)
|
resp, content, dest = uri(module, url, dest, user, password, body, method, dict_headers, redirects, socket_timeout)
|
||||||
|
|
||||||
# Write the file out if requested
|
# Write the file out if requested
|
||||||
if dest is not None:
|
if dest is not None:
|
||||||
write_file(module, url, dest, content)
|
if resp['status'] == "304":
|
||||||
|
status_code = "304"
|
||||||
# allow file attribute changes
|
changed = False
|
||||||
changed = True
|
else:
|
||||||
module.params['path'] = dest
|
write_file(module, url, dest, content)
|
||||||
file_args = module.load_file_common_arguments(module.params)
|
# allow file attribute changes
|
||||||
file_args['path'] = dest
|
changed = True
|
||||||
changed = module.set_file_attributes_if_different(file_args, changed)
|
module.params['path'] = dest
|
||||||
|
file_args = module.load_file_common_arguments(module.params)
|
||||||
|
file_args['path'] = dest
|
||||||
|
changed = module.set_file_attributes_if_different(file_args, changed)
|
||||||
|
resp['path'] = dest
|
||||||
|
|
||||||
# Transmogrify the headers, replacing '-' with '_', since variables dont work with dashes.
|
# Transmogrify the headers, replacing '-' with '_', since variables dont work with dashes.
|
||||||
uresp = {}
|
uresp = {}
|
||||||
|
@ -377,13 +408,12 @@ def main():
|
||||||
uresp['json'] = js
|
uresp['json'] = js
|
||||||
except:
|
except:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if resp['status'] != status_code:
|
if resp['status'] != status_code:
|
||||||
module.fail_json(msg="Status code was not " + status_code, content=content, **uresp)
|
module.fail_json(msg="Status code was not " + status_code, content=content, **uresp)
|
||||||
elif return_content:
|
elif return_content:
|
||||||
module.exit_json(changed=True, content=content, **uresp)
|
module.exit_json(changed=changed, content=content, **uresp)
|
||||||
else:
|
else:
|
||||||
module.exit_json(changed=True, **uresp)
|
module.exit_json(changed=changed, **uresp)
|
||||||
|
|
||||||
|
|
||||||
# this is magic, see lib/ansible/module_common.py
|
# this is magic, see lib/ansible/module_common.py
|
||||||
|
|
Loading…
Reference in a new issue