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  
40  /**
41   * Default {@link org.apache.hc.core5.http.message.HeaderValueParser} implementation.
42   *
43   * @since 4.0
44   */
45  @Contract(threading = ThreadingBehavior.IMMUTABLE)
46  public class BasicHeaderValueParser implements HeaderValueParser {
47  
48      public final static BasicHeaderValueParser INSTANCE = new BasicHeaderValueParser();
49  
50      private final static char PARAM_DELIMITER                = ';';
51      private final static char ELEM_DELIMITER                 = ',';
52  
53      // IMPORTANT!
54      // These private static variables must be treated as immutable and never exposed outside this class
55      private static final BitSet TOKEN_DELIMS = TokenParser.INIT_BITSET('=', PARAM_DELIMITER, ELEM_DELIMITER);
56      private static final BitSet VALUE_DELIMS = TokenParser.INIT_BITSET(PARAM_DELIMITER, ELEM_DELIMITER);
57  
58      private final TokenParser tokenParser;
59  
60      public BasicHeaderValueParser() {
61          this.tokenParser = TokenParser.INSTANCE;
62      }
63  
64      @Override
65      public HeaderElement[] parseElements(final CharSequence buffer, final ParserCursor cursor) {
66          Args.notNull(buffer, "Char sequence");
67          Args.notNull(cursor, "Parser cursor");
68          final List<HeaderElement> elements = new ArrayList<>();
69          while (!cursor.atEnd()) {
70              final HeaderElement element = parseHeaderElement(buffer, cursor);
71              if (!(element.getName().isEmpty() && element.getValue() == null)) {
72                  elements.add(element);
73              }
74          }
75          return elements.toArray(new HeaderElement[elements.size()]);
76      }
77  
78      @Override
79      public HeaderElement parseHeaderElement(final CharSequence buffer, final ParserCursor cursor) {
80          Args.notNull(buffer, "Char sequence");
81          Args.notNull(cursor, "Parser cursor");
82          final NameValuePair nvp = parseNameValuePair(buffer, cursor);
83          NameValuePair[] params = null;
84          if (!cursor.atEnd()) {
85              final char ch = buffer.charAt(cursor.getPos() - 1);
86              if (ch != ELEM_DELIMITER) {
87                  params = parseParameters(buffer, cursor);
88              }
89          }
90          return new BasicHeaderElement(nvp.getName(), nvp.getValue(), params);
91      }
92  
93      @Override
94      public NameValuePair[] parseParameters(final CharSequence buffer, final ParserCursor cursor) {
95          Args.notNull(buffer, "Char sequence");
96          Args.notNull(cursor, "Parser cursor");
97          tokenParser.skipWhiteSpace(buffer, cursor);
98          final List<NameValuePair> params = new ArrayList<>();
99          while (!cursor.atEnd()) {
100             final NameValuePair param = parseNameValuePair(buffer, cursor);
101             params.add(param);
102             final char ch = buffer.charAt(cursor.getPos() - 1);
103             if (ch == ELEM_DELIMITER) {
104                 break;
105             }
106         }
107         return params.toArray(new NameValuePair[params.size()]);
108     }
109 
110     @Override
111     public NameValuePair parseNameValuePair(final CharSequence buffer, final ParserCursor cursor) {
112         Args.notNull(buffer, "Char sequence");
113         Args.notNull(cursor, "Parser cursor");
114 
115         final String name = tokenParser.parseToken(buffer, cursor, TOKEN_DELIMS);
116         if (cursor.atEnd()) {
117             return new BasicNameValuePair(name, null);
118         }
119         final int delim = buffer.charAt(cursor.getPos());
120         cursor.updatePos(cursor.getPos() + 1);
121         if (delim != '=') {
122             return new BasicNameValuePair(name, null);
123         }
124         final String value = tokenParser.parseValue(buffer, cursor, VALUE_DELIMS);
125         if (!cursor.atEnd()) {
126             cursor.updatePos(cursor.getPos() + 1);
127         }
128         return new BasicNameValuePair(name, value);
129     }
130 
131 }
132