Skip to content

Commit f1abcba

Browse files
committed
Support ipV6 Host addresses in HttpHeaders
This commit parses the "Host" HTTP request header as an `InetSocketAddress`, while supporting IPv6 addresses like `[::1]`. This host string contains `:` chars even though it has no port information. Issue: SPR-15799
1 parent bb684ce commit f1abcba

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

spring-web/src/main/java/org/springframework/http/HttpHeaders.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,12 @@ public InetSocketAddress getHost() {
971971
if (value == null) {
972972
return null;
973973
}
974-
int idx = value.lastIndexOf(':');
974+
final int idx;
975+
if (value.startsWith("[")) {
976+
idx = value.indexOf(':', value.indexOf(']'));
977+
} else {
978+
idx = value.lastIndexOf(':');
979+
}
975980
String hostname = null;
976981
int port = 0;
977982
if (idx != -1 && idx < value.length() - 1) {

spring-web/src/test/java/org/springframework/http/HttpHeadersTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,14 @@ public void hostNoPort() {
168168
assertEquals("Invalid Host header", "localhost", headers.getFirst("Host"));
169169
}
170170

171+
@Test
172+
public void ipv6Host() {
173+
InetSocketAddress host = InetSocketAddress.createUnresolved("[::1]", 0);
174+
headers.setHost(host);
175+
assertEquals("Invalid Host header", host, headers.getHost());
176+
assertEquals("Invalid Host header", "[::1]", headers.getFirst("Host"));
177+
}
178+
171179
@Test(expected = IllegalArgumentException.class)
172180
public void illegalETag() {
173181
String eTag = "v2.6";

0 commit comments

Comments
 (0)