Skip to content

Commit 252aa6c

Browse files
authored
Adjusting how we clean urls (#2002)
1 parent fdcac72 commit 252aa6c

File tree

2 files changed

+59
-10
lines changed

2 files changed

+59
-10
lines changed

jdbc-v2/src/main/java/com/clickhouse/jdbc/internal/JdbcConfiguration.java

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,30 +50,44 @@ public boolean isDisableFrameworkDetection() {
5050
}
5151

5252
public JdbcConfiguration(String url, Properties info) {
53+
this.allProperties = new ConcurrentHashMap<>();
54+
info.forEach((k, v) -> allProperties.put(k.toString(), v.toString()));
55+
5356
this.jdbcUrl = url;//Raw URL
5457
this.url = cleanUrl(url);
5558
this.user = info.getProperty("user", "default");
5659
this.password = info.getProperty("password", "");
5760
this.disableFrameworkDetection = Boolean.parseBoolean(info.getProperty("disable_frameworks_detection", "false"));
58-
this.allProperties = new ConcurrentHashMap<>();
59-
info.forEach((k, v) -> allProperties.put(k.toString(), v.toString()));
6061
}
6162

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

66-
private String cleanUrl(String url) {
67+
protected String cleanUrl(String url) {
6768
url = stripUrlPrefix(url);
69+
boolean setSSL = false;
70+
boolean ssl = false;
71+
try {
72+
ssl = Boolean.parseBoolean(allProperties.get("ssl"));
73+
setSSL = true;
74+
} catch (Exception e) {
75+
log.trace("Failed to parse SSL property.", e);
76+
}
77+
6878
if (url.startsWith("//")) {
69-
url = "http:" + url;//Default to HTTP
70-
try {
71-
URL parsedUrl = new URL(url);
72-
if (parsedUrl.getPort() == ClickHouseHttpProto.DEFAULT_HTTPS_PORT) {//If port is 8443, switch to HTTPS
73-
url = "https:" + url;
79+
if (setSSL) {
80+
url = (ssl ? "https:" : "http:") + url;
81+
} else {
82+
url = "http:" + url;//Default to HTTP
83+
try {
84+
URL parsedUrl = new URL(url);
85+
if (parsedUrl.getPort() == ClickHouseHttpProto.DEFAULT_HTTPS_PORT) {//If port is 8443, switch to HTTPS
86+
url = "https:" + url.substring(5);
87+
}
88+
} catch (MalformedURLException e) {
89+
throw new IllegalArgumentException("URL is not valid.", e);
7490
}
75-
} catch (MalformedURLException e) {
76-
throw new IllegalArgumentException("URL is not valid.", e);
7791
}
7892
}
7993

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.clickhouse.jdbc.internal;
2+
3+
import com.clickhouse.jdbc.JdbcIntegrationTest;
4+
import org.testng.annotations.BeforeTest;
5+
import org.testng.annotations.Test;
6+
7+
import java.util.Properties;
8+
9+
import static org.testng.Assert.assertEquals;
10+
11+
public class JdbcConfigurationTest extends JdbcIntegrationTest {
12+
@Test(groups = { "integration" })
13+
public void testCleanUrl() {
14+
JdbcConfiguration configuration = new JdbcConfiguration("jdbc:clickhouse://localhost:8123/clickhouse?param1=value1&param2=value2", new Properties());
15+
16+
String url = "jdbc:clickhouse://localhost:8123/clickhouse?param1=value1&param2=value2";
17+
String cleanUrl = configuration.cleanUrl(url);
18+
assertEquals(cleanUrl, "http://localhost:8123/clickhouse?param1=value1&param2=value2");
19+
20+
url = "jdbc:clickhouse://localhost:8443/clickhouse?param1=value1&param2=value2";
21+
cleanUrl = configuration.cleanUrl(url);
22+
assertEquals(cleanUrl, "https://localhost:8443/clickhouse?param1=value1&param2=value2");
23+
24+
Properties info = new Properties();
25+
info.setProperty("ssl", "true");
26+
configuration = new JdbcConfiguration("jdbc:clickhouse://localhost:8123/clickhouse?param1=value1&param2=value2", info);
27+
cleanUrl = configuration.cleanUrl(url);
28+
assertEquals(cleanUrl, "https://localhost:8123/clickhouse?param1=value1&param2=value2");
29+
30+
info.setProperty("ssl", "false");
31+
configuration = new JdbcConfiguration("jdbc:clickhouse://localhost:8443/clickhouse?param1=value1&param2=value2", info);
32+
cleanUrl = configuration.cleanUrl(url);
33+
assertEquals(cleanUrl, "http://localhost:8443/clickhouse?param1=value1&param2=value2");
34+
}
35+
}

0 commit comments

Comments
 (0)