Skip to content

Commit 30d19e5

Browse files
committed
remove ipv6
1 parent 11a9d74 commit 30d19e5

4 files changed

Lines changed: 42 additions & 30 deletions

File tree

httpcore5/src/main/java/org/apache/hc/core5/http/HttpHost.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ public String toURI() {
310310
final int port = this.host.getPort();
311311

312312
// Bracket only real IPv6 literals; decide using the address part only (ignore zone)
313-
if (ZoneIdSupport.isIPv6AddressPart(hostname)) {
313+
if (ZoneIdSupport.looksLikeIPv6AddressPart(hostname)) {
314314
ZoneIdSupport.appendBracketedIPv6(buffer, hostname);
315315
if (port >= 0) {
316316
buffer.append(':').append(port);

httpcore5/src/main/java/org/apache/hc/core5/net/URIAuthority.java

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,30 +80,37 @@ static URIAuthority parse(final CharSequence s, final Tokenizer.Cursor cursor) t
8080
final int upper = cursor.getUpperBound();
8181
int rb = -1;
8282
for (int i = lb + 1; i < upper; i++) {
83-
if (s.charAt(i) == ']') { rb = i; break; }
83+
if (s.charAt(i) == ']') {
84+
rb = i;
85+
break;
86+
}
8487
}
8588
if (rb < 0) {
8689
throw URISupport.createException(s.toString(), cursor, "Expected closing bracket for IPv6 address");
8790
}
8891

8992
final String literal = s.subSequence(lb + 1, rb).toString();
90-
final int z = literal.indexOf("%25");
91-
final String addr = z >= 0 ? literal.substring(0, z) : literal;
92-
93-
// validate only the IPv6 address part (zone excluded)
94-
if (!InetAddressUtils.isIPv6(addr)) {
93+
final int zoneMark = literal.indexOf("%25");
94+
final String addrPart = zoneMark >= 0 ? literal.substring(0, zoneMark) : literal;
95+
96+
int colons = 0;
97+
for (int i = 0; i < addrPart.length(); i++) {
98+
if (addrPart.charAt(i) == ':') {
99+
if (++colons >= 2) {
100+
break;
101+
}
102+
}
103+
}
104+
if (colons < 2) {
95105
throw URISupport.createException(s.toString(), cursor, "Expected an IPv6 address");
96106
}
97107

98-
// hostName must be stored in friendly form: "...%<decoded-zone>"
99-
final String hostName;
100-
if (z >= 0) {
101-
final String zoneEnc = literal.substring(z + 3);
108+
if (zoneMark >= 0) {
109+
final String zoneEnc = literal.substring(zoneMark + 3);
102110
ZoneIdSupport.validateZoneIdEncoded(zoneEnc);
103-
hostName = ZoneIdSupport.decodeZoneId(literal); // turns %25 + %HH into % + decoded UTF-8
104-
} else {
105-
hostName = addr;
106111
}
112+
// Store host in friendly form: "...%<decoded-zone>" (or literal as-is if no zone)
113+
final String hostName = ZoneIdSupport.decodeZoneId(literal);
107114

108115
// optional :port
109116
int pos = rb + 1;
@@ -113,15 +120,15 @@ static URIAuthority parse(final CharSequence s, final Tokenizer.Cursor cursor) t
113120
if (pos >= upper || !Character.isDigit(s.charAt(pos))) {
114121
throw URISupport.createException(s.toString(), cursor, "Invalid port");
115122
}
116-
long p = 0;
123+
long acc = 0;
117124
while (pos < upper && Character.isDigit(s.charAt(pos))) {
118-
p = p * 10 + (s.charAt(pos) - '0');
119-
if (p > 65535) {
125+
acc = acc * 10 + (s.charAt(pos) - '0');
126+
if (acc > 65535) {
120127
throw URISupport.createException(s.toString(), cursor, "Port out of range");
121128
}
122129
pos++;
123130
}
124-
port = (int) p;
131+
port = (int) acc;
125132
}
126133
cursor.updatePos(pos);
127134
return new URIAuthority(userInfo, hostName, port);
@@ -141,17 +148,14 @@ static URIAuthority parse(final CharSequence s, final Tokenizer.Cursor cursor) t
141148
break; // safety
142149
}
143150
if (ch == ':') {
144-
colonCount++;
145-
// more than one ':' before path/query/fragment => looks like IPv6, but not bracketed
146-
if (colonCount > 1) {
151+
if (++colonCount > 1) {
147152
throw URISupport.createException(s.toString(), cursor, "Expected an IPv6 address");
148153
}
149154
}
150155
i++;
151156
}
152157
}
153158

154-
// ===== fallback: reg-name / IPv4 (and optional :port) =====
155159
final Host host = Host.parse(s, cursor);
156160
return new URIAuthority(userInfo, host);
157161
}

httpcore5/src/main/java/org/apache/hc/core5/net/ZoneIdSupport.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,16 @@ public static void validateZoneIdEncoded(final CharSequence enc) {
138138
* <p>Rule: if the address-part (up to '%', if present) contains &gt;= 2 colons,
139139
* treat it as IPv6-like.</p>
140140
*/
141-
public static boolean isIPv6AddressPart(final CharSequence host) {
141+
public static boolean looksLikeIPv6AddressPart(final CharSequence host) {
142142
if (host == null) {
143143
return false;
144144
}
145145
int end = host.length();
146146
for (int i = 0; i < end; i++) {
147-
if (host.charAt(i) == '%') { end = i; break; }
147+
if (host.charAt(i) == '%') {
148+
end = i;
149+
break;
150+
}
148151
}
149152
int colons = 0;
150153
for (int i = 0; i < end; i++) {
@@ -164,13 +167,16 @@ public static boolean isIPv6AddressPart(final CharSequence host) {
164167
* RFC 6874-encoded ZoneID. Returns {@code true} iff it wrote the bracketed literal.
165168
*/
166169
public static boolean appendBracketedIPv6(final StringBuilder buf, final CharSequence host) {
167-
if (!isIPv6AddressPart(host)) {
170+
if (!looksLikeIPv6AddressPart(host)) {
168171
return false;
169172
}
170173
// address part
171174
int zoneIdx = -1;
172175
for (int i = 0; i < host.length(); i++) {
173-
if (host.charAt(i) == '%') { zoneIdx = i; break; }
176+
if (host.charAt(i) == '%') {
177+
zoneIdx = i;
178+
break;
179+
}
174180
}
175181
buf.append('[');
176182
if (zoneIdx >= 0) {
@@ -187,7 +193,9 @@ public static boolean appendBracketedIPv6(final StringBuilder buf, final CharSeq
187193
return true;
188194
}
189195

190-
/** RFC 3986 unreserved characters. */
196+
/**
197+
* RFC 3986 unreserved characters.
198+
*/
191199
private static boolean unreserved(final char ch) {
192200
return ch >= 'A' && ch <= 'Z'
193201
|| ch >= 'a' && ch <= 'z'

httpcore5/src/test/java/org/apache/hc/core5/net/TestInetAddressUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,9 @@ void zoneId_allows_unreserved_and_pct() throws URISyntaxException {
279279

280280
@Test
281281
void inetAddressUtils_helper_accepts_zone() {
282-
Assertions.assertTrue(ZoneIdSupport.isIPv6AddressPart("fe80::1%eth0"));
283-
Assertions.assertTrue(ZoneIdSupport.isIPv6AddressPart("fe80::1234:0:0:0:0:0%en1"));
284-
Assertions.assertFalse(ZoneIdSupport.isIPv6AddressPart("not-an-ip"));
282+
Assertions.assertTrue(ZoneIdSupport.looksLikeIPv6AddressPart("fe80::1%eth0"));
283+
Assertions.assertTrue(ZoneIdSupport.looksLikeIPv6AddressPart("fe80::1234:0:0:0:0:0%en1"));
284+
Assertions.assertFalse(ZoneIdSupport.looksLikeIPv6AddressPart("not-an-ip"));
285285
}
286286

287287
}

0 commit comments

Comments
 (0)