better streamer

This commit is contained in:
mdipierro
2012-09-16 15:38:31 -05:00
parent 508b3e1db7
commit 6d79d3352c
2 changed files with 13 additions and 10 deletions

View File

@@ -1 +1 @@
Version 2.0.9 (2012-09-16 14:10:35) stable
Version 2.0.9 (2012-09-16 15:38:27) stable

View File

@@ -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)