Normalize the given hier path part. Algorithm taken from URI reference parser at http://www.apache.org/~fielding/uri/rev-2002/issues.html. @param path the path to normalize @return the normalized path @throws URIException no more higher path level to be normalized
(char[] path)
| 3477 | * @throws URIException no more higher path level to be normalized |
| 3478 | */ |
| 3479 | protected char[] normalize(char[] path) throws URIException { |
| 3480 | |
| 3481 | if (path == null) { |
| 3482 | return null; |
| 3483 | } |
| 3484 | |
| 3485 | String normalized = new String(path); |
| 3486 | |
| 3487 | // If the buffer begins with "./" or "../", the "." or ".." is removed. |
| 3488 | if (normalized.startsWith("./")) { |
| 3489 | normalized = normalized.substring(1); |
| 3490 | } else if (normalized.startsWith("../")) { |
| 3491 | normalized = normalized.substring(2); |
| 3492 | } else if (normalized.startsWith("..")) { |
| 3493 | normalized = normalized.substring(2); |
| 3494 | } |
| 3495 | |
| 3496 | // All occurrences of "/./" in the buffer are replaced with "/" |
| 3497 | int index = -1; |
| 3498 | while ((index = normalized.indexOf("/./")) != -1) { |
| 3499 | normalized = normalized.substring(0, index) + normalized.substring(index + 2); |
| 3500 | } |
| 3501 | |
| 3502 | // If the buffer ends with "/.", the "." is removed. |
| 3503 | if (normalized.endsWith("/.")) { |
| 3504 | normalized = normalized.substring(0, normalized.length() - 1); |
| 3505 | } |
| 3506 | |
| 3507 | int startIndex = 0; |
| 3508 | |
| 3509 | // All occurrences of "/<segment>/../" in the buffer, where ".." |
| 3510 | // and <segment> are complete path segments, are iteratively replaced |
| 3511 | // with "/" in order from left to right until no matching pattern remains. |
| 3512 | // If the buffer ends with "/<segment>/..", that is also replaced |
| 3513 | // with "/". Note that <segment> may be empty. |
| 3514 | while ((index = normalized.indexOf("/../", startIndex)) != -1) { |
| 3515 | int slashIndex = normalized.lastIndexOf('/', index - 1); |
| 3516 | if (slashIndex >= 0) { |
| 3517 | normalized = normalized.substring(0, slashIndex) + normalized.substring(index + 3); |
| 3518 | } else { |
| 3519 | startIndex = index + 3; |
| 3520 | } |
| 3521 | } |
| 3522 | if (normalized.endsWith("/..")) { |
| 3523 | int slashIndex = normalized.lastIndexOf('/', normalized.length() - 4); |
| 3524 | if (slashIndex >= 0) { |
| 3525 | normalized = normalized.substring(0, slashIndex + 1); |
| 3526 | } |
| 3527 | } |
| 3528 | |
| 3529 | // All prefixes of "<segment>/../" in the buffer, where ".." |
| 3530 | // and <segment> are complete path segments, are iteratively replaced |
| 3531 | // with "/" in order from left to right until no matching pattern remains. |
| 3532 | // If the buffer ends with "<segment>/..", that is also replaced |
| 3533 | // with "/". Note that <segment> may be empty. |
| 3534 | while ((index = normalized.indexOf("/../")) != -1) { |
| 3535 | int slashIndex = normalized.lastIndexOf('/', index - 1); |
| 3536 | if (slashIndex >= 0) { |
no test coverage detected