Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4199 -- Changed date formatting in HTTP expires header to be spec

compliant. Thanks, Chris Bennett.


git-svn-id: http://code.djangoproject.com/svn/django/trunk@5712 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 5dd9a2ab38a79457ba9c21f07ca855727f9568a3 1 parent d5129ee
Malcolm Tredinnick authored July 16, 2007
1  AUTHORS
@@ -57,6 +57,7 @@ answer newbie questions, and generally made Django that much better:
57 57
     Ned Batchelder <http://www.nedbatchelder.com/>
58 58
     Shannon -jj Behrens <http://jjinux.blogspot.com/>
59 59
     Esdras Beleza <linux@esdrasbeleza.com>
  60
+    Chris Bennett <chrisrbennett@yahoo.com>
60 61
     James Bennett
61 62
     Ben <afternoon@uk2.net>
62 63
     Paul Bissex <http://e-scribe.com/>
10  django/contrib/sessions/middleware.py
@@ -2,7 +2,9 @@
2 2
 from django.contrib.sessions.models import Session
3 3
 from django.core.exceptions import SuspiciousOperation
4 4
 from django.utils.cache import patch_vary_headers
  5
+from email.Utils import formatdate
5 6
 import datetime
  7
+import time
6 8
 
7 9
 TEST_COOKIE_NAME = 'testcookie'
8 10
 TEST_COOKIE_VALUE = 'worked'
@@ -37,7 +39,7 @@ def get(self, key, default=None):
37 39
         return self._session.get(key, default)
38 40
 
39 41
     def pop(self, key, *args):
40  
-        self.modified = self.modified or key in self._session 
  42
+        self.modified = self.modified or key in self._session
41 43
         return self._session.pop(key, *args)
42 44
 
43 45
     def set_test_cookie(self):
@@ -98,7 +100,11 @@ def process_response(self, request, response):
98 100
                     expires = None
99 101
                 else:
100 102
                     max_age = settings.SESSION_COOKIE_AGE
101  
-                    expires = datetime.datetime.strftime(datetime.datetime.utcnow() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE), "%a, %d-%b-%Y %H:%M:%S GMT")
  103
+                    rfcdate = formatdate(time.time() + settings.SESSION_COOKIE_AGE)
  104
+                    # Fixed length date must have '-' separation in the format
  105
+                    # DD-MMM-YYYY for compliance with Netscape cookie standard
  106
+                    expires = (rfcdate[:7] + "-" + rfcdate[8:11]
  107
+                               + "-" + rfcdate[12:26] + "GMT")
102 108
                 new_session = Session.objects.save(session_key, request.session._session,
103 109
                     datetime.datetime.now() + datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE))
104 110
                 response.set_cookie(settings.SESSION_COOKIE_NAME, session_key,
10  django/core/servers/basehttp.py
@@ -9,7 +9,13 @@
9 9
 
10 10
 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
11 11
 from types import ListType, StringType
12  
-import os, re, sys, time, urllib, mimetypes
  12
+from email.Utils import formatdate
  13
+import mimetypes
  14
+import os
  15
+import re
  16
+import sys
  17
+import time
  18
+import urllib
13 19
 
14 20
 __version__ = "0.1"
15 21
 __all__ = ['WSGIServer','WSGIRequestHandler','demo_app']
@@ -370,7 +376,7 @@ def send_preamble(self):
370 376
                 self._write('HTTP/%s %s\r\n' % (self.http_version,self.status))
371 377
                 if 'Date' not in self.headers:
372 378
                     self._write(
373  
-                        'Date: %s\r\n' % time.asctime(time.gmtime(time.time()))
  379
+                        'Date: %s\r\n' % formatdate()[:26] + "GMT"
374 380
                     )
375 381
                 if self.server_software and 'Server' not in self.headers:
376 382
                     self._write('Server: %s\r\n' % self.server_software)
5  django/middleware/http.py
... ...
@@ -1,4 +1,4 @@
1  
-import datetime
  1
+from email.Utils import formatdate
2 2
 
3 3
 class ConditionalGetMiddleware(object):
4 4
     """
@@ -11,8 +11,7 @@ class ConditionalGetMiddleware(object):
11 11
     Also sets the Date and Content-Length response-headers.
12 12
     """
13 13
     def process_response(self, request, response):
14  
-        now = datetime.datetime.utcnow()
15  
-        response['Date'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
  14
+        response['Date'] = formatdate()[:26] + "GMT"
16 15
         if not response.has_header('Content-Length'):
17 16
             response['Content-Length'] = str(len(response.content))
18 17
 
17  django/utils/cache.py
@@ -17,7 +17,10 @@
17 17
 "Accept-language" header.
18 18
 """
19 19
 
20  
-import datetime, md5, re
  20
+import md5
  21
+import re
  22
+import time
  23
+from email.Utils import formatdate
21 24
 from django.conf import settings
22 25
 from django.core.cache import cache
23 26
 from django.utils.encoding import smart_str
@@ -44,7 +47,7 @@ def dictitem(s):
44 47
             return (t[0].lower().replace('-', '_'), True)
45 48
 
46 49
     def dictvalue(t):
47  
-        if t[1] == True:
  50
+        if t[1] is True:
48 51
             return t[0]
49 52
         else:
50 53
             return t[0] + '=' + smart_str(t[1])
@@ -73,16 +76,14 @@ def patch_response_headers(response, cache_timeout=None):
73 76
     """
74 77
     if cache_timeout is None:
75 78
         cache_timeout = settings.CACHE_MIDDLEWARE_SECONDS
76  
-    now = datetime.datetime.utcnow()
  79
+    if cache_timeout < 0:
  80
+        cache_timeout = 0 # Can't have max-age negative
77 81
     if not response.has_header('ETag'):
78 82
         response['ETag'] = md5.new(response.content).hexdigest()
79 83
     if not response.has_header('Last-Modified'):
80  
-        response['Last-Modified'] = now.strftime('%a, %d %b %Y %H:%M:%S GMT')
  84
+        response['Last-Modified'] = formatdate()[:26] + "GMT"
81 85
     if not response.has_header('Expires'):
82  
-        expires = now + datetime.timedelta(0, cache_timeout)
83  
-        response['Expires'] = expires.strftime('%a, %d %b %Y %H:%M:%S GMT')
84  
-    if cache_timeout < 0:
85  
-        cache_timeout = 0 # Can't have max-age negative
  86
+        response['Expires'] = formatdate(time.time() + cache_timeout)[:26] + "GMT"
86 87
     patch_cache_control(response, max_age=cache_timeout)
87 88
 
88 89
 def add_never_cache_headers(response):

0 notes on commit 5dd9a2a

Please sign in to comment.
Something went wrong with that request. Please try again.