| 1 |
from typing import Literal, Optional, overload, Union |
| 2 |
|
| 3 |
import trio |
| 4 |
from quart.wrappers.request import Body, Request |
| 5 |
from werkzeug.exceptions import RequestEntityTooLarge, RequestTimeout |
| 6 |
|
| 7 |
from ..datastructures import TrioQueue |
| 8 |
from ..formparser import TrioFormDataParser |
| 9 |
|
| 10 |
|
| 11 |
class EventWrapper: |
| 12 |
def __init__(self) -> None: |
| 13 |
self._event = trio.Event() |
| 14 |
|
| 15 |
def clear(self) -> None: |
| 16 |
self._event = trio.Event() |
| 17 |
|
| 18 |
async def wait(self) -> None: |
| 19 |
await self._event.wait() |
| 20 |
|
| 21 |
def is_set(self) -> bool: |
| 22 |
return self._event.is_set() |
| 23 |
|
| 24 |
def set(self) -> None: |
| 25 |
self._event.set() |
| 26 |
|
| 27 |
|
| 28 |
class TrioBody(Body): |
| 29 |
def __init__( |
| 30 |
self, expected_content_length: Optional[int], max_content_length: Optional[int] |
| 31 |
) -> None: |
| 32 |
self._data = None |
| 33 |
self._complete = EventWrapper() # type: ignore |
| 34 |
self._queue = TrioQueue[bytes]() # type: ignore |
| 35 |
self._max_content_length = max_content_length |
| 36 |
# Exceptions must be raised within application (not ASGI) |
| 37 |
# calls, this is achieved by having the ASGI methods set this |
| 38 |
# to an exception on error. |
| 39 |
self._must_raise: Optional[Exception] = None |
| 40 |
if ( |
| 41 |
expected_content_length is not None |
| 42 |
and max_content_length is not None |
| 43 |
and expected_content_length > max_content_length |
| 44 |
): |
| 45 |
self._must_raise = RequestEntityTooLarge() |
| 46 |
|
| 47 |
|
| 48 |
class TrioRequest(Request): |
| 49 |
body_class = TrioBody |
| 50 |
form_data_parser_class = TrioFormDataParser |
| 51 |
lock_class = trio.Lock # type: ignore |
| 52 |
|
| 53 |
@overload |
| 54 |
async def get_data( |
| 55 |
self, cache: bool, as_text: Literal[False], parse_form_data: bool |
| 56 |
) -> bytes: ... |
| 57 |
|
| 58 |
@overload |
| 59 |
async def get_data(self, cache: bool, as_text: Literal[True], parse_form_data: bool) -> str: ... |
| 60 |
|
| 61 |
@overload |
| 62 |
async def get_data( |
| 63 |
self, cache: bool = True, as_text: bool = False, parse_form_data: bool = False |
| 64 |
) -> Union[str, bytes]: ... |
| 65 |
|
| 66 |
async def get_data( |
| 67 |
self, cache: bool = True, as_text: bool = False, parse_form_data: bool = False |
| 68 |
) -> Union[str, bytes]: |
| 69 |
if parse_form_data: |
| 70 |
await self._load_form_data() |
| 71 |
|
| 72 |
timeout = float("inf") if self.body_timeout is None else self.body_timeout |
| 73 |
with trio.move_on_after(timeout) as cancel_scope: |
| 74 |
raw_data = await self.body |
| 75 |
if cancel_scope.cancelled_caught: |
| 76 |
raise RequestTimeout() |
| 77 |
|
| 78 |
if not cache: |
| 79 |
self.body.clear() |
| 80 |
|
| 81 |
if as_text: |
| 82 |
return raw_data.decode() |
| 83 |
else: |
| 84 |
return raw_data |
| 85 |
|
| 86 |
async def _load_form_data(self) -> None: |
| 87 |
async with self._parsing_lock: |
| 88 |
if self._form is None: |
| 89 |
parser = self.make_form_data_parser() |
| 90 |
|
| 91 |
timeout = float("inf") if self.body_timeout is None else self.body_timeout |
| 92 |
with trio.move_on_after(timeout) as cancel_scope: |
| 93 |
self._form, self._files = await parser.parse( |
| 94 |
self.body, |
| 95 |
self.mimetype, |
| 96 |
self.content_length, |
| 97 |
self.mimetype_params, |
| 98 |
) |
| 99 |
if cancel_scope.cancelled_caught: |
| 100 |
raise RequestTimeout() |