From 76af842d2865214eb419bedb7e1f8f509b9a544b Mon Sep 17 00:00:00 2001 From: Martin Kouba Date: Wed, 5 Jun 2024 16:53:10 +0200 Subject: [PATCH] WebSockets Next client: encode path param values automatically - fixes #40981 --- .../websockets/next/test/client/ClientEndpointTest.java | 9 +++++---- .../quarkus/websockets/next/BasicWebSocketConnector.java | 4 ++++ .../websockets/next/WebSocketClientConnection.java | 2 +- .../io/quarkus/websockets/next/WebSocketConnection.java | 2 +- .../io/quarkus/websockets/next/WebSocketConnector.java | 4 ++++ .../websockets/next/runtime/WebSocketConnectorBase.java | 4 +++- .../websockets/next/runtime/WebSocketConnectorImpl.java | 2 +- 7 files changed, 19 insertions(+), 8 deletions(-) diff --git a/extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/ClientEndpointTest.java b/extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/ClientEndpointTest.java index 5a36ee3511326..617ea30bd31d8 100644 --- a/extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/ClientEndpointTest.java +++ b/extensions/websockets-next/deployment/src/test/java/io/quarkus/websockets/next/test/client/ClientEndpointTest.java @@ -43,14 +43,15 @@ public class ClientEndpointTest { void testClient() throws InterruptedException { WebSocketClientConnection connection = connector .baseUri(uri) - .pathParam("name", "Lu") + // The value will be encoded automatically + .pathParam("name", "Lu=") .connectAndAwait(); - assertEquals("Lu", connection.pathParam("name")); + assertEquals("Lu=", connection.pathParam("name")); connection.sendTextAndAwait("Hi!"); assertTrue(ClientEndpoint.MESSAGE_LATCH.await(5, TimeUnit.SECONDS)); - assertEquals("Lu:Hello Lu!", ClientEndpoint.MESSAGES.get(0)); - assertEquals("Lu:Hi!", ClientEndpoint.MESSAGES.get(1)); + assertEquals("Lu=:Hello Lu=!", ClientEndpoint.MESSAGES.get(0)); + assertEquals("Lu=:Hi!", ClientEndpoint.MESSAGES.get(1)); connection.closeAndAwait(); assertTrue(ClientEndpoint.CLOSED_LATCH.await(5, TimeUnit.SECONDS)); diff --git a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/BasicWebSocketConnector.java b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/BasicWebSocketConnector.java index 7ee5be65764e7..b1e21c9b12966 100644 --- a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/BasicWebSocketConnector.java +++ b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/BasicWebSocketConnector.java @@ -1,6 +1,7 @@ package io.quarkus.websockets.next; import java.net.URI; +import java.net.URLEncoder; import java.util.function.BiConsumer; import java.util.function.Consumer; @@ -51,6 +52,9 @@ static BasicWebSocketConnector create() { /** * Set the path param. + *

+ * The value is encoded using {@link URLEncoder#encode(String, java.nio.charset.Charset)} before it's used to build the + * target URI. * * @param name * @param value diff --git a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/WebSocketClientConnection.java b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/WebSocketClientConnection.java index e262a9839bd44..393ba422b7351 100644 --- a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/WebSocketClientConnection.java +++ b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/WebSocketClientConnection.java @@ -27,7 +27,7 @@ public interface WebSocketClientConnection extends Sender, BlockingSender { /** * * @param name - * @return the actual value of the path parameter or {@code null} + * @return the value of the path parameter or {@code null} * @see WebSocketClient#path() */ String pathParam(String name); diff --git a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/WebSocketConnection.java b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/WebSocketConnection.java index d8e1a3cd98551..a63a3e2e5772e 100644 --- a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/WebSocketConnection.java +++ b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/WebSocketConnection.java @@ -37,7 +37,7 @@ public interface WebSocketConnection extends Sender, BlockingSender { /** * * @param name - * @return the actual value of the path parameter or {@code null} + * @return the decoded value of the path parameter or {@code null} * @see WebSocket#path() */ String pathParam(String name); diff --git a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/WebSocketConnector.java b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/WebSocketConnector.java index 4b771a66c7833..257094e31fe23 100644 --- a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/WebSocketConnector.java +++ b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/WebSocketConnector.java @@ -1,6 +1,7 @@ package io.quarkus.websockets.next; import java.net.URI; +import java.net.URLEncoder; import io.smallrye.common.annotation.CheckReturnValue; import io.smallrye.common.annotation.Experimental; @@ -28,6 +29,9 @@ public interface WebSocketConnector { /** * Set the path param. + *

+ * The value is encoded using {@link URLEncoder#encode(String, java.nio.charset.Charset)} before it's used to build the + * target URI. * * @param name * @param value diff --git a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorBase.java b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorBase.java index 4059996cd8369..728850f3083fd 100644 --- a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorBase.java +++ b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorBase.java @@ -1,6 +1,8 @@ package io.quarkus.websockets.next.runtime; import java.net.URI; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -121,7 +123,7 @@ String replacePathParameters(String path) { if (val == null) { throw new WebSocketClientException("Unable to obtain the path param for: " + paramName); } - m.appendReplacement(sb, val); + m.appendReplacement(sb, URLEncoder.encode(val, StandardCharsets.UTF_8)); } m.appendTail(sb); return path.startsWith("/") ? sb.toString() : "/" + sb.toString(); diff --git a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorImpl.java b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorImpl.java index 8b8781ccac2ed..ceaeab285dd80 100644 --- a/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorImpl.java +++ b/extensions/websockets-next/runtime/src/main/java/io/quarkus/websockets/next/runtime/WebSocketConnectorImpl.java @@ -92,7 +92,7 @@ public Uni connect() { .setPort(serverEndpointUri.getPort()); StringBuilder uri = new StringBuilder(); if (serverEndpointUri.getPath() != null) { - uri.append(serverEndpointUri.getPath()); + uri.append(serverEndpointUri.getRawPath()); } if (serverEndpointUri.getQuery() != null) { uri.append("?").append(serverEndpointUri.getQuery());