test/jdk/java/net/httpclient/http2/ImplicitPushCancel.java
author phh
Sat, 30 Nov 2019 14:33:05 -0800
changeset 59330 5b96c12f909d
parent 52121 934969c63223
permissions -rw-r--r--
8234541: C1 emits an empty message when it inlines successfully Summary: Use "inline" as the message when successfull Reviewed-by: thartmann, mdoerr Contributed-by: navy.xliu@gmail.com
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
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    26
 * @library /test/lib server
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 50681
diff changeset
    27
 * @build jdk.test.lib.net.SimpleSSLContext
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    28
 * @modules java.base/sun.net.www.http
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    29
 *          java.net.http/jdk.internal.net.http.common
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    30
 *          java.net.http/jdk.internal.net.http.frame
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    31
 *          java.net.http/jdk.internal.net.http.hpack
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    32
 * @run testng/othervm
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    33
 *      -Djdk.internal.httpclient.debug=true
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    34
 *      -Djdk.httpclient.HttpClient.log=errors,requests,responses,trace
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    35
 *      ImplicitPushCancel
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    36
 */
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    37
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    38
import java.io.ByteArrayInputStream;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    39
import java.io.IOException;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    40
import java.io.InputStream;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    41
import java.io.OutputStream;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    42
import java.net.URI;
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    43
import java.net.http.HttpClient;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    44
import java.net.http.HttpHeaders;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    45
import java.net.http.HttpRequest;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    46
import java.net.http.HttpResponse;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    47
import java.net.http.HttpResponse.BodyHandlers;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    48
import java.net.http.HttpResponse.PushPromiseHandler;
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
    49
import java.util.Collections;
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    50
import java.util.Map;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    51
import java.util.Objects;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    52
import java.util.concurrent.CompletableFuture;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    53
import java.util.concurrent.ConcurrentHashMap;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    54
import java.util.concurrent.ConcurrentMap;
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    55
import org.testng.annotations.AfterTest;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    56
import org.testng.annotations.BeforeTest;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    57
import org.testng.annotations.Test;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    58
import static java.nio.charset.StandardCharsets.UTF_8;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    59
import static org.testng.Assert.assertEquals;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    60
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    61
public class ImplicitPushCancel {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    62
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    63
    static Map<String,String> PUSH_PROMISES = Map.of(
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    64
            "/x/y/z/1", "the first push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    65
            "/x/y/z/2", "the second push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    66
            "/x/y/z/3", "the third push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    67
            "/x/y/z/4", "the fourth push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    68
            "/x/y/z/5", "the fifth push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    69
            "/x/y/z/6", "the sixth push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    70
            "/x/y/z/7", "the seventh push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    71
            "/x/y/z/8", "the eight push promise body",
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    72
            "/x/y/z/9", "the ninth push promise body"
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    73
    );
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    74
    static final String MAIN_RESPONSE_BODY = "the main response body";
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
    Http2TestServer server;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    77
    URI uri;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    78
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    79
    @BeforeTest
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    80
    public void setup() throws Exception {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    81
        server = new Http2TestServer(false, 0);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    82
        Http2Handler handler = new ServerPushHandler(MAIN_RESPONSE_BODY,
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    83
                                                     PUSH_PROMISES);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    84
        server.addHandler(handler, "/");
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    85
        server.start();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    86
        int port = server.getAddress().getPort();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    87
        System.err.println("Server listening on port " + port);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
    88
        uri = new URI("http://localhost:" + port + "/foo/a/b/c");
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    89
    }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    90
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    91
    @AfterTest
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    92
    public void teardown() {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    93
        server.stop();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    94
    }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    95
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    96
    static final <T> HttpResponse<T> assert200ResponseCode(HttpResponse<T> response) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    97
        assertEquals(response.statusCode(), 200);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    98
        return response;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
    99
    }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   100
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   101
    /*
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   102
     * With a handler not capable of accepting push promises, then all push
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   103
     * promises should be rejected / cancelled, without interfering with the
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   104
     * main response.
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   105
     */
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   106
    @Test
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   107
    public void test() throws Exception {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   108
        HttpClient client = HttpClient.newHttpClient();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   109
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   110
        client.sendAsync(HttpRequest.newBuilder(uri).build(), BodyHandlers.ofString())
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   111
                .thenApply(ImplicitPushCancel::assert200ResponseCode)
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   112
                .thenApply(HttpResponse::body)
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   113
                .thenAccept(body -> body.equals(MAIN_RESPONSE_BODY))
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   114
                .join();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   115
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   116
        ConcurrentMap<HttpRequest, CompletableFuture<HttpResponse<String>>> promises
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   117
                = new ConcurrentHashMap<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   118
        PushPromiseHandler<String> pph = PushPromiseHandler
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   119
                .of((r) -> BodyHandlers.ofString(), promises);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   120
        HttpResponse<String> main = client.sendAsync(
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   121
                HttpRequest.newBuilder(uri).build(),
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   122
                BodyHandlers.ofString(),
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   123
                pph)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   124
                .join();
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   125
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   126
        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
   127
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   128
        promises.putIfAbsent(main.request(), CompletableFuture.completedFuture(main));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48523
diff changeset
   129
        promises.entrySet().stream().forEach(entry -> {
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   130
            HttpRequest request = entry.getKey();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   131
            HttpResponse<String> response = entry.getValue().join();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   132
            assertEquals(response.statusCode(), 200);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   133
            if (PUSH_PROMISES.containsKey(request.uri().getPath())) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   134
                assertEquals(response.body(), PUSH_PROMISES.get(request.uri().getPath()));
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   135
            } else {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   136
                assertEquals(response.body(), MAIN_RESPONSE_BODY);
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
        } );
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   140
    }
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
    // --- server push handler ---
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   143
    static class ServerPushHandler implements Http2Handler {
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
        private final String mainResponseBody;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   146
        private final Map<String,String> promises;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   147
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   148
        public ServerPushHandler(String mainResponseBody,
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   149
                                 Map<String,String> promises)
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   150
            throws Exception
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   151
        {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   152
            Objects.requireNonNull(promises);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   153
            this.mainResponseBody = mainResponseBody;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   154
            this.promises = promises;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   155
        }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   156
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   157
        public void handle(Http2TestExchange exchange) throws IOException {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   158
            System.err.println("Server: handle " + exchange);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   159
            try (InputStream is = exchange.getRequestBody()) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   160
                is.readAllBytes();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   161
            }
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
            if (exchange.serverPushAllowed()) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   164
                pushPromises(exchange);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   165
            }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   166
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   167
            // response data for the main response
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   168
            try (OutputStream os = exchange.getResponseBody()) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   169
                byte[] bytes = mainResponseBody.getBytes(UTF_8);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   170
                exchange.sendResponseHeaders(200, bytes.length);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   171
                os.write(bytes);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   172
            }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   173
        }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   174
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   175
        private void pushPromises(Http2TestExchange exchange) throws IOException {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   176
            URI requestURI = exchange.getRequestURI();
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   177
            for (Map.Entry<String,String> promise : promises.entrySet()) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   178
                URI uri = requestURI.resolve(promise.getKey());
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   179
                InputStream is = new ByteArrayInputStream(promise.getValue().getBytes(UTF_8));
50681
4254bed3c09d 8204679: HTTP Client refresh
chegar
parents: 49765
diff changeset
   180
                HttpHeaders headers = HttpHeaders.of(Collections.emptyMap(), (x, y) -> true);
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   181
                exchange.serverPush(uri, headers, is);
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
            System.err.println("Server: All pushes sent");
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   184
        }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   185
    }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   186
}
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents:
diff changeset
   187