Ticket #717: ifmodifiedsince.diff
File ifmodifiedsince.diff, 6.2 KB (added by , 14 years ago) |
---|
-
django/middleware/http.py
1 1 from django.core.exceptions import MiddlewareNotUsed 2 2 from django.utils.http import http_date 3 from email.utils import parsedate 4 import datetime 3 5 4 6 class ConditionalGetMiddleware(object): 5 7 """ … … 24 26 25 27 if response.has_header('Last-Modified'): 26 28 if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE', None) 27 if if_modified_since == response['Last-Modified']: 28 # Setting the status code is enough here (same reasons as 29 # above). 30 response.status_code = 304 29 if if_modified_since: 30 lm = datetime.datetime(*parsedate(response['Last-Modified'])[:6]) 31 ims = datetime.datetime(*parsedate(if_modified_since)[:6]) 32 if lm <= ims: 33 # Setting the status code is enough here (same reasons as 34 # above). 35 response.status_code = 304 31 36 32 37 return response 33 38 -
django/views/decorators/http.py
7 7 except ImportError: 8 8 from django.utils.functional import wraps # Python 2.4 fallback. 9 9 10 import datetime 10 11 from calendar import timegm 11 12 from datetime import timedelta 12 from email.Utils import formatdate 13 from email.Utils import formatdate, parsedate 13 14 14 15 from django.utils.decorators import decorator_from_middleware, available_attrs 15 16 from django.utils.http import parse_etags, quote_etag … … 69 70 def decorator(func): 70 71 def inner(request, *args, **kwargs): 71 72 # Get HTTP request headers 72 if_modified_since = request.META.get("HTTP_IF_MODIFIED_SINCE") 73 if_modified_since = request.META.get('HTTP_IF_MODIFIED_SINCE', None) 74 if if_modified_since: 75 try: 76 if_modified_since = datetime.datetime(*parsedate(if_modified_since)[:6]) 77 except ValueError: 78 if_modified_since = None 73 79 if_none_match = request.META.get("HTTP_IF_NONE_MATCH") 74 80 if_match = request.META.get("HTTP_IF_MATCH") 75 81 if if_none_match or if_match: … … 86 92 if_match = None 87 93 88 94 # Compute values (if any) for the requested resource. 95 res_etag = None 89 96 if etag_func: 90 97 res_etag = etag_func(request, *args, **kwargs) 91 else: 92 res_etag = None 98 res_last_modified = None 93 99 if last_modified_func: 94 100 dt = last_modified_func(request, *args, **kwargs) 95 101 if dt: 96 res_last_modified = formatdate(timegm(dt.utctimetuple()))[:26] + 'GMT' 97 else: 98 res_last_modified = None 99 else: 100 res_last_modified = None 102 res_last_modified = dt 101 103 102 104 response = None 103 105 if not ((if_match and (if_modified_since or if_none_match)) or … … 107 109 if ((if_none_match and (res_etag in etags or 108 110 "*" in etags and res_etag)) and 109 111 (not if_modified_since or 110 res_last_modified == if_modified_since)): 112 (if_modified_since and 113 res_last_modified <= if_modified_since))): 111 114 if request.method in ("GET", "HEAD"): 112 115 response = HttpResponseNotModified() 113 116 else: … … 117 120 response = HttpResponse(status=412) 118 121 elif (not if_none_match and if_modified_since and 119 122 request.method == "GET" and 120 res_last_modified == if_modified_since): 123 res_last_modified and 124 res_last_modified <= if_modified_since): 121 125 response = HttpResponseNotModified() 122 126 123 127 if response is None: … … 125 129 126 130 # Set relevant headers on the response if they don't already exist. 127 131 if res_last_modified and not response.has_header('Last-Modified'): 128 response['Last-Modified'] = res_last_modified132 response['Last-Modified'] = formatdate(timegm(res_last_modified.utctimetuple()))[:26] + 'GMT' 129 133 if res_etag and not response.has_header('ETag'): 130 134 response['ETag'] = quote_etag(res_etag) 131 135 -
tests/regressiontests/conditional_processing/models.py
8 8 FULL_RESPONSE = 'Test conditional get response' 9 9 LAST_MODIFIED = datetime(2007, 10, 21, 23, 21, 47) 10 10 LAST_MODIFIED_STR = 'Sun, 21 Oct 2007 23:21:47 GMT' 11 LAST_MODIFIED_NEWER_STR = 'Mon, 18 Oct 2010 16:56:23 GMT' 12 LAST_MODIFIED_INVALID_STR = 'Mon, 32 Oct 2010 16:56:23 GMT' 11 13 EXPIRED_LAST_MODIFIED_STR = 'Sat, 20 Oct 2007 23:21:47 GMT' 12 14 ETAG = 'b4246ffc4f62314ca13147c9d4f76974' 13 15 EXPIRED_ETAG = '7fae4cd4b0f81e7d2914700043aa8ed6' … … 33 35 self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR 34 36 response = self.client.get('/condition/') 35 37 self.assertNotModified(response) 38 self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_NEWER_STR 39 response = self.client.get('/condition/') 40 self.assertNotModified(response) 41 self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_INVALID_STR 42 response = self.client.get('/condition/') 43 self.assertFullResponse(response) 36 44 self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR 37 45 response = self.client.get('/condition/') 38 46 self.assertFullResponse(response)