From c05bad9f743ccbd479ea9528d5e23bb904a61be8 Mon Sep 17 00:00:00 2001 From: Michael Riss Date: Tue, 30 Aug 2016 11:54:22 +0200 Subject: [PATCH] Improved caching for urls - When there is no file at the destination yet, we have no modification time for the `If-Modified-Since`-Header. In this case trust the cache to make the right decision to either serve a cached version or to refresh from origin. This should help with mass-deployment scenarios where you want to use a local cache to relieve your uplink. - If you don't trust the cache to make the right decision you can still force it to refresh by providing the `force: yes` option. --- lib/ansible/module_utils/urls.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/ansible/module_utils/urls.py b/lib/ansible/module_utils/urls.py index 9d8422f3a7b..0d7e7d2edec 100644 --- a/lib/ansible/module_utils/urls.py +++ b/lib/ansible/module_utils/urls.py @@ -848,13 +848,14 @@ def open_url(url, data=None, headers=None, method=None, use_proxy=True, if http_agent: request.add_header('User-agent', http_agent) - # if we're ok with getting a 304, set the timestamp in the - # header, otherwise make sure we don't get a cached copy - if last_mod_time and not force: + # Cache control + # Either we directly force a cache refresh + if force: + 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') request.add_header('If-Modified-Since', tstamp) - else: - request.add_header('cache-control', 'no-cache') # user defined headers now, which may override things we've set above if headers: