From da7ffcaf82645b0ba1e29a2f0d9d87dfd61270c7 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 16 Oct 2015 10:58:52 +0530 Subject: [PATCH] Send uncompressed length in a custom header, if available, when using gzip transfer encoding --- src/calibre/srv/http_response.py | 2 ++ src/calibre/srv/tests/http.py | 1 + 2 files changed, 3 insertions(+) diff --git a/src/calibre/srv/http_response.py b/src/calibre/srv/http_response.py index 28dfd6e960..8d9f9112c6 100644 --- a/src/calibre/srv/http_response.py +++ b/src/calibre/srv/http_response.py @@ -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) diff --git a/src/calibre/srv/tests/http.py b/src/calibre/srv/tests/http.py index 379fd76faf..5218964aea 100644 --- a/src/calibre/srv/tests/http.py +++ b/src/calibre/srv/tests/http.py @@ -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