|
16 | 16 |
|
17 | 17 | from __future__ import annotations
|
18 | 18 |
|
| 19 | +import warnings |
19 | 20 | from functools import lru_cache
|
20 | 21 |
|
21 | 22 | import h11
|
| 23 | +from h11._state import _SWITCH_UPGRADE, ConnectionState |
22 | 24 |
|
23 | 25 | from ..._stream_matrix import StreamMatrix
|
24 | 26 | from ..._typing import HeadersType
|
@@ -100,11 +102,35 @@ def headers_from_response(
|
100 | 102 | ] + response.headers.raw_items()
|
101 | 103 |
|
102 | 104 |
|
| 105 | +class RelaxConnectionState(ConnectionState): |
| 106 | + def process_event( # type: ignore[no-untyped-def] |
| 107 | + self, |
| 108 | + role, |
| 109 | + event_type, |
| 110 | + server_switch_event=None, |
| 111 | + ) -> None: |
| 112 | + if server_switch_event is not None: |
| 113 | + if server_switch_event not in self.pending_switch_proposals: |
| 114 | + if server_switch_event is _SWITCH_UPGRADE: |
| 115 | + warnings.warn( |
| 116 | + f"Received server {server_switch_event} event without a pending proposal. " |
| 117 | + "This will raise an exception in a future version. It is temporarily relaxed to match the " |
| 118 | + "legacy http.client standard library.", |
| 119 | + DeprecationWarning, |
| 120 | + stacklevel=2, |
| 121 | + ) |
| 122 | + self.pending_switch_proposals.add(_SWITCH_UPGRADE) |
| 123 | + |
| 124 | + return super().process_event(role, event_type, server_switch_event) |
| 125 | + |
| 126 | + |
103 | 127 | class HTTP1ProtocolHyperImpl(HTTP1Protocol):
|
104 | 128 | implementation: str = "h11"
|
105 | 129 |
|
106 | 130 | def __init__(self) -> None:
|
107 | 131 | self._connection: h11.Connection = h11.Connection(h11.CLIENT)
|
| 132 | + self._connection._cstate = RelaxConnectionState() |
| 133 | + |
108 | 134 | self._data_buffer: list[bytes] = []
|
109 | 135 | self._events: StreamMatrix = StreamMatrix()
|
110 | 136 | self._terminated: bool = False
|
@@ -255,7 +281,7 @@ def _headers_from_h11_response(
|
255 | 281 | )
|
256 | 282 |
|
257 | 283 | def _data_from_h11(self, h11_event: h11.Data) -> Event:
|
258 |
| - return DataReceived(self._current_stream_id, h11_event.data) |
| 284 | + return DataReceived(self._current_stream_id, bytes(h11_event.data)) |
259 | 285 |
|
260 | 286 | def _connection_terminated(
|
261 | 287 | self, error_code: int = 0, message: str | None = None
|
|
0 commit comments