test/jdk/java/net/httpclient/http2/ImplicitPushCancel.java
author chegar
Wed, 07 Feb 2018 21:45:37 +0000
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56089 42208b2f224e
child 56093 22d94c4a3641
permissions -rw-r--r--
http-client-branch: move implementation to jdk.internal.net.http
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
     1
/*
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
     4
 *
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
     8
 *
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    13
 * accompanied this code).
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    14
 *
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    18
 *
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    21
 * questions.
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    22
 */
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    23
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    24
/*
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    25
 * @test
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    26
 * @library /lib/testlibrary server
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    27
 * @build jdk.testlibrary.SimpleSSLContext
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    28
 * @modules java.base/sun.net.www.http
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    29
 *          java.net.http/jdk.internal.net.http.common
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.frame
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.hpack
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    32
 * @run testng/othervm -Djdk.internal.httpclient.debug=true -Djdk.httpclient.HttpClient.log=errors,requests,responses,trace ImplicitPushCancel
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    33
 */
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    34
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    35
import java.io.ByteArrayInputStream;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    36
import java.io.IOException;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    37
import java.io.InputStream;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    38
import java.io.OutputStream;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    39
import java.net.URI;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    40
import java.util.Map;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    41
import java.util.Objects;
56019
dfuchs
parents: 48523
diff changeset
    42
import java.util.concurrent.CompletableFuture;
dfuchs
parents: 48523
diff changeset
    43
import java.util.concurrent.ConcurrentHashMap;
dfuchs
parents: 48523
diff changeset
    44
import java.util.concurrent.ConcurrentMap;
dfuchs
parents: 48523
diff changeset
    45
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56034
diff changeset
    46
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56034
diff changeset
    47
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56034
diff changeset
    48
import java.net.http.HttpResponse;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56034
diff changeset
    49
import java.net.http.HttpResponse.BodyHandler;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56034
diff changeset
    50
