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