Index: src/java/org/apache/commons/httpclient/ChunkedInputStream.java =================================================================== --- src/java/org/apache/commons/httpclient/ChunkedInputStream.java (revision 694322) +++ src/java/org/apache/commons/httpclient/ChunkedInputStream.java (working copy) @@ -34,6 +34,7 @@ import java.io.IOException; import java.io.InputStream; +import org.apache.commons.httpclient.params.HttpMethodParams; import org.apache.commons.httpclient.util.EncodingUtil; import org.apache.commons.httpclient.util.ExceptionUtil; import org.apache.commons.logging.Log; @@ -219,6 +220,13 @@ readCRLF(); } chunkSize = getChunkSizeFromInputStream(in); + if (chunkSize < 0) { + if (this.method == null || this.method.getParams().isParameterFalse(HttpMethodParams.ALLOW_CHUNKED_CLOSE)) { + throw new IOException("chunked stream ended unexpectedly"); + } + eof = true; + return; + } bof = false; pos = 0; if (chunkSize == 0) { @@ -249,7 +257,7 @@ while (state != -1) { int b = in.read(); if (b == -1) { - throw new IOException("chunked stream ended unexpectedly"); + return -1; } switch (state) { case 0: Index: src/java/org/apache/commons/httpclient/params/HttpMethodParams.java =================================================================== --- src/java/org/apache/commons/httpclient/params/HttpMethodParams.java (revision 694322) +++ src/java/org/apache/commons/httpclient/params/HttpMethodParams.java (working copy) @@ -101,6 +101,16 @@ public static final String STRICT_TRANSFER_ENCODING = "http.protocol.strict-transfer-encoding"; /** + * Defines the behavior when a server closes a Transfer-Encoding: chunked connection + * before sending the last, zero-length, chunk. If this parameter is false or not defined, + * an IOException will be thrown. + *
+ * This parameter expects a value of type {@link Boolean}. + *
+ */ + public static final String ALLOW_CHUNKED_CLOSE = "http.protocol.allow-chunked-close"; + + /** * Defines whether the content body sent in response to * {@link org.apache.commons.httpclient.methods.HeadMethod} should be rejected. *Index: xdocs/preference-api.xml =================================================================== --- xdocs/preference-api.xml (revision 694322) +++ xdocs/preference-api.xml (working copy) @@ -200,6 +200,18 @@
<undefined>
http.protocol.allow-chunked-close
Boolean
+ Defines the behavior when a server closes a Transfer-Encoding: chunked connection before + sending the last, zero-length, chunk. If this parameter is false or not defined, an + IOException will be thrown. +
+false
http.protocol.reject-head-body
Boolean