Index: /home/oleg/src/apache.org/jakarta-commons/httpclient-trunk/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java =================================================================== --- /home/oleg/src/apache.org/jakarta-commons/httpclient-trunk/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java (revision 326187) +++ /home/oleg/src/apache.org/jakarta-commons/httpclient-trunk/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java (working copy) @@ -99,9 +99,6 @@ if (partSource == null) { throw new IllegalArgumentException("Source may not be null"); } - if (partSource.getLength() < 0) { - throw new IllegalArgumentException("Source length must be >= 0"); - } this.source = partSource; } Index: /home/oleg/src/apache.org/jakarta-commons/httpclient-trunk/src/java/org/apache/commons/httpclient/methods/multipart/Part.java =================================================================== --- /home/oleg/src/apache.org/jakarta-commons/httpclient-trunk/src/java/org/apache/commons/httpclient/methods/multipart/Part.java (revision 331603) +++ /home/oleg/src/apache.org/jakarta-commons/httpclient-trunk/src/java/org/apache/commons/httpclient/methods/multipart/Part.java (working copy) @@ -323,6 +323,9 @@ */ public long length() throws IOException { LOG.trace("enter length()"); + if (lengthOfData() < 0) { + return -1; + } ByteArrayOutputStream overhead = new ByteArrayOutputStream(); sendStart(overhead); sendDispositionHeader(overhead); Index: /home/oleg/src/apache.org/jakarta-commons/httpclient-trunk/src/test/org/apache/commons/httpclient/TestMultipartPost.java =================================================================== --- /home/oleg/src/apache.org/jakarta-commons/httpclient-trunk/src/test/org/apache/commons/httpclient/TestMultipartPost.java (revision 326187) +++ /home/oleg/src/apache.org/jakarta-commons/httpclient-trunk/src/test/org/apache/commons/httpclient/TestMultipartPost.java (working copy) @@ -31,7 +31,9 @@ package org.apache.commons.httpclient; +import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import junit.framework.Test; import junit.framework.TestSuite; @@ -41,6 +43,7 @@ import org.apache.commons.httpclient.methods.multipart.FilePart; import org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity; import org.apache.commons.httpclient.methods.multipart.Part; +import org.apache.commons.httpclient.methods.multipart.PartSource; import org.apache.commons.httpclient.methods.multipart.StringPart; /** @@ -118,5 +121,60 @@ assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0); assertTrue(body.indexOf("Hello") >= 0); } + + /** + * Test that the body consisting of a file part of unknown length can be posted. + */ + + public class TestPartSource implements PartSource { + + private String fileName; + + private InputStream inputStream; + + public TestPartSource(String fileName, InputStream inputStream) { + this.fileName = fileName; + this.inputStream = inputStream; + } + + public long getLength() { + return -1; + } + + public String getFileName() { + return fileName; + } + + public InputStream createInputStream() throws IOException { + return inputStream; + } + + } + + public void testPostFilePartUnknownLength() throws Exception { + + this.server.setHttpService(new EchoService()); + + PostMethod method = new PostMethod(); + byte[] content = "Hello".getBytes(); + MultipartRequestEntity entity = new MultipartRequestEntity( + new Part[] { + new FilePart( + "param1", + new TestPartSource("filename.txt", new ByteArrayInputStream(content)), + "text/plain", + "ISO-8859-1") }, + method.getParams()); + method.setRequestEntity(entity); + + client.executeMethod(method); + + assertEquals(200,method.getStatusCode()); + String body = method.getResponseBodyAsString(); + assertTrue(body.indexOf("Content-Disposition: form-data; name=\"param1\"; filename=\"filename.txt\"") >= 0); + assertTrue(body.indexOf("Content-Type: text/plain; charset=ISO-8859-1") >= 0); + assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0); + assertTrue(body.indexOf("Hello") >= 0); + } + } -