Trouble with gzip output compression

Discussion in 'Ruby' started by Szymon Rozga, May 25, 2006.

  1. Szymon Rozga

    Szymon Rozga Guest

    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?
     
    Szymon Rozga, May 25, 2006
    #1
    1. Advertising

  2. Szymon Rozga wrote:
    > 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
     
    Robert Klemme, May 26, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chad A. Beckner

    GZip Compression for IIS 5.0/ASP .NET

    Chad A. Beckner, Jun 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    589
    Landi
    Jul 23, 2004
  2. Anders K. Jacobsen [DK]

    Gzip compression and ASP.NET

    Anders K. Jacobsen [DK], Dec 30, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    4,725
    Joerg Jooss
    Dec 31, 2004
  3. Nikk & Jak Anderson

    GZIP output and input strange behaviour

    Nikk & Jak Anderson, Apr 26, 2004, in forum: Java
    Replies:
    2
    Views:
    1,209
    Roedy Green
    Apr 26, 2004
  4. Mike P2

    GZip output compression with AJAX

    Mike P2, Jun 4, 2007, in forum: ASP .Net
    Replies:
    4
    Views:
    795
    =?Utf-8?B?YmlrZW1pa2U=?=
    Jun 12, 2007
  5. Fernando Silva

    Gzip compression with SOAP

    Fernando Silva, Dec 29, 2009, in forum: Ruby
    Replies:
    0
    Views:
    245
    Fernando Silva
    Dec 29, 2009
Loading...

Share This Page