Skip to content

Commit 990ee70

Browse files
committed
JVMCBC-1588 Fix parsing connection string where parameter value is URI
Motivation ---------- With the upcoming application telemetry feature, we can expect users to specify the telemetry endpoint with a connection string like this (note absent scheme): 127.0.0.1?app_telemetry_endpoint=ws://example.com When the scheme is omitted as in this example, and there's a parameter value that contains a URI, the connection string parser erroneously treats everything before "://" as the connection string scheme. This fails. Modifications ------------- Adjust the connection string scheme regex to disallow question marks. Rejected Alternatives --------------------- RFC 2396 defines URI scheme as: scheme = alpha *( alpha | digit | "+" | "-" | "." ) where alpha is [a-zA-Z] and digit is [0-9]. By that definition, a strict Java regex for our scheme would be: "((?<scheme>\\p{Alpha}[\\p{Alnum}+.-]*?)://)?" but that seemed needlessly pedantic for our purposes. Change-Id: Iafed4cec5cde5ba00ea607b844b3c941b49748cd Reviewed-on: https://review.couchbase.org/c/couchbase-jvm-clients/+/221172 Tested-by: Build Bot <[email protected]> Reviewed-by: Michael Reiche <[email protected]>
1 parent 234215e commit 990ee70

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

core-io/src/main/java/com/couchbase/client/core/util/ConnectionString.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class ConnectionString {
4949
public static final String DEFAULT_SCHEME = "couchbase://";
5050

5151
private static final Pattern connectionStringPattern = Pattern.compile(
52-
"((?<scheme>.*?)://)?" +
52+
"((?<scheme>[^?]*?)://)?" +
5353
"((?<user>.*?)@)?" +
5454
"(?<hosts>.*?)" +
5555
"(\\?(?<params>.*?))?"

core-io/src/test/java/com/couchbase/client/core/util/ConnectionStringTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ void shouldFailOnInvalidScheme() {
127127
assertThrows(CouchbaseException.class, () -> ConnectionString.create("invalid://"));
128128
}
129129

130+
@Test
131+
void shouldParseParameterValueThatLooksLikeUri() {
132+
ConnectionString cs = ConnectionString.create("couchbase://127.0.0.1?uri=ws://example.com");
133+
assertEquals(COUCHBASE, cs.scheme());
134+
assertEquals("127.0.0.1", cs.hosts().get(0).host());
135+
assertEquals(mapOf("uri", "ws://example.com"), cs.params());
136+
}
137+
138+
@Test
139+
void shouldParseParameterValueThatLooksLikeUriEvenIfSchemeIsOmitted() {
140+
ConnectionString cs = ConnectionString.create("127.0.0.1?uri=ws://example.com");
141+
assertEquals(COUCHBASE, cs.scheme());
142+
assertEquals("127.0.0.1", cs.hosts().get(0).host());
143+
assertEquals(mapOf("uri", "ws://example.com"), cs.params());
144+
}
145+
130146
@Test
131147
void shouldParseHostList() {
132148
ConnectionString parsed = ConnectionString.create("couchbase://localhost");

0 commit comments

Comments
 (0)