Ticket #5816: http_dates.diff
File http_dates.diff, 7.8 KB (added by , 17 years ago) |
---|
-
django/core/servers/basehttp.py
9 9 10 10 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer 11 11 from types import ListType, StringType 12 from email.Utils import formatdate13 12 import mimetypes 14 13 import os 15 14 import re 16 15 import sys 17 16 import time 18 17 import urllib 18 from django.utils.http import http_date 19 19 20 20 __version__ = "0.1" 21 21 __all__ = ['WSGIServer','WSGIRequestHandler','demo_app'] … … 376 376 self._write('HTTP/%s %s\r\n' % (self.http_version,self.status)) 377 377 if 'Date' not in self.headers: 378 378 self._write( 379 'Date: %s\r\n' % (formatdate()[:26] + "GMT")379 'Date: %s\r\n' % http_date() 380 380 ) 381 381 if self.server_software and 'Server' not in self.headers: 382 382 self._write('Server: %s\r\n' % self.server_software) -
django/views/static.py
7 7 import os 8 8 import posixpath 9 9 import re 10 import rfc82211 10 import stat 12 11 import urllib 12 from email.utils import parsedate_tz, mktime_tz 13 13 14 14 from django.template import loader 15 15 from django.http import Http404, HttpResponse, HttpResponseRedirect, HttpResponseNotModified 16 16 from django.template import Template, Context, TemplateDoesNotExist 17 from django.utils.http import http_date 17 18 18 19 def serve(request, path, document_root=None, show_indexes=False): 19 20 """ … … 60 61 mimetype = mimetypes.guess_type(fullpath)[0] 61 62 contents = open(fullpath, 'rb').read() 62 63 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]) 64 65 return response 65 66 66 67 DEFAULT_DIRECTORY_INDEX_TEMPLATE = """ … … 119 120 raise ValueError 120 121 matches = re.match(r"^([^;]+)(; length=([0-9]+))?$", header, 121 122 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))) 124 124 header_len = matches.group(3) 125 125 if header_len and int(header_len) != size: 126 126 raise ValueError -
django/contrib/sessions/middleware.py
1 import time 1 2 from django.conf import settings 2 3 from django.utils.cache import patch_vary_headers 3 from email.Utils import formatdate 4 import datetime 5 import time 4 from django.utils.http import cookie_date 6 5 7 6 TEST_COOKIE_NAME = 'testcookie' 8 7 TEST_COOKIE_VALUE = 'worked' … … 30 29 expires = None 31 30 else: 32 31 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) 40 34 # Save the seesion data and refresh the client cookie. 41 35 request.session.save() 42 36 response.set_cookie(settings.SESSION_COOKIE_NAME, -
django/utils/cache.py
20 20 import md5 21 21 import re 22 22 import time 23 from email.Utils import formatdate24 23 from django.conf import settings 25 24 from django.core.cache import cache 26 25 from django.utils.encoding import smart_str, iri_to_uri 26 from django.utils.http import http_date 27 27 28 28 cc_delim_re = re.compile(r'\s*,\s*') 29 29 … … 88 88 if not response.has_header('ETag'): 89 89 response['ETag'] = md5.new(response.content).hexdigest() 90 90 if not response.has_header('Last-Modified'): 91 response['Last-Modified'] = formatdate()[:26] + "GMT"91 response['Last-Modified'] = http_date() 92 92 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) 94 94 patch_cache_control(response, max_age=cache_timeout) 95 95 96 96 def add_never_cache_headers(response): -
django/utils/http.py
1 1 import urllib 2 from email.Utils import formatdate 2 3 from django.utils.encoding import smart_str, force_unicode 3 4 from django.utils.functional import allow_lazy 4 5 … … 37 38 for k, v in query], 38 39 doseq) 39 40 41 def 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 56 def 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 formatdate1 from django.utils.http import http_date 2 2 3 3 class ConditionalGetMiddleware(object): 4 4 """ … … 11 11 Also sets the Date and Content-Length response-headers. 12 12 """ 13 13 def process_response(self, request, response): 14 response['Date'] = formatdate()[:26] + "GMT"14 response['Date'] = http_date() 15 15 if not response.has_header('Content-Length'): 16 16 response['Content-Length'] = str(len(response.content)) 17 17 -
tests/regressiontests/text/tests.py
27 27 >>> urlquote_plus(u'Paris & Orl\xe9ans', safe="&") 28 28 u'Paris+&+Orl%C3%A9ans' 29 29 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 30 38 ### iri_to_uri ########################################################### 31 39 >>> from django.utils.encoding import iri_to_uri 32 40 >>> iri_to_uri(u'red%09ros\xe9#red')