test/jdk/java/net/httpclient/http2/server/Http2TestServerConnection.java
author michaelm
Fri, 24 Nov 2017 17:34:57 +0000
branchhttp-client-branch
changeset 55868 5899aa5e1837
parent 55852 32f6aefec11e
child 55871 45c88bf0592b
permissions -rw-r--r--
http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
     2
 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
import java.io.BufferedInputStream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
import java.io.BufferedOutputStream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    26
import java.io.Closeable;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
import java.io.IOException;
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
    28
import java.io.UncheckedIOException;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.io.InputStream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    30
import java.io.OutputStream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    31
import java.net.Socket;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    32
import java.net.URI;
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
    33
import java.net.InetAddress;
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
    34
import javax.net.ssl.*;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    35
import java.net.URISyntaxException;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
import java.nio.ByteBuffer;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    37
import java.nio.charset.StandardCharsets;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    38
import java.util.*;
55868
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
    39
import java.util.concurrent.CompletableFuture;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
import java.util.concurrent.ExecutorService;
55868
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
    41
import java.util.concurrent.atomic.AtomicLong;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    42
import java.util.function.Consumer;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    43
import jdk.incubator.http.internal.common.HttpHeadersImpl;
55868
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
    44
import jdk.incubator.http.internal.frame.*;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    45
import jdk.incubator.http.internal.hpack.Decoder;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    46
import jdk.incubator.http.internal.hpack.DecodingCallback;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    47
import jdk.incubator.http.internal.hpack.Encoder;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
    48
import sun.net.www.http.ChunkedInputStream;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
    49
