From 55fb979923f9e314074aa4b2ae0de86b8acae087 Mon Sep 17 00:00:00 2001 From: Kadir Can Ozden <101993364+bysiber@users.noreply.github.com> Date: Fri, 20 Feb 2026 18:57:52 +0300 Subject: [PATCH] Fix perfect match missed for headers with empty values MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit HeaderTable.search() returns (index, name, value) for perfect matches and (index, name, None) for partial matches. The encoder distinguishes them with `if perfect:`, but this fails when value is b"" because empty bytes are falsy in Python. 46 of 61 static table entries have an empty value (e.g. :authority, accept-charset, accept-language, age, allow, …). When encoding a header that perfectly matches one of these entries, the encoder falls through to the indexed-literal path — using 2+ bytes instead of 1 and unnecessarily adding the entry to the dynamic table. Change the check to `if perfect is not None:` so that b"" is treated as a valid perfect match. --- src/hpack/hpack.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hpack/hpack.py b/src/hpack/hpack.py index 7e33e77..20dce0a 100644 --- a/src/hpack/hpack.py +++ b/src/hpack/hpack.py @@ -313,7 +313,7 @@ def add(self, to_add: tuple[bytes, bytes], sensitive: bool, huffman: bool = Fals # can use the indexed literal. index, name, perfect = match - if perfect: + if perfect is not None: # Indexed representation. encoded = self._encode_indexed(index) else: