Opened 18 years ago
Closed 10 years ago
#6727 closed Cleanup/optimization (fixed)
django.utils.cache.patch_cache_control enhancement when response has empty cache control header
| Reported by: | Owned by: | Dwight Gunning | |
|---|---|---|---|
| Component: | HTTP handling | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | doug@… | Triage Stage: | Ready for checkin |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
See source:/django/trunk/django/utils/cache.py :
60: if response.has_header('Cache-Control'):
61: cc = cc_delim_re.split(response['Cache-Control'])
62: cc = dict([dictitem(el) for el in cc])
If a Cache-Control header exists but is empty, this will add a useless comma in the result.
See:
>>> cache_control_header=''
>>> import re
>>> cc_delim_re = re.compile(r'\s*,\s*')
>>> cc = cc_delim_re.split(cache_control_header)
>>> def dictitem(s):
... t = s.split('=',1)
... if len(t) > 1:
... return (t[0].lower(), t[1])
... else:
... return (t[0].lower(), True)
...
>>> cc = dict([dictitem(el) for el in cc])
>>> cc
{'': True}
>>> def dictvalue(t):
... if t[1] is True:
... return t[0]
... else:
... return t[0] + '=' + smart_str(t[1])
...
>>> from django.utils.encoding import smart_str
>>> cc['max-age']='300'
>>> cc = ', '.join([dictvalue(el) for el in cc.items()])
>>> cc
', max-age=300'
Note that this is still a valid cache control header according to:
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.9
- http://www.w3.org/Protocols/rfc2616/rfc2616-sec2.html#sec2.1
But It would be nicer to test if existing cache control response header is empty, so something like:
60: if response.has_header('Cache-Control') and response['Cache-Control']:
Change History (11)
comment:1 by , 18 years ago
| Cc: | removed |
|---|
comment:2 by , 18 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:3 by , 17 years ago
| Description: | modified (diff) |
|---|
comment:4 by , 15 years ago
| Severity: | → Normal |
|---|---|
| Type: | → Cleanup/optimization |
comment:5 by , 14 years ago
| UI/UX: | unset |
|---|
comment:7 by , 10 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:8 by , 10 years ago
Created Github branch: https://github.com/dwightgunning/django/tree/ticket_6727
comment:9 by , 10 years ago
Additional test created. Test suite passes.
Raised pull request: https://github.com/django/django/pull/5597
Change UI/UX from NULL to False.