Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/HttpExchange/AutoCloseableHttpExchange.java
41161 views
1
/*
2
* Copyright (c) 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
import com.sun.net.httpserver.*;
25
26
import java.io.IOException;
27
import java.io.InputStream;
28
import java.net.*;
29
import java.util.concurrent.CountDownLatch;
30
import java.util.concurrent.ExecutorService;
31
import java.util.concurrent.Executors;
32
import java.util.concurrent.atomic.AtomicBoolean;
33
34
import jdk.test.lib.net.URIBuilder;
35
import sun.net.httpserver.HttpExchangeAccess;
36
37
38
/**
39
* @test
40
* @bug 8203036
41
* @library /test/lib
42
* @modules jdk.httpserver/sun.net.httpserver
43
* @build jdk.httpserver/sun.net.httpserver.HttpExchangeAccess AutoCloseableHttpExchange
44
* @run main/othervm AutoCloseableHttpExchange
45
* @summary Ensure that HttpExchange closes correctly when utilising the
46
* AutoCloseable interface e.g. both request InputStream and response OutputStream
47
* are closed, if not already.
48
*/
49
50
public class AutoCloseableHttpExchange {
51
52
static HttpServer testHttpServer;
53
static AtomicBoolean exchangeCloseFail = new AtomicBoolean(false);
54
55
static class Handler implements HttpHandler {
56
private CountDownLatch latch;
57
58
Handler(CountDownLatch latch) {
59
this.latch = latch;
60
}
61
62
public void handle(HttpExchange t) throws IOException {
63
InputStream is = t.getRequestBody();
64
try (HttpExchange e = t) {
65
while (is.read() != -1) ;
66
t.sendResponseHeaders(200, -1);
67
}
68
if (!HttpExchangeAccess.isClosed(t)) {
69
exchangeCloseFail.set(true);
70
}
71
latch.countDown();
72
}
73
}
74
75
static void connectAndCheck(String realm) throws Exception {
76
URL url = URIBuilder.newBuilder()
77
.scheme("http")
78
.loopback()
79
.port(testHttpServer.getAddress().getPort())
80
.path(realm)
81
.toURL();
82
HttpURLConnection testConnection = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
83
InputStream is = testConnection.getInputStream();
84
while (is.read() != -1) ;
85
is.close();
86
}
87
88
public static void main(String[] args) throws Exception {
89
int CONNECTION_COUNT = 5;
90
CountDownLatch latch = new CountDownLatch(CONNECTION_COUNT);
91
92
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
93
testHttpServer = HttpServer.create(addr, 0);
94
testHttpServer.createContext("/test", new Handler(latch));
95
96
ExecutorService executor = Executors.newFixedThreadPool(CONNECTION_COUNT);
97
testHttpServer.setExecutor(executor);
98
testHttpServer.start();
99
100
while (CONNECTION_COUNT-- != 0) {
101
connectAndCheck("/test");
102
}
103
latch.await();
104
testHttpServer.stop(2);
105
executor.shutdown();
106
107
if (exchangeCloseFail.get())
108
throw new RuntimeException("The exchange was not closed properly");
109
}
110
}
111
112