[seam-commits] Seam SVN: r14146 - in branches/enterprise/JBPAPP_5_0/src: test/unit/org/jboss/seam/test/unit and 1 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Aug 11 10:14:38 EDT 2011


Author: manaRH
Date: 2011-08-11 10:14:38 -0400 (Thu, 11 Aug 2011)
New Revision: 14146

Added:
   branches/enterprise/JBPAPP_5_0/src/test/unit/org/jboss/seam/test/unit/web/MultipartRequestImplTest.java
Modified:
   branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam/web/MultipartRequestImpl.java
   branches/enterprise/JBPAPP_5_0/src/test/unit/org/jboss/seam/test/unit/testng.xml
Log:
JBPAPP-6946 - fixing MultipartRequestImpl for missing data

Modified: branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam/web/MultipartRequestImpl.java
===================================================================
--- branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam/web/MultipartRequestImpl.java	2011-08-11 13:21:46 UTC (rev 14145)
+++ branches/enterprise/JBPAPP_5_0/src/main/org/jboss/seam/web/MultipartRequestImpl.java	2011-08-11 14:14:38 UTC (rev 14146)
@@ -330,6 +330,14 @@
                   {
                      if (checkSequence(buffer, i, CR_LF))
                      {
+                        // Check next CR_LF available in the buffer
+                        if ( read <= i + CR_LF.length)
+                        {  /* if there is no space left for CRLF in the buffer, 
+                           force pushing of remaining (unread) bytes to the beginning of the buffer,
+                           read more bytes and try again */
+                           i = read;
+                           break;
+                        }
                         String param = (encoding == null) ? 
                                  new String(buffer, pos, i - pos - 1) :
                                  new String(buffer, pos, i - pos - 1, encoding);                        
@@ -420,16 +428,40 @@
             {
                // move the bytes that weren't read to the start of the buffer
                int bytesNotRead = read - pos;
-               System.arraycopy(buffer, pos, buffer, 0, bytesNotRead);               
-               read = input.read(buffer, bytesNotRead, buffer.length - bytesNotRead);
                
-               // Decrement loopCounter if no data was readable
-               if (read == 0)
+               if (buffer.length == bytesNotRead ) 
                {
-                  loopCounter--;
-               }
+                  //if no end of parameter value can be found in the buffer, we have to increase size of the buffer                                    
+                  
+                  byte[] buffer1 = new byte[buffer.length * 2];
+                  System.arraycopy(buffer, 0, buffer1, 0, buffer.length);
+                  buffer = buffer1;
+                  
+                  read = input.read(buffer, bytesNotRead, buffer.length - bytesNotRead);
+                  if (read == -1) 
+                  {
+                     break;// too bad - nothing more to read (EOF) and last parameter value couldn't be parsed
+                  }
+          
+                  read += bytesNotRead;
+                  } 
+                  else 
+                  {
+                     System.arraycopy(buffer, pos, buffer, 0, bytesNotRead);
+                     read = input.read(buffer, bytesNotRead, buffer.length - bytesNotRead);
+                  
+                     if (read == -1) 
+                     {
+                        break;
+                        // too bad - nothing more to read (EOF) and last parameter value couldn't be parsed
+                     }
+                     
+                     if(read == 0)
+                        loopCounter--;
+                     
+                     read += bytesNotRead;
+                  }
                
-               read += bytesNotRead;
             }
             else
             {

Modified: branches/enterprise/JBPAPP_5_0/src/test/unit/org/jboss/seam/test/unit/testng.xml
===================================================================
--- branches/enterprise/JBPAPP_5_0/src/test/unit/org/jboss/seam/test/unit/testng.xml	2011-08-11 13:21:46 UTC (rev 14145)
+++ branches/enterprise/JBPAPP_5_0/src/test/unit/org/jboss/seam/test/unit/testng.xml	2011-08-11 14:14:38 UTC (rev 14146)
@@ -64,6 +64,7 @@
    <test name="Seam Unit Tests - Filters">
      <classes>
         <class name="org.jboss.seam.test.unit.web.MultipartRequestTest" />
+        <class name="org.jboss.seam.test.unit.web.MultipartRequestImplTest" />
         <class name="org.jboss.seam.test.unit.web.IdentityRequestWrapperTest" />
      </classes>
    </test>

Added: branches/enterprise/JBPAPP_5_0/src/test/unit/org/jboss/seam/test/unit/web/MultipartRequestImplTest.java
===================================================================
--- branches/enterprise/JBPAPP_5_0/src/test/unit/org/jboss/seam/test/unit/web/MultipartRequestImplTest.java	                        (rev 0)
+++ branches/enterprise/JBPAPP_5_0/src/test/unit/org/jboss/seam/test/unit/web/MultipartRequestImplTest.java	2011-08-11 14:14:38 UTC (rev 14146)
@@ -0,0 +1,91 @@
+package org.jboss.seam.test.unit.web;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+import java.util.Map;
+import org.testng.annotations.Test;
+import org.jboss.seam.web.MultipartRequestImpl;
+import org.jboss.seam.mock.EnhancedMockHttpServletRequest;
+
+public class MultipartRequestImplTest
+{
+   private static final String CRLF = "\r\n";
+   private static final String HYPHENS = "--";
+
+   @Test
+   public void testParseRequestBasic() throws Throwable
+   {
+      String boundary = "boundary10"; //10 bytes
+
+      String data =
+         HYPHENS + boundary + CRLF +
+         "Content-Disposition: form-data; name=\"foo\"" + CRLF +
+         CRLF +
+         "bar" + CRLF +
+         HYPHENS + boundary + HYPHENS;
+          
+      byte[] dataBytes = data.getBytes("UTF-8");
+
+      EnhancedMockHttpServletRequest req = new EnhancedMockHttpServletRequest();
+      req.setContent(dataBytes);
+      req.setContentType("multipart/form-data; boundary=" + boundary);
+      MultipartRequestImpl r = new MultipartRequestImpl(req, false, 0);
+      Map m = r.getParameterMap();
+      assertNotNull(m);
+      assertEquals(r.getParameterValues("foo")[0], "bar");
+   }
+
+   @Test
+   public void testParseRequestBufferBoundary() throws Throwable
+   {
+
+      int bufferSize = 2048; // See MultipartRequestImpl
+
+      String boundary = "boundary10";
+
+      String paddingParameter =
+         HYPHENS + boundary + CRLF +
+         "Content-Disposition: form-data; name=\"padding\"" + CRLF +
+         CRLF;
+
+      String testParameter =
+         HYPHENS + boundary + CRLF +
+         "Content-Disposition: form-data; name=\"foo\"" + CRLF +
+         CRLF +
+         "bar" + CRLF +
+         HYPHENS + boundary + HYPHENS;
+
+      // let's put test parameter near the buffer boundary, from (bufferSize - 100) to (bufferSize + 100)
+      for (int i = -100; i < 100; i++)
+      {
+         StringBuffer buffer = new StringBuffer(bufferSize + 256);
+         buffer.append(paddingParameter);
+         int paddingSize = bufferSize - i - paddingParameter.length() - CRLF.length();
+         appendPaddingValue(buffer, paddingSize);
+         buffer.append(CRLF);
+         buffer.append(testParameter);
+         String data = buffer.toString();
+
+         byte[] dataBytes = data.getBytes("UTF-8");
+         EnhancedMockHttpServletRequest req = new EnhancedMockHttpServletRequest();
+         req.setContent(dataBytes);
+         req.setContentType("multipart/form-data; boundary=" + boundary);
+         MultipartRequestImpl r = new MultipartRequestImpl(req, false, 0);
+         Map m = r.getParameterMap();
+         assertNotNull(m);
+         assertEquals(r.getParameterValues("foo")[0], "bar");
+      }
+   }
+
+   private static StringBuffer appendPaddingValue(StringBuffer buffer, int length)
+   {
+      for (int i = 0; i < length; i++)
+      {
+         buffer.append("x");
+      }
+      return buffer;
+   }
+}



More information about the seam-commits mailing list