You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2019/05/03 19:00:35 UTC

[tomcat] 02/02: Align fork of Commons FileUpload with the 9.0.x copy

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 5b9399df512f5340bdf29573200aaa3eacbb0f8a
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Fri May 3 19:59:50 2019 +0100

    Align fork of Commons FileUpload with the 9.0.x copy
---
 .../util/http/fileupload/FileUploadBase.java       | 27 ++++++++++++----------
 .../tomcat/util/http/fileupload/IOUtils.java       | 16 ++++++-------
 .../util/http/fileupload/ParameterParser.java      | 10 ++++----
 .../util/http/fileupload/disk/DiskFileItem.java    | 11 ++++++---
 .../tomcat/util/http/fileupload/util/Streams.java  |  2 +-
 .../http/fileupload/util/mime/MimeUtility.java     |  2 +-
 webapps/docs/changelog.xml                         |  4 ++++
 7 files changed, 41 insertions(+), 31 deletions(-)

diff --git a/java/org/apache/tomcat/util/http/fileupload/FileUploadBase.java b/java/org/apache/tomcat/util/http/fileupload/FileUploadBase.java
index eb5a487..486434a 100644
--- a/java/org/apache/tomcat/util/http/fileupload/FileUploadBase.java
+++ b/java/org/apache/tomcat/util/http/fileupload/FileUploadBase.java
@@ -279,6 +279,7 @@ public abstract class FileUploadBase {
         try {
             FileItemIterator iter = getItemIterator(ctx);
             FileItemFactory fac = getFileItemFactory();
+            final byte[] buffer = new byte[Streams.DEFAULT_BUFFER_SIZE];
             if (fac == null) {
                 throw new NullPointerException("No FileItemFactory has been set.");
             }
@@ -290,7 +291,7 @@ public abstract class FileUploadBase {
                                                    item.isFormField(), fileName);
                 items.add(fileItem);
                 try {
-                    Streams.copy(item.openStream(), fileItem.getOutputStream(), true);
+                    Streams.copy(item.openStream(), fileItem.getOutputStream(), true, buffer);
                 } catch (FileUploadIOException e) {
                     throw (FileUploadException) e.getCause();
                 } catch (IOException e) {
@@ -403,8 +404,7 @@ public abstract class FileUploadBase {
                 ParameterParser parser = new ParameterParser();
                 parser.setLowerCaseNames(true);
                 // Parameter parser can handle null input
-                Map<String,String> params =
-                    parser.parse(pContentDisposition, ';');
+                Map<String, String> params = parser.parse(pContentDisposition, ';');
                 if (params.containsKey("filename")) {
                     fileName = params.get("filename");
                     if (fileName != null) {
@@ -446,7 +446,7 @@ public abstract class FileUploadBase {
             ParameterParser parser = new ParameterParser();
             parser.setLowerCaseNames(true);
             // Parameter parser can handle null input
-            Map<String,String> params = parser.parse(pContentDisposition, ';');
+            Map<String, String> params = parser.parse(pContentDisposition, ';');
             fieldName = params.get("name");
             if (fieldName != null) {
                 fieldName = fieldName.trim();
@@ -606,20 +606,23 @@ public abstract class FileUploadBase {
                 fieldName = pFieldName;
                 contentType = pContentType;
                 formField = pFormField;
-                final ItemInputStream itemStream = multi.newInputStream();
-                InputStream istream = itemStream;
-                if (fileSizeMax != -1) {
+                if (fileSizeMax != -1) { // Check if limit is already exceeded
                     if (pContentLength != -1
-                            &&  pContentLength > fileSizeMax) {
+                            && pContentLength > fileSizeMax) {
                         FileSizeLimitExceededException e =
-                            new FileSizeLimitExceededException(
-                                String.format("The field %s exceeds its maximum permitted size of %s bytes.",
-                                        fieldName, Long.valueOf(fileSizeMax)),
-                                pContentLength, fileSizeMax);
+                                new FileSizeLimitExceededException(
+                                        String.format("The field %s exceeds its maximum permitted size of %s bytes.",
+                                                       fieldName, Long.valueOf(fileSizeMax)),
+                                        pContentLength, fileSizeMax);
                         e.setFileName(pName);
                         e.setFieldName(pFieldName);
                         throw new FileUploadIOException(e);
                     }
+                }
+                // OK to construct stream now
+                final ItemInputStream itemStream = multi.newInputStream();
+                InputStream istream = itemStream;
+                if (fileSizeMax != -1) {
                     istream = new LimitedInputStream(istream, fileSizeMax) {
                         @Override
                         protected void raiseError(long pSizeMax, long pCount)
diff --git a/java/org/apache/tomcat/util/http/fileupload/IOUtils.java b/java/org/apache/tomcat/util/http/fileupload/IOUtils.java
index 2de282e..069b0ae 100644
--- a/java/org/apache/tomcat/util/http/fileupload/IOUtils.java
+++ b/java/org/apache/tomcat/util/http/fileupload/IOUtils.java
@@ -58,20 +58,18 @@ public class IOUtils {
     // Writer. Each method should take at least one of these as a parameter,
     // or return one of them.
 
-    private static final int EOF = -1;
-
     /**
-     * The default buffer size ({@value}) to use for
-     * {@link #copyLarge(InputStream, OutputStream)}
+     * Represents the end-of-file (or stream).
+     * @since 2.5 (made public)
      */
-    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
+    public static final int EOF = -1;
+
 
     /**
-     * Instances should NOT be constructed in standard programming.
+     * The default buffer size ({@value}) to use for
+     * {@link #copyLarge(InputStream, OutputStream)}.
      */
-    public IOUtils() {
-        super();
-    }
+    private static final int DEFAULT_BUFFER_SIZE = 1024 * 4;
 
     /**
      * Closes a <code>Closeable</code> unconditionally.
diff --git a/java/org/apache/tomcat/util/http/fileupload/ParameterParser.java b/java/org/apache/tomcat/util/http/fileupload/ParameterParser.java
index e0ccd91..061be67 100644
--- a/java/org/apache/tomcat/util/http/fileupload/ParameterParser.java
+++ b/java/org/apache/tomcat/util/http/fileupload/ParameterParser.java
@@ -226,7 +226,7 @@ public class ParameterParser {
      *
      * @return a map of name/value pairs
      */
-    public Map<String,String> parse(final String str, char[] separators) {
+    public Map<String, String> parse(final String str, char[] separators) {
         if (separators == null || separators.length == 0) {
             return new HashMap<>();
         }
@@ -253,7 +253,7 @@ public class ParameterParser {
      *
      * @return a map of name/value pairs
      */
-    public Map<String,String> parse(final String str, char separator) {
+    public Map<String, String> parse(final String str, char separator) {
         if (str == null) {
             return new HashMap<>();
         }
@@ -270,7 +270,7 @@ public class ParameterParser {
      *
      * @return a map of name/value pairs
      */
-    public Map<String,String> parse(final char[] charArray, char separator) {
+    public Map<String, String> parse(final char[] charArray, char separator) {
         if (charArray == null) {
             return new HashMap<>();
         }
@@ -289,7 +289,7 @@ public class ParameterParser {
      *
      * @return a map of name/value pairs
      */
-    public Map<String,String> parse(
+    public Map<String, String> parse(
         final char[] charArray,
         int offset,
         int length,
@@ -298,7 +298,7 @@ public class ParameterParser {
         if (charArray == null) {
             return new HashMap<>();
         }
-        HashMap<String,String> params = new HashMap<>();
+        HashMap<String, String> params = new HashMap<>();
         this.chars = charArray;
         this.pos = offset;
         this.len = length;
diff --git a/java/org/apache/tomcat/util/http/fileupload/disk/DiskFileItem.java b/java/org/apache/tomcat/util/http/fileupload/disk/DiskFileItem.java
index 87f8a8c..3992162 100644
--- a/java/org/apache/tomcat/util/http/fileupload/disk/DiskFileItem.java
+++ b/java/org/apache/tomcat/util/http/fileupload/disk/DiskFileItem.java
@@ -223,7 +223,7 @@ public class DiskFileItem
         ParameterParser parser = new ParameterParser();
         parser.setLowerCaseNames(true);
         // Parameter parser can handle null input
-        Map<String,String> params = parser.parse(getContentType(), ';');
+        Map<String, String> params = parser.parse(getContentType(), ';');
         return params.get("charset");
     }
 
@@ -391,6 +391,12 @@ public class DiskFileItem
                  * in a temporary location so move it to the
                  * desired file.
                  */
+                if (file.exists()) {
+                    if (!file.delete()) {
+                        throw new FileUploadException(
+                                "Cannot write uploaded file to disk!");
+                    }
+                }
                 if (!outputFile.renameTo(file)) {
                     BufferedInputStream in = null;
                     BufferedOutputStream out = null;
@@ -569,8 +575,7 @@ public class DiskFileItem
                 tempDir = new File(System.getProperty("java.io.tmpdir"));
             }
 
-            String tempFileName =
-                    String.format("upload_%s_%s.tmp", UID, getUniqueId());
+            String tempFileName = String.format("upload_%s_%s.tmp", UID, getUniqueId());
 
             tempFile = new File(tempDir, tempFileName);
         }
diff --git a/java/org/apache/tomcat/util/http/fileupload/util/Streams.java b/java/org/apache/tomcat/util/http/fileupload/util/Streams.java
index d755d7b..feeec94 100644
--- a/java/org/apache/tomcat/util/http/fileupload/util/Streams.java
+++ b/java/org/apache/tomcat/util/http/fileupload/util/Streams.java
@@ -41,7 +41,7 @@ public final class Streams {
      * Default buffer size for use in
      * {@link #copy(InputStream, OutputStream, boolean)}.
      */
-    private static final int DEFAULT_BUFFER_SIZE = 8192;
+    public static final int DEFAULT_BUFFER_SIZE = 8192;
 
     /**
      * Copies the contents of the given {@link InputStream}
diff --git a/java/org/apache/tomcat/util/http/fileupload/util/mime/MimeUtility.java b/java/org/apache/tomcat/util/http/fileupload/util/mime/MimeUtility.java
index 7e45525..b5b4cdd 100644
--- a/java/org/apache/tomcat/util/http/fileupload/util/mime/MimeUtility.java
+++ b/java/org/apache/tomcat/util/http/fileupload/util/mime/MimeUtility.java
@@ -101,7 +101,7 @@ public final class MimeUtility {
     public static String decodeText(String text) throws UnsupportedEncodingException {
         // if the text contains any encoded tokens, those tokens will be marked with "=?".  If the
         // source string doesn't contain that sequent, no decoding is required.
-        if (text.indexOf(ENCODED_TOKEN_MARKER) < 0) {
+        if (!text.contains(ENCODED_TOKEN_MARKER)) {
             return text;
         }
 
diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml
index 565a3ed..72c1424 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -135,6 +135,10 @@
         Update the internal fork of Apache Commons Pool 2 to 0664f4d
         (2019-04-30) to pick up some enhancements and bug fixes. (markt)
       </update>
+      <update>
+        Update the internal fork of Apache Commons FileUpload to 41e4047
+        (2019-04-24) pick up some enhancements. (markt)
+      </update>
     </changelog>
   </subsection>
 </section>


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