Skip to content

Commit

Permalink
Cherry-pick #7559 large-torrent-creation from main
Browse files Browse the repository at this point in the history
  • Loading branch information
kozlovsky committed Aug 14, 2023
2 parents e21f9cf + 6c87f8f commit 0848abf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/tribler/core/components/restapi/rest/rest_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@
HTTP_BAD_REQUEST = 400
HTTP_UNAUTHORIZED = 401
HTTP_NOT_FOUND = 404
HTTP_REQUEST_ENTITY_TOO_LARGE = 413
HTTP_INTERNAL_SERVER_ERROR = 500

MAX_REQUEST_SIZE = 16 * 1024 ** 2 # 16 MB


class RESTEndpoint:

def __init__(self, middlewares=()):
self._logger = logging.getLogger(self.__class__.__name__)
self.app = web.Application(middlewares=middlewares, client_max_size=2 * 1024 ** 2)
self.app = web.Application(middlewares=middlewares, client_max_size=MAX_REQUEST_SIZE)
self.endpoints: Dict[str, RESTEndpoint] = {}
self.async_group = AsyncGroup()
self.setup_routes()
Expand Down
9 changes: 8 additions & 1 deletion src/tribler/core/components/restapi/rest/rest_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import Optional

from aiohttp import web
from aiohttp.web_exceptions import HTTPNotFound
from aiohttp.web_exceptions import HTTPNotFound, HTTPRequestEntityTooLarge
from aiohttp_apispec import AiohttpApiSpec
from apispec.core import VALID_METHODS_OPENAPI_V2

Expand All @@ -14,6 +14,8 @@
HTTP_INTERNAL_SERVER_ERROR,
HTTP_NOT_FOUND,
HTTP_UNAUTHORIZED,
HTTP_REQUEST_ENTITY_TOO_LARGE,
MAX_REQUEST_SIZE,
RESTResponse,
)
from tribler.core.components.restapi.rest.root_endpoint import RootEndpoint
Expand Down Expand Up @@ -63,6 +65,11 @@ async def error_middleware(request, handler):
'handled': True,
'message': f'Could not find {request.path}'
}}, status=HTTP_NOT_FOUND)
except HTTPRequestEntityTooLarge:
return RESTResponse({'error': {
'handled': True,
'message': f'Request size is larger than {MAX_REQUEST_SIZE} bytes'
}}, status=HTTP_REQUEST_ENTITY_TOO_LARGE)
except Exception as e:
logger.exception(e)
full_exception = traceback.format_exc()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import random
import string
from unittest.mock import Mock

import pytest
from aiohttp.web_app import Application
from ipv8.util import succeed

from tribler.core.components.libtorrent.restapi.create_torrent_endpoint import CreateTorrentEndpoint
from tribler.core.components.libtorrent.settings import DownloadDefaultsSettings
from tribler.core.components.restapi.rest.base_api_test import do_request
from tribler.core.components.restapi.rest.rest_manager import error_middleware
from tribler.core.components.restapi.rest.rest_endpoint import HTTP_REQUEST_ENTITY_TOO_LARGE, MAX_REQUEST_SIZE
from tribler.core.tests.tools.common import TESTS_DATA_DIR


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


async def test_create_torrent_of_large_size(rest_api):
"""
Testing whether the API returns a formatted 413 error if request size is above set client size.
"""

post_data = {
"description": ''.join(random.choice(string.ascii_letters) for _ in range(MAX_REQUEST_SIZE))
}

error_response = await do_request(
rest_api, 'createtorrent',
expected_code=HTTP_REQUEST_ENTITY_TOO_LARGE,
request_type='POST',
post_data=post_data
)

expected_response = {
"error": {
"handled": True,
"message": f"Request size is larger than {MAX_REQUEST_SIZE} bytes"
}
}
assert expected_response == error_response


async def test_create_torrent_missing_files_parameter(rest_api):
expected_json = {"error": "files parameter missing"}
await do_request(rest_api, 'createtorrent', expected_code=400, expected_json=expected_json, request_type='POST')

0 comments on commit 0848abf

Please sign in to comment.