Skip to content

Commit 99e5658

Browse files
committed
fix encoding with generators
Previously we printed a debug message with the headers to encode. This headers iterable could be a generator, which just debug-printed the type information, but not the expected header values within. As potential fix a previous commit simply converted it to a list - which then rendered the generator empty and unusable. The tests didn't cover this yet. This commit removes the debug-print altogether, because each added header is already debug-printed in the add() function. We add some additional information to this existing debug print and remove the top-level debug-print in the encode() function. This commit adds a simple test case for passing a generator as headers into encode().
1 parent da26ef4 commit 99e5658

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/hpack/hpack.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,6 @@ def encode(self, headers, huffman=True):
229229
# are already in the header table we can represent them using the
230230
# indexed representation: the same is true if they are in the static
231231
# table. Otherwise, a literal representation will be used.
232-
log.debug("HPACK encoding %s", list(headers))
233232
header_block = []
234233

235234
# Turn the headers into a list of tuples if possible. This is the
@@ -265,7 +264,12 @@ def add(self, to_add, sensitive, huffman=False):
265264
"""
266265
This function takes a header key-value tuple and serializes it.
267266
"""
268-
log.debug("Adding %s to the header table", to_add)
267+
log.debug(
268+
"Adding %s to the header table, sensitive:%s, huffman:%s",
269+
to_add,
270+
sensitive,
271+
huffman
272+
)
269273

270274
name, value = to_add
271275

test/test_hpack.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,16 @@ def test_evicting_header_table_objects(self):
338338

339339
assert len(e.header_table.dynamic_entries) == 1
340340

341+
def test_headers_generator(self):
342+
e = Encoder()
343+
344+
def headers_generator():
345+
return (("k" + str(i), "v" + str(i)) for i in range(3))
346+
347+
header_set = headers_generator()
348+
out = e.encode(header_set)
349+
assert Decoder().decode(out) == list(headers_generator())
350+
341351

342352
class TestHPACKDecoder:
343353
# These tests are stolen entirely from the IETF specification examples.

0 commit comments

Comments
 (0)