-
Notifications
You must be signed in to change notification settings - Fork 19
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
Empty files when request logger enabled. #4
Comments
Thanks for the detailed bug report. You're right, the behaviour is not optimal and I will improve it. I will fix it in wsgi-request-logger but you can work-around this issue by setting the "Content-Length" header in your application. If you call @app.route('/file', methods=['GET'])
def serve_file():
f = open('data', 'rb')
sf = send_file(f, mimetype="application/ocet-stream")
sf.headers['Content-Length'] = str(os.path.getsize('data'))
return sf Concerning the how-to-fix-it in wsgi-request-logger I'm thinking about something like this: if content_lengths :
content_length = content_lengths[0]
elif all(hasattr(retval, attr) for attr in ('seek', 'tell')):
retval.seek(0, 2)
content_length = retval.tell()
retval.seek(0)
elif hasattr(retval, 'file') and hasattr(retval.file, 'seek') and hasattr(retval.file, 'tell'):
retval.file.seek(0, 2)
content_length = retval.file.tell()
retval.file.seek(0)
else:
content_length = len(b''.join(retval)) Not optimal yet... If you're really streaming content where you don't know the content size at time when you start serving it (e.g. live content such as mjpeg frames from a web cam), the Let me know what you think and thanks again for reporting the issue. |
the alternative is to delay the log until response close, you have to do some counting during the response, |
simple POC
|
I'm serving dynamically generated files with my WSGI application and are streaming them to the client. When I apply the wsgi-request-logger middleware the files are empty. The reason is that when you are calculating the content type you are calling len on the retval which on a file-like object sets the internal pointer to the end of the file.
A minimal example with flask, where the middleware breaks the application:
This application will serves an empty file. Commenting out the middleware serves the file properly.
The text was updated successfully, but these errors were encountered: