1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
|
/*
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @modules java.net.http
* java.logging
* jdk.httpserver
* @library /lib/testlibrary/ /
* @build jdk.testlibrary.SimpleSSLContext
* @compile ../../../com/sun/net/httpserver/LogFilter.java
* @compile ../../../com/sun/net/httpserver/EchoHandler.java
* @compile ../../../com/sun/net/httpserver/FileServerHandler.java
* @run main/othervm/timeout=40 ManyRequestsLegacy
* @run main/othervm/timeout=40 -Dtest.insertDelay=true ManyRequestsLegacy
* @run main/othervm/timeout=40 -Dtest.chunkSize=64 ManyRequestsLegacy
* @run main/othervm/timeout=40 -Dtest.insertDelay=true -Dtest.chunkSize=64 ManyRequestsLegacy
* @summary Send a large number of requests asynchronously using the legacy URL.openConnection(), to help sanitize results of the test ManyRequest.java.
*/
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.HostnameVerifier;
import com.sun.net.httpserver.HttpsConfigurator;
import com.sun.net.httpserver.HttpsParameters;
import com.sun.net.httpserver.HttpsServer;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.URI;
import java.net.URLConnection;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import java.net.http.HttpClient;
import java.net.http.HttpClient.Version;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse;
import java.net.InetSocketAddress;
import java.util.Arrays;
import java.util.Formatter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Random;
import java.util.logging.Logger;
import java.util.logging.Level;
import jdk.testlibrary.SimpleSSLContext;
public class ManyRequestsLegacy {
volatile static int counter = 0;
public static void main(String[] args) throws Exception {
Logger logger = Logger.getLogger("com.sun.net.httpserver");
logger.setLevel(Level.ALL);
logger.info("TEST");
System.out.println("Sending " + REQUESTS
+ " requests; delay=" + INSERT_DELAY
+ ", chunks=" + CHUNK_SIZE
+ ", XFixed=" + XFIXED);
SSLContext ctx = new SimpleSSLContext().get();
SSLContext.setDefault(ctx);
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
HttpsServer server = HttpsServer.create(addr, 0);
server.setHttpsConfigurator(new Configurator(ctx));
LegacyHttpClient client = new LegacyHttpClient();
try {
test(server, client);
System.out.println("OK");
} finally {
server.stop(0);
}
}
//static final int REQUESTS = 1000;
static final int REQUESTS = 20;
static final boolean INSERT_DELAY = Boolean.getBoolean("test.insertDelay");
static final int CHUNK_SIZE = Math.max(0,
Integer.parseInt(System.getProperty("test.chunkSize", "0")));
static final boolean XFIXED = Boolean.getBoolean("test.XFixed");
static class LegacyHttpClient {
static final class LegacyHttpResponse implements HttpResponse<byte[]> {
final HttpRequest request;
final byte[] response;
final int statusCode;
public LegacyHttpResponse(HttpRequest request, int statusCode, byte[] response) {
this.request = request;
this.statusCode = statusCode;
this.response = response;
}
private <T> T error() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public int statusCode() { return statusCode;}
@Override
public HttpRequest request() {return request;}
@Override
public Optional<HttpResponse<byte[]>> previousResponse() {return Optional.empty();}
@Override
public HttpHeaders headers() { return error(); }
@Override
public byte[] body() {return response;}
@Override
public Optional<SSLSession> sslSession() {
return Optional.empty(); // for now
}
@Override
public URI uri() { return request.uri();}
@Override
public HttpClient.Version version() { return Version.HTTP_1_1;}
}
private void debugCompleted(String tag, long startNanos, HttpRequest req) {
System.err.println(tag + " elapsed "
+ (System.nanoTime() - startNanos)/1000_000L
+ " millis for " + req.method()
+ " to " + req.uri());
}
CompletableFuture<? extends HttpResponse<byte[]>> sendAsync(HttpRequest r, byte[] buf) {
long start = System.nanoTime();
try {
CompletableFuture<LegacyHttpResponse> cf = new CompletableFuture<>();
URLConnection urlc = r.uri().toURL().openConnection();
HttpURLConnection httpc = (HttpURLConnection)urlc;
httpc.setRequestMethod(r.method());
for (String s : r.headers().map().keySet()) {
httpc.setRequestProperty(s, r.headers().allValues(s)
.stream().collect(Collectors.joining(",")));
}
httpc.setDoInput(true);
if (buf != null) httpc.setDoOutput(true);
Thread t = new Thread(() -> {
try {
if (buf != null) {
try (OutputStream os = httpc.getOutputStream()) {
os.write(buf);
os.flush();
}
}
LegacyHttpResponse response = new LegacyHttpResponse(r,
httpc.getResponseCode(),httpc.getInputStream().readAllBytes());
cf.complete(response);
} catch(Throwable x) {
cf.completeExceptionally(x);
}
});
t.start();
return cf.whenComplete((b,x) -> debugCompleted("ClientImpl (async)", start, r));
} catch(Throwable t) {
debugCompleted("ClientImpl (async)", start, r);
return CompletableFuture.failedFuture(t);
}
}
}
static class TestEchoHandler extends EchoHandler {
final Random rand = new Random();
@Override
public void handle(HttpExchange e) throws IOException {
System.out.println("Server: received " + e.getRequestURI());
super.handle(e);
}
@Override
protected void close(HttpExchange t, OutputStream os) throws IOException {
if (INSERT_DELAY) {
try { Thread.sleep(rand.nextInt(200)); }
catch (InterruptedException e) {}
}
System.out.println("Server: close outbound: " + t.getRequestURI());
os.close();
}
@Override
protected void close(HttpExchange t, InputStream is) throws IOException {
if (INSERT_DELAY) {
try { Thread.sleep(rand.nextInt(200)); }
catch (InterruptedException e) {}
}
System.out.println("Server: close inbound: " + t.getRequestURI());
is.close();
}
}
static void test(HttpsServer server, LegacyHttpClient client) throws Exception {
int port = server.getAddress().getPort();
URI baseURI = new URI("https://localhost:" + port + "/foo/x");
server.createContext("/foo", new TestEchoHandler());
server.start();
RequestLimiter limiter = new RequestLimiter(40);
Random rand = new Random();
CompletableFuture<?>[] results = new CompletableFuture<?>[REQUESTS];
HashMap<HttpRequest,byte[]> bodies = new HashMap<>();
for (int i=0; i<REQUESTS; i++) {
byte[] buf = new byte[(i+1)*CHUNK_SIZE+i+1]; // different size bodies
rand.nextBytes(buf);
URI uri = new URI(baseURI.toString() + String.valueOf(i+1));
HttpRequest r = HttpRequest.newBuilder(uri)
.header("XFixed", "true")
.POST(BodyPublishers.ofByteArray(buf))
.build();
bodies.put(r, buf);
results[i] =
limiter.whenOkToSend()
.thenCompose((v) -> {
System.out.println("Client: sendAsync: " + r.uri());
return client.sendAsync(r, buf);
})
.thenCompose((resp) -> {
limiter.requestComplete();
if (resp.statusCode() != 200) {
String s = "Expected 200, got: " + resp.statusCode();
System.out.println(s + " from "
+ resp.request().uri().getPath());
return completedWithIOException(s);
} else {
counter++;
System.out.println("Result (" + counter + ") from "
+ resp.request().uri().getPath());
}
return CompletableFuture.completedStage(resp.body())
.thenApply((b) -> new Pair<>(resp, b));
})
.thenAccept((pair) -> {
HttpRequest request = pair.t.request();
byte[] requestBody = bodies.get(request);
check(Arrays.equals(requestBody, pair.u),
"bodies not equal:[" + bytesToHexString(requestBody)
+ "] [" + bytesToHexString(pair.u) + "]");
});
}
// wait for them all to complete and throw exception in case of error
CompletableFuture.allOf(results).join();
}
static <T> CompletableFuture<T> completedWithIOException(String message) {
return CompletableFuture.failedFuture(new IOException(message));
}
static String bytesToHexString(byte[] bytes) {
if (bytes == null)
return "null";
StringBuilder sb = new StringBuilder(bytes.length * 2);
Formatter formatter = new Formatter(sb);
for (byte b : bytes) {
formatter.format("%02x", b);
}
return sb.toString();
}
static final class Pair<T,U> {
Pair(T t, U u) {
this.t = t; this.u = u;
}
T t;
U u;
}
/**
* A simple limiter for controlling the number of requests to be run in
* parallel whenOkToSend() is called which returns a CF<Void> that allows
* each individual request to proceed, or block temporarily (blocking occurs
* on the waiters list here. As each request actually completes
* requestComplete() is called to notify this object, and allow some
* requests to continue.
*/
static class RequestLimiter {
static final CompletableFuture<Void> COMPLETED_FUTURE =
CompletableFuture.completedFuture(null);
final int maxnumber;
final LinkedList<CompletableFuture<Void>> waiters;
int number;
boolean blocked;
RequestLimiter(int maximum) {
waiters = new LinkedList<>();
maxnumber = maximum;
}
synchronized void requestComplete() {
number--;
// don't unblock until number of requests has halved.
if ((blocked && number <= maxnumber / 2) ||
(!blocked && waiters.size() > 0)) {
int toRelease = Math.min(maxnumber - number, waiters.size());
for (int i=0; i<toRelease; i++) {
CompletableFuture<Void> f = waiters.remove();
number ++;
f.complete(null);
}
blocked = number >= maxnumber;
}
}
synchronized CompletableFuture<Void> whenOkToSend() {
if (blocked || number + 1 >= maxnumber) {
blocked = true;
CompletableFuture<Void> r = new CompletableFuture<>();
waiters.add(r);
return r;
} else {
number++;
return COMPLETED_FUTURE;
}
}
}
static void check(boolean cond, Object... msg) {
if (cond)
return;
StringBuilder sb = new StringBuilder();
for (Object o : msg)
sb.append(o);
throw new RuntimeException(sb.toString());
}
static class Configurator extends HttpsConfigurator {
public Configurator(SSLContext ctx) {
super(ctx);
}
public void configure(HttpsParameters params) {
params.setSSLParameters(getSSLContext().getSupportedSSLParameters());
}
}
}
|