Ticket #4148: 5078-fix_ie_vary_bug_core__for_4148.diff

File 5078-fix_ie_vary_bug_core__for_4148.diff, 2.9 KB (added by Michael Axiak <axiak@…>, 18 years ago)

Take 2: This file alters the BaseHandler to do it and adds a setting to disable it if one wants.

  • conf/global_settings.py

     
    240240# isExistingURL validator.
    241241URL_VALIDATOR_USER_AGENT = "Django/0.96pre (http://www.djangoproject.com)"
    242242
     243# A boolean that determines whether or not the response will have the Vary
     244# header removed to work with Internet Explorer in some cases.
     245ENABLE_IE_VARY_FIX = True
     246
    243247##############
    244248# MIDDLEWARE #
    245249##############
  • core/handlers/base.py

     
    33from django import http
    44import sys
    55
     6def fix_IE_for_vary(request, response):
     7    """
     8    This function will fix the bug reported at
     9    http://support.microsoft.com/kb/824847/en-us?spid=8722&sid=global
     10    by clearing the Vary header whenever the mime-type is not safe
     11    enough for Internet Explorer to handle.
     12    """
     13   
     14    # a list of mime-types that are decreed "Vary-safe" for IE
     15    safe_mime_types = ('text/html',
     16                       'text/plain',
     17                       'text/sgml',
     18                       )
     19
     20    # establish that the user is using IE
     21    try:
     22        if 'MSIE' not in request.META['User-Agent'].upper():
     23            return response
     24    except KeyError:
     25        return response
     26
     27
     28    # IE will break
     29    if response.mimetype.lower() not in safe_mime_types:
     30        try:
     31            del response['Vary']
     32            response['Pragma'] = 'no-cache'
     33            response['Cache-Control'] = 'no-cache, must-revalidate'
     34        except KeyError:
     35            return response
     36           
     37    return response
     38
    639class BaseHandler(object):
    740    def __init__(self):
    841        self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None
     
    1750        from django.core import exceptions
    1851        self._request_middleware = []
    1952        self._view_middleware = []
    20         self._response_middleware = []
     53        if settings.ENABLE_IE_VARY_FIX:
     54            self._response_middleware = [fix_IE_for_vary]
     55        else:
     56            self._response_middleware = []
     57           
    2158        self._exception_middleware = []
    2259        for middleware_path in settings.MIDDLEWARE_CLASSES:
    2360            try:
     
    4582                self._view_middleware.append(mw_instance.process_view)
    4683            if hasattr(mw_instance, 'process_response'):
    4784                self._response_middleware.insert(0, mw_instance.process_response)
     85           
    4886            if hasattr(mw_instance, 'process_exception'):
    4987                self._exception_middleware.insert(0, mw_instance.process_exception)
     88               
    5089
    5190    def get_response(self, request):
    5291        "Returns an HttpResponse object for the given HttpRequest"
Back to Top