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 13:52:59 UTC

[tomcat] branch 8.5.x updated (57c6917 -> 8c789b3)

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

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


    from 57c6917  Fix BZ number
     new 8b2d892  Refactor Vary parser to the more generic TokenList parser
     new 30d46a4  Add a case sensitive / insensitive option to the token list parser
     new 8c789b3  Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=63824

The 3 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 java/org/apache/coyote/http11/Http11Processor.java | 28 +++++++++---
 java/org/apache/tomcat/util/http/ResponseUtil.java |  4 +-
 .../util/http/parser/{Vary.java => TokenList.java} | 50 +++++++++++++++++++---
 java/org/apache/tomcat/util/http/parser/Vary.java  | 34 +++------------
 .../parser/{TestVary.java => TestTokenList.java}   |  9 +++-
 5 files changed, 79 insertions(+), 46 deletions(-)
 copy java/org/apache/tomcat/util/http/parser/{Vary.java => TokenList.java} (50%)
 rename test/org/apache/tomcat/util/http/parser/{TestVary.java => TestTokenList.java} (93%)


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


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

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 8b2d892e6544beb33b61ebbe850bd44c1402a882
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 6921f32..36ac977 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<>();
         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


[tomcat] 02/03: Add a case sensitive / insensitive option to the token list parser

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 30d46a4a76145902f94e046e76098d812723eeb9
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Wed Oct 16 17:45:25 2019 +0100

    Add a case sensitive / insensitive option to the token list parser
---
 .../apache/tomcat/util/http/parser/TokenList.java  | 35 +++++++++++++++++++---
 1 file changed, 31 insertions(+), 4 deletions(-)

diff --git a/java/org/apache/tomcat/util/http/parser/TokenList.java b/java/org/apache/tomcat/util/http/parser/TokenList.java
index 9ba88b0..49e50a5 100644
--- a/java/org/apache/tomcat/util/http/parser/TokenList.java
+++ b/java/org/apache/tomcat/util/http/parser/TokenList.java
@@ -29,15 +29,34 @@ public class TokenList {
 
 
     /**
-     * Parses a header of the form 1#token.
+     * Parses a header of the form 1#token, forcing all parsed values to lower
+     * case. This is typically used when header values are case-insensitive.
      *
      * @param input  The header to parse
      * @param result The Collection (usually a list of a set) to which the
-     *                  parsed token should be added
+     *                   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 {
+        parseTokenList(input, true, result);
+    }
+
+
+    /**
+     * Parses a header of the form 1#token.
+     *
+     * @param input          The header to parse
+     * @param forceLowerCase Should parsed tokens be forced to lower case? This
+     *                           is intended for headers where the values are
+     *                           case-insensitive
+     * @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, boolean forceLowerCase, Collection<String> result)
+            throws IOException {
 
         do {
             String fieldName = HttpParser.readToken(input);
@@ -55,10 +74,18 @@ public class TokenList {
             SkipResult skipResult = HttpParser.skipConstant(input, ",");
             if (skipResult == SkipResult.EOF) {
                 // EOF
-                result.add(fieldName.toLowerCase(Locale.ENGLISH));
+                if (forceLowerCase) {
+                    result.add(fieldName.toLowerCase(Locale.ENGLISH));
+                } else {
+                    result.add(fieldName);
+                }
                 break;
             } else if (skipResult == SkipResult.FOUND) {
-                result.add(fieldName.toLowerCase(Locale.ENGLISH));
+                if (forceLowerCase) {
+                    result.add(fieldName.toLowerCase(Locale.ENGLISH));
+                } else {
+                    result.add(fieldName);
+                }
                 continue;
             } else {
                 // Not a token - ignore it


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[tomcat] 03/03: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=63824

Posted by ma...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

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

commit 8c789b3d59122229de8093ed5070544a734cddce
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Oct 17 09:58:41 2019 +0100

    Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=63824
    
    Correctly parse the Connection header when checking to see if the
    "close" option is present.
---
 java/org/apache/coyote/http11/Http11Processor.java | 28 ++++++++++++++++------
 1 file changed, 21 insertions(+), 7 deletions(-)

diff --git a/java/org/apache/coyote/http11/Http11Processor.java b/java/org/apache/coyote/http11/Http11Processor.java
index 6072602..10c8709 100644
--- a/java/org/apache/coyote/http11/Http11Processor.java
+++ b/java/org/apache/coyote/http11/Http11Processor.java
@@ -18,8 +18,10 @@ package org.apache.coyote.http11;
 
 import java.io.IOException;
 import java.io.InterruptedIOException;
+import java.io.StringReader;
 import java.nio.ByteBuffer;
 import java.util.Enumeration;
+import java.util.HashSet;
 import java.util.Locale;
 import java.util.Map;
 import java.util.Set;
@@ -53,6 +55,7 @@ import org.apache.tomcat.util.buf.MessageBytes;
 import org.apache.tomcat.util.http.FastHttpDateFormat;
 import org.apache.tomcat.util.http.MimeHeaders;
 import org.apache.tomcat.util.http.parser.HttpParser;
+import org.apache.tomcat.util.http.parser.TokenList;
 import org.apache.tomcat.util.log.UserDataHelper;
 import org.apache.tomcat.util.net.AbstractEndpoint;
 import org.apache.tomcat.util.net.AbstractEndpoint.Handler.SocketState;
@@ -1298,7 +1301,7 @@ public class Http11Processor extends AbstractProcessor {
         }
 
         long contentLength = response.getContentLengthLong();
-        boolean connectionClosePresent = false;
+        boolean connectionClosePresent = isConnectionClose(headers);
         if (contentLength != -1) {
             headers.setValue("Content-Length").setLong(contentLength);
             outputBuffer.addActiveFilter
@@ -1307,10 +1310,8 @@ public class Http11Processor extends AbstractProcessor {
         } else {
             // If the response code supports an entity body and we're on
             // HTTP 1.1 then we chunk unless we have a Connection: close header
-            connectionClosePresent = isConnectionClose(headers);
-            if (entityBody && http11 && !connectionClosePresent) {
-                outputBuffer.addActiveFilter
-                    (outputFilters[Constants.CHUNKED_FILTER]);
+            if (http11 && entityBody && !connectionClosePresent) {
+                outputBuffer.addActiveFilter(outputFilters[Constants.CHUNKED_FILTER]);
                 contentDelimitation = true;
                 headers.addValue(Constants.TRANSFERENCODING).setString(Constants.CHUNKED);
             } else {
@@ -1403,12 +1404,25 @@ public class Http11Processor extends AbstractProcessor {
         outputBuffer.commit();
     }
 
-    private static boolean isConnectionClose(MimeHeaders headers) {
+    private static boolean isConnectionClose(MimeHeaders headers) throws IOException {
         MessageBytes connection = headers.getValue(Constants.CONNECTION);
         if (connection == null) {
             return false;
         }
-        return connection.equals(Constants.CLOSE);
+
+        Enumeration<String> values = headers.values(Constants.CONNECTION);
+        Set<String> result = null;
+        while (values.hasMoreElements()) {
+            if (result == null) {
+                result = new HashSet<>();
+            }
+            TokenList.parseTokenList(new StringReader(values.nextElement()), result);
+        }
+
+        if (result == null) {
+            return false;
+        }
+        return result.contains(Constants.CLOSE);
     }
 
     private void prepareSendfile(OutputFilter[] outputFilters) {


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org