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