Skip to content

Commit

Permalink
GH-1580: ipv6 zone id in URI should be prefixed by %25.
Browse files Browse the repository at this point in the history
  • Loading branch information
sbernard31 committed Feb 5, 2024
1 parent 3388cf8 commit c93f774
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*******************************************************************************/
package org.eclipse.leshan.core.endpoint;

import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.URI;
import java.net.URISyntaxException;
Expand All @@ -33,7 +35,7 @@ public static URI createUri(String scheme, String host, int port) {

public static URI createUri(String scheme, InetSocketAddress addr) {
try {
return new URI(scheme, null, addr.getHostString(), addr.getPort(), null, null, null);
return new URI(scheme, null, toUriHostName(addr), addr.getPort(), null, null, null);
} catch (URISyntaxException e) {
throw new IllegalStateException(e);
}
Expand All @@ -49,7 +51,7 @@ public static URI createUri(String uri) {

public static URI replaceAddress(URI originalUri, InetSocketAddress newAddress) {
try {
return new URI(originalUri.getScheme(), null, newAddress.getHostString(), newAddress.getPort(), null, null,
return new URI(originalUri.getScheme(), null, toUriHostName(newAddress), newAddress.getPort(), null, null,
null);
} catch (URISyntaxException e) {
throw new IllegalStateException(e);
Expand All @@ -75,4 +77,31 @@ public static void validateURI(URI uri) throws IllegalArgumentException {
throw new IllegalArgumentException(String.format("Invalid URI[%s]: Post MUST NOT be undefined", uri));
}
}

/**
* This convert socket address in URI hostname.
* <p>
* Following https://www.rfc-editor.org/rfc/rfc6874#section-2, zone id (also called scope id) in URI should be
* prefixed by <code>%25</code>
*/
private static String toUriHostName(InetSocketAddress socketAddr) {
if (socketAddr == null) {
Validate.notNull(socketAddr);
}
InetAddress addr = socketAddr.getAddress();
String host = addr.getHostAddress();
if (addr instanceof Inet6Address) {
Inet6Address address6 = (Inet6Address) addr;
if (address6.getScopedInterface() != null || address6.getScopeId() > 0) {
int pos = host.indexOf('%');
if (pos > 0 && pos + 1 < host.length()) {
String separator = "%25";
String scope = host.substring(pos + 1);
String hostAddress = host.substring(0, pos);
host = hostAddress + separator + scope;
}
}
}
return host;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.eclipse.leshan.core.californium.ExceptionTranslator;
import org.eclipse.leshan.core.californium.SyncRequestObserver;
import org.eclipse.leshan.core.californium.identity.IdentityHandler;
import org.eclipse.leshan.core.endpoint.EndpointUriUtil;
import org.eclipse.leshan.core.endpoint.Protocol;
import org.eclipse.leshan.core.observation.Observation;
import org.eclipse.leshan.core.request.DownlinkRequest;
Expand Down Expand Up @@ -86,7 +85,7 @@ public Protocol getProtocol() {

@Override
public URI getURI() {
return EndpointUriUtil.createUri(protocol.getUriScheme(), endpoint.getAddress());
return endpoint.getUri();
}

@Override
Expand Down

0 comments on commit c93f774

Please sign in to comment.