| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.net; |
| 18 |
|
| 19 |
import java.util.ArrayList; |
| 20 |
import java.util.HashMap; |
| 21 |
import java.util.List; |
| 22 |
import java.util.Locale; |
| 23 |
import java.util.Set; |
| 24 |
import java.util.StringTokenizer; |
| 25 |
import java.util.regex.Matcher; |
| 26 |
import java.util.regex.Pattern; |
| 27 |
|
| 28 |
|
| 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 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public class UrlQuerySanitizer { |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public class ParameterValuePair { |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public ParameterValuePair(String parameter, |
| 78 |
String value) { |
| 79 |
mParameter = parameter; |
| 80 |
mValue = value; |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public String mParameter; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public String mValue; |
| 90 |
} |
| 91 |
|
| 92 |
final private HashMap<String, ValueSanitizer> mSanitizers = |
| 93 |
new HashMap<String, ValueSanitizer>(); |
| 94 |
final private HashMap<String, String> mEntries = |
| 95 |
new HashMap<String, String>(); |
| 96 |
final private ArrayList<ParameterValuePair> mEntriesList = |
| 97 |
new ArrayList<ParameterValuePair>(); |
| 98 |
private boolean mAllowUnregisteredParamaters; |
| 99 |
private boolean mPreferFirstRepeatedParameter; |
| 100 |
private ValueSanitizer mUnregisteredParameterValueSanitizer = |
| 101 |
getAllIllegal(); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static interface ValueSanitizer { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
public String sanitize(String value); |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
public static class IllegalCharacterValueSanitizer implements |
| 122 |
ValueSanitizer { |
| 123 |
private int mFlags; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
public final static int SPACE_OK = 1 << 0; |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public final static int OTHER_WHITESPACE_OK = 1 << 1; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public final static int NON_7_BIT_ASCII_OK = 1 << 2; |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public final static int DQUOTE_OK = 1 << 3; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public final static int SQUOTE_OK = 1 << 4; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public final static int LT_OK = 1 << 5; |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public final static int GT_OK = 1 << 6; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public final static int AMP_OK = 1 << 7; |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public final static int PCT_OK = 1 << 8; |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public final static int NUL_OK = 1 << 9; |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
public final static int SCRIPT_URL_OK = 1 << 10; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
public final static int ALL_OK = 0x7ff; |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
public final static int ALL_WHITESPACE_OK = |
| 182 |
SPACE_OK | OTHER_WHITESPACE_OK; |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
public final static int ALL_ILLEGAL = |
| 194 |
0; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
public final static int ALL_BUT_NUL_LEGAL = |
| 202 |
ALL_OK & ~NUL_OK; |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
public final static int ALL_BUT_WHITESPACE_LEGAL = |
| 214 |
ALL_OK & ~(ALL_WHITESPACE_OK | NUL_OK); |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public final static int URL_LEGAL = |
| 222 |
NON_7_BIT_ASCII_OK | SQUOTE_OK | AMP_OK | PCT_OK; |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
public final static int URL_AND_SPACE_LEGAL = |
| 231 |
URL_LEGAL | SPACE_OK; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public final static int AMP_LEGAL = |
| 239 |
AMP_OK; |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
public final static int AMP_AND_SPACE_LEGAL = |
| 248 |
AMP_OK | SPACE_OK; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
public final static int SPACE_LEGAL = |
| 256 |
SPACE_OK; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
public final static int ALL_BUT_NUL_AND_ANGLE_BRACKETS_LEGAL = |
| 268 |
ALL_OK & ~(NUL_OK | LT_OK | GT_OK); |
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
private final static String JAVASCRIPT_PREFIX = "javascript:"; |
| 275 |
|
| 276 |
private final static String VBSCRIPT_PREFIX = "vbscript:"; |
| 277 |
|
| 278 |
private final static int MIN_SCRIPT_PREFIX_LENGTH = Math.min( |
| 279 |
JAVASCRIPT_PREFIX.length(), VBSCRIPT_PREFIX.length()); |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
public IllegalCharacterValueSanitizer( |
| 287 |
int flags) { |
| 288 |
mFlags = flags; |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public String sanitize(String value) { |
| 306 |
if (value == null) { |
| 307 |
return null; |
| 308 |
} |
| 309 |
int length = value.length(); |
| 310 |
if ((mFlags & SCRIPT_URL_OK) == 0) { |
| 311 |
if (length >= MIN_SCRIPT_PREFIX_LENGTH) { |
| 312 |
String asLower = value.toLowerCase(Locale.ROOT); |
| 313 |
if (asLower.startsWith(JAVASCRIPT_PREFIX) || |
| 314 |
asLower.startsWith(VBSCRIPT_PREFIX)) { |
| 315 |
return ""; |
| 316 |
} |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
if ( (mFlags & ALL_WHITESPACE_OK) == 0) { |
| 323 |
value = trimWhitespace(value); |
| 324 |
|
| 325 |
|
| 326 |
length = value.length(); |
| 327 |
} |
| 328 |
|
| 329 |
StringBuilder stringBuilder = new StringBuilder(length); |
| 330 |
for(int i = 0; i < length; i++) { |
| 331 |
char c = value.charAt(i); |
| 332 |
if (!characterIsLegal(c)) { |
| 333 |
if ((mFlags & SPACE_OK) != 0) { |
| 334 |
c = ' '; |
| 335 |
} |
| 336 |
else { |
| 337 |
c = '_'; |
| 338 |
} |
| 339 |
} |
| 340 |
stringBuilder.append(c); |
| 341 |
} |
| 342 |
return stringBuilder.toString(); |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
private String trimWhitespace(String value) { |
| 354 |
int start = 0; |
| 355 |
int last = value.length() - 1; |
| 356 |
int end = last; |
| 357 |
while (start <= end && isWhitespace(value.charAt(start))) { |
| 358 |
start++; |
| 359 |
} |
| 360 |
while (end >= start && isWhitespace(value.charAt(end))) { |
| 361 |
end--; |
| 362 |
} |
| 363 |
if (start == 0 && end == last) { |
| 364 |
return value; |
| 365 |
} |
| 366 |
return value.substring(start, end + 1); |
| 367 |
} |
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
private boolean isWhitespace(char c) { |
| 375 |
switch(c) { |
| 376 |
case ' ': |
| 377 |
case '\t': |
| 378 |
case '\f': |
| 379 |
case '\n': |
| 380 |
case '\r': |
| 381 |
case 11: |
| 382 |
return true; |
| 383 |
default: |
| 384 |
return false; |
| 385 |
} |
| 386 |
} |
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
private boolean characterIsLegal(char c) { |
| 395 |
switch(c) { |
| 396 |
case ' ' : return (mFlags & SPACE_OK) != 0; |
| 397 |
case '\t': case '\f': case '\n': case '\r': case 11: |
| 398 |
return (mFlags & OTHER_WHITESPACE_OK) != 0; |
| 399 |
case '\"': return (mFlags & DQUOTE_OK) != 0; |
| 400 |
case '\'': return (mFlags & SQUOTE_OK) != 0; |
| 401 |
case '<' : return (mFlags & LT_OK) != 0; |
| 402 |
case '>' : return (mFlags & GT_OK) != 0; |
| 403 |
case '&' : return (mFlags & AMP_OK) != 0; |
| 404 |
case '%' : return (mFlags & PCT_OK) != 0; |
| 405 |
case '\0': return (mFlags & NUL_OK) != 0; |
| 406 |
default : return (c >= 32 && c < 127) || |
| 407 |
((c >= 128) && ((mFlags & NON_7_BIT_ASCII_OK) != 0)); |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
public ValueSanitizer getUnregisteredParameterValueSanitizer() { |
| 424 |
return mUnregisteredParameterValueSanitizer; |
| 425 |
} |
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
public void setUnregisteredParameterValueSanitizer( |
| 434 |
ValueSanitizer sanitizer) { |
| 435 |
mUnregisteredParameterValueSanitizer = sanitizer; |
| 436 |
} |
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
private static final ValueSanitizer sAllIllegal = |
| 442 |
new IllegalCharacterValueSanitizer( |
| 443 |
IllegalCharacterValueSanitizer.ALL_ILLEGAL); |
| 444 |
|
| 445 |
private static final ValueSanitizer sAllButNulLegal = |
| 446 |
new IllegalCharacterValueSanitizer( |
| 447 |
IllegalCharacterValueSanitizer.ALL_BUT_NUL_LEGAL); |
| 448 |
|
| 449 |
private static final ValueSanitizer sAllButWhitespaceLegal = |
| 450 |
new IllegalCharacterValueSanitizer( |
| 451 |
IllegalCharacterValueSanitizer.ALL_BUT_WHITESPACE_LEGAL); |
| 452 |
|
| 453 |
private static final ValueSanitizer sURLLegal = |
| 454 |
new IllegalCharacterValueSanitizer( |
| 455 |
IllegalCharacterValueSanitizer.URL_LEGAL); |
| 456 |
|
| 457 |
private static final ValueSanitizer sUrlAndSpaceLegal = |
| 458 |
new IllegalCharacterValueSanitizer( |
| 459 |
IllegalCharacterValueSanitizer.URL_AND_SPACE_LEGAL); |
| 460 |
|
| 461 |
private static final ValueSanitizer sAmpLegal = |
| 462 |
new IllegalCharacterValueSanitizer( |
| 463 |
IllegalCharacterValueSanitizer.AMP_LEGAL); |
| 464 |
|
| 465 |
private static final ValueSanitizer sAmpAndSpaceLegal = |
| 466 |
new IllegalCharacterValueSanitizer( |
| 467 |
IllegalCharacterValueSanitizer.AMP_AND_SPACE_LEGAL); |
| 468 |
|
| 469 |
private static final ValueSanitizer sSpaceLegal = |
| 470 |
new IllegalCharacterValueSanitizer( |
| 471 |
IllegalCharacterValueSanitizer.SPACE_LEGAL); |
| 472 |
|
| 473 |
private static final ValueSanitizer sAllButNulAndAngleBracketsLegal = |
| 474 |
new IllegalCharacterValueSanitizer( |
| 475 |
IllegalCharacterValueSanitizer.ALL_BUT_NUL_AND_ANGLE_BRACKETS_LEGAL); |
| 476 |
|
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
public static final ValueSanitizer getAllIllegal() { |
| 483 |
return sAllIllegal; |
| 484 |
} |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
public static final ValueSanitizer getAllButNulLegal() { |
| 492 |
return sAllButNulLegal; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
public static final ValueSanitizer getAllButWhitespaceLegal() { |
| 501 |
return sAllButWhitespaceLegal; |
| 502 |
} |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
public static final ValueSanitizer getUrlLegal() { |
| 509 |
return sURLLegal; |
| 510 |
} |
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
public static final ValueSanitizer getUrlAndSpaceLegal() { |
| 519 |
return sUrlAndSpaceLegal; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
public static final ValueSanitizer getAmpLegal() { |
| 527 |
return sAmpLegal; |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
public static final ValueSanitizer getAmpAndSpaceLegal() { |
| 535 |
return sAmpAndSpaceLegal; |
| 536 |
} |
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
public static final ValueSanitizer getSpaceLegal() { |
| 543 |
return sSpaceLegal; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
public static final ValueSanitizer getAllButNulAndAngleBracketsLegal() { |
| 552 |
return sAllButNulAndAngleBracketsLegal; |
| 553 |
} |
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
public UrlQuerySanitizer() { |
| 566 |
} |
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
public UrlQuerySanitizer(String url) { |
| 589 |
setAllowUnregisteredParamaters(true); |
| 590 |
parseUrl(url); |
| 591 |
} |
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
public void parseUrl(String url) { |
| 601 |
int queryIndex = url.indexOf('?'); |
| 602 |
String query; |
| 603 |
if (queryIndex >= 0) { |
| 604 |
query = url.substring(queryIndex + 1); |
| 605 |
} |
| 606 |
else { |
| 607 |
query = ""; |
| 608 |
} |
| 609 |
parseQuery(query); |
| 610 |
} |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
public void parseQuery(String query) { |
| 620 |
clear(); |
| 621 |
|
| 622 |
StringTokenizer tokenizer = new StringTokenizer(query, "&"); |
| 623 |
while(tokenizer.hasMoreElements()) { |
| 624 |
String attributeValuePair = tokenizer.nextToken(); |
| 625 |
if (attributeValuePair.length() > 0) { |
| 626 |
int assignmentIndex = attributeValuePair.indexOf('='); |
| 627 |
if (assignmentIndex < 0) { |
| 628 |
|
| 629 |
parseEntry(attributeValuePair, ""); |
| 630 |
} |
| 631 |
else { |
| 632 |
parseEntry(attributeValuePair.substring(0, assignmentIndex), |
| 633 |
attributeValuePair.substring(assignmentIndex + 1)); |
| 634 |
} |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
public Set<String> getParameterSet() { |
| 646 |
return mEntries.keySet(); |
| 647 |
} |
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
public List<ParameterValuePair> getParameterList() { |
| 656 |
return mEntriesList; |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
public boolean hasParameter(String parameter) { |
| 665 |
return mEntries.containsKey(parameter); |
| 666 |
} |
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
public String getValue(String parameter) { |
| 677 |
return mEntries.get(parameter); |
| 678 |
} |
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
public void registerParameter(String parameter, |
| 692 |
ValueSanitizer valueSanitizer) { |
| 693 |
if (valueSanitizer == null) { |
| 694 |
mSanitizers.remove(parameter); |
| 695 |
} |
| 696 |
mSanitizers.put(parameter, valueSanitizer); |
| 697 |
} |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
public void registerParameters(String[] parameters, |
| 706 |
ValueSanitizer valueSanitizer) { |
| 707 |
int length = parameters.length; |
| 708 |
for(int i = 0; i < length; i++) { |
| 709 |
mSanitizers.put(parameters[i], valueSanitizer); |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
public void setAllowUnregisteredParamaters( |
| 722 |
boolean allowUnregisteredParamaters) { |
| 723 |
mAllowUnregisteredParamaters = allowUnregisteredParamaters; |
| 724 |
} |
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
public boolean getAllowUnregisteredParamaters() { |
| 733 |
return mAllowUnregisteredParamaters; |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
|
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
|
| 749 |
public void setPreferFirstRepeatedParameter( |
| 750 |
boolean preferFirstRepeatedParameter) { |
| 751 |
mPreferFirstRepeatedParameter = preferFirstRepeatedParameter; |
| 752 |
} |
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
|
| 760 |
|
| 761 |
public boolean getPreferFirstRepeatedParameter() { |
| 762 |
return mPreferFirstRepeatedParameter; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
protected void parseEntry(String parameter, String value) { |
| 775 |
String unescapedParameter = unescape(parameter); |
| 776 |
ValueSanitizer valueSanitizer = |
| 777 |
getEffectiveValueSanitizer(unescapedParameter); |
| 778 |
|
| 779 |
if (valueSanitizer == null) { |
| 780 |
return; |
| 781 |
} |
| 782 |
String unescapedValue = unescape(value); |
| 783 |
String sanitizedValue = valueSanitizer.sanitize(unescapedValue); |
| 784 |
addSanitizedEntry(unescapedParameter, sanitizedValue); |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
protected void addSanitizedEntry(String parameter, String value) { |
| 794 |
mEntriesList.add( |
| 795 |
new ParameterValuePair(parameter, value)); |
| 796 |
if (mPreferFirstRepeatedParameter) { |
| 797 |
if (mEntries.containsKey(parameter)) { |
| 798 |
return; |
| 799 |
} |
| 800 |
} |
| 801 |
mEntries.put(parameter, value); |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
|
| 810 |
|
| 811 |
public ValueSanitizer getValueSanitizer(String parameter) { |
| 812 |
return mSanitizers.get(parameter); |
| 813 |
} |
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
public ValueSanitizer getEffectiveValueSanitizer(String parameter) { |
| 824 |
ValueSanitizer sanitizer = getValueSanitizer(parameter); |
| 825 |
if (sanitizer == null && mAllowUnregisteredParamaters) { |
| 826 |
sanitizer = getUnregisteredParameterValueSanitizer(); |
| 827 |
} |
| 828 |
return sanitizer; |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
private static final Pattern plusOrPercent = Pattern.compile("[+%]"); |
| 844 |
public String unescape(String string) { |
| 845 |
final Matcher matcher = plusOrPercent.matcher(string); |
| 846 |
if (!matcher.find()) return string; |
| 847 |
final int firstEscape = matcher.start(); |
| 848 |
|
| 849 |
int length = string.length(); |
| 850 |
|
| 851 |
StringBuilder stringBuilder = new StringBuilder(length); |
| 852 |
stringBuilder.append(string.substring(0, firstEscape)); |
| 853 |
for (int i = firstEscape; i < length; i++) { |
| 854 |
char c = string.charAt(i); |
| 855 |
if (c == '+') { |
| 856 |
c = ' '; |
| 857 |
} else if (c == '%' && i + 2 < length) { |
| 858 |
char c1 = string.charAt(i + 1); |
| 859 |
char c2 = string.charAt(i + 2); |
| 860 |
if (isHexDigit(c1) && isHexDigit(c2)) { |
| 861 |
c = (char) (decodeHexDigit(c1) * 16 + decodeHexDigit(c2)); |
| 862 |
i += 2; |
| 863 |
} |
| 864 |
} |
| 865 |
stringBuilder.append(c); |
| 866 |
} |
| 867 |
return stringBuilder.toString(); |
| 868 |
} |
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
protected boolean isHexDigit(char c) { |
| 877 |
return decodeHexDigit(c) >= 0; |
| 878 |
} |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
protected int decodeHexDigit(char c) { |
| 889 |
if (c >= '0' && c <= '9') { |
| 890 |
return c - '0'; |
| 891 |
} |
| 892 |
else if (c >= 'A' && c <= 'F') { |
| 893 |
return c - 'A' + 10; |
| 894 |
} |
| 895 |
else if (c >= 'a' && c <= 'f') { |
| 896 |
return c - 'a' + 10; |
| 897 |
} |
| 898 |
else { |
| 899 |
return -1; |
| 900 |
} |
| 901 |
} |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
protected void clear() { |
| 908 |
mEntries.clear(); |
| 909 |
mEntriesList.clear(); |
| 910 |
} |
| 911 |
} |
| 912 |
|