From cc4209307cfc281fd7c11abbf7334d29791f7d2d Mon Sep 17 00:00:00 2001 From: James McMullan Date: Wed, 29 May 2024 09:23:21 -0400 Subject: [PATCH] HPCC4J-605 Connection: Improve Invalid URL Error Message - Added explicit check for underscores in hostname Signed-off-by: James McMullan James.McMullan@lexisnexis.com --- .../ws/client/utils/Connection.java | 25 ++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Connection.java b/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Connection.java index a28da183d..21e5e6ce6 100644 --- a/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Connection.java +++ b/wsclient/src/main/java/org/hpccsystems/ws/client/utils/Connection.java @@ -12,6 +12,8 @@ import java.util.Base64; import java.util.Base64.Decoder; import java.util.Base64.Encoder; +import java.util.regex.Matcher; +import java.util.regex.Pattern; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -287,7 +289,28 @@ public static boolean isSslProtocol(String protocol) */ public Connection(String connectionstring) throws MalformedURLException { - URL theurl = new URL(connectionstring); + URL theurl = null; + try + { + theurl = new URL(connectionstring); + } + catch (MalformedURLException e) + { + Pattern urlPattern = Pattern.compile("((https?|ftp|file):\\/\\/)?(([\\da-z\\.-_]+)\\.([a-z\\.]{2,6}))(:\\d{2,6})?([\\/\\w \\.-]*)*\\/?"); + Matcher matcher = urlPattern.matcher(connectionstring); + if (matcher.matches()) + { + String hostName = matcher.group(3); + if (hostName.contains("_")) + { + throw new MalformedURLException("Invalid URL: Check hostname for invalid underscores: '" + connectionstring + "': " + e.getMessage()); + } + } + else + { + throw e; + } + } setProtocol(theurl.getProtocol());