tomcat6chunked-encoding

Forcing chunked encoding in Tomcat response


I am migrating an application for a customer to Tomcat 6. During testing we discovered that some of their clients expect the response to always be chunked, i.e. Transfer-encoding: chunked. However, Tomcat does not always use chunked encoding and sometimes uses Content-Length instead, I assume because the response size is small enough that it knew the whole length when the headers were being sent.

I have seen tons of questions/answers about how to disable chunked encoding but I have the opposite problem. I know that it is not ideal to force chunked encoding in every response and that the client should be fully compatible with the http1.1 spec and accept either encoding, but I'm just trying to find out if there is any easy configuration I can do to enable the desired behavior.

I am using Tomcat alone and the standard http connector. I've looked at the connector attributes and played around with bufferSize and socketBuffer with no luck.

Is there anything I can do to force chunked responses?


Solution

  • Just in case anyone else runs into this, I found a hacky solution. I immediately flush the output stream before I write anything to it. That forces Tomcat to immediately send the response headers and use chunked encoding. I know it's lame because that means it immediately sends a 200 status code before I've even done my processing but that's the only way I could find to force the chunked encoding.