@@ -52,13 +52,96 @@ class Request(RequestBase):
5252 #: something similar.
5353 routing_exception : HTTPException | None = None
5454
55+ _max_content_length : int | None = None
56+ _max_form_memory_size : int | None = None
57+ _max_form_parts : int | None = None
58+
5559 @property
56- def max_content_length (self ) -> int | None : # type: ignore[override]
57- """Read-only view of the ``MAX_CONTENT_LENGTH`` config key."""
58- if current_app :
59- return current_app .config ["MAX_CONTENT_LENGTH" ] # type: ignore[no-any-return]
60- else :
61- return None
60+ def max_content_length (self ) -> int | None :
61+ """The maximum number of bytes that will be read during this request. If
62+ this limit is exceeded, a 413 :exc:`~werkzeug.exceptions.RequestEntityTooLarge`
63+ error is raised. If it is set to ``None``, no limit is enforced at the
64+ Flask application level. However, if it is ``None`` and the request has
65+ no ``Content-Length`` header and the WSGI server does not indicate that
66+ it terminates the stream, then no data is read to avoid an infinite
67+ stream.
68+
69+ Each request defaults to the :data:`MAX_CONTENT_LENGTH` config, which
70+ defaults to ``None``. It can be set on a specific ``request`` to apply
71+ the limit to that specific view. This should be set appropriately based
72+ on an application's or view's specific needs.
73+
74+ .. versionchanged:: 3.1
75+ This can be set per-request.
76+
77+ .. versionchanged:: 0.6
78+ This is configurable through Flask config.
79+ """
80+ if self ._max_content_length is not None :
81+ return self ._max_content_length
82+
83+ if not current_app :
84+ return super ().max_content_length
85+
86+ return current_app .config ["MAX_CONTENT_LENGTH" ] # type: ignore[no-any-return]
87+
88+ @max_content_length .setter
89+ def max_content_length (self , value : int | None ) -> None :
90+ self ._max_content_length = value
91+
92+ @property
93+ def max_form_memory_size (self ) -> int | None :
94+ """The maximum size in bytes any non-file form field may be in a
95+ ``multipart/form-data`` body. If this limit is exceeded, a 413
96+ :exc:`~werkzeug.exceptions.RequestEntityTooLarge` error is raised. If it
97+ is set to ``None``, no limit is enforced at the Flask application level.
98+
99+ Each request defaults to the :data:`MAX_FORM_MEMORY_SIZE` config, which
100+ defaults to ``500_000``. It can be set on a specific ``request`` to
101+ apply the limit to that specific view. This should be set appropriately
102+ based on an application's or view's specific needs.
103+
104+ .. versionchanged:: 3.1
105+ This is configurable through Flask config.
106+ """
107+ if self ._max_form_memory_size is not None :
108+ return self ._max_form_memory_size
109+
110+ if not current_app :
111+ return super ().max_form_memory_size
112+
113+ return current_app .config ["MAX_FORM_MEMORY_SIZE" ] # type: ignore[no-any-return]
114+
115+ @max_form_memory_size .setter
116+ def max_form_memory_size (self , value : int | None ) -> None :
117+ self ._max_form_memory_size = value
118+
119+ @property # type: ignore[override]
120+ def max_form_parts (self ) -> int | None :
121+ """The maximum number of fields that may be present in a
122+ ``multipart/form-data`` body. If this limit is exceeded, a 413
123+ :exc:`~werkzeug.exceptions.RequestEntityTooLarge` error is raised. If it
124+ is set to ``None``, no limit is enforced at the Flask application level.
125+
126+ Each request defaults to the :data:`MAX_FORM_PARTS` config, which
127+ defaults to ``1_000``. It can be set on a specific ``request`` to apply
128+ the limit to that specific view. This should be set appropriately based
129+ on an application's or view's specific needs.
130+
131+ .. versionchanged:: 3.1
132+ This is configurable through Flask config.
133+ """
134+ if self ._max_form_parts is not None :
135+ return self ._max_form_parts
136+
137+ if not current_app :
138+ return super ().max_form_parts
139+
140+ return current_app .config ["MAX_FORM_PARTS" ] # type: ignore[no-any-return]
141+
142+ @max_form_parts .setter
143+ def max_form_parts (self , value : int | None ) -> None :
144+ self ._max_form_parts = value
62145
63146 @property
64147 def endpoint (self ) -> str | None :
0 commit comments