Skip to content

Commit

Permalink
Add an argument to override request body size (#6340)
Browse files Browse the repository at this point in the history
  • Loading branch information
anesabml committed Nov 23, 2021
1 parent a0a9b3d commit f6334e2
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES/5704.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
A new argument ``client_max_size`` was added to ``BaseRequest.clone`` method to allow overriding the request body size -- :user:`anesabml`.
11 changes: 10 additions & 1 deletion aiohttp/web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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,
)
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions docs/web_advanced.rst
Original file line number Diff line number Diff line change
Expand Up @@ -985,9 +985,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::

Expand Down
17 changes: 17 additions & 0 deletions docs/web_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.,
Expand Down
6 changes: 6 additions & 0 deletions tests/test_web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit f6334e2

Please sign in to comment.