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

flask doesn't return http error 413 if file too large #3874

Closed
danieltuzes opened this issue Jan 21, 2021 · 5 comments
Closed

flask doesn't return http error 413 if file too large #3874

danieltuzes opened this issue Jan 21, 2021 · 5 comments

Comments

@danieltuzes
Copy link

situation overview

I set up a file upload application and in case the user gives a too large file, I want to send a message that the file is too large. On my own PC, everything works fine, but in my corporate environment, flask doesn't return the message I want.

Example code

 """app.py"""

from flask import Flask, request
from werkzeug.exceptions import RequestEntityTooLarge

app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 1 * 1024 * 1024  # 1 MB


@app.route('/upload')
def upload():
    """Gives a form to upload the file."""

    return '''<!doctype html>
<body>Upload files
    <form method="POST" action="/submit_script" enctype="multipart/form-data">
        <input type="file" name="file" id="content" class="form-control-file">
        <input type="submit" value="Submit" class="form-control-file">
    </form>
</body>

</html>'''


@app.route('/submit_script', methods=['POST'])
def submit():
    """Saves the uploaded file and sends confirmation."""
    message = ""
    try:
        uploaded_file = request.files['file']
        uploaded_file.save(uploaded_file.filename)
    except RequestEntityTooLarge as my_exception:
        message = str(my_exception)
        print(message)    # this goes to the cmd line

    return "I could send a template with the result." + message 


@app.errorhandler(413)
def request_entity_too_large(error):
    """What happens if a file is too large."""

    return str(error) + " I could have sent a HTML template too.", 413

Expected Behavior

I expect flask to return "I could send a template with the result." or without werkzeug, to return str(error) + " I could have sent an HTML template too.", 413 But none of them happens.

Actual Behavior

My browser gets an ERR_CONNECTION_ABORTED message.

This is what I get on the terminal:

(flask) C:\Users\uname\source\repos\flask_non_we>flask run
 * Environment: production
   WARNING: This is a development server. Do not use it in a production deployment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [21/Jan/2021 23:21:14] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [21/Jan/2021 23:21:16] "GET /upload HTTP/1.1" 200 -
413 Request Entity Too Large: The data value transmitted exceeds the capacity limit.
127.0.0.1 - - [21/Jan/2021 23:21:23] "POST /submit_script HTTP/1.1" 200 -

(flask) C:\Users\uname\source\repos\flask_non_we>

Environment

  • Windows 10
  • flask is running in cmd
  • Python version: 3.8.5
  • Flask version: 1.1.2
  • Werkzeug version: 1.0.1
@greyli
Copy link
Member

greyli commented Jan 22, 2021

Try to use a production server like Gunicorn, as explained in the docs for uploading:

Connection Reset Issue
When using the local development server, you may get a connection reset error instead of a 413 response. You will get the correct status response when running the app with a production WSGI server.

By the way, what's the environment of your own PC (that works as expected)?

@greyli
Copy link
Member

greyli commented Jan 24, 2021

I'm going to close this, feel free to comment further.

@greyli greyli closed this as completed Jan 24, 2021
@danieltuzes
Copy link
Author

@greyli
I think the config is very similar:

flask                     1.1.2
python                    3.8.3
werkzeug                  1.0.1

I run it Powershell, Windows 10.

Gunicorn is a Unix webserver, uWSGI needs linux subsystem, event works but it seems you need to provide '0.0.0.0' or localhost, bc '' is not enough.

Flask says it does not scale well and not suggested, it doesn't say it doesn't work properly, that's a large difference. It would be better to mention to not expect to work properly, many of us think it works according to the specs, but with low performance. And why does it work on my home pc and not on my work pc? I am happy to give further details if sb wants to investigate it.

@greyli
Copy link
Member

greyli commented Jan 24, 2021

Waitress works on Windows.

I am happy to give further details if sb wants to investigate it.

Please provide more details at pallets/werkzeug#1513, I think this will be fixed further in Werkzueg (check the discussion in that issue). Thank you!

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 8, 2021
@davidism
Copy link
Member

Addressed by pallets/werkzeug#2620, the development server will exhaust input in most cases and let the 413 through, although the previous comments here still stand. It's up to the client how they handle the connection being closed before all input is sent.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants