diff --git a/CHANGES/5704.feature b/CHANGES/5704.feature new file mode 100644 index 00000000000..6f9951bc993 --- /dev/null +++ b/CHANGES/5704.feature @@ -0,0 +1 @@ +A new argument ``client_max_size`` was added to ``BaseRequest.clone`` method to allow overriding the request body size -- :user:`anesabml`. diff --git a/aiohttp/web_request.py b/aiohttp/web_request.py index 6646a82f6ca..46a2aeb05b8 100644 --- a/aiohttp/web_request.py +++ b/aiohttp/web_request.py @@ -205,6 +205,7 @@ def clone( scheme: Union[str, _SENTINEL] = sentinel, host: Union[str, _SENTINEL] = sentinel, remote: Union[str, _SENTINEL] = sentinel, + client_max_size: Union[int, _SENTINEL] = sentinel, ) -> "BaseRequest": """Clone itself with replacement some attributes. @@ -239,6 +240,8 @@ def clone( kwargs["host"] = host if remote is not sentinel: kwargs["remote"] = remote + if client_max_size is sentinel: + client_max_size = self._client_max_size return self.__class__( message, @@ -247,7 +250,7 @@ def clone( self._payload_writer, self._task, self._loop, - client_max_size=self._client_max_size, + client_max_size=client_max_size, state=self._state.copy(), **kwargs, ) @@ -270,6 +273,10 @@ def transport(self) -> Optional[asyncio.Transport]: def writer(self) -> AbstractStreamWriter: return self._payload_writer + @property + def client_max_size(self) -> int: + return self._client_max_size + @reify def rel_url(self) -> URL: return self._rel_url @@ -852,6 +859,7 @@ def clone( scheme: Union[str, _SENTINEL] = sentinel, host: Union[str, _SENTINEL] = sentinel, remote: Union[str, _SENTINEL] = sentinel, + client_max_size: Union[int, _SENTINEL] = sentinel, ) -> "Request": ret = super().clone( method=method, @@ -860,6 +868,7 @@ def clone( scheme=scheme, host=host, remote=remote, + client_max_size=client_max_size, ) new_ret = cast(Request, ret) new_ret._match_info = self._match_info diff --git a/docs/web_advanced.rst b/docs/web_advanced.rst index 480becf97c7..ab44078c3ad 100644 --- a/docs/web_advanced.rst +++ b/docs/web_advanced.rst @@ -974,9 +974,9 @@ headers too, pushing non-trusted data values. That's why *aiohttp server* should setup *forwarded* headers in custom middleware in tight conjunction with *reverse proxy configuration*. -For changing :attr:`BaseRequest.scheme` :attr:`BaseRequest.host` and -:attr:`BaseRequest.remote` the middleware might use -:meth:`BaseRequest.clone`. +For changing :attr:`BaseRequest.scheme` :attr:`BaseRequest.host` +:attr:`BaseRequest.remote` and :attr:`BaseRequest.client_max_size` +the middleware might use :meth:`BaseRequest.clone`. .. seealso:: diff --git a/docs/web_reference.rst b/docs/web_reference.rst index bc60bcabd28..417238361a8 100644 --- a/docs/web_reference.rst +++ b/docs/web_reference.rst @@ -149,6 +149,23 @@ and :ref:`aiohttp-web-signals` handlers. .. seealso:: :ref:`aiohttp-web-forwarded-support` + .. attribute:: client_max_size + + The maximum size of the request body. + + The value could be overridden by :meth:`~BaseRequest.clone`. + + Read-only :class:`int` property. + + .. versionchanged:: 3.8 + + *Forwarded* and *X-Forwarded-Proto* are not used anymore. + + Call ``.clone(client_max_size=new_size)`` for setting up the value + explicitly. + + .. seealso:: :ref:`aiohttp-web-forwarded-support` + .. attribute:: path_qs The URL including PATH_INFO and the query string. e.g., diff --git a/tests/test_web_request.py b/tests/test_web_request.py index b4d6514d16a..60c34319ddc 100644 --- a/tests/test_web_request.py +++ b/tests/test_web_request.py @@ -513,6 +513,12 @@ def test_clone_client_max_size() -> None: assert req2._client_max_size == 1024 +def test_clone_override_client_max_size() -> None: + req = make_mocked_request("GET", "/path", client_max_size=1024) + req2 = req.clone(client_max_size=2048) + assert req2.client_max_size == 2048 + + def test_clone_method() -> None: req = make_mocked_request("GET", "/path") req2 = req.clone(method="POST")