Skip to content

Commit

Permalink
Merge pull request #538 from alphagov/ris-dm-gzip-middleware-warning
Browse files Browse the repository at this point in the history
DMGzipMiddleware: emit warning when middleware receives an unknown X-Compression-Safe value
  • Loading branch information
risicle authored Sep 27, 2019
2 parents c0f6d2d + 6b1b0fe commit 158a742
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 1 addition & 1 deletion dmutils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
from .flask_init import init_app, init_manager


__version__ = '48.6.0'
__version__ = '48.6.1'
10 changes: 9 additions & 1 deletion dmutils/flask.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from functools import partial
import warnings

from flask import render_template, render_template_string
from flask_gzip import Gzip
Expand Down Expand Up @@ -45,7 +46,14 @@ def __init__(self, *args, compress_by_default: bool = False, **kwargs):

def after_request(self, response):
x_compression_safe = response.headers.pop("X-Compression-Safe", None)
compress = {"0": False, "1": True}.get(x_compression_safe, self.compress_by_default)
known_values = {"0": False, "1": True}
compress = known_values.get(x_compression_safe, self.compress_by_default)

if x_compression_safe not in known_values and x_compression_safe != "" and x_compression_safe is not None:
warnings.warn(
f"{self.__class__.__name__} received unknown X-Compression-Safe header value {x_compression_safe!r} - "
"falling back to default"
)

# flask_gzip makes the minimum_size comparison itself, but we want to avoid outputting a misleading
# logged_duration message if it's going to be prevented in the superclass.
Expand Down

0 comments on commit 158a742

Please sign in to comment.