1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.http2;
17
18 import java.io.UnsupportedEncodingException;
19 import java.net.URI;
20 import java.net.URLDecoder;
21 import java.nio.charset.Charset;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.jboss.netty.util.CharsetUtil;
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class QueryStringDecoder {
51
52 private static final int DEFAULT_MAX_PARAMS = 1024;
53
54 private final Charset charset;
55 private final String uri;
56 private final boolean hasPath;
57 private final int maxParams;
58 private String path;
59 private Map<String, List<String>> params;
60 private int nParams;
61
62
63
64
65
66 public QueryStringDecoder(String uri) {
67 this(uri, HttpCodecUtil.DEFAULT_CHARSET);
68 }
69
70
71
72
73
74 public QueryStringDecoder(String uri, boolean hasPath) {
75 this(uri, HttpCodecUtil.DEFAULT_CHARSET, hasPath);
76 }
77
78
79
80
81
82 public QueryStringDecoder(String uri, Charset charset) {
83 this(uri, charset, true);
84 }
85
86
87
88
89
90 public QueryStringDecoder(String uri, Charset charset, boolean hasPath) {
91 this(uri, charset, hasPath, DEFAULT_MAX_PARAMS);
92 }
93
94
95
96
97
98 public QueryStringDecoder(String uri, Charset charset, boolean hasPath, int maxParams) {
99 if (uri == null) {
100 throw new NullPointerException("uri");
101 }
102 if (charset == null) {
103 throw new NullPointerException("charset");
104 }
105 if (maxParams <= 0) {
106 throw new IllegalArgumentException(
107 "maxParams: " + maxParams + " (expected: a positive integer)");
108 }
109
110
111 this.uri = uri.replace(';', '&');
112 this.charset = charset;
113 this.maxParams = maxParams;
114 this.hasPath = hasPath;
115 }
116
117
118
119
120 @Deprecated
121 public QueryStringDecoder(String uri, String charset) {
122 this(uri, Charset.forName(charset));
123 }
124
125
126
127
128
129 public QueryStringDecoder(URI uri) {
130 this(uri, HttpCodecUtil.DEFAULT_CHARSET);
131 }
132
133
134
135
136
137 public QueryStringDecoder(URI uri, Charset charset) {
138 this(uri, charset, DEFAULT_MAX_PARAMS);
139 }
140
141
142
143
144
145 public QueryStringDecoder(URI uri, Charset charset, int maxParams) {
146 if (uri == null) {
147 throw new NullPointerException("uri");
148 }
149 if (charset == null) {
150 throw new NullPointerException("charset");
151 }
152 if (maxParams <= 0) {
153 throw new IllegalArgumentException(
154 "maxParams: " + maxParams + " (expected: a positive integer)");
155 }
156
157 String rawPath = uri.getRawPath();
158 if (rawPath != null) {
159 hasPath = true;
160 } else {
161 rawPath = "";
162 hasPath = false;
163 }
164
165 String newUri = rawPath + "?" + uri.getRawQuery();
166
167
168 this.uri = newUri.replace(';', '&');
169 this.charset = charset;
170 this.maxParams = maxParams;
171
172 }
173
174
175
176
177 @Deprecated
178 public QueryStringDecoder(URI uri, String charset) {
179 this(uri, Charset.forName(charset));
180 }
181
182
183
184
185 public String getPath() {
186 if (path == null) {
187 if (!hasPath) {
188 return path = "";
189 }
190
191 int pathEndPos = uri.indexOf('?');
192 if (pathEndPos < 0) {
193 path = uri;
194 } else {
195 return path = uri.substring(0, pathEndPos);
196 }
197 }
198 return path;
199 }
200
201
202
203
204 public Map<String, List<String>> getParameters() {
205 if (params == null) {
206 if (hasPath) {
207 int pathLength = getPath().length();
208 if (uri.length() == pathLength) {
209 return Collections.emptyMap();
210 }
211 decodeParams(uri.substring(pathLength + 1));
212 } else {
213 if (uri.length() == 0) {
214 return Collections.emptyMap();
215 }
216 decodeParams(uri);
217 }
218 }
219 return params;
220 }
221
222 private void decodeParams(String s) {
223 Map<String, List<String>> params = this.params = new LinkedHashMap<String, List<String>>();
224 nParams = 0;
225 String name = null;
226 int pos = 0;
227 int i;
228 char c = 0;
229 for (i = 0; i < s.length(); i++) {
230 c = s.charAt(i);
231 if (c == '=' && name == null) {
232 if (pos != i) {
233 name = decodeComponent(s.substring(pos, i), charset);
234 }
235 pos = i + 1;
236 } else if (c == '&') {
237 if (name == null && pos != i) {
238
239
240
241 if (!addParam(params, decodeComponent(s.substring(pos, i), charset), "")) {
242 return;
243 }
244 } else if (name != null) {
245 if (!addParam(params, name, decodeComponent(s.substring(pos, i), charset))) {
246 return;
247 }
248 name = null;
249 }
250 pos = i + 1;
251 }
252 }
253
254 if (pos != i) {
255 if (name == null) {
256 if (!addParam(params, decodeComponent(s.substring(pos, i), charset), "")) {
257 return;
258 }
259 } else {
260 if (!addParam(params, name, decodeComponent(s.substring(pos, i), charset))) {
261 return;
262 }
263 }
264 } else if (name != null) {
265 if (!addParam(params, name, "")) {
266 return;
267 }
268 }
269 }
270
271 private boolean addParam(Map<String, List<String>> params, String name, String value) {
272 if (nParams >= maxParams) {
273 return false;
274 }
275
276 List<String> values = params.get(name);
277 if (values == null) {
278 values = new ArrayList<String>(1);
279 params.put(name, values);
280 }
281 values.add(value);
282 nParams ++;
283 return true;
284 }
285
286
287
288
289
290
291
292
293
294
295
296
297 public static String decodeComponent(final String s) {
298 return decodeComponent(s, HttpCodecUtil.DEFAULT_CHARSET);
299 }
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323 @SuppressWarnings("fallthrough")
324 public static String decodeComponent(final String s,
325 final Charset charset) {
326 if (s == null) {
327 return "";
328 }
329 final int size = s.length();
330 boolean modified = false;
331 for (int i = 0; i < size; i++) {
332 final char c = s.charAt(i);
333 switch (c) {
334 case '%':
335 i++;
336
337 case '+':
338 modified = true;
339 break;
340 }
341 }
342 if (!modified) {
343 return s;
344 }
345 final byte[] buf = new byte[size];
346 int pos = 0;
347 for (int i = 0; i < size; i++) {
348 char c = s.charAt(i);
349 switch (c) {
350 case '+':
351 buf[pos++] = ' ';
352 break;
353 case '%':
354 if (i == size - 1) {
355 throw new IllegalArgumentException("unterminated escape"
356 + " sequence at end of string: " + s);
357 }
358 c = s.charAt(++i);
359 if (c == '%') {
360 buf[pos++] = '%';
361 break;
362 } else if (i == size - 1) {
363 throw new IllegalArgumentException("partial escape"
364 + " sequence at end of string: " + s);
365 }
366 c = decodeHexNibble(c);
367 final char c2 = decodeHexNibble(s.charAt(++i));
368 if (c == Character.MAX_VALUE || c2 == Character.MAX_VALUE) {
369 throw new IllegalArgumentException(
370 "invalid escape sequence `%" + s.charAt(i - 1)
371 + s.charAt(i) + "' at index " + (i - 2)
372 + " of: " + s);
373 }
374 c = (char) (c * 16 + c2);
375
376 default:
377 buf[pos++] = (byte) c;
378 break;
379 }
380 }
381 try {
382 return new String(buf, 0, pos, charset.name());
383 } catch (UnsupportedEncodingException e) {
384 throw new IllegalArgumentException("unsupported encoding: " + charset.name());
385 }
386 }
387
388
389
390
391
392
393
394
395 private static char decodeHexNibble(final char c) {
396 if ('0' <= c && c <= '9') {
397 return (char) (c - '0');
398 } else if ('a' <= c && c <= 'f') {
399 return (char) (c - 'a' + 10);
400 } else if ('A' <= c && c <= 'F') {
401 return (char) (c - 'A' + 10);
402 } else {
403 return Character.MAX_VALUE;
404 }
405 }
406 }