diff --git a/README.md b/README.md index 842b52e..cdbfb32 100644 --- a/README.md +++ b/README.md @@ -114,3 +114,5 @@ Within your Flask application's settings you can provide the following settings | `COMPRESS_CACHE_BACKEND` | Specified the backend for storing the cached response data. | `None` | | `COMPRESS_REGISTER` | Specifies if compression should be automatically registered. | `True` | | `COMPRESS_ALGORITHM` | Supported compression algorithms. | `['br', 'gzip', 'deflate']` | +| `COMPRESS_EXCLUDE_UA` | Do not compress User Agents containing one of the strings in the list. | `[]` | +| `COMPRESS_TRACE` | Log debug information. | `False` | diff --git a/flask_compress/flask_compress.py b/flask_compress/flask_compress.py index 8f01287..e245426 100644 --- a/flask_compress/flask_compress.py +++ b/flask_compress/flask_compress.py @@ -78,6 +78,8 @@ def init_app(self, app): ('COMPRESS_CACHE_BACKEND', None), ('COMPRESS_REGISTER', True), ('COMPRESS_ALGORITHM', ['br', 'gzip', 'deflate']), + ('COMPRESS_EXCLUDE_UA', []), + ('COMPRESS_TRACE', False) ] for k, v in defaults: @@ -166,6 +168,9 @@ def after_request(self, response): app = self.app or current_app vary = response.headers.get('Vary') + if app.config["COMPRESS_TRACE"]: + app.logger.info('COMPRESS_TRACE_UA: "%s"', + request.headers.get('user-agent')) if not vary: response.headers['Vary'] = 'Accept-Encoding' elif 'accept-encoding' not in vary.lower(): @@ -175,6 +180,8 @@ def after_request(self, response): chosen_algorithm = self._choose_compress_algorithm(accept_encoding) if (chosen_algorithm is None or + any([i in request.headers.get('user-agent') + for i in app.config["COMPRESS_EXCLUDE_UA"]]) or response.mimetype not in app.config["COMPRESS_MIMETYPES"] or response.status_code < 200 or response.status_code >= 300 or @@ -182,6 +189,8 @@ def after_request(self, response): "Content-Encoding" in response.headers or (response.content_length is not None and response.content_length < app.config["COMPRESS_MIN_SIZE"])): + if app.config["COMPRESS_TRACE"]: + app.logger.info('COMPRESS_TRACE: no compression') return response response.direct_passthrough = False