@@ -263,10 +263,17 @@ public static Pair<String, Integer> validateUrl(String url) throws IllegalArgume
263263 }
264264
265265 public static Pair <String , Integer > validateUrl (String format , String url ) throws IllegalArgumentException {
266- return validateUrl (format , url , false );
266+ return validateUrl (format , url , false , false );
267267 }
268268
269- public static Pair <String , Integer > validateUrl (String format , String url , boolean skipIpv6Check ) throws IllegalArgumentException {
269+ /**
270+ * Verifies whether the provided URL is valid.
271+ * @param skipHostCheck if false, this function will verify whether the provided URL is resolvable, if it is a legal address and if it does not use IPv6 (configured by `skipIpv6Check`). If any of these conditions are false, an exception will be thrown.
272+ * @param skipIpv6Check if false, this function will verify whether the host uses IPv6 and, if it does, an exception will be thrown. This check is also skipped if `skipHostCheck` is true.
273+ * @return a pair containing the host and the corresponding port.
274+ * @throws IllegalArgumentException if the provided URL is invalid.
275+ */
276+ public static Pair <String , Integer > validateUrl (String format , String url , boolean skipHostCheck , boolean skipIpv6Check ) throws IllegalArgumentException {
270277 try {
271278 URI uri = new URI (url );
272279 if ((uri .getScheme () == null ) ||
@@ -282,16 +289,8 @@ public static Pair<String, Integer> validateUrl(String format, String url, boole
282289 }
283290
284291 String host = uri .getHost ();
285- try {
286- InetAddress hostAddr = InetAddress .getByName (host );
287- if (hostAddr .isAnyLocalAddress () || hostAddr .isLinkLocalAddress () || hostAddr .isLoopbackAddress () || hostAddr .isMulticastAddress ()) {
288- throw new IllegalArgumentException ("Illegal host specified in url" );
289- }
290- if (!skipIpv6Check && hostAddr instanceof Inet6Address ) {
291- throw new IllegalArgumentException ("IPV6 addresses not supported (" + hostAddr .getHostAddress () + ")" );
292- }
293- } catch (UnknownHostException uhe ) {
294- throw new IllegalArgumentException ("Unable to resolve " + host );
292+ if (!skipHostCheck ) {
293+ checkHost (host , skipIpv6Check );
295294 }
296295
297296 // verify format
@@ -305,6 +304,28 @@ public static Pair<String, Integer> validateUrl(String format, String url, boole
305304 }
306305 }
307306
307+ /**
308+ * Verifies whether the provided host is valid. Throws an `IllegalArgumentException` if:
309+ * <ul>
310+ * <li>The host is not resolvable;</li>
311+ * <li>The host address is illegal (any local, link local, loopback or multicast address);</li>
312+ * <li>The host uses IPv6. This check is skipped if `skipIv6Check` is set to true.</li>
313+ * </ul>
314+ */
315+ private static void checkHost (String host , boolean skipIpv6Check ) {
316+ try {
317+ InetAddress hostAddr = InetAddress .getByName (host );
318+ if (hostAddr .isAnyLocalAddress () || hostAddr .isLinkLocalAddress () || hostAddr .isLoopbackAddress () || hostAddr .isMulticastAddress ()) {
319+ throw new IllegalArgumentException ("Illegal host specified in URL." );
320+ }
321+ if (!skipIpv6Check && hostAddr instanceof Inet6Address ) {
322+ throw new IllegalArgumentException (String .format ("IPv6 addresses are not supported (%s)." , hostAddr .getHostAddress ()));
323+ }
324+ } catch (UnknownHostException uhe ) {
325+ throw new IllegalArgumentException (String .format ("Unable to resolve %s." , host ));
326+ }
327+ }
328+
308329 /**
309330 * Add element to priority list examining node attributes: priority (for urls) and type (for checksums)
310331 */
0 commit comments