Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/KeepAliveStream/KeepAliveStreamClose.java
41155 views
1
/*
2
* Copyright (c) 2000, 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 4392195
27
* @summary Infinite loop in sun.net.www.http.KeepAliveStream [due to skip()]
28
* @library /test/lib
29
* @run main/othervm/timeout=30 KeepAliveStreamClose
30
*/
31
32
import java.net.*;
33
import java.io.*;
34
import jdk.test.lib.net.URIBuilder;
35
36
public class KeepAliveStreamClose {
37
static class XServer extends Thread {
38
ServerSocket srv;
39
Socket s;
40
InputStream is;
41
OutputStream os;
42
43
XServer (ServerSocket s) {
44
srv = s;
45
}
46
47
Socket getSocket () {
48
return (s);
49
}
50
51
// simulated HTTP server response
52
static String response = "HTTP/1.1 200 OK\nDate: Thu, 07 Dec 2000 11:32:28 GMT\n"+
53
"Server: Apache/1.3.6 (Unix)\nKeep-Alive: timeout=15, max=100\nConnection: Keep-Alive\n"+
54
"Content-length: 255\nContent-Type: text/html\n\n";
55
56
public void run() {
57
try {
58
s = srv.accept ();
59
is = s.getInputStream ();
60
os = s.getOutputStream ();
61
// read the first ten bytes
62
for (int i=0; i<10; i++) {
63
is.read();
64
}
65
os.write (response.getBytes());
66
for (int i=0; i<255; i++) {
67
os.write ("X".getBytes());
68
Thread.sleep (1000);
69
}
70
} catch (Exception e) {
71
}
72
}
73
}
74
75
/*
76
* If the server closes the connection at the same time as the client
77
* does, then the client will hang (jdk1.3) because KeepAliveStream.skip
78
* is returning zero and the calling close() stays in a loop
79
*/
80
81
public static void main (String[] args) {
82
try {
83
InetAddress loopback = InetAddress.getLoopbackAddress();
84
ServerSocket serversocket = new ServerSocket (0, 50, loopback);
85
int port = serversocket.getLocalPort ();
86
XServer server = new XServer (serversocket);
87
server.start ();
88
URL url = URIBuilder.newBuilder()
89
.scheme("http")
90
.loopback()
91
.port(port)
92
.toURL();
93
URLConnection urlc = url.openConnection ();
94
InputStream is = urlc.getInputStream ();
95
int i=0, c;
96
while ((c=is.read())!= -1) {
97
i++;
98
if (i == 5) {
99
server.getSocket().close ();
100
is.close();
101
break;
102
}
103
}
104
} catch (Exception e) {
105
e.printStackTrace();
106
throw new RuntimeException ("Unexpected exception");
107
}
108
}
109
}
110
111