Skip to content

Commit fdb675c

Browse files
authoredMar 17, 2025··
Merge pull request #1286 from nats-io/websocket-never-resolve-hosts
[BUG] Hosts should never be resolved on websocket URI's
2 parents d4a9c3e + ee59342 commit fdb675c

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed
 

‎src/main/java/io/nats/client/impl/NatsConnection.java

+7-4
Original file line numberDiff line numberDiff line change
@@ -1901,23 +1901,26 @@ public Collection<String> getServers() {
19011901
}
19021902

19031903
protected List<NatsUri> resolveHost(NatsUri nuri) {
1904-
// 1. If the host is not an ip address, let the pool resolve it.
1904+
// 1. If the nuri host is not already an ip address or the nuri is not for websocket
1905+
// let the pool resolve it.
19051906
List<NatsUri> results = new ArrayList<>();
1906-
if (!nuri.hostIsIpAddress()) {
1907+
if (!nuri.hostIsIpAddress() && !nuri.isWebsocket()) {
19071908
List<String> ips = serverPool.resolveHostToIps(nuri.getHost());
19081909
if (ips != null) {
19091910
for (String ip : ips) {
19101911
try {
19111912
results.add(nuri.reHost(ip));
1912-
} catch (URISyntaxException u) {
1913+
}
1914+
catch (URISyntaxException u) {
19131915
// ??? should never happen
19141916
}
19151917
}
19161918
}
19171919
}
19181920

19191921
// 2. If there were no results,
1920-
// - host was an ip address or
1922+
// - host was already an ip address or
1923+
// - host was for websocket or
19211924
// - pool returned nothing or
19221925
// - resolving failed...
19231926
// so the list just becomes the original host.

‎src/main/java/io/nats/client/support/NatsConstants.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public interface NatsConstants {
3232

3333
List<String> KNOWN_PROTOCOLS = Arrays.asList(NATS_PROTOCOL, TLS_PROTOCOL, OPENTLS_PROTOCOL, WEBSOCKET_PROTOCOL, SECURE_WEBSOCKET_PROTOCOL);
3434
List<String> SECURE_PROTOCOLS = Arrays.asList(TLS_PROTOCOL, OPENTLS_PROTOCOL, SECURE_WEBSOCKET_PROTOCOL);
35-
List<String> WSS_PROTOCOLS = Arrays.asList(WEBSOCKET_PROTOCOL, SECURE_WEBSOCKET_PROTOCOL);
35+
List<String> WEBSOCKET_PROTOCOLS = Arrays.asList(WEBSOCKET_PROTOCOL, SECURE_WEBSOCKET_PROTOCOL);
3636

3737
String SPACE = " ";
3838
String EMPTY = "";
@@ -94,4 +94,7 @@ public interface NatsConstants {
9494
String OUTPUT_QUEUE_IS_FULL = "Output queue is full ";
9595

9696
long NANOS_PER_MILLI = 1_000_000L;
97+
98+
@Deprecated
99+
List<String> WSS_PROTOCOLS = WEBSOCKET_PROTOCOLS;
97100
}

‎src/main/java/io/nats/client/support/NatsUri.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public NatsUri(String url, String defaultScheme) throws URISyntaxException {
221221
private void postConstruct() {
222222
String s = uri.getScheme().toLowerCase();
223223
isSecure = SECURE_PROTOCOLS.contains(s);
224-
isWebsocket = WSS_PROTOCOLS.contains(s);
224+
isWebsocket = WEBSOCKET_PROTOCOLS.contains(s);
225225
s = uri.getHost();
226226
hostIsIpAddress = IPV4_RE.matcher(s).matches() || s.startsWith("[") && s.endsWith("]");
227227
}

0 commit comments

Comments
 (0)
Please sign in to comment.