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.http.impl.cookie;
29  
30  import java.util.ArrayList;
31  import java.util.BitSet;
32  import java.util.List;
33  
34  import org.apache.http.HeaderElement;
35  import org.apache.http.NameValuePair;
36  import org.apache.http.ParseException;
37  import org.apache.http.annotation.Contract;
38  import org.apache.http.annotation.ThreadingBehavior;
39  import org.apache.http.message.BasicHeaderElement;
40  import org.apache.http.message.BasicNameValuePair;
41  import org.apache.http.message.ParserCursor;
42  import org.apache.http.message.TokenParser;
43  import org.apache.http.util.Args;
44  import org.apache.http.util.CharArrayBuffer;
45  
46  /**
47   *
48   * @since 4.0
49   */
50  @Contract(threading = ThreadingBehavior.IMMUTABLE)
51  public class NetscapeDraftHeaderParser {
52  
53      public final static NetscapeDraftHeaderParser DEFAULT = new NetscapeDraftHeaderParser();
54  
55      private final static char PARAM_DELIMITER                = ';';
56  
57      // IMPORTANT!
58      // These private static variables must be treated as immutable and never exposed outside this class
59      private static final BitSet TOKEN_DELIMS = TokenParser.INIT_BITSET('=', PARAM_DELIMITER);
60      private static final BitSet VALUE_DELIMS = TokenParser.INIT_BITSET(PARAM_DELIMITER);
61  
62      private final TokenParser tokenParser;
63  
64      public NetscapeDraftHeaderParser() {
65          super();
66          this.tokenParser = TokenParser.INSTANCE;
67      }
68  
69      public HeaderElement parseHeader(
70              final CharArrayBuffer buffer,
71              final ParserCursor cursor) throws ParseException {
72          Args.notNull(buffer, "Char array buffer");
73          Args.notNull(cursor, "Parser cursor");
74          final NameValuePair nvp = parseNameValuePair(buffer, cursor);
75          final List<NameValuePair> params = new ArrayList<NameValuePair>();
76          while (!cursor.atEnd()) {
77              final NameValuePair param = parseNameValuePair(buffer, cursor);
78              params.add(param);
79          }
80          return new BasicHeaderElement(
81                  nvp.getName(),
82                  nvp.getValue(), params.toArray(new NameValuePair[params.size()]));
83      }
84  
85      private NameValuePair parseNameValuePair(
86              final CharArrayBuffer buffer, final ParserCursor cursor) {
87          final String name = tokenParser.parseToken(buffer, cursor, TOKEN_DELIMS);
88          if (cursor.atEnd()) {
89              return new BasicNameValuePair(name, null);
90          }
91          final int delim = buffer.charAt(cursor.getPos());
92          cursor.updatePos(cursor.getPos() + 1);
93          if (delim != '=') {
94              return new BasicNameValuePair(name, null);
95          }
96          final String value = tokenParser.parseToken(buffer, cursor, VALUE_DELIMS);
97          if (!cursor.atEnd()) {
98              cursor.updatePos(cursor.getPos() + 1);
99          }
100         return new BasicNameValuePair(name, value);
101     }
102 
103 }