Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/test/jdk/sun/net/www/http/KeepAliveCache/KeepAliveProperty.java
66646 views
1
/*
2
* Copyright (c) 2022, 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
* @library /test/lib
27
* @bug 8278067
28
* @run main/othervm -Dhttp.keepAlive.time.server=30 KeepAliveProperty long
29
* @run main/othervm -Dhttp.keepAlive.time.server=1 KeepAliveProperty short
30
* @run main/othervm -ea -Dhttp.keepAlive.time.server=0 KeepAliveProperty short
31
*/
32
33
import java.net.*;
34
import java.io.*;
35
import java.nio.charset.*;
36
import java.util.logging.*;
37
import jdk.test.lib.net.URIBuilder;
38
import static java.net.Proxy.NO_PROXY;
39
40
public class KeepAliveProperty {
41
42
static volatile boolean pass = false;
43
44
static class Server extends Thread {
45
final ServerSocket server;
46
47
Server (ServerSocket server) {
48
super ();
49
this.server = server;
50
}
51
52
void readAll (Socket s) throws IOException {
53
byte[] buf = new byte [128];
54
int c;
55
String request = "";
56
InputStream is = s.getInputStream ();
57
while ((c=is.read(buf)) > 0) {
58
request += new String(buf, 0, c, StandardCharsets.US_ASCII);
59
if (request.contains("\r\n\r\n")) {
60
return;
61
}
62
}
63
if (c == -1)
64
throw new IOException("Socket closed");
65
}
66
67
Socket s = null;
68
String BODY;
69
String CLEN;
70
PrintStream out;
71
72
public void run() {
73
try {
74
s = server.accept();
75
readAll(s);
76
77
BODY = "Hello world";
78
CLEN = "Content-Length: " + BODY.length() + "\r\n";
79
out = new PrintStream(new BufferedOutputStream(s.getOutputStream() ));
80
81
/* send the header */
82
out.print("HTTP/1.1 200 OK\r\n");
83
out.print("Content-Type: text/plain; charset=iso-8859-1\r\n");
84
out.print(CLEN);
85
out.print("\r\n");
86
out.print(BODY);
87
out.flush();
88
} catch (Exception e) {
89
pass = false;
90
try {
91
if (s != null)
92
s.close();
93
server.close();
94
} catch (IOException unused) {}
95
return;
96
}
97
98
// second request may legitimately fail
99
100
try (Socket s2 = s; ServerSocket server2 = server; PrintStream out2 = out) {
101
// wait for second request.
102
readAll(s2);
103
104
BODY = "Goodbye world";
105
CLEN = "Content-Length: " + BODY.length() + "\r\n";
106
107
/* send the header */
108
out2.print("HTTP/1.1 200 OK\r\n");
109
out2.print("Content-Type: text/plain; charset=iso-8859-1\r\n");
110
out2.print(CLEN);
111
out2.print("\r\n");
112
out2.print(BODY);
113
out2.flush();
114
pass = !expectClose;
115
if (!pass) System.out.println("Failed: expected close");
116
} catch (Exception e) {
117
pass = expectClose;
118
if (!pass) System.out.println("Failed: did not expect close");
119
}
120
}
121
}
122
123
static String fetch(URL url) throws Exception {
124
InputStream in = url.openConnection(NO_PROXY).getInputStream();
125
String s = "";
126
byte b[] = new byte[128];
127
int n;
128
do {
129
n = in.read(b);
130
if (n > 0)
131
s += new String(b, 0, n, StandardCharsets.US_ASCII);
132
} while (n > 0);
133
in.close();
134
return s;
135
}
136
137
static volatile boolean expectClose;
138
139
public static void main(String args[]) throws Exception {
140
// exercise the logging code
141
Logger logger = Logger.getLogger("sun.net.www.protocol.http.HttpURLConnection");
142
logger.setLevel(Level.FINEST);
143
ConsoleHandler h = new ConsoleHandler();
144
h.setLevel(Level.FINEST);
145
logger.addHandler(h);
146
147
expectClose = args[0].equals("short");
148
InetAddress loopback = InetAddress.getLoopbackAddress();
149
ServerSocket ss = new ServerSocket();
150
ss.bind(new InetSocketAddress(loopback, 0));
151
Server s = new Server(ss);
152
s.start();
153
154
URL url = URIBuilder.newBuilder()
155
.scheme("http")
156
.loopback()
157
.port(ss.getLocalPort())
158
.toURL();
159
System.out.println("URL: " + url);
160
161
if (!fetch(url).equals("Hello world"))
162
throw new RuntimeException("Failed on first request");
163
164
// Wait a while to see if connection is closed
165
Thread.sleep(3 * 1000);
166
167
try {
168
if (!fetch(url).equals("Goodbye world"))
169
throw new RuntimeException("Failed on second request");
170
} catch (Exception e) {
171
if (!expectClose)
172
throw e;
173
}
174
175
if (!pass)
176
throw new RuntimeException("Failed in server");
177
}
178
}
179
180