Flask not sending 304 Not Modified even if last_modified<=if_modified_since.
Here's my view.
@app.route('/image/<file_id>')
@login_required
def images(file_id):
file = file_like_object_here
rv = send_file(file,
mimetype=file.content_type,
# as_attachment=True,
attachment_filename=file.filename,
add_etags=False,
conditional=True
)
rv.last_modified = file.upload_date
app.logger.debug(rv.last_modified) # 2012-11-24 08:51:27
app.logger.debug(request.if_modified_since) # 2012-11-24 08:51:27
app.logger.debug(rv.last_modified<=request.if_modified_since) # True
return rv
Here is the HTTP: Request
Request URL:http://127.0.0.1:5000/image/50b0855f2c2d291cbe896e56
Request Method:GET
Status Code:200 OK
GET /image/50b0855f2c2d291cbe896e56 HTTP/1.1
Host: 127.0.0.1:5000
Connection: keep-alive
Cache-Control: max-age=0
User-Agent: Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.64 Safari/537.11
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
Cookie: session="lde2mnK7NSOGS8DqcU5YVgTkVEc=?_expires=STEzNTY1MDE0OTgKLg==&_permanent=STAxCi4=&username=VmFhYWFhCnAxCi4="
If-Modified-Since: Sat, 24 Nov 2012 08:51:27 GMT
Response:
HTTP/1.0 200 OK
Content-Type: image/png
Cache-Control: public, max-age=43200
Expires: Sun, 25 Nov 2012 18:43:12 GMT
Last-Modified: Sat, 24 Nov 2012 08:51:27 GMT
Connection: close
Server: Werkzeug/0.8.3 Python/2.7.3
Date: Sun, 25 Nov 2012 06:43:12 GMT
Ideally it should return 304 Not Modified but you can see that's not happening.
Maybe it's because in this case last_modified == if_modified_since. Can you test with a last_modified < if_modified_since?
EDIT: That should work anyways. I'll check it out.
I was able to reproduce this with https://gist.github.com/4142919
Adding
rv.make_conditional(request)
fixed it.
I guess another way would be to set the conditional argument of flask.send_file to True.
Setting only conditional will not work, though, it seems, as send_etags has to be enabled as well: https://github.com/mitsuhiko/flask/blob/master/flask/helpers.py#L521
So using rv.make_conditional(request) makes much more sense I think, as @untitaker suggests
https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/wrappers.py#L1249
I'm pretty sure that's not ideal behavior since the server should return a 304: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.25
If the variant has not been modified since a valid If-
Modified-Since date, the server SHOULD return a 304 (Not
Modified) response.
Actually :neckbeard: ... Werkzeug is HTTP/1.0, but the same stands true.
http://www.w3.org/Protocols/HTTP/1.0/spec.html#If-Modified-Since http://www.w3.org/Protocols/HTTP/1.0/spec.html#Code304
By default Werkzeug does do this for static file serving, and make_conditional is only employed in Flask's send_file (which is used for static files, too), the development server uses the SharedDataMiddleware middleware for static paths:
https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/serving.py#L662 https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/wsgi.py#L254 https://github.com/mitsuhiko/werkzeug/blob/master/werkzeug/wsgi.py#L418
send_file doesn't do this as it can accept a filestream without a creation time at all, for one. Perhaps you should serve your files via the SharedDataMiddleware instead (multiple static paths). But, yes, send_file seems quirky at first glance...
Production-ready servers are able to handle this much more reliably for static data; dynamically generated content caching logic is always custom, though.
At least Etags would be possible for streams. So they could be enabled by default.
That was deprecated https://github.com/mitsuhiko/flask/blob/master/flask/helpers.py#L461
In future flask releases etags will no longer be generated for file objects passed to the send_file function because this behavior was unreliable. Pass filenames instead if possible, otherwise attach an etag yourself based on another value
https://github.com/mitsuhiko/flask/commit/fda14678c07d036ef3a1984a4e346e793cc5a63c huge discussion in related issue