Home / Class/ ContentSizeLimitMiddleware Class — fastapi Architecture

ContentSizeLimitMiddleware Class — fastapi Architecture

Architecture documentation for the ContentSizeLimitMiddleware class in test_custom_middleware_exception.py from the fastapi codebase.

Entity Profile

Dependency Diagram

graph TD
  74723938_6a55_2853_752f_6576aa5c1d17["ContentSizeLimitMiddleware"]
  701a2e1b_486c_9b9e_27dc_b223ac1a3925["test_custom_middleware_exception.py"]
  74723938_6a55_2853_752f_6576aa5c1d17 -->|defined in| 701a2e1b_486c_9b9e_27dc_b223ac1a3925
  f95d764f_6515_a1a7_8b3d_0fa11a13d499["__init__()"]
  74723938_6a55_2853_752f_6576aa5c1d17 -->|method| f95d764f_6515_a1a7_8b3d_0fa11a13d499
  d268a417_9bb1_869f_1b2d_baf49422f22c["receive_wrapper()"]
  74723938_6a55_2853_752f_6576aa5c1d17 -->|method| d268a417_9bb1_869f_1b2d_baf49422f22c
  4014ea23_5fbb_eb4b_c698_f3833bb20f2e["__call__()"]
  74723938_6a55_2853_752f_6576aa5c1d17 -->|method| 4014ea23_5fbb_eb4b_c698_f3833bb20f2e

Relationship Graph

Source Code

tests/test_custom_middleware_exception.py lines 13–54

class ContentSizeLimitMiddleware:
    """Content size limiting middleware for ASGI applications
    Args:
      app (ASGI application): ASGI application
      max_content_size (optional): the maximum content size allowed in bytes, None for no limit
    """

    def __init__(self, app: APIRouter, max_content_size: Optional[int] = None):
        self.app = app
        self.max_content_size = max_content_size

    def receive_wrapper(self, receive):
        received = 0

        async def inner():
            nonlocal received
            message = await receive()
            if message["type"] != "http.request":
                return message  # pragma: no cover

            body_len = len(message.get("body", b""))
            received += body_len
            if received > self.max_content_size:
                raise HTTPException(
                    422,
                    detail={
                        "name": "ContentSizeLimitExceeded",
                        "code": 999,
                        "message": "File limit exceeded",
                    },
                )
            return message

        return inner

    async def __call__(self, scope, receive, send):
        if scope["type"] != "http" or self.max_content_size is None:
            await self.app(scope, receive, send)
            return

        wrapper = self.receive_wrapper(receive)
        await self.app(scope, wrapper, send)

Domain

Frequently Asked Questions

What is the ContentSizeLimitMiddleware class?
ContentSizeLimitMiddleware is a class in the fastapi codebase, defined in tests/test_custom_middleware_exception.py.
Where is ContentSizeLimitMiddleware defined?
ContentSizeLimitMiddleware is defined in tests/test_custom_middleware_exception.py at line 13.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free