RFR: 8335135: HttpURLConnection#HttpInputStream does not throw IOException when response is truncated

Daniel Jeliński djelinski at openjdk.org
Thu Jun 27 05:15:09 UTC 2024


On Thu, 27 Jun 2024 05:06:30 GMT, Jaikiran Pai <jpai at openjdk.org> wrote:

>> Currently HttpUrlConnection accepts truncated responses: if the server sends a `Content-Length` header, and then closes the connection before transferring all promised bytes, the input stream reports a clean EOF.
>> 
>> In this PR I modify the MeteredStream class to throw an IOException when it detects EOF before receiving all promised response bytes. MeteredStream (or its subclass KeepAliveStream) is used when the response contains a content-length header.
>> 
>> The included test fails without the change, passes with it.
>> The same exception message and type is reported when a chunked response (`Transfer-Encoding: chunked`) is truncated.
>> Unknown length responses that are terminated by EOF continue to work.
>> 
>> 2 tests depended on the old behavior and had to be adjusted. The remaining tier 1-3 tests continue to pass.
>
> src/java.base/share/classes/sun/net/www/MeteredStream.java line 58:
> 
>> 56:         if (n == -1) {
>> 57:             if (expected > count) {
>> 58:                 throw new IOException("Premature EOF");
> 
> Hello Daniel, should we instead throw `java.io.EOFException` which is much more precise than a generic `IOException`? `EOFException` states:
> 
> 
>  * Signals that an end of file or end of stream has been reached
>  * unexpectedly during input.
> 
> which I think accurately depicts what's happening here.

Hi Jaikiran, I used IOException because that's already used by the ChunkedInputStream:

https://github.com/openjdk/jdk/blob/409a3fe601c61451e36554d57f29e661fe16058a/src/java.base/share/classes/sun/net/www/http/ChunkedInputStream.java#L257

No code in sun.net uses EOFException today. I suppose I could change that, but it might require changes in more places.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/19909#discussion_r1656424221


More information about the net-dev mailing list