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.core5.net;
29
30 import java.net.URI;
31 import java.nio.charset.Charset;
32 import java.nio.charset.StandardCharsets;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.BitSet;
36 import java.util.List;
37
38 import org.apache.hc.core5.http.NameValuePair;
39 import org.apache.hc.core5.http.message.BasicNameValuePair;
40 import org.apache.hc.core5.util.Args;
41 import org.apache.hc.core5.util.Tokenizer;
42
43
44
45
46
47
48
49
50
51 @Deprecated
52 public class URLEncodedUtils {
53
54 private static final char QP_SEP_A = '&';
55 private static final char QP_SEP_S = ';';
56
57
58
59
60
61
62
63
64
65
66
67 public static List<NameValuePair> parse(final URI uri, final Charset charset) {
68 Args.notNull(uri, "URI");
69 final String query = uri.getRawQuery();
70 if (query != null && !query.isEmpty()) {
71 return parse(query, charset);
72 }
73 return new ArrayList<>(0);
74 }
75
76
77
78
79
80
81
82
83
84
85
86 public static List<NameValuePair> parse(final CharSequence s, final Charset charset) {
87 if (s == null) {
88 return new ArrayList<>(0);
89 }
90 return parse(s, charset, QP_SEP_A, QP_SEP_S);
91 }
92
93
94
95
96
97
98
99
100
101
102
103 public static List<NameValuePair> parse(
104 final CharSequence s, final Charset charset, final char... separators) {
105 Args.notNull(s, "Char sequence");
106 final Tokenizer tokenParser = Tokenizer.INSTANCE;
107 final BitSet delimSet = new BitSet();
108 for (final char separator: separators) {
109 delimSet.set(separator);
110 }
111 final Tokenizer.Cursor cursor = new Tokenizer.Cursor(0, s.length());
112 final List<NameValuePair> list = new ArrayList<>();
113 while (!cursor.atEnd()) {
114 delimSet.set('=');
115 final String name = tokenParser.parseToken(s, cursor, delimSet);
116 String value = null;
117 if (!cursor.atEnd()) {
118 final int delim = s.charAt(cursor.getPos());
119 cursor.updatePos(cursor.getPos() + 1);
120 if (delim == '=') {
121 delimSet.clear('=');
122 value = tokenParser.parseToken(s, cursor, delimSet);
123 if (!cursor.atEnd()) {
124 cursor.updatePos(cursor.getPos() + 1);
125 }
126 }
127 }
128 if (!name.isEmpty()) {
129 list.add(new BasicNameValuePair(
130 PercentCodec.decode(name, charset, true),
131 PercentCodec.decode(value, charset, true)));
132 }
133 }
134 return list;
135 }
136
137
138
139
140
141
142
143
144
145
146 public static List<String> parsePathSegments(final CharSequence s, final Charset charset) {
147 return URIBuilder.parsePath(s, charset);
148 }
149
150
151
152
153
154
155
156
157
158 public static List<String> parsePathSegments(final CharSequence s) {
159 return parsePathSegments(s, StandardCharsets.UTF_8);
160 }
161
162
163
164
165
166
167
168
169
170
171 public static String formatSegments(final Iterable<String> segments, final Charset charset) {
172 Args.notNull(segments, "Segments");
173 final StringBuilder buf = new StringBuilder();
174 URIBuilder.formatPath(buf, segments, false, charset);
175 return buf.toString();
176 }
177
178
179
180
181
182
183
184
185
186 public static String formatSegments(final String... segments) {
187 return formatSegments(Arrays.asList(segments), StandardCharsets.UTF_8);
188 }
189
190
191
192
193
194
195
196
197
198
199
200
201 public static String format(
202 final Iterable<? extends NameValuePair> parameters,
203 final char parameterSeparator,
204 final Charset charset) {
205 Args.notNull(parameters, "Parameters");
206 final StringBuilder buf = new StringBuilder();
207 int i = 0;
208 for (final NameValuePair parameter : parameters) {
209 if (i > 0) {
210 buf.append(parameterSeparator);
211 }
212 PercentCodec.encode(buf, parameter.getName(), charset, URLENCODER, true);
213 if (parameter.getValue() != null) {
214 buf.append('=');
215 PercentCodec.encode(buf, parameter.getValue(), charset, URLENCODER, true);
216 }
217 i++;
218 }
219 return buf.toString();
220 }
221
222
223
224
225
226
227
228
229
230
231
232 public static String format(
233 final Iterable<? extends NameValuePair> parameters,
234 final Charset charset) {
235 return format(parameters, QP_SEP_A, charset);
236 }
237
238 private static final BitSet URLENCODER = new BitSet(256);
239
240 static {
241
242
243 for (int i = 'a'; i <= 'z'; i++) {
244 URLENCODER.set(i);
245 }
246 for (int i = 'A'; i <= 'Z'; i++) {
247 URLENCODER.set(i);
248 }
249
250 for (int i = '0'; i <= '9'; i++) {
251 URLENCODER.set(i);
252 }
253 URLENCODER.set('_');
254 URLENCODER.set('-');
255 URLENCODER.set('.');
256 URLENCODER.set('*');
257 }
258
259 }