Skip to content

Commit efab6eb

Browse files
committed
Ignore empty entries when parsing MediaTypes and MimeTypes
Prior to Spring Framework 5.1.3, MimeTypeUtils.parseMimeTypes() and MediaType.parseMediaTypes() ignored empty entries, but 5.1.3 introduced a regression in that an empty entry -- for example, due to a trailing comma in the list of media types in an HTTP Accept header -- would result in a "406 Not Acceptable" response status. This commit fixes this by filtering out empty entries before parsing them into MimeType and MediaType instances. Empty entries are therefore effectively ignored. Fixes gh-23241
1 parent 7a7d410 commit efab6eb

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

spring-core/src/main/java/org/springframework/util/MimeTypeUtils.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
* @author Arjen Poutsma
3939
* @author Rossen Stoyanchev
4040
* @author Dimitrios Liapis
41+
* @author Sam Brannen
4142
* @since 4.0
4243
*/
4344
public abstract class MimeTypeUtils {
@@ -259,10 +260,11 @@ public static List<MimeType> parseMimeTypes(String mimeTypes) {
259260
return Collections.emptyList();
260261
}
261262
return tokenize(mimeTypes).stream()
262-
.map(MimeTypeUtils::parseMimeType).collect(Collectors.toList());
263+
.filter(StringUtils::hasText)
264+
.map(MimeTypeUtils::parseMimeType)
265+
.collect(Collectors.toList());
263266
}
264267

265-
266268
/**
267269
* Tokenize the given comma-separated string of {@code MimeType} objects
268270
* into a {@code List<String>}. Unlike simple tokenization by ",", this

spring-core/src/test/java/org/springframework/util/MimeTypeTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,13 @@ public void parseMimeTypes() {
277277
assertEquals("Invalid amount of mime types", 0, mimeTypes.size());
278278
}
279279

280+
@Test // gh-23241
281+
public void parseMimeTypesWithTrailingComma() {
282+
List<MimeType> mimeTypes = MimeTypeUtils.parseMimeTypes("text/plain, text/html,");
283+
assertNotNull("No mime types returned", mimeTypes);
284+
assertEquals("Incorrect number of mime types", 2, mimeTypes.size());
285+
}
286+
280287
@Test // SPR-17459
281288
public void parseMimeTypesWithQuotedParameters() {
282289
testWithQuotedParameters("foo/bar;param=\",\"");

spring-web/src/main/java/org/springframework/http/MediaType.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -44,6 +44,7 @@
4444
* @author Rossen Stoyanchev
4545
* @author Sebastien Deleuze
4646
* @author Kazuki Shimizu
47+
* @author Sam Brannen
4748
* @since 3.0
4849
* @see <a href="https://tools.ietf.org/html/rfc7231#section-3.1.1.1">
4950
* HTTP 1.1: Semantics and Content, section 3.1.1.1</a>
@@ -553,7 +554,9 @@ public static List<MediaType> parseMediaTypes(@Nullable String mediaTypes) {
553554
return Collections.emptyList();
554555
}
555556
return MimeTypeUtils.tokenize(mediaTypes).stream()
556-
.map(MediaType::parseMediaType).collect(Collectors.toList());
557+
.filter(StringUtils::hasText)
558+
.map(MediaType::parseMediaType)
559+
.collect(Collectors.toList());
557560
}
558561

559562
/**

spring-web/src/test/java/org/springframework/http/MediaTypeTests.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ public void parseMediaTypes() throws Exception {
143143
assertEquals("Invalid amount of media types", 0, mediaTypes.size());
144144
}
145145

146+
@Test // gh-23241
147+
public void parseMediaTypesWithTrailingComma() {
148+
List<MediaType> mediaTypes = MediaType.parseMediaTypes("text/plain, text/html, ");
149+
assertNotNull("No media types returned", mediaTypes);
150+
assertEquals("Incorrect number of media types", 2, mediaTypes.size());
151+
}
152+
146153
@Test
147154
public void compareTo() {
148155
MediaType audioBasic = new MediaType("audio", "basic");

0 commit comments

Comments
 (0)