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 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
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
187 @Deprecated
188 public QueryStringDecoder(URI uri, String charset) {
189 this(uri, Charset.forName(charset));
190 }
191
192
193
194
195 public String getPath() {
196 if (path == null) {
197 if (!hasPath) {
198 return path = "";
199 }
200
201 int pathEndPos = uri.indexOf('?');
202 if (pathEndPos < 0) {
203 path = uri;
204 } else {
205 return path = uri.substring(0, pathEndPos);
206 }
207 }
208 return path;
209 }
210
211
212
213
214 public Map<String, List<String>> getParameters() {
215 if (params == null) {
216 if (hasPath) {
217 int pathLength = getPath().length();
218 if (uri.length() == pathLength) {
219 return Collections.emptyMap();
220 }
221 decodeParams(uri.substring(pathLength + 1));
222 } else {
223 if (uri.length() == 0) {
224 return Collections.emptyMap();
225 }
226 decodeParams(uri);
227 }
228 }
229 return params;
230 }
231
232 private void decodeParams(String s) {
233 Map<String, List<String>> params = this.params = new LinkedHashMap<String, List<String>>();
234 nParams = 0;
235 String name = null;
236 int pos = 0;
237 int i;
238 char c = 0;
239 for (i = 0; i < s.length(); i++) {
240 c = s.charAt(i);
241 if (c == '=' && name == null) {
242 if (pos != i) {
243 name = decodeComponent(s.substring(pos, i), charset);
244 }
245 pos = i + 1;
246 } else if (c == '&') {
247 if (name == null && pos != i) {
248
249
250
251 if (!addParam(params, decodeComponent(s.substring(pos, i), charset), "")) {
252 return;
253 }
254 } else if (name != null) {
255 if (!addParam(params, name, decodeComponent(s.substring(pos, i), charset))) {
256 return;
257 }
258 name = null;
259 }
260 pos = i + 1;
261 }
262 }
263
264 if (pos != i) {
265 if (name == null) {
266 if (!addParam(params, decodeComponent(s.substring(pos, i), charset), "")) {
267 return;
268 }
269 } else {
270 if (!addParam(params, name, decodeComponent(s.substring(pos, i), charset))) {
271 return;
272 }
273 }
274 } else if (name != null) {
275 if (!addParam(params, name, "")) {
276 return;
277 }
278 }
279 }
280
281 private boolean addParam(Map<String, List<String>> params, String name, String value) {
282 if (nParams >= maxParams) {
283 return false;
284 }
285
286 List<String> values = params.get(name);
287 if (values == null) {
288 values = new ArrayList<String>(1);
289 params.put(name, values);
290 }
291 values.add(value);
292 nParams ++;
293 return true;
294 }
295
296
297
298
299
300
301
302
303
304
305
306
307 public static String decodeComponent(final String s) {
308 return decodeComponent(s, HttpConstants.DEFAULT_CHARSET);
309 }
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333 @SuppressWarnings("fallthrough")
334 public static String decodeComponent(final String s,
335 final Charset charset) {
336 if (s == null) {
337 return "";
338 }
339 final int size = s.length();
340 boolean modified = false;
341 for (int i = 0; i < size; i++) {
342 final char c = s.charAt(i);
343 switch (c) {
344 case '%':
345 i++;
346
347 case '+':
348 modified = true;
349 break;
350 }
351 }
352 if (!modified) {
353 return s;
354 }
355 final byte[] buf = new byte[size];
356 int pos = 0;
357 for (int i = 0; i < size; i++) {
358 char c = s.charAt(i);
359 switch (c) {
360 case '+':
361 buf[pos++] = ' ';
362 break;
363 case '%':
364 if (i == size - 1) {
365 throw new IllegalArgumentException("unterminated escape"
366 + " sequence at end of string: " + s);
367 }
368 c = s.charAt(++i);
369 if (c == '%') {
370 buf[pos++] = '%';
371 break;
372 } else if (i == size - 1) {
373 throw new IllegalArgumentException("partial escape"
374 + " sequence at end of string: " + s);
375 }
376 c = decodeHexNibble(c);
377 final char c2 = decodeHexNibble(s.charAt(++i));
378 if (c == Character.MAX_VALUE || c2 == Character.MAX_VALUE) {
379 throw new IllegalArgumentException(
380 "invalid escape sequence `%" + s.charAt(i - 1)
381 + s.charAt(i) + "' at index " + (i - 2)
382 + " of: " + s);
383 }
384 c = (char) (c * 16 + c2);
385
386 default:
387 buf[pos++] = (byte) c;
388 break;
389 }
390 }
391 try {
392 return new String(buf, 0, pos, charset.name());
393 } catch (UnsupportedEncodingException e) {
394 throw new IllegalArgumentException("unsupported encoding: " + charset.name());
395 }
396 }
397
398
399
400
401
402
403
404
405 private static char decodeHexNibble(final char c) {
406 if ('0' <= c && c <= '9') {
407 return (char) (c - '0');
408 } else if ('a' <= c && c <= 'f') {
409 return (char) (c - 'a' + 10);
410 } else if ('A' <= c && c <= 'F') {
411 return (char) (c - 'A' + 10);
412 } else {
413 return Character.MAX_VALUE;
414 }
415 }
416 }