Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions prometheus_client/exposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down
16 changes: 16 additions & 0 deletions tests/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Loading