ok2c commented on code in PR #571:
URL: 
https://github.com/apache/httpcomponents-core/pull/571#discussion_r2421060673


##########
httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java:
##########
@@ -303,13 +304,24 @@ public InetAddress getAddress() {
      */
     public String toURI() {
         final StringBuilder buffer = new StringBuilder();
-        buffer.append(this.schemeName);
-        buffer.append("://");
-        buffer.append(this.host.toString());
+        buffer.append(this.schemeName).append("://");
+
+        final String hostname = this.host.getHostName();
+        final int port = this.host.getPort();
+
+        // Bracket only real IPv6 literals; decide using the address part only 
(ignore zone)
+        if (ZoneIdSupport.isIPv6AddressPart(hostname)) {
+            ZoneIdSupport.appendBracketedIPv6(buffer, hostname);

Review Comment:
   @arturobernalg If I understand it right `#isIPv6AddressPart` will be 
executed twice. Is this needed?



##########
httpcore5/src/main/java/org/apache/hc/core5/net/ZoneIdSupport.java:
##########
@@ -0,0 +1,148 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.hc.core5.net;
+
+import java.io.ByteArrayOutputStream;
+import java.nio.charset.StandardCharsets;
+
+import org.apache.hc.core5.annotation.Internal;
+import org.apache.hc.core5.util.TextUtils;
+
+@Internal
+public final class ZoneIdSupport {
+
+    private ZoneIdSupport() {
+    }
+
+
+    public static String encodeZoneIdRfc6874(final String raw) {
+        if (raw == null || raw.isEmpty()) {
+            return raw;
+        }
+        final StringBuilder out = new StringBuilder(raw.length() + 8);
+        for (int i = 0; i < raw.length(); i++) {
+            final char ch = raw.charAt(i);
+            final boolean unreserved = unreserved(ch);
+            if (unreserved) {
+                out.append(ch);
+            } else if (ch == '%' && i + 2 < raw.length()
+                    && TextUtils.isHex(raw.charAt(i + 1)) && 
TextUtils.isHex(raw.charAt(i + 2))) {
+                out.append('%').append(raw.charAt(i + 1)).append(raw.charAt(i 
+ 2));
+                i += 2;
+            } else {
+                final byte[] bytes = 
String.valueOf(ch).getBytes(java.nio.charset.StandardCharsets.UTF_8);
+                for (final byte b : bytes) {
+                    final int v = b & 0xFF;
+                    out.append('%')
+                            .append(Character.toUpperCase(Character.forDigit(v 
>>> 4 & 0xF, 16)))
+                            .append(Character.toUpperCase(Character.forDigit(v 
& 0xF, 16)));
+                }
+            }
+        }
+        return out.toString();
+    }
+
+
+    public static String decodeZoneId(final String host) {
+        if (host == null) {
+            return null;
+        }
+        if (host.indexOf('%') < 0) {
+            return host;
+        }
+        final int p = host.indexOf("%25");
+        if (p < 0) {
+            return host;
+        }
+        final String addr = host.substring(0, p);
+        final String encZone = host.substring(p + 3);
+        final ByteArrayOutputStream baos = new 
ByteArrayOutputStream(encZone.length());
+        for (int i = 0; i < encZone.length(); i++) {
+            final char ch = encZone.charAt(i);
+            if (ch == '%' && i + 2 < encZone.length() && 
TextUtils.isHex(encZone.charAt(i + 1)) && TextUtils.isHex(encZone.charAt(i + 
2))) {
+                final int hi = Character.digit(encZone.charAt(i + 1), 16);
+                final int lo = Character.digit(encZone.charAt(i + 2), 16);
+                baos.write((hi << 4) + lo);
+                i += 2;
+            } else {
+                baos.write((byte) ch);
+            }
+        }
+        final String zone = new String(baos.toByteArray(), 
StandardCharsets.UTF_8);
+        return addr + '%' + zone;
+    }
+
+    /**
+     * ZoneID = 1*( unreserved / pct-encoded ); throw IAE on invalid.
+     */
+    public static void validateZoneIdEncoded(final String enc) {
+        if (enc == null || enc.isEmpty()) {
+            throw new IllegalArgumentException("ZoneID must not be empty");
+        }
+        for (int i = 0; i < enc.length(); i++) {
+            final char ch = enc.charAt(i);
+            if (unreserved(ch)) {
+                continue;
+            }
+            if (ch == '%' && i + 2 < enc.length() && 
TextUtils.isHex(enc.charAt(i + 1)) && TextUtils.isHex(enc.charAt(i + 2))) {
+                i += 2;
+                continue;
+            }
+            throw new IllegalArgumentException("Illegal character in ZoneID");
+        }
+    }
+
+    public static boolean isIPv6AddressPart(final String host) {

Review Comment:
   @arturobernalg Please use `CharSequence` instead of `String` whenever 
possible. 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to