prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Error when serving images through the URL generator
  I posted a comment on https://github.com/torchbox/wagtail/issues/983 but probably better to open a new issue. Looks like the same problem to me though.
  
  Hi guys, I think I'm having the same problem but when serving images using the URL generator. It does work if I'm logged-in in the site (cache not working) but doesn't when I'm not (cache full on).
  
  Cheers,
  Jordi
  
  Internal Server Error: /images/2dMQIUOPwS5DlZuprp_E_WFdfhw=/47/width-75/
  Traceback (most recent call last):
    File "/var/www/buildability/venvs/buildability.co.nz/local/lib/python2.7/site-packages/django/core/handlers/base.py", line 204, in get_response
      response = middleware_method(request, response)
    File "/var/www/buildability/venvs/buildability.co.nz/local/lib/python2.7/site-packages/django/middleware/cache.py", line 121, in process_response
      self.cache.set(cache_key, response, timeout)
    File "/var/www/buildability/venvs/buildability.co.nz/local/lib/python2.7/site-packages/redis_cache/cache.py", line 239, in set
      result = self._set(key, pickle.dumps(value), timeout, client, _add_only)
    File "/var/www/buildability/venvs/buildability.co.nz/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
      raise TypeError, "can't pickle %s objects" % base.__name__
  TypeError: can't pickle instancemethod objects
  
  Request repr(): 
  <WSGIRequest
  path:/images/2dMQIUOPwS5DlZuprp_E_WFdfhw=/47/width-75/,
  GET:<QueryDict: {}>,
  POST:<QueryDict: {}>,
  COOKIES:{'_ga': 'GA1.3.1219121887.1434427204',
   'csrftoken': 'GNhfTEGBu40y8wRAFPa15lQTV66F9WCs'},
  META:{'CONTENT_LENGTH': '',
   'CONTENT_TYPE': '',
   u'CSRF_COOKIE': u'GNhfTEGBu40y8wRAFPa15lQTV66F9WCs',
   'DOCUMENT_ROOT': '/usr/share/nginx/html',
   'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,_/_;q=0.8',
   'HTTP_ACCEPT_ENCODING': 'gzip, deflate, sdch',
   'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8',
   'HTTP_CACHE_CONTROL': 'max-age=0',
   'HTTP_CONNECTION': 'keep-alive',
   'HTTP_COOKIE': '_ga=GA1.3.1219121887.1434427204; csrftoken=GNhfTEGBu40y8wRAFPa15lQTV66F9WCs',
   'HTTP_HOST': 'www.buildability.co.nz',
   'HTTP_UPGRADE_INSECURE_REQUESTS': '1',
   'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36',
   'PATH_INFO': u'/images/2dMQIUOPwS5DlZuprp_E_WFdfhw=/47/width-75/',
   'QUERY_STRING': '',
   'REMOTE_ADDR': '131.203.137.142',
   'REMOTE_PORT': '51455',
   'REQUEST_METHOD': 'GET',
   'REQUEST_URI': '/images/2dMQIUOPwS5DlZuprp_E_WFdfhw%3D/47/width-75/',
   u'SCRIPT_NAME': u'',
   'SERVER_NAME': 'www.buildability.co.nz',
   'SERVER_PORT': '80',
   'SERVER_PROTOCOL': 'HTTP/1.1',
   'UWSGI_SCHEME': 'http',
   'uwsgi.core': 7,
   'uwsgi.node': 'avinton',
   'uwsgi.version': '1.9.17.1-debian',
   'wsgi.errors': <open file 'wsgi_errors', mode 'w' at 0x7f0548a548a0>,
   'wsgi.file_wrapper': <built-in function uwsgi_sendfile>,
   'wsgi.input': <uwsgi._Input object at 0x7f0548a20a08>,
   'wsgi.multiprocess': True,
   'wsgi.multithread': True,
   'wsgi.run_once': False,
   'wsgi.url_scheme': 'http',
   'wsgi.version': (1, 0)}>
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `wagtail/wagtailimages/views/frontend.py`
  Content:
  ```
  1 from wsgiref.util import FileWrapper
  2 import imghdr
  3 
  4 from django.shortcuts import get_object_or_404
  5 from django.http import HttpResponse
  6 from django.core.exceptions import PermissionDenied
  7 
  8 from wagtail.wagtailimages.models import get_image_model
  9 from wagtail.wagtailimages.utils import verify_signature
  10 from wagtail.wagtailimages.exceptions import InvalidFilterSpecError
  11 
  12 
  13 def serve(request, signature, image_id, filter_spec):
  14     image = get_object_or_404(get_image_model(), id=image_id)
  15 
  16     if not verify_signature(signature.encode(), image_id, filter_spec):
  17         raise PermissionDenied
  18 
  19     try:
  20         rendition = image.get_rendition(filter_spec)
  21         rendition.file.open('rb')
  22         image_format = imghdr.what(rendition.file)
  23         return HttpResponse(FileWrapper(rendition.file), content_type='image/' + image_format)
  24     except InvalidFilterSpecError:
  25         return HttpResponse("Invalid filter spec: " + filter_spec, content_type='text/plain', status=400)
  26
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/wagtail/wagtailimages/views/frontend.py b/wagtail/wagtailimages/views/frontend.py
  --- a/wagtail/wagtailimages/views/frontend.py
  +++ b/wagtail/wagtailimages/views/frontend.py
  @@ -2,7 +2,7 @@
   import imghdr
   
   from django.shortcuts import get_object_or_404
  -from django.http import HttpResponse
  +from django.http import HttpResponse, StreamingHttpResponse
   from django.core.exceptions import PermissionDenied
   
   from wagtail.wagtailimages.models import get_image_model
  @@ -20,6 +20,6 @@
           rendition = image.get_rendition(filter_spec)
           rendition.file.open('rb')
           image_format = imghdr.what(rendition.file)
  -        return HttpResponse(FileWrapper(rendition.file), content_type='image/' + image_format)
  +        return StreamingHttpResponse(FileWrapper(rendition.file), content_type='image/' + image_format)
       except InvalidFilterSpecError:
           return HttpResponse("Invalid filter spec: " + filter_spec, content_type='text/plain', status=400)
  
verification_info: {"golden_diff": "diff --git a/wagtail/wagtailimages/views/frontend.py b/wagtail/wagtailimages/views/frontend.py\n--- a/wagtail/wagtailimages/views/frontend.py\n+++ b/wagtail/wagtailimages/views/frontend.py\n@@ -2,7 +2,7 @@\n import imghdr\n \n from django.shortcuts import get_object_or_404\n-from django.http import HttpResponse\n+from django.http import HttpResponse, StreamingHttpResponse\n from django.core.exceptions import PermissionDenied\n \n from wagtail.wagtailimages.models import get_image_model\n@@ -20,6 +20,6 @@\n         rendition = image.get_rendition(filter_spec)\n         rendition.file.open('rb')\n         image_format = imghdr.what(rendition.file)\n-        return HttpResponse(FileWrapper(rendition.file), content_type='image/' + image_format)\n+        return StreamingHttpResponse(FileWrapper(rendition.file), content_type='image/' + image_format)\n     except InvalidFilterSpecError:\n         return HttpResponse(\"Invalid filter spec: \" + filter_spec, content_type='text/plain', status=400)\n", "issue": "Error when serving images through the URL generator\nI posted a comment on https://github.com/torchbox/wagtail/issues/983 but probably better to open a new issue. Looks like the same problem to me though.\n\nHi guys, I think I'm having the same problem but when serving images using the URL generator. It does work if I'm logged-in in the site (cache not working) but doesn't when I'm not (cache full on).\n\nCheers,\nJordi\n\nInternal Server Error: /images/2dMQIUOPwS5DlZuprp_E_WFdfhw=/47/width-75/\nTraceback (most recent call last):\n  File \"/var/www/buildability/venvs/buildability.co.nz/local/lib/python2.7/site-packages/django/core/handlers/base.py\", line 204, in get_response\n    response = middleware_method(request, response)\n  File \"/var/www/buildability/venvs/buildability.co.nz/local/lib/python2.7/site-packages/django/middleware/cache.py\", line 121, in process_response\n    self.cache.set(cache_key, response, timeout)\n  File \"/var/www/buildability/venvs/buildability.co.nz/local/lib/python2.7/site-packages/redis_cache/cache.py\", line 239, in set\n    result = self._set(key, pickle.dumps(value), timeout, client, _add_only)\n  File \"/var/www/buildability/venvs/buildability.co.nz/lib/python2.7/copy_reg.py\", line 70, in _reduce_ex\n    raise TypeError, \"can't pickle %s objects\" % base.__name__\nTypeError: can't pickle instancemethod objects\n\nRequest repr(): \n<WSGIRequest\npath:/images/2dMQIUOPwS5DlZuprp_E_WFdfhw=/47/width-75/,\nGET:<QueryDict: {}>,\nPOST:<QueryDict: {}>,\nCOOKIES:{'_ga': 'GA1.3.1219121887.1434427204',\n 'csrftoken': 'GNhfTEGBu40y8wRAFPa15lQTV66F9WCs'},\nMETA:{'CONTENT_LENGTH': '',\n 'CONTENT_TYPE': '',\n u'CSRF_COOKIE': u'GNhfTEGBu40y8wRAFPa15lQTV66F9WCs',\n 'DOCUMENT_ROOT': '/usr/share/nginx/html',\n 'HTTP_ACCEPT': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,_/_;q=0.8',\n 'HTTP_ACCEPT_ENCODING': 'gzip, deflate, sdch',\n 'HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.8',\n 'HTTP_CACHE_CONTROL': 'max-age=0',\n 'HTTP_CONNECTION': 'keep-alive',\n 'HTTP_COOKIE': '_ga=GA1.3.1219121887.1434427204; csrftoken=GNhfTEGBu40y8wRAFPa15lQTV66F9WCs',\n 'HTTP_HOST': 'www.buildability.co.nz',\n 'HTTP_UPGRADE_INSECURE_REQUESTS': '1',\n 'HTTP_USER_AGENT': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/44.0.2403.130 Safari/537.36',\n 'PATH_INFO': u'/images/2dMQIUOPwS5DlZuprp_E_WFdfhw=/47/width-75/',\n 'QUERY_STRING': '',\n 'REMOTE_ADDR': '131.203.137.142',\n 'REMOTE_PORT': '51455',\n 'REQUEST_METHOD': 'GET',\n 'REQUEST_URI': '/images/2dMQIUOPwS5DlZuprp_E_WFdfhw%3D/47/width-75/',\n u'SCRIPT_NAME': u'',\n 'SERVER_NAME': 'www.buildability.co.nz',\n 'SERVER_PORT': '80',\n 'SERVER_PROTOCOL': 'HTTP/1.1',\n 'UWSGI_SCHEME': 'http',\n 'uwsgi.core': 7,\n 'uwsgi.node': 'avinton',\n 'uwsgi.version': '1.9.17.1-debian',\n 'wsgi.errors': <open file 'wsgi_errors', mode 'w' at 0x7f0548a548a0>,\n 'wsgi.file_wrapper': <built-in function uwsgi_sendfile>,\n 'wsgi.input': <uwsgi._Input object at 0x7f0548a20a08>,\n 'wsgi.multiprocess': True,\n 'wsgi.multithread': True,\n 'wsgi.run_once': False,\n 'wsgi.url_scheme': 'http',\n 'wsgi.version': (1, 0)}>\n\n", "before_files": [{"content": "from wsgiref.util import FileWrapper\nimport imghdr\n\nfrom django.shortcuts import get_object_or_404\nfrom django.http import HttpResponse\nfrom django.core.exceptions import PermissionDenied\n\nfrom wagtail.wagtailimages.models import get_image_model\nfrom wagtail.wagtailimages.utils import verify_signature\nfrom wagtail.wagtailimages.exceptions import InvalidFilterSpecError\n\n\ndef serve(request, signature, image_id, filter_spec):\n    image = get_object_or_404(get_image_model(), id=image_id)\n\n    if not verify_signature(signature.encode(), image_id, filter_spec):\n        raise PermissionDenied\n\n    try:\n        rendition = image.get_rendition(filter_spec)\n        rendition.file.open('rb')\n        image_format = imghdr.what(rendition.file)\n        return HttpResponse(FileWrapper(rendition.file), content_type='image/' + image_format)\n    except InvalidFilterSpecError:\n        return HttpResponse(\"Invalid filter spec: \" + filter_spec, content_type='text/plain', status=400)\n", "path": "wagtail/wagtailimages/views/frontend.py"}], "after_files": [{"content": "from wsgiref.util import FileWrapper\nimport imghdr\n\nfrom django.shortcuts import get_object_or_404\nfrom django.http import HttpResponse, StreamingHttpResponse\nfrom django.core.exceptions import PermissionDenied\n\nfrom wagtail.wagtailimages.models import get_image_model\nfrom wagtail.wagtailimages.utils import verify_signature\nfrom wagtail.wagtailimages.exceptions import InvalidFilterSpecError\n\n\ndef serve(request, signature, image_id, filter_spec):\n    image = get_object_or_404(get_image_model(), id=image_id)\n\n    if not verify_signature(signature.encode(), image_id, filter_spec):\n        raise PermissionDenied\n\n    try:\n        rendition = image.get_rendition(filter_spec)\n        rendition.file.open('rb')\n        image_format = imghdr.what(rendition.file)\n        return StreamingHttpResponse(FileWrapper(rendition.file), content_type='image/' + image_format)\n    except InvalidFilterSpecError:\n        return HttpResponse(\"Invalid filter spec: \" + filter_spec, content_type='text/plain', status=400)\n", "path": "wagtail/wagtailimages/views/frontend.py"}]}