This is an automated email from the ASF dual-hosted git repository.

guoyangze pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new 1182d15ec0d [FLINK-33289] Fix the conversion of InetSocketAddress to 
URL in Network utils to work with ipv6 address correctly
1182d15ec0d is described below

commit 1182d15ec0d1047375f007ead26c09e86425b02c
Author: 曹帝胄 <caodizho...@bytedance.com>
AuthorDate: Tue Oct 17 21:46:11 2023 +0800

    [FLINK-33289] Fix the conversion of InetSocketAddress to URL in Network 
utils to work with ipv6 address correctly
---
 .../src/main/java/org/apache/flink/util/NetUtils.java    | 11 ++++++++++-
 .../test/java/org/apache/flink/util/NetUtilsTest.java    | 16 ++++++++++++++++
 2 files changed, 26 insertions(+), 1 deletion(-)

diff --git a/flink-core/src/main/java/org/apache/flink/util/NetUtils.java 
b/flink-core/src/main/java/org/apache/flink/util/NetUtils.java
index 41d657c42c4..09f178ee736 100644
--- a/flink-core/src/main/java/org/apache/flink/util/NetUtils.java
+++ b/flink-core/src/main/java/org/apache/flink/util/NetUtils.java
@@ -135,7 +135,16 @@ public class NetUtils {
      * @return a URL object representing the provided socket address with 
"http://"; schema
      */
     public static URL socketToUrl(InetSocketAddress socketAddress) {
-        String hostPort = socketAddress.getHostString() + ":" + 
socketAddress.getPort();
+        String hostString = socketAddress.getHostString();
+        // If the hostString is an IPv6 address, it needs to be enclosed in 
square brackets
+        // at the beginning and end.
+        if (socketAddress.getAddress() != null
+                && socketAddress.getAddress() instanceof Inet6Address
+                && 
hostString.equals(socketAddress.getAddress().getHostAddress())) {
+            hostString = "[" + hostString + "]";
+        }
+        String hostPort = hostString + ":" + socketAddress.getPort();
+
         return validateHostPortString(hostPort);
     }
 
diff --git a/flink-core/src/test/java/org/apache/flink/util/NetUtilsTest.java 
b/flink-core/src/test/java/org/apache/flink/util/NetUtilsTest.java
index da186c2aea9..a21b6c9727e 100644
--- a/flink-core/src/test/java/org/apache/flink/util/NetUtilsTest.java
+++ b/flink-core/src/test/java/org/apache/flink/util/NetUtilsTest.java
@@ -396,4 +396,20 @@ public class NetUtilsTest extends TestLogger {
 
         
Assertions.assertThat(socketToUrl(socketAddress)).isEqualTo(expectedResult);
     }
+
+    @Test
+    public void testIpv6SocketToUrl() throws MalformedURLException {
+        InetSocketAddress socketAddress = new 
InetSocketAddress("[2001:1db8::ff00:42:8329]", 8080);
+        URL expectedResult = new URL("http://[2001:1db8::ff00:42:8329]:8080";);
+
+        
Assertions.assertThat(socketToUrl(socketAddress)).isEqualTo(expectedResult);
+    }
+
+    @Test
+    public void testIpv4SocketToUrl() throws MalformedURLException {
+        InetSocketAddress socketAddress = new InetSocketAddress("192.168.0.1", 
8080);
+        URL expectedResult = new URL("http://192.168.0.1:8080";);
+
+        
Assertions.assertThat(socketToUrl(socketAddress)).isEqualTo(expectedResult);
+    }
 }

Reply via email to