1 48 49 50 package com.caucho.portal.generic; 51 52 import java.io.IOException ; 53 import java.io.Writer ; 54 import java.util.LinkedHashSet ; 55 import java.util.Map ; 56 import java.util.Set ; 57 import java.util.regex.Matcher ; 58 import java.util.regex.Pattern ; 59 60 67 public class HttpUtil 68 { 69 74 private static Object _bufferLock = new Integer (1); 75 private static StringBuffer _buffer = new StringBuffer (256); 76 77 static private StringBuffer getStringBuffer() 78 { 79 StringBuffer buf; 80 81 synchronized (_bufferLock) { 82 buf = _buffer; 83 _buffer = null; 84 } 85 86 if (buf == null) 87 buf = new StringBuffer (256); 88 89 return buf; 90 } 91 92 static private void releaseStringBuffer(StringBuffer buf) 93 { 94 if (buf.capacity() <= 1024) { 95 96 synchronized (_bufferLock) { 97 if (_buffer == null || _buffer.capacity() < buf.capacity()) { 98 buf.setLength(0); 99 _buffer = buf; 100 } 101 } 102 } 103 } 104 105 private static Pattern _headerPattern 106 = Pattern.compile("s/[,;\\s]*([^;,\\s]+)[^,]*//"); 107 108 130 static public Set <String > getHeaderElements(String headerValue) 131 { 132 if (headerValue == null) 133 return null; 134 135 Matcher matcher = _headerPattern.matcher(headerValue); 136 137 Set <String > resultSet = null; 138 139 while (matcher.find()) { 140 141 if (resultSet == null) 142 return new LinkedHashSet <String >(); 143 144 resultSet.add(matcher.group(1)); 145 } 146 147 return resultSet; 148 } 149 150 160 static public String getFirstHeaderElement(String headerValue) 161 { 162 if (headerValue == null) 163 return null; 164 165 Matcher matcher = _headerPattern.matcher(headerValue); 166 167 if (matcher.find()) 168 return matcher.group(1); 169 else 170 return null; 171 } 172 173 183 static public String extractParameters(Map <String , String []> map, String url) 184 { 185 int beginIndex = url.indexOf('?'); 186 187 if (beginIndex == -1) 188 return url; 189 190 return extractParameters(map, url, beginIndex + 1); 191 } 192 193 203 static public String extractParameters(Map <String , String []> map, 204 String url, 205 int beginIndex) 206 { 207 if (beginIndex == -1) 208 return url; 209 210 String result = url.substring(0, beginIndex); 211 212 StringBuffer buf = getStringBuffer(); 213 214 if (buf == null) 215 buf = new StringBuffer (256); 216 217 String name = null; 218 String value = null; 219 220 int len = url.length(); 221 222 do { 223 int endIndex = url.indexOf('=', beginIndex); 224 225 if (endIndex == -1) { 226 endIndex = len; 227 } 228 else { 229 buf.setLength(0); 230 HttpUtil.decode(url, beginIndex, endIndex, buf); 231 name = buf.toString(); 232 } 233 234 if (endIndex == len) { 235 value = ""; 236 } 237 else { 238 beginIndex = endIndex + 1; 239 endIndex = url.indexOf('&', beginIndex); 240 241 if (endIndex == -1) 242 endIndex = len; 243 244 buf.setLength(0); 245 HttpUtil.decode(url, beginIndex, endIndex, buf); 246 value = buf.toString(); 247 } 248 249 String [] values = map.get(name); 250 251 if (values == null) { 252 map.put(name, new String [] { value }); 253 } 254 else { 255 int valuesLen = values.length; 256 257 String [] newValues = new String [valuesLen + 1]; 258 259 for (int valuesIndex = 0; valuesIndex < valuesLen; valuesIndex++) { 260 newValues[valuesIndex] = values[valuesIndex]; 261 } 262 263 newValues[valuesLen] = value; 264 265 map.put(name, newValues); 266 } 267 268 if (endIndex == len) 269 beginIndex = -1; 270 else 271 beginIndex = url.indexOf('&', endIndex) + 1; 272 273 } while (beginIndex > 0); 274 275 releaseStringBuffer(buf); 276 277 return result; 278 } 279 280 287 static public String encode(String source) 288 { 289 StringBuffer dest = getStringBuffer(); 290 291 encodeUri(source, 0, source.length(), dest); 292 293 String result = dest.toString(); 294 295 releaseStringBuffer(dest); 296 297 return result; 298 } 299 300 306 static public void encode(String source, StringBuffer dest) 307 { 308 encodeUri(source, 0, source.length(), dest); 309 } 310 311 319 static public void encode(String source, 320 int beginIndex, 321 int endIndex, 322 StringBuffer dest) 323 { 324 encodeUri(source, beginIndex, endIndex, dest); 325 } 326 327 335 static public void encode(StringBuffer source, 336 int beginIndex, 337 int endIndex, 338 StringBuffer dest) 339 { 340 encodeUri(source, beginIndex, endIndex, dest); 341 } 342 343 static public void encodeUri(CharSequence source, 344 int beginIndex, 345 int endIndex, 346 StringBuffer dest) 347 { 348 for (int i = beginIndex; i < endIndex; i++) { 349 char ch = source.charAt(i); 350 if ( (ch >= 'a' && ch <= 'z') 351 || (ch >= 'A' && ch <= 'z') 352 || (ch >= '0' && ch <= '9') 353 || ch == '-' 354 || ch == '_' 355 || ch == '.' 356 || ch == '*') 357 { 358 dest.append(ch); 359 } 360 else if (ch == ' ') { 361 dest.append('+'); 362 } 363 else if (ch <= 0xff) { 364 dest.append('%'); 366 dest.append(encodeHex(ch >> 4)); 367 dest.append(encodeHex(ch)); 368 } 369 else { 370 dest.append('%'); 372 dest.append('u'); 373 dest.append(encodeHex(ch >> 12)); 374 dest.append(encodeHex(ch >> 8)); 375 dest.append(encodeHex(ch >> 4)); 376 dest.append(encodeHex(ch)); 377 } 378 } 379 } 380 381 389 static public void encode(String source, Writer dest) 390 throws IOException 391 { 392 encodeUri(source, 0, source.length(), dest); 393 } 394 395 403 static public void encode(String source, 404 int beginIndex, 405 int endIndex, 406 Writer dest) 407 throws IOException 408 { 409 encodeUri(source, beginIndex, endIndex, dest); 410 } 411 412 420 static public void encode(StringBuffer source, 421 int beginIndex, 422 int endIndex, 423 Writer dest) 424 throws IOException 425 { 426 encodeUri(source, beginIndex, endIndex, dest); 427 } 428 429 static public void encodeUri(CharSequence source, 430 int beginIndex, 431 int endIndex, 432 Writer dest) 433 throws IOException 434 { 435 for (int i = beginIndex; i < endIndex; i++) { 436 char ch = source.charAt(i); 437 if ( (ch >= 'a' && ch <= 'z') 438 || (ch >= 'A' && ch <= 'z') 439 || (ch >= '0' && ch <= '9') 440 || ch == '-' 441 || ch == '_' 442 || ch == '.' 443 || ch == '*') 444 { 445 dest.write(ch); 446 } 447 else if (ch == ' ') { 448 dest.write('+'); 449 } 450 else if (ch <= 0xff) { 451 dest.write('%'); 453 dest.write(encodeHex(ch >> 4)); 454 dest.write(encodeHex(ch)); 455 } 456 else { 457 dest.write('%'); 459 dest.write('u'); 460 dest.write(encodeHex(ch >> 12)); 461 dest.write(encodeHex(ch >> 8)); 462 dest.write(encodeHex(ch >> 4)); 463 dest.write(encodeHex(ch)); 464 } 465 } 466 } 467 468 475 static public String decode(String source) 476 { 477 StringBuffer dest = getStringBuffer(); 478 479 decodeUri(source, 0, source.length(), dest); 480 481 String result = dest.toString(); 482 483 releaseStringBuffer(dest); 484 485 return result; 486 } 487 488 494 static public void decode(String source, StringBuffer dest) 495 { 496 decodeUri(source, 0, source.length(), dest); 497 } 498 499 507 static public void decode(String source, 508 int beginIndex, 509 int endIndex, 510 StringBuffer dest) 511 { 512 decodeUri(source, beginIndex, endIndex, dest); 513 } 514 515 523 static public void decode(StringBuffer source, 524 int beginIndex, 525 int endIndex, 526 StringBuffer dest) 527 { 528 decodeUri(source, beginIndex, endIndex, dest); 529 } 530 531 static private void decodeUri(CharSequence source, 532 int beginIndex, 533 int endIndex, 534 StringBuffer dest) 535 { 536 int i = beginIndex; 537 538 while (i < endIndex) { 539 char ch = source.charAt(i); 540 if (ch == '%') 541 i = scanUriEscape(source, i + 1, endIndex, dest); 542 else if (ch == '+') { 543 dest.append(' '); 544 i++; 545 } else { 546 dest.append(ch); 547 i++; 548 } 549 } 550 } 551 552 private static int scanUriEscape(CharSequence source, int i, int len, 553 StringBuffer dest ) 554 { 555 int ch1 = i < len ? ( ((int)source.charAt(i++)) & 0xff) : -1; 556 557 if (ch1 == 'u') { 558 ch1 = i < len ? ( ((int)source.charAt(i++)) & 0xff) : -1; 559 int ch2 = i < len ? ( ((int)source.charAt(i++)) & 0xff) : -1; 560 int ch3 = i < len ? ( ((int)source.charAt(i++)) & 0xff) : -1; 561 int ch4 = i < len ? ( ((int)source.charAt(i++)) & 0xff) : -1; 562 563 dest.append((char) ((decodeHex(ch1) << 12) + 564 (decodeHex(ch2) << 8) + 565 (decodeHex(ch3) << 4) + 566 (decodeHex(ch4)))); 567 } 568 else { 569 int ch2 = i < len ? ( ((int)source.charAt(i++)) & 0xff) : -1; 570 571 int b = (decodeHex(ch1) << 4) + decodeHex(ch2);; 572 573 dest.append( (char) b); 574 } 575 576 return i; 577 } 578 579 static char encodeHex(int ch) 580 { 581 ch &= 0xf; 582 if (ch < 10) 583 return (char) (ch + '0'); 584 else 585 return (char) (ch + 'a' - 10); 586 } 587 588 private static int decodeHex(int ch) 589 { 590 if (ch >= '0' && ch <= '9') 591 return ch - '0'; 592 else if (ch >= 'a' && ch <= 'f') 593 return ch - 'a' + 10; 594 else if (ch >= 'A' && ch <= 'F') 595 return ch - 'A' + 10; 596 else 597 return -1; 598 } 599 } 600 601 | Popular Tags |