Skip to content

Commit e909c2f

Browse files
committed
Taken into account PR review comments
1 parent 8c8106c commit e909c2f

1 file changed

Lines changed: 31 additions & 22 deletions

File tree

  • org.restlet.java/org.restlet/src/main/java/org/restlet/data

org.restlet.java/org.restlet/src/main/java/org/restlet/data/Reference.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2882,7 +2882,7 @@ private void validateAuthority(String authority) {
28822882
if (ipV6EndIndex == -1) {
28832883
throw new IllegalArgumentException("Invalid IPv6 address format: no closing bracket");
28842884
} else {
2885-
validateIPv6(authority.substring(0, ipV6EndIndex + 1));
2885+
validateIpV6(authority.substring(0, ipV6EndIndex + 1));
28862886
if (ipV6EndIndex + 1 < authority.length()) {
28872887
if (authority.charAt(ipV6EndIndex + 1) == ':') {
28882888
validateHostPort(authority.substring(ipV6EndIndex + 2));
@@ -2916,56 +2916,65 @@ private void validateHostPort(final String hostPort) {
29162916
/**
29172917
* Validate an IP v6 according to RFC 2373.
29182918
*
2919-
* @param ipv6 The IP v6 to validate.
2919+
* @param ipV6 The IP v6 to validate.
29202920
*/
2921-
private void validateIPv6(String ipv6) {
2922-
if (ipv6 == null || ipv6.isEmpty()) {
2921+
private void validateIpV6(String ipV6) {
2922+
if (ipV6 == null || ipV6.isEmpty()) {
29232923
throw new IllegalArgumentException("Invalid IPv6 address");
29242924
}
29252925

2926-
if (ipv6.startsWith("[")) {
2927-
ipv6 = ipv6.substring(1);
2928-
}
2929-
if (ipv6.endsWith("]")) {
2930-
ipv6 = ipv6.substring(0, ipv6.length() - 1);
2931-
}
2926+
ipV6 = trimIpV6Brackets(ipV6);
29322927

29332928
// Check for double colon compression (only one allowed)
29342929
int doubleColonCount = 0;
2935-
int idx = ipv6.indexOf("::");
2930+
int idx = ipV6.indexOf("::");
29362931
while (idx != -1) {
29372932
doubleColonCount++;
2938-
idx = ipv6.indexOf("::", idx + 2);
2933+
idx = ipV6.indexOf("::", idx + 2);
29392934
}
29402935
if (doubleColonCount > 1) {
29412936
throw new IllegalArgumentException("Invalid IPv6 address format");
29422937
}
29432938

2944-
String[] parts = ipv6.split(":", -1);
2939+
String[] parts = ipV6.split(":", -1);
29452940
int maxParts = 8;
29462941

29472942
if (parts.length > maxParts) {
29482943
throw new IllegalArgumentException("Invalid IPv6 address format");
29492944
}
29502945

29512946
for (String part : parts) {
2952-
if (part.isEmpty() && doubleColonCount == 0) {
2947+
validateIpV6Part(part, doubleColonCount);
2948+
}
2949+
}
2950+
2951+
private void validateIpV6Part(final String part, final int doubleColonCount) {
2952+
if (part.isEmpty() && doubleColonCount == 0) {
2953+
throw new IllegalArgumentException("Invalid IPv6 address format");
2954+
}
2955+
if (!part.isEmpty()) {
2956+
if (part.length() > 4) {
29532957
throw new IllegalArgumentException("Invalid IPv6 address format");
29542958
}
2955-
if (!part.isEmpty()) {
2956-
if (part.length() > 4) {
2959+
for (char c : part.toCharArray()) {
2960+
if (!isAlpha(c) && !isDigit(c)) {
29572961
throw new IllegalArgumentException("Invalid IPv6 address format");
29582962
}
2959-
for (char c : part.toCharArray()) {
2960-
if (!isAlpha(c) && !isDigit(c)) {
2961-
throw new IllegalArgumentException("Invalid IPv6 address format");
2962-
}
2963-
}
29642963
}
29652964
}
29662965
}
29672966

2968-
/**
2967+
private String trimIpV6Brackets(String ipV6) {
2968+
if (ipV6.startsWith("[")) {
2969+
ipV6 = ipV6.substring(1);
2970+
}
2971+
if (ipV6.endsWith("]")) {
2972+
ipV6 = ipV6.substring(0, ipV6.length() - 1);
2973+
}
2974+
return ipV6;
2975+
}
2976+
2977+
/**
29692978
* Validate a scheme according to RFC 3986.
29702979
*
29712980
* @param scheme The scheme to validate.

0 commit comments

Comments
 (0)