n'essaie pas de gziper les fichiers trop gros (le serveur du BxLUG vient de se
vautrer à charger en RAM une ISO pour la compresser) (+ lecture progressive pour le transfert sur le stream http)
This commit is contained in:
parent
698e92483c
commit
236f4a7d5d
|
@ -212,7 +212,7 @@ class Application(applications.Application):
|
||||||
lastModTime = time.gmtime(os.stat(_req.filename)[-2])
|
lastModTime = time.gmtime(os.stat(_req.filename)[-2])
|
||||||
try:
|
try:
|
||||||
return self.outputStaticFile(
|
return self.outputStaticFile(
|
||||||
req, staticFile.read(), lastModTime)
|
req, staticFile, lastModTime)
|
||||||
except IOError, error:
|
except IOError, error:
|
||||||
if error.errno in (errno.EACCES, errno.EISDIR):
|
if error.errno in (errno.EACCES, errno.EISDIR):
|
||||||
return HTTP_FORBIDDEN
|
return HTTP_FORBIDDEN
|
||||||
|
@ -416,7 +416,7 @@ class Application(applications.Application):
|
||||||
lastModTime = time.gmtime(os.stat(staticFilePath)[-2])
|
lastModTime = time.gmtime(os.stat(staticFilePath)[-2])
|
||||||
try:
|
try:
|
||||||
return self.outputStaticFile(
|
return self.outputStaticFile(
|
||||||
req, staticFile.read(), lastModTime, mimeType)
|
req, staticFile, lastModTime, mimeType)
|
||||||
except IOError, error:
|
except IOError, error:
|
||||||
if error.errno in (errno.EACCES, errno.EISDIR):
|
if error.errno in (errno.EACCES, errno.EISDIR):
|
||||||
return HTTP_FORBIDDEN
|
return HTTP_FORBIDDEN
|
||||||
|
@ -768,19 +768,28 @@ class Application(applications.Application):
|
||||||
except ImportError:
|
except ImportError:
|
||||||
gzip = None
|
gzip = None
|
||||||
|
|
||||||
if gzip and req.headers_in.has_key('Accept-Encoding'):
|
if type(data) is type(''):
|
||||||
|
fileSize = len(data)
|
||||||
|
else:
|
||||||
|
fileSize = os.fstat(data.fileno())[6]
|
||||||
|
if gzip and fileSize < 1000000 and \
|
||||||
|
req.headers_in.has_key('Accept-Encoding'):
|
||||||
value = req.headers_in['Accept-Encoding']
|
value = req.headers_in['Accept-Encoding']
|
||||||
if value.find('gzip') != -1 and value.find('gzip;q=0') == -1:
|
if value.find('gzip') != -1 and value.find('gzip;q=0') == -1:
|
||||||
|
if not type(data) is type(''):
|
||||||
|
data = data.read()
|
||||||
zbuf = cStringIO.StringIO()
|
zbuf = cStringIO.StringIO()
|
||||||
zfile = gzip.GzipFile(mode = 'wb', fileobj = zbuf)
|
zfile = gzip.GzipFile(mode = 'wb', fileobj = zbuf)
|
||||||
zfile.write(data)
|
zfile.write(data)
|
||||||
zfile.close()
|
zfile.close()
|
||||||
data = zbuf.getvalue()
|
data = zbuf.getvalue()
|
||||||
|
del zbuf
|
||||||
req.headers_out['Content-Encoding'] = 'gzip'
|
req.headers_out['Content-Encoding'] = 'gzip'
|
||||||
|
fileSize = len(data)
|
||||||
if lastModTime:
|
if lastModTime:
|
||||||
req.headers_out['Last-Modified'] = time.strftime(
|
req.headers_out['Last-Modified'] = time.strftime(
|
||||||
'%a, %d %b %Y %H:%M:%S GMT', lastModTime)
|
'%a, %d %b %Y %H:%M:%S GMT', lastModTime)
|
||||||
req.headers_out['Content-Length'] = '%d' % len(data)
|
req.headers_out['Content-Length'] = '%d' % fileSize
|
||||||
# TODO: could also output Content-MD5
|
# TODO: could also output Content-MD5
|
||||||
if lastModTime and req.headers_in.has_key('If-Modified-Since'):
|
if lastModTime and req.headers_in.has_key('If-Modified-Since'):
|
||||||
# we don't want to use bandwith if the file was not modified
|
# we don't want to use bandwith if the file was not modified
|
||||||
|
@ -795,11 +804,21 @@ class Application(applications.Application):
|
||||||
req.content_type = mimeType
|
req.content_type = mimeType
|
||||||
req.send_http_header()
|
req.send_http_header()
|
||||||
if req.method != 'HEAD':
|
if req.method != 'HEAD':
|
||||||
try:
|
if type(data) is type(''):
|
||||||
req.write(data)
|
try:
|
||||||
except IOError:
|
req.write(data)
|
||||||
# the user probably cancelled the request
|
except IOError:
|
||||||
return OK
|
# the user probably cancelled the request
|
||||||
|
return OK
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
t = 1
|
||||||
|
while t:
|
||||||
|
t = data.read(1000000) # 1MB chunks
|
||||||
|
req.write(t)
|
||||||
|
except IOError:
|
||||||
|
# the user probably cancelled the request
|
||||||
|
return OK
|
||||||
return OK
|
return OK
|
||||||
|
|
||||||
def parseHttpPath(self, remaining):
|
def parseHttpPath(self, remaining):
|
||||||
|
|
Reference in New Issue