Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/6725892/Test.java
41161 views
1
/*
2
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved.
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
*
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.
22
*/
23
24
/**
25
* @test
26
* @bug 6725892
27
* @library /test/lib
28
* @run main/othervm -Dsun.net.httpserver.maxReqTime=2 Test
29
* @run main/othervm -Djava.net.preferIPv6Addresses=true -Dsun.net.httpserver.maxReqTime=2 Test
30
* @summary
31
*/
32
33
import com.sun.net.httpserver.*;
34
35
import java.util.concurrent.*;
36
import java.util.logging.*;
37
import java.io.*;
38
import java.net.*;
39
import javax.net.ssl.*;
40
41
import jdk.test.lib.net.URIBuilder;
42
43
public class Test {
44
45
static HttpServer s1;
46
static int port;
47
static URL url;
48
static final String RESPONSE_BODY = "response";
49
static boolean failed = false;
50
51
static class Handler implements HttpHandler {
52
53
public void handle(HttpExchange t)
54
throws IOException
55
{
56
InputStream is = t.getRequestBody();
57
InetSocketAddress rem = t.getRemoteAddress();
58
System.out.println("Request from: " + rem);
59
while (is.read () != -1) ;
60
is.close();
61
String requrl = t.getRequestURI().toString();
62
OutputStream os = t.getResponseBody();
63
t.sendResponseHeaders(200, RESPONSE_BODY.length());
64
os.write(RESPONSE_BODY.getBytes());
65
t.close();
66
}
67
}
68
69
public static void main(String[] args) throws Exception {
70
71
ExecutorService exec = Executors.newCachedThreadPool();
72
73
InetAddress loopback = InetAddress.getLoopbackAddress();
74
try {
75
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
76
s1 = HttpServer.create(addr, 100);
77
HttpHandler h = new Handler();
78
HttpContext c1 = s1.createContext("/", h);
79
s1.setExecutor(exec);
80
s1.start();
81
82
port = s1.getAddress().getPort();
83
System.out.println("Server on port " + port);
84
url = URIBuilder.newBuilder()
85
.scheme("http")
86
.loopback()
87
.port(port)
88
.path("/foo")
89
.toURLUnchecked();
90
System.out.println("URL: " + url);
91
test1();
92
test2();
93
test3();
94
Thread.sleep(2000);
95
} catch (Exception e) {
96
e.printStackTrace();
97
System.out.println("FAIL");
98
throw new RuntimeException();
99
} finally {
100
s1.stop(0);
101
System.out.println("After Shutdown");
102
exec.shutdown();
103
}
104
}
105
106
// open TCP connection without sending anything. Check server closes it.
107
108
static void test1() throws IOException {
109
failed = false;
110
Socket s = new Socket(InetAddress.getLoopbackAddress(), port);
111
InputStream is = s.getInputStream();
112
// server should close connection after 2 seconds. We wait up to 10
113
s.setSoTimeout(10000);
114
try {
115
is.read();
116
} catch (SocketTimeoutException e) {
117
failed = true;
118
}
119
s.close();
120
if (failed) {
121
System.out.println("test1: FAIL");
122
throw new RuntimeException();
123
} else {
124
System.out.println("test1: OK");
125
}
126
}
127
128
// send request and don't read response. Check server closes connection
129
130
static void test2() throws IOException {
131
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
132
urlc.setReadTimeout(20 * 1000);
133
InputStream is = urlc.getInputStream();
134
// we won't read response and check if it times out
135
// on server. If it timesout at client then there is a problem
136
try {
137
Thread.sleep(10 * 1000);
138
while (is.read() != -1) ;
139
} catch (InterruptedException e) {
140
System.out.println(e);
141
System.out.println("test2: FAIL");
142
throw new RuntimeException("unexpected error");
143
} catch (SocketTimeoutException e1) {
144
System.out.println(e1);
145
System.out.println("test2: FAIL");
146
throw new RuntimeException("client timedout");
147
} finally {
148
is.close();
149
}
150
System.out.println("test2: OK");
151
}
152
153
// same as test2, but repeated with multiple connections
154
// including a number of valid request/responses
155
156
// Worker: a thread opens a connection to the server in one of three modes.
157
// NORMAL - sends a request, waits for response, and checks valid response
158
// REQUEST - sends a partial request, and blocks, to see if
159
// server closes the connection.
160
// RESPONSE - sends a request, partially reads response and blocks,
161
// to see if server closes the connection.
162
163
static class Worker extends Thread {
164
CountDownLatch latch;
165
Mode mode;
166
167
enum Mode {
168
REQUEST, // block during sending of request
169
RESPONSE, // block during reading of response
170
NORMAL // don't block
171
};
172
173
Worker (CountDownLatch latch, Mode mode) {
174
this.latch = latch;
175
this.mode = mode;
176
}
177
178
void fail(String msg) {
179
System.out.println(msg);
180
failed = true;
181
}
182
183
public void run() {
184
HttpURLConnection urlc;
185
InputStream is = null;
186
187
try {
188
urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
189
urlc.setReadTimeout(20 * 1000);
190
urlc.setDoOutput(true);
191
} catch (IOException e) {
192
fail("Worker: failed to connect to server");
193
latch.countDown();
194
return;
195
}
196
try {
197
OutputStream os = urlc.getOutputStream();
198
os.write("foo".getBytes());
199
if (mode == Mode.REQUEST) {
200
Thread.sleep(3000);
201
}
202
os.close();
203
is = urlc.getInputStream();
204
if (mode == Mode.RESPONSE) {
205
Thread.sleep(3000);
206
}
207
if (!checkResponse(is, RESPONSE_BODY)) {
208
fail("Worker: response");
209
}
210
is.close();
211
return;
212
} catch (InterruptedException e0) {
213
fail("Worker: timedout");
214
} catch (SocketTimeoutException e1) {
215
fail("Worker: timedout");
216
} catch (IOException e2) {
217
switch (mode) {
218
case NORMAL:
219
fail("Worker: " + e2.getMessage());
220
break;
221
case RESPONSE:
222
if (is == null) {
223
fail("Worker: " + e2.getMessage());
224
break;
225
}
226
// default: is ok
227
}
228
} finally {
229
latch.countDown();
230
}
231
}
232
}
233
234
static final int NUM = 20;
235
236
static void test3() throws Exception {
237
failed = false;
238
CountDownLatch l = new CountDownLatch(NUM*3);
239
Worker[] workers = new Worker[NUM*3];
240
for (int i=0; i<NUM; i++) {
241
workers[i*3] = new Worker(l, Worker.Mode.NORMAL);
242
workers[i*3+1] = new Worker(l, Worker.Mode.REQUEST);
243
workers[i*3+2] = new Worker(l, Worker.Mode.RESPONSE);
244
workers[i*3].start();
245
workers[i*3+1].start();
246
workers[i*3+2].start();
247
}
248
l.await();
249
for (int i=0; i<NUM*3; i++) {
250
workers[i].join();
251
}
252
if (failed) {
253
throw new RuntimeException("test3: failed");
254
}
255
System.out.println("test3: OK");
256
}
257
258
static boolean checkResponse(InputStream is, String resp) {
259
try {
260
ByteArrayOutputStream bos = new ByteArrayOutputStream();
261
byte[] buf = new byte[64];
262
int c;
263
while ((c=is.read(buf)) != -1) {
264
bos.write(buf, 0, c);
265
}
266
bos.close();
267
if (!bos.toString().equals(resp)) {
268
System.out.println("Wrong response: " + bos.toString());
269
return false;
270
}
271
} catch (IOException e) {
272
System.out.println(e);
273
return false;
274
}
275
return true;
276
}
277
}
278
279