|
5 | 5 | Implements the HPACK header compression algorithm as detailed by the IETF. |
6 | 6 | """ |
7 | 7 | import logging |
8 | | -from typing import Any, Generator, Union |
| 8 | +from typing import Any, Generator, Iterable, Optional, Union |
9 | 9 |
|
10 | 10 | from .table import HeaderTable, table_entry_size |
11 | 11 | from .exceptions import ( |
|
16 | 16 | REQUEST_CODES, REQUEST_CODES_LENGTH |
17 | 17 | ) |
18 | 18 | from .huffman_table import decode_huffman |
19 | | -from .struct import HeaderTuple, NeverIndexedHeaderTuple, Headers |
| 19 | +from .struct import HeaderTuple, NeverIndexedHeaderTuple, HeaderWeaklyTyped |
| 20 | +from .table import HeaderTable, table_entry_size |
20 | 21 |
|
21 | 22 | log = logging.getLogger(__name__) |
22 | 23 |
|
|
34 | 35 | DEFAULT_MAX_HEADER_LIST_SIZE = 2 ** 16 |
35 | 36 |
|
36 | 37 |
|
37 | | -def _unicode_if_needed(header: HeaderTuple, raw: bool) -> HeaderTuple: |
| 38 | +def _unicode_if_needed(header: HeaderWeaklyTyped, raw: bool) -> HeaderTuple: |
38 | 39 | """ |
39 | 40 | Provides a header as a unicode string if raw is False, otherwise returns |
40 | 41 | it as a bytestring. |
41 | 42 | """ |
42 | 43 | name = bytes(header[0]) # type: ignore |
43 | 44 | value = bytes(header[1]) # type: ignore |
| 45 | + |
44 | 46 | if not raw: |
45 | | - return header.__class__(name.decode('utf-8'), value.decode('utf-8')) |
46 | | - else: |
47 | | - return header.__class__(name, value) |
| 47 | + return header.__class__(name.decode("utf-8"), value.decode("utf-8")) # type: ignore |
| 48 | + return header.__class__(name, value) # type: ignore |
48 | 49 |
|
49 | 50 |
|
50 | 51 | def encode_integer(integer: int, prefix_bits: int) -> bytearray: |
@@ -123,7 +124,7 @@ def decode_integer(data: bytes, prefix_bits: int) -> tuple[int, int]: |
123 | 124 |
|
124 | 125 |
|
125 | 126 | def _dict_to_iterable(header_dict: Union[dict[bytes, bytes], dict[str, str]]) \ |
126 | | - -> Generator[Union[tuple[bytes, bytes], tuple[str, str]], None, None]: |
| 127 | + -> Generator[Union[tuple[bytes, bytes, Optional[bool]], tuple[str, str, Optional[bool]]], None, None]: |
127 | 128 | """ |
128 | 129 | This converts a dictionary to an iterable of two-tuples. This is a |
129 | 130 | HPACK-specific function because it pulls "special-headers" out first and |
@@ -177,7 +178,7 @@ def header_table_size(self, value: int) -> None: |
177 | 178 | self.table_size_changes.append(value) |
178 | 179 |
|
179 | 180 | def encode(self, |
180 | | - headers: Headers, |
| 181 | + headers: Union[Iterable[tuple[Union[bytes, str], Union[bytes, str], Optional[bool]]], dict[Union[bytes, str], Union[bytes, str, tuple]]], |
181 | 182 | huffman: bool = True) -> bytes: |
182 | 183 | """ |
183 | 184 | Takes a set of headers and encodes them into a HPACK-encoded header |
@@ -433,7 +434,7 @@ def header_table_size(self) -> int: |
433 | 434 | def header_table_size(self, value: int) -> None: |
434 | 435 | self.header_table.maxsize = value |
435 | 436 |
|
436 | | - def decode(self, data: bytes, raw: bool = False) -> Headers: |
| 437 | + def decode(self, data: bytes, raw: bool = False) -> Iterable[HeaderTuple]: |
437 | 438 | """ |
438 | 439 | Takes an HPACK-encoded header block and decodes it into a header set. |
439 | 440 |
|
|
0 commit comments