From d8a338690b572dd84d4554c8ea8764e8d26cf4fd Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 4 Feb 2010 09:47:52 +0000 Subject: [PATCH] SPR-6786: added more checks for quality factors & charsets git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2912 50f2f4bb-b051-0410-bef5-90022cba6387 --- .../org/springframework/http/MediaType.java | 33 ++++++++++++------- .../springframework/http/MediaTypeTests.java | 15 +++++++++ 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/org.springframework.web/src/main/java/org/springframework/http/MediaType.java b/org.springframework.web/src/main/java/org/springframework/http/MediaType.java index dca496c5977..39db262e9fc 100644 --- a/org.springframework.web/src/main/java/org/springframework/http/MediaType.java +++ b/org.springframework.web/src/main/java/org/springframework/http/MediaType.java @@ -18,6 +18,7 @@ package org.springframework.http; import java.nio.charset.Charset; import java.util.ArrayList; +import java.util.BitSet; import java.util.Collection; import java.util.Collections; import java.util.Comparator; @@ -26,7 +27,6 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.BitSet; import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; @@ -56,7 +56,7 @@ public class MediaType implements Comparable { private static final String WILDCARD_TYPE = "*"; - private static final String PARAM_QUALITY_FACTORY = "q"; + private static final String PARAM_QUALITY_FACTOR = "q"; private static final String PARAM_CHARSET = "charset"; @@ -149,7 +149,7 @@ public class MediaType implements Comparable { * @throws IllegalArgumentException if any of the parameters contain illegal characters */ public MediaType(String type, String subtype, double qualityValue) { - this(type, subtype, Collections.singletonMap(PARAM_QUALITY_FACTORY, Double.toString(qualityValue))); + this(type, subtype, Collections.singletonMap(PARAM_QUALITY_FACTOR, Double.toString(qualityValue))); } /** @@ -172,10 +172,7 @@ public class MediaType implements Comparable { for (Map.Entry entry : parameters.entrySet()) { String attribute = entry.getKey(); String value = entry.getValue(); - Assert.hasLength(attribute, "pameter attribute must not be empty"); - Assert.hasLength(value, "pameter value must not be empty"); - checkToken(attribute); - checkTokenOrQuotedString(value); + checkParameters(attribute, value); m.put(attribute, value); } this.parameters = Collections.unmodifiableMap(m); @@ -200,10 +197,24 @@ public class MediaType implements Comparable { } } - private void checkTokenOrQuotedString(String s) { - if (!(s.startsWith("\"") && s.endsWith("\""))) { - checkToken(s); + private void checkParameters(String attribute, String value) { + Assert.hasLength(attribute, "parameter attribute must not be empty"); + Assert.hasLength(value, "parameter value must not be empty"); + checkToken(attribute); + if (PARAM_QUALITY_FACTOR.equals(attribute)) { + double d = Double.parseDouble(value); + Assert.isTrue(d >= 0D && d <= 1D, "Invalid quality value \"" + value + "\": should be between 0.0 and 1.0"); } + else if (PARAM_CHARSET.equals(attribute)) { + Charset.forName(value); + } + else if (!isQuotedString(value)) { + checkToken(value); + } + } + + private boolean isQuotedString(String s) { + return s.length() > 1 && s.startsWith("\"") && s.endsWith("\"") ; } /** Return the primary type. */ @@ -246,7 +257,7 @@ public class MediaType implements Comparable { * @return the quality factory */ public double getQualityValue() { - String qualityFactory = getParameter(PARAM_QUALITY_FACTORY); + String qualityFactory = getParameter(PARAM_QUALITY_FACTOR); return (qualityFactory != null ? Double.parseDouble(qualityFactory) : 1D); } diff --git a/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java b/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java index b262f201244..0a042fbb204 100644 --- a/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java +++ b/org.springframework.web/src/test/java/org/springframework/http/MediaTypeTests.java @@ -121,11 +121,26 @@ public class MediaTypeTests { MediaType.parseMediaType("audio/*;attr=v>alue"); } + @Test(expected = IllegalArgumentException.class) + public void parseMediaTypeIllegalQualityFactor() { + MediaType.parseMediaType("audio/basic;q=1.1"); + } + + @Test(expected = IllegalArgumentException.class) + public void parseMediaTypeIllegalCharset() { + MediaType.parseMediaType("text/html; charset=foo-bar"); + } + @Test public void parseMediaTypeQuotedParameterValue() { MediaType.parseMediaType("audio/*;attr=\"v>alue\""); } + @Test(expected = IllegalArgumentException.class) + public void parseMediaTypeIllegalQuotedParameterValue() { + MediaType.parseMediaType("audio/*;attr=\""); + } + @Test public void parseCharset() throws Exception { String s = "text/html; charset=iso-8859-1";