comparison piecrust/serving/util.py @ 1095:c8518f5cedbb

serve: Do some more useful debug logging when serving assets.
author Ludovic Chabant <ludovic@chabant.com>
date Sat, 17 Feb 2018 11:50:42 -0800
parents 8adc27285d93
children e94737572542
comparison
equal deleted inserted replaced
1094:ea6cbd6d2af5 1095:c8518f5cedbb
105 mimetype_map[t] = tokens[0] 105 mimetype_map[t] = tokens[0]
106 return mimetype_map 106 return mimetype_map
107 107
108 108
109 def make_wrapped_file_response(environ, request, path): 109 def make_wrapped_file_response(environ, request, path):
110 logger.debug("Serving %s" % path)
111
112 # Check if we can return a 304 status code. 110 # Check if we can return a 304 status code.
113 mtime = os.path.getmtime(path) 111 mtime = os.path.getmtime(path)
114 etag_str = '%s$$%s' % (path, mtime) 112 etag_str = '%s$$%s' % (path, mtime)
115 etag = hashlib.md5(etag_str.encode('utf8')).hexdigest() 113 etag = hashlib.md5(etag_str.encode('utf8')).hexdigest()
116 if etag in request.if_none_match: 114 if etag in request.if_none_match:
115 logger.debug("Serving %s [no download, E-Tag matches]" % path)
117 response = Response() 116 response = Response()
118 response.status_code = 304 117 response.status_code = 304
119 return response 118 return response
120 119
120 logger.debug("Serving %s [full download]" % path)
121 wrapper = wrap_file(environ, open(path, 'rb')) 121 wrapper = wrap_file(environ, open(path, 'rb'))
122 response = Response(wrapper) 122 response = Response(wrapper)
123 _, ext = os.path.splitext(path) 123 _, ext = os.path.splitext(path)
124 response.set_etag(etag) 124 response.set_etag(etag)
125 response.last_modified = datetime.datetime.fromtimestamp(mtime) 125 response.last_modified = datetime.datetime.fromtimestamp(mtime)