1 /* 2 * Copyright (c) 2015, 2016, 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 8087112 8159814 27 * @library /lib/testlibrary server 28 * @build jdk.testlibrary.SimpleSSLContext 29 * @modules jdk.incubator.httpclient/jdk.incubator.http.internal.common 30 * jdk.incubator.httpclient/jdk.incubator.http.internal.frame 31 * jdk.incubator.httpclient/jdk.incubator.http.internal.hpack 32 * @run testng/othervm -Djdk.httpclient.HttpClient.log=errors,requests,responses ServerPush 33 */ 34 35 import java.io.*; 36 import java.net.*; 37 import java.nio.file.*; 38 import java.nio.file.attribute.*; 39 import jdk.incubator.http.*; 40 import jdk.incubator.http.HttpResponse.MultiProcessor; 41 import jdk.incubator.http.HttpResponse.BodyHandler; 42 import java.util.*; 43 import java.util.concurrent.*; 44 import org.testng.annotations.Test; 45 46 public class ServerPush { 47 48 static ExecutorService e = Executors.newCachedThreadPool(); 49 50 static final int LOOPS = 13; 51 static final int FILE_SIZE = 512 * 1024 + 343; 52 53 static Path tempFile; 54 55 @Test(timeOut=30000) 56 public static void test() throws Exception { 57 Http2TestServer server = null; 58 final Path dir = Files.createTempDirectory("serverPush"); 59 try { 60 server = new Http2TestServer(false, 0); 61 server.addHandler(new PushHandler(FILE_SIZE, LOOPS), "/"); 62 tempFile = TestUtil.getAFile(FILE_SIZE); 63 64 System.err.println("Server listening on port " + server.getAddress().getPort()); 65 server.start(); 66 int port = server.getAddress().getPort(); 67 68 // use multi-level path 69 URI uri = new URI("http://127.0.0.1:" + port + "/foo/a/b/c"); 70 HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); 71 72 CompletableFuture<MultiMapResult<Path>> cf = 73 HttpClient.newBuilder().version(HttpClient.Version.HTTP_2) 74 .executor(e).build().sendAsync( 75 request, MultiProcessor.asMap((req) -> { 76 URI u = req.uri(); 77 Path path = Paths.get(dir.toString(), u.getPath()); 78 try { 79 Files.createDirectories(path.getParent()); 80 } catch (IOException ee) { 81 throw new UncheckedIOException(ee); 82 } 83 return Optional.of(BodyHandler.asFile(path)); 84 } 85 )); 86 MultiMapResult<Path> results = cf.get(); 87 88 //HttpResponse resp = request.response(); 89 System.err.println(results.size()); 90 Set<HttpRequest> requests = results.keySet(); 91 92 for (HttpRequest r : requests) { 93 URI u = r.uri(); 94 Path result = results.get(r).get().body(); 95 System.err.printf("%s -> %s\n", u.toString(), result.toString()); | 1 /* 2 * Copyright (c) 2015, 2017, 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 8087112 8159814 27 * @library /lib/testlibrary server 28 * @build jdk.testlibrary.SimpleSSLContext 29 * @modules java.base/sun.net.www.http 30 * jdk.incubator.httpclient/jdk.incubator.http.internal.common 31 * jdk.incubator.httpclient/jdk.incubator.http.internal.frame 32 * jdk.incubator.httpclient/jdk.incubator.http.internal.hpack 33 * @run testng/othervm -Djdk.internal.httpclient.hpack.debug=true -Djdk.internal.httpclient.debug=true -Djdk.httpclient.HttpClient.log=errors,requests,responses ServerPush 34 */ 35 36 import java.io.*; 37 import java.net.*; 38 import java.nio.file.*; 39 import java.nio.file.attribute.*; 40 import jdk.incubator.http.*; 41 import jdk.incubator.http.HttpResponse.MultiSubscriber; 42 import jdk.incubator.http.HttpResponse.BodyHandler; 43 import java.util.*; 44 import java.util.concurrent.*; 45 import org.testng.annotations.Test; 46 47 public class ServerPush { 48 49 static ExecutorService e = Executors.newCachedThreadPool(); 50 51 static final int LOOPS = 13; 52 static final int FILE_SIZE = 512 * 1024 + 343; 53 54 static Path tempFile; 55 56 @Test 57 public static void test() throws Exception { 58 Http2TestServer server = null; 59 final Path dir = Files.createTempDirectory("serverPush"); 60 try { 61 server = new Http2TestServer(false, 0); 62 server.addHandler(new PushHandler(FILE_SIZE, LOOPS), "/"); 63 tempFile = TestUtil.getAFile(FILE_SIZE); 64 65 System.err.println("Server listening on port " + server.getAddress().getPort()); 66 server.start(); 67 int port = server.getAddress().getPort(); 68 69 // use multi-level path 70 URI uri = new URI("http://127.0.0.1:" + port + "/foo/a/b/c"); 71 HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); 72 73 CompletableFuture<MultiMapResult<Path>> cf = 74 HttpClient.newBuilder().version(HttpClient.Version.HTTP_2) 75 .executor(e).build().sendAsync( 76 request, MultiSubscriber.asMap((req) -> { 77 URI u = req.uri(); 78 Path path = Paths.get(dir.toString(), u.getPath()); 79 try { 80 Files.createDirectories(path.getParent()); 81 } catch (IOException ee) { 82 throw new UncheckedIOException(ee); 83 } 84 return Optional.of(BodyHandler.asFile(path)); 85 } 86 )); 87 MultiMapResult<Path> results = cf.get(); 88 89 //HttpResponse resp = request.response(); 90 System.err.println(results.size()); 91 Set<HttpRequest> requests = results.keySet(); 92 93 for (HttpRequest r : requests) { 94 URI u = r.uri(); 95 Path result = results.get(r).get().body(); 96 System.err.printf("%s -> %s\n", u.toString(), result.toString()); |