okhttp icon indicating copy to clipboard operation
okhttp copied to clipboard

[BUG] okhttp3 will raise "java.io.IOException: Content-Length (x) and stream length (0) disagree" while parseFrom a HEAD response

Open gaort opened this issue 9 years ago • 3 comments

with rfc2616-sec9.4:

The HEAD method is identical to GET except that the server MUST NOT return a message-body in the response.

so, while parsing a HEAD response, the body-stream length will always be 0;

with rfc2616-sec14.13:

The Content-Length entity-header field indicates the size of the entity-body, in decimal number of OCTETs, sent to the recipient or, in the case of the HEAD method, the size of the entity-body that would have been sent had the request been a GET.

 Content-Length    = "Content-Length" ":" 1*DIGIT

An example is

Content-Length: 3495

so, while parsing a HEAD response, the "Content-Length" header may NOT be 0;

in okhttp3.ResponseBody#bytes(), line 140 nearby

public final byte[] bytes() throws IOException {
    long contentLength = contentLength();
    if (contentLength > Integer.MAX_VALUE) {
        throw new IOException("Cannot buffer entire body for content length: " + contentLength);
    }

    BufferedSource source = source();
    byte[] bytes;
    try {
        bytes = source.readByteArray();
    } finally {
        Util.closeQuietly(source);
    }
    if (contentLength != -1 && contentLength != bytes.length) {
        throw new IOException("Content-Length ("
        + contentLength
        + ") and stream length ("
        + bytes.length
        + ") disagree");
    }
    return bytes;
}

so, while okhttp3 receive a HEAD response, there will always raise a java.io.IOException

gaort avatar May 18 '17 09:05 gaort

I meet this issue too. Please help fix this. Thanks

songkaiape avatar Jul 03 '17 08:07 songkaiape

I get this in 3.14.3 once we turned on H2 on the server. No other changes except that.

travisspencer avatar Jul 28 '20 14:07 travisspencer

@travisspencer We would need a way to reproduce against MockWebServer or externally to investigate. Please report a bug with a repro, or consider a post to stackoverflow for help.

yschimke avatar Jul 28 '20 17:07 yschimke