Skip to content

Commit c71e301

Browse files
BYKKriechi
authored andcommitted
fix: No more BytesWarnings
Fixes #1236. This patch makes all header operations operate on `bytes` and converts all headers and values to bytes before operation. With a follow up patch to `hpack` it should also increase efficiency as currently, `hpack` casts everything to a `str` first before converting back to bytes: https://github.com/python-hyper/hpack/blob/02afcab28ca56eb5259904fd414baa89e9f50266/src/hpack/hpack.py#L150-L151
1 parent 25f4b75 commit c71e301

File tree

6 files changed

+111
-151
lines changed

6 files changed

+111
-151
lines changed

src/h2/connection.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from .frame_buffer import FrameBuffer
3434
from .settings import Settings, SettingCodes
3535
from .stream import H2Stream, StreamClosedBy
36-
from .utilities import SizeLimitDict, guard_increment_window
36+
from .utilities import SizeLimitDict, guard_increment_window, utf8_encode_headers
3737
from .windows import WindowManager
3838

3939

@@ -975,6 +975,7 @@ def push_stream(self, stream_id, promised_stream_id, request_headers):
975975
)
976976
self.streams[promised_stream_id] = new_stream
977977

978+
request_headers = utf8_encode_headers(request_headers)
978979
frames = stream.push_stream_in_band(
979980
promised_stream_id, request_headers, self.encoder
980981
)

src/h2/stream.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
from .utilities import (
2626
guard_increment_window, is_informational_response, authority_from_headers,
2727
validate_headers, validate_outbound_headers, normalize_outbound_headers,
28-
HeaderValidationFlags, extract_method_header, normalize_inbound_headers
28+
HeaderValidationFlags, extract_method_header, normalize_inbound_headers,
29+
utf8_encode_headers
2930
)
3031
from .windows import WindowManager
3132

@@ -851,6 +852,8 @@ def send_headers(self, headers, encoder, end_stream=False):
851852
# we need to scan the header block to see if this is an informational
852853
# response.
853854
input_ = StreamInputs.SEND_HEADERS
855+
856+
headers = utf8_encode_headers(headers)
854857
if ((not self.state_machine.client) and
855858
is_informational_response(headers)):
856859
if end_stream:
@@ -1319,7 +1322,7 @@ def _initialize_content_length(self, headers):
13191322
self._expected_content_length = int(v, 10)
13201323
except ValueError:
13211324
raise ProtocolError(
1322-
"Invalid content-length header: %s" % v
1325+
f"Invalid content-length header: {repr(v)}"
13231326
)
13241327

13251328
return

0 commit comments

Comments
 (0)