You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2019/10/17 17:49:56 UTC

[tomcat] 01/05: Refactor Vary parser to the more generic TokenList parser

This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 7.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit 463b5caab72a58eba93844fe0853a0e909936094
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  | 36 +++-------------------
 .../parser/{TestVary.java => TestTokenList.java}   |  9 +++++-
 4 files changed, 29 insertions(+), 39 deletions(-)

diff --git a/java/org/apache/tomcat/util/http/ResponseUtil.java b/java/org/apache/tomcat/util/http/ResponseUtil.java
index 330cb11..1c40d3b 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 79%
copy from java/org/apache/tomcat/util/http/parser/Vary.java
copy to java/org/apache/tomcat/util/http/parser/TokenList.java
index 16c65ed..facd2ad 100644
--- a/java/org/apache/tomcat/util/http/parser/Vary.java
+++ b/java/org/apache/tomcat/util/http/parser/TokenList.java
@@ -17,20 +17,29 @@
 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;
 
 import org.apache.tomcat.util.http.parser.HttpParser.SkipResult;
 
-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 16c65ed..e064620 100644
--- a/java/org/apache/tomcat/util/http/parser/Vary.java
+++ b/java/org/apache/tomcat/util/http/parser/Vary.java
@@ -18,11 +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;
 
-import org.apache.tomcat.util.http.parser.HttpParser.SkipResult;
-
+/**
+ * @deprecated  Use {@link TokenList}.
+ */
+@Deprecated
 public class Vary {
 
     private Vary() {
@@ -31,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 fc3a1a9..74053bb 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 {
@@ -127,10 +127,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<String>();
         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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org