Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/AsyncDisconnect.java
41159 views
1
/*
2
* Copyright (c) 2006, 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 6358532
27
* @library /test/lib
28
* @modules jdk.httpserver
29
* @run main/othervm AsyncDisconnect
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true AsyncDisconnect
31
* @summary HttpURLConnection.disconnect doesn't really do the job
32
*/
33
34
import java.net.*;
35
import java.io.*;
36
import com.sun.net.httpserver.*;
37
import java.util.concurrent.Executors;
38
import java.util.concurrent.ExecutorService;
39
40
import jdk.test.lib.net.URIBuilder;
41
42
public class AsyncDisconnect implements Runnable
43
{
44
com.sun.net.httpserver.HttpServer httpServer;
45
MyHandler httpHandler;
46
ExecutorService executorService;
47
HttpURLConnection uc;
48
49
public static void main(String[] args) throws Exception {
50
new AsyncDisconnect();
51
}
52
53
public AsyncDisconnect() throws Exception {
54
startHttpServer();
55
doClient();
56
}
57
58
void doClient() throws Exception {
59
Thread t = new Thread(this);
60
61
try {
62
InetSocketAddress address = httpServer.getAddress();
63
URL url = URIBuilder.newBuilder()
64
.scheme("http")
65
.host(address.getAddress())
66
.port(address.getPort())
67
.path("/test/")
68
.toURL();
69
uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
70
71
// create a thread that will disconnect the connection
72
t.start();
73
74
uc.getInputStream();
75
76
// if we reach here then we have failed
77
throw new RuntimeException("Failed: We Expect a SocketException to be thrown");
78
79
} catch (SocketException se) {
80
// this is what we expect to happen and is OK.
81
//System.out.println(se);
82
} finally {
83
httpServer.stop(1);
84
t.join();
85
executorService.shutdown();
86
87
}
88
}
89
90
public void run() {
91
// wait for the request to be sent to the server before calling disconnect
92
try { Thread.sleep(2000); }
93
catch (Exception e) {}
94
95
uc.disconnect();
96
}
97
98
/**
99
* Http Server
100
*/
101
public void startHttpServer() throws IOException {
102
InetAddress loopback = InetAddress.getLoopbackAddress();
103
InetSocketAddress address = new InetSocketAddress(loopback, 0);
104
httpServer = com.sun.net.httpserver.HttpServer.create(address, 0);
105
httpHandler = new MyHandler();
106
107
HttpContext ctx = httpServer.createContext("/test/", httpHandler);
108
109
executorService = Executors.newCachedThreadPool();
110
httpServer.setExecutor(executorService);
111
httpServer.start();
112
}
113
114
class MyHandler implements HttpHandler {
115
public void handle(HttpExchange t) throws IOException {
116
// give the other thread a chance to close the connection
117
try { Thread.sleep(4000); }
118
catch (Exception e) {}
119
120
t.sendResponseHeaders(400, -1);
121
t.close();
122
}
123
}
124
125
}
126
127