From e4f23a1c91f623f4990d43f9c32856294289899f Mon Sep 17 00:00:00 2001 From: Anes Abismail Date: Tue, 23 Nov 2021 18:51:08 +0100 Subject: [PATCH 1/5] Add an argument to override request body size --- CHANGES/5704.feature | 1 + aiohttp/web_request.py | 11 ++++++++++- docs/web_advanced.rst | 6 +++--- docs/web_reference.rst | 17 +++++++++++++++++ tests/test_web_request.py | 6 ++++++ 5 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 CHANGES/5704.feature diff --git a/CHANGES/5704.feature b/CHANGES/5704.feature new file mode 100644 index 00000000000..51d5d0ee1e6 --- /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..759fe2eb633 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: int = 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: int = 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..9ca36d72aac 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:: 4.0 + + *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") From 2b6a22f9032d23a4a994be434494796d7a08b38a Mon Sep 17 00:00:00 2001 From: Anes Abismail Date: Tue, 23 Nov 2021 19:38:37 +0100 Subject: [PATCH 2/5] Fix mypy issue --- aiohttp/web_request.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/aiohttp/web_request.py b/aiohttp/web_request.py index 759fe2eb633..30b2e8d3fc4 100644 --- a/aiohttp/web_request.py +++ b/aiohttp/web_request.py @@ -205,7 +205,7 @@ def clone( scheme: Union[str, _SENTINEL] = sentinel, host: Union[str, _SENTINEL] = sentinel, remote: Union[str, _SENTINEL] = sentinel, - client_max_size: int = sentinel, + client_max_size: int = 1024 ** 2, ) -> "BaseRequest": """Clone itself with replacement some attributes. @@ -240,8 +240,6 @@ 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, From 1f847d608fb080e469e93c1b9c202e7a93b14ca4 Mon Sep 17 00:00:00 2001 From: Anes Abismail Date: Tue, 23 Nov 2021 22:56:25 +0100 Subject: [PATCH 3/5] Inline code in the changes file --- CHANGES/5704.feature | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGES/5704.feature b/CHANGES/5704.feature index 51d5d0ee1e6..6f9951bc993 100644 --- a/CHANGES/5704.feature +++ b/CHANGES/5704.feature @@ -1 +1 @@ -A new argument `client_max_size` was added to `BaseRequest.clone` method to allow overriding the request body size -- :user:`anesabml`. +A new argument ``client_max_size`` was added to ``BaseRequest.clone`` method to allow overriding the request body size -- :user:`anesabml`. From e521c8a2c7e76e6b3cfd198113942b9e3c8bbe09 Mon Sep 17 00:00:00 2001 From: Anes Abismail Date: Tue, 23 Nov 2021 23:01:02 +0100 Subject: [PATCH 4/5] Use Union instead of int --- aiohttp/web_request.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/aiohttp/web_request.py b/aiohttp/web_request.py index 30b2e8d3fc4..46a2aeb05b8 100644 --- a/aiohttp/web_request.py +++ b/aiohttp/web_request.py @@ -205,7 +205,7 @@ def clone( scheme: Union[str, _SENTINEL] = sentinel, host: Union[str, _SENTINEL] = sentinel, remote: Union[str, _SENTINEL] = sentinel, - client_max_size: int = 1024 ** 2, + client_max_size: Union[int, _SENTINEL] = sentinel, ) -> "BaseRequest": """Clone itself with replacement some attributes. @@ -240,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, @@ -857,7 +859,7 @@ def clone( scheme: Union[str, _SENTINEL] = sentinel, host: Union[str, _SENTINEL] = sentinel, remote: Union[str, _SENTINEL] = sentinel, - client_max_size: int = sentinel, + client_max_size: Union[int, _SENTINEL] = sentinel, ) -> "Request": ret = super().clone( method=method, From ad3611cd9f7a288f045208c81e69a43f55e786c6 Mon Sep 17 00:00:00 2001 From: Anes Abismail Date: Tue, 23 Nov 2021 23:03:31 +0100 Subject: [PATCH 5/5] Change versionchanged to 3.8 --- docs/web_reference.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/web_reference.rst b/docs/web_reference.rst index 9ca36d72aac..417238361a8 100644 --- a/docs/web_reference.rst +++ b/docs/web_reference.rst @@ -157,7 +157,7 @@ and :ref:`aiohttp-web-signals` handlers. Read-only :class:`int` property. - .. versionchanged:: 4.0 + .. versionchanged:: 3.8 *Forwarded* and *X-Forwarded-Proto* are not used anymore.