author | dholmes |
Wed, 06 Nov 2019 21:18:42 -0500 | |
changeset 58956 | 9a0a5e70eeb2 |
parent 55330 | 1fef7d9309a9 |
permissions | -rw-r--r-- |
4047
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
1 |
/* |
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2009, 2019, Oracle and/or its affiliates. All rights reserved. |
4047
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
4 |
* |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
8 |
* |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
13 |
* accompanied this code). |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
14 |
* |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
18 |
* |
5506 | 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. |
|
4047
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
22 |
*/ |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
23 |
|
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
24 |
/** |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
25 |
* @test |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
26 |
* @bug 6886436 |
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
27 |
* @summary HttpServer should not send a body with 204 response. |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
28 |
* @library /test/lib |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
29 |
* @run main B6886436 |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
30 |
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6886436 |
4047
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
31 |
*/ |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
32 |
|
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
33 |
import com.sun.net.httpserver.*; |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
34 |
|
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
35 |
import java.util.*; |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
36 |
import java.util.concurrent.*; |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
37 |
import java.util.logging.*; |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
38 |
import java.io.*; |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
39 |
import java.net.*; |
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
40 |
import jdk.test.lib.net.URIBuilder; |
4047
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
41 |
|
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
42 |
public class B6886436 { |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
43 |
|
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
44 |
public static void main (String[] args) throws Exception { |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
45 |
Logger logger = Logger.getLogger ("com.sun.net.httpserver"); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
46 |
ConsoleHandler c = new ConsoleHandler(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
47 |
c.setLevel (Level.WARNING); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
48 |
logger.addHandler (c); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
49 |
logger.setLevel (Level.WARNING); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
50 |
Handler handler = new Handler(); |
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
51 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
52 |
InetSocketAddress addr = new InetSocketAddress (loopback, 0); |
4047
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
53 |
HttpServer server = HttpServer.create (addr, 0); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
54 |
HttpContext ctx = server.createContext ("/test", handler); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
55 |
ExecutorService executor = Executors.newCachedThreadPool(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
56 |
server.setExecutor (executor); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
57 |
server.start (); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
58 |
|
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
59 |
URL url = URIBuilder.newBuilder() |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
60 |
.scheme("http") |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
61 |
.loopback() |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
62 |
.port(server.getAddress().getPort()) |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
63 |
.path("/test/foo.html") |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
64 |
.toURL(); |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
65 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY); |
4047
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
66 |
try { |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
67 |
InputStream is = urlc.getInputStream(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
68 |
while (is.read()!= -1) ; |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
69 |
is.close (); |
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
70 |
urlc = (HttpURLConnection)url.openConnection(Proxy.NO_PROXY); |
4047
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
71 |
urlc.setReadTimeout (3000); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
72 |
is = urlc.getInputStream(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
73 |
while (is.read()!= -1); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
74 |
is.close (); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
75 |
|
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
76 |
} catch (IOException e) { |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
77 |
server.stop(2); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
78 |
executor.shutdown(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
79 |
throw new RuntimeException ("Test failed"); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
80 |
} |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
81 |
server.stop(2); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
82 |
executor.shutdown(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
83 |
System.out.println ("OK"); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
84 |
} |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
85 |
|
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
86 |
public static boolean error = false; |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
87 |
|
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
88 |
static class Handler implements HttpHandler { |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
89 |
int invocation = 1; |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
90 |
public void handle (HttpExchange t) |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
91 |
throws IOException |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
92 |
{ |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
93 |
InputStream is = t.getRequestBody(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
94 |
Headers map = t.getRequestHeaders(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
95 |
Headers rmap = t.getResponseHeaders(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
96 |
while (is.read () != -1) ; |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
97 |
is.close(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
98 |
// send a 204 response with an empty chunked body |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
99 |
t.sendResponseHeaders (204, 0); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
100 |
t.close(); |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
101 |
} |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
102 |
} |
f5dcf30f9206
6886436: Lightwight HTTP Container (com.sun.* package) is unstable
michaelm
parents:
diff
changeset
|
103 |
} |