Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HPCC4J-605 Connection: Improve Invalid URL Error Message #712

Merged
merged 1 commit into from
Jun 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -205,6 +207,9 @@ public int hashCode()
private StringBuffer baseUrl;
private StringBuffer uriAndParams;

// Note: this pattern is very basic and is only meant to extract hostnames from URLs
public final static Pattern URL_HOSTNAME_PATTERN = Pattern.compile("((https?|ftp|file):\\/\\/)?(?<hostname>([\\da-z\\.\\-_]+)(\\.[a-z\\.]{2,6})?)(:\\d{2,6})?.*");

/** Constant <code>CONNECT_TIMEOUT_PARAM="connecttimeoutmillis"</code> */
final static public String CONNECT_TIMEOUT_PARAM = "connecttimeoutmillis";
/** Constant <code>READ_TIMEOUT_PARAM="readtimeoutmillis"</code> */
Expand Down Expand Up @@ -287,7 +292,27 @@ 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)
{
Matcher matcher = URL_HOSTNAME_PATTERN.matcher(connectionstring);
if (matcher.matches())
{
String hostName = matcher.group("hostname");
if (hostName.contains("_"))
{
throw new MalformedURLException("Invalid URL: Hostname contains invalid underscores: '" + connectionstring + "': " + e.getMessage());
}
}
else
{
throw e;
}
}

setProtocol(theurl.getProtocol());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.*;

import java.net.MalformedURLException;
import java.util.regex.Matcher;

import org.junit.Test;

Expand Down Expand Up @@ -107,4 +108,27 @@ public void testInvalidProtHostPort() throws MalformedURLException
assertFalse(con.getIsHttps());
assertEquals(con.getProtocol(), http);
}

@Test
public void hostNamePatternTest() throws MalformedURLException
{
// Note: we want to test improved error messaging with underscores, but not all versions
// of Java throw an exception for underscores in hostnames.
// So we are testing the pattern instead
String[] urls = {
"https://invalid_host_name.test:8010?params",
"https://invalid_host_name.test:8010",
"http://invalid_host_name.test:8010",
"invalid_host_name.test:8010",
"invalid_host_name.test"
};

String hostName = "invalid_host_name.test";
for (String url : urls)
{
Matcher matcher = Connection.URL_HOSTNAME_PATTERN.matcher(url);
assertTrue(matcher.matches());
assertEquals(matcher.group("hostname"), hostName);
}
}
}
Loading