View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.http.message;
29  
30  import java.util.ArrayList;
31  import java.util.BitSet;
32  import java.util.List;
33  
34  import org.apache.hc.core5.annotation.Contract;
35  import org.apache.hc.core5.annotation.ThreadingBehavior;
36  import org.apache.hc.core5.http.HeaderElement;
37  import org.apache.hc.core5.http.NameValuePair;
38  import org.apache.hc.core5.util.Args;
39  import org.apache.hc.core5.util.Tokenizer;
40  
41  /**
42   * Default {@link org.apache.hc.core5.http.message.HeaderValueParser} implementation.
43   *
44   * @since 4.0
45   */
46  @Contract(threading = ThreadingBehavior.IMMUTABLE)
47  public class BasicHeaderValueParser implements HeaderValueParser {
48  
49      public final static BasicHeaderValueParseraderValueParser.html#BasicHeaderValueParser">BasicHeaderValueParser INSTANCE = new BasicHeaderValueParser();
50  
51      private final static char PARAM_DELIMITER                = ';';
52      private final static char ELEM_DELIMITER                 = ',';
53  
54      // IMPORTANT!
55      // These private static variables must be treated as immutable and never exposed outside this class
56      private static final BitSet TOKEN_DELIMS = Tokenizer.INIT_BITSET('=', PARAM_DELIMITER, ELEM_DELIMITER);
57      private static final BitSet VALUE_DELIMS = Tokenizer.INIT_BITSET(PARAM_DELIMITER, ELEM_DELIMITER);
58  
59      private final Tokenizer tokenizer;
60  
61      public BasicHeaderValueParser() {
62          this.tokenizer = Tokenizer.INSTANCE;
63      }
64  
65      /**
66       * An empty immutable {@code HeaderElement} array.
67       */
68      private static final HeaderElementlement">HeaderElement[] EMPTY_HEADER_ELEMENT_ARRAY = new HeaderElement[0];
69  
70      /**
71       * An empty immutable {@code NameValuePair} array.
72       */
73      private static final NameValuePaireValuePair">NameValuePair[] EMPTY_NAME_VALUE_ARRAY = new NameValuePair[0];
74  
75      @Override
76      public HeaderElement[] parseElements(final CharSequence buffer, final ParserCursor cursor) {
77          Args.notNull(buffer, "Char sequence");
78          Args.notNull(cursor, "Parser cursor");
79          final List<HeaderElement> elements = new ArrayList<>();
80          while (!cursor.atEnd()) {
81              final HeaderElement element = parseHeaderElement(buffer, cursor);
82              if (!(element.getName().isEmpty() && element.getValue() == null)) {
83                  elements.add(element);
84              }
85          }
86          return elements.toArray(EMPTY_HEADER_ELEMENT_ARRAY);
87      }
88  
89      @Override
90      public HeaderElement parseHeaderElement(final CharSequence buffer, final ParserCursor cursor) {
91          Args.notNull(buffer, "Char sequence");
92          Args.notNull(cursor, "Parser cursor");
93          final NameValuePair nvp = parseNameValuePair(buffer, cursor);
94          NameValuePair[] params = null;
95          if (!cursor.atEnd()) {
96              final char ch = buffer.charAt(cursor.getPos() - 1);
97              if (ch != ELEM_DELIMITER) {
98                  params = parseParameters(buffer, cursor);
99              }
100         }
101         return new BasicHeaderElement(nvp.getName(), nvp.getValue(), params);
102     }
103 
104     @Override
105     public NameValuePair[] parseParameters(final CharSequence buffer, final ParserCursor cursor) {
106         Args.notNull(buffer, "Char sequence");
107         Args.notNull(cursor, "Parser cursor");
108         tokenizer.skipWhiteSpace(buffer, cursor);
109         final List<NameValuePair> params = new ArrayList<>();
110         while (!cursor.atEnd()) {
111             final NameValuePair param = parseNameValuePair(buffer, cursor);
112             params.add(param);
113             final char ch = buffer.charAt(cursor.getPos() - 1);
114             if (ch == ELEM_DELIMITER) {
115                 break;
116             }
117         }
118         return params.toArray(EMPTY_NAME_VALUE_ARRAY);
119     }
120 
121     @Override
122     public NameValuePair parseNameValuePair(final CharSequence buffer, final ParserCursor cursor) {
123         Args.notNull(buffer, "Char sequence");
124         Args.notNull(cursor, "Parser cursor");
125 
126         final String name = tokenizer.parseToken(buffer, cursor, TOKEN_DELIMS);
127         if (cursor.atEnd()) {
128             return new BasicNameValuePair(name, null);
129         }
130         final int delim = buffer.charAt(cursor.getPos());
131         cursor.updatePos(cursor.getPos() + 1);
132         if (delim != '=') {
133             return new BasicNameValuePair(name, null);
134         }
135         final String value = tokenizer.parseValue(buffer, cursor, VALUE_DELIMS);
136         if (!cursor.atEnd()) {
137             cursor.updatePos(cursor.getPos() + 1);
138         }
139         return new BasicNameValuePair(name, value);
140     }
141 
142 }
143