From 972e1293816549fc430c080bc2758cfd2d741c18 Mon Sep 17 00:00:00 2001 From: Thomas Sileo Date: Fri, 23 Aug 2019 23:49:33 +0200 Subject: [PATCH] Support gzip compression for JSON resp --- core/shared.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/core/shared.py b/core/shared.py index 5d24198..d2d212f 100644 --- a/core/shared.py +++ b/core/shared.py @@ -1,7 +1,10 @@ +import gzip import json import os from functools import wraps from typing import Any +from typing import Dict +from typing import Tuple import flask from bson.objectid import ObjectId @@ -43,12 +46,27 @@ ap.use_backend(back) MY_PERSON = ap.Person(**ME) +def build_resp(resp): + """Encode the response to gzip if supported by the client.""" + headers = {} + accept_encoding = request.headers.get("Accept-Encoding", "") + if "gzip" in accept_encoding.lower(): + return ( + gzip.compress(resp.encode(), compresslevel=6), + {"Vary": "Accept-Encoding", "Content-Encoding": "gzip"}, + ) + + return resp, headers + + def jsonify(**data): if "@context" not in data: data["@context"] = config.DEFAULT_CTX + resp, headers = build_resp(json.dumps(data)) return Response( - response=json.dumps(data), + response=resp, headers={ + **headers, "Cache-Control": "max-age=0, private, must-revalidate", "Content-Type": "application/json" if app.debug