import sun.net.www.http.HttpClient;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    50
import static jdk.incubator.http.internal.frame.SettingsFrame.HEADER_TABLE_SIZE;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    52
/**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    53
 * Represents one HTTP2 connection, either plaintext upgraded from HTTP/1.1
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    54
 * or HTTPS opened using "h2" ALPN.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    55
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    56
public class Http2TestServerConnection {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    57
    final Http2TestServer server;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    58
    @SuppressWarnings({"rawtypes","unchecked"})
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    59
    final Map<Integer, Queue> streams; // input q per stream
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
    60
    final Map<Integer, BodyOutputStream> outStreams; // output q per stream
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    61
    final HashSet<Integer> pushStreams;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    62
    final Queue<Http2Frame> outputQ;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    63
    volatile int nextstream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    64
    final Socket socket;
55817
2aec8081d2ad http-client-branch: Add a test for CONTINUATION frame handling
chegar
parents: 55806
diff changeset
    65
    final Http2TestExchangeSupplier exchangeSupplier;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    66
    final InputStream is;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    67
    final OutputStream os;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    68
    volatile Encoder hpackOut;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    69
    volatile Decoder hpackIn;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    70
    volatile SettingsFrame clientSettings;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    71
    final SettingsFrame serverSettings;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    72
    final ExecutorService exec;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    73
    final boolean secure;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    74
    volatile boolean stopping;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    75
    volatile int nextPushStreamId = 2;
55868
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
    76
    volatile byte[] pingData;
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
    77
    volatile CompletableFuture<Long> pingResponseHandler;
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
    78
    final AtomicLong pingStamp; // milliseconds at time PING was sent
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    79
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    80
    final static ByteBuffer EMPTY_BUFFER = ByteBuffer.allocate(0);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    81
    final static byte[] EMPTY_BARRAY = new byte[0];
55868
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
    82
    final Random random;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    83
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    84
    final static byte[] clientPreface = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n".getBytes();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    85
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
    86
    static class Sentinel extends Http2Frame {
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
    87
        Sentinel() { super(-1,-1);}
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
    88
    }
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
    89
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
    90
    static Sentinel sentinel;
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
    91
55817
2aec8081d2ad http-client-branch: Add a test for CONTINUATION frame handling
chegar
parents: 55806
diff changeset
    92
    Http2TestServerConnection(Http2TestServer server,
2aec8081d2ad http-client-branch: Add a test for CONTINUATION frame handling
chegar
parents: 55806
diff changeset
    93
                              Socket socket,
2aec8081d2ad http-client-branch: Add a test for CONTINUATION frame handling
chegar
parents: 55806
diff changeset
    94
                              Http2TestExchangeSupplier exchangeSupplier)
2aec8081d2ad http-client-branch: Add a test for CONTINUATION frame handling
chegar
parents: 55806
diff changeset
    95
        throws IOException
2aec8081d2ad http-client-branch: Add a test for CONTINUATION frame handling
chegar
parents: 55806
diff changeset
    96
    {
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
    97
        if (socket instanceof SSLSocket) {
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
    98
            handshake(server.serverName(), (SSLSocket)socket);
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
    99
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   100
        System.err.println("TestServer: New connection from " + socket);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   101
        this.server = server;
55817
2aec8081d2ad http-client-branch: Add a test for CONTINUATION frame handling
chegar
parents: 55806
diff changeset
   102
        this.exchangeSupplier = exchangeSupplier;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   103
        this.streams = Collections.synchronizedMap(new HashMap<>());
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   104
        this.outStreams = Collections.synchronizedMap(new HashMap<>());
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   105
        this.outputQ = new Queue<>(sentinel);
55868
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   106
        this.random = new Random();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   107
        this.socket = socket;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   108
        this.socket.setTcpNoDelay(true);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   109
        this.serverSettings = SettingsFrame.getDefaultSettings();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   110
        this.exec = server.exec;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   111
        this.secure = server.secure;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
        this.pushStreams = new HashSet<>();
55868
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   113
        this.pingStamp = new AtomicLong();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   114
        is = new BufferedInputStream(socket.getInputStream());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   115
        os = new BufferedOutputStream(socket.getOutputStream());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   116
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   117
55868
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   118
    /**
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   119
     * Sends a PING frame on this connection, and invokes the given
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   120
     * handler when the PING ack is received. The handler is given
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   121
     * an integer, whose value if >= 0, is the number of milliseconds
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   122
     * between PING and ACK, If < 0 signifies an error occured.
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   123
     *
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   124
     * Only one PING is allowed to be outstanding at any time
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   125
     */
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   126
    void sendPing(CompletableFuture<Long> cf) throws IOException {
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   127
        if (pingData != null) {
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   128
            throw new IllegalStateException("PING already outstanding");
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   129
        }
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   130
        pingData = new byte[8];
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   131
        random.nextBytes(pingData);
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   132
        this.pingResponseHandler = cf;
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   133
        pingStamp.set(System.currentTimeMillis());
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   134
        PingFrame ping = new PingFrame(0, pingData);
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   135
        outputQ.put(ping);
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   136
    }
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   137
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   138
    /**
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   139
     * Handles incoming Ping, which could be an ack
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   140
     * or a client originated Ping
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   141
     */
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   142
    void handlePing(PingFrame ping) throws IOException {
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   143
        if (ping.streamid() != 0) {
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   144
            System.err.println("Invalid ping received");
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   145
            close();
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   146
            return;
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   147
        }
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   148
        if (ping.getFlag(PingFrame.ACK)) {
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   149
            // did we send a Ping?
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   150
            if (pingData == null) {
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   151
                System.err.println("Invalid ping received");
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   152
                close();
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   153
                return;
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   154
            } else if (!Arrays.equals(pingData, ping.getData())) {
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   155
                pingResponseHandler.completeExceptionally(new RuntimeException("Wrong ping data in ACK"));
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   156
            } else {
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   157
                pingResponseHandler.complete(System.currentTimeMillis() - pingStamp.getAndSet(0));
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   158
            }
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   159
            pingResponseHandler = null;
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   160
            pingData = null;
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   161
        } else {
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   162
            // client originated PING. Just send it back with ACK set
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   163
            ping.setFlag(PingFrame.ACK);
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   164
            outputQ.put(ping);
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   165
        }
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   166
    }
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   167
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   168
    private static boolean compareIPAddrs(InetAddress addr1, String host) {
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   169
        try {
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   170
            InetAddress addr2 = InetAddress.getByName(host);
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   171
            return addr1.equals(addr2);
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   172
        } catch (IOException e) {
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   173
            throw new UncheckedIOException(e);
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   174
        }
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   175
    }
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   176
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   177
    private static void handshake(String name, SSLSocket sock) throws IOException {
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   178
        if (name == null) {
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   179
            // no name set. No need to check
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   180
            return;
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   181
        } else if (name.equals("127.0.0.1")) {
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   182
            name = "localhost";
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   183
        }
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   184
        final String fname = name;
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   185
        final InetAddress addr1 = InetAddress.getByName(name);
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   186
        SSLParameters params = sock.getSSLParameters();
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   187
        SNIMatcher matcher = new SNIMatcher(StandardConstants.SNI_HOST_NAME) {
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   188
            public boolean matches (SNIServerName n) {
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   189
                String host = ((SNIHostName)n).getAsciiName();
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   190
                if (host.equals("127.0.0.1"))
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   191
                    host = "localhost";
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   192
                boolean cmp = host.equalsIgnoreCase(fname);
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   193
                if (cmp)
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   194
                    return true;
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   195
                return compareIPAddrs(addr1, host);
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   196
            }
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   197
        };
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   198
        List<SNIMatcher> list = List.of(matcher);
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   199
        params.setSNIMatchers(list);
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   200
        sock.setSSLParameters(params);
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   201
        sock.getSession(); // blocks until handshake done
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   202
    }
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43788
diff changeset
   203
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   204
    void close() {
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   205
        stopping = true;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   206
        streams.forEach((i, q) -> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   207
            q.close();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   208
        });
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   209
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   210
            socket.close();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   211
            // TODO: put a reset on each stream
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   212
        } catch (IOException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   213
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   214
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   215
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   216
    private void readPreface() throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   217
        int len = clientPreface.length;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   218
        byte[] bytes = new byte[len];
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   219
        is.readNBytes(bytes, 0, len);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   220
        if (Arrays.compare(clientPreface, bytes) != 0) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   221
            throw new IOException("Invalid preface: " + new String(bytes, 0, len));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   222
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   223
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   224
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   225
    String doUpgrade() throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   226
        String upgrade = readHttp1Request();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   227
        String h2c = getHeader(upgrade, "Upgrade");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   228
        if (h2c == null || !h2c.equals("h2c")) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   229
            System.err.println("Server:HEADERS: " + upgrade);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   230
            throw new IOException("Bad upgrade 1 " + h2c);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   231
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   232
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   233
        sendHttp1Response(101, "Switching Protocols", "Connection", "Upgrade",
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   234
                "Upgrade", "h2c");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   235
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   236
        sendSettingsFrame();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   237
        readPreface();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   238
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   239
        String clientSettingsString = getHeader(upgrade, "HTTP2-Settings");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   240
        clientSettings = getSettingsFromString(clientSettingsString);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   241
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   242
        return upgrade;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   243
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   244
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   245
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   246
     * Decodes the given, Client, settings payload provided in base64 HTTP1
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   247
     * header value.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   248
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   249
    private SettingsFrame getSettingsFromString(String s) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   250
        Base64.Decoder decoder = Base64.getUrlDecoder();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   251
        byte[] payload = decoder.decode(s);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   252
        ByteBuffer bb1 = ByteBuffer.wrap(payload);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   253
        // simulate header of Settings Frame
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   254
        ByteBuffer bb0 = ByteBuffer.wrap(
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   255
                new byte[] {0, 0, (byte)payload.length, 4, 0, 0, 0, 0, 0});
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   256
        List<Http2Frame> frames = new ArrayList<>();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   257
        FramesDecoder reader = new FramesDecoder(frames::add);
55806
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   258
        reader.decode(bb0);
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   259
        reader.decode(bb1);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   260
        if (frames.size()!=1)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   261
            throw new IOException("Expected 1 frame got "+frames.size()) ;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   262
        Http2Frame frame = frames.get(0);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   263
        if (!(frame instanceof SettingsFrame))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   264
            throw new IOException("Expected SettingsFrame");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   265
        return (SettingsFrame)frame;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   266
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   267
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   268
    void run() throws Exception {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   269
        String upgrade = null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   270
        if (!secure) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   271
            upgrade = doUpgrade();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   272
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   273
            readPreface();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   274
            sendSettingsFrame(true);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   275
            clientSettings = (SettingsFrame) readFrame();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   276
            if (clientSettings.getFlag(SettingsFrame.ACK)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   277
                // we received the ack to our frame first
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   278
                clientSettings = (SettingsFrame) readFrame();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   279
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   280
            nextstream = 1;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   281
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   282
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   283
        System.out.println("ServerSettings: " + serverSettings);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   284
        System.out.println("ClientSettings: " + clientSettings);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   285
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   286
        hpackOut = new Encoder(serverSettings.getParameter(HEADER_TABLE_SIZE));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   287
        hpackIn = new Decoder(clientSettings.getParameter(HEADER_TABLE_SIZE));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   288
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   289
        if (!secure) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   290
            createPrimordialStream(upgrade);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   291
            nextstream = 3;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   292
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   293
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   294
        exec.submit(this::readLoop);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   295
        exec.submit(this::writeLoop);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   296
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   297
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   298
    private void writeFrame(Http2Frame frame) throws IOException {
55806
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   299
        List<ByteBuffer> bufs = new FramesEncoder().encodeFrame(frame);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   300
        //System.err.println("TestServer: Writing frame " + frame.toString());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   301
        int c = 0;
55806
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   302
        for (ByteBuffer buf : bufs) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   303
            byte[] ba = buf.array();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   304
            int start = buf.arrayOffset() + buf.position();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   305
            c += buf.remaining();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   306
            os.write(ba, start, buf.remaining());
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   307
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   308
//            System.out.println("writing byte at a time");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   309
//            while (buf.hasRemaining()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   310
//                byte b = buf.get();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   311
//                os.write(b);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   312
//                os.flush();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   313
//                try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   314
//                    Thread.sleep(1);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   315
//                } catch(InterruptedException e) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   316
//                    UncheckedIOException uie = new UncheckedIOException(new IOException(""));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   317
//                    uie.addSuppressed(e);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   318
//                    throw uie;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   319
//                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   320
//            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   321
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   322
        os.flush();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   323
        //System.err.printf("TestServer: wrote %d bytes\n", c);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   324
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   325
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   326
    private void handleCommonFrame(Http2Frame f) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   327
        if (f instanceof SettingsFrame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   328
            SettingsFrame sf = (SettingsFrame) f;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   329
            if (sf.getFlag(SettingsFrame.ACK)) // ignore
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   330
            {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   331
                return;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   332
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   333
            // otherwise acknowledge it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   334
            clientSettings = sf;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   335
            SettingsFrame frame = new SettingsFrame();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   336
            frame.setFlag(SettingsFrame.ACK);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   337
            frame.streamid(0);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   338
            outputQ.put(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   339
            return;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   340
        } else if (f instanceof GoAwayFrame) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   341
            System.err.println("Closing: "+ f.toString());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   342
            close();
55868
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   343
        } else if (f instanceof PingFrame) {
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   344
            handlePing((PingFrame)f);
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   345
        } else
5899aa5e1837 http-client-branch: added ping support to test http2 server and some test pings from server to client in the BasicTest
michaelm
parents: 55852
diff changeset
   346
            throw new UnsupportedOperationException("Not supported yet: " + f.toString());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   347
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   348
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   349
    void sendWindowUpdates(int len, int streamid) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   350
        if (len == 0)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   351
            return;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   352
        WindowUpdateFrame wup = new WindowUpdateFrame(streamid, len);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   353
        outputQ.put(wup);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   354
        wup = new WindowUpdateFrame(0 , len);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   355
        outputQ.put(wup);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   356
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   357
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   358
    HttpHeadersImpl decodeHeaders(List<HeaderFrame> frames) throws IOException {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   359
        HttpHeadersImpl headers = new HttpHeadersImpl();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   360
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   361
        DecodingCallback cb = (name, value) -> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   362
            headers.addHeader(name.toString(), value.toString());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   363
        };
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   364
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   365
        for (HeaderFrame frame : frames) {
55806
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   366
            List<ByteBuffer> buffers = frame.getHeaderBlock();
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   367
            for (ByteBuffer buffer : buffers) {
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   368
                hpackIn.decode(buffer, false, cb);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   369
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   370
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   371
        hpackIn.decode(EMPTY_BUFFER, true, cb);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   372
        return headers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   373
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   374
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   375
    String getRequestLine(String request) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   376
        int eol = request.indexOf(CRLF);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   377
        return request.substring(0, eol);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   378
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   379
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   380
    String getHeaders(String request) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   381
        int start = request.indexOf(CRLF);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   382
        int end = request.indexOf(CRLFCRLF);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   383
        if (start == -1 || end == -1) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   384
            throw new RuntimeException("Malformed request");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   385
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   386
        return request.substring(start,end);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   387
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   388
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   389
    void addHeaders(String headers, HttpHeadersImpl hdrs) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   390
        String[] hh = headers.split(CRLF);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   391
        for (String header : hh) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   392
            int colon = header.indexOf(':');
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   393
            if (colon == -1)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   394
                continue;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   395
            String name = header.substring(0, colon);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   396
            String value = header.substring(colon+1);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   397
            while (value.startsWith(" "))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   398
                value = value.substring(1);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   399
            hdrs.addHeader(name, value);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   400
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   401
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   402
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   403
    // First stream (1) comes from a plaintext HTTP/1.1 request
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   404
    @SuppressWarnings({"rawtypes","unchecked"})
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   405
    void createPrimordialStream(String request) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   406
        HttpHeadersImpl headers = new HttpHeadersImpl();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   407
        String requestLine = getRequestLine(request);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   408
        String[] tokens = requestLine.split(" ");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   409
        if (!tokens[2].equals("HTTP/1.1")) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   410
            throw new IOException("bad request line");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   411
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   412
        URI uri = null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   413
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   414
            uri = new URI(tokens[1]);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   415
        } catch (URISyntaxException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   416
            throw new IOException(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   417
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   418
        String host = getHeader(request, "Host");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   419
        if (host == null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   420
            throw new IOException("missing Host");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   421
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   422
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   423
        headers.setHeader(":method", tokens[0]);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   424
        headers.setHeader(":scheme", "http"); // always in this case
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   425
        headers.setHeader(":authority", host);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   426
        headers.setHeader(":path", uri.getPath());
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   427
        Queue q = new Queue(sentinel);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   428
        String body = getRequestBody(request);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   429
        addHeaders(getHeaders(request), headers);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   430
        headers.setHeader("Content-length", Integer.toString(body.length()));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   431
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   432
        addRequestBodyToQueue(body, q);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   433
        streams.put(1, q);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   434
        exec.submit(() -> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   435
            handleRequest(headers, q, 1, true /*complete request has been read*/);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   436
        });
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   437
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   438
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   439
    // all other streams created here
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   440
    @SuppressWarnings({"rawtypes","unchecked"})
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   441
    void createStream(HeaderFrame frame) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   442
        List<HeaderFrame> frames = new LinkedList<>();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   443
        frames.add(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   444
        int streamid = frame.streamid();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   445
        if (streamid != nextstream) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   446
            throw new IOException("unexpected stream id");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   447
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   448
        nextstream += 2;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   449
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   450
        boolean endStream = false;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   451
        if (frame.getFlag(HeaderFrame.END_STREAM)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   452
            endStream = true;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   453
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   454
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   455
        while (!frame.getFlag(HeaderFrame.END_HEADERS)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   456
            Http2Frame f = readFrame();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   457
            if (!(f instanceof HeaderFrame)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   458
                handleCommonFrame(f); // should only be error frames
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   459
            } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   460
                frame = (HeaderFrame) f;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   461
                if (frame.getFlag(HeaderFrame.END_STREAM)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   462
                    endStream = true;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   463
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   464
                frames.add(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   465
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   466
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   467
        boolean endStreamReceived = endStream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   468
        HttpHeadersImpl headers = decodeHeaders(frames);
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   469
        Queue q = new Queue(sentinel);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   470
        streams.put(streamid, q);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   471
        exec.submit(() -> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   472
            handleRequest(headers, q, streamid, endStreamReceived);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   473
        });
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   474
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   475
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   476
    // runs in own thread. Handles request from start to finish. Incoming frames
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   477
    // for this stream/request delivered on Q
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   478
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   479
    @SuppressWarnings({"rawtypes","unchecked"})
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   480
    void handleRequest(HttpHeadersImpl headers,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   481
                       Queue queue,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   482
                       int streamid,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   483
                       boolean endStreamReceived)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   484
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   485
        String method = headers.firstValue(":method").orElse("");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   486
        //System.out.println("method = " + method);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   487
        String path = headers.firstValue(":path").orElse("");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   488
        //System.out.println("path = " + path);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   489
        String scheme = headers.firstValue(":scheme").orElse("");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   490
        //System.out.println("scheme = " + scheme);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   491
        String authority = headers.firstValue(":authority").orElse("");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   492
        //System.out.println("authority = " + authority);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   493
        System.err.printf("TestServer: %s %s\n", method, path);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   494
        HttpHeadersImpl rspheaders = new HttpHeadersImpl();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   495
        int winsize = clientSettings.getParameter(
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   496
                SettingsFrame.INITIAL_WINDOW_SIZE);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   497
        //System.err.println ("Stream window size = " + winsize);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   498
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   499
        final InputStream bis;
43788
22a618ec8268 8174833: java/net/httpclient/http2/BasicTest.java always fails but always report success
dfuchs
parents: 42460
diff changeset
   500
        if (endStreamReceived && queue.size() == 0) {
22a618ec8268 8174833: java/net/httpclient/http2/BasicTest.java always fails but always report success
dfuchs
parents: 42460
diff changeset
   501
            System.err.println("Server: got END_STREAM for stream " + streamid);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   502
            bis = NullInputStream.INSTANCE;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   503
        } else {
43788
22a618ec8268 8174833: java/net/httpclient/http2/BasicTest.java always fails but always report success
dfuchs
parents: 42460
diff changeset
   504
            System.err.println("Server: creating input stream for stream " + streamid);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   505
            bis = new BodyInputStream(queue, streamid, this);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   506
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   507
        try (bis;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   508
             BodyOutputStream bos = new BodyOutputStream(streamid, winsize, this))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   509
        {
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   510
            outStreams.put(streamid, bos);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   511
            String us = scheme + "://" + authority + path;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   512
            URI uri = new URI(us);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   513
            boolean pushAllowed = clientSettings.getParameter(SettingsFrame.ENABLE_PUSH) == 1;
55817
2aec8081d2ad http-client-branch: Add a test for CONTINUATION frame handling
chegar
parents: 55806
diff changeset
   514
            Http2TestExchange exchange = exchangeSupplier.get(streamid, method,
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   515
                    headers, rspheaders, uri, bis, getSSLSession(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   516
                    bos, this, pushAllowed);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   517
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   518
            // give to user
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   519
            Http2Handler handler = server.getHandlerFor(uri.getPath());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   520
            handler.handle(exchange);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   521
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   522
            // everything happens in the exchange from here. Hopefully will
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   523
            // return though.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   524
        } catch (Throwable e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   525
            System.err.println("TestServer: handleRequest exception: " + e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   526
            e.printStackTrace();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   527
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   528
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   529
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   530
    private SSLSession getSSLSession() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   531
        if (! (socket instanceof SSLSocket))
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   532
            return null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   533
        SSLSocket ssl = (SSLSocket)socket;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   534
        return ssl.getSession();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   535
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   536
    // Runs in own thread
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   537
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   538
    @SuppressWarnings({"rawtypes","unchecked"})
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   539
    void readLoop() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   540
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   541
            while (!stopping) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   542
                Http2Frame frame = readFrame();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   543
                //System.err.printf("TestServer: received frame %s\n", frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   544
                int stream = frame.streamid();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   545
                if (stream == 0) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   546
                    if (frame.type() == WindowUpdateFrame.TYPE) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   547
                        WindowUpdateFrame wup = (WindowUpdateFrame) frame;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   548
                        updateConnectionWindow(wup.getUpdate());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   549
                    } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   550
                        // other common frame types
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   551
                        handleCommonFrame(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   552
                    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   553
                } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   554
                    Queue q = streams.get(stream);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   555
                    if (frame.type() == HeadersFrame.TYPE) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   556
                        if (q != null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   557
                            System.err.println("HEADERS frame for existing stream! Error.");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   558
                            // TODO: close connection
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   559
                            continue;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   560
                        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   561
                            createStream((HeadersFrame) frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   562
                        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   563
                    } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   564
                        if (q == null && !pushStreams.contains(stream)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   565
                            System.err.printf("Non Headers frame received with"+
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   566
                                    " non existing stream (%d) ", frame.streamid());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   567
                            System.err.println(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   568
                            continue;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   569
                        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   570
                        if (frame.type() == WindowUpdateFrame.TYPE) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   571
                            WindowUpdateFrame wup = (WindowUpdateFrame) frame;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   572
                            synchronized (updaters) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   573
                                Consumer<Integer> r = updaters.get(stream);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   574
                                r.accept(wup.getUpdate());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   575
                            }
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   576
                        } else if (frame.type() == ResetFrame.TYPE) {
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   577
                            // do orderly close on input q
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   578
                            // and close the output q immediately
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   579
                            // This should mean depending on what the
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   580
                            // handler is doing: either an EOF on read
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   581
                            // or an IOException if writing the response.
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   582
                            q.orderlyClose();
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   583
                            BodyOutputStream oq = outStreams.get(stream);
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   584
                            if (oq != null)
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   585
                                oq.closeInternal();
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   586
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   587
                        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   588
                            q.put(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   589
                        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   590
                    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   591
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   592
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   593
        } catch (Throwable e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   594
            if (!stopping) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   595
                System.err.println("Http server reader thread shutdown");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   596
                e.printStackTrace();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   597
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   598
            close();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   599
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   600
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   601
55806
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   602
    List<ByteBuffer> encodeHeaders(HttpHeadersImpl headers) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   603
        List<ByteBuffer> buffers = new LinkedList<>();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   604
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   605
        ByteBuffer buf = getBuffer();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   606
        boolean encoded;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   607
        for (Map.Entry<String, List<String>> entry : headers.map().entrySet()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   608
            List<String> values = entry.getValue();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   609
            String key = entry.getKey().toLowerCase();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   610
            for (String value : values) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   611
                do {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   612
                    hpackOut.header(key, value);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   613
                    encoded = hpackOut.encode(buf);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   614
                    if (!encoded) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   615
                        buf.flip();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   616
                        buffers.add(buf);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   617
                        buf = getBuffer();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   618
                    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   619
                } while (!encoded);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   620
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   621
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   622
        buf.flip();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   623
        buffers.add(buf);
55806
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   624
        return buffers;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   625
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   626
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   627
    static void closeIgnore(Closeable c) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   628
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   629
            c.close();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   630
        } catch (IOException e) {}
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   631
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   632
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   633
    // Runs in own thread
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   634
    void writeLoop() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   635
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   636
            while (!stopping) {
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   637
                Http2Frame frame;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   638
                try {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   639
                    frame = outputQ.take();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   640
                } catch(IOException x) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   641
                    if (stopping && x.getCause() instanceof InterruptedException) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   642
                        break;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   643
                    } else throw x;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45713
diff changeset
   644
                }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   645
                if (frame instanceof ResponseHeaders) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   646
                    ResponseHeaders rh = (ResponseHeaders)frame;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   647
                    HeadersFrame hf = new HeadersFrame(rh.streamid(), rh.getFlags(), encodeHeaders(rh.headers));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   648
                    writeFrame(hf);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   649
                } else if (frame instanceof OutgoingPushPromise) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   650
                    handlePush((OutgoingPushPromise)frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   651
                } else
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   652
                    writeFrame(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   653
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   654
            System.err.println("TestServer: Connection writer stopping");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   655
        } catch (Throwable e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   656
            e.printStackTrace();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   657
            /*close();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   658
            if (!stopping) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   659
                e.printStackTrace();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   660
                System.err.println("TestServer: writeLoop exception: " + e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   661
            }*/
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   662
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   663
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   664
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   665
    private void handlePush(OutgoingPushPromise op) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   666
        int promisedStreamid = nextPushStreamId;
55806
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   667
        PushPromiseFrame pp = new PushPromiseFrame(op.parentStream,
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   668
                                                   HeaderFrame.END_HEADERS,
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   669
                                                   promisedStreamid,
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   670
                                                   encodeHeaders(op.headers),
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   671
                                                   0);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   672
        pushStreams.add(promisedStreamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   673
        nextPushStreamId += 2;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   674
        pp.streamid(op.parentStream);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   675
        writeFrame(pp);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   676
        final InputStream ii = op.is;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   677
        final BodyOutputStream oo = new BodyOutputStream(
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   678
                promisedStreamid,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   679
                clientSettings.getParameter(
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   680
                        SettingsFrame.INITIAL_WINDOW_SIZE), this);
55852
32f6aefec11e http-client-branch: HttpRequest/HttpResponse api change: remove link between requests, add links between responses. Fixed some redirection problems
michaelm
parents: 55817
diff changeset
   681
        outStreams.put(promisedStreamid, oo);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   682
        oo.goodToGo();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   683
        exec.submit(() -> {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   684
            try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   685
                ResponseHeaders oh = getPushResponse(promisedStreamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   686
                outputQ.put(oh);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   687
                ii.transferTo(oo);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   688
            } catch (Throwable ex) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   689
                System.err.printf("TestServer: pushing response error: %s\n",
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   690
                        ex.toString());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   691
            } finally {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   692
                closeIgnore(ii);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   693
                closeIgnore(oo);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   694
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   695
        });
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   696
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   697
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   698
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   699
    // returns a minimal response with status 200
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   700
    // that is the response to the push promise just sent
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   701
    private ResponseHeaders getPushResponse(int streamid) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   702
        HttpHeadersImpl h = new HttpHeadersImpl();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   703
        h.addHeader(":status", "200");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   704
        ResponseHeaders oh = new ResponseHeaders(h);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   705
        oh.streamid(streamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   706
        oh.setFlag(HeaderFrame.END_HEADERS);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   707
        return oh;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   708
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   709
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   710
    private ByteBuffer getBuffer() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   711
        return ByteBuffer.allocate(8 * 1024);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   712
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   713
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   714
    private Http2Frame readFrame() throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   715
        byte[] buf = new byte[9];
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   716
        if (is.readNBytes(buf, 0, 9) != 9)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   717
            throw new IOException("readFrame: connection closed");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   718
        int len = 0;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   719
        for (int i = 0; i < 3; i++) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   720
            int n = buf[i] & 0xff;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   721
            //System.err.println("n = " + n);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   722
            len = (len << 8) + n;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   723
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   724
        byte[] rest = new byte[len];
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   725
        int n = is.readNBytes(rest, 0, len);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   726
        if (n != len)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   727
            throw new IOException("Error reading frame");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   728
        List<Http2Frame> frames = new ArrayList<>();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   729
        FramesDecoder reader = new FramesDecoder(frames::add);
55806
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   730
        reader.decode(ByteBuffer.wrap(buf));
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   731
        reader.decode(ByteBuffer.wrap(rest));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   732
        if (frames.size()!=1)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   733
            throw new IOException("Expected 1 frame got "+frames.size()) ;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   734
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   735
        return frames.get(0);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   736
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   737
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   738
    void sendSettingsFrame() throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   739
        sendSettingsFrame(false);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   740
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   741
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   742
    void sendSettingsFrame(boolean now) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   743
        if (now) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   744
            writeFrame(serverSettings);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   745
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   746
            outputQ.put(serverSettings);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   747
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   748
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   749
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   750
    String readUntil(String end) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   751
        int number = end.length();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   752
        int found = 0;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   753
        StringBuilder sb = new StringBuilder();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   754
        while (found < number) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   755
            char expected = end.charAt(found);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   756
            int c = is.read();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   757
            if (c == -1) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   758
                throw new IOException("Connection closed");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   759
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   760
            char c0 = (char) c;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   761
            sb.append(c0);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   762
            if (c0 != expected) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   763
                found = 0;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   764
                continue;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   765
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   766
            found++;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   767
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   768
        return sb.toString();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   769
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   770
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   771
    private int getContentLength(String headers) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   772
        return getIntHeader(headers, "Content-length");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   773
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   774
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   775
    private int getIntHeader(String headers, String name) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   776
        String val = getHeader(headers, name);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   777
        if (val == null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   778
            return -1;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   779
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   780
        return Integer.parseInt(val);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   781
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   782
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   783
    private String getHeader(String headers, String name) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   784
        String headers1 = headers.toLowerCase(); // not efficient
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   785
        name = CRLF + name.toLowerCase();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   786
        int start = headers1.indexOf(name);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   787
        if (start == -1) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   788
            return null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   789
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   790
        start += 2;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   791
        int end = headers1.indexOf(CRLF, start);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   792
        String line = headers.substring(start, end);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   793
        start = line.indexOf(':');
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   794
        if (start == -1) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   795
            return null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   796
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   797
        return line.substring(start + 1).trim();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   798
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   799
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   800
    final static String CRLF = "\r\n";
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   801
    final static String CRLFCRLF = "\r\n\r\n";
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   802
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   803
    String readHttp1Request() throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   804
        String headers = readUntil(CRLF + CRLF);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   805
        int clen = getContentLength(headers);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   806
        byte[] buf;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   807
        if (clen >= 0) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   808
            // HTTP/1.1 fixed length content ( may be 0 ), read it
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   809
            buf = new byte[clen];
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   810
            is.readNBytes(buf, 0, clen);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   811
        } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   812
            //  HTTP/1.1 chunked data, read it
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   813
            buf = readChunkedInputStream(is);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   814
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   815
        String body = new String(buf, StandardCharsets.US_ASCII);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   816
        return headers + body;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   817
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   818
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   819
    // This is a quick hack to get a chunked input stream reader.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   820
    private static byte[] readChunkedInputStream(InputStream is) throws IOException {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   821
        ChunkedInputStream cis = new ChunkedInputStream(is, new HttpClient() {}, null);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   822
        return cis.readAllBytes();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   823
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   824
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   825
    void sendHttp1Response(int code, String msg, String... headers) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   826
        StringBuilder sb = new StringBuilder();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   827
        sb.append("HTTP/1.1 ")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   828
                .append(code)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   829
                .append(' ')
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   830
                .append(msg)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   831
                .append(CRLF);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   832
        int numheaders = headers.length;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   833
        for (int i = 0; i < numheaders; i += 2) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   834
            sb.append(headers[i])
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   835
                    .append(": ")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   836
                    .append(headers[i + 1])
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   837
                    .append(CRLF);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   838
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   839
        sb.append(CRLF);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   840
        String s = sb.toString();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   841
        os.write(s.getBytes("US-ASCII"));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   842
        os.flush();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   843
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   844
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   845
    private void unexpectedFrame(Http2Frame frame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   846
        System.err.println("OOPS. Unexpected");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   847
        assert false;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   848
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   849
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   850
    final static ByteBuffer[] bbarray = new ByteBuffer[0];
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   851
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   852
    // wrapper around a BlockingQueue that throws an exception when it's closed
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   853
    // Each stream has one of these
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   854
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   855
    String getRequestBody(String request) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   856
        int bodystart = request.indexOf(CRLF+CRLF);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   857
        String body;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   858
        if (bodystart == -1)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   859
            body = "";
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   860
        else
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   861
            body = request.substring(bodystart+4);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   862
        return body;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   863
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   864
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   865
    @SuppressWarnings({"rawtypes","unchecked"})
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   866
    void addRequestBodyToQueue(String body, Queue q) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   867
        ByteBuffer buf = ByteBuffer.wrap(body.getBytes(StandardCharsets.US_ASCII));
55806
e8bc8370f528 http-client-impl: drop usage of ByteBufferReference temporarily. Using it properly requires changing FlowTube to work with List<BBR> or BBR[] instead of List<BB>. May be added back later if there is a performance gain.
dfuchs
parents: 55799
diff changeset
   868
        DataFrame df = new DataFrame(1, DataFrame.END_STREAM, buf);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   869
        // only used for primordial stream
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   870
        q.put(df);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   871
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   872
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   873
    // window updates done in main reader thread because they may
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   874
    // be used to unblock BodyOutputStreams waiting for WUPs
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   875
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   876
    HashMap<Integer,Consumer<Integer>> updaters = new HashMap<>();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   877
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   878
    void registerStreamWindowUpdater(int streamid, Consumer<Integer> r) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   879
        synchronized(updaters) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   880
            updaters.put(streamid, r);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   881
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   882
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   883
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   884
    int sendWindow = 64 * 1024 - 1; // connection level send window
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   885
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   886
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   887
     * BodyOutputStreams call this to get the connection window first.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   888
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   889
     * @param amount
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   890
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   891
    synchronized void obtainConnectionWindow(int amount) throws InterruptedException {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   892
        while (amount > 0) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   893
            int n = Math.min(amount, sendWindow);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   894
            amount -= n;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   895
            sendWindow -= n;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   896
            if (amount > 0)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   897
                wait();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   898
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   899
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   900
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   901
    synchronized void updateConnectionWindow(int amount) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   902
        sendWindow += amount;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   903
        notifyAll();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   904
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   905
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   906
    // simplified output headers class. really just a type safe container
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   907
    // for the hashmap.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   908
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   909
    static class ResponseHeaders extends Http2Frame {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   910
        HttpHeadersImpl headers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   911
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   912
        ResponseHeaders(HttpHeadersImpl headers) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   913
            super(0, 0);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   914
            this.headers = headers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   915
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   916
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   917
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   918
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   919
    static class NullInputStream extends InputStream {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   920
        static final NullInputStream INSTANCE = new NullInputStream();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   921
        private NullInputStream() {}
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   922
        public int read()      { return -1; }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   923
        public int available() { return 0;  }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents: 47216
diff changeset
   924
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   925
}