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