cobrateam / django-htmlmin

HTML minifier for Python frameworks (not only Django, despite the name).
http://pypi.python.org/pypi/django-htmlmin
BSD 2-Clause "Simplified" License
542 stars 73 forks source link

Exception raised when no content type set: "Error in formatting: KeyError: u'content-type'" #98

Closed JoeJasinski closed 8 years ago

JoeJasinski commented 8 years ago

Hi,

When the content-type is not available in the response, the following exception is raised. I encounter this error when serving up static media with django-filer, but it looks like this could happen in any case where the request['Content-Type'] is not present.

` Environment:

Request Method: GET Request URL: http://localhost:8000/media/filer_public/df/66/df664fc4-d8a6-4d88-8aef-7be5e3aef32a/chipy-05-chicago_flag_python_logo-01.jpg

Django Version: 1.9.4 Python Version: 2.7.9 Installed Applications: ('django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.staticfiles', 'django.contrib.sitemaps', 'djangocms_admin_style', 'django.contrib.admin', 'easy_thumbnails', 'mptt', 'filer', 'nocaptcha_recaptcha', 'cms', 'treebeard', 'menus', 'sekizai', 'reversion', 'sorl.thumbnail', 'django_extensions', 'pagefeature', 'contact_form', 'photo_gallery.apps.PhotoConfig', 'djangocms_inherit', 'djangocms_teaser', 'djangocms_link', 'djangocms_snippet', 'djangocms_text_ckeditor', 'djangocms_plain_text') Installed Middleware: ('django.middleware.common.CommonMiddleware', 'htmlmin.middleware.HtmlMinifyMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.locale.LocaleMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'cms.middleware.user.CurrentUserMiddleware', 'cms.middleware.page.CurrentPageMiddleware', 'cms.middleware.toolbar.ToolbarMiddleware', 'cms.middleware.language.LanguageCookieMiddleware', 'htmlmin.middleware.MarkRequestMiddleware')

Traceback:

File "/Users/jjasinski/Sites/joejasinski/envs/joejasinski/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response

  1. response = middleware_method(request, response)

File "/mysite/mysite/envs/mysite/lib/python2.7/site-packages/htmlmin/middleware.py" in process_response

  1. if minify and self.can_minify_response(request, response):

File "/mysite/envs/mysite/lib/python2.7/site-packages/htmlmin/middleware.py" in can_minify_response

  1. resp_ok = 'text/html' in response['Content-Type']

File "/mysite/envs/mysite/lib/python2.7/site-packages/django/http/response.py" in getitem

  1. return self._headers[header.lower()][1]

Exception Type: KeyError at /media/filer_public/df/66/df664fc4-d8a6-4d88-8aef-7be5e3aef32a/chipy-05-chicago_flag_python_logo-01.jpg Exception Value: 'content-type' `

atten commented 8 years ago

I also viewed this bug while requesting files from MEDIA_URL with DEBUG=True. It looks like you need to add additional check for existence 'Content-Type' in response (htmlmin/middleware.py:32).

andrewsmedina commented 8 years ago

released in 0.9.1 :)