diff --git a/glasnost-web/code/webhandler.py b/glasnost-web/code/webhandler.py index 588dca82..c4b2e314 100644 --- a/glasnost-web/code/webhandler.py +++ b/glasnost-web/code/webhandler.py @@ -212,7 +212,7 @@ class Application(applications.Application): lastModTime = time.gmtime(os.stat(_req.filename)[-2]) try: return self.outputStaticFile( - req, staticFile.read(), lastModTime) + req, staticFile, lastModTime) except IOError, error: if error.errno in (errno.EACCES, errno.EISDIR): return HTTP_FORBIDDEN @@ -416,7 +416,7 @@ class Application(applications.Application): lastModTime = time.gmtime(os.stat(staticFilePath)[-2]) try: return self.outputStaticFile( - req, staticFile.read(), lastModTime, mimeType) + req, staticFile, lastModTime, mimeType) except IOError, error: if error.errno in (errno.EACCES, errno.EISDIR): return HTTP_FORBIDDEN @@ -768,19 +768,28 @@ class Application(applications.Application): except ImportError: 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'] if value.find('gzip') != -1 and value.find('gzip;q=0') == -1: + if not type(data) is type(''): + data = data.read() zbuf = cStringIO.StringIO() zfile = gzip.GzipFile(mode = 'wb', fileobj = zbuf) zfile.write(data) zfile.close() data = zbuf.getvalue() + del zbuf req.headers_out['Content-Encoding'] = 'gzip' + fileSize = len(data) if lastModTime: req.headers_out['Last-Modified'] = time.strftime( '%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 if lastModTime and req.headers_in.has_key('If-Modified-Since'): # 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.send_http_header() if req.method != 'HEAD': - try: - req.write(data) - except IOError: - # the user probably cancelled the request - return OK + if type(data) is type(''): + try: + req.write(data) + except IOError: + # 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 def parseHttpPath(self, remaining):