Skip to content

Commit f5ebd98

Browse files
asrinivadi
andauthored
Read request data after request to prevent connection error (#66)
* Read request data after request * Update src/functions_framework/__init__.py * Update src/functions_framework/__init__.py Co-authored-by: Dustin Ingram <[email protected]>
1 parent 8f3fe35 commit f5ebd98

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/functions_framework/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939

4040
DEFAULT_SOURCE = os.path.realpath("./main.py")
4141
DEFAULT_SIGNATURE_TYPE = "http"
42+
MAX_CONTENT_LENGTH = 10 * 1024 * 1024
4243

4344

4445
class _EventType(enum.Enum):
@@ -162,6 +163,16 @@ def view_func(path):
162163
return view_func
163164

164165

166+
def read_request(response):
167+
"""
168+
Force the framework to read the entire request before responding, to avoid
169+
connection errors when returning prematurely.
170+
"""
171+
172+
flask.request.get_data()
173+
return response
174+
175+
165176
def create_app(target=None, source=None, signature_type=None):
166177
# Get the configured function target
167178
target = target or os.environ.get("FUNCTION_TARGET", "")
@@ -218,6 +229,7 @@ def create_app(target=None, source=None, signature_type=None):
218229
spec.loader.exec_module(source_module)
219230

220231
app = flask.Flask(target, template_folder=template_folder)
232+
app.config["MAX_CONTENT_LENGTH"] = MAX_CONTENT_LENGTH
221233

222234
# Extract the target function from the source file
223235
try:
@@ -250,6 +262,7 @@ def create_app(target=None, source=None, signature_type=None):
250262
app.url_map.add(werkzeug.routing.Rule("/<path:path>", endpoint="run"))
251263
app.view_functions["run"] = _http_view_func_wrapper(function, flask.request)
252264
app.view_functions["error"] = lambda: flask.abort(404, description="Not Found")
265+
app.after_request(read_request)
253266
elif signature_type == "event" or signature_type == "cloudevent":
254267
app.url_map.add(
255268
werkzeug.routing.Rule(

0 commit comments

Comments
 (0)