Skip to content

Commit

Permalink
Ignore empty entries when parsing MediaTypes and MimeTypes
Browse files Browse the repository at this point in the history
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
  • Loading branch information
sbrannen committed Jul 7, 2019
1 parent 7a7d410 commit efab6eb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
* @author Arjen Poutsma
* @author Rossen Stoyanchev
* @author Dimitrios Liapis
* @author Sam Brannen
* @since 4.0
*/
public abstract class MimeTypeUtils {
Expand Down Expand Up @@ -259,10 +260,11 @@ public static List<MimeType> parseMimeTypes(String mimeTypes) {
return Collections.emptyList();
}
return tokenize(mimeTypes).stream()
.map(MimeTypeUtils::parseMimeType).collect(Collectors.toList());
.filter(StringUtils::hasText)
.map(MimeTypeUtils::parseMimeType)
.collect(Collectors.toList());
}


/**
* Tokenize the given comma-separated string of {@code MimeType} objects
* into a {@code List<String>}. Unlike simple tokenization by ",", this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,13 @@ public void parseMimeTypes() {
assertEquals("Invalid amount of mime types", 0, mimeTypes.size());
}

@Test // gh-23241
public void parseMimeTypesWithTrailingComma() {
List<MimeType> mimeTypes = MimeTypeUtils.parseMimeTypes("text/plain, text/html,");
assertNotNull("No mime types returned", mimeTypes);
assertEquals("Incorrect number of mime types", 2, mimeTypes.size());
}

@Test // SPR-17459
public void parseMimeTypesWithQuotedParameters() {
testWithQuotedParameters("foo/bar;param=\",\"");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,6 +44,7 @@
* @author Rossen Stoyanchev
* @author Sebastien Deleuze
* @author Kazuki Shimizu
* @author Sam Brannen
* @since 3.0
* @see <a href="https://tools.ietf.org/html/rfc7231#section-3.1.1.1">
* HTTP 1.1: Semantics and Content, section 3.1.1.1</a>
Expand Down Expand Up @@ -553,7 +554,9 @@ public static List<MediaType> parseMediaTypes(@Nullable String mediaTypes) {
return Collections.emptyList();
}
return MimeTypeUtils.tokenize(mediaTypes).stream()
.map(MediaType::parseMediaType).collect(Collectors.toList());
.filter(StringUtils::hasText)
.map(MediaType::parseMediaType)
.collect(Collectors.toList());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,13 @@ public void parseMediaTypes() throws Exception {
assertEquals("Invalid amount of media types", 0, mediaTypes.size());
}

@Test // gh-23241
public void parseMediaTypesWithTrailingComma() {
List<MediaType> mediaTypes = MediaType.parseMediaTypes("text/plain, text/html, ");
assertNotNull("No media types returned", mediaTypes);
assertEquals("Incorrect number of media types", 2, mediaTypes.size());
}

@Test
public void compareTo() {
MediaType audioBasic = new MediaType("audio", "basic");
Expand Down

0 comments on commit efab6eb

Please sign in to comment.