Set content length for compressed response

  • Brijesh

    Brijesh - 2005-10-21


    Content-Length is not set for compressed response. Is there a way by which this can be enabled or set?


    • Sean Owen

      Sean Owen - 2005-10-21

      Unfortunately, no. The filter does not buffer the whole response into memory before compressing it, for performance reasons. But that also means it can't know how large the compressed response will be before it starts writing it.

    • Brijesh

      Brijesh - 2005-10-22

      I tested the compressingfilter example that comes with Tomcat. The response header has the content-length set when I use this one.

      I compared the source code of both the filters. The content-length is not set in either case but for some reason the content-length still gets set when I use the example filter. When the PJL compressing filter is used, transfer-encoding is set to chunked. I believe this is what prevents  tomcat from setting the content-length for this response.

      Is there a way to somehow prevent the header transfer-encoding? For some reason it is important for me have the content-length set in the response.

      • Sean Owen

        Sean Owen - 2005-10-22

        I believe that Tomcat's filter reads the whole response into memory, then compresses it and writes it out. Therefore it can know the length of the response. The PJL filter does not buffer the response, by design, so that it uses much less memory.

        Tomcat is correct to use chunked transfer-coding when it is not told the content length. The only way to prevent that is to set the content length, and this filter can't do that for compressed responses.

        If you absolutely need content length, I'd use Tomcat's built-in filter.

        Alternatively, maybe you can just use the PJL filter for compressing very large files. For example, if for some reason you have to have content-length set on all your pages and normally-sized files, you could set the compression threshold to 100000 or something so that only very large data files that you serve are compressed. Not sure if that helps.


Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks