You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by ol...@apache.org on 2005/11/09 20:41:49 UTC

svn commit: r332125 - in /jakarta/commons/proper/httpclient/trunk/src: java/org/apache/commons/httpclient/methods/multipart/ test/org/apache/commons/httpclient/

Author: olegk
Date: Wed Nov  9 11:41:40 2005
New Revision: 332125

URL: http://svn.apache.org/viewcvs?rev=332125&view=rev
Log:
PR #37256 (MultipartEntity incorrectly computes unknown length)

Contributed by Loïc Péron <loic.peron at bigfoot.com>
Reviewed by Michael Becke, Oleg Kalnichevski, Ortwin Glück

Modified:
    jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java
    jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/methods/multipart/Part.java
    jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestMultipartPost.java

Modified: jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java?rev=332125&r1=332124&r2=332125&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/methods/multipart/FilePart.java Wed Nov  9 11:41:40 2005
@@ -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;
     }
         

Modified: jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/methods/multipart/Part.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/methods/multipart/Part.java?rev=332125&r1=332124&r2=332125&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/methods/multipart/Part.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/java/org/apache/commons/httpclient/methods/multipart/Part.java Wed Nov  9 11:41:40 2005
@@ -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);

Modified: jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestMultipartPost.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestMultipartPost.java?rev=332125&r1=332124&r2=332125&view=diff
==============================================================================
--- jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestMultipartPost.java (original)
+++ jakarta/commons/proper/httpclient/trunk/src/test/org/apache/commons/httpclient/TestMultipartPost.java Wed Nov  9 11:41:40 2005
@@ -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,59 @@
         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 byte[] data;
+
+        public TestPartSource(String fileName, byte[] data) {
+            this.fileName = fileName;
+            this.data = data;
+        }
+
+        public long getLength() {
+            return -1;
+        }
+
+        public String getFileName() {
+            return fileName;
+        }
+
+        public InputStream createInputStream() throws IOException {
+            return new ByteArrayInputStream(data);
+        }
+
+    }
+    
+    public void testPostFilePartUnknownLength() throws Exception {
+        
+        this.server.setHttpService(new EchoService());
+
+        String enc = "ISO-8859-1";
+        PostMethod method = new PostMethod();
+        byte[] content = "Hello".getBytes(enc);
+        MultipartRequestEntity entity = new MultipartRequestEntity(
+            new Part[] { 
+                new FilePart(
+                    "param1", 
+                    new TestPartSource("filename.txt", content), 
+                     "text/plain", 
+                     enc) },
+             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="+enc) >= 0);
+        assertTrue(body.indexOf("Content-Transfer-Encoding: binary") >= 0);
+        assertTrue(body.indexOf("Hello") >= 0);
+    }
+    
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org