summaryrefslogtreecommitdiff
path: root/django/utils/cache.py
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2005-10-29 17:00:20 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2005-10-29 17:00:20 +0000
commitf12e3243326a2b6b0d09206b373b34e028eab25c (patch)
tree706dab0c8455592749216f2ca724bdc1ec178c98 /django/utils/cache.py
parent67d490a61dc5ee42b972f6c64bf589fbfc8db83f (diff)
downloaddjango-f12e3243326a2b6b0d09206b373b34e028eab25c.tar.gz
Fixed #612 - added cache control headers (thanks, hugo)
git-svn-id: http://code.djangoproject.com/svn/django/trunk@1020 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/utils/cache.py')
-rw-r--r--django/utils/cache.py42
1 files changed, 40 insertions, 2 deletions
diff --git a/django/utils/cache.py b/django/utils/cache.py
index fcd0825a22..631ea8f08d 100644
--- a/django/utils/cache.py
+++ b/django/utils/cache.py
@@ -21,6 +21,45 @@ import datetime, md5, re
from django.conf import settings
from django.core.cache import cache
+cc_delim_re = re.compile(r'\s*,\s*')
+def patch_cache_control(response, **kwargs):
+ """
+ This function patches the Cache-Control header by adding all
+ keyword arguments to it. The transformation is as follows:
+
+ - all keyword parameter names are turned to lowercase and
+ all _ will be translated to -
+ - if the value of a parameter is True (exatly True, not just a
+ true value), only the parameter name is added to the header
+ - all other parameters are added with their value, after applying
+ str to it.
+ """
+
+ def dictitem(s):
+ t = s.split('=',1)
+ if len(t) > 1:
+ return (t[0].lower().replace('-', '_'), t[1])
+ else:
+ return (t[0].lower().replace('-', '_'), True)
+
+ def dictvalue(t):
+ if t[1] == True:
+ return t[0]
+ else:
+ return t[0] + '=' + str(t[1])
+
+ if response.has_header('Cache-Control'):
+ print response['Cache-Control']
+ cc = cc_delim_re.split(response['Cache-Control'])
+ print cc
+ cc = dict([dictitem(el) for el in cc])
+ else:
+ cc = {}
+ for (k,v) in kwargs.items():
+ cc[k.replace('_', '-')] = v
+ cc = ', '.join([dictvalue(el) for el in cc.items()])
+ response['Cache-Control'] = cc
+
vary_delim_re = re.compile(r',\s*')
def patch_response_headers(response, cache_timeout=None):
@@ -43,8 +82,7 @@ def patch_response_headers(response, cache_timeout=None):
response['Last-Modified'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
if not response.has_header('Expires'):
response['Expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S GMT')
- if not response.has_header('Cache-Control'):
- response['Cache-Control'] = 'max-age=%d' % cache_timeout
+ patch_cache_control(response, max_age=cache_timeout)
def patch_vary_headers(response, newheaders):
"""