Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/Test9a.java
41152 views
1
/*
2
* Copyright (c) 2005, 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 6270015
27
* @library /test/lib
28
* @build jdk.test.lib.net.SimpleSSLContext jdk.test.lib.net.URIBuilder
29
* @run main/othervm Test9a
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test9a
31
* @summary Light weight HTTP server
32
*/
33
34
import com.sun.net.httpserver.*;
35
36
import java.util.concurrent.*;
37
import java.io.*;
38
import java.net.*;
39
import javax.net.ssl.*;
40
import jdk.test.lib.net.SimpleSSLContext;
41
import jdk.test.lib.net.URIBuilder;
42
43
/* Same as Test1 but requests run in parallel.
44
*/
45
46
public class Test9a extends Test {
47
48
static SSLContext serverCtx;
49
static volatile SSLContext clientCtx = null;
50
static volatile boolean error = false;
51
52
public static void main (String[] args) throws Exception {
53
HttpsServer server = null;
54
ExecutorService executor=null;
55
try {
56
String root = System.getProperty ("test.src")+ "/docs";
57
System.out.print ("Test9a: ");
58
InetAddress loopback = InetAddress.getLoopbackAddress();
59
InetSocketAddress addr = new InetSocketAddress(loopback, 0);
60
server = HttpsServer.create (addr, 0);
61
HttpHandler h = new FileServerHandler (root);
62
HttpContext c1 = server.createContext ("/test1", h);
63
executor = Executors.newCachedThreadPool();
64
server.setExecutor (executor);
65
serverCtx = new SimpleSSLContext().get();
66
clientCtx = new SimpleSSLContext().get();
67
server.setHttpsConfigurator(new HttpsConfigurator (serverCtx));
68
server.start();
69
70
int port = server.getAddress().getPort();
71
error = false;
72
Thread[] t = new Thread[100];
73
74
t[0] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
75
t[1] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
76
t[2] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
77
t[3] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
78
t[4] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
79
t[5] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
80
t[6] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
81
t[7] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
82
t[8] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
83
t[9] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
84
t[10] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
85
t[11] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
86
t[12] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
87
t[13] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
88
t[14] = test (true, "https", root+"/test1", port, "smallfile.txt", 23);
89
t[15] = test (true, "https", root+"/test1", port, "largefile.txt", 2730088);
90
for (int i=0; i<16; i++) {
91
t[i].join();
92
}
93
if (error) {
94
throw new RuntimeException ("error");
95
}
96
97
System.out.println ("OK");
98
} finally {
99
delay();
100
if (server != null)
101
server.stop(2);
102
if (executor != null)
103
executor.shutdown();
104
}
105
}
106
107
static int foo = 1;
108
109
static ClientThread test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {
110
ClientThread t = new ClientThread (fixedLen, protocol, root, port, f, size);
111
t.start();
112
return t;
113
}
114
115
static Object fileLock = new Object();
116
117
static class ClientThread extends Thread {
118
119
boolean fixedLen;
120
String protocol;
121
String root;
122
int port;
123
String f;
124
int size;
125
126
ClientThread (boolean fixedLen, String protocol, String root, int port, String f, int size) {
127
this.fixedLen = fixedLen;
128
this.protocol = protocol;
129
this.root = root;
130
this.port = port;
131
this.f = f;
132
this.size = size;
133
}
134
135
public void run () {
136
try {
137
URL url = URIBuilder.newBuilder()
138
.scheme(protocol)
139
.loopback()
140
.port(port)
141
.path("/test1/" + f)
142
.toURL();
143
144
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
145
if (urlc instanceof HttpsURLConnection) {
146
HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
147
urlcs.setHostnameVerifier (new HostnameVerifier () {
148
public boolean verify (String s, SSLSession s1) {
149
return true;
150
}
151
});
152
urlcs.setSSLSocketFactory (clientCtx.getSocketFactory());
153
}
154
byte [] buf = new byte [4096];
155
156
String s = "chunk";
157
if (fixedLen) {
158
urlc.setRequestProperty ("XFixed", "yes");
159
s = "fixed";
160
}
161
InputStream is = urlc.getInputStream();
162
File temp;
163
synchronized (fileLock) {
164
temp = File.createTempFile (s, null);
165
temp.deleteOnExit();
166
}
167
OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
168
int c, count = 0;
169
while ((c=is.read(buf)) != -1) {
170
count += c;
171
fout.write (buf, 0, c);
172
}
173
is.close();
174
fout.close();
175
176
if (count != size) {
177
System.out.println ("wrong amount of data returned");
178
System.out.println ("fixedLen = "+fixedLen);
179
System.out.println ("protocol = "+protocol);
180
System.out.println ("root = "+root);
181
System.out.println ("port = "+port);
182
System.out.println ("f = "+f);
183
System.out.println ("size = "+size);
184
System.out.println ("temp = "+temp);
185
System.out.println ("count = "+count);
186
error = true;
187
}
188
String orig = root + "/" + f;
189
compare (new File(orig), temp);
190
temp.delete();
191
} catch (Exception e) {
192
e.printStackTrace();
193
error = true;
194
}
195
}
196
}
197
198
/* compare the contents of the two files */
199
200
static void compare (File f1, File f2) throws IOException {
201
InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
202
InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
203
204
int c1,c2;
205
try {
206
while ((c1=i1.read()) != -1) {
207
c2 = i2.read();
208
if (c1 != c2) {
209
throw new RuntimeException ("file compare failed 1");
210
}
211
}
212
if (i2.read() != -1) {
213
throw new RuntimeException ("file compare failed 2");
214
}
215
} finally {
216
i1.close();
217
i2.close();
218
}
219
}
220
}
221
222