Skip to content

Commit d55d4e7

Browse files
committed
context: handle IPv6 addresses in getHost() and getPort()
getHost() and getPort() split the Host header at its first colon, so an IPv6 literal such as [::1]:8080 or [2001:db8::1] gave the host "[" or "2001" and made getPort(), and with it getRequestURL(), throw a NumberFormatException (400). The port of an IPv6 address now comes after the closing bracket, and getHost() returns the address in brackets, as in the Host header.
1 parent 3a1ee0b commit d55d4e7

2 files changed

Lines changed: 59 additions & 3 deletions

File tree

‎jooby/src/main/java/io/jooby/DefaultContext.java‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,7 @@ default int getServerPort() {
421421
@Override
422422
default int getPort() {
423423
var hostAndPort = getHostAndPort();
424-
int index = hostAndPort.indexOf(':');
424+
int index = portSeparator(hostAndPort);
425425
if (index > 0) {
426426
return Integer.parseInt(hostAndPort.substring(index + 1));
427427
}
@@ -431,8 +431,26 @@ default int getPort() {
431431
@Override
432432
default String getHost() {
433433
String hostAndPort = getHostAndPort();
434-
int index = hostAndPort.indexOf(':');
435-
return index > 0 ? hostAndPort.substring(0, index).trim() : hostAndPort;
434+
int index = portSeparator(hostAndPort);
435+
if (index > 0) {
436+
return hostAndPort.substring(0, index).trim();
437+
}
438+
if (!hostAndPort.startsWith("[") && hostAndPort.indexOf(':') != hostAndPort.lastIndexOf(':')) {
439+
// IPv6 address without port, which getHostAndPort() returns without brackets
440+
return "[" + hostAndPort + "]";
441+
}
442+
return hostAndPort;
443+
}
444+
445+
private static int portSeparator(String hostAndPort) {
446+
int index = hostAndPort.lastIndexOf(':');
447+
if (hostAndPort.startsWith("[")) {
448+
// IPv6 address: the port follows the closing bracket, as in [::1]:8080
449+
int end = hostAndPort.indexOf(']');
450+
return end > 0 && index > end ? index : -1;
451+
}
452+
// more than one colon is an IPv6 address without brackets, so without port
453+
return index == hostAndPort.indexOf(':') ? index : -1;
436454
}
437455

438456
@Override

‎jooby/src/test/java/io/jooby/DefaultContextTest.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,44 @@ void hostAndPortIPv6() {
542542
doReturn(mockHostHeader).when(ctx).header("Host");
543543

544544
assertEquals("::1", ctx.getHostAndPort());
545+
assertEquals("[::1]", ctx.getHost());
546+
}
547+
548+
@Test
549+
void hostAndPortIPv6WithPort() {
550+
Value mockHostHeader = mock(Value.class);
551+
when(mockHostHeader.valueOrNull()).thenReturn("[2001:db8::1]:8080");
552+
doReturn(mockHostHeader).when(ctx).header("Host");
553+
554+
assertEquals("[2001:db8::1]:8080", ctx.getHostAndPort());
555+
assertEquals("[2001:db8::1]", ctx.getHost());
556+
assertEquals(8080, ctx.getPort());
557+
}
558+
559+
@Test
560+
void hostAndPortIPv6DefaultPort() {
561+
doReturn(false).when(ctx).isSecure();
562+
563+
Value mockHostHeader = mock(Value.class);
564+
when(mockHostHeader.valueOrNull()).thenReturn("[2001:db8::1]");
565+
doReturn(mockHostHeader).when(ctx).header("Host");
566+
567+
assertEquals("[2001:db8::1]", ctx.getHost());
568+
assertEquals(80, ctx.getPort());
569+
}
570+
571+
@Test
572+
void requestURLIPv6() {
573+
doReturn("http").when(ctx).getScheme();
574+
doReturn("/").when(ctx).getContextPath();
575+
doReturn("/path").when(ctx).getRequestPath();
576+
doReturn("?q=1").when(ctx).queryString();
577+
578+
Value mockHostHeader = mock(Value.class);
579+
when(mockHostHeader.valueOrNull()).thenReturn("[::1]:8080");
580+
doReturn(mockHostHeader).when(ctx).header("Host");
581+
582+
assertEquals("http://[::1]:8080/path?q=1", ctx.getRequestURL());
545583
}
546584

547585
@Test

0 commit comments

Comments
 (0)