Skip to content

Commit

Permalink
fix: No more BytesWarnings
Browse files Browse the repository at this point in the history
Fixes python-hyper#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
  • Loading branch information
BYK authored and Kriechi committed Nov 23, 2024
1 parent 25f4b75 commit 5841683
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 151 deletions.
3 changes: 2 additions & 1 deletion src/h2/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from .frame_buffer import FrameBuffer
from .settings import Settings, SettingCodes
from .stream import H2Stream, StreamClosedBy
from .utilities import SizeLimitDict, guard_increment_window
from .utilities import SizeLimitDict, guard_increment_window, utf8_encode_headers
from .windows import WindowManager


Expand Down Expand Up @@ -975,6 +975,7 @@ def push_stream(self, stream_id, promised_stream_id, request_headers):
)
self.streams[promised_stream_id] = new_stream

request_headers = utf8_encode_headers(request_headers)
frames = stream.push_stream_in_band(
promised_stream_id, request_headers, self.encoder
)
Expand Down
7 changes: 5 additions & 2 deletions src/h2/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
from .utilities import (
guard_increment_window, is_informational_response, authority_from_headers,
validate_headers, validate_outbound_headers, normalize_outbound_headers,
HeaderValidationFlags, extract_method_header, normalize_inbound_headers
HeaderValidationFlags, extract_method_header, normalize_inbound_headers,
utf8_encode_headers
)
from .windows import WindowManager

Expand Down Expand Up @@ -851,6 +852,8 @@ def send_headers(self, headers, encoder, end_stream=False):
# we need to scan the header block to see if this is an informational
# response.
input_ = StreamInputs.SEND_HEADERS

headers = utf8_encode_headers(headers)
if ((not self.state_machine.client) and
is_informational_response(headers)):
if end_stream:
Expand Down Expand Up @@ -1319,7 +1322,7 @@ def _initialize_content_length(self, headers):
self._expected_content_length = int(v, 10)
except ValueError:
raise ProtocolError(
"Invalid content-length header: %s" % v
f"Invalid content-length header: {repr(v)}"
)

return
Expand Down
Loading

0 comments on commit 5841683

Please sign in to comment.