Login

Avoid IE Brokenness When using Vary and Attachments

Author:
axiak
Posted:
April 5, 2007
Language:
Python
Version:
.96
Score:
3 (after 3 ratings)

Apparently Internet Explorer (6 and 7) have a bug whereby if you blindly attach a PDF or some other file, it will choke. The problem lies in the Vary header (bug described in http://support.microsoft.com/kb/824847/en-us?spid=8722&sid=global).

To use, just add to the beginning of your middleware classes.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
class FixIEVaryBugMiddleware(object):
    """
    Quick MiddleWare that will fix the bug reported at
    http://support.microsoft.com/kb/824847/en-us?spid=8722&sid=global (thanks aconbere)
    for Internet Explorer since Microsoft doesn't know how to do HTTP.

    To use: Make sure you put this at the *beginning* of your middleware
    list (since Django applies responses in reverse order).
    """

    def process_response(self, request, response):

        # a list of mime-types that are decreed "Vary-safe" for IE
        safe_mime_types = ('text/html',
                           'text/plain',
                           'text/sgml',
                           )

        # establish that the user is using IE
        try:
            if 'MSIE' not in request.META['User-Agent'].upper():
                return response
        except KeyError:
            return response


        # IE will break
        if response.mimetype.lower() not in safe_mime_types: 
            try:
                del response['Vary']
                response['Pragma'] = 'no-cache'
                response['Cache-Control'] = 'no-cache, must-revalidate'
            except KeyError:
                return response

        return response
        

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 3 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

jdunck (on April 9, 2007):

FUD: Don't use this unless you know what you're doing.

The Vary header controls upstream caching, and removing it from a response is dangerous to do in the general case.

Consider a PDF containing private data, which you'd like to cache. You might Vary by cookie (which has session ID).

Deleting the Vary header in the response allows upstream caches to serve the prior response to the new requestor, even though the prior response should have been private to that requestor's session, per the Vary header.

#

axiak (on April 11, 2007):

As a reply to jdunck's FUD, I have modified the middleware to explicitly tell everyone to just NOT cache this file entirely.

(Better safe than sorry.)

#

Please login first before commenting.