Skip to content

Commit

Permalink
#1491: support pre-compressed web assets
Browse files Browse the repository at this point in the history
git-svn-id: https://xpra.org/svn/Xpra/trunk@15539 3bb7dfac-3a0b-4e04-842a-767bc560f471
  • Loading branch information
totaam committed Apr 9, 2017
1 parent d33441a commit 176019f
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
44 changes: 30 additions & 14 deletions src/xpra/net/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,23 +149,39 @@ def send_head(self):
# newline translations, making the actual size of the content
# transmitted *less* than the content-length!
f = open(path, 'rb')
fs = os.fstat(f.fileno())
content_length = fs[6]
headers = {
"Content-type" : ctype,
"Content-Length" : content_length,
}
gzip = 'gzip' in self.headers.get('accept-encoding', [])
gz_path = "%s.gz" % path
if gzip and os.path.exists(gz_path):
log("sending pre-gzipped file '%s'", gz_path)
#read pre-gzipped file:
f.close()
f = open(gz_path, 'rb')
content = f.read()
headers["Content-Encoding"] = "gzip"
else:
content = f.read()
if gzip and len(content)>128:
gzip_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
compressed_content = gzip_compress.compress(content) + gzip_compress.flush()
if len(compressed_content)<content_length:
log("gzip compressed '%s': %i down to %i bytes", path, content_length, len(compressed_content))
headers["Content-Encoding"] = "gzip"
content = compressed_content
f.close()
headers["Last-Modified"] = self.date_time_string(fs.st_mtime)
self.send_response(200)
for k,v in headers.items():
self.send_header(k, v)
self.end_headers()
except IOError:
self.send_error(404, "File not found")
return None
fs = os.fstat(f.fileno())
raw_content_length = fs[6]
content = f.read()
self.send_response(200)
self.send_header("Content-type", ctype)
if 'gzip' in self.headers.get('accept-encoding', []) and len(content)>128:
self.send_header("Content-Encoding", "gzip")
gzip_compress = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)
content = gzip_compress.compress(content) + gzip_compress.flush()
compressed_content_length = len(content)
f.close()
self.send_header("Content-Length", max(raw_content_length, compressed_content_length))
self.send_header("Last-Modified", self.date_time_string(fs.st_mtime))
self.end_headers()
return content


Expand Down
1 change: 0 additions & 1 deletion src/xpra/server/server_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,6 @@ def may_wrap_socket(self, conn, socktype, peek_data=""):
return True, conn, v

def invalid_header(self, proto, data, msg=""):
traceback.print_stack()
netlog("invalid_header(%s, %s bytes: '%s', %s) input_packetcount=%s, tcp_proxy=%s, html=%s, ssl=%s", proto, len(data or ""), msg, repr_ellipsized(data), proto.input_packetcount, self._tcp_proxy, self._html, bool(self._ssl_wrap_socket))
err = "invalid packet format, %s" % self.guess_header_protocol(data)
proto.gibberish(err, data)
Expand Down

0 comments on commit 176019f

Please sign in to comment.