Use voussoirkit.flasktools for gzip_response.

This commit is contained in:
voussoir 2021-04-02 18:31:44 -07:00
parent c73556d1e6
commit ff1ec3896b
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB

View file

@ -3,12 +3,11 @@ Do not execute this file directly.
Use etiquette_flask_dev.py or etiquette_flask_prod.py. Use etiquette_flask_dev.py or etiquette_flask_prod.py.
''' '''
import flask; from flask import request import flask; from flask import request
import gzip
import io
import mimetypes import mimetypes
import traceback import traceback
from voussoirkit import bytestring from voussoirkit import bytestring
from voussoirkit import flasktools
from voussoirkit import pathclass from voussoirkit import pathclass
import etiquette import etiquette
@ -82,54 +81,11 @@ def before_request():
if site.localhost_only and not request.is_localhost: if site.localhost_only and not request.is_localhost:
flask.abort(403) flask.abort(403)
gzip_minimum_size = 500 * bytestring.BYTE
gzip_maximum_size = 5 * bytestring.MIBIBYTE
gzip_level = 3
def should_gzip(response):
if response.direct_passthrough:
return False
if response.status_code < 200:
return False
if response.status_code >= 300:
return False
content_length = response.headers.get('Content-Length', None)
if content_length is not None and int(content_length) > gzip_maximum_size:
return False
if content_length is not None and int(content_length) < gzip_minimum_size:
return False
if 'Content-Encoding' in response.headers:
return False
accept_encoding = request.headers.get('Accept-Encoding', '')
if 'gzip' not in accept_encoding.lower():
return False
content_type = response.headers.get('Content-Type', '')
if not (content_type.startswith('application/json') or content_type.startswith('text/')):
return False
return True
@site.after_request @site.after_request
def after_request(response): def after_request(response):
if not should_gzip(response): response = flasktools.gzip_response(request, response)
return response
###
gzip_buffer = io.BytesIO()
gzip_file = gzip.GzipFile(mode='wb', compresslevel=gzip_level, fileobj=gzip_buffer)
gzip_file.write(response.get_data())
gzip_file.close()
response.set_data(gzip_buffer.getvalue())
response.headers['Content-Encoding'] = 'gzip'
response.headers['Content-Length'] = len(response.get_data())
return response return response
# P functions ###################################################################################### # P functions ######################################################################################