Ticket #5816: http_dates.diff

File http_dates.diff, 7.8 KB (added by Chris Beaven, 17 years ago)
  • django/core/servers/basehttp.py

     
    99
    1010from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
    1111from types import ListType, StringType
    12 from email.Utils import formatdate
    1312import mimetypes
    1413import os
    1514import re
    1615import sys
    1716import time
    1817import urllib
     18from django.utils.http import http_date
    1919
    2020__version__ = "0.1"
    2121__all__ = ['WSGIServer','WSGIRequestHandler','demo_app']
     
    376376                self._write('HTTP/%s %s\r\n' % (self.http_version,self.status))
    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:
    382382                    self._write('Server: %s\r\n' % self.server_software)
  • django/views/static.py

     
    77import os
    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):
    1920    """
     
    6061    mimetype = mimetypes.guess_type(fullpath)[0]
    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
    6667DEFAULT_DIRECTORY_INDEX_TEMPLATE = """
     
    119120            raise ValueError
    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:
    126126            raise ValueError
  • django/contrib/sessions/middleware.py

     
     1import time
    12from django.conf import settings
    23from django.utils.cache import patch_vary_headers
    3 from email.Utils import formatdate
    4 import datetime
    5 import time
     4from django.utils.http import cookie_date
    65
    76TEST_COOKIE_NAME = 'testcookie'
    87TEST_COOKIE_VALUE = 'worked'
     
    3029                    expires = None
    3130                else:
    3231                    max_age = settings.SESSION_COOKIE_AGE
    33                     rfcdate = formatdate(time.time() + settings.SESSION_COOKIE_AGE)
    34 
    35                     # Fixed length date must have '-' separation in the format
    36                     # DD-MMM-YYYY for compliance with Netscape cookie standard
    37                     expires = datetime.datetime.strftime(datetime.datetime.utcnow() + \
    38                               datetime.timedelta(seconds=settings.SESSION_COOKIE_AGE), "%a, %d-%b-%Y %H:%M:%S GMT")
    39 
     32                    expires_time = time.time() + settings.SESSION_COOKIE_AGE
     33                    expires = cookie_date(expires_time)
    4034                # Save the seesion data and refresh the client cookie.
    4135                request.session.save()
    4236                response.set_cookie(settings.SESSION_COOKIE_NAME,
  • django/utils/cache.py

     
    2020import md5
    2121import re
    2222import time
    23 from email.Utils import formatdate
    2423from django.conf import settings
    2524from django.core.cache import cache
    2625from django.utils.encoding import smart_str, iri_to_uri
     26from django.utils.http import http_date
    2727
    2828cc_delim_re = re.compile(r'\s*,\s*')
    2929
     
    8888    if not response.has_header('ETag'):
    8989        response['ETag'] = md5.new(response.content).hexdigest()
    9090    if not response.has_header('Last-Modified'):
    91         response['Last-Modified'] = formatdate()[:26] + "GMT"
     91        response['Last-Modified'] = http_date()
    9292    if not response.has_header('Expires'):
    93         response['Expires'] = formatdate(time.time() + cache_timeout)[:26] + "GMT"
     93        response['Expires'] = http_date(time.time() + cache_timeout)
    9494    patch_cache_control(response, max_age=cache_timeout)
    9595
    9696def add_never_cache_headers(response):
  • django/utils/http.py

     
    11import urllib
     2from email.Utils import formatdate
    23from django.utils.encoding import smart_str, force_unicode
    34from django.utils.functional import allow_lazy
    45
     
    3738            for k, v in query],
    3839        doseq)
    3940
     41def cookie_date(epoch_seconds=None):
     42    """
     43    Format the time to ensure compatibility with Netscape's cookie standard
     44    (RFC2019 section 10.1.2).
     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 the
     48    current time.
     49
     50    Outputs a string in the format 'Wdy, DD-Mon-YY HH:MM:SS GMT'.
     51    """
     52    rfcdate = formatdate(epoch_seconds)
     53    return '%s-%s-%s %s GMT' % (rfcdate[:7], rfcdate[8:11], rfcdate[14:16],
     54                                rfcdate[17:25])
     55
     56def http_date(epoch_seconds=None):
     57    """
     58    Format the time to match the RFC1123 date format as specified by HTTP
     59    RFC2616 section 3.3.1.
     60
     61    Accepts a floating point number expressed in seconds since the epoch, in
     62    UTC - such as that outputted by time.time(). If set to None, defaults to the
     63    current time.
     64
     65    Outputs a string in the format 'Wdy, DD Mon YYYY HH:MM:SS GMT'.
     66    """
     67    rfcdate = formatdate(epoch_seconds)
     68    return '%s GMT' % rfcdate[:25]
  • django/middleware/http.py

     
    1 from email.Utils import formatdate
     1from django.utils.http import http_date
    22
    33class ConditionalGetMiddleware(object):
    44    """
     
    1111    Also sets the Date and Content-Length response-headers.
    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))
    1717
  • tests/regressiontests/text/tests.py

     
    2727>>> urlquote_plus(u'Paris & Orl\xe9ans', safe="&")
    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-07 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
    3240>>> iri_to_uri(u'red%09ros\xe9#red')
Back to Top