import java.net.http.HttpResponse.PushPromiseHandler;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    51
import jdk.internal.net.http.common.HttpHeadersImpl;
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    52
import org.testng.annotations.AfterTest;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    53
import org.testng.annotations.BeforeTest;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    54
import org.testng.annotations.Test;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    55
import static java.nio.charset.StandardCharsets.UTF_8;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    56
import static org.testng.Assert.assertEquals;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    57
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    58
public class ImplicitPushCancel {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    59
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    60
    static Map<String,String> PUSH_PROMISES = Map.of(
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    61
            "/x/y/z/1", "the first push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    62
            "/x/y/z/2", "the second push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    63
            "/x/y/z/3", "the third push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    64
            "/x/y/z/4", "the fourth push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    65
            "/x/y/z/5", "the fifth push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    66
            "/x/y/z/6", "the sixth push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    67
            "/x/y/z/7", "the seventh push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    68
            "/x/y/z/8", "the eight push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    69
            "/x/y/z/9", "the ninth push promise body"
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    70
    );
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    71
    static final String MAIN_RESPONSE_BODY = "the main response body";
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    72
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    73
    Http2TestServer server;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    74
    URI uri;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    75
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    76
    @BeforeTest
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    77
    public void setup() throws Exception {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    78
        server = new Http2TestServer(false, 0);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    79
        Http2Handler handler = new ServerPushHandler(MAIN_RESPONSE_BODY,
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    80
                                                     PUSH_PROMISES);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    81
        server.addHandler(handler, "/");
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    82
        server.start();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    83
        int port = server.getAddress().getPort();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    84
        System.err.println("Server listening on port " + port);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    85
        uri = new URI("http://127.0.0.1:" + port + "/foo/a/b/c");
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    86
    }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    87
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    88
    @AfterTest
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    89
    public void teardown() {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    90
        server.stop();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    91
    }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    92
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    93
    static final <T> HttpResponse<T> assert200ResponseCode(HttpResponse<T> response) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    94
        assertEquals(response.statusCode(), 200);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    95
        return response;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    96
    }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    97
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    98
    /*
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    99
     * With a handler not capable of accepting push promises, then all push
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   100
     * promises should be rejected / cancelled, without interfering with the
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   101
     * main response.
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   102
     */
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   103
    @Test
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   104
    public void test() throws Exception {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   105
        HttpClient client = HttpClient.newHttpClient();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   106
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   107
        client.sendAsync(HttpRequest.newBuilder(uri).build(), BodyHandler.asString())
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   108
                .thenApply(ImplicitPushCancel::assert200ResponseCode)
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   109
                .thenApply(HttpResponse::body)
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   110
                .thenAccept(body -> body.equals(MAIN_RESPONSE_BODY))
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   111
                .join();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   112
56019
dfuchs
parents: 48523
diff changeset
   113
        ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<String>>> promises
dfuchs
parents: 48523
diff changeset
   114
                = new ConcurrentHashMap<>();
dfuchs
parents: 48523
diff changeset
   115
        PushPromiseHandler<String> pph = PushPromiseHandler
56034
43b531ed872b http-client-branch: renamed PushPromiseHandler.withPushPromises into PushPromiseHandler.of
dfuchs
parents: 56033
diff changeset
   116
                .of((r) -> BodyHandler.asString(), promises);
56019
dfuchs
parents: 48523
diff changeset
   117
        HttpResponse<String> main = client.sendAsync(
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   118
                HttpRequest.newBuilder(uri).build(),
56019
dfuchs
parents: 48523
diff changeset
   119
                BodyHandler.asString(),
dfuchs
parents: 48523
diff changeset
   120
                pph)
dfuchs
parents: 48523
diff changeset
   121
                .join();
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   122
56019
dfuchs
parents: 48523
diff changeset
   123
        promises.entrySet().stream().forEach(e -> System.out.println(e.getKey() + ":" + e.getValue().join().body()));
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   124
56019
dfuchs
parents: 48523
diff changeset
   125
        promises.putIfAbsent(main.request(), CompletableFuture.completedFuture(main));
dfuchs
parents: 48523
diff changeset
   126
        promises.entrySet().stream().forEach(entry -> {
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   127
            HttpRequest request = entry.getKey();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   128
            HttpResponse<String> response = entry.getValue().join();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   129
            assertEquals(response.statusCode(), 200);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   130
            if (PUSH_PROMISES.containsKey(request.uri().getPath())) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   131
                assertEquals(response.body(), PUSH_PROMISES.get(request.uri().getPath()));
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   132
            } else {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   133
                assertEquals(response.body(), MAIN_RESPONSE_BODY);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   134
            }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   135
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   136
        } );
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   137
    }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   138
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   139
    // --- server push handler ---
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   140
    static class ServerPushHandler implements Http2Handler {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   141
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   142
        private final String mainResponseBody;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   143
        private final Map<String,String> promises;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   144
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   145
        public ServerPushHandler(String mainResponseBody,
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   146
                                 Map<String,String> promises)
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   147
            throws Exception
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   148
        {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   149
            Objects.requireNonNull(promises);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   150
            this.mainResponseBody = mainResponseBody;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   151
            this.promises = promises;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   152
        }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   153
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   154
        public void handle(Http2TestExchange exchange) throws IOException {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   155
            System.err.println("Server: handle " + exchange);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   156
            try (InputStream is = exchange.getRequestBody()) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   157
                is.readAllBytes();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   158
            }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   159
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   160
            if (exchange.serverPushAllowed()) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   161
                pushPromises(exchange);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   162
            }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   163
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   164
            // response data for the main response
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   165
            try (OutputStream os = exchange.getResponseBody()) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   166
                byte[] bytes = mainResponseBody.getBytes(UTF_8);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   167
                exchange.sendResponseHeaders(200, bytes.length);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   168
                os.write(bytes);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   169
            }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   170
        }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   171
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   172
        private void pushPromises(Http2TestExchange exchange) throws IOException {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   173
            URI requestURI = exchange.getRequestURI();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   174
            for (Map.Entry<String,String> promise : promises.entrySet()) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   175
                URI uri = requestURI.resolve(promise.getKey());
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   176
                InputStream is = new ByteArrayInputStream(promise.getValue().getBytes(UTF_8));
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   177
                HttpHeadersImpl headers = new HttpHeadersImpl();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   178
                exchange.serverPush(uri, headers, is);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   179
            }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   180
            System.err.println("Server: All pushes sent");
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   181
        }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   182
    }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   183
}
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   184