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/Test13.java
38855 views
1
/*
2
* Copyright (c) 2005, 2010, 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 Test13
28
* @summary Light weight HTTP server
29
*/
30
31
import com.sun.net.httpserver.*;
32
33
import java.util.concurrent.*;
34
import java.util.logging.*;
35
import java.io.*;
36
import java.net.*;
37
38
import javax.net.ssl.*;
39
40
/* basic http/s connectivity test
41
* Tests:
42
* - same as Test12, but with 64 threads
43
*/
44
45
public class Test13 extends Test {
46
47
static SSLContext ctx;
48
49
final static int NUM = 32; // was 32
50
51
static boolean fail = false;
52
53
public static void main (String[] args) throws Exception {
54
HttpServer s1 = null;
55
HttpsServer s2 = null;
56
ExecutorService executor=null;
57
Logger l = Logger.getLogger ("com.sun.net.httpserver");
58
Handler ha = new ConsoleHandler();
59
ha.setLevel(Level.ALL);
60
l.setLevel(Level.ALL);
61
l.addHandler(ha);
62
try {
63
String root = System.getProperty ("test.src")+ "/docs";
64
System.out.print ("Test13: ");
65
InetSocketAddress addr = new InetSocketAddress (0);
66
s1 = HttpServer.create (addr, 0);
67
s2 = HttpsServer.create (addr, 0);
68
HttpHandler h = new FileServerHandler (root);
69
HttpContext c1 = s1.createContext ("/test1", h);
70
HttpContext c2 = s2.createContext ("/test1", h);
71
executor = Executors.newCachedThreadPool();
72
s1.setExecutor (executor);
73
s2.setExecutor (executor);
74
ctx = new SimpleSSLContext(System.getProperty("test.src")).get();
75
s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
76
s1.start();
77
s2.start();
78
79
int port = s1.getAddress().getPort();
80
int httpsport = s2.getAddress().getPort();
81
Runner r[] = new Runner[NUM*2];
82
for (int i=0; i<NUM; i++) {
83
r[i] = new Runner (true, "http", root+"/test1", port, "smallfile.txt", 23);
84
r[i+NUM] = new Runner (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);
85
}
86
start (r);
87
join (r);
88
System.out.println ("OK");
89
} finally {
90
delay();
91
if (s1 != null)
92
s1.stop(2);
93
if (s2 != null)
94
s2.stop(2);
95
if (executor != null)
96
executor.shutdown ();
97
}
98
}
99
100
static void start (Runner[] x) {
101
for (int i=0; i<x.length; i++) {
102
if (x[i] != null)
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
if (x[i] != null)
111
x[i].join();
112
} catch (InterruptedException e) {}
113
}
114
}
115
116
117
static class Runner extends Thread {
118
119
boolean fixedLen;
120
String protocol;
121
String root;
122
int port;
123
String f;
124
int size;
125
126
Runner (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 = new URL (protocol+"://localhost:"+port+"/test1/"+f);
138
HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
139
if (urlc instanceof HttpsURLConnection) {
140
HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
141
urlcs.setHostnameVerifier (new HostnameVerifier () {
142
public boolean verify (String s, SSLSession s1) {
143
return true;
144
}
145
});
146
urlcs.setSSLSocketFactory (ctx.getSocketFactory());
147
}
148
byte [] buf = new byte [4096];
149
150
if (fixedLen) {
151
urlc.setRequestProperty ("XFixed", "yes");
152
}
153
InputStream is = urlc.getInputStream();
154
File temp = File.createTempFile ("Test1", null);
155
temp.deleteOnExit();
156
OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
157
int c, count = 0;
158
while ((c=is.read(buf)) != -1) {
159
count += c;
160
fout.write (buf, 0, c);
161
}
162
is.close();
163
fout.close();
164
165
if (count != size) {
166
throw new RuntimeException ("wrong amount of data returned");
167
}
168
String orig = root + "/" + f;
169
compare (new File(orig), temp);
170
temp.delete();
171
} catch (Exception e) {
172
e.printStackTrace();
173
fail = true;
174
}
175
}
176
}
177
178
/* compare the contents of the two files */
179
180
static void compare (File f1, File f2) throws IOException {
181
InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
182
InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
183
184
int c1,c2;
185
try {
186
while ((c1=i1.read()) != -1) {
187
c2 = i2.read();
188
if (c1 != c2) {
189
throw new RuntimeException ("file compare failed 1");
190
}
191
}
192
if (i2.read() != -1) {
193
throw new RuntimeException ("file compare failed 2");
194
}
195
} finally {
196
i1.close();
197
i2.close();
198
}
199
}
200
}
201
202