Ticket #4148: 5079-fix_IE_cache_bugs_2.diff

File 5079-fix_IE_cache_bugs_2.diff, 2.9 KB (added by Michael Axiak <axiak@…>, 17 years ago)

Cleaned the patch a little bit...added fix for content-disposition...take two

  • django/core/handlers/base.py

     
    33from django import http
    44import sys
    55
     6def fix_IE_for_attach(request, response):
     7    """
     8    This function will prevent Django from serving a
     9    Content-Disposition header while expecting the browser
     10    to cache it. This leads to IE not allowing the client
     11    to download.
     12    """
     13
     14    offending_headers = ('no-cache','no-store',)
     15   
     16    try:
     17        if 'MSIE' not in request.META['User-Agent'].upper():
     18            return response
     19    except KeyError:
     20        return response
     21
     22    if response.has_header('Content-Disposition'):
     23        del response['Pragma']
     24        if response.has_header('Cache-Control'):
     25            cache_control_values = [value.strip() for value in
     26                                    response['Cache-Control'].split(',')
     27                                    if value.strip().lower() not in
     28                                    offending_headers                   ]
     29
     30            if len(cache_control_values) == 0:
     31                del response['Cache-Control']
     32            else:
     33                response['Cache-Control'] = ', '.join(cache_control_values)
     34       
     35    return response
     36
     37def fix_IE_for_vary(request, response):
     38    """
     39    This function will fix the bug reported at
     40    http://support.microsoft.com/kb/824847/en-us?spid=8722&sid=global
     41    by clearing the Vary header whenever the mime-type is not safe
     42    enough for Internet Explorer to handle.
     43    """
     44   
     45    # a list of mime-types that are decreed "Vary-safe" for IE
     46    safe_mime_types = ('text/html',
     47                       'text/plain',
     48                       'text/sgml',
     49                       )
     50
     51    # establish that the user is using IE
     52    try:
     53        if 'MSIE' not in request.META['User-Agent'].upper():
     54            return response
     55    except KeyError:
     56        return response
     57
     58
     59    # IE will break
     60    if response.mimetype.lower() not in safe_mime_types:
     61        try:
     62            del response['Vary']
     63            response['Pragma'] = 'no-cache'
     64            response['Cache-Control'] = 'no-cache, must-revalidate'
     65        except KeyError:
     66            return response
     67           
     68    return response
     69
    670class BaseHandler(object):
    771    def __init__(self):
    872        self._request_middleware = self._view_middleware = self._response_middleware = self._exception_middleware = None
     
    1781        from django.core import exceptions
    1882        self._request_middleware = []
    1983        self._view_middleware = []
    20         self._response_middleware = []
     84        # add processing for IE caching bugs
     85        self._response_middleware = [fix_IE_for_vary,fix_IE_for_attach]
     86           
    2187        self._exception_middleware = []
    2288        for middleware_path in settings.MIDDLEWARE_CLASSES:
    2389            try:
Back to Top