Gracefully end_stream early on no-body requests. (#682)
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 0ee0005 · Browse files · 1 file·+20 added−6 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+20 added−6 removed
- other:+20 added−6 removed
Provenance
- Commit
f3b7999- Tree
87753a1- Author
Tom Christie <tom@tomchristie.com>- Committer
GitHub <noreply@github.com>- Parent
0ee0005
1 file changed+20 added−6 removed
modified:httpx/dispatch/http2.py+20 added-6 removed
164 unmodified lines165 async def send_headers(166 self,167 stream_id: int,168 headers: typing.List[typing.Tuple[bytes, bytes]],169+ end_stream: bool,170 timeout: Timeout,171 ) -> None:171- self.state.send_headers(stream_id, headers)172+ self.state.send_headers(stream_id, headers, end_stream=end_stream)173 self.state.increment_flow_control_window(2 ** 24, stream_id=stream_id)174 data_to_send = self.state.data_to_send()175 await self.socket.write(data_to_send, timeout)176 28 unmodified lines205 self.connection = connection206 207 async def send(self, request: Request, timeout: Timeout) -> Response:208 # Send the request.208- await self.send_headers(request, timeout)209- await self.send_body(request, timeout)209+ has_body = (210+ "Content-Length" in request.headers211+ or "Transfer-Encoding" in request.headers212+ )213 214+ await self.send_headers(request, has_body, timeout)215+ if has_body:216+ await self.send_body(request, timeout)217+218 # Receive the response.219 status_code, headers = await self.receive_response(timeout)220 stream = AsyncIteratorStream(221 aiterator=self.body_iter(timeout), close_func=self.close6 unmodified lines228 stream=stream,229 request=request,230 )231 225- async def send_headers(self, request: Request, timeout: Timeout) -> None:232+ async def send_headers(233+ self, request: Request, has_body: bool, timeout: Timeout234+ ) -> None:235 headers = [236 (b":method", request.method.encode("ascii")),237 (b":authority", request.url.authority.encode("ascii")),238 (b":scheme", request.url.scheme.encode("ascii")),239 (b":path", request.url.full_path.encode("ascii")),231- ] + [(k, v) for k, v in request.headers.raw if k != b"host"]240+ ] + [241+ (k, v)242+ for k, v in request.headers.raw243+ if k not in (b"host", b"transfer-encoding")244+ ]245+ end_stream = not has_body246 247 logger.trace(248 f"send_headers "249 f"stream_id={self.stream_id} "250 f"method={request.method!r} "251 f"target={request.url.full_path!r} "252 f"headers={headers!r}"253 )240- await self.connection.send_headers(self.stream_id, headers, timeout)254+ await self.connection.send_headers(self.stream_id, headers, end_stream, timeout)255 256 async def send_body(self, request: Request, timeout: Timeout) -> None:257 logger.trace(f"send_body stream_id={self.stream_id}")258 async for data in request.stream:45 unmodified lines