Scrapy, a fast high-level web crawling & scraping framework for Python.
Return partial HTTP/2 responses on timeout when fail_on_dataloss is False (#8464)
Adrian36a7e21239f4
diff --git a/scrapy/core/_http2/stream.py b/scrapy/core/_http2/stream.pyindex 6f6df1f81..022650c7c 100644--- a/scrapy/core/_http2/stream.py+++ b/scrapy/core/_http2/stream.py@@ -183,6 +183,12 @@ class Stream:else:self.close(StreamCloseReason.CANCELLED)+ @property+ def _cancel_keeps_body(self) -> bool:+ """Whether cancelling the stream, e.g. on download timeout, returns+ the response received so far as a partial response."""+ return not self._fail_on_dataloss and self._response["status"] is not None+@propertydef _log_warnsize(self) -> bool:"""Checks if we have received data which exceeds the download warnsize@@ -400,9 +406,11 @@ class Stream:def reset_stream(self, reason: StreamCloseReason = StreamCloseReason.RESET) -> None:"""Close this stream by sending a RST_FRAME to the remote peer"""# The data received so far is the body of the response built for a- # stopped download, otherwise the buffer is cleared early to avoid- # keeping data in memory for a long time- if reason is not StreamCloseReason.STOP_DOWNLOAD:+ # stopped download or a partial response, otherwise the buffer is+ # cleared early to avoid keeping data in memory for a long time+ if reason is not StreamCloseReason.STOP_DOWNLOAD and not (+ reason is StreamCloseReason.CANCELLED and self._cancel_keeps_body+ ):self._response["body"].truncate(0)self.metadata["stream_closed_local"] = True@@ -458,6 +466,9 @@ class Stream:elif reason in {StreamCloseReason.ENDED, StreamCloseReason.STOP_DOWNLOAD}:self._fire_response_deferred()+ elif reason is StreamCloseReason.CANCELLED and self._cancel_keeps_body:+ self._fire_response_deferred(flags=["dataloss"])+# Stream was abruptly ended hereelif reason is StreamCloseReason.CANCELLED:# Client has cancelled the request. Remove all the datadiff --git a/scrapy/core/downloader/handlers/http2.py b/scrapy/core/downloader/handlers/http2.pyindex 4729cfc32..88831ef72 100644--- a/scrapy/core/downloader/handlers/http2.py+++ b/scrapy/core/downloader/handlers/http2.py@@ -5,6 +5,8 @@ from time import monotonicfrom typing import TYPE_CHECKINGfrom urllib.parse import urldefrag+from twisted.python.failure import Failure+from scrapy.core._http2.agent import H2Agent, H2ConnectionPoolfrom scrapy.core.downloader._idna_patch import _install_twisted_idna_fallbacksfrom scrapy.core.downloader.contextfactory import _load_context_factory_from_settings@@ -137,5 +139,8 @@ class _ScrapyH2Agent:timeout_cl.cancel()return response+ if not isinstance(response, Failure) and "dataloss" in response.flags:+ return response+url = urldefrag(request.url)[0]raise DownloadTimeoutError(f"Getting {url} took longer than {timeout} seconds.")diff --git a/tests/test_downloader_handler_twisted_http2.py b/tests/test_downloader_handler_twisted_http2.pyindex 4b3d19349..31d6eae49 100644--- a/tests/test_downloader_handler_twisted_http2.py+++ b/tests/test_downloader_handler_twisted_http2.py@@ -50,6 +50,8 @@ pytestmark = [class H2DownloadHandlerMixin:+ handler_supports_partial_response_on_timeout = True+@propertydef download_handler_cls(self) -> type[DownloadHandlerProtocol]:# the import can fail when H2_ENABLED is False