Skip to content

Commit f7fa05f

Browse files
tabata-dMichael-Mc-Mahon
authored andcommitted
8353698: Output of Simple Web Server is garbled if the console's encoding is not UTF-8
Reviewed-by: djelinski, dfuchs
1 parent 250eb74 commit f7fa05f

File tree

10 files changed

+62
-56
lines changed

10 files changed

+62
-56
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, 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
@@ -27,8 +27,6 @@
2727

2828
import java.io.PrintWriter;
2929

30-
import static java.nio.charset.StandardCharsets.UTF_8;
31-
3230
/**
3331
* Programmatic entry point to start the jwebserver tool.
3432
*/
@@ -65,7 +63,7 @@ public static void main(String... args) {
6563
setMaxReqTime();
6664
setMaxConnectionsIfNotSet();
6765

68-
int ec = SimpleFileServerImpl.start(new PrintWriter(System.out, true, UTF_8), "jwebserver", args);
66+
int ec = SimpleFileServerImpl.start(new PrintWriter(System.out, true), "jwebserver", args);
6967
if (ec != 0) {
7068
System.exit(ec);
7169
} // otherwise, the server has either been started successfully and

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

Lines changed: 2 additions & 3 deletions
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, 2025, 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
@@ -26,7 +26,6 @@
2626
package sun.net.httpserver.simpleserver;
2727

2828
import java.io.PrintWriter;
29-
import static java.nio.charset.StandardCharsets.UTF_8;
3029

3130
/**
3231
* Programmatic entry point to start "java -m jdk.httpserver".
@@ -61,7 +60,7 @@ public static void main(String... args) {
6160
setMaxReqTime();
6261
JWebServer.setMaxConnectionsIfNotSet();
6362

64-
int ec = SimpleFileServerImpl.start(new PrintWriter(System.out, true, UTF_8), "java", args);
63+
int ec = SimpleFileServerImpl.start(new PrintWriter(System.out, true), "java", args);
6564
if (ec != 0) {
6665
System.exit(ec);
6766
} // otherwise, the server has either been started successfully and

test/jdk/com/sun/net/httpserver/simpleserver/CommandLineNegativeTest.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, 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
@@ -48,6 +48,7 @@
4848
public class CommandLineNegativeTest {
4949

5050
static final Path JAVA_HOME = Path.of(System.getProperty("java.home"));
51+
static final String LOCALE_OPT = "-Duser.language=en -Duser.country=US";
5152
static final String JAVA = getJava(JAVA_HOME);
5253
static final Path CWD = Path.of(".").toAbsolutePath().normalize();
5354
static final Path TEST_DIR = CWD.resolve("CommandLineNegativeTest");
@@ -74,7 +75,7 @@ public Object[][] unknownOption() {
7475
@Test(dataProvider = "unknownOption")
7576
public void testBadOption(String opt) throws Throwable {
7677
out.println("\n--- testUnknownOption, opt=\"%s\" ".formatted(opt));
77-
simpleserver(JAVA, "-m", "jdk.httpserver", opt)
78+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt)
7879
.shouldNotHaveExitValue(0)
7980
.shouldContain("Error: unknown option: " + opt);
8081
}
@@ -97,7 +98,7 @@ public Object[][] tooManyOptionArgs() {
9798
@Test(dataProvider = "tooManyOptionArgs")
9899
public void testTooManyOptionArgs(String opt, String arg) throws Throwable {
99100
out.println("\n--- testTooManyOptionArgs, opt=\"%s\" ".formatted(opt));
100-
simpleserver(JAVA, "-m", "jdk.httpserver", opt, arg, arg)
101+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt, arg, arg)
101102
.shouldNotHaveExitValue(0)
102103
.shouldContain("Error: unknown option: " + arg);
103104
}
@@ -124,7 +125,7 @@ public Object[][] noArg() {
124125
@Test(dataProvider = "noArg")
125126
public void testNoArg(String opt, String msg) throws Throwable {
126127
out.println("\n--- testNoArg, opt=\"%s\" ".formatted(opt));
127-
simpleserver(JAVA, "-m", "jdk.httpserver", opt)
128+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt)
128129
.shouldNotHaveExitValue(0)
129130
.shouldContain("Error: no value given for " + opt)
130131
.shouldContain(msg);
@@ -148,7 +149,7 @@ public Object[][] invalidValue() {
148149
@Test(dataProvider = "invalidValue")
149150
public void testInvalidValue(String opt, String val) throws Throwable {
150151
out.println("\n--- testInvalidValue, opt=\"%s\" ".formatted(opt));
151-
simpleserver(JAVA, "-m", "jdk.httpserver", opt, val)
152+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt, val)
152153
.shouldNotHaveExitValue(0)
153154
.shouldContain("Error: invalid value given for " + opt + ": " + val);
154155
}
@@ -159,7 +160,7 @@ public void testInvalidValue(String opt, String val) throws Throwable {
159160
@Test(dataProvider = "portOptions")
160161
public void testPortOutOfRange(String opt) throws Throwable {
161162
out.println("\n--- testPortOutOfRange, opt=\"%s\" ".formatted(opt));
162-
simpleserver(JAVA, "-m", "jdk.httpserver", opt, "65536") // range 0 to 65535
163+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt, "65536") // range 0 to 65535
163164
.shouldNotHaveExitValue(0)
164165
.shouldContain("Error: server config failed: " + "port out of range:65536");
165166
}
@@ -172,7 +173,7 @@ public void testRootNotAbsolute(String opt) throws Throwable {
172173
out.println("\n--- testRootNotAbsolute, opt=\"%s\" ".formatted(opt));
173174
var root = Path.of(".");
174175
assertFalse(root.isAbsolute());
175-
simpleserver(JAVA, "-m", "jdk.httpserver", opt, root.toString())
176+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt, root.toString())
176177
.shouldNotHaveExitValue(0)
177178
.shouldContain("Error: server config failed: " + "Path is not absolute:");
178179
}
@@ -182,7 +183,7 @@ public void testRootNotADirectory(String opt) throws Throwable {
182183
out.println("\n--- testRootNotADirectory, opt=\"%s\" ".formatted(opt));
183184
var file = TEST_FILE.toString();
184185
assertFalse(Files.isDirectory(TEST_FILE));
185-
simpleserver(JAVA, "-m", "jdk.httpserver", opt, file)
186+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt, file)
186187
.shouldNotHaveExitValue(0)
187188
.shouldContain("Error: server config failed: " + "Path is not a directory: " + file);
188189
}
@@ -192,7 +193,7 @@ public void testRootDoesNotExist(String opt) throws Throwable {
192193
out.println("\n--- testRootDoesNotExist, opt=\"%s\" ".formatted(opt));
193194
Path root = TEST_DIR.resolve("not/existent/dir");
194195
assertFalse(Files.exists(root));
195-
simpleserver(JAVA, "-m", "jdk.httpserver", opt, root.toString())
196+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt, root.toString())
196197
.shouldNotHaveExitValue(0)
197198
.shouldContain("Error: server config failed: " + "Path does not exist: " + root.toString());
198199
}
@@ -209,7 +210,7 @@ public void testRootNotReadable(String opt) throws Throwable {
209210
try {
210211
root.toFile().setReadable(false, false);
211212
assertFalse(Files.isReadable(root));
212-
simpleserver(JAVA, "-m", "jdk.httpserver", opt, root.toString())
213+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt, root.toString())
213214
.shouldNotHaveExitValue(0)
214215
.shouldContain("Error: server config failed: " + "Path is not readable: " + root.toString());
215216
} finally {

test/jdk/com/sun/net/httpserver/simpleserver/CommandLinePortNotSpecifiedTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, 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
@@ -47,6 +47,7 @@
4747
public class CommandLinePortNotSpecifiedTest {
4848

4949
static final Path JAVA_HOME = Path.of(System.getProperty("java.home"));
50+
static final String LOCALE_OPT = "-Duser.language=en -Duser.country=US";
5051
static final String JAVA = getJava(JAVA_HOME);
5152
static final Path CWD = Path.of(".").toAbsolutePath().normalize();
5253
static final Path TEST_DIR = CWD.resolve("CommandLinePortNotSpecifiedTest");
@@ -84,7 +85,7 @@ static int normalExitCode() {
8485
@Test
8586
public void testPortNotSpecified() throws Throwable {
8687
out.println("\n--- testPortNotSpecified");
87-
simpleserver(JAVA, "-m", "jdk.httpserver")
88+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver")
8889
.shouldHaveExitValue(NORMAL_EXIT_CODE)
8990
.shouldContain("Binding to loopback by default. For all interfaces use \"-b 0.0.0.0\" or \"-b ::\".")
9091
.shouldContain("Serving " + TEST_DIR_STR + " and subdirectories on " + LOOPBACK_ADDR + " port")

test/jdk/com/sun/net/httpserver/simpleserver/CommandLinePositiveTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, 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
@@ -50,6 +50,7 @@ public class CommandLinePositiveTest {
5050

5151
static final String JAVA_VERSION = System.getProperty("java.version");
5252
static final Path JAVA_HOME = Path.of(System.getProperty("java.home"));
53+
static final String LOCALE_OPT = "-Duser.language=en -Duser.country=US";
5354
static final String JAVA = getJava(JAVA_HOME);
5455
static final Path CWD = Path.of(".").toAbsolutePath().normalize();
5556
static final Path TEST_DIR = CWD.resolve("CommandLinePositiveTest");
@@ -84,7 +85,7 @@ static int normalExitCode() {
8485
@Test(dataProvider = "directoryOptions")
8586
public void testDirectory(String opt) throws Throwable {
8687
out.println("\n--- testDirectory, opt=\"%s\" ".formatted(opt));
87-
simpleserver(JAVA, "-m", "jdk.httpserver", "-p", "0", opt, TEST_DIR_STR)
88+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", "-p", "0", opt, TEST_DIR_STR)
8889
.shouldHaveExitValue(NORMAL_EXIT_CODE)
8990
.shouldContain("Binding to loopback by default. For all interfaces use \"-b 0.0.0.0\" or \"-b ::\".")
9091
.shouldContain("Serving " + TEST_DIR_STR + " and subdirectories on " + LOOPBACK_ADDR + " port")
@@ -97,7 +98,7 @@ public void testDirectory(String opt) throws Throwable {
9798
@Test(dataProvider = "portOptions")
9899
public void testPort(String opt) throws Throwable {
99100
out.println("\n--- testPort, opt=\"%s\" ".formatted(opt));
100-
simpleserver(JAVA, "-m", "jdk.httpserver", opt, "0")
101+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt, "0")
101102
.shouldHaveExitValue(NORMAL_EXIT_CODE)
102103
.shouldContain("Binding to loopback by default. For all interfaces use \"-b 0.0.0.0\" or \"-b ::\".")
103104
.shouldContain("Serving " + TEST_DIR_STR + " and subdirectories on " + LOOPBACK_ADDR + " port")
@@ -128,7 +129,7 @@ public void testHelp(String opt) throws Throwable {
128129
out.println("\n--- testHelp, opt=\"%s\" ".formatted(opt));
129130
simpleserver(WaitForLine.HELP_STARTUP_LINE,
130131
false, // do not explicitly destroy the process
131-
JAVA, "-m", "jdk.httpserver", opt)
132+
JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt)
132133
.shouldHaveExitValue(0)
133134
.shouldContain(USAGE_TEXT)
134135
.shouldContain(OPTIONS_TEXT);
@@ -142,7 +143,7 @@ public void testVersion(String opt) throws Throwable {
142143
out.println("\n--- testVersion, opt=\"%s\" ".formatted(opt));
143144
simpleserver(WaitForLine.VERSION_STARTUP_LINE,
144145
false, // do not explicitly destroy the process
145-
JAVA, "-m", "jdk.httpserver", opt)
146+
JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt)
146147
.shouldHaveExitValue(0);
147148
}
148149

@@ -152,12 +153,12 @@ public void testVersion(String opt) throws Throwable {
152153
@Test(dataProvider = "bindOptions")
153154
public void testBindAllInterfaces(String opt) throws Throwable {
154155
out.println("\n--- testBindAllInterfaces, opt=\"%s\" ".formatted(opt));
155-
simpleserver(JAVA, "-m", "jdk.httpserver", "-p", "0", opt, "0.0.0.0")
156+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", "-p", "0", opt, "0.0.0.0")
156157
.shouldHaveExitValue(NORMAL_EXIT_CODE)
157158
.shouldContain("Serving " + TEST_DIR_STR + " and subdirectories on 0.0.0.0 (all interfaces) port")
158159
.shouldContain("URL http://" + InetAddress.getLocalHost().getHostAddress());
159160
if (IPSupport.hasIPv6()) {
160-
simpleserver(JAVA, "-m", "jdk.httpserver", opt, "::0")
161+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt, "::0")
161162
.shouldHaveExitValue(NORMAL_EXIT_CODE)
162163
.shouldContain("Serving " + TEST_DIR_STR + " and subdirectories on 0.0.0.0 (all interfaces) port")
163164
.shouldContain("URL http://" + InetAddress.getLocalHost().getHostAddress());
@@ -167,7 +168,7 @@ public void testBindAllInterfaces(String opt) throws Throwable {
167168
@Test(dataProvider = "bindOptions")
168169
public void testLastOneWinsBindAddress(String opt) throws Throwable {
169170
out.println("\n--- testLastOneWinsBindAddress, opt=\"%s\" ".formatted(opt));
170-
simpleserver(JAVA, "-m", "jdk.httpserver", "-p", "0", opt, "123.4.5.6", opt, LOOPBACK_ADDR)
171+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", "-p", "0", opt, "123.4.5.6", opt, LOOPBACK_ADDR)
171172
.shouldHaveExitValue(NORMAL_EXIT_CODE)
172173
.shouldContain("Serving " + TEST_DIR_STR + " and subdirectories on " + LOOPBACK_ADDR + " port")
173174
.shouldContain("URL http://" + LOOPBACK_ADDR);
@@ -177,7 +178,7 @@ public void testLastOneWinsBindAddress(String opt) throws Throwable {
177178
@Test(dataProvider = "directoryOptions")
178179
public void testLastOneWinsDirectory(String opt) throws Throwable {
179180
out.println("\n--- testLastOneWinsDirectory, opt=\"%s\" ".formatted(opt));
180-
simpleserver(JAVA, "-m", "jdk.httpserver", "-p", "0", opt, TEST_DIR_STR, opt, TEST_DIR_STR)
181+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", "-p", "0", opt, TEST_DIR_STR, opt, TEST_DIR_STR)
181182
.shouldHaveExitValue(NORMAL_EXIT_CODE)
182183
.shouldContain("Binding to loopback by default. For all interfaces use \"-b 0.0.0.0\" or \"-b ::\".")
183184
.shouldContain("Serving " + TEST_DIR_STR + " and subdirectories on " + LOOPBACK_ADDR + " port")
@@ -190,7 +191,7 @@ public void testLastOneWinsDirectory(String opt) throws Throwable {
190191
@Test(dataProvider = "outputOptions")
191192
public void testLastOneWinsOutput(String opt) throws Throwable {
192193
out.println("\n--- testLastOneWinsOutput, opt=\"%s\" ".formatted(opt));
193-
simpleserver(JAVA, "-m", "jdk.httpserver", "-p", "0", opt, "none", opt, "verbose")
194+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", "-p", "0", opt, "none", opt, "verbose")
194195
.shouldHaveExitValue(NORMAL_EXIT_CODE)
195196
.shouldContain("Binding to loopback by default. For all interfaces use \"-b 0.0.0.0\" or \"-b ::\".")
196197
.shouldContain("Serving " + TEST_DIR_STR + " and subdirectories on " + LOOPBACK_ADDR + " port")
@@ -200,7 +201,7 @@ public void testLastOneWinsOutput(String opt) throws Throwable {
200201
@Test(dataProvider = "portOptions")
201202
public void testLastOneWinsPort(String opt) throws Throwable {
202203
out.println("\n--- testLastOneWinsPort, opt=\"%s\" ".formatted(opt));
203-
simpleserver(JAVA, "-m", "jdk.httpserver", opt, "-999", opt, "0")
204+
simpleserver(JAVA, LOCALE_OPT, "-m", "jdk.httpserver", opt, "-999", opt, "0")
204205
.shouldHaveExitValue(NORMAL_EXIT_CODE)
205206
.shouldContain("Binding to loopback by default. For all interfaces use \"-b 0.0.0.0\" or \"-b ::\".")
206207
.shouldContain("Serving " + TEST_DIR_STR + " and subdirectories on " + LOOPBACK_ADDR + " port")

0 commit comments

Comments
 (0)