Removes /./ and /../ sequences from absolute URLs. Code borrowed heavily from CoyoteAdapter.normalize() @param cc the char chunk containing the chars to normalize
(CharChunk cc)
| 1494 | * @param cc the char chunk containing the chars to normalize |
| 1495 | */ |
| 1496 | private void normalize(CharChunk cc) { |
| 1497 | // Strip query string and/or fragment first as doing it this way makes |
| 1498 | // the normalization logic a lot simpler |
| 1499 | int truncate = cc.indexOf('?'); |
| 1500 | if (truncate == -1) { |
| 1501 | truncate = cc.indexOf('#'); |
| 1502 | } |
| 1503 | char[] truncateCC = null; |
| 1504 | if (truncate > -1) { |
| 1505 | truncateCC = Arrays.copyOfRange(cc.getBuffer(), cc.getStart() + truncate, cc.getEnd()); |
| 1506 | cc.setEnd(cc.getStart() + truncate); |
| 1507 | } |
| 1508 | |
| 1509 | if (cc.endsWith("/.") || cc.endsWith("/..")) { |
| 1510 | try { |
| 1511 | cc.append('/'); |
| 1512 | } catch (IOException ioe) { |
| 1513 | throw new IllegalArgumentException(cc.toString(), ioe); |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | char[] c = cc.getChars(); |
| 1518 | int start = cc.getStart(); |
| 1519 | int end = cc.getEnd(); |
| 1520 | int startIndex = 0; |
| 1521 | |
| 1522 | // Advance past the first three / characters (should place index just |
| 1523 | // scheme://host[:port] |
| 1524 | |
| 1525 | for (int i = 0; i < 3; i++) { |
| 1526 | startIndex = cc.indexOf('/', startIndex + 1); |
| 1527 | } |
| 1528 | |
| 1529 | // Remove /./ |
| 1530 | int index = startIndex; |
| 1531 | while (true) { |
| 1532 | index = cc.indexOf("/./", 0, 3, index); |
| 1533 | if (index < 0) { |
| 1534 | break; |
| 1535 | } |
| 1536 | copyChars(c, start + index, start + index + 2, end - start - index - 2); |
| 1537 | end = end - 2; |
| 1538 | cc.setEnd(end); |
| 1539 | } |
| 1540 | |
| 1541 | // Remove /../ |
| 1542 | index = startIndex; |
| 1543 | int pos; |
| 1544 | while (true) { |
| 1545 | index = cc.indexOf("/../", 0, 4, index); |
| 1546 | if (index < 0) { |
| 1547 | break; |
| 1548 | } |
| 1549 | // Can't go above the server root |
| 1550 | if (index == startIndex) { |
| 1551 | throw new IllegalArgumentException(); |
| 1552 | } |
| 1553 | int index2 = -1; |