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.client5.http.ssl;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import org.apache.hc.core5.http.NameValuePair;
34  import org.apache.hc.core5.http.message.BasicNameValuePair;
35  import org.apache.hc.core5.util.CharArrayBuffer;
36  import org.apache.hc.core5.util.Tokenizer;
37  
38  final class DistinguishedNameParser {
39  
40      /**
41       * Default instance of {@link DistinguishedNameParser}.
42       */
43      public final static DistinguishedNameParser INSTANCE = new DistinguishedNameParser();
44  
45      private static final Tokenizer.Delimiter EQUAL_OR_COMMA_OR_PLUS = Tokenizer.delimiters('=', ',', '+');
46      private static final Tokenizer.Delimiter COMMA_OR_PLUS = Tokenizer.delimiters(',', '+');
47  
48      private final Tokenizer tokenParser = new InternalTokenParser();
49  
50      private DistinguishedNameParser() {
51          // empty
52      }
53  
54      private String parseToken(final CharArrayBuffer buf, final Tokenizer.Cursor cursor, final Tokenizer.Delimiter delimiters) {
55          return tokenParser.parseToken(buf, cursor, delimiters);
56      }
57  
58      private String parseValue(final CharArrayBuffer buf, final Tokenizer.Cursor cursor, final Tokenizer.Delimiter delimiters) {
59          return tokenParser.parseValue(buf, cursor, delimiters);
60      }
61  
62      private NameValuePair parseParameter(final CharArrayBuffer buf, final Tokenizer.Cursor cursor) {
63          final String name = parseToken(buf, cursor, EQUAL_OR_COMMA_OR_PLUS);
64          if (cursor.atEnd()) {
65              return new BasicNameValuePair(name, null);
66          }
67          final int delim = buf.charAt(cursor.getPos());
68          cursor.updatePos(cursor.getPos() + 1);
69          if (delim == ',') {
70              return new BasicNameValuePair(name, null);
71          }
72          final String value = parseValue(buf, cursor, COMMA_OR_PLUS);
73          if (!cursor.atEnd()) {
74              cursor.updatePos(cursor.getPos() + 1);
75          }
76          return new BasicNameValuePair(name, value);
77      }
78  
79      List<NameValuePair> parse(final CharArrayBuffer buf, final Tokenizer.Cursor cursor) {
80          final List<NameValuePair> params = new ArrayList<>();
81          tokenParser.skipWhiteSpace(buf, cursor);
82          while (!cursor.atEnd()) {
83              final NameValuePair param = parseParameter(buf, cursor);
84              params.add(param);
85          }
86          return params;
87      }
88  
89      List<NameValuePair> parse(final String s) {
90          if (s == null) {
91              return null;
92          }
93          final CharArrayBuffer buffer = new CharArrayBuffer(s.length());
94          buffer.append(s);
95          final Tokenizer.Cursor cursor = new Tokenizer.Cursor(0, s.length());
96          return parse(buffer, cursor);
97      }
98  
99      static class InternalTokenParser extends Tokenizer {
100 
101         @Override
102         public void copyUnquotedContent(
103                 final CharSequence buf,
104                 final Tokenizer.Cursor cursor,
105                 final Tokenizer.Delimiter delimiters,
106                 final StringBuilder dst) {
107             int pos = cursor.getPos();
108             final int indexFrom = cursor.getPos();
109             final int indexTo = cursor.getUpperBound();
110             boolean escaped = false;
111             for (int i = indexFrom; i < indexTo; i++, pos++) {
112                 final char current = buf.charAt(i);
113                 if (escaped) {
114                     dst.append(current);
115                     escaped = false;
116                 } else {
117                     if ((delimiters != null && delimiters.test(current))
118                             || Tokenizer.isWhitespace(current) || current == '\"') {
119                         break;
120                     } else if (current == '\\') {
121                         escaped = true;
122                     } else {
123                         dst.append(current);
124                     }
125                 }
126             }
127             cursor.updatePos(pos);
128         }
129     }
130 
131 }
132