Skip to content

Commit d122951

Browse files
Taizo Kurashigejerboaa
authored andcommitted
8332020: jwebserver tool prints invalid URL in case of IPv6 address binding
Backport-of: f9a1d3381b12c97784c11649be079147c85939c0
1 parent d590823 commit d122951

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

src/jdk.httpserver/share/classes/sun/net/httpserver/simpleserver/SimpleFileServerImpl.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,7 @@
3030
import com.sun.net.httpserver.SimpleFileServer.OutputLevel;
3131

3232
import java.io.PrintWriter;
33+
import java.net.Inet6Address;
3334
import java.net.InetAddress;
3435
import java.net.InetSocketAddress;
3536
import java.net.UnknownHostException;
@@ -169,6 +170,13 @@ void printStartMessage(Path root, HttpServer server)
169170
if (!addrSpecified) {
170171
writer.println(ResourceBundleHelper.getMessage("loopback.info"));
171172
}
173+
if (inetAddr instanceof Inet6Address && addr.contains(":") && !addr.startsWith("[")) {
174+
// we use the "addr" when printing the URL, so make sure it
175+
// conforms to RFC-2732, section 2:
176+
// To use a literal IPv6 address in a URL, the literal
177+
// address should be enclosed in "[" and "]" characters.
178+
addr = "[" + addr + "]";
179+
}
172180
if (isAnyLocal) {
173181
writer.println(ResourceBundleHelper.getMessage("msg.start.anylocal", root, addr, port));
174182
} else {
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2024, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.nio.file.Path;
25+
import java.util.ArrayList;
26+
import java.util.List;
27+
import java.util.concurrent.TimeUnit;
28+
import java.util.function.Predicate;
29+
30+
import jdk.internal.util.OperatingSystem;
31+
import jdk.test.lib.net.IPSupport;
32+
import jdk.test.lib.process.ProcessTools;
33+
import jtreg.SkippedException;
34+
35+
/*
36+
* @test
37+
* @bug 8332020
38+
* @summary verifies that when jwebserver is launched with a IPv6 bind address
39+
* then the URL printed contains the correct host literal
40+
* @modules jdk.httpserver java.base/jdk.internal.util
41+
* @library /test/lib
42+
* @build jdk.test.lib.net.IPSupport
43+
* @run driver IPv6BoundHost
44+
*/
45+
public class IPv6BoundHost {
46+
47+
private static final Path JDK_BIN_DIR = Path.of(System.getProperty("java.home")).resolve("bin");
48+
private static final Path JWEBSERVER_BINARY = OperatingSystem.isWindows()
49+
? JDK_BIN_DIR.resolve("jwebserver.exe") : JDK_BIN_DIR.resolve("jwebserver");
50+
51+
public static void main(final String[] args) throws Exception {
52+
IPSupport.printPlatformSupport(System.err); // for debug purposes
53+
if (!IPSupport.hasIPv6()) {
54+
throw new SkippedException("Skipping test - IPv6 is not supported");
55+
}
56+
final String output = launchJwebserverAndExit(List.of("-b", "::1", "-p", "0"));
57+
if (output.contains("URL http://[::1]:")
58+
|| output.contains("URL http://[0:0:0:0:0:0:0:1]:")) {
59+
// found expected content
60+
System.out.println("found expected URL in jwebserver output");
61+
} else {
62+
throw new AssertionError("missing IPv6 address in jwebserver process output");
63+
}
64+
}
65+
66+
private static String launchJwebserverAndExit(final List<String> args) throws Exception {
67+
final Predicate<String> waitForLine = (s) -> s.startsWith("URL http://");
68+
final StringBuilder sb = new StringBuilder(); // stdout & stderr
69+
final List<String> cmd = new ArrayList<>();
70+
cmd.add(JWEBSERVER_BINARY.toString());
71+
cmd.addAll(args);
72+
// start the process and await the waitForLine before returning
73+
final Process p = ProcessTools.startProcess("8332020-test", new ProcessBuilder(cmd),
74+
line -> sb.append(line).append("\n"),
75+
waitForLine,
76+
30, // suitably high default timeout, not expected to timeout
77+
TimeUnit.SECONDS);
78+
System.out.println(sb.toString()); // print the process' stdout/stderr
79+
// the process has started and it is confirmed that the process output has the line
80+
// we were waiting for. now kill the process.
81+
p.destroy();
82+
final int exitCode = p.waitFor();
83+
if (!isNormalExitCode(exitCode)) {
84+
throw new AssertionError("jwebserver exited with unexpected exit code: " + exitCode);
85+
}
86+
return sb.toString();
87+
}
88+
89+
private static boolean isNormalExitCode(final int exitCode) {
90+
final int SIGTERM = 15;
91+
if (OperatingSystem.isWindows()) {
92+
return exitCode == 1; // we expect exit code == 1 on Windows for Process.destroy()
93+
} else {
94+
// signal terminated exit code on Unix is 128 + signal value
95+
return exitCode == (128 + SIGTERM);
96+
}
97+
}
98+
}

0 commit comments

Comments
 (0)