Skip to content

Commit

Permalink
Adjusting how we clean urls (#2002)
Browse files Browse the repository at this point in the history
  • Loading branch information
Paultagoras authored Dec 5, 2024
1 parent fdcac72 commit 252aa6c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,44 @@ public boolean isDisableFrameworkDetection() {
}

public JdbcConfiguration(String url, Properties info) {
this.allProperties = new ConcurrentHashMap<>();
info.forEach((k, v) -> allProperties.put(k.toString(), v.toString()));

this.jdbcUrl = url;//Raw URL
this.url = cleanUrl(url);
this.user = info.getProperty("user", "default");
this.password = info.getProperty("password", "");
this.disableFrameworkDetection = Boolean.parseBoolean(info.getProperty("disable_frameworks_detection", "false"));
this.allProperties = new ConcurrentHashMap<>();
info.forEach((k, v) -> allProperties.put(k.toString(), v.toString()));
}

public static boolean acceptsURL(String url) {
return url.startsWith(PREFIX_CLICKHOUSE) || url.startsWith(PREFIX_CLICKHOUSE_SHORT);
}

private String cleanUrl(String url) {
protected String cleanUrl(String url) {
url = stripUrlPrefix(url);
boolean setSSL = false;
boolean ssl = false;
try {
ssl = Boolean.parseBoolean(allProperties.get("ssl"));
setSSL = true;
} catch (Exception e) {
log.trace("Failed to parse SSL property.", e);
}

if (url.startsWith("//")) {
url = "http:" + url;//Default to HTTP
try {
URL parsedUrl = new URL(url);
if (parsedUrl.getPort() == ClickHouseHttpProto.DEFAULT_HTTPS_PORT) {//If port is 8443, switch to HTTPS
url = "https:" + url;
if (setSSL) {
url = (ssl ? "https:" : "http:") + url;
} else {
url = "http:" + url;//Default to HTTP
try {
URL parsedUrl = new URL(url);
if (parsedUrl.getPort() == ClickHouseHttpProto.DEFAULT_HTTPS_PORT) {//If port is 8443, switch to HTTPS
url = "https:" + url.substring(5);
}
} catch (MalformedURLException e) {
throw new IllegalArgumentException("URL is not valid.", e);
}
} catch (MalformedURLException e) {
throw new IllegalArgumentException("URL is not valid.", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.clickhouse.jdbc.internal;

import com.clickhouse.jdbc.JdbcIntegrationTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.util.Properties;

import static org.testng.Assert.assertEquals;

public class JdbcConfigurationTest extends JdbcIntegrationTest {
@Test(groups = { "integration" })
public void testCleanUrl() {
JdbcConfiguration configuration = new JdbcConfiguration("jdbc:clickhouse://localhost:8123/clickhouse?param1=value1&param2=value2", new Properties());

String url = "jdbc:clickhouse://localhost:8123/clickhouse?param1=value1&param2=value2";
String cleanUrl = configuration.cleanUrl(url);
assertEquals(cleanUrl, "http://localhost:8123/clickhouse?param1=value1&param2=value2");

url = "jdbc:clickhouse://localhost:8443/clickhouse?param1=value1&param2=value2";
cleanUrl = configuration.cleanUrl(url);
assertEquals(cleanUrl, "https://localhost:8443/clickhouse?param1=value1&param2=value2");

Properties info = new Properties();
info.setProperty("ssl", "true");
configuration = new JdbcConfiguration("jdbc:clickhouse://localhost:8123/clickhouse?param1=value1&param2=value2", info);
cleanUrl = configuration.cleanUrl(url);
assertEquals(cleanUrl, "https://localhost:8123/clickhouse?param1=value1&param2=value2");

info.setProperty("ssl", "false");
configuration = new JdbcConfiguration("jdbc:clickhouse://localhost:8443/clickhouse?param1=value1&param2=value2", info);
cleanUrl = configuration.cleanUrl(url);
assertEquals(cleanUrl, "http://localhost:8443/clickhouse?param1=value1&param2=value2");
}
}

0 comments on commit 252aa6c

Please sign in to comment.