1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.messagelet.impl;
19
20 import java.io.UnsupportedEncodingException;
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.Map;
24 import java.util.TimeZone;
25
26 import javax.servlet.http.Cookie;
27
28
29
30
31
32
33
34
35
36
37 public final class RequestUtil {
38
39
40
41
42
43 private static SimpleDateFormat format =
44 new SimpleDateFormat(" EEEE, dd-MMM-yy kk:mm:ss zz");
45
46 static {
47 format.setTimeZone(TimeZone.getTimeZone("GMT"));
48 }
49
50
51
52
53
54
55
56
57
58 public static String encodeCookie(Cookie cookie) {
59
60 StringBuffer buf = new StringBuffer( cookie.getName() );
61 buf.append("=");
62 buf.append(cookie.getValue());
63
64 if (cookie.getComment() != null) {
65 buf.append("; Comment=\"");
66 buf.append(cookie.getComment());
67 buf.append("\"");
68 }
69
70 if (cookie.getDomain() != null) {
71 buf.append("; Domain=\"");
72 buf.append(cookie.getDomain());
73 buf.append("\"");
74 }
75
76 long age = cookie.getMaxAge();
77 if (cookie.getMaxAge() >= 0) {
78 buf.append("; Max-Age=\"");
79 buf.append(cookie.getMaxAge());
80 buf.append("\"");
81 }
82
83 if (cookie.getPath() != null) {
84 buf.append("; Path=\"");
85 buf.append(cookie.getPath());
86 buf.append("\"");
87 }
88
89 if (cookie.getSecure()) {
90 buf.append("; Secure");
91 }
92
93 if (cookie.getVersion() > 0) {
94 buf.append("; Version=\"");
95 buf.append(cookie.getVersion());
96 buf.append("\"");
97 }
98
99 return (buf.toString());
100 }
101
102
103
104
105
106
107
108
109
110 public static String filter(String message) {
111
112 if (message == null)
113 return (null);
114
115 char content[] = new char[message.length()];
116 message.getChars(0, message.length(), content, 0);
117 StringBuffer result = new StringBuffer(content.length + 50);
118 for (int i = 0; i < content.length; i++) {
119 switch (content[i]) {
120 case '<':
121 result.append("<");
122 break;
123 case '>':
124 result.append(">");
125 break;
126 case '&':
127 result.append("&");
128 break;
129 case '"':
130 result.append(""");
131 break;
132 default:
133 result.append(content[i]);
134 }
135 }
136 return (result.toString());
137
138 }
139
140
141
142
143
144
145
146
147
148
149 public static String normalize(String path) {
150
151 if (path == null)
152 return null;
153
154
155 String normalized = path;
156
157 if (normalized.equals("/."))
158 return "/";
159
160
161 if (!normalized.startsWith("/"))
162 normalized = "/" + normalized;
163
164
165 while (true) {
166 int index = normalized.indexOf("//");
167 if (index < 0)
168 break;
169 normalized = normalized.substring(0, index) +
170 normalized.substring(index + 1);
171 }
172
173
174 while (true) {
175 int index = normalized.indexOf("/./");
176 if (index < 0)
177 break;
178 normalized = normalized.substring(0, index) +
179 normalized.substring(index + 2);
180 }
181
182
183 while (true) {
184 int index = normalized.indexOf("/../");
185 if (index < 0)
186 break;
187 if (index == 0)
188 return (null);
189 int index2 = normalized.lastIndexOf('/', index - 1);
190 normalized = normalized.substring(0, index2) +
191 normalized.substring(index + 3);
192 }
193
194
195 return (normalized);
196
197 }
198
199
200
201
202
203
204
205
206
207 public static String parseCharacterEncoding(String contentType) {
208
209 if (contentType == null)
210 return (null);
211 int start = contentType.indexOf("charset=");
212 if (start < 0)
213 return (null);
214 String encoding = contentType.substring(start + 8);
215 int end = encoding.indexOf(';');
216 if (end >= 0)
217 encoding = encoding.substring(0, end);
218 encoding = encoding.trim();
219 if ((encoding.length() > 2) && (encoding.startsWith("\""))
220 && (encoding.endsWith("\"")))
221 encoding = encoding.substring(1, encoding.length() - 1);
222 return (encoding.trim());
223
224 }
225
226
227
228
229
230
231
232 public static Cookie[] parseCookieHeader(String header) {
233
234 if ((header == null) || (header.length() < 1))
235 return (new Cookie[0]);
236
237 ArrayList cookies = new ArrayList();
238 while (header.length() > 0) {
239 int semicolon = header.indexOf(';');
240 if (semicolon < 0)
241 semicolon = header.length();
242 if (semicolon == 0)
243 break;
244 String token = header.substring(0, semicolon);
245 if (semicolon < header.length())
246 header = header.substring(semicolon + 1);
247 else
248 header = "";
249 try {
250 int equals = token.indexOf('=');
251 if (equals > 0) {
252 String name = URLDecode(token.substring(0, equals).trim());
253 String value = URLDecode(token.substring(equals+1).trim());
254 cookies.add(new Cookie(name, value));
255 }
256 } catch (Throwable e) {
257 ;
258 }
259 }
260
261 return ((Cookie[]) cookies.toArray(new Cookie[cookies.size()]));
262
263 }
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283 public static void parseParameters(Map map, String data, String encoding)
284 throws UnsupportedEncodingException {
285
286 if ((data != null) && (data.length() > 0)) {
287 int len = data.length();
288 byte[] bytes = new byte[len];
289 data.getBytes(0, len, bytes, 0);
290 parseParameters(map, bytes, encoding);
291 }
292
293 }
294
295
296
297
298
299
300
301
302
303
304
305
306
307 public static String URLDecode(String str) {
308
309 return URLDecode(str, null);
310
311 }
312
313
314
315
316
317
318
319
320
321
322 public static String URLDecode(String str, String enc) {
323
324 if (str == null)
325 return (null);
326
327 int len = str.length();
328 byte[] bytes = new byte[len];
329 str.getBytes(0, len, bytes, 0);
330
331 return URLDecode(bytes, enc);
332
333 }
334
335
336
337
338
339
340
341
342
343 public static String URLDecode(byte[] bytes) {
344 return URLDecode(bytes, null);
345 }
346
347
348
349
350
351
352
353
354
355
356 public static String URLDecode(byte[] bytes, String enc) {
357
358 if (bytes == null)
359 return (null);
360
361 int len = bytes.length;
362 int ix = 0;
363 int ox = 0;
364 while (ix < len) {
365 byte b = bytes[ix++];
366 if (b == '+') {
367 b = (byte)' ';
368 } else if (b == '%') {
369 b = (byte) ((convertHexDigit(bytes[ix++]) << 4)
370 + convertHexDigit(bytes[ix++]));
371 }
372 bytes[ox++] = b;
373 }
374 if (enc != null) {
375 try {
376 return new String(bytes, 0, ox, enc);
377 } catch (Exception e) {
378 e.printStackTrace();
379 }
380 }
381 return new String(bytes, 0, ox);
382
383 }
384
385
386
387
388
389
390
391 private static byte convertHexDigit( byte b ) {
392 if ((b >= '0') && (b <= '9')) return (byte)(b - '0');
393 if ((b >= 'a') && (b <= 'f')) return (byte)(b - 'a' + 10);
394 if ((b >= 'A') && (b <= 'F')) return (byte)(b - 'A' + 10);
395 return 0;
396 }
397
398
399
400
401
402
403
404
405
406
407 private static void putMapEntry( Map map, String name, String value) {
408 String[] newValues = null;
409 String[] oldValues = (String[]) map.get(name);
410 if (oldValues == null) {
411 newValues = new String[1];
412 newValues[0] = value;
413 } else {
414 newValues = new String[oldValues.length + 1];
415 System.arraycopy(oldValues, 0, newValues, 0, oldValues.length);
416 newValues[oldValues.length] = value;
417 }
418 map.put(name, newValues);
419 }
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441 public static void parseParameters(Map map, byte[] data, String encoding)
442 throws UnsupportedEncodingException {
443
444 if (data != null && data.length > 0) {
445 int pos = 0;
446 int ix = 0;
447 int ox = 0;
448 String key = null;
449 String value = null;
450 while (ix < data.length) {
451 byte c = data[ix++];
452 switch ((char) c) {
453 case '&':
454 value = new String(data, 0, ox, encoding);
455 if (key != null) {
456 putMapEntry(map, key, value);
457 key = null;
458 }
459 ox = 0;
460 break;
461 case '=':
462 key = new String(data, 0, ox, encoding);
463 ox = 0;
464 break;
465 case '+':
466 data[ox++] = (byte)' ';
467 break;
468 case '%':
469 data[ox++] = (byte)((convertHexDigit(data[ix++]) << 4)
470 + convertHexDigit(data[ix++]));
471 break;
472 default:
473 data[ox++] = c;
474 }
475 }
476
477 if (key != null) {
478 value = new String(data, 0, ox, encoding);
479 putMapEntry(map, key, value);
480 }
481 }
482
483 }
484
485
486
487 }
488