Send uncompressed length in a custom header, if available, when using gzip transfer encoding

This commit is contained in:
Kovid Goyal 2015-10-16 10:58:52 +05:30
parent 673eee9f5a
commit da7ffcaf82
2 changed files with 3 additions and 0 deletions

View File

@ -595,6 +595,8 @@ class HTTPConnection(HTTPRequest):
outheaders.set('Accept-Ranges', 'bytes', replace_all=True)
if compressible and not ranges:
outheaders.set('Content-Encoding', 'gzip', replace_all=True)
if getattr(output, 'content_length', None):
outheaders.set('Calibre-Uncompressed-Length', '%d' % output.content_length)
output = GeneratedOutput(compress_readable_output(output.src_file), etag=output.etag)
if output.content_length is not None and not compressible and not ranges:
outheaders.set('Content-Length', '%d' % output.content_length, replace_all=True)

View File

@ -313,6 +313,7 @@ class TestHTTP(BaseTest):
conn = server.connect()
conn.request('GET', '/an_etagged_path', headers={'Accept-Encoding':'gzip'})
r = conn.getresponse()
self.ae(str(len(raw)), r.getheader('Calibre-Uncompressed-Length'))
self.ae(r.status, httplib.OK), self.ae(zlib.decompress(r.read(), 16+zlib.MAX_WBITS), raw)
# Test getting a filesystem file