| | 7 | def 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 | |
| | 38 | def 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 | |