Skip to content
Myr

Ensure H2 state is only accessed by the connection, not per-stream. (#628)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Ensure H2 state is only accessed by the connection, not per-stream * Formatting tweak

authored by Tom Christie · committed by GitHub · parent 869714f · Browse files · 1 file+33 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+33 added−20 removed

  • other:+33 added−20 removed

1 file+33 added−20 removed

modified:httpx/dispatch/http2.py+33 added-20 removed
62 unmodified lines
63 # All other streams need to wait until the connection is established.64 await self.init_complete.wait()65 stream_id = self.state.get_next_available_stream_id()6667 stream = HTTP2Stream(stream_id=stream_id, connection=self, state=self.state)67 stream = HTTP2Stream(stream_id=stream_id, connection=self)68 self.streams[stream_id] = stream69 self.events[stream_id] = []70 return await stream.send(request, timeout)71
85 unmodified lines
157158 data_to_send = self.state.data_to_send()159 await self.socket.write(data_to_send, timeout)160161 async def send_outgoing_data(self, timeout: Timeout) -> None:161 async def send_headers(162 self,163 stream_id: int,164 headers: typing.List[typing.Tuple[bytes, bytes]],165 timeout: Timeout,166 ) -> None:167 self.state.send_headers(stream_id, headers)168 data_to_send = self.state.data_to_send()163 if data_to_send:164 await self.socket.write(data_to_send, timeout)169 await self.socket.write(data_to_send, timeout)170171 async def send_data(self, stream_id: int, chunk: bytes, timeout: Timeout) -> None:172 self.state.send_data(stream_id, chunk)173 data_to_send = self.state.data_to_send()174 await self.socket.write(data_to_send, timeout)175176 async def end_stream(self, stream_id: int, timeout: Timeout) -> None:177 self.state.end_stream(stream_id)178 data_to_send = self.state.data_to_send()179 await self.socket.write(data_to_send, timeout)180181 async def acknowledge_received_data(182 self, stream_id: int, amount: int, timeout: Timeout183 ) -> None:184 self.state.acknowledge_received_data(amount, stream_id)185 data_to_send = self.state.data_to_send()186 await self.socket.write(data_to_send, timeout)187188 async def close_stream(self, stream_id: int) -> None:189 del self.streams[stream_id]190 del self.events[stream_id]191
1 unmodified line
193 await self.on_release()194195196class HTTP2Stream:175 def __init__(176 self,177 stream_id: int,178 connection: HTTP2Connection,179 state: h2.connection.H2Connection,180 ) -> None:197 def __init__(self, stream_id: int, connection: HTTP2Connection) -> None:198 self.stream_id = stream_id199 self.connection = connection183 self.state = state200201 async def send(self, request: Request, timeout: Timeout) -> Response:202 # Send the request.203 await self.send_headers(request, timeout)
25 unmodified lines
229 f"method={request.method!r} "230 f"target={request.url.full_path!r} "231 f"headers={headers!r}"232 )217 self.state.send_headers(self.stream_id, headers)218 await self.connection.send_outgoing_data(timeout)233 await self.connection.send_headers(self.stream_id, headers, timeout)234235 async def send_body(self, request: Request, timeout: Timeout) -> None:236 logger.trace(f"send_body stream_id={self.stream_id}")237 async for data in request.stream():
2 unmodified lines
240 self.stream_id, timeout241 )242 chunk_size = min(len(data), max_flow)243 chunk, data = data[:chunk_size], data[chunk_size:]229 self.state.send_data(self.stream_id, chunk)230 await self.connection.send_outgoing_data(timeout)244 await self.connection.send_data(self.stream_id, chunk, timeout)245232 self.state.end_stream(self.stream_id)233 await self.connection.send_outgoing_data(timeout)246 await self.connection.end_stream(self.stream_id, timeout)247248 async def receive_response(249 self, timeout: Timeout250 ) -> typing.Tuple[int, typing.List[typing.Tuple[bytes, bytes]]]:
18 unmodified lines
269 async def body_iter(self, timeout: Timeout) -> typing.AsyncIterator[bytes]:270 while True:271 event = await self.connection.wait_for_event(self.stream_id, timeout)272 if isinstance(event, h2.events.DataReceived):260 self.state.acknowledge_received_data(261 event.flow_controlled_length, self.stream_id273 amount = event.flow_controlled_length274 await self.connection.acknowledge_received_data(275 self.stream_id, amount, timeout276 )263 await self.connection.send_outgoing_data(timeout)277 yield event.data278 elif isinstance(event, (h2.events.StreamEnded, h2.events.StreamReset)):279 break280
2 unmodified lines