Update localAddress handling in StandardWebSocketClient

Closes gh-34331
This commit is contained in:
rstoyanchev 2025-01-28 15:45:06 +00:00
parent 52c187bf64
commit 83cdd58ac8
3 changed files with 12 additions and 23 deletions

View File

@ -73,11 +73,9 @@ public interface WebSocketSession extends Closeable {
/** /**
* Return the address on which the request was received. * Return the address on which the request was received.
* <p><strong>Note:</strong> The localAddress is not always possible to access, * <p><strong>Note:</strong> The localAddress is not always possible to access,
* which is the case with the Standard WebSocket client. In 6.2.x * which is the case with the Standard WebSocket client API, and accordingly
* {@link org.springframework.web.socket.client.standard.StandardWebSocketClient} * {@link org.springframework.web.socket.client.standard.StandardWebSocketClient}
* returns an address based on the local host and the port of the target * returns {@code null}.
* address (not the same as the local port). In 7.0, the same will return
* {@code null} instead.
*/ */
@Nullable InetSocketAddress getLocalAddress(); @Nullable InetSocketAddress getLocalAddress();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -149,12 +149,10 @@ public class StandardWebSocketClient extends AbstractWebSocketClient {
HttpHeaders headers, final URI uri, List<String> protocols, HttpHeaders headers, final URI uri, List<String> protocols,
List<WebSocketExtension> extensions, Map<String, Object> attributes) { List<WebSocketExtension> extensions, Map<String, Object> attributes) {
int port = getPort(uri); InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), getPort(uri));
InetSocketAddress localAddress = new InetSocketAddress(getLocalHost(), port);
InetSocketAddress remoteAddress = new InetSocketAddress(uri.getHost(), port);
StandardWebSocketSession session = new StandardWebSocketSession(headers, StandardWebSocketSession session =
attributes, localAddress, remoteAddress); new StandardWebSocketSession(headers, attributes, null, remoteAddress);
ClientEndpointConfig endpointConfig = ClientEndpointConfig.Builder.create() ClientEndpointConfig endpointConfig = ClientEndpointConfig.Builder.create()
.configurator(new StandardWebSocketClientConfigurator(headers)) .configurator(new StandardWebSocketClientConfigurator(headers))

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2024 the original author or authors. * Copyright 2002-2025 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -60,18 +60,11 @@ class StandardWebSocketClientTests {
void getLocalAddress() throws Exception { void getLocalAddress() throws Exception {
URI uri = URI.create("ws://localhost/abc"); URI uri = URI.create("ws://localhost/abc");
WebSocketSession session = this.wsClient.execute(this.wsHandler, this.headers, uri).get(); WebSocketSession session = this.wsClient.execute(this.wsHandler, this.headers, uri).get();
assertThat(session.getLocalAddress()).isNull();
assertThat(session.getLocalAddress()).isNotNull(); uri = URI.create("wss://localhost/abc");
assertThat(session.getLocalAddress().getPort()).isEqualTo(80); session = this.wsClient.execute(this.wsHandler, this.headers, uri).get();
} assertThat(session.getLocalAddress()).isNull();
@Test
void getLocalAddressWss() throws Exception {
URI uri = URI.create("wss://localhost/abc");
WebSocketSession session = this.wsClient.execute(this.wsHandler, this.headers, uri).get();
assertThat(session.getLocalAddress()).isNotNull();
assertThat(session.getLocalAddress().getPort()).isEqualTo(443);
} }
@Test @Test
@ -88,7 +81,7 @@ class StandardWebSocketClientTests {
assertThat(session.getRemoteAddress()).isNotNull(); assertThat(session.getRemoteAddress()).isNotNull();
assertThat(session.getRemoteAddress().getHostName()).isEqualTo("localhost"); assertThat(session.getRemoteAddress().getHostName()).isEqualTo("localhost");
assertThat(session.getLocalAddress().getPort()).isEqualTo(443); assertThat(session.getLocalAddress()).isNull();
} }
@Test @Test