| 6 | def 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 | |
| 37 | def 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 | |