From 10694d8d1e40ed1d176e18c753d789f7197bc555 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 6 Sep 2012 16:33:00 +0200 Subject: [PATCH] send library.xml gzip encoded --- oxcd/__init__.py | 5 ++-- oxcd/gziprequest.py | 68 +++++++++++++++++++++++++++++++++++++++++++++ oxcd/server.py | 10 ++++++- 3 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 oxcd/gziprequest.py diff --git a/oxcd/__init__.py b/oxcd/__init__.py index 265dc54..b99aa78 100644 --- a/oxcd/__init__.py +++ b/oxcd/__init__.py @@ -24,10 +24,11 @@ def itunes_path(): def main(port, itunes): base = os.path.abspath(os.path.dirname(__file__)) + print 'loading', itunes backend = iTunes(itunes) root = Server(base, backend) site = Site(root) reactor.listenTCP(port, site) - reactor.callLater(1, lambda: webbrowser.open_new_tab('http://127.0.0.1:%s/' % port)) - print 'opening browser at http://127.0.0.1:%s/ ...' % port + #reactor.callLater(1, lambda: webbrowser.open_new_tab('http://127.0.0.1:%s/' % port)) + print 'listening for browsers at http://127.0.0.1:%s/ ...' % port reactor.run() diff --git a/oxcd/gziprequest.py b/oxcd/gziprequest.py new file mode 100644 index 0000000..7a627cd --- /dev/null +++ b/oxcd/gziprequest.py @@ -0,0 +1,68 @@ +import struct +import zlib +from twisted.web.static import File + +class GzipFile(File): + def render_GET(self, request): + request = GzipRequest(request) + return File.render_GET(self, request) + +class GzipRequest(object): + """Wrapper for a request that applies a gzip content encoding""" + + def __init__(self, request, compressLevel=6): + self.request = request + self.request.setHeader('Content-Encoding', 'gzip') + # Borrowed from twisted.web2 gzip filter + self.compress = zlib.compressobj(compressLevel, zlib.DEFLATED, + -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL,0) + + def __getattr__(self, attr): + if 'request' in self.__dict__: + return getattr(self.request, attr) + else: + raise AttributeError, attr + + def __setattr__(self, attr, value): + if 'request' in self.__dict__: + return setattr(self.request, attr, value) + else: + self.__dict__[attr] = value + + def write(self, data): + if not self.request.startedWriting: + #print 'GzipRequest: Initializing' + self.crc = zlib.crc32('') + self.size = self.csize = 0 + # XXX: Zap any length for now since we don't know final size + if 'content-length' in self.request.headers: + del self.request.headers['content-length'] + # Borrow header information from twisted.web2 gzip filter + self.request.write('\037\213\010\000' '\0\0\0\0' '\002\377') + + self.crc = zlib.crc32(data, self.crc) + self.size += len(data) + cdata = self.compress.compress(data) + self.csize += len(cdata) + ''' + print 'GzipRequest: ' \ + 'Writing %d bytes, %d total (%d compressed, %d total)' % \ + (len(data),self.size,len(cdata),self.csize) + ''' + if cdata: + self.request.write(cdata) + elif self.request.producer: + # Simulate another pull even though it hasn't really made it + # out to the consumer yet. + self.request.producer.resumeProducing() + + def finish(self): + remain = self.compress.flush() + self.csize += len(remain) + #print 'GzipRequest: Finishing (size %d, compressed %d)' % (self.size, self.csize) + if remain: + self.request.write(remain) + self.request.write(struct.pack('