ChunkedOutputStream missing last CRLF

Chris Hegarty chris.hegarty at oracle.com
Thu Feb 28 07:49:33 PST 2013


Clearly sun.net.www.http.ChunkedOutpuStream is not a public API, so its 
direct use is not supported, or recommended.

Users of the HttpURLConnection API can only get indirect references to 
ChunkedOutputStream, and these are wrapped in a StreamingOutputStream 
[1]. The StreamingOutputStream.close will write the trailing CRLF.

-Chris.

[1] 
http://hg.openjdk.java.net/jdk8/tl/jdk/file/7d272e524768/src/share/classes/sun/net/www/protocol/http/HttpURLConnection.java

On 02/28/2013 03:15 PM, Zhong Yu wrote:
> 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