Skip to content
Myr

Rename BaseSocketStream cases from connection.stream to connection.socket (#601)

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

authored by Tom Christie · committed by GitHub · parent 8d8ea8b · Browse files · 3 files+22 added−22 removed
This commit

State

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

Diff stats

3 files+22 added−22 removed

  • other:+22 added−22 removed

3 files+22 added−22 removed

modified:httpx/dispatch/http11.py+7 added-7 removed
30 unmodified lines
31 READ_NUM_BYTES = 40963233 def __init__(34 self,35 stream: BaseSocketStream,35 socket: BaseSocketStream,36 on_release: typing.Optional[OnReleaseCallback] = None,37 ):38 self.stream = stream38 self.socket = socket39 self.on_release = on_release40 self.h11_state = h11.Connection(our_role=h11.CLIENT)41 self.timeout_flag = TimeoutFlag()42
21 unmodified lines
64 self.h11_state.send(event)65 except h11.LocalProtocolError: # pragma: no cover66 # Premature client disconnect67 pass68 await self.stream.close()68 await self.socket.close()6970 async def _send_request(self, request: Request, timeout: Timeout) -> None:71 """72 Send the request method, URL, and headers to the network.
38 unmodified lines
111 Send a single `h11` event to the network, waiting for the data to112 drain before returning.113 """114 bytes_to_send = self.h11_state.send(event)115 await self.stream.write(bytes_to_send, timeout)115 await self.socket.write(bytes_to_send, timeout)116117 async def _receive_response(118 self, timeout: Timeout119 ) -> typing.Tuple[str, int, typing.List[typing.Tuple[bytes, bytes]]]:
39 unmodified lines
159 "h11.RemoteProtocolError exception "160 + f"their_state={self.h11_state.their_state} "161 + f"error_status_hint={e.error_status_hint}"162 )163 if self.stream.is_connection_dropped():163 if self.socket.is_connection_dropped():164 raise ConnectionClosed(e)165 raise ProtocolError(e)166167 if isinstance(event, h11.Data):
2 unmodified lines
170 logger.trace(f"receive_event event={event!r}")171172 if event is h11.NEED_DATA:173 try:174 data = await self.stream.read(174 data = await self.socket.read(175 self.READ_NUM_BYTES, timeout, flag=self.timeout_flag176 )177 except OSError: # pragma: nocover178 data = b""
26 unmodified lines
205 def is_closed(self) -> bool:206 return self.h11_state.our_state in (h11.CLOSED, h11.ERROR)207208 def is_connection_dropped(self) -> bool:209 return self.stream.is_connection_dropped()209 return self.socket.is_connection_dropped()
modified:httpx/dispatch/http2.py+10 added-10 removed
23 unmodified lines
24 READ_NUM_BYTES = 40962526 def __init__(27 self,28 stream: BaseSocketStream,28 socket: BaseSocketStream,29 backend: typing.Union[str, ConcurrencyBackend] = "auto",30 on_release: typing.Callable = None,31 ):32 self.stream = stream32 self.socket = socket33 self.backend = lookup_backend(backend)34 self.on_release = on_release35 self.h2_state = h2.connection.H2Connection()36 self.events = {} # type: typing.Dict[int, typing.List[h2.events.Event]]
43 unmodified lines
80 request=request,81 )8283 async def close(self) -> None:84 await self.stream.close()84 await self.socket.close()8586 async def send_connection_init(self, timeout: Timeout) -> None:87 # Need to set these manually here instead of manipulating via88 # __setitem__() otherwise the H2Connection will emit SettingsUpdate
18 unmodified lines
107 ]108109 self.h2_state.initiate_connection()110 data_to_send = self.h2_state.data_to_send()111 await self.stream.write(data_to_send, timeout)111 await self.socket.write(data_to_send, timeout)112113 async def send_headers(self, request: Request, timeout: Timeout) -> int:114 stream_id = self.h2_state.get_next_available_stream_id()115 headers = [
11 unmodified lines
127 f"headers={headers!r}"128 )129 self.h2_state.send_headers(stream_id, headers)130 data_to_send = self.h2_state.data_to_send()131 await self.stream.write(data_to_send, timeout)131 await self.socket.write(data_to_send, timeout)132 return stream_id133134 async def send_request_data(135 self, stream_id: int, stream: typing.AsyncIterator[bytes], timeout: Timeout,
26 unmodified lines
162 else:163 chunk, data = data[:chunk_size], data[chunk_size:]164 self.h2_state.send_data(stream_id, chunk)165 data_to_send = self.h2_state.data_to_send()166 await self.stream.write(data_to_send, timeout)166 await self.socket.write(data_to_send, timeout)167168 async def end_stream(self, stream_id: int, timeout: Timeout) -> None:169 logger.trace(f"end_stream stream_id={stream_id}")170 self.h2_state.end_stream(stream_id)171 data_to_send = self.h2_state.data_to_send()172 await self.stream.write(data_to_send, timeout)172 await self.socket.write(data_to_send, timeout)173174 async def receive_response(175 self, stream_id: int, timeout: Timeout176 ) -> typing.Tuple[int, typing.List[typing.Tuple[bytes, bytes]]]:
33 unmodified lines
210211 async def receive_event(self, stream_id: int, timeout: Timeout) -> h2.events.Event:212 while not self.events[stream_id]:213 flag = self.timeout_flags[stream_id]214 data = await self.stream.read(self.READ_NUM_BYTES, timeout, flag=flag)214 data = await self.socket.read(self.READ_NUM_BYTES, timeout, flag=flag)215 events = self.h2_state.receive_data(data)216 for event in events:217 event_stream_id = getattr(event, "stream_id", 0)218 logger.trace(
19 unmodified lines
238 if event_stream_id:239 self.events[event.stream_id].append(event)240241 data_to_send = self.h2_state.data_to_send()242 await self.stream.write(data_to_send, timeout)242 await self.socket.write(data_to_send, timeout)243244 return self.events[stream_id].pop(0)245246 async def response_closed(self, stream_id: int) -> None:
8 unmodified lines
255 def is_closed(self) -> bool:256 return False257258 def is_connection_dropped(self) -> bool:259 return self.stream.is_connection_dropped()259 return self.socket.is_connection_dropped()
modified:httpx/dispatch/proxy_http.py+5 added-5 removed
173 unmodified lines
174 http_connection = connection.h11_connection175 assert http_connection is not None176 assert http_connection.h11_state.our_state == h11.SWITCHED_PROTOCOL177 on_release = http_connection.on_release178 stream = http_connection.stream178 socket = http_connection.socket179180 # If we need to start TLS again for the target server181 # we need to pull the socket stream off the internal182 # HTTP connection object and run start_tls()
6 unmodified lines
189 f"tunnel_start_tls "190 f"proxy_url={self.proxy_url!r} "191 f"origin={origin!r}"192 )193 stream = await stream.start_tls(193 socket = await socket.start_tls(194 hostname=origin.host, ssl_context=ssl_context, timeout=timeout195 )196 http_version = stream.get_http_version()196 http_version = socket.get_http_version()197 logger.trace(198 f"tunnel_tls_complete "199 f"proxy_url={self.proxy_url!r} "200 f"origin={origin!r} "
1 unmodified line
202 )203204 if http_version == "HTTP/2":205 connection.h2_connection = HTTP2Connection(206 stream, self.backend, on_release=on_release206 socket, self.backend, on_release=on_release207 )208 else:209 assert http_version == "HTTP/1.1"210 connection.h11_connection = HTTP11Connection(stream, on_release=on_release)210 connection.h11_connection = HTTP11Connection(socket, on_release=on_release)211212 def should_forward_origin(self, origin: Origin) -> bool:213 """Determines if the given origin should214 be forwarded or tunneled. If 'proxy_mode' is 'DEFAULT'
35 unmodified lines