/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.tomcat.util.http.parser;

import java.io.IOException;
import java.io.Reader;
import java.util.Collection;
import java.util.Locale;

public class TokenList {

    private TokenList() {
        // Utility class. Hide default constructor.
    }


    /**
     * 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);
            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);
    }
}

@@ -1109,16 +1109,128 @@
  1#token
+, forcing all parsed values to lower%0A     * case. This is typically used when header values are case-insensitive
 .%0A     *
@@ -1370,16 +1370,17 @@
         
+ 
 parsed t
@@ -1583,16 +1583,750 @@
 eption %7B
+%0A        parseTokenList(input, true, result);%0A    %7D%0A%0A%0A    /**%0A     * Parses a header of the form 1#token.%0A     *%0A     * @param input          The header to parse%0A     * @param forceLowerCase Should parsed tokens be forced to lower case? This%0A     *                           is intended for headers where the values are%0A     *                           case-insensitive%0A     * @param result         The Collection (usually a list of a set) to which%0A     *                           the parsed token should be added%0A     *%0A     * @throws IOException If an I/O error occurs reading the header%0A     */%0A    public static void parseTokenList(Reader input, boolean forceLowerCase, Collection%3CString%3E result)%0A            throws IOException %7B
 %0A%0A      
@@ -2848,16 +2848,58 @@
  // EOF%0A
+                if (forceLowerCase) %7B%0A    
         
@@ -2949,32 +2949,118 @@
 cale.ENGLISH));%0A
+                %7D else %7B%0A                    result.add(fieldName);%0A                %7D%0A
                 
@@ -3119,24 +3119,66 @@
 lt.FOUND) %7B%0A
+                if (forceLowerCase) %7B%0A    
             
@@ -3224,32 +3224,118 @@
 cale.ENGLISH));%0A
+                %7D else %7B%0A                    result.add(fieldName);%0A                %7D%0A
                 

Add a case sensitive / insensitive option to the token list parser