distinguish between IPv4, IPv6, domain name and validate it based on its type
(boolean forceServer, String host)
| 567 | * its type |
| 568 | */ |
| 569 | private boolean isValidHost(boolean forceServer, String host) |
| 570 | throws URISyntaxException { |
| 571 | if (host.charAt(0) == '[') { |
| 572 | // ipv6 address |
| 573 | if (host.charAt(host.length() - 1) != ']') { |
| 574 | throw new URISyntaxException(host, |
| 575 | Messages.getString("luni.8D"), 0); //$NON-NLS-1$ |
| 576 | } |
| 577 | if (!isValidIP6Address(host)) { |
| 578 | throw new URISyntaxException(host, Messages.getString("luni.8E")); //$NON-NLS-1$ |
| 579 | } |
| 580 | return true; |
| 581 | } |
| 582 | |
| 583 | // '[' and ']' can only be the first char and last char |
| 584 | // of the host name |
| 585 | if (host.indexOf('[') != -1 || host.indexOf(']') != -1) { |
| 586 | throw new URISyntaxException(host, Messages.getString("luni.8F"), 0); //$NON-NLS-1$ |
| 587 | } |
| 588 | |
| 589 | int index = host.lastIndexOf('.'); |
| 590 | if (index < 0 || index == host.length() - 1 |
| 591 | || !Character.isDigit(host.charAt(index + 1))) { |
| 592 | // domain name |
| 593 | if (isValidDomainName(host)) { |
| 594 | return true; |
| 595 | } |
| 596 | if (forceServer) { |
| 597 | throw new URISyntaxException(host, |
| 598 | Messages.getString("luni.8F"), 0); //$NON-NLS-1$ |
| 599 | } |
| 600 | return false; |
| 601 | } |
| 602 | |
| 603 | // IPv4 address |
| 604 | if (isValidIPv4Address(host)) { |
| 605 | return true; |
| 606 | } |
| 607 | if (forceServer) { |
| 608 | throw new URISyntaxException(host, Messages.getString("luni.90"), 0); //$NON-NLS-1$ |
| 609 | } |
| 610 | return false; |
| 611 | } |
| 612 | |
| 613 | private boolean isValidDomainName(String host) { |
| 614 | try { |
no test coverage detected