Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/AbstractNoBody.java
41149 views
1
/*
2
* Copyright (c) 2015, 2018, 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
import java.io.IOException;
25
import java.io.InputStream;
26
import java.net.InetAddress;
27
import java.net.InetSocketAddress;
28
import java.util.concurrent.Executor;
29
import java.util.concurrent.ExecutorService;
30
import java.util.concurrent.Executors;
31
import com.sun.net.httpserver.HttpExchange;
32
import com.sun.net.httpserver.HttpHandler;
33
import com.sun.net.httpserver.HttpServer;
34
import com.sun.net.httpserver.HttpsConfigurator;
35
import com.sun.net.httpserver.HttpsServer;
36
import java.net.http.HttpClient;
37
import javax.net.ssl.SSLContext;
38
import jdk.test.lib.net.SimpleSSLContext;
39
import org.testng.annotations.AfterTest;
40
import org.testng.annotations.BeforeTest;
41
import org.testng.annotations.DataProvider;
42
43
public abstract class AbstractNoBody {
44
45
SSLContext sslContext;
46
HttpServer httpTestServer; // HTTP/1.1 [ 4 servers ]
47
HttpsServer httpsTestServer; // HTTPS/1.1
48
Http2TestServer http2TestServer; // HTTP/2 ( h2c )
49
Http2TestServer https2TestServer; // HTTP/2 ( h2 )
50
String httpURI_fixed;
51
String httpURI_chunk;
52
String httpsURI_fixed;
53
String httpsURI_chunk;
54
String http2URI_fixed;
55
String http2URI_chunk;
56
String https2URI_fixed;
57
String https2URI_chunk;
58
59
static final String SIMPLE_STRING = "Hello world. Goodbye world";
60
static final int ITERATION_COUNT = 3;
61
// a shared executor helps reduce the amount of threads created by the test
62
static final Executor executor = Executors.newFixedThreadPool(ITERATION_COUNT * 2);
63
static final ExecutorService serverExecutor = Executors.newFixedThreadPool(ITERATION_COUNT * 4);
64
65
@DataProvider(name = "variants")
66
public Object[][] variants() {
67
return new Object[][]{
68
{ httpURI_fixed, false },
69
{ httpURI_chunk, false },
70
{ httpsURI_fixed, false },
71
{ httpsURI_chunk, false },
72
{ http2URI_fixed, false },
73
{ http2URI_chunk, false },
74
{ https2URI_fixed, false,},
75
{ https2URI_chunk, false },
76
77
{ httpURI_fixed, true },
78
{ httpURI_chunk, true },
79
{ httpsURI_fixed, true },
80
{ httpsURI_chunk, true },
81
{ http2URI_fixed, true },
82
{ http2URI_chunk, true },
83
{ https2URI_fixed, true,},
84
{ https2URI_chunk, true },
85
};
86
}
87
88
HttpClient newHttpClient() {
89
return HttpClient.newBuilder()
90
.executor(executor)
91
.sslContext(sslContext)
92
.build();
93
}
94
95
static String serverAuthority(HttpServer server) {
96
return InetAddress.getLoopbackAddress().getHostName() + ":"
97
+ server.getAddress().getPort();
98
}
99
100
@BeforeTest
101
public void setup() throws Exception {
102
printStamp(START, "setup");
103
sslContext = new SimpleSSLContext().get();
104
if (sslContext == null)
105
throw new AssertionError("Unexpected null sslContext");
106
107
// HTTP/1.1
108
HttpHandler h1_fixedLengthNoBodyHandler = new HTTP1_FixedLengthNoBodyHandler();
109
HttpHandler h1_chunkNoBodyHandler = new HTTP1_ChunkedNoBodyHandler();
110
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
111
httpTestServer = HttpServer.create(sa, 0);
112
httpTestServer.setExecutor(serverExecutor);
113
httpTestServer.createContext("/http1/noBodyFixed", h1_fixedLengthNoBodyHandler);
114
httpTestServer.createContext("/http1/noBodyChunk", h1_chunkNoBodyHandler);
115
httpURI_fixed = "http://" + serverAuthority(httpTestServer) + "/http1/noBodyFixed";
116
httpURI_chunk = "http://" + serverAuthority(httpTestServer) + "/http1/noBodyChunk";
117
118
httpsTestServer = HttpsServer.create(sa, 0);
119
httpsTestServer.setExecutor(serverExecutor);
120
httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
121
httpsTestServer.createContext("/https1/noBodyFixed", h1_fixedLengthNoBodyHandler);
122
httpsTestServer.createContext("/https1/noBodyChunk", h1_chunkNoBodyHandler);
123
httpsURI_fixed = "https://" + serverAuthority(httpsTestServer) + "/https1/noBodyFixed";
124
httpsURI_chunk = "https://" + serverAuthority(httpsTestServer) + "/https1/noBodyChunk";
125
126
// HTTP/2
127
Http2Handler h2_fixedLengthNoBodyHandler = new HTTP2_FixedLengthNoBodyHandler();
128
Http2Handler h2_chunkedNoBodyHandler = new HTTP2_ChunkedNoBodyHandler();
129
130
http2TestServer = new Http2TestServer("localhost", false, 0, serverExecutor, null);
131
http2TestServer.addHandler(h2_fixedLengthNoBodyHandler, "/http2/noBodyFixed");
132
http2TestServer.addHandler(h2_chunkedNoBodyHandler, "/http2/noBodyChunk");
133
http2URI_fixed = "http://" + http2TestServer.serverAuthority() + "/http2/noBodyFixed";
134
http2URI_chunk = "http://" + http2TestServer.serverAuthority() + "/http2/noBodyChunk";
135
136
https2TestServer = new Http2TestServer("localhost", true, 0, serverExecutor, sslContext);
137
https2TestServer.addHandler(h2_fixedLengthNoBodyHandler, "/https2/noBodyFixed");
138
https2TestServer.addHandler(h2_chunkedNoBodyHandler, "/https2/noBodyChunk");
139
https2URI_fixed = "https://" + https2TestServer.serverAuthority() + "/https2/noBodyFixed";
140
https2URI_chunk = "https://" + https2TestServer.serverAuthority() + "/https2/noBodyChunk";
141
142
httpTestServer.start();
143
httpsTestServer.start();
144
http2TestServer.start();
145
https2TestServer.start();
146
printStamp(END,"setup");
147
}
148
149
@AfterTest
150
public void teardown() throws Exception {
151
printStamp(START, "teardown");
152
httpTestServer.stop(0);
153
httpsTestServer.stop(0);
154
http2TestServer.stop();
155
https2TestServer.stop();
156
printStamp(END, "teardown");
157
}
158
159
static final long start = System.nanoTime();
160
static final String START = "start";
161
static final String END = "end ";
162
static long elapsed() { return (System.nanoTime() - start)/1000_000;}
163
void printStamp(String what, String fmt, Object... args) {
164
long elapsed = elapsed();
165
long sec = elapsed/1000;
166
long ms = elapsed % 1000;
167
String time = sec > 0 ? sec + "sec " : "";
168
time = time + ms + "ms";
169
System.out.printf("%s: %s \t [%s]\t %s%n",
170
getClass().getSimpleName(), what, time, String.format(fmt,args));
171
}
172
173
174
static class HTTP1_FixedLengthNoBodyHandler implements HttpHandler {
175
@Override
176
public void handle(HttpExchange t) throws IOException {
177
//out.println("NoBodyHandler received request to " + t.getRequestURI());
178
try (InputStream is = t.getRequestBody()) {
179
is.readAllBytes();
180
}
181
t.sendResponseHeaders(200, -1); // no body
182
}
183
}
184
185
static class HTTP1_ChunkedNoBodyHandler implements HttpHandler {
186
@Override
187
public void handle(HttpExchange t) throws IOException {
188
//out.println("NoBodyHandler received request to " + t.getRequestURI());
189
try (InputStream is = t.getRequestBody()) {
190
is.readAllBytes();
191
}
192
t.sendResponseHeaders(200, 0); // chunked
193
t.getResponseBody().close(); // write nothing
194
}
195
}
196
197
static class HTTP2_FixedLengthNoBodyHandler implements Http2Handler {
198
@Override
199
public void handle(Http2TestExchange t) throws IOException {
200
//out.println("NoBodyHandler received request to " + t.getRequestURI());
201
try (InputStream is = t.getRequestBody()) {
202
is.readAllBytes();
203
}
204
t.sendResponseHeaders(200, 0);
205
}
206
}
207
208
static class HTTP2_ChunkedNoBodyHandler implements Http2Handler {
209
@Override
210
public void handle(Http2TestExchange t) throws IOException {
211
//out.println("NoBodyHandler received request to " + t.getRequestURI());
212
try (InputStream is = t.getRequestBody()) {
213
is.readAllBytes();
214
}
215
t.sendResponseHeaders(200, -1);
216
t.getResponseBody().close(); // write nothing
217
}
218
}
219
}
220
221