Ticket #4148: 7737-fix_ie_cache_bugs_3.diff

File 7737-fix_ie_cache_bugs_3.diff, 3.0 KB (added by Adam Vollrath, 16 years ago)

It's been a year, and this bug persists. It prevents documented examples like http://www.djangoproject.com/documentation/request_response/#telling-the-browser-to-treat-the-response-as-a-file-attachment I've modified the last submitted patch, bringing it in line with the newer version of the HttpResponse class.

  • django/core/handlers/base.py

     
    44from django.core import signals
    55from django.dispatch import dispatcher
    66
     7def fix_IE_for_attach(request, response):
     8    """
     9    This function will prevent Django from serving a
     10    Content-Disposition header while expecting the browser
     11    to cache it. This leads to IE not allowing the client
     12    to download.
     13    """
     14
     15    offending_headers = ('no-cache','no-store',)
     16   
     17    try:
     18        if 'MSIE' not in request.META['HTTP_USER_AGENT'].upper():
     19            return response
     20    except KeyError:
     21        return response
     22
     23    if response.has_header('Content-Disposition'):
     24        del response['Pragma']
     25        if response.has_header('Cache-Control'):
     26            cache_control_values = [value.strip() for value in
     27                                    response['Cache-Control'].split(',')
     28                                    if value.strip().lower() not in
     29                                    offending_headers                   ]
     30
     31            if len(cache_control_values) == 0:
     32                del response['Cache-Control']
     33            else:
     34                response['Cache-Control'] = ', '.join(cache_control_values)
     35       
     36    return response
     37
     38def fix_IE_for_vary(request, response):
     39    """
     40    This function will fix the bug reported at
     41    http://support.microsoft.com/kb/824847/en-us?spid=8722&sid=global
     42    by clearing the Vary header whenever the mime-type is not safe
     43    enough for Internet Explorer to handle.  Poor thing.
     44    """
     45   
     46    # a list of mime-types that are decreed "Vary-safe" for IE
     47    safe_mime_types = ('text/html',
     48                       'text/plain',
     49                       'text/sgml',
     50                       )
     51
     52    # establish that the user is using IE
     53    try:
     54        if 'MSIE' not in request.META['HTTP_USER_AGENT'].upper():
     55            return response
     56    except KeyError:
     57        return response
     58
     59    # IE will break
     60    # The first part of the Content-Type field will be the MIME type,
     61    # everything after ; such as character-set can be ignored.
     62    if response['Content-Type'].split(';')[0] not in safe_mime_types:
     63        try:
     64            del response['Vary']
     65        except KeyError:
     66            return response
     67           
     68    return response
     69
    770class BaseHandler(object):
    871    # Changes that are always applied to a response (in this order).
    972    response_fixes = [http.fix_location_header,
     
    2285        from django.core import exceptions
    2386        self._request_middleware = []
    2487        self._view_middleware = []
    25         self._response_middleware = []
     88        # add processing for IE caching bugs
     89        self._response_middleware = [fix_IE_for_vary,fix_IE_for_attach]
     90           
    2691        self._exception_middleware = []
    2792        for middleware_path in settings.MIDDLEWARE_CLASSES:
    2893            try:
Back to Top