Re: Jetty closing sockets when contentLength != lengthOfcontentWritten
Kevin Conaway wrote:
> When using persistent connections, jetty will close a socket connection
> if the length of the content body written does not match the
> Content-Length header set.
> This seems to be a bug in our software when we are using compression as
> we set the Content-Length to be the length of the uncompressed file but
> we are writing the compressed bytes over the wire.
Ah yup, that's a bug all right :) You need to either set the Content-Length
to be the length of the compressed data, or don't set the Content-Length
field at all, in which case Jetty will use chunking to output the data.
> I'd like to know why Jetty forces the connection to be closed though.
> If anyone could shed some light on the reasoning behind this, I'd be
The HTTP spec says that the server can close a connection to delimit
a response http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.4 -
and this would seem reasonable if there's a problem with the Content-Length -
but the fundamental reason is that if you've promised to write x bytes and
then only send x-n bytes, the browser may take the next n bytes received
on that connection to complete that response. Then your message framing
will be up the spout.
> The relevant code is in org.mortbay.jetty.AbstractGenerator#complete()
> Kevin Conaway