Based on the reason sent we will handle each case.
(
self,
reason: StreamCloseReason,
errors: Sequence[BaseException] | None = None,
from_protocol: bool = False,
)
| 395 | self.close(reason) |
| 396 | |
| 397 | def close( |
| 398 | self, |
| 399 | reason: StreamCloseReason, |
| 400 | errors: Sequence[BaseException] | None = None, |
| 401 | from_protocol: bool = False, |
| 402 | ) -> None: |
| 403 | """Based on the reason sent we will handle each case.""" |
| 404 | if self.metadata["stream_closed_server"]: |
| 405 | raise StreamClosedError(self.stream_id) |
| 406 | |
| 407 | if not isinstance(reason, StreamCloseReason): |
| 408 | raise TypeError( |
| 409 | f"Expected StreamCloseReason, received {reason.__class__.__qualname__}" |
| 410 | ) |
| 411 | |
| 412 | # Have default value of errors as an empty list as |
| 413 | # some cases can add a list of exceptions |
| 414 | errors = errors or () |
| 415 | |
| 416 | if not from_protocol: |
| 417 | self._protocol.pop_stream(self.stream_id) |
| 418 | |
| 419 | self.metadata["stream_closed_server"] = True |
| 420 | |
| 421 | # We do not check for Content-Length or Transfer-Encoding in response headers |
| 422 | # and add `partial` flag as in HTTP/1.1 as 'A request or response that includes |
| 423 | # a payload body can include a content-length header field' (RFC 7540 - Section 8.1.2.6) |
| 424 | |
| 425 | # NOTE: Order of handling the events is important here |
| 426 | # As we immediately cancel the request when maxsize is exceeded while |
| 427 | # receiving DATA_FRAME's when we have received the headers (not |
| 428 | # having Content-Length) |
| 429 | if reason in { |
| 430 | StreamCloseReason.MAXSIZE_EXCEEDED, |
| 431 | StreamCloseReason.MAXSIZE_EXCEEDED_ACTUAL, |
| 432 | }: |
| 433 | expected_size = int( |
| 434 | self._response["headers"].get( |
| 435 | b"Content-Length", self._response["flow_controlled_size"] |
| 436 | ) |
| 437 | ) |
| 438 | error_msg = get_maxsize_msg( |
| 439 | expected_size, |
| 440 | self._download_maxsize, |
| 441 | self._request, |
| 442 | expected=reason == StreamCloseReason.MAXSIZE_EXCEEDED, |
| 443 | ) |
| 444 | logger.error(error_msg) |
| 445 | self._deferred_response.errback(DownloadCancelledError(error_msg)) |
| 446 | |
| 447 | elif reason is StreamCloseReason.ENDED: |
| 448 | self._fire_response_deferred() |
| 449 | |
| 450 | # Stream was abruptly ended here |
| 451 | elif reason is StreamCloseReason.CANCELLED: |
| 452 | # Client has cancelled the request. Remove all the data |
| 453 | # received and fire the response deferred with no flags set |
| 454 |
no test coverage detected