Skip to content

Commit a905ecd

Browse files
authored
Merge pull request quarkusio#49915 from gamliela/handshake-peer-address
WebSockets Next: Expose localAddress and remoteAddress in HandshakeRequest
2 parents 6de1108 + 5db3917 commit a905ecd

File tree

6 files changed

+75
-13
lines changed

6 files changed

+75
-13
lines changed

extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/BasicConnectorTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,8 @@ void testClient() throws InterruptedException {
9191
})
9292
.onClose((c, s) -> closedLatch.countDown())
9393
.connectAndAwait();
94+
assertTrue(connection1.handshakeRequest().localAddress().matches("127\\.0\\.0\\.1:\\d+"));
95+
assertEquals("127.0.0.1:8081", connection1.handshakeRequest().remoteAddress());
9496
assertEquals("Lu", connection1.pathParam("name"));
9597
assertTrue(connection1.userData().get(TypedKey.forBoolean("boolean")));
9698
assertEquals(Integer.MAX_VALUE, connection1.userData().get(TypedKey.forInt("int")));
@@ -131,6 +133,8 @@ void testClient() throws InterruptedException {
131133
})
132134
.connectAndAwait();
133135
assertNotNull(connection2);
136+
assertTrue(connection2.handshakeRequest().localAddress().matches("127\\.0\\.0\\.1:\\d+"));
137+
assertEquals("127.0.0.1:8081", connection2.handshakeRequest().remoteAddress());
134138
assertTrue(conn2Latch.await(5, TimeUnit.SECONDS));
135139
}
136140

extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/handshake/HandshakeRequestTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.quarkus.websockets.next.test.handshake;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertTrue;
45

56
import java.net.URI;
67

@@ -48,6 +49,8 @@ void testHandshake() {
4849
assertEquals(baseUri.getPort(), reply.getInteger("port"));
4950
assertEquals("/head", reply.getString("path"));
5051
assertEquals(query, reply.getString("query"));
52+
assertEquals("127.0.0.1:8081", reply.getString("localAddress"));
53+
assertTrue(reply.getString("remoteAddress").matches("127\\.0\\.0\\.1:\\d+"));
5154
}
5255

5356
@WebSocket(path = "/head")
@@ -67,7 +70,9 @@ JsonObject process(String message) throws InterruptedException {
6770
.put("host", connection.handshakeRequest().host())
6871
.put("port", connection.handshakeRequest().port())
6972
.put("path", connection.handshakeRequest().path())
70-
.put("query", connection.handshakeRequest().query());
73+
.put("query", connection.handshakeRequest().query())
74+
.put("localAddress", connection.handshakeRequest().localAddress().toString())
75+
.put("remoteAddress", connection.handshakeRequest().remoteAddress().toString());
7176
}
7277

