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