django-gallery/gallery/gzip.py
David Todd 51a117ca5f
Add configuration to connect to a postgres database (#2)
CircleCI's free service only allows running 1 container at a time.
This causes problems if I want to use a database.
A workaround that I found seems to work by running the database server directly within the test container. This does increase test time however
2018-03-08 21:25:54 -06:00

28 lines
986 B
Python

import logging
from StringIO import StringIO
import zlib
class UnzipRequestMiddleware(object):
"""A middleware that unzips POSTed data.
For this middleware to kick in, the client must provide a value
for the ``Content-Encoding`` header. The only accepted value is
``gzip``. Any other value is ignored.
"""
def __init__(self, app):
self.app = app
def __call__(self, environ, start_response):
encoding = environ.get('HTTP_CONTENT_ENCODING')
if encoding == 'gzip':
data = environ['wsgi.input'].read()
try:
uncompressed = zlib.decompress(data, 31)
environ['wsgi.input'] = StringIO(uncompressed)
environ['CONTENT_LENGTH'] = len(uncompressed)
except zlib.error:
logging.warning(u"Could not decompress request data.", exc_info=True)
environ['wsgi.input'] = StringIO(data)
return self.app(environ, start_response)