Skip to content

Commit 1158844

Browse files
authored
Fix zero division errors for accept headers with zero q values (#2491)
1 parent 7b4378d commit 1158844

3 files changed

Lines changed: 28 additions & 3 deletions

File tree

‎pygeoapi/util.py‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# =================================================================
22
#
33
# Authors: Tom Kralidis <tomkralidis@gmail.com>
4+
# Francesco Bartoli <xbartolone@gmail.com>
45
#
56
# Copyright (c) 2026 Tom Kralidis
7+
# Copyright (c) 2026 Francesco Bartoli
68
#
79
# Permission is hereby granted, free of charge, to any person
810
# obtaining a copy of this software and associated documentation
@@ -745,15 +747,17 @@ def get_choice_from_headers(headers: dict,
745747
value, q_value = match.groups()
746748
q_value = float(q_value) if q_value else 1.0
747749

748-
# Sort choices by q value and index
749-
if 0 <= q_value <= 1:
750+
# Sort choices by q value and index; q=0 means not acceptable
751+
if 0 < q_value <= 1:
750752
heappush(choices, (1 / q_value, i, value))
751753

752754
# Drop q value
753755
sorted_choices = [choice[-1] for choice in choices]
754756

755757
# Return one or all choices
756-
return sorted_choices if all else sorted_choices[0]
758+
if all:
759+
return sorted_choices
760+
return sorted_choices[0] if sorted_choices else None
757761

758762

759763
def get_dataset_formatters(dataset: dict) -> dict:

‎tests/api/test_api.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,13 @@ def test_apirequest(api_):
186186
assert apireq.get_linkrel(F_JSONLD) == 'self'
187187
assert apireq.get_linkrel(F_HTML) == 'alternate'
188188

189+
# A format with q=0 is not acceptable (RFC 9110, section 12.4.2)
190+
hh = 'text/html;q=0,application/json;q=0.5'
191+
req = mock_request(HTTP_ACCEPT=hh)
192+
apireq = APIRequest(req, api_.locales)
193+
assert apireq.is_valid()
194+
assert apireq.format == F_JSON
195+
189196
# Overrule HTTP content negotiation
190197
req = mock_request({'f': 'html'}, HTTP_ACCEPT='application/json') # noqa
191198
apireq = APIRequest(req, api_.locales)

‎tests/other/test_util.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
# =================================================================
22
#
33
# Authors: Tom Kralidis <tomkralidis@gmail.com>
4+
# Francesco Bartoli <xbartolone@gmail.com>
45
#
56
# Copyright (c) 2026 Tom Kralidis
7+
# Copyright (c) 2026 Francesco Bartoli
68
#
79
# Permission is hereby granted, free of charge, to any person
810
# obtaining a copy of this software and associated documentation
@@ -331,6 +333,18 @@ def test_get_choice_from_headers():
331333
{'accept-language': 'en_US', 'accept': '*/*'}, 'accept') == '*/*'
332334

333335

336+
def test_get_choice_from_headers_drops_q_zero():
337+
# RFC 9110, section 12.4.2: q=0 means "not acceptable"
338+
_headers = {'accept': 'image/png;q=0,application/json;q=0.5'}
339+
340+
assert util.get_choice_from_headers(
341+
_headers, 'accept', all=True) == ['application/json']
342+
assert util.get_choice_from_headers(
343+
{'accept': 'image/png;q=0'}, 'accept') is None
344+
assert util.get_choice_from_headers(
345+
{'accept': 'image/png;q=0'}, 'accept', all=True) == []
346+
347+
334348
@pytest.mark.parametrize('url,allow_internal,result', [
335349
['http://127.0.0.1/test', False, False],
336350
['http://127.0.0.1/test', True, True],

0 commit comments

Comments
 (0)