struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlen...@apache.org
Subject [1/2] struts git commit: WW-4839 JakartaStreamMultiPartRequest Should Honor "struts.multipart.maxSize"
Date Mon, 07 Aug 2017 06:18:52 GMT
Repository: struts
Updated Branches:
  refs/heads/master 41c633581 -> ff6148795


WW-4839 JakartaStreamMultiPartRequest Should Honor "struts.multipart.maxSize"


Project: http://git-wip-us.apache.org/repos/asf/struts/repo
Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/eb013716
Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/eb013716
Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/eb013716

Branch: refs/heads/master
Commit: eb0137168f0c0e09a1ef6532965ffc1634a36291
Parents: c9d20b5
Author: Aaron Johnson <johnson.aaron5@gmail.com>
Authored: Fri Aug 4 15:58:23 2017 -0500
Committer: Aaron Johnson <johnson.aaron5@gmail.com>
Committed: Fri Aug 4 15:58:23 2017 -0500

----------------------------------------------------------------------
 .../JakartaStreamMultiPartRequest.java          |  3 +
 .../JakartaStreamMultiPartRequestTest.java      | 58 ++++++++++++++++++++
 2 files changed, 61 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/struts/blob/eb013716/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java
b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java
index 4703338..b8c1a33 100644
--- a/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java
+++ b/core/src/main/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequest.java
@@ -191,6 +191,9 @@ public class JakartaStreamMultiPartRequest extends AbstractMultiPartRequest
{
             // Interface with Commons FileUpload API
             // Using the Streaming API
             ServletFileUpload servletFileUpload = new ServletFileUpload();
+            if (maxSizeProvided) {
+                servletFileUpload.setSizeMax(maxSize);
+            }
             FileItemIterator i = servletFileUpload.getItemIterator(request);
 
             // Iterate the file items

http://git-wip-us.apache.org/repos/asf/struts/blob/eb013716/core/src/test/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequestTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequestTest.java
b/core/src/test/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequestTest.java
new file mode 100644
index 0000000..04195cb
--- /dev/null
+++ b/core/src/test/java/org/apache/struts2/dispatcher/multipart/JakartaStreamMultiPartRequestTest.java
@@ -0,0 +1,58 @@
+package org.apache.struts2.dispatcher.multipart;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import javax.servlet.ServletInputStream;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.struts2.dispatcher.LocalizedMessage;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.springframework.mock.web.DelegatingServletInputStream;
+import org.testng.Assert;
+
+public class JakartaStreamMultiPartRequestTest {
+
+    private JakartaStreamMultiPartRequest multiPart;
+    private Path tempDir;
+    
+    @Before
+    public void initialize() {
+        multiPart = new JakartaStreamMultiPartRequest();
+        tempDir = Paths.get("target", "multi-part-test");
+    }
+    
+    /**
+     * Number of bytes in files greater than 2GB overflow the {@code int} primative.
+     * The {@link HttpServletRequest#getContentLength()} returns {@literal -1} 
+     * when the header is not present or the size is greater than {@link Integer#MAX_VALUE}.
+     * @throws IOException 
+     */
+    @Test
+    public void unknownContentLength() throws IOException {
+        HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
+        Mockito.when(request.getContentType()).thenReturn("multipart/form-data; charset=utf-8;
boundary=__X_BOUNDARY__");
+        Mockito.when(request.getMethod()).thenReturn("POST");
+        Mockito.when(request.getContentLength()).thenReturn(Integer.valueOf(-1));
+        StringBuilder entity = new StringBuilder();
+        entity.append("\r\n--__X_BOUNDARY__\r\n");
+        entity.append("Content-Disposition: form-data; name=\"upload\"; filename=\"test.csv\"\r\n");
+        entity.append("Content-Type: text/csv\r\n\r\n1,2\r\n\r\n");
+        entity.append("--__X_BOUNDARY__\r\n");
+        entity.append("Content-Disposition: form-data; name=\"upload2\"; filename=\"test2.csv\"\r\n");
+        entity.append("Content-Type: text/csv\r\n\r\n3,4\r\n\r\n");
+        entity.append("--__X_BOUNDARY__--\r\n");
+        Mockito.when(request.getInputStream()).thenReturn(new DelegatingServletInputStream(new
ByteArrayInputStream(entity.toString().getBytes(StandardCharsets.UTF_8))));
+        multiPart.setMaxSize("4");
+        multiPart.parse(request, tempDir.toString());
+        LocalizedMessage next = multiPart.getErrors().iterator().next();
+        Assert.assertEquals(next.getTextKey(), "struts.messages.upload.error.SizeLimitExceededException");
+    }
+}


Mime
View raw message