Skip to content

Commit 0848abf

Browse files
committed
Cherry-pick #7559 large-torrent-creation from main
2 parents e21f9cf + 6c87f8f commit 0848abf

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/tribler/core/components/restapi/rest/rest_endpoint.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@
2020
HTTP_BAD_REQUEST = 400
2121
HTTP_UNAUTHORIZED = 401
2222
HTTP_NOT_FOUND = 404
23+
HTTP_REQUEST_ENTITY_TOO_LARGE = 413
2324
HTTP_INTERNAL_SERVER_ERROR = 500
2425

26+
MAX_REQUEST_SIZE = 16 * 1024 ** 2 # 16 MB
27+
2528

2629
class RESTEndpoint:
2730

2831
def __init__(self, middlewares=()):
2932
self._logger = logging.getLogger(self.__class__.__name__)
30-
self.app = web.Application(middlewares=middlewares, client_max_size=2 * 1024 ** 2)
33+
self.app = web.Application(middlewares=middlewares, client_max_size=MAX_REQUEST_SIZE)
3134
self.endpoints: Dict[str, RESTEndpoint] = {}
3235
self.async_group = AsyncGroup()
3336
self.setup_routes()

src/tribler/core/components/restapi/rest/rest_manager.py

+8-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from typing import Optional
66

77
from aiohttp import web
8-
from aiohttp.web_exceptions import HTTPNotFound
8+
from aiohttp.web_exceptions import HTTPNotFound, HTTPRequestEntityTooLarge
99
from aiohttp_apispec import AiohttpApiSpec
1010
from apispec.core import VALID_METHODS_OPENAPI_V2
1111

@@ -14,6 +14,8 @@
1414
HTTP_INTERNAL_SERVER_ERROR,
1515
HTTP_NOT_FOUND,
1616
HTTP_UNAUTHORIZED,
17+
HTTP_REQUEST_ENTITY_TOO_LARGE,
18+
MAX_REQUEST_SIZE,
1719
RESTResponse,
1820
)
1921
from tribler.core.components.restapi.rest.root_endpoint import RootEndpoint
@@ -63,6 +65,11 @@ async def error_middleware(request, handler):
6365
'handled': True,
6466
'message': f'Could not find {request.path}'
6567
}}, status=HTTP_NOT_FOUND)
68+
except HTTPRequestEntityTooLarge:
69+
return RESTResponse({'error': {
70+
'handled': True,
71+
'message': f'Request size is larger than {MAX_REQUEST_SIZE} bytes'
72+
}}, status=HTTP_REQUEST_ENTITY_TOO_LARGE)
6673
except Exception as e:
6774
logger.exception(e)
6875
full_exception = traceback.format_exc()

src/tribler/core/components/restapi/rest/tests/test_create_torrent_endpoint.py

+28-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import random
2+
import string
13
from unittest.mock import Mock
24

35
import pytest
4-
from aiohttp.web_app import Application
56
from ipv8.util import succeed
67

78
from tribler.core.components.libtorrent.restapi.create_torrent_endpoint import CreateTorrentEndpoint
89
from tribler.core.components.libtorrent.settings import DownloadDefaultsSettings
910
from tribler.core.components.restapi.rest.base_api_test import do_request
10-
from tribler.core.components.restapi.rest.rest_manager import error_middleware
11+
from tribler.core.components.restapi.rest.rest_endpoint import HTTP_REQUEST_ENTITY_TOO_LARGE, MAX_REQUEST_SIZE
1112
from tribler.core.tests.tools.common import TESTS_DATA_DIR
1213

1314

@@ -77,6 +78,31 @@ def fake_create_torrent_file(*_, **__):
7778
assert expected_response == error_response
7879

7980

81+
async def test_create_torrent_of_large_size(rest_api):
82+
"""
83+
Testing whether the API returns a formatted 413 error if request size is above set client size.
84+
"""
85+
86+
post_data = {
87+
"description": ''.join(random.choice(string.ascii_letters) for _ in range(MAX_REQUEST_SIZE))
88+
}
89+
90+
error_response = await do_request(
91+
rest_api, 'createtorrent',
92+
expected_code=HTTP_REQUEST_ENTITY_TOO_LARGE,
93+
request_type='POST',
94+
post_data=post_data
95+
)
96+
97+
expected_response = {
98+
"error": {
99+
"handled": True,
100+
"message": f"Request size is larger than {MAX_REQUEST_SIZE} bytes"
101+
}
102+
}
103+
assert expected_response == error_response
104+
105+
80106
async def test_create_torrent_missing_files_parameter(rest_api):
81107
expected_json = {"error": "files parameter missing"}
82108
await do_request(rest_api, 'createtorrent', expected_code=400, expected_json=expected_json, request_type='POST')

0 commit comments

Comments
 (0)