Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/net/httpserver/Test1.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 Test1
30
* @run main/othervm -Djava.net.preferIPv6Addresses=true Test1
31
* @run main/othervm -Djdk.net.usePlainSocketImpl Test1
32
* @run main/othervm -Dsun.net.httpserver.maxReqTime=10 Test1
33
* @run main/othervm -Dsun.net.httpserver.nodelay=true Test1
34
* @summary Light weight HTTP server
35
*/
36
37
import com.sun.net.httpserver.*;
38
39
import java.util.concurrent.*;
40
import java.io.*;
41
import java.net.*;
42
import javax.net.ssl.*;
43
import jdk.test.lib.net.SimpleSSLContext;
44
import jdk.test.lib.net.URIBuilder;
45
46
/* basic http/s connectivity test
47
* Tests:
48
* - client/server
49
* - send/receive large/small file
50
* - chunked encoding
51
* - via http and https
52
*
53
* The test is also run with sun.net.httpserver.nodelay simply to exercise
54
* this option. There is no specific pass or failure related to running with
55
* this option.
56
*/
57
58
public class Test1 extends Test {
59
60
static SSLContext ctx;
61
62
public static void main (String[] args) throws Exception {
63
HttpServer s1 = null;
64
HttpsServer s2 = null;
65
ExecutorService executor=null;
66
try {
67
String root = System.getProperty ("test.src")+ "/docs";
68
System.out.print ("Test1: ");
69
InetAddress loopback = InetAddress.getLoopbackAddress();
70
InetSocketAddress addr = new InetSocketAddress (loopback, 0);
71
s1 = HttpServer.create (addr, 0);
72
if (s1 instanceof HttpsServer) {
73
throw new RuntimeException ("should not be httpsserver");
74
}
75
s2 = HttpsServer.create (addr, 0);
76
HttpHandler h = new FileServerHandler (root);
77
HttpContext c1 = s1.createContext ("/test1", h);
78
HttpContext c2 = s2.createContext ("/test1", h);
79
executor = Executors.newCachedThreadPool();
80
s1.setExecutor (executor);
81
s2.setExecutor (executor);
82
ctx = new SimpleSSLContext().get();
83
s2.setHttpsConfigurator(new HttpsConfigurator (ctx));
84
s1.start();
85
s2.start();
86
87
int port = s1.getAddress().getPort();
88
int httpsport = s2.getAddress().getPort();
89
test (true, "http", root+"/test1", port, "smallfile.txt", 23);
90
test (true, "http", root+"/test1", port, "largefile.txt", 2730088);
91
test (true, "https", root+"/test1", httpsport, "smallfile.txt", 23);
92
test (true, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
93
test (false, "http", root+"/test1", port, "smallfile.txt", 23);
94
test (false, "http", root+"/test1", port, "largefile.txt", 2730088);
95
test (false, "https", root+"/test1", httpsport, "smallfile.txt", 23);
96
test (false, "https", root+"/test1", httpsport, "largefile.txt", 2730088);
97
System.out.println ("OK");
98
} finally {
99
delay();
100
if (s1 != null)
101
s1.stop(2);
102
if (s2 != null)
103
s2.stop(2);
104
if (executor != null)
105
executor.shutdown ();
106
}
107
}
108
109
static void test (boolean fixedLen, String protocol, String root, int port, String f, int size) throws Exception {
110
URL url = URIBuilder.newBuilder()
111
.scheme(protocol)
112
.loopback()
113
.port(port)
114
.path("/test1/"+f)
115
.toURL();
116
HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
117
if (urlc instanceof HttpsURLConnection) {
118
HttpsURLConnection urlcs = (HttpsURLConnection) urlc;
119
urlcs.setHostnameVerifier (new HostnameVerifier () {
120
public boolean verify (String s, SSLSession s1) {
121
return true;
122
}
123
});
124
urlcs.setSSLSocketFactory (ctx.getSocketFactory());
125
}
126
byte [] buf = new byte [4096];
127
128
if (fixedLen) {
129
urlc.setRequestProperty ("XFixed", "yes");
130
}
131
InputStream is = urlc.getInputStream();
132
File temp = File.createTempFile ("Test1", null);
133
temp.deleteOnExit();
134
OutputStream fout = new BufferedOutputStream (new FileOutputStream(temp));
135
int c, count = 0;
136
while ((c=is.read(buf)) != -1) {
137
count += c;
138
fout.write (buf, 0, c);
139
}
140
is.close();
141
fout.close();
142
143
if (count != size) {
144
throw new RuntimeException ("wrong amount of data returned");
145
}
146
String orig = root + "/" + f;
147
compare (new File(orig), temp);
148
temp.delete();
149
}
150
151
/* compare the contents of the two files */
152
153
static void compare (File f1, File f2) throws IOException {
154
InputStream i1 = new BufferedInputStream (new FileInputStream(f1));
155
InputStream i2 = new BufferedInputStream (new FileInputStream(f2));
156
157
int c1,c2;
158
try {
159
while ((c1=i1.read()) != -1) {
160
c2 = i2.read();
161
if (c1 != c2) {
162
throw new RuntimeException ("file compare failed 1");
163
}
164
}
165
if (i2.read() != -1) {
166
throw new RuntimeException ("file compare failed 2");
167
}
168
} finally {
169
i1.close();
170
i2.close();
171
}
172
}
173
}
174
175