Skip to content

Commit 77adc77

Browse files
pconcannonMichael-Mc-Mahon
authored andcommitted
8229235: com.sun.net.httpserver.HttpExchange should implement AutoCloseable
Reviewed-by: dfuchs, michaelm
1 parent 5110530 commit 77adc77

File tree

3 files changed

+145
-1
lines changed

3 files changed

+145
-1
lines changed

src/jdk.httpserver/share/classes/com/sun/net/httpserver/HttpExchange.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
* @since 1.6
6565
*/
6666

67-
public abstract class HttpExchange {
67+
public abstract class HttpExchange implements AutoCloseable {
6868

6969
protected HttpExchange () {
7070
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package sun.net.httpserver;
27+
28+
public class HttpExchangeAccess {
29+
public static boolean isClosed(com.sun.net.httpserver.HttpExchange exch) {
30+
synchronized (exch) {
31+
return ((HttpExchangeImpl) exch).getExchangeImpl().closed;
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)