Django

Code

Changeset 6634

Show
Ignore:
Timestamp:
10/30/07 22:59:40 (10 months ago)
Author:
gwilson
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/contrib/sessions/middleware.py

    r6628 r6634  
    11import time 
    2 import datetime 
    3 from email.Utils import formatdate 
    42 
    53from django.conf import settings 
    64from django.utils.cache import patch_vary_headers 
     5from django.utils.http import cookie_date 
    76 
    87TEST_COOKIE_NAME = 'testcookie' 
     
    3332                else: 
    3433                    max_age = settings.SESSION_COOKIE_AGE 
    35                     rfcdate = formatdate(time.time() + settings.SESSION_COOKIE_AGE) 
    36  
    37                     # Fixed length date must have '-' separation in the format 
    38                     # DD-MMM-YYYY for compliance with Netscape cookie standard 
    39                     expires = datetime.datetime.strftime(datetime.datetime.utcnow() + \ 
    40                               datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE), "%a, %d-%b-%Y %H:%M:%S GMT") 
    41  
     34                    expires_time = time.time() + settings.SESSION_COOKIE_AGE 
     35                    expires = cookie_date(expires_time) 
    4236                # Save the seesion data and refresh the client cookie. 
    4337                request.session.save() 
  • django/trunk/django/core/servers/basehttp.py

    r5721 r6634  
    1010from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer 
    1111from types import ListType, StringType 
    12 from email.Utils import formatdate 
    1312import mimetypes 
    1413import os 
    1514import re 
    1615import sys 
    17 import time 
    1816import urllib 
     17 
     18from django.utils.http import http_date 
    1919 
    2020__version__ = "0.1" 
     
    377377                if 'Date' not in self.headers: 
    378378                    self._write( 
    379                         'Date: %s\r\n' % (formatdate()[:26] + "GMT"
     379                        'Date: %s\r\n' % http_date(
    380380                    ) 
    381381                if self.server_software and 'Server' not in self.headers: 
  • django/trunk/django/middleware/http.py

    r6397 r6634  
    1 from email.Utils import formatdate 
     1from django.utils.http import http_date 
    22 
    33class ConditionalGetMiddleware(object): 
     
    1212    """ 
    1313    def process_response(self, request, response): 
    14         response['Date'] = formatdate()[:26] + "GMT" 
     14        response['Date'] = http_date() 
    1515        if not response.has_header('Content-Length'): 
    1616            response['Content-Length'] = str(len(response.content)) 
  • django/trunk/django/utils/cache.py

    r6626 r6634  
    2121import re 
    2222import time 
    23 from email.Utils import formatdate 
    2423 
    2524from django.conf import settings 
    2625from django.core.cache import cache 
    2726from django.utils.encoding import smart_str, iri_to_uri 
     27from django.utils.http import http_date 
    2828 
    2929cc_delim_re = re.compile(r'\s*,\s*') 
     
    9090        response['ETag'] = md5.new(response.content).hexdigest() 
    9191    if not response.has_header('Last-Modified'): 
    92         response['Last-Modified'] = formatdate()[:26] + "GMT" 
     92        response['Last-Modified'] = http_date() 
    9393    if not response.has_header('Expires'): 
    94         response['Expires'] = formatdate(time.time() + cache_timeout)[:26] + "GMT" 
     94        response['Expires'] = http_date(time.time() + cache_timeout) 
    9595    patch_cache_control(response, max_age=cache_timeout) 
    9696 
  • django/trunk/django/utils/http.py

    r6554 r6634  
    11import urllib 
     2from email.Utils import formatdate 
     3 
    24from django.utils.encoding import smart_str, force_unicode 
    35from django.utils.functional import allow_lazy 
     
    3840        doseq) 
    3941 
     42def cookie_date(epoch_seconds=None): 
     43    """ 
     44    Formats the time to ensure compatibility with Netscape's cookie standard. 
     45 
     46    Accepts a floating point number expressed in seconds since the epoch, in 
     47    UTC - such as that outputted by time.time(). If set to None, defaults to 
     48    the current time. 
     49 
     50    Outputs a string in the format 'Wdy, DD-Mon-YYYY HH:MM:SS GMT'. 
     51    """ 
     52    rfcdate = formatdate(epoch_seconds) 
     53    return '%s-%s-%s GMT' % (rfcdate[:7], rfcdate[8:11], rfcdate[12:25]) 
     54 
     55def http_date(epoch_seconds=None): 
     56    """ 
     57    Formats the time to match the RFC1123 date format as specified by HTTP 
     58    RFC2616 section 3.3.1. 
     59 
     60    Accepts a floating point number expressed in seconds since the epoch, in 
     61    UTC - such as that outputted by time.time(). If set to None, defaults to 
     62    the current time. 
     63 
     64    Outputs a string in the format 'Wdy, DD Mon YYYY HH:MM:SS GMT'. 
     65    """ 
     66    rfcdate = formatdate(epoch_seconds) 
     67    return '%s GMT' % rfcdate[:25] 
  • django/trunk/django/views/static.py

    r6505 r6634  
    88import posixpath 
    99import re 
    10 import rfc822 
    1110import stat 
    1211import urllib 
     12from email.Utils import parsedate_tz, mktime_tz 
    1313 
    1414from django.template import loader 
    1515from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseNotModified 
    1616from django.template import Template, Context, TemplateDoesNotExist 
     17from django.utils.http import http_date 
    1718 
    1819def serve(request, path, document_root=None, show_indexes=False): 
     
    6162    contents = open(fullpath, 'rb').read() 
    6263    response = HttpResponse(contents, mimetype=mimetype) 
    63     response["Last-Modified"] = rfc822.formatdate(statobj[stat.ST_MTIME]) 
     64    response["Last-Modified"] = http_date(statobj[stat.ST_MTIME]) 
    6465    return response 
    6566 
     
    120121        matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header, 
    121122                           re.IGNORECASE) 
    122         header_mtime = rfc822.mktime_tz(rfc822.parsedate_tz( 
    123             matches.group(1))) 
     123        header_mtime = mktime_tz(parsedate_tz(matches.group(1))) 
    124124        header_len = matches.group(3) 
    125125        if header_len and int(header_len) != size: 
  • django/trunk/tests/regressiontests/text/tests.py

    r6554 r6634  
    2828u'Paris+&+Orl%C3%A9ans' 
    2929 
     30### cookie_date, http_date ############################################### 
     31>>> from django.utils.http import cookie_date, http_date 
     32>>> t = 1167616461.0 
     33>>> cookie_date(t) 
     34'Mon, 01-Jan-2007 01:54:21 GMT' 
     35>>> http_date(t) 
     36'Mon, 01 Jan 2007 01:54:21 GMT' 
     37 
    3038### iri_to_uri ########################################################### 
    3139>>> from django.utils.encoding import iri_to_uri