5

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?

Heinrich Schmetterling
  • 6,614
  • 11
  • 40
  • 56
  • Here's how to do with with the zlib library: http://stackoverflow.com/questions/2695152/in-python-how-do-i-decode-gzip-encoding – Ric Mar 06 '15 at 02:47

4 Answers4

5

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.

Jonathan Works
  • 1,826
  • 1
  • 17
  • 13
2

gzip is not the same as zlib.

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
0

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.

Alfe
  • 56,346
  • 20
  • 107
  • 159
0

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".

John Machin
  • 81,303
  • 11
  • 141
  • 189