diff --git a/prometheus_client/exposition.py b/prometheus_client/exposition.py index 0b63f6f6..1c60e5ba 100644 --- a/prometheus_client/exposition.py +++ b/prometheus_client/exposition.py @@ -429,8 +429,18 @@ def _get_escaping(accept_header: List[str]) -> str: def gzip_accepted(accept_encoding_header: str) -> bool: accept_encoding_header = accept_encoding_header or '' for accepted in accept_encoding_header.split(','): - if accepted.split(';')[0].strip().lower() == 'gzip': - return True + encoding, *params = accepted.split(';') + if encoding.strip().lower() != 'gzip': + continue + for param in params: + key, sep, value = param.strip().partition('=') + if sep and key.strip().lower() == 'q': + try: + quality = float(value.strip()) + except ValueError: + return False + return 0 < quality <= 1 + return True return False diff --git a/tests/test_wsgi.py b/tests/test_wsgi.py index eb2d0566..3c12fc69 100644 --- a/tests/test_wsgi.py +++ b/tests/test_wsgi.py @@ -112,3 +112,19 @@ def test_gzip_disabled(self): outputs = app(gzip_environ, self.capture) # Assert outputs are not compressed. self.assert_outputs(outputs, metric_name, help_text, increments, compressed=False) + + def test_gzip_quality(self): + self.increment_metrics("counter", "A counter", 2) + app = make_wsgi_app(self.registry) + for encoding, compressed in [ + ("gzip;q=0", False), + ("gzip;q=0.0", False), + ("br, GZip; Q=0", False), + ("gzip;q=0.5", True), + ("gzip;q=bogus", False), + ]: + with self.subTest(encoding=encoding): + environ = dict(self.environ) + environ['HTTP_ACCEPT_ENCODING'] = encoding + outputs = app(environ, self.capture) + self.assert_outputs(outputs, "counter", "A counter", 2, compressed)