Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/bugs/B6526913.java
41154 views
1
/*
2
* Copyright (c) 2007, 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 6526913
27
* @library /test/lib
28
* @run main/othervm -Dhttp.keepAlive=false B6526913
29
* @run main/othervm -Djava.net.preferIPv6Addresses=true
30
* -Dhttp.keepAlive=false B6526913
31
* @summary HttpExchange.getResponseBody().close() throws Exception
32
*/
33
34
import com.sun.net.httpserver.*;
35
36
import java.util.*;
37
import java.util.concurrent.*;
38
import java.io.*;
39
import java.net.*;
40
import java.security.*;
41
import java.security.cert.*;
42
import javax.net.ssl.*;
43
import jdk.test.lib.net.URIBuilder;
44
45
public class B6526913 {
46
47
public static void main (String[] args) throws Exception {
48
Handler handler = new Handler();
49
InetAddress loopback = InetAddress.getLoopbackAddress();
50
InetSocketAddress addr = new InetSocketAddress (loopback, 0);
51
HttpServer server = HttpServer.create (addr, 0);
52
HttpContext ctx = server.createContext ("/test", handler);
53
54
ExecutorService executor = Executors.newCachedThreadPool();
55
server.setExecutor (executor);
56
server.start ();
57
58
URL url = URIBuilder.newBuilder()
59
.scheme("http")
60
.loopback()
61
.port(server.getAddress().getPort())
62
.path("/test/foo.html")
63
.toURL();
64
HttpURLConnection urlc = (HttpURLConnection)url.openConnection (Proxy.NO_PROXY);
65
try {
66
InputStream is = urlc.getInputStream();
67
int c ,count = 0;
68
byte [] buf = new byte [32 * 1024];
69
while (count < 32 * 1024) {
70
count += is.read (buf);
71
}
72
is.close();
73
} finally {
74
server.stop(2);
75
executor.shutdown();
76
}
77
if (error) {
78
throw new RuntimeException ("Test failed");
79
}
80
}
81
82
public static boolean error = false;
83
84
static class Handler implements HttpHandler {
85
int invocation = 1;
86
public void handle (HttpExchange t)
87
throws IOException
88
{
89
InputStream is = t.getRequestBody();
90
try {
91
while (is.read() != -1) ;
92
is.close();
93
} catch (IOException e) {
94
e.printStackTrace();
95
error = true;
96
}
97
/* send a chunked response, but wait a while before
98
* sending the final empty chunk
99
*/
100
t.sendResponseHeaders (200, 0);
101
OutputStream os = t.getResponseBody();
102
byte[] bb = new byte [32 * 1024];
103
os.write (bb);
104
os.flush();
105
try {Thread.sleep (5000); } catch (InterruptedException e){}
106
try {
107
/* empty chunk sent here */
108
os.close();
109
} catch (IOException e) {
110
error = true;
111
e.printStackTrace();
112
}
113
t.close();
114
}
115
}
116
}
117
118