From 734d18fb93da4f363acca96bd78ca6ca0f4b577b Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Fri, 22 Jan 2016 14:07:51 -0700 Subject: [PATCH] 486394 - MultipartConfig.fileSizeThreshold default of 0 should always create a file + Refactored fileSizeThreshold logic so that a configuration value of 0 is always create file (negative) is never create file positive is tested against filesize --- .../util/MultiPartInputStreamParser.java | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartInputStreamParser.java b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartInputStreamParser.java index 946d708c307..95fbcd635ef 100644 --- a/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartInputStreamParser.java +++ b/jetty-util/src/main/java/org/eclipse/jetty/util/MultiPartInputStreamParser.java @@ -40,6 +40,7 @@ import java.util.List; import java.util.Locale; import javax.servlet.MultipartConfigElement; +import javax.servlet.annotation.MultipartConfig; import javax.servlet.http.Part; import org.eclipse.jetty.util.log.Log; @@ -112,7 +113,7 @@ public class MultiPartInputStreamParser if (MultiPartInputStreamParser.this._config.getMaxFileSize() > 0 && _size + 1 > MultiPartInputStreamParser.this._config.getMaxFileSize()) throw new IllegalStateException ("Multipart Mime part "+_name+" exceeds max filesize"); - if (MultiPartInputStreamParser.this._config.getFileSizeThreshold() > 0 && _size + 1 > MultiPartInputStreamParser.this._config.getFileSizeThreshold() && _file==null) + if (_file == null && MultiPartInputStreamParser.this.isFileNeeded(_size + 1)) createFile(); _out.write(b); @@ -125,7 +126,7 @@ public class MultiPartInputStreamParser if (MultiPartInputStreamParser.this._config.getMaxFileSize() > 0 && _size + length > MultiPartInputStreamParser.this._config.getMaxFileSize()) throw new IllegalStateException ("Multipart Mime part "+_name+" exceeds max filesize"); - if (MultiPartInputStreamParser.this._config.getFileSizeThreshold() > 0 && _size + length > MultiPartInputStreamParser.this._config.getFileSizeThreshold() && _file==null) + if (_file == null && MultiPartInputStreamParser.this.isFileNeeded(_size + length)) createFile(); _out.write(bytes, offset, length); @@ -429,6 +430,31 @@ public class MultiPartInputStreamParser } + /** + * Test to determine if the file content sizes exceeds the {@link MultipartConfig#fileSizeThreshold()} + * necessary to produce a file on disk + *

+ * Configured {@link MultipartConfig#fileSizeThreshold()} of 0 is always true, negative is always false, + * all other configurations are tested against size parameter + * + * @param size the file size to test against. + * @return true if file is needed, false otherwise + */ + protected boolean isFileNeeded(long size) + { + if (_config.getFileSizeThreshold() < 0) + { + // Negative file size threshold means no file, ever + return false; + } + if (_config.getFileSizeThreshold() == 0) + { + // 0 means always create a file + return true; + } + return size > _config.getFileSizeThreshold(); + } + /** * Parse, if necessary, the multipart stream. *