1 17 18 19 package org.apache.catalina.util; 20 21 import java.io.UnsupportedEncodingException ; 22 import java.text.SimpleDateFormat ; 23 import java.util.ArrayList ; 24 import java.util.Map ; 25 import java.util.TimeZone ; 26 27 import javax.servlet.http.Cookie ; 28 29 30 37 38 public final class RequestUtil { 39 40 41 44 private static SimpleDateFormat format = 45 new SimpleDateFormat (" EEEE, dd-MMM-yy kk:mm:ss zz"); 46 47 static { 48 format.setTimeZone(TimeZone.getTimeZone("GMT")); 49 } 50 51 52 59 public static String encodeCookie(Cookie cookie) { 60 61 StringBuffer buf = new StringBuffer ( cookie.getName() ); 62 buf.append("="); 63 buf.append(cookie.getValue()); 64 65 if (cookie.getComment() != null) { 66 buf.append("; Comment=\""); 67 buf.append(cookie.getComment()); 68 buf.append("\""); 69 } 70 71 if (cookie.getDomain() != null) { 72 buf.append("; Domain=\""); 73 buf.append(cookie.getDomain()); 74 buf.append("\""); 75 } 76 77 long age = cookie.getMaxAge(); 78 if (cookie.getMaxAge() >= 0) { 79 buf.append("; Max-Age=\""); 80 buf.append(cookie.getMaxAge()); 81 buf.append("\""); 82 } 83 84 if (cookie.getPath() != null) { 85 buf.append("; Path=\""); 86 buf.append(cookie.getPath()); 87 buf.append("\""); 88 } 89 90 if (cookie.getSecure()) { 91 buf.append("; Secure"); 92 } 93 94 if (cookie.getVersion() > 0) { 95 buf.append("; Version=\""); 96 buf.append(cookie.getVersion()); 97 buf.append("\""); 98 } 99 100 return (buf.toString()); 101 } 102 103 104 111 public static String filter(String message) { 112 113 if (message == null) 114 return (null); 115 116 char content[] = new char[message.length()]; 117 message.getChars(0, message.length(), content, 0); 118 StringBuffer result = new StringBuffer (content.length + 50); 119 for (int i = 0; i < content.length; i++) { 120 switch (content[i]) { 121 case '<': 122 result.append("<"); 123 break; 124 case '>': 125 result.append(">"); 126 break; 127 case '&': 128 result.append("&"); 129 break; 130 case '"': 131 result.append("""); 132 break; 133 default: 134 result.append(content[i]); 135 } 136 } 137 return (result.toString()); 138 139 } 140 141 142 150 public static String normalize(String path) { 151 152 if (path == null) 153 return null; 154 155 String normalized = path; 157 158 if (normalized.equals("/.")) 159 return "/"; 160 161 if (!normalized.startsWith("/")) 163 normalized = "/" + normalized; 164 165 while (true) { 167 int index = normalized.indexOf("//"); 168 if (index < 0) 169 break; 170 normalized = normalized.substring(0, index) + 171 normalized.substring(index + 1); 172 } 173 174 while (true) { 176 int index = normalized.indexOf("/./"); 177 if (index < 0) 178 break; 179 normalized = normalized.substring(0, index) + 180 normalized.substring(index + 2); 181 } 182 183 while (true) { 185 int index = normalized.indexOf("/../"); 186 if (index < 0) 187 break; 188 if (index == 0) 189 return (null); int index2 = normalized.lastIndexOf('/', index - 1); 191 normalized = normalized.substring(0, index2) + 192 normalized.substring(index + 3); 193 } 194 195 return (normalized); 197 198 } 199 200 201 208 public static String parseCharacterEncoding(String contentType) { 209 210 if (contentType == null) 211 return (null); 212 int start = contentType.indexOf("charset="); 213 if (start < 0) 214 return (null); 215 String encoding = contentType.substring(start + 8); 216 int end = encoding.indexOf(';'); 217 if (end >= 0) 218 encoding = encoding.substring(0, end); 219 encoding = encoding.trim(); 220 if ((encoding.length() > 2) && (encoding.startsWith("\"")) 221 && (encoding.endsWith("\""))) 222 encoding = encoding.substring(1, encoding.length() - 1); 223 return (encoding.trim()); 224 225 } 226 227 228 233 public static Cookie [] parseCookieHeader(String header) { 234 235 if ((header == null) || (header.length() < 1)) 236 return (new Cookie [0]); 237 238 ArrayList cookies = new ArrayList (); 239 while (header.length() > 0) { 240 int semicolon = header.indexOf(';'); 241 if (semicolon < 0) 242 semicolon = header.length(); 243 if (semicolon == 0) 244 break; 245 String token = header.substring(0, semicolon); 246 if (semicolon < header.length()) 247 header = header.substring(semicolon + 1); 248 else 249 header = ""; 250 try { 251 int equals = token.indexOf('='); 252 if (equals > 0) { 253 String name = token.substring(0, equals).trim(); 254 String value = token.substring(equals+1).trim(); 255 cookies.add(new Cookie (name, value)); 256 } 257 } catch (Throwable e) { 258 ; 259 } 260 } 261 262 return ((Cookie []) cookies.toArray(new Cookie [cookies.size()])); 263 264 } 265 266 267 283 public static void parseParameters(Map map, String data, String encoding) 284 throws UnsupportedEncodingException { 285 286 if ((data != null) && (data.length() > 0)) { 287 288 byte[] bytes = null; 292 try { 293 if (encoding == null) { 294 bytes = data.getBytes(); 295 } else { 296 bytes = data.getBytes(encoding); 297 } 298 } catch (UnsupportedEncodingException uee) { 299 } 300 301 parseParameters(map, bytes, encoding); 302 } 303 304 } 305 306 307 318 public static String URLDecode(String str) { 319 320 return URLDecode(str, null); 321 322 } 323 324 325 333 public static String URLDecode(String str, String enc) { 334 335 if (str == null) 336 return (null); 337 338 byte[] bytes = null; 342 try { 343 if (enc == null) { 344 bytes = str.getBytes(); 345 } else { 346 bytes = str.getBytes(enc); 347 } 348 } catch (UnsupportedEncodingException uee) {} 349 350 return URLDecode(bytes, enc); 351 352 } 353 354 355 362 public static String URLDecode(byte[] bytes) { 363 return URLDecode(bytes, null); 364 } 365 366 367 375 public static String URLDecode(byte[] bytes, String enc) { 376 377 if (bytes == null) 378 return (null); 379 380 int len = bytes.length; 381 int ix = 0; 382 int ox = 0; 383 while (ix < len) { 384 byte b = bytes[ix++]; if (b == '+') { 386 b = (byte)' '; 387 } else if (b == '%') { 388 b = (byte) ((convertHexDigit(bytes[ix++]) << 4) 389 + convertHexDigit(bytes[ix++])); 390 } 391 bytes[ox++] = b; 392 } 393 if (enc != null) { 394 try { 395 return new String (bytes, 0, ox, enc); 396 } catch (Exception e) { 397 e.printStackTrace(); 398 } 399 } 400 return new String (bytes, 0, ox); 401 402 } 403 404 405 410 private static byte convertHexDigit( byte b ) { 411 if ((b >= '0') && (b <= '9')) return (byte)(b - '0'); 412 if ((b >= 'a') && (b <= 'f')) return (byte)(b - 'a' + 10); 413 if ((b >= 'A') && (b <= 'F')) return (byte)(b - 'A' + 10); 414 return 0; 415 } 416 417 418 426 private static void putMapEntry( Map map, String name, String value) { 427 String [] newValues = null; 428 String [] oldValues = (String []) map.get(name); 429 if (oldValues == null) { 430 newValues = new String [1]; 431 newValues[0] = value; 432 } else { 433 newValues = new String [oldValues.length + 1]; 434 System.arraycopy(oldValues, 0, newValues, 0, oldValues.length); 435 newValues[oldValues.length] = value; 436 } 437 map.put(name, newValues); 438 } 439 440 441 460 public static void parseParameters(Map map, byte[] data, String encoding) 461 throws UnsupportedEncodingException { 462 463 if (data != null && data.length > 0) { 464 int pos = 0; 465 int ix = 0; 466 int ox = 0; 467 String key = null; 468 String value = null; 469 while (ix < data.length) { 470 byte c = data[ix++]; 471 switch ((char) c) { 472 case '&': 473 value = new String (data, 0, ox, encoding); 474 if (key != null) { 475 putMapEntry(map, key, value); 476 key = null; 477 } 478 ox = 0; 479 break; 480 case '=': 481 if (key == null) { 482 key = new String (data, 0, ox, encoding); 483 ox = 0; 484 } else { 485 data[ox++] = c; 486 } 487 break; 488 case '+': 489 data[ox++] = (byte)' '; 490 break; 491 case '%': 492 data[ox++] = (byte)((convertHexDigit(data[ix++]) << 4) 493 + convertHexDigit(data[ix++])); 494 break; 495 default: 496 data[ox++] = c; 497 } 498 } 499 if (key != null) { 501 value = new String (data, 0, ox, encoding); 502 putMapEntry(map, key, value); 503 } 504 } 505 506 } 507 508 509 510 } 511 | Popular Tags |