Skip to content

Commit

Permalink
Introduce and use locale-naive rfc2822 date format function. Fixes an…
Browse files Browse the repository at this point in the history
  • Loading branch information
sivel committed Aug 29, 2018
1 parent cac51e6 commit dffa42b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/urls-if-modified-since.yaml
@@ -0,0 +1,2 @@
bugfixes:
- get_url / uri - Use custom rfc2822 date format function instead of locale specific strftime (https://github.com/ansible/ansible/issues/44857)
19 changes: 18 additions & 1 deletion lib/ansible/module_utils/urls.py
Expand Up @@ -822,6 +822,23 @@ def maybe_add_ssl_handler(url, validate_certs):
return SSLValidationHandler(hostname, port)


def rfc2822_date_string(timetuple, zone='-0000'):
"""Accepts a timetuple and optional zone which defaults to ``-0000``
and returns a date string as specified by RFC 2822, e.g.:
Fri, 09 Nov 2001 01:08:47 -0000
Copied from email.utils.formatdate and modified for separate use
"""
return '%s, %02d %s %04d %02d:%02d:%02d %s' % (
['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'][timetuple[6]],
timetuple[2],
['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'][timetuple[1] - 1],
timetuple[0], timetuple[3], timetuple[4], timetuple[5],
zone)


class Request:
def __init__(self, headers=None, use_proxy=True, force=False, timeout=10, validate_certs=True,
url_username=None, url_password=None, http_agent=None, force_basic_auth=False,
Expand Down Expand Up @@ -1037,7 +1054,7 @@ def open(self, method, url, data=None, headers=None, use_proxy=None,
request.add_header('cache-control', 'no-cache')
# or we do it if the original is more recent than our copy
elif last_mod_time:
tstamp = last_mod_time.strftime('%a, %d %b %Y %H:%M:%S +0000')
tstamp = rfc2822_date_string(last_mod_time.timetuple())
request.add_header('If-Modified-Since', tstamp)

# user defined headers now, which may override things we've set above
Expand Down
7 changes: 3 additions & 4 deletions lib/ansible/modules/net_tools/basics/uri.py
Expand Up @@ -405,6 +405,7 @@ def uri(module, url, dest, body, body_format, method, headers, socket_timeout):
else:
data = body

kwargs = {}
if dest is not None:
# Stash follow_redirects, in this block we don't want to follow
# we'll reset back to the supplied value soon
Expand All @@ -424,15 +425,13 @@ def uri(module, url, dest, body, body_format, method, headers, socket_timeout):
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
kwargs['last_mod_time'] = datetime.datetime.utcfromtimestamp(os.path.getmtime(dest))

# Reset follow_redirects back to the stashed value
module.params['follow_redirects'] = follow_redirects

resp, info = fetch_url(module, url, data=data, headers=headers,
method=method, timeout=socket_timeout)
method=method, timeout=socket_timeout, **kwargs)

try:
content = resp.read()
Expand Down

0 comments on commit dffa42b

Please sign in to comment.