This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/tomcat.git
commit 101ac3947aacc8ded56d69f78090418fa77d8dbb Author: Mark Thomas <ma...@apache.org> AuthorDate: Wed Oct 16 17:38:53 2019 +0100 Refactor Vary parser to the more generic TokenList parser --- java/org/apache/tomcat/util/http/ResponseUtil.java | 4 +-- .../util/http/parser/{Vary.java => TokenList.java} | 19 ++++++++---- java/org/apache/tomcat/util/http/parser/Vary.java | 34 ++++------------------ .../parser/{TestVary.java => TestTokenList.java} | 9 +++++- 4 files changed, 29 insertions(+), 37 deletions(-) diff --git a/java/org/apache/tomcat/util/http/ResponseUtil.java b/java/org/apache/tomcat/util/http/ResponseUtil.java index 025bd49..295e7b7 100644 --- a/java/org/apache/tomcat/util/http/ResponseUtil.java +++ b/java/org/apache/tomcat/util/http/ResponseUtil.java @@ -27,7 +27,7 @@ import java.util.Set; import javax.servlet.http.HttpServletResponse; -import org.apache.tomcat.util.http.parser.Vary; +import org.apache.tomcat.util.http.parser.TokenList; public class ResponseUtil { @@ -81,7 +81,7 @@ public class ResponseUtil { for (String varyHeader : varyHeaders) { StringReader input = new StringReader(varyHeader); try { - Vary.parseVary(input, fieldNames); + TokenList.parseTokenList(input, fieldNames); } catch (IOException ioe) { // Should never happen } diff --git a/java/org/apache/tomcat/util/http/parser/Vary.java b/java/org/apache/tomcat/util/http/parser/TokenList.java similarity index 78% copy from java/org/apache/tomcat/util/http/parser/Vary.java copy to java/org/apache/tomcat/util/http/parser/TokenList.java index 130ddcf..9ba88b0 100644 --- a/java/org/apache/tomcat/util/http/parser/Vary.java +++ b/java/org/apache/tomcat/util/http/parser/TokenList.java @@ -17,18 +17,27 @@ package org.apache.tomcat.util.http.parser; import java.io.IOException; -import java.io.StringReader; +import java.io.Reader; +import java.util.Collection; import java.util.Locale; -import java.util.Set; -public class Vary { +public class TokenList { - private Vary() { + private TokenList() { // Utility class. Hide default constructor. } - public static void parseVary(StringReader input, Set<String> result) throws IOException { + /** + * Parses a header of the form 1#token. + * + * @param input The header to parse + * @param result The Collection (usually a list of a set) to which the + * parsed token should be added + * + * @throws IOException If an I/O error occurs reading the header + */ + public static void parseTokenList(Reader input, Collection<String> result) throws IOException { do { String fieldName = HttpParser.readToken(input); diff --git a/java/org/apache/tomcat/util/http/parser/Vary.java b/java/org/apache/tomcat/util/http/parser/Vary.java index 130ddcf..e064620 100644 --- a/java/org/apache/tomcat/util/http/parser/Vary.java +++ b/java/org/apache/tomcat/util/http/parser/Vary.java @@ -18,9 +18,12 @@ package org.apache.tomcat.util.http.parser; import java.io.IOException; import java.io.StringReader; -import java.util.Locale; import java.util.Set; +/** + * @deprecated Use {@link TokenList}. + */ +@Deprecated public class Vary { private Vary() { @@ -29,33 +32,6 @@ public class Vary { public static void parseVary(StringReader input, Set<String> result) throws IOException { - - do { - String fieldName = HttpParser.readToken(input); - if (fieldName == null) { - // Invalid field-name, skip to the next one - HttpParser.skipUntil(input, 0, ','); - continue; - } - - if (fieldName.length() == 0) { - // No more data to read - break; - } - - SkipResult skipResult = HttpParser.skipConstant(input, ","); - if (skipResult == SkipResult.EOF) { - // EOF - result.add(fieldName.toLowerCase(Locale.ENGLISH)); - break; - } else if (skipResult == SkipResult.FOUND) { - result.add(fieldName.toLowerCase(Locale.ENGLISH)); - continue; - } else { - // Not a token - ignore it - HttpParser.skipUntil(input, 0, ','); - continue; - } - } while (true); + TokenList.parseTokenList(input, result); } } diff --git a/test/org/apache/tomcat/util/http/parser/TestVary.java b/test/org/apache/tomcat/util/http/parser/TestTokenList.java similarity index 93% rename from test/org/apache/tomcat/util/http/parser/TestVary.java rename to test/org/apache/tomcat/util/http/parser/TestTokenList.java index ae0bc6a..8405bda 100644 --- a/test/org/apache/tomcat/util/http/parser/TestVary.java +++ b/test/org/apache/tomcat/util/http/parser/TestTokenList.java @@ -25,7 +25,7 @@ import java.util.Set; import org.junit.Assert; import org.junit.Test; -public class TestVary { +public class TestTokenList { @Test public void testAll() throws IOException { @@ -125,10 +125,17 @@ public class TestVary { } + @SuppressWarnings("deprecation") private void doTestVary(String input, Set<String> expected) throws IOException { StringReader reader = new StringReader(input); Set<String> result = new HashSet<>(); Vary.parseVary(reader, result); Assert.assertEquals(expected, result); + + // Can't use reset(). Parser uses marks. + reader = new StringReader(input); + result.clear(); + TokenList.parseTokenList(reader, result); + Assert.assertEquals(expected, result); } } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org