Skip to content

Commit 422ee47

Browse files
authored
HPCC4J-605 Connection: Improve Invalid URL Error Message (hpcc-systems#712)
Signed-off-by: James McMullan [email protected]
1 parent 0fffb9a commit 422ee47

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

wsclient/src/main/java/org/hpccsystems/ws/client/utils/Connection.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import java.util.Base64;
1313
import java.util.Base64.Decoder;
1414
import java.util.Base64.Encoder;
15+
import java.util.regex.Matcher;
16+
import java.util.regex.Pattern;
1517

1618
import org.apache.logging.log4j.LogManager;
1719
import org.apache.logging.log4j.Logger;
@@ -205,6 +207,9 @@ public int hashCode()
205207
private StringBuffer baseUrl;
206208
private StringBuffer uriAndParams;
207209

210+
// Note: this pattern is very basic and is only meant to extract hostnames from URLs
211+
public final static Pattern URL_HOSTNAME_PATTERN = Pattern.compile("((https?|ftp|file):\\/\\/)?(?<hostname>([\\da-z\\.\\-_]+)(\\.[a-z\\.]{2,6})?)(:\\d{2,6})?.*");
212+
208213
/** Constant <code>CONNECT_TIMEOUT_PARAM="connecttimeoutmillis"</code> */
209214
final static public String CONNECT_TIMEOUT_PARAM = "connecttimeoutmillis";
210215
/** Constant <code>READ_TIMEOUT_PARAM="readtimeoutmillis"</code> */
@@ -287,7 +292,27 @@ public static boolean isSslProtocol(String protocol)
287292
*/
288293
public Connection(String connectionstring) throws MalformedURLException
289294
{
290-
URL theurl = new URL(connectionstring);
295+
URL theurl = null;
296+
try
297+
{
298+
theurl = new URL(connectionstring);
299+
}
300+
catch (MalformedURLException e)
301+
{
302+
Matcher matcher = URL_HOSTNAME_PATTERN.matcher(connectionstring);
303+
if (matcher.matches())
304+
{
305+
String hostName = matcher.group("hostname");
306+
if (hostName.contains("_"))
307+
{
308+
throw new MalformedURLException("Invalid URL: Hostname contains invalid underscores: '" + connectionstring + "': " + e.getMessage());
309+
}
310+
}
311+
else
312+
{
313+
throw e;
314+
}
315+
}
291316

292317
setProtocol(theurl.getProtocol());
293318

wsclient/src/test/java/org/hpccsystems/ws/client/utils/ConnectionTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.*;
2020

2121
import java.net.MalformedURLException;
22+
import java.util.regex.Matcher;
2223

2324
import org.junit.Test;
2425

@@ -107,4 +108,27 @@ public void testInvalidProtHostPort() throws MalformedURLException
107108
assertFalse(con.getIsHttps());
108109
assertEquals(con.getProtocol(), http);
109110
}
111+
112+
@Test
113+
public void hostNamePatternTest() throws MalformedURLException
114+
{
115+
// Note: we want to test improved error messaging with underscores, but not all versions
116+
// of Java throw an exception for underscores in hostnames.
117+
// So we are testing the pattern instead
118+
String[] urls = {
119+
"https://invalid_host_name.test:8010?params",
120+
"https://invalid_host_name.test:8010",
121+
"http://invalid_host_name.test:8010",
122+
"invalid_host_name.test:8010",
123+
"invalid_host_name.test"
124+
};
125+
126+
String hostName = "invalid_host_name.test";
127+
for (String url : urls)
128+
{
129+
Matcher matcher = Connection.URL_HOSTNAME_PATTERN.matcher(url);
130+
assertTrue(matcher.matches());
131+
assertEquals(matcher.group("hostname"), hostName);
132+
}
133+
}
110134
}

0 commit comments

Comments
 (0)