Martiniでレスポンスをgzip compressする
martini-contribにgzipも用意されている。
ただ、Accept-Encodingにgzipが含まれていれば無条件でgzipするので、小さいコンテンツを返すときは余計にサイズが大きくなってしまう。
djangoを見てみたら、django.middleware.gzip.GZipMiddlewareというのが用意されているが、デフォルトのMIDDLEWARE_CLASSESからはコメントアウトされている。
MIDDLEWARE_CLASSES = ( 'django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', # 'django.middleware.http.ConditionalGetMiddleware', # 'django.middleware.gzip.GZipMiddleware', )
さらに小さいレスポンスの場合はgzipしないようになっている。
class GZipMiddleware(object): """ This middleware compresses content if the browser allows gzip compression. It sets the Vary header accordingly, so that caches will base their storage on the Accept-Encoding header. """ def process_response(self, request, response): # It's not worth attempting to compress really short responses. if not response.streaming and len(response.content) < 200: return response
dedoratorも用意されていて、view関数ごとにgzipすることができる