Mercurial > piecrust2
diff piecrust/serving.py @ 7:343d08ef5668
More PieCrust 3 fixes, and a couple of miscellaneous bug fixes.
author | Ludovic Chabant <ludovic@chabant.com> |
---|---|
date | Sat, 16 Aug 2014 15:07:22 -0700 |
parents | 474c9882decf |
children | 617191dec18e |
line wrap: on
line diff
--- a/piecrust/serving.py Sat Aug 16 08:15:30 2014 -0700 +++ b/piecrust/serving.py Sat Aug 16 15:07:22 2014 -0700 @@ -23,8 +23,6 @@ logger = logging.getLogger(__name__) - - class Server(object): def __init__(self, root_dir, host='localhost', port='8080', debug=False, static_preview=True): @@ -122,7 +120,7 @@ pipeline.run(asset_in_path) logger.debug("Serving %s" % asset_out_path) - wrapper = wrap_file(environ, open(asset_out_path)) + wrapper = wrap_file(environ, open(asset_out_path, 'rb')) response = Response(wrapper) _, ext = os.path.splitext(rel_req_path) response.mimetype = self._mimetype_map.get( @@ -138,7 +136,7 @@ return None logger.debug("Serving %s" % full_path) - wrapper = wrap_file(environ, open(full_path)) + wrapper = wrap_file(environ, open(full_path, 'rb')) response = Response(wrapper) _, ext = os.path.splitext(full_path) response.mimetype = self._mimetype_map.get( @@ -197,10 +195,17 @@ rendered_page = render_page(render_ctx) rp_content = rendered_page.content + if app.debug: + now_time = time.clock() + timing_info = ('%8.1f ms' % + ((now_time - app.env.start_time) * 1000.0)) + rp_content = rp_content.replace('__PIECRUST_TIMING_INFORMATION__', + timing_info) + # Start response. response = Response() - etag = hashlib.md5(rp_content).hexdigest() + etag = hashlib.md5(rp_content.encode('utf8')).hexdigest() if not app.debug and etag in request.if_none_match: response.status_code = 304 return response @@ -227,29 +232,18 @@ if mimetype: response.mimetype = mimetype - if app.debug: - now_time = time.clock() - timing_info = ('%8.1f ms' % - ((now_time - app.env.start_time) * 1000.0)) - rp_content = rp_content.replace('__PIECRUST_TIMING_INFORMATION__', - timing_info) - if ('gzip' in request.accept_encodings and app.config.get('site/enable_gzip')): try: - gzip_buffer = io.StringIO() - gzip_file = gzip.GzipFile( - mode='wb', - compresslevel=9, - fileobj=gzip_buffer) - gzip_file.write(rp_content) - gzip_file.close() - rp_content = gzip_buffer.getvalue() - response.content_encoding = 'gzip' + with io.BytesIO() as gzip_buffer: + with gzip.open(gzip_buffer, mode='wt', + encoding='utf8') as gzip_file: + gzip_file.write(rp_content) + rp_content = gzip_buffer.getvalue() + response.content_encoding = 'gzip' except Exception: logger.exception("Error compressing response, " "falling back to uncompressed.") - rp_content = rendered_page.content response.set_data(rp_content) return response