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
This commit is contained in:
Sam Brannen
2019-07-07 12:39:46 +02:00
parent 7a7d4109ac
commit efab6eb55d
4 changed files with 23 additions and 4 deletions

View File

@@ -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.
@@ -44,6 +44,7 @@ import org.springframework.util.StringUtils;
* @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>
@@ -553,7 +554,9 @@ public class MediaType extends MimeType implements Serializable {
return Collections.emptyList();
}
return MimeTypeUtils.tokenize(mediaTypes).stream()
.map(MediaType::parseMediaType).collect(Collectors.toList());
.filter(StringUtils::hasText)
.map(MediaType::parseMediaType)
.collect(Collectors.toList());
}
/**