Convert a string, which may just be a hostname, into a valid URI. If no scheme is given, it is set to http by default. We prefer to use URI instead of URL since the latter requires a scheme handler to be registered for types, so strings like {@code docker://localh
(String rawUri)
| 70 | * method, that is not a problem. |
| 71 | */ |
| 72 | public static URI from(String rawUri) { |
| 73 | Require.nonNull("URL to convert", rawUri); |
| 74 | try { |
| 75 | // Things to consider: |
| 76 | // * A plain string hostname |
| 77 | // * A host represented as an IPv4 address |
| 78 | // * A host represented as an IPv6 address |
| 79 | // * A host represented as an abbreviated IPv6 address |
| 80 | |
| 81 | // If there's no colon, then we have a plain hostname |
| 82 | int colonIndex = rawUri.indexOf(':'); |
| 83 | int slashIndex = rawUri.indexOf('/'); |
| 84 | if (slashIndex == -1 && colonIndex == -1) { |
| 85 | return createHttpUri(rawUri); |
| 86 | } |
| 87 | |
| 88 | // Check the characters preceding the colon. If they're all numbers |
| 89 | // (or there's no preceding character), we're dealing with a short form |
| 90 | // ip6 address. |
| 91 | if (colonIndex != -1) { |
| 92 | if (colonIndex == 0) { |
| 93 | return createHttpUri(rawUri); |
| 94 | } |
| 95 | |
| 96 | if (isAllDigits(rawUri.substring(0, colonIndex))) { |
| 97 | return createHttpUri(rawUri); |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // If there's a slash immediately after the colon, that's a scheme |
| 102 | // and we can just create a URI from that. |
| 103 | return new URI(rawUri); |
| 104 | } catch (URISyntaxException e) { |
| 105 | throw new UncheckedIOException(new IOException(e)); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | private static boolean isAllDigits(final String input) { |
| 110 | for (int i = 0; i < input.length(); i++) { |