From ede8f1d334aaf6e7f5312fea09a56908f22b4f10 Mon Sep 17 00:00:00 2001 From: fengyubiao Date: Tue, 24 Mar 2020 11:32:10 +0800 Subject: [PATCH 1/3] MimeTypeUtils no longer cache MediaType.MULTIPART_FORM_DATA_VALUE --- .../springframework/util/MimeTypeUtils.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java index 89609925bc7d..c5b58b3e1d13 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java @@ -35,6 +35,7 @@ import java.util.function.Function; import java.util.stream.Collectors; +import org.springframework.http.MediaType; import org.springframework.lang.Nullable; /** @@ -181,6 +182,19 @@ public abstract class MimeTypeUtils { TEXT_XML = new MimeType("text", "xml"); } + /*** + * Whether need cache. + *

For example "multipart/form-data; boundary=----WebKitFormBoundarymKzwdDkWNDNzQFP0", + * this mimeType with random characters, wastes LRU cache space, resulting in severe performance degradation. + * @param mimeType the mime type + * @return whether need cache + */ + private static boolean ensureMimeTypeRequiredCache(String mimeType){ + if(mimeType.startsWith(MediaType.MULTIPART_FORM_DATA_VALUE + ";")) + return false; + return true; + } + /** * Parse the given String into a single {@code MimeType}. @@ -193,7 +207,9 @@ public static MimeType parseMimeType(String mimeType) { if (!StringUtils.hasLength(mimeType)) { throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty"); } - return cachedMimeTypes.get(mimeType); + if (ensureMimeTypeRequiredCache(mimeType)) + return cachedMimeTypes.get(mimeType); + return parseMimeTypeInternal(mimeType); } private static MimeType parseMimeTypeInternal(String mimeType) { From aa8bebb41672e87b076c5d146e0b8848ccbf4baf Mon Sep 17 00:00:00 2001 From: fengyubiao Date: Tue, 24 Mar 2020 12:11:45 +0800 Subject: [PATCH 2/3] fixed bug MimeTypeUtils compile fail --- .../src/main/java/org/springframework/util/MimeTypeUtils.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java index c5b58b3e1d13..31aa115bfce3 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java @@ -190,7 +190,8 @@ public abstract class MimeTypeUtils { * @return whether need cache */ private static boolean ensureMimeTypeRequiredCache(String mimeType){ - if(mimeType.startsWith(MediaType.MULTIPART_FORM_DATA_VALUE + ";")) + // TODO constant + if(mimeType.startsWith("multipart/form-data;")) return false; return true; } From 16574a293a5266bfdc793fd10b04283a9dbcda82 Mon Sep 17 00:00:00 2001 From: fengyubiao Date: Tue, 24 Mar 2020 12:16:05 +0800 Subject: [PATCH 3/3] fixed bug MimeTypeUtils compile fail --- .../src/main/java/org/springframework/util/MimeTypeUtils.java | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java index 31aa115bfce3..f7f2d1e3d3c4 100644 --- a/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java +++ b/spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java @@ -35,7 +35,6 @@ import java.util.function.Function; import java.util.stream.Collectors; -import org.springframework.http.MediaType; import org.springframework.lang.Nullable; /**