When force=yes, get_url should always download the specified file

This is accomplished by not setting the If-Modified-Since header,
and setting "cache-control: no-cache" instead. Note that if the
file content has not changed, the module will still report that
changed=false, as the md5's of the tmp file and existing file are
compared before swapping

Fixes #5104
This commit is contained in:
James Cammarata 2014-02-05 09:45:24 -06:00
parent 552ce8b68c
commit b8356b525a

View file

@ -130,7 +130,7 @@ def url_filename(url):
return 'index.html'
return fn
def url_do_get(module, url, dest, use_proxy, last_mod_time):
def url_do_get(module, url, dest, use_proxy, last_mod_time, force):
"""
Get url and return request and info
Credits: http://stackoverflow.com/questions/7006574/how-to-download-file-from-ftp
@ -176,9 +176,11 @@ def url_do_get(module, url, dest, use_proxy, last_mod_time):
request = urllib2.Request(url)
request.add_header('User-agent', USERAGENT)
if last_mod_time:
if last_mod_time and not force:
tstamp = last_mod_time.strftime('%a, %d %b %Y %H:%M:%S +0000')
request.add_header('If-Modified-Since', tstamp)
else:
request.add_header('cache-control', 'no-cache')
try:
r = urllib2.urlopen(request)
@ -194,16 +196,15 @@ def url_do_get(module, url, dest, use_proxy, last_mod_time):
return r, info
def url_get(module, url, dest, use_proxy, last_mod_time):
def url_get(module, url, dest, use_proxy, last_mod_time, force):
"""
Download data from the url and store in a temporary file.
Return (tempfile, info about the request)
"""
req, info = url_do_get(module, url, dest, use_proxy, last_mod_time)
req, info = url_do_get(module, url, dest, use_proxy, last_mod_time, force)
# TODO: should really handle 304, but how? src file could exist (and be newer) but empty
if info['status'] == 304:
module.exit_json(url=url, dest=dest, changed=False, msg=info.get('msg', ''))
@ -283,7 +284,7 @@ def main():
last_mod_time = datetime.datetime.utcfromtimestamp(mtime)
# download to tmpsrc
tmpsrc, info = url_get(module, url, dest, use_proxy, last_mod_time)
tmpsrc, info = url_get(module, url, dest, use_proxy, last_mod_time, force)
# Now the request has completed, we can finally generate the final
# destination file name from the info dict.