Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read request data after request to prevent connection error #66

Merged
merged 3 commits into from
Jul 1, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/functions_framework/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

DEFAULT_SOURCE = os.path.realpath("./main.py")
DEFAULT_SIGNATURE_TYPE = "http"
MAX_CONTENT_LENGTH = 10 * 1024 * 1024


class _EventType(enum.Enum):
Expand Down Expand Up @@ -162,6 +163,16 @@ def view_func(path):
return view_func


def read_request(response):
"""
Force the framework to read the entire request before responding, to avoid
connection errors when returning prematurely.
"""

flask.request.get_data()
return response


def create_app(target=None, source=None, signature_type=None):
# Get the configured function target
target = target or os.environ.get("FUNCTION_TARGET", "")
Expand Down Expand Up @@ -218,6 +229,7 @@ def create_app(target=None, source=None, signature_type=None):
spec.loader.exec_module(source_module)

app = flask.Flask(target, template_folder=template_folder)
app.config["MAX_CONTENT_LENGTH"] = MAX_CONTENT_LENGTH

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