Skip to content

Commit 3b154b7

Browse files
derek-pynehashhar
authored andcommitted
Do not fail on empty values for mutli-valued headers
Before this change user would receive a ValueError: not enough values to unpack if the session property, role or prepared statements headers existed but had empty values. While Trino itself doesn't send empty headers it's possible for them to be present because of some proxy or gateway sitting between client and the server.
1 parent 0399b86 commit 3b154b7

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

tests/unit/test_http.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
# limitations under the License.
1212

1313
from trino import constants
14-
from trino.client import get_header_values, get_session_property_values
14+
from trino.client import (
15+
get_header_values,
16+
get_prepared_statement_values,
17+
get_roles_values,
18+
get_session_property_values,
19+
)
1520

1621

1722
def test_get_header_values():
@@ -24,3 +29,21 @@ def test_get_session_property_values():
2429
headers = {constants.HEADER_SET_SESSION: "a=1, b=2, c=more%3Dv1%2Cv2"}
2530
values = get_session_property_values(headers, constants.HEADER_SET_SESSION)
2631
assert values == [("a", "1"), ("b", "2"), ("c", "more=v1,v2")]
32+
33+
34+
def test_get_session_property_values_ignores_empty_values():
35+
headers = {constants.HEADER_SET_SESSION: ""}
36+
values = get_session_property_values(headers, constants.HEADER_SET_SESSION)
37+
assert len(values) == 0
38+
39+
40+
def test_get_prepared_statement_values_ignores_empty_values():
41+
headers = {constants.HEADER_SET_SESSION: ""}
42+
values = get_prepared_statement_values(headers, constants.HEADER_SET_SESSION)
43+
assert len(values) == 0
44+
45+
46+
def test_get_roles_values_ignores_empty_values():
47+
headers = {constants.HEADER_SET_SESSION: ""}
48+
values = get_roles_values(headers, constants.HEADER_SET_SESSION)
49+
assert len(values) == 0

trino/client.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,23 +225,23 @@ def get_session_property_values(headers, header):
225225
kvs = get_header_values(headers, header)
226226
return [
227227
(k.strip(), urllib.parse.unquote_plus(v.strip()))
228-
for k, v in (kv.split("=", 1) for kv in kvs)
228+
for k, v in (kv.split("=", 1) for kv in kvs if kv)
229229
]
230230

231231

232232
def get_prepared_statement_values(headers, header):
233233
kvs = get_header_values(headers, header)
234234
return [
235235
(k.strip(), urllib.parse.unquote_plus(v.strip()))
236-
for k, v in (kv.split("=", 1) for kv in kvs)
236+
for k, v in (kv.split("=", 1) for kv in kvs if kv)
237237
]
238238

239239

240240
def get_roles_values(headers, header):
241241
kvs = get_header_values(headers, header)
242242
return [
243243
(k.strip(), urllib.parse.unquote_plus(v.strip()))
244-
for k, v in (kv.split("=", 1) for kv in kvs)
244+
for k, v in (kv.split("=", 1) for kv in kvs if kv)
245245
]
246246

247247

0 commit comments

Comments
 (0)