Skip to content

Comments

Fix perfect match missed for headers with empty values#287

Open
bysiber wants to merge 1 commit intopython-hyper:masterfrom
bysiber:fix-empty-value-perfect-match
Open

Fix perfect match missed for headers with empty values#287
bysiber wants to merge 1 commit intopython-hyper:masterfrom
bysiber:fix-empty-value-perfect-match

Conversation

@bysiber
Copy link

@bysiber bysiber commented Feb 20, 2026

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 check fails when value is b"" because empty bytes are falsy in Python.

46 of 61 static table entries have an empty value (:authority, accept-charset, accept-language, age, allow, authorization, cache-control, etc.). When encoding a header that perfectly matches one of those entries, the encoder falls through to the indexed-literal branch instead:

>>> from hpack import Encoder
>>> e = Encoder()
>>> e.encode([(b':authority', b'')]).hex()
'4180'   # indexed literal, 2 bytes — also adds to dynamic table

Expected: 81 (indexed representation, 1 byte, no dynamic table entry).

The indexed-literal path also calls self.header_table.add(name, value), which adds an unnecessary duplicate of the static entry to the dynamic table, wasting space and evicting useful entries.

This changes if perfect: to if perfect is not None:.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant