Author: gwilson
Date: 2007-10-30 22:59:40 -0500 (Tue, 30 Oct 2007)
New Revision: 6634

Modified:
   django/trunk/django/contrib/sessions/middleware.py
   django/trunk/django/core/servers/basehttp.py
   django/trunk/django/middleware/http.py
   django/trunk/django/utils/cache.py
   django/trunk/django/utils/http.py
   django/trunk/django/views/static.py
   django/trunk/tests/regressiontests/text/tests.py
Log:
Fixed #5816 -- Fixed a regression from [6333] that generates incorrect cookie 
"expires" dates when using a locale other than English.  Introduced `http_date` 
and `cookie_date` utility functions.  Thanks for the report Michael Lemaire.  
Thanks for the patch Karen Tracey and `SmileyChris`.


Modified: django/trunk/django/contrib/sessions/middleware.py
===================================================================
--- django/trunk/django/contrib/sessions/middleware.py  2007-10-31 01:46:32 UTC 
(rev 6633)
+++ django/trunk/django/contrib/sessions/middleware.py  2007-10-31 03:59:40 UTC 
(rev 6634)
@@ -1,9 +1,8 @@
 import time
-import datetime
-from email.Utils import formatdate
 
 from django.conf import settings
 from django.utils.cache import patch_vary_headers
+from django.utils.http import cookie_date
 
 TEST_COOKIE_NAME = 'testcookie'
 TEST_COOKIE_VALUE = 'worked'
@@ -32,13 +31,8 @@
                     expires = None
                 else:
                     max_age = settings.SESSION_COOKIE_AGE
-                    rfcdate = formatdate(time.time() + 
settings.SESSION_COOKIE_AGE)
-
-                    # Fixed length date must have '-' separation in the format
-                    # DD-MMM-YYYY for compliance with Netscape cookie standard
-                    expires = 
datetime.datetime.strftime(datetime.datetime.utcnow() + \
-                              
datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE), "%a, %d-%b-%Y %H:%M:%S 
GMT")
-
+                    expires_time = time.time() + settings.SESSION_COOKIE_AGE
+                    expires = cookie_date(expires_time)
                 # Save the seesion data and refresh the client cookie.
                 request.session.save()
                 response.set_cookie(settings.SESSION_COOKIE_NAME,

Modified: django/trunk/django/core/servers/basehttp.py
===================================================================
--- django/trunk/django/core/servers/basehttp.py        2007-10-31 01:46:32 UTC 
(rev 6633)
+++ django/trunk/django/core/servers/basehttp.py        2007-10-31 03:59:40 UTC 
(rev 6634)
@@ -9,14 +9,14 @@
 
 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
 from types import ListType, StringType
-from email.Utils import formatdate
 import mimetypes
 import os
 import re
 import sys
-import time
 import urllib
 
+from django.utils.http import http_date
+
 __version__ = "0.1"
 __all__ = ['WSGIServer','WSGIRequestHandler','demo_app']
 
@@ -376,7 +376,7 @@
                 self._write('HTTP/%s %s\r\n' % (self.http_version,self.status))
                 if 'Date' not in self.headers:
                     self._write(
-                        'Date: %s\r\n' % (formatdate()[:26] + "GMT")
+                        'Date: %s\r\n' % http_date()
                     )
                 if self.server_software and 'Server' not in self.headers:
                     self._write('Server: %s\r\n' % self.server_software)

Modified: django/trunk/django/middleware/http.py
===================================================================
--- django/trunk/django/middleware/http.py      2007-10-31 01:46:32 UTC (rev 
6633)
+++ django/trunk/django/middleware/http.py      2007-10-31 03:59:40 UTC (rev 
6634)
@@ -1,4 +1,4 @@
-from email.Utils import formatdate
+from django.utils.http import http_date
 
 class ConditionalGetMiddleware(object):
     """
@@ -11,7 +11,7 @@
     Also sets the Date and Content-Length response-headers.
     """
     def process_response(self, request, response):
-        response['Date'] = formatdate()[:26] + "GMT"
+        response['Date'] = http_date()
         if not response.has_header('Content-Length'):
             response['Content-Length'] = str(len(response.content))
 

Modified: django/trunk/django/utils/cache.py
===================================================================
--- django/trunk/django/utils/cache.py  2007-10-31 01:46:32 UTC (rev 6633)
+++ django/trunk/django/utils/cache.py  2007-10-31 03:59:40 UTC (rev 6634)
@@ -20,11 +20,11 @@
 import md5
 import re
 import time
-from email.Utils import formatdate
 
 from django.conf import settings
 from django.core.cache import cache
 from django.utils.encoding import smart_str, iri_to_uri
+from django.utils.http import http_date
 
 cc_delim_re = re.compile(r'\s*,\s*')
 
@@ -89,9 +89,9 @@
     if not response.has_header('ETag'):
         response['ETag'] = md5.new(response.content).hexdigest()
     if not response.has_header('Last-Modified'):
-        response['Last-Modified'] = formatdate()[:26] + "GMT"
+        response['Last-Modified'] = http_date()
     if not response.has_header('Expires'):
-        response['Expires'] = formatdate(time.time() + cache_timeout)[:26] + 
"GMT"
+        response['Expires'] = http_date(time.time() + cache_timeout)
     patch_cache_control(response, max_age=cache_timeout)
 
 def add_never_cache_headers(response):

Modified: django/trunk/django/utils/http.py
===================================================================
--- django/trunk/django/utils/http.py   2007-10-31 01:46:32 UTC (rev 6633)
+++ django/trunk/django/utils/http.py   2007-10-31 03:59:40 UTC (rev 6634)
@@ -1,4 +1,6 @@
 import urllib
+from email.Utils import formatdate
+
 from django.utils.encoding import smart_str, force_unicode
 from django.utils.functional import allow_lazy
 
@@ -37,3 +39,29 @@
             for k, v in query],
         doseq)
 
+def cookie_date(epoch_seconds=None):
+    """
+    Formats the time to ensure compatibility with Netscape's cookie standard.
+
+    Accepts a floating point number expressed in seconds since the epoch, in
+    UTC - such as that outputted by time.time(). If set to None, defaults to
+    the current time.
+
+    Outputs a string in the format 'Wdy, DD-Mon-YYYY HH:MM:SS GMT'.
+    """
+    rfcdate = formatdate(epoch_seconds)
+    return '%s-%s-%s GMT' % (rfcdate[:7], rfcdate[8:11], rfcdate[12:25])
+
+def http_date(epoch_seconds=None):
+    """
+    Formats the time to match the RFC1123 date format as specified by HTTP
+    RFC2616 section 3.3.1.
+
+    Accepts a floating point number expressed in seconds since the epoch, in
+    UTC - such as that outputted by time.time(). If set to None, defaults to
+    the current time.
+
+    Outputs a string in the format 'Wdy, DD Mon YYYY HH:MM:SS GMT'.
+    """
+    rfcdate = formatdate(epoch_seconds)
+    return '%s GMT' % rfcdate[:25]

Modified: django/trunk/django/views/static.py
===================================================================
--- django/trunk/django/views/static.py 2007-10-31 01:46:32 UTC (rev 6633)
+++ django/trunk/django/views/static.py 2007-10-31 03:59:40 UTC (rev 6634)
@@ -7,13 +7,14 @@
 import os
 import posixpath
 import re
-import rfc822
 import stat
 import urllib
+from email.Utils import parsedate_tz, mktime_tz
 
 from django.template import loader
 from django.http import Http404, HttpResponse, HttpResponseRedirect, 
HttpResponseNotModified
 from django.template import Template, Context, TemplateDoesNotExist
+from django.utils.http import http_date
 
 def serve(request, path, document_root=None, show_indexes=False):
     """
@@ -60,7 +61,7 @@
     mimetype = mimetypes.guess_type(fullpath)[0]
     contents = open(fullpath, 'rb').read()
     response = HttpResponse(contents, mimetype=mimetype)
-    response["Last-Modified"] = rfc822.formatdate(statobj[stat.ST_MTIME])
+    response["Last-Modified"] = http_date(statobj[stat.ST_MTIME])
     return response
 
 DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
@@ -119,8 +120,7 @@
             raise ValueError
         matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header,
                            re.IGNORECASE)
-        header_mtime = rfc822.mktime_tz(rfc822.parsedate_tz(
-            matches.group(1)))
+        header_mtime = mktime_tz(parsedate_tz(matches.group(1)))
         header_len = matches.group(3)
         if header_len and int(header_len) != size:
             raise ValueError

Modified: django/trunk/tests/regressiontests/text/tests.py
===================================================================
--- django/trunk/tests/regressiontests/text/tests.py    2007-10-31 01:46:32 UTC 
(rev 6633)
+++ django/trunk/tests/regressiontests/text/tests.py    2007-10-31 03:59:40 UTC 
(rev 6634)
@@ -27,6 +27,14 @@
 >>> urlquote_plus(u'Paris & Orl\xe9ans', safe="&")
 u'Paris+&+Orl%C3%A9ans'
 
+### cookie_date, http_date ###############################################
+>>> from django.utils.http import cookie_date, http_date
+>>> t = 1167616461.0
+>>> cookie_date(t)
+'Mon, 01-Jan-2007 01:54:21 GMT'
+>>> http_date(t)
+'Mon, 01 Jan 2007 01:54:21 GMT'
+
 ### iri_to_uri ###########################################################
 >>> from django.utils.encoding import iri_to_uri
 >>> iri_to_uri(u'red%09ros\xe9#red')


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to