Trouble with gzip output compression

S

Szymon Rozga

I am using the Rails output_compression plugin, but this applies to
nonRails applications as well. I am trying to compress a large amount
of output and send it. However, the browser doesn't seem to decode the
gzip properly (instead of some tables I need, it displays a lot of
junk) The code in question is the following:

def compress_output
return unless accepts_gzip?
output = StringIO.new
def output.close
# Zlib does a close. Bad Zlib...
rewind
end
gz = Zlib::GzipWriter.new(output)
gz.write(response.body)
gz.close
if output.length < response.body.length
@old_response_body = response.body
response.body = output.string
response.headers['Content-encoding'] = @compression_encoding
end
end

def accepts_gzip?
return false unless GZIP_SUPPORTED
accepts = request.env['HTTP_ACCEPT_ENCODING']
return false unless accepts && accepts =~ /(x-gzip|gzip)/
@compression_encoding = $1
true
end

Is there any reason that this code would _not_ properly compress the
output?
 
R

Robert Klemme

Szymon said:
I am using the Rails output_compression plugin, but this applies to
nonRails applications as well. I am trying to compress a large amount
of output and send it. However, the browser doesn't seem to decode the
gzip properly (instead of some tables I need, it displays a lot of
junk) The code in question is the following:

def compress_output
return unless accepts_gzip?
output = StringIO.new
def output.close
# Zlib does a close. Bad Zlib...
rewind
end
gz = Zlib::GzipWriter.new(output)
gz.write(response.body)
gz.close
if output.length < response.body.length
@old_response_body = response.body
response.body = output.string
response.headers['Content-encoding'] = @compression_encoding
end
end

def accepts_gzip?
return false unless GZIP_SUPPORTED
accepts = request.env['HTTP_ACCEPT_ENCODING']
return false unless accepts && accepts =~ /(x-gzip|gzip)/
@compression_encoding = $1
true
end

Is there any reason that this code would _not_ properly compress the
output?

Do you indicate compression via a HTTP header? Maybe that's the missing
part.

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top