author | phh |
Sat, 30 Nov 2019 14:33:05 -0800 | |
changeset 59330 | 5b96c12f909d |
parent 55330 | 1fef7d9309a9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2007, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 6526913 |
|
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
27 |
* @library /test/lib |
2 | 28 |
* @run main/othervm -Dhttp.keepAlive=false B6526913 |
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
29 |
* @run main/othervm -Djava.net.preferIPv6Addresses=true |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
30 |
* -Dhttp.keepAlive=false B6526913 |
2 | 31 |
* @summary HttpExchange.getResponseBody().close() throws Exception |
32 |
*/ |
|
33 |
||
34 |
import com.sun.net.httpserver.*; |
|
35 |
||
36 |
import java.util.*; |
|
37 |
import java.util.concurrent.*; |
|
38 |
import java.io.*; |
|
39 |
import java.net.*; |
|
40 |
import java.security.*; |
|
41 |
import java.security.cert.*; |
|
42 |
import javax.net.ssl.*; |
|
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
43 |
import jdk.test.lib.net.URIBuilder; |
2 | 44 |
|
45 |
public class B6526913 { |
|
46 |
||
47 |
public static void main (String[] args) throws Exception { |
|
48 |
Handler handler = new Handler(); |
|
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
49 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
50 |
InetSocketAddress addr = new InetSocketAddress (loopback, 0); |
2 | 51 |
HttpServer server = HttpServer.create (addr, 0); |
52 |
HttpContext ctx = server.createContext ("/test", handler); |
|
53 |
||
54 |
ExecutorService executor = Executors.newCachedThreadPool(); |
|
55 |
server.setExecutor (executor); |
|
56 |
server.start (); |
|
57 |
||
55330
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
58 |
URL url = URIBuilder.newBuilder() |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
59 |
.scheme("http") |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
60 |
.loopback() |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
61 |
.port(server.getAddress().getPort()) |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
62 |
.path("/test/foo.html") |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
63 |
.toURL(); |
1fef7d9309a9
8225512: Replace wildcard address with loopback or local host in tests - part 15
dfuchs
parents:
47216
diff
changeset
|
64 |
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY); |
2 | 65 |
try { |
66 |
InputStream is = urlc.getInputStream(); |
|
67 |
int c ,count = 0; |
|
68 |
byte [] buf = new byte [32 * 1024]; |
|
69 |
while (count < 32 * 1024) { |
|
70 |
count += is.read (buf); |
|
71 |
} |
|
72 |
is.close(); |
|
73 |
} finally { |
|
74 |
server.stop(2); |
|
75 |
executor.shutdown(); |
|
76 |
} |
|
77 |
if (error) { |
|
78 |
throw new RuntimeException ("Test failed"); |
|
79 |
} |
|
80 |
} |
|
81 |
||
82 |
public static boolean error = false; |
|
83 |
||
84 |
static class Handler implements HttpHandler { |
|
85 |
int invocation = 1; |
|
86 |
public void handle (HttpExchange t) |
|
87 |
throws IOException |
|
88 |
{ |
|
89 |
InputStream is = t.getRequestBody(); |
|
90 |
try { |
|
91 |
while (is.read() != -1) ; |
|
92 |
is.close(); |
|
93 |
} catch (IOException e) { |
|
94 |
e.printStackTrace(); |
|
95 |
error = true; |
|
96 |
} |
|
97 |
/* send a chunked response, but wait a while before |
|
98 |
* sending the final empty chunk |
|
99 |
*/ |
|
100 |
t.sendResponseHeaders (200, 0); |
|
101 |
OutputStream os = t.getResponseBody(); |
|
102 |
byte[] bb = new byte [32 * 1024]; |
|
103 |
os.write (bb); |
|
104 |
os.flush(); |
|
105 |
try {Thread.sleep (5000); } catch (InterruptedException e){} |
|
106 |
try { |
|
107 |
/* empty chunk sent here */ |
|
108 |
os.close(); |
|
109 |
} catch (IOException e) { |
|
110 |
error = true; |
|
111 |
e.printStackTrace(); |
|
112 |
} |
|
113 |
t.close(); |
|
114 |
} |
|
115 |
} |
|
116 |
} |