1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 public final static DistinguishedNameParser INSTANCE = new DistinguishedNameParser();
41
42 private static final Tokenizer.Delimiter EQUAL_OR_COMMA_OR_PLUS = Tokenizer.delimiters('=', ',', '+');
43 private static final Tokenizer.Delimiter COMMA_OR_PLUS = Tokenizer.delimiters(',', '+');
44
45 private final Tokenizer tokenParser;
46
47 DistinguishedNameParser() {
48 this.tokenParser = new InternalTokenParser();
49 }
50
51 private String parseToken(final CharArrayBuffer buf, final Tokenizer.Cursor cursor, final Tokenizer.Delimiter delimiters) {
52 return tokenParser.parseToken(buf, cursor, delimiters);
53 }
54
55 private String parseValue(final CharArrayBuffer buf, final Tokenizer.Cursor cursor, final Tokenizer.Delimiter delimiters) {
56 return tokenParser.parseValue(buf, cursor, delimiters);
57 }
58
59 private NameValuePair parseParameter(final CharArrayBuffer buf, final Tokenizer.Cursor cursor) {
60 final String name = parseToken(buf, cursor, EQUAL_OR_COMMA_OR_PLUS);
61 if (cursor.atEnd()) {
62 return new BasicNameValuePair(name, null);
63 }
64 final int delim = buf.charAt(cursor.getPos());
65 cursor.updatePos(cursor.getPos() + 1);
66 if (delim == ',') {
67 return new BasicNameValuePair(name, null);
68 }
69 final String value = parseValue(buf, cursor, COMMA_OR_PLUS);
70 if (!cursor.atEnd()) {
71 cursor.updatePos(cursor.getPos() + 1);
72 }
73 return new BasicNameValuePair(name, value);
74 }
75
76 List<NameValuePair> parse(final CharArrayBuffer buf, final Tokenizer.Cursor cursor) {
77 final List<NameValuePair> params = new ArrayList<>();
78 tokenParser.skipWhiteSpace(buf, cursor);
79 while (!cursor.atEnd()) {
80 final NameValuePair param = parseParameter(buf, cursor);
81 params.add(param);
82 }
83 return params;
84 }
85
86 List<NameValuePair> parse(final String s) {
87 if (s == null) {
88 return null;
89 }
90 final CharArrayBuffer buffer = new CharArrayBuffer(s.length());
91 buffer.append(s);
92 final Tokenizer.Cursor cursor = new Tokenizer.Cursor(0, s.length());
93 return parse(buffer, cursor);
94 }
95
96 static class InternalTokenParser extends Tokenizer {
97
98 @Override
99 public void copyUnquotedContent(
100 final CharSequence buf,
101 final Tokenizer.Cursor cursor,
102 final Tokenizer.Delimiter delimiters,
103 final StringBuilder dst) {
104 int pos = cursor.getPos();
105 final int indexFrom = cursor.getPos();
106 final int indexTo = cursor.getUpperBound();
107 boolean escaped = false;
108 for (int i = indexFrom; i < indexTo; i++, pos++) {
109 final char current = buf.charAt(i);
110 if (escaped) {
111 dst.append(current);
112 escaped = false;
113 } else {
114 if ((delimiters != null && delimiters.test(current))
115 || Tokenizer.isWhitespace(current) || current == '\"') {
116 break;
117 } else if (current == '\\') {
118 escaped = true;
119 } else {
120 dst.append(current);
121 }
122 }
123 }
124 cursor.updatePos(pos);
125 }
126 }
127
128 }
129