From 6d79d3352c8a052efa3f6d7526189a0d3fef9ad4 Mon Sep 17 00:00:00 2001 From: mdipierro Date: Sun, 16 Sep 2012 15:38:31 -0500 Subject: [PATCH] better streamer --- VERSION | 2 +- gluon/streamer.py | 21 ++++++++++++--------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/VERSION b/VERSION index da509fa6..33df070a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -Version 2.0.9 (2012-09-16 14:10:35) stable +Version 2.0.9 (2012-09-16 15:38:27) stable diff --git a/gluon/streamer.py b/gluon/streamer.py index a46ae7cf..ed259638 100644 --- a/gluon/streamer.py +++ b/gluon/streamer.py @@ -13,6 +13,7 @@ import time import re import errno import rewrite +from rocket import SERVER_SOFTWARE from http import HTTP from contenttype import contenttype @@ -60,8 +61,8 @@ def stream_file_or_304_or_206( fp.close() stat_file = os.stat(static_file) fsize = stat_file[stat.ST_SIZE] - mtime = time.strftime('%a, %d %b %Y %H:%M:%S GMT', - time.gmtime(stat_file[stat.ST_MTIME])) + modified = stat_file[stat.ST_MTIME] + mtime = time.strftime('%a, %d %b %Y %H:%M:%S GMT',time.gmtime(modified)) headers.setdefault('Content-Type', contenttype(static_file)) headers.setdefault('Last-Modified', mtime) headers.setdefault('Pragma', 'cache') @@ -91,6 +92,15 @@ def stream_file_or_304_or_206( headers['Content-Length'] = '%i' % bytes status = 206 else: + if request.env.server == SERVER_SOFTWARE and \ + 'gzip' in request.env.http_accept_encoding and\ + not 'Content-Encoding' in headers: + gzipped = static_file + '.gz' + if os.path.isfile(gzipped) and os.path.getmtime(gzipped)>modified: + static_file = gzipped + fsize = os.path.getsize(gzipped) + headers['Content-Encoding'] = 'gzip' + headers['Vary'] = 'Accept-Encoding' try: stream = open(static_file, 'rb') except IOError, e: @@ -106,10 +116,3 @@ def stream_file_or_304_or_206( else: wrapped = streamer(stream, chunk_size=chunk_size, bytes=bytes) raise HTTP(status, wrapped, **headers) - - - - - - -