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