Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an argument to override request body size #6340

Merged
merged 5 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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`.
anesabml marked this conversation as resolved.
Show resolved Hide resolved
9 changes: 8 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: int = 1024 ** 2,
anesabml marked this conversation as resolved.
Show resolved Hide resolved
) -> "BaseRequest":
"""Clone itself with replacement some attributes.

Expand Down Expand Up @@ -247,7 +248,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 +271,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 +857,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,
Expand All @@ -860,6 +866,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 @@ -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::

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:: 4.0
anesabml marked this conversation as resolved.
Show resolved Hide resolved

*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