7378
}

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/HandshakeRequest.java

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,34 +43,44 @@ public interface HandshakeRequest {
4343

4444
/**
4545
*
46-
* @return the scheme
46+
* @return the scheme component of the server endpoint URL
4747
*/
4848
String scheme();
4949

5050
/**
5151
*
52-
* @return the host
52+
* @return the host component of the server endpoint URL
5353
*/
5454
String host();
5555

5656
/**
5757
*
58-
* @return the port
58+
* @return the port number of the server endpoint URL
5959
*/
6060
int port();
6161

6262
/**
6363
*
64-
* @return the path
64+
* @return the path component of the server endpoint URL
6565
*/
6666
String path();
6767

6868
/**
69-
*
70-
* @return the query string
69+
* @return the query string of the server endpoint URL
7170
*/
7271
String query();
7372

73+
/**
74+
*
75+
* @return the local IP address and port for this connection, or {@code null} if not available
76+
*/
77+
String localAddress();
78+
79+
/**
80+
* @return the remote IP address and port for this connection, or {@code null} if not available
81+
*/
82+
String remoteAddress();
83+
7484
/**
7585
* See <a href="https://datatracker.ietf.org/doc/html/rfc6455#section-11.3.1">Sec-WebSocket-Key</a>.
7686
*/
@@ -96,4 +106,4 @@ public interface HandshakeRequest {
96106
*/
97107
String SEC_WEBSOCKET_VERSION = "Sec-WebSocket-Version";
98108

99-
}
109+
}

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketClientConnectionImpl.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@ class WebSocketClientConnectionImpl extends WebSocketConnectionBase implements W
2626
Map<String, String> pathParams, URI serverEndpointUri, Map<String, List<String>> headers,
2727
TrafficLogger trafficLogger, Map<String, Object> userData, SendingInterceptor sendingInterceptor,
2828
Consumer<WebSocketClientConnection> cleanup) {
29-
super(Map.copyOf(pathParams), codecs, new ClientHandshakeRequestImpl(serverEndpointUri, headers), trafficLogger,
29+
super(Map.copyOf(pathParams), codecs,
30+
new ClientHandshakeRequestImpl(serverEndpointUri, Objects.requireNonNull(webSocket), headers), trafficLogger,
3031
new UserDataImpl(userData), sendingInterceptor);
3132
this.clientId = clientId;
32-
this.webSocket = Objects.requireNonNull(webSocket);
33+
this.webSocket = webSocket;
3334
this.cleanup = cleanup;
3435
}
3536

@@ -71,13 +72,15 @@ public boolean equals(Object obj) {
7172
return Objects.equals(identifier, other.identifier);
7273
}
7374

74-
private static class ClientHandshakeRequestImpl implements HandshakeRequest {
75+
private static class ClientHandshakeRequestImpl extends HandshakeRequestBase implements HandshakeRequest {
7576

7677
private final URI serverEndpointUrl;
78+
private final WebSocket webSocket;
7779
private final Map<String, List<String>> headers;
7880

79-
ClientHandshakeRequestImpl(URI serverEndpointUrl, Map<String, List<String>> headers) {
81+
ClientHandshakeRequestImpl(URI serverEndpointUrl, WebSocket webSocket, Map<String, List<String>> headers) {
8082
this.serverEndpointUrl = serverEndpointUrl;
83+
this.webSocket = webSocket;
8184
Map<String, List<String>> copy = new HashMap<>();
8285
for (Entry<String, List<String>> e : headers.entrySet()) {
8386
copy.put(e.getKey().toLowerCase(), List.copyOf(e.getValue()));
@@ -126,6 +129,16 @@ public String query() {
126129
return serverEndpointUrl.getQuery();
127130
}
128131

132+
@Override
133+
public String localAddress() {
134+
return formatSocketAddress(webSocket.localAddress());
135+
}
136+
137+
@Override
138+
public String remoteAddress() {
139+
return formatSocketAddress(webSocket.remoteAddress());
140+
}
141+
129142
}
130143

131144
}

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectionBase.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.vertx.core.http.WebSocketBase;
2323
import io.vertx.core.json.JsonArray;
2424
import io.vertx.core.json.JsonObject;
25+
import io.vertx.core.net.SocketAddress;
2526

2627
public abstract class WebSocketConnectionBase implements Connection {
2728

@@ -192,4 +193,23 @@ public UserData userData() {
192193
return userData;
193194
}
194195

196+
protected static class HandshakeRequestBase {
197+
198+
protected String formatSocketAddress(SocketAddress socketAddress) {
199+
if (socketAddress == null) {
200+
return null;
201+
}
202+
if (socketAddress.isInetSocket() && socketAddress.hostAddress() != null) {
203+
return socketAddress.hostAddress() + ":" + socketAddress.port();
204+
}
205+
if (socketAddress.isInetSocket() && socketAddress.hostName() != null) {
206+
return socketAddress.hostName() + ":" + socketAddress.port();
207+
}
208+
if (socketAddress.isDomainSocket()) {
209+
return socketAddress.path();
210+
}
211+
return null;
212+
}
213+
}
214+
195215
}

extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectionImpl.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public boolean equals(Object obj) {
9797
return Objects.equals(identifier, other.identifier);
9898
}
9999

100-
private static class HandshakeRequestImpl implements HandshakeRequest {
100+
private static class HandshakeRequestImpl extends HandshakeRequestBase implements HandshakeRequest {
101101

102102
private final ServerWebSocket webSocket;
103103

@@ -149,6 +149,16 @@ public String query() {
149149
return webSocket.query();
150150
}
151151

152+
@Override
153+
public String localAddress() {
154+
return formatSocketAddress(webSocket.localAddress());
155+
}
156+
157+
@Override
158+
public String remoteAddress() {
159+
return formatSocketAddress(webSocket.remoteAddress());
160+
}
161+
152162
static Map<String, List<String>> initHeaders(RoutingContext ctx) {
153163
Map<String, List<String>> headers = new HashMap<>();
154164
for (Entry<String, String> e : ctx.request().headers()) {

0 commit comments

Comments
 (0)