Reputation: 6884
I'm using Amazon S3 to serve static files. When the Content-Type is just 'text/css' and I haven't compressed the file, it is returned ok. If I try to zlib.compress() the contents that will be returned and change Content-Encoding to 'gzip', the browser cannot decode the result. In Chrome, the error is
Error 330 net::ERR_CONTENT_DECODING_FAILED
in Safari,
“cannot decode raw data” (NSURLErrorDomain:-1015)
Is there something special to do with python's zlib to make sure the result can be returned and decompressed by the browser?
Upvotes: 5
Views: 1376
Reputation: 59436
Instead of using the module zlib
, (originalString = inputFile.read()
and then compressedString = zlib.compress(originalString)
) I am now using the module gzip
:
stream = cStringIO.StringIO()
compressor = gzip.GzipFile(fileobj=stream, mode='w')
while True: # until EOF
chunk = inputFile.read(8192)
if not chunk: # EOF?
compressor.close()
return stream.getvalue()
compressor.write(chunk)
The result then is compatible to gzip
; I don't know if it fixes your webserver issue as well.
Upvotes: 0
Reputation: 1846
I have this same problem.
If you send the header:
Content-Encoding: gzip
Safari/Chrome show that error.
But if you instead send:
Content-Encoding: deflate
Safari/Chrome decodes the input fine.
Upvotes: 5
Reputation: 82934
It is decodable. The problem is that the sender is lying to the receiver -- not a good way of ensuring harmonious communication. Try calling it "zlib" instead of "gzip".
Upvotes: 0