author | dfuchs |
Mon, 10 Jun 2019 11:17:57 +0100 | |
changeset 55309 | 8081b181bba8 |
parent 54314 | 46cf212cdcca |
permissions | -rw-r--r-- |
2 | 1 |
/* |
55309
8081b181bba8
8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents:
54314
diff
changeset
|
2 |
* Copyright (c) 2006, 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 |
|
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
26 |
* @library /test/lib |
2 | 27 |
* @bug 6401598 |
28 |
* @summary new HttpServer cannot serve binary stream data |
|
55309
8081b181bba8
8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents:
54314
diff
changeset
|
29 |
* @run main B6401598 |
8081b181bba8
8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents:
54314
diff
changeset
|
30 |
* @run main/othervm -Djava.net.preferIPv6Addresses=true B6401598 |
2 | 31 |
*/ |
32 |
||
33 |
import java.io.*; |
|
34 |
import java.net.HttpURLConnection; |
|
35 |
import java.net.MalformedURLException; |
|
55309
8081b181bba8
8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents:
54314
diff
changeset
|
36 |
import java.net.Proxy; |
2 | 37 |
import java.net.URL; |
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
38 |
import java.net.InetAddress; |
2 | 39 |
import java.net.InetSocketAddress; |
40 |
import java.util.concurrent.*; |
|
41 |
||
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
42 |
import jdk.test.lib.net.URIBuilder; |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
43 |
|
2 | 44 |
import com.sun.net.httpserver.HttpExchange; |
45 |
import com.sun.net.httpserver.HttpHandler; |
|
46 |
import com.sun.net.httpserver.HttpServer; |
|
47 |
||
48 |
public class B6401598 { |
|
49 |
||
50 |
static class MyHandler implements HttpHandler { |
|
51 |
||
52 |
public MyHandler() { |
|
53 |
||
54 |
} |
|
55 |
||
56 |
public void handle(HttpExchange arg0) throws IOException { |
|
57 |
try { |
|
58 |
InputStream is = arg0.getRequestBody(); |
|
59 |
OutputStream os = arg0.getResponseBody(); |
|
60 |
||
61 |
DataInputStream dis = new DataInputStream(is); |
|
62 |
||
63 |
short input = dis.readShort(); |
|
64 |
while (dis.read() != -1) ; |
|
65 |
dis.close(); |
|
66 |
||
67 |
DataOutputStream dos = new DataOutputStream(os); |
|
68 |
||
69 |
arg0.sendResponseHeaders(200, 0); |
|
70 |
||
71 |
dos.writeShort(input); |
|
72 |
||
73 |
dos.flush(); |
|
74 |
dos.close(); |
|
75 |
} catch (IOException e) { |
|
76 |
e.printStackTrace(); |
|
77 |
error = true; |
|
78 |
} |
|
79 |
} |
|
80 |
||
81 |
} |
|
82 |
||
83 |
static int port; |
|
84 |
static boolean error = false; |
|
85 |
static ExecutorService exec; |
|
86 |
static HttpServer server; |
|
87 |
||
88 |
public static void main(String[] args) { |
|
89 |
try { |
|
55309
8081b181bba8
8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents:
54314
diff
changeset
|
90 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
8081b181bba8
8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents:
54314
diff
changeset
|
91 |
server = HttpServer.create(new InetSocketAddress(loopback, 0), 400); |
2 | 92 |
server.createContext("/server/", new MyHandler()); |
93 |
exec = Executors.newFixedThreadPool(3); |
|
7271 | 94 |
server.setExecutor(exec); |
2 | 95 |
port = server.getAddress().getPort(); |
96 |
server.start(); |
|
97 |
||
98 |
short counter; |
|
99 |
||
100 |
for (counter = 0; counter < 1000; counter++) { |
|
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
101 |
URL url = URIBuilder.newBuilder() |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
102 |
.scheme("http") |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
103 |
.loopback() |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
104 |
.port(port) |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
105 |
.path("/server/") |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
106 |
.toURLUnchecked(); |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
107 |
System.out.println("URL: " + url); |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
108 |
HttpURLConnection connection = getHttpURLConnection(url, 10000); |
2 | 109 |
|
110 |
OutputStream os = connection.getOutputStream(); |
|
111 |
||
112 |
DataOutputStream dos = new DataOutputStream(os); |
|
113 |
||
114 |
dos.writeShort(counter); |
|
115 |
||
116 |
dos.flush(); |
|
117 |
dos.close(); |
|
118 |
||
119 |
counter++; |
|
120 |
||
121 |
InputStream is = connection.getInputStream(); |
|
122 |
||
123 |
DataInputStream dis = new DataInputStream(is); |
|
124 |
||
125 |
short ret = dis.readShort(); |
|
126 |
||
127 |
dis.close(); |
|
128 |
} |
|
129 |
System.out.println ("Stopping"); |
|
55309
8081b181bba8
8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents:
54314
diff
changeset
|
130 |
} catch (Exception e) { |
8081b181bba8
8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents:
54314
diff
changeset
|
131 |
throw new AssertionError("Unexpected exception: " + e, e); |
8081b181bba8
8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents:
54314
diff
changeset
|
132 |
} finally { |
2 | 133 |
server.stop (1); |
134 |
exec.shutdown(); |
|
135 |
} |
|
136 |
} |
|
137 |
||
138 |
||
139 |
||
140 |
static HttpURLConnection getHttpURLConnection(URL url, int timeout) throws IOException { |
|
141 |
||
55309
8081b181bba8
8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents:
54314
diff
changeset
|
142 |
HttpURLConnection httpURLConnection = |
8081b181bba8
8224865: Replace wildcard address with loopback or local host in tests - part 13
dfuchs
parents:
54314
diff
changeset
|
143 |
(HttpURLConnection) url.openConnection(Proxy.NO_PROXY); |
2 | 144 |
|
145 |
httpURLConnection.setConnectTimeout(40000); |
|
146 |
httpURLConnection.setReadTimeout(timeout); |
|
147 |
httpURLConnection.setDoOutput(true); |
|
148 |
httpURLConnection.setDoInput(true); |
|
149 |
httpURLConnection.setUseCaches(false); |
|
150 |
httpURLConnection.setAllowUserInteraction(false); |
|
151 |
httpURLConnection.setRequestMethod("POST"); |
|
152 |
||
153 |
// HttpURLConnection httpURLConnection = new MyHttpURLConnection(url); |
|
154 |
||
155 |
return httpURLConnection; |
|
156 |
} |
|
157 |
} |