Implements `.HTTPConnection.finish`.
(self)
| 505 | return future |
| 506 | |
| 507 | def finish(self) -> None: |
| 508 | """Implements `.HTTPConnection.finish`.""" |
| 509 | if ( |
| 510 | self._expected_content_remaining is not None |
| 511 | and self._expected_content_remaining != 0 |
| 512 | and not self.stream.closed() |
| 513 | ): |
| 514 | self.stream.close() |
| 515 | raise httputil.HTTPOutputError( |
| 516 | "Tried to write %d bytes less than Content-Length" |
| 517 | % self._expected_content_remaining |
| 518 | ) |
| 519 | if self._chunking_output: |
| 520 | if not self.stream.closed(): |
| 521 | self._pending_write = self.stream.write(b"0\r\n\r\n") |
| 522 | self._pending_write.add_done_callback(self._on_write_complete) |
| 523 | self._write_finished = True |
| 524 | # If the app finished the request while we're still reading, |
| 525 | # divert any remaining data away from the delegate and |
| 526 | # close the connection when we're done sending our response. |
| 527 | # Closing the connection is the only way to avoid reading the |
| 528 | # whole input body. |
| 529 | if not self._read_finished: |
| 530 | self._disconnect_on_finish = True |
| 531 | # No more data is coming, so instruct TCP to send any remaining |
| 532 | # data immediately instead of waiting for a full packet or ack. |
| 533 | self.stream.set_nodelay(True) |
| 534 | if self._pending_write is None: |
| 535 | self._finish_request(None) |
| 536 | else: |
| 537 | future_add_done_callback(self._pending_write, self._finish_request) |
| 538 | |
| 539 | def _on_write_complete(self, future: "Future[None]") -> None: |
| 540 | exc = future.exception() |
no test coverage detected