ChunkedOutputStream missing last CRLF

Zhong Yu zhong.j.yu at gmail.com
Thu Feb 28 07:15:06 PST 2013


HTTP chunked body should be ended with

    0 CRLF CRLF

See http://tools.ietf.org/html/rfc2616#section-3.6.1

However sun.net.www.http.ChunkedOutputStream ends only with

    0 CRLF

missing the last CRLF.

This is a serious framing error. If an HTTP connection is reused, the
next message head will be considered trailer for the chunked body and
ignored, and the next message body will be parsed as head. If the HTTP
connection is not reused, the other end may be waiting indefinitely
for the chunked body to end.

Test code:

    public static void main(String[] args) throws IOException
    {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ChunkedOutputStream chunked = new ChunkedOutputStream(new
PrintStream(out));
        chunked.close();
        out.close();
        for(byte b : out.toByteArray())
            System.out.println(b);
    }

produces "0 CR LF", while it should produce "0 CR LF CR LF".

Zhong Yu



More information about the net-dev mailing list