| 2 | |
| 3 | The method should also be utilized in the existing code: |
| 4 | {{{ |
| 5 | diff --git a/django/middleware/common.py b/django/middleware/common.py |
| 6 | --- a/django/middleware/common.py |
| 7 | +++ b/django/middleware/common.py |
| 8 | @@ -107,8 +107,8 @@ class CommonMiddleware(MiddlewareMixin): |
| 9 | |
| 10 | # Add the Content-Length header to non-streaming responses if not |
| 11 | # already set. |
| 12 | - if not response.streaming and not response.has_header('Content-Length'): |
| 13 | - response['Content-Length'] = str(len(response.content)) |
| 14 | + if not response.streaming: |
| 15 | + response.setdefault('Content-Length', str(len(response.content))) |
| 16 | |
| 17 | return response |
| 18 | |
| 19 | diff --git a/django/utils/cache.py b/django/utils/cache.py |
| 20 | --- a/django/utils/cache.py |
| 21 | +++ b/django/utils/cache.py |
| 22 | @@ -246,8 +246,7 @@ def patch_response_headers(response, cache_timeout=None): |
| 23 | cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS |
| 24 | if cache_timeout < 0: |
| 25 | cache_timeout = 0 # Can't have max-age negative |
| 26 | - if not response.has_header('Expires'): |
| 27 | - response['Expires'] = http_date(time.time() + cache_timeout) |
| 28 | + response.setdefault('Expires', http_date(time.time() + cache_timeout)) |
| 29 | patch_cache_control(response, max_age=cache_timeout) |
| 30 | |
| 31 | |
| 32 | diff --git a/django/views/decorators/http.py b/django/views/decorators/http.py |
| 33 | --- a/django/views/decorators/http.py |
| 34 | +++ b/django/views/decorators/http.py |
| 35 | @@ -101,10 +101,10 @@ def condition(etag_func=None, last_modified_func=None): |
| 36 | # Set relevant headers on the response if they don't already exist |
| 37 | # and if the request method is safe. |
| 38 | if request.method in ('GET', 'HEAD'): |
| 39 | - if res_last_modified and not response.has_header('Last-Modified'): |
| 40 | - response['Last-Modified'] = http_date(res_last_modified) |
| 41 | - if res_etag and not response.has_header('ETag'): |
| 42 | - response['ETag'] = res_etag |
| 43 | + if res_last_modified:: |
| 44 | + response.setdefault('Last-Modified', http_date(res_last_modified)) |
| 45 | + if res_etag: |
| 46 | + response.setdefault('ETag', res_etag) |
| 47 | |
| 48 | return response |
| 49 | |
| 50 | }}} |