Skip to content

Commit 932f771

Browse files
committed
Improve performance of FormContentFilter
Improve the performance of `FormContentFilter` by checking directly if `contentType` is empty. This saves the need for an exception to thrown then immediately caught. Closes gh-23216
1 parent b3d56eb commit 932f771

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

spring-web/src/main/java/org/springframework/web/filter/FormContentFilter.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,17 @@ public InputStream getBody() throws IOException {
109109
}
110110

111111
private boolean shouldParse(HttpServletRequest request) {
112-
if (!HTTP_METHODS.contains(request.getMethod())) {
113-
return false;
114-
}
115-
try {
116-
MediaType mediaType = MediaType.parseMediaType(request.getContentType());
117-
return MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType);
118-
}
119-
catch (IllegalArgumentException ex) {
120-
return false;
112+
String contentType = request.getContentType();
113+
String method = request.getMethod();
114+
if (StringUtils.hasLength(contentType) && HTTP_METHODS.contains(method)) {
115+
try {
116+
MediaType mediaType = MediaType.parseMediaType(contentType);
117+
return MediaType.APPLICATION_FORM_URLENCODED.includes(mediaType);
118+
}
119+
catch (IllegalArgumentException ex) {
120+
}
121121
}
122+
return false;
122123
}
123124

124125

0 commit comments

Comments
 (0)