src/java.net.http/share/classes/jdk/internal/net/http/Http2Connection.java
author dfuchs
Fri, 02 Mar 2018 15:14:03 +0000
branchhttp-client-branch
changeset 56227 278e1c6c3e99
parent 56205 f4c9c5920141
child 56252 e4b05854c51f
permissions -rw-r--r--
http-client-branch: 8198947: HttpClient fails to send successive POST requests under HTTP/2
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
/*
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
     2
 * Copyright (c) 2015, 2018, 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.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * 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
    12
 * 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
    13
 * 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
    14
 * 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
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * 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
    18
 * 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
    19
 * 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
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * 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
    22
 * 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
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    26
package jdk.internal.net.http;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    28
import java.io.EOFException;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    29
import java.io.IOException;
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
    30
import java.io.UncheckedIOException;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    31
import java.lang.System.Logger.Level;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    32
import java.net.InetSocketAddress;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    33
import java.net.URI;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    34
import java.nio.ByteBuffer;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    35
import java.nio.charset.StandardCharsets;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
import java.util.Iterator;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    37
import java.util.LinkedList;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    38
import java.util.List;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    39
import java.util.Map;
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
    40
import java.util.Set;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    41
import java.util.concurrent.CompletableFuture;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    42
import java.util.ArrayList;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    43
import java.util.Objects;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    44
import java.util.concurrent.ConcurrentHashMap;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    45
import java.util.concurrent.ConcurrentLinkedQueue;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    46
import java.util.concurrent.Flow;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    47
import java.util.function.Function;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    48
import java.util.function.Supplier;
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
    49
import javax.net.ssl.SSLEngine;
56044
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents: 56040
diff changeset
    50
import javax.net.ssl.SSLException;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56079
diff changeset
    51
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56079
diff changeset
    52
import java.net.http.HttpHeaders;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    53
import jdk.internal.net.http.HttpConnection.HttpPublisher;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    54
import jdk.internal.net.http.common.FlowTube;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    55
import jdk.internal.net.http.common.FlowTube.TubeSubscriber;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    56
import jdk.internal.net.http.common.HttpHeadersImpl;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    57
import jdk.internal.net.http.common.Log;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    58
import jdk.internal.net.http.common.MinimalFuture;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    59
import jdk.internal.net.http.common.SequentialScheduler;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    60
import jdk.internal.net.http.common.Utils;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    61
import jdk.internal.net.http.frame.ContinuationFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    62
import jdk.internal.net.http.frame.DataFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    63
import jdk.internal.net.http.frame.ErrorFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    64
import jdk.internal.net.http.frame.FramesDecoder;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    65
import jdk.internal.net.http.frame.FramesEncoder;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    66
import jdk.internal.net.http.frame.GoAwayFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    67
import jdk.internal.net.http.frame.HeaderFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    68
import jdk.internal.net.http.frame.HeadersFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    69
import jdk.internal.net.http.frame.Http2Frame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    70
import jdk.internal.net.http.frame.MalformedFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    71
import jdk.internal.net.http.frame.OutgoingHeaders;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    72
import jdk.internal.net.http.frame.PingFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    73
import jdk.internal.net.http.frame.PushPromiseFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    74
import jdk.internal.net.http.frame.ResetFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    75
import jdk.internal.net.http.frame.SettingsFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    76
import jdk.internal.net.http.frame.WindowUpdateFrame;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    77
import jdk.internal.net.http.hpack.Encoder;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    78
import jdk.internal.net.http.hpack.Decoder;
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    79
import jdk.internal.net.http.hpack.DecodingCallback;
56072
96c1f6e984eb http-client-branch: use UTF-8 for all convertions to String
chegar
parents: 56060
diff changeset
    80
import static java.nio.charset.StandardCharsets.UTF_8;
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    81
import static jdk.internal.net.http.frame.SettingsFrame.*;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    82
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
/**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    85
 * An Http2Connection. Encapsulates the socket(channel) and any SSLEngine used
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    86
 * over it. Contains an HttpConnection which hides the SocketChannel SSL stuff.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    87
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    88
 * Http2Connections belong to a Http2ClientImpl, (one of) which belongs
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    89
 * to a HttpClientImpl.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    90
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    91
 * Creation cases:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    92
 * 1) upgraded HTTP/1.1 plain tcp connection
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    93
 * 2) prior knowledge directly created plain tcp connection
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    94
 * 3) directly created HTTP/2 SSL connection which uses ALPN.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    95
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    96
 * Sending is done by writing directly to underlying HttpConnection object which
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    97
 * is operating in async mode. No flow control applies on output at this level
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    98
 * and all writes are just executed as puts to an output Q belonging to HttpConnection
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    99
 * Flow control is implemented by HTTP/2 protocol itself.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   100
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   101
 * Hpack header compression
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   102
 * and outgoing stream creation is also done here, because these operations
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   103
 * must be synchronized at the socket level. Stream objects send frames simply
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   104
 * by placing them on the connection's output Queue. sendFrame() is called
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   105
 * from a higher level (Stream) thread.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   106
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   107
 * asyncReceive(ByteBuffer) is always called from the selector thread. It assembles
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   108
 * incoming Http2Frames, and directs them to the appropriate Stream.incoming()
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   109
 * or handles them directly itself. This thread performs hpack decompression
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   110
 * and incoming stream creation (Server push). Incoming frames destined for a
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   111
 * stream are provided by calling Stream.incoming().
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   113
class Http2Connection  {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   114
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   115
    static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
    static final boolean DEBUG_HPACK = Utils.DEBUG_HPACK; // Revisit: temporary dev flag.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   117
    final System.Logger  debug = Utils.getDebugLogger(this::dbgString, DEBUG);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   118
    final static System.Logger  DEBUG_LOGGER =
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   119
            Utils.getDebugLogger("Http2Connection"::toString, DEBUG);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   120
    private final System.Logger debugHpack =
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   121
                  Utils.getHpackLogger(this::dbgString, DEBUG_HPACK);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   122
    static final ByteBuffer EMPTY_TRIGGER = ByteBuffer.allocate(0);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   123
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   124
    private boolean singleStream; // used only for stream 1, then closed
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   125
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   126
    /*
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   127
     *  ByteBuffer pooling strategy for HTTP/2 protocol:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   128
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   129
     * In general there are 4 points where ByteBuffers are used:
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   130
     *  - incoming/outgoing frames from/to ByteBuffers plus incoming/outgoing encrypted data
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   131
     *    in case of SSL connection.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   132
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   133
     * 1. Outgoing frames encoded to ByteBuffers.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   134
     *    Outgoing ByteBuffers are created with requited size and frequently small (except DataFrames, etc)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   135
     *    At this place no pools at all. All outgoing buffers should be collected by GC.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   136
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   137
     * 2. Incoming ByteBuffers (decoded to frames).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   138
     *    Here, total elimination of BB pool is not a good idea.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   139
     *    We don't know how many bytes we will receive through network.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   140
     * So here we allocate buffer of reasonable size. The following life of the BB:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   141
     * - If all frames decoded from the BB are other than DataFrame and HeaderFrame (and HeaderFrame subclasses)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   142
     *     BB is returned to pool,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   143
     * - If we decoded DataFrame from the BB. In that case DataFrame refers to subbuffer obtained by slice() method.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   144
     *     Such BB is never returned to pool and will be GCed.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   145
     * - If we decoded HeadersFrame from the BB. Then header decoding is performed inside processFrame method and
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   146
     *     the buffer could be release to pool.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   147
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   148
     * 3. SLL encrypted buffers. Here another pool was introduced and all net buffers are to/from the pool,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   149
     *    because of we can't predict size encrypted packets.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   150
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   151
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   152
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   153
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   154
    // A small class that allows to control frames with respect to the state of
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   155
    // the connection preface. Any data received before the connection
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   156
    // preface is sent will be buffered.
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   157
    private final class FramesController {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   158
        volatile boolean prefaceSent;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   159
        volatile List<ByteBuffer> pending;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   160
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   161
        boolean processReceivedData(FramesDecoder decoder, ByteBuffer buf)
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   162
                throws IOException
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   163
        {
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   164
            // if preface is not sent, buffers data in the pending list
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   165
            if (!prefaceSent) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   166
                debug.log(Level.DEBUG, "Preface is not sent: buffering %d",
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   167
                          buf.remaining());
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   168
                synchronized (this) {
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   169
                    if (!prefaceSent) {
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   170
                        if (pending == null) pending = new ArrayList<>();
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   171
                        pending.add(buf);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   172
                        debug.log(Level.DEBUG, () -> "there are now "
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   173
                              + Utils.remaining(pending)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   174
                              + " bytes buffered waiting for preface to be sent");
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   175
                        return false;
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   176
                    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   177
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   178
            }
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   179
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   180
            // Preface is sent. Checks for pending data and flush it.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   181
            // We rely on this method being called from within the Http2TubeSubscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   182
            // scheduler, so we know that no other thread could execute this method
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   183
            // concurrently while we're here.
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   184
            // This ensures that later incoming buffers will not
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   185
            // be processed before we have flushed the pending queue.
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   186
            // No additional synchronization is therefore necessary here.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   187
            List<ByteBuffer> pending = this.pending;
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   188
            this.pending = null;
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   189
            if (pending != null) {
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   190
                // flush pending data
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   191
                debug.log(Level.DEBUG, () -> "Processing buffered data: "
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   192
                      + Utils.remaining(pending));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   193
                for (ByteBuffer b : pending) {
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   194
                    decoder.decode(b);
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   195
                }
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   196
            }
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   197
            // push the received buffer to the frames decoder.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   198
            if (buf != EMPTY_TRIGGER) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   199
                debug.log(Level.DEBUG, "Processing %d", buf.remaining());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   200
                decoder.decode(buf);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   201
            }
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   202
            return true;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   203
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   204
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   205
        // Mark that the connection preface is sent
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   206
        void markPrefaceSent() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   207
            assert !prefaceSent;
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   208
            synchronized (this) {
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   209
                prefaceSent = true;
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   210
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   211
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   212
    }
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
    volatile boolean closed;
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
    //-------------------------------------
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   217
    final HttpConnection connection;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   218
    private final Http2ClientImpl client2;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   219
    private final Map<Integer,Stream<?>> streams = new ConcurrentHashMap<>();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   220
    private int nextstreamid;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   221
    private int nextPushStream = 2;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   222
    private final Encoder hpackOut;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   223
    private final Decoder hpackIn;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   224
    final SettingsFrame clientSettings;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   225
    private volatile SettingsFrame serverSettings;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   226
    private final String key; // for HttpClientImpl.connections map
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   227
    private final FramesDecoder framesDecoder;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   228
    private final FramesEncoder framesEncoder = new FramesEncoder();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   229
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   230
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   231
     * Send Window controller for both connection and stream windows.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   232
     * Each of this connection's Streams MUST use this controller.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   233
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   234
    private final WindowController windowController = new WindowController();
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   235
    private final FramesController framesController = new FramesController();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   236
    private final Http2TubeSubscriber subscriber = new Http2TubeSubscriber();
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   237
    final ConnectionWindowUpdateSender windowUpdater;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   238
    private volatile Throwable cause;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   239
    private volatile Supplier<ByteBuffer> initial;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   240
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   241
    static final int DEFAULT_FRAME_SIZE = 16 * 1024;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   242
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
    // TODO: need list of control frames from other threads
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   245
    // that need to be sent
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   246
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   247
    private Http2Connection(HttpConnection connection,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   248
                            Http2ClientImpl client2,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   249
                            int nextstreamid,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   250
                            String key) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   251
        this.connection = connection;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   252
        this.client2 = client2;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   253
        this.nextstreamid = nextstreamid;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   254
        this.key = key;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   255
        this.clientSettings = this.client2.getClientSettings();
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   256
        this.framesDecoder = new FramesDecoder(this::processFrame,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   257
                clientSettings.getParameter(SettingsFrame.MAX_FRAME_SIZE));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   258
        // serverSettings will be updated by server
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   259
        this.serverSettings = SettingsFrame.getDefaultSettings();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   260
        this.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
   261
        this.hpackIn = new Decoder(clientSettings.getParameter(HEADER_TABLE_SIZE));
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   262
        debugHpack.log(Level.DEBUG, () -> "For the record:" + super.toString());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   263
        debugHpack.log(Level.DEBUG, "Decoder created: %s", hpackIn);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   264
        debugHpack.log(Level.DEBUG, "Encoder created: %s", hpackOut);
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   265
        this.windowUpdater = new ConnectionWindowUpdateSender(this,
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   266
                client2.getConnectionWindowSize(clientSettings));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   267
    }
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
   268
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
   269
    /**
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
   270
     * Case 1) Create from upgraded HTTP/1.1 connection.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   271
     * Is ready to use. Can be SSL. exchange is the Exchange
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
   272
     * that initiated the connection, whose response will be delivered
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
   273
     * on a Stream.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
   274
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   275
    private Http2Connection(HttpConnection connection,
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   276
                    Http2ClientImpl client2,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   277
                    Exchange<?> exchange,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   278
                    Supplier<ByteBuffer> initial)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   279
        throws IOException, InterruptedException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   280
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   281
        this(connection,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   282
                client2,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   283
                3, // stream 1 is registered during the upgrade
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   284
                keyFor(connection));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   285
        Log.logTrace("Connection send window size {0} ", windowController.connectionWindowSize());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   286
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   287
        Stream<?> initialStream = createStream(exchange);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   288
        initialStream.registerStream(1);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   289
        windowController.registerStream(1, getInitialSendWindowSize());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   290
        initialStream.requestSent();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   291
        // Upgrading:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   292
        //    set callbacks before sending preface - makes sure anything that
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   293
        //    might be sent by the server will come our way.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   294
        this.initial = initial;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   295
        connectFlows(connection);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   296
        sendConnectionPreface();
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
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   299
    // Used when upgrading an HTTP/1.1 connection to HTTP/2 after receiving
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   300
    // agreement from the server. Async style but completes immediately, because
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   301
    // the connection is already connected.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   302
    static CompletableFuture<Http2Connection> createAsync(HttpConnection connection,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   303
                                                          Http2ClientImpl client2,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   304
                                                          Exchange<?> exchange,
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   305
                                                          Supplier<ByteBuffer> initial)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   306
    {
43984
f33383dcb1fb 8175274: Fix httpclient asynchronous usage
skuksenko
parents: 42460
diff changeset
   307
        return MinimalFuture.supply(() -> new Http2Connection(connection, client2, exchange, initial));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   308
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   309
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   310
    // Requires TLS handshake. So, is really async
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   311
    static CompletableFuture<Http2Connection> createAsync(HttpRequestImpl request,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   312
                                                          Http2ClientImpl h2client) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   313
        assert request.secure();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   314
        AbstractAsyncSSLConnection connection = (AbstractAsyncSSLConnection)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   315
        HttpConnection.getConnection(request.getAddress(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   316
                                     h2client.client(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   317
                                     request,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   318
                                     HttpClient.Version.HTTP_2);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   319
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   320
        return connection.connectAsync()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   321
                  .thenCompose(unused -> checkSSLConfig(connection))
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   322
                  .thenCompose(notused-> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   323
                      CompletableFuture<Http2Connection> cf = new MinimalFuture<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   324
                      try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   325
                          Http2Connection hc = new Http2Connection(request, h2client, connection);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   326
                          cf.complete(hc);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   327
                      } catch (IOException e) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   328
                          cf.completeExceptionally(e);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   329
                      }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   330
                      return cf; } );
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   331
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   332
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   333
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   334
     * Cases 2) 3)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   335
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   336
     * request is request to be sent.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   337
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   338
    private Http2Connection(HttpRequestImpl request,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   339
                            Http2ClientImpl h2client,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   340
                            HttpConnection connection)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   341
        throws IOException
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   342
    {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   343
        this(connection,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   344
             h2client,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   345
             1,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   346
             keyFor(request.uri(), request.proxy()));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   347
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   348
        Log.logTrace("Connection send window size {0} ", windowController.connectionWindowSize());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   349
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   350
        // safe to resume async reading now.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   351
        connectFlows(connection);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   352
        sendConnectionPreface();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   353
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   354
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   355
    private void connectFlows(HttpConnection connection) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   356
        FlowTube tube =  connection.getConnectionFlow();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   357
        // Connect the flow to our Http2TubeSubscriber:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   358
        tube.connectFlows(connection.publisher(), subscriber);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   359
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   360
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   361
    final HttpClientImpl client() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   362
        return client2.client();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   363
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   364
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   365
    /**
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   366
     * Throws an IOException if h2 was not negotiated
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   367
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   368
    private static CompletableFuture<?> checkSSLConfig(AbstractAsyncSSLConnection aconn) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   369
        assert aconn.isSecure();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   370
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   371
        Function<String, CompletableFuture<Void>> checkAlpnCF = (alpn) -> {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   372
            CompletableFuture<Void> cf = new MinimalFuture<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   373
            SSLEngine engine = aconn.getEngine();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   374
            assert Objects.equals(alpn, engine.getApplicationProtocol());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   375
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   376
            DEBUG_LOGGER.log(Level.DEBUG, "checkSSLConfig: alpn: %s", alpn );
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   377
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   378
            if (alpn == null || !alpn.equals("h2")) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   379
                String msg;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   380
                if (alpn == null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   381
                    Log.logSSL("ALPN not supported");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   382
                    msg = "ALPN not supported";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   383
                } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   384
                    switch (alpn) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   385
                        case "":
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   386
                            Log.logSSL(msg = "No ALPN negotiated");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   387
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   388
                        case "http/1.1":
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   389
                            Log.logSSL( msg = "HTTP/1.1 ALPN returned");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   390
                            break;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   391
                        default:
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   392
                            Log.logSSL(msg = "Unexpected ALPN: " + alpn);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   393
                            cf.completeExceptionally(new IOException(msg));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   394
                    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   395
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   396
                cf.completeExceptionally(new ALPNException(msg, aconn));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   397
                return cf;
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   398
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   399
            cf.complete(null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   400
            return cf;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   401
        };
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   402
56044
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents: 56040
diff changeset
   403
        return aconn.getALPN()
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents: 56040
diff changeset
   404
                .whenComplete((r,t) -> {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents: 56040
diff changeset
   405
                    if (t != null && t instanceof SSLException) {
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents: 56040
diff changeset
   406
                        // something went wrong during the initial handshake
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents: 56040
diff changeset
   407
                        // close the connection
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents: 56040
diff changeset
   408
                        aconn.close();
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents: 56040
diff changeset
   409
                    }
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents: 56040
diff changeset
   410
                })
a8423a38386e http-client-branch: fix issue in SSL flow delegate when handshake error occurs during unwrap
chegar
parents: 56040
diff changeset
   411
                .thenCompose(checkAlpnCF);
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   412
    }
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
   413
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   414
    synchronized boolean singleStream() {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   415
        return singleStream;
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   416
    }
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   417
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   418
    synchronized void setSingleStream(boolean use) {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   419
        singleStream = use;
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   420
    }
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   421
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   422
    static String keyFor(HttpConnection connection) {
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   423
        boolean isProxy = connection.isProxied(); // tunnel or plain clear connection through proxy
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   424
        boolean isSecure = connection.isSecure();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   425
        InetSocketAddress addr = connection.address();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   426
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   427
        return keyString(isSecure, isProxy, addr.getHostString(), addr.getPort());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   428
    }
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   429
    
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   430
    static String keyFor(URI uri, InetSocketAddress proxy) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   431
        boolean isSecure = uri.getScheme().equalsIgnoreCase("https");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   432
        boolean isProxy = proxy != null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   433
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   434
        String host;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   435
        int port;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   436
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   437
        if (proxy != null && !isSecure) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   438
            // clear connection through proxy: use
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   439
            // proxy host / proxy port
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   440
            host = proxy.getHostString();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   441
            port = proxy.getPort();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   442
        } else {
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   443
            // either secure tunnel connection through proxy
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   444
            // or direct connection to host, but in either
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   445
            // case only that host can be reached through
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   446
            // the connection: use target host / target port
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   447
            host = uri.getHost();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   448
            port = uri.getPort();
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
        return keyString(isSecure, isProxy, host, port);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   451
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   452
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   453
    // {C,S}:{H:P}:host:port
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   454
    // C indicates clear text connection "http"
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   455
    // S indicates secure "https"
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   456
    // H indicates host (direct) connection
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   457
    // P indicates proxy
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   458
    // Eg: "S:H:foo.com:80"
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   459
    static String keyString(boolean secure, boolean proxy, String host, int port) {
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   460
        if (secure && port == -1)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   461
            port = 443;
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   462
        else if (!secure && port == -1)
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   463
            port = 80;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   464
        return (secure ? "S:" : "C:") + (proxy ? "P:" : "H:") + host + ":" + port;
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
    String key() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   468
        return this.key;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   469
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   470
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   471
    boolean offerConnection() {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   472
        return client2.offerConnection(this);
42460
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
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   475
    private HttpPublisher publisher() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   476
        return connection.publisher();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   477
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   478
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   479
    private void decodeHeaders(HeaderFrame frame, DecodingCallback decoder)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   480
            throws IOException
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   481
    {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   482
        debugHpack.log(Level.DEBUG, "decodeHeaders(%s)", decoder);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   483
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   484
        boolean endOfHeaders = frame.getFlag(HeaderFrame.END_HEADERS);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   485
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   486
        List<ByteBuffer> buffers = frame.getHeaderBlock();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   487
        int len = buffers.size();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   488
        for (int i = 0; i < len; i++) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   489
            ByteBuffer b = buffers.get(i);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   490
            hpackIn.decode(b, endOfHeaders && (i == len - 1), decoder);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   491
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   492
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   493
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   494
    final int getInitialSendWindowSize() {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   495
        return serverSettings.getParameter(INITIAL_WINDOW_SIZE);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   496
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   497
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   498
    void close() {
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   499
        Log.logTrace("Closing HTTP/2 connection: to {0}", connection.address());
56072
96c1f6e984eb http-client-branch: use UTF-8 for all convertions to String
chegar
parents: 56060
diff changeset
   500
        GoAwayFrame f = new GoAwayFrame(0,
96c1f6e984eb http-client-branch: use UTF-8 for all convertions to String
chegar
parents: 56060
diff changeset
   501
                                        ErrorFrame.NO_ERROR,
96c1f6e984eb http-client-branch: use UTF-8 for all convertions to String
chegar
parents: 56060
diff changeset
   502
                                        "Requested by user".getBytes(UTF_8));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   503
        // TODO: set last stream. For now zero ok.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   504
        sendFrame(f);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   505
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   506
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   507
    long count;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   508
    final void asyncReceive(ByteBuffer buffer) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   509
        // We don't need to read anything and
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   510
        // we don't want to send anything back to the server
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   511
        // until the connection preface has been sent.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   512
        // Therefore we're going to wait if needed before reading
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   513
        // (and thus replying) to anything.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   514
        // Starting to reply to something (e.g send an ACK to a
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   515
        // SettingsFrame sent by the server) before the connection
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   516
        // preface is fully sent might result in the server
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   517
        // sending a GOAWAY frame with 'invalid_preface'.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   518
        //
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   519
        // Note: asyncReceive is only called from the Http2TubeSubscriber
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   520
        //       sequential scheduler.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   521
        try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   522
            Supplier<ByteBuffer> bs = initial;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   523
            // ensure that we always handle the initial buffer first,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   524
            // if any.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   525
            if (bs != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   526
                initial = null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   527
                ByteBuffer b = bs.get();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   528
                if (b.hasRemaining()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   529
                    long c = ++count;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   530
                    debug.log(Level.DEBUG, () -> "H2 Receiving Initial("
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   531
                        + c +"): " + b.remaining());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   532
                    framesController.processReceivedData(framesDecoder, b);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   533
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   534
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   535
            ByteBuffer b = buffer;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   536
            // the Http2TubeSubscriber scheduler ensures that the order of incoming
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   537
            // buffers is preserved.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   538
            if (b == EMPTY_TRIGGER) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   539
                debug.log(Level.DEBUG, "H2 Received EMPTY_TRIGGER");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   540
                boolean prefaceSent = framesController.prefaceSent;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   541
                assert prefaceSent;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   542
                // call framesController.processReceivedData to potentially
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   543
                // trigger the processing of all the data buffered there.
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   544
                framesController.processReceivedData(framesDecoder, buffer);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   545
                debug.log(Level.DEBUG, "H2 processed buffered data");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   546
            } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   547
                long c = ++count;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   548
                debug.log(Level.DEBUG, "H2 Receiving(%d): %d", c, b.remaining());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   549
                framesController.processReceivedData(framesDecoder, buffer);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   550
                debug.log(Level.DEBUG, "H2 processed(%d)", c);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   551
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   552
        } catch (Throwable e) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   553
            String msg = Utils.stackTrace(e);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   554
            Log.logTrace(msg);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   555
            shutdown(e);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   556
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   557
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   558
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   559
    Throwable getRecordedCause() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   560
        return cause;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   561
    }
42460
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
    void shutdown(Throwable t) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   564
        debug.log(Level.DEBUG, () -> "Shutting down h2c (closed="+closed+"): " + t);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   565
        if (closed == true) return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   566
        synchronized (this) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   567
            if (closed == true) return;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   568
            closed = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   569
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   570
        Log.logError(t);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   571
        Throwable initialCause = this.cause;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   572
        if (initialCause == null) this.cause = t;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   573
        client2.deleteConnection(this);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   574
        List<Stream<?>> c = new LinkedList<>(streams.values());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   575
        for (Stream<?> s : c) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   576
            s.cancelImpl(t);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   577
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   578
        connection.close();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   579
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   580
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   581
    /**
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   582
     * Streams initiated by a client MUST use odd-numbered stream
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   583
     * identifiers; those initiated by the server MUST use even-numbered
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   584
     * stream identifiers.
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   585
     */
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   586
    private static final boolean isServerInitiatedStream(int streamid) {
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   587
        return (streamid & 0x1) == 0;
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   588
    }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   589
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   590
    /**
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   591
     * Handles stream 0 (common) frames that apply to whole connection and passes
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   592
     * other stream specific frames to that Stream object.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   593
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   594
     * Invokes Stream.incoming() which is expected to process frame without
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   595
     * blocking.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   596
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   597
    void processFrame(Http2Frame frame) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   598
        Log.logFrames(frame, "IN");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   599
        int streamid = frame.streamid();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   600
        if (frame instanceof MalformedFrame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   601
            Log.logError(((MalformedFrame) frame).getMessage());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   602
            if (streamid == 0) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   603
                framesDecoder.close("Malformed frame on stream 0");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   604
                protocolError(((MalformedFrame) frame).getErrorCode(),
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   605
                        ((MalformedFrame) frame).getMessage());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   606
            } else {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   607
                debug.log(Level.DEBUG, () -> "Reset stream: "
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   608
                          + ((MalformedFrame) frame).getMessage());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   609
                resetStream(streamid, ((MalformedFrame) frame).getErrorCode());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   610
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   611
            return;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   612
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   613
        if (streamid == 0) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   614
            handleConnectionFrame(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   615
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   616
            if (frame instanceof SettingsFrame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   617
                // The stream identifier for a SETTINGS frame MUST be zero
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   618
                framesDecoder.close(
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   619
                        "The stream identifier for a SETTINGS frame MUST be zero");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   620
                protocolError(GoAwayFrame.PROTOCOL_ERROR);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   621
                return;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   622
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   623
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   624
            Stream<?> stream = getStream(streamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   625
            if (stream == null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   626
                // Should never receive a frame with unknown stream id
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   627
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   628
                if (frame instanceof HeaderFrame) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   629
                    // always decode the headers as they may affect
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   630
                    // connection-level HPACK decoding state
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   631
                    DecodingCallback decoder = new ValidatingHeadersConsumer();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   632
                    try {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   633
                        decodeHeaders((HeaderFrame) frame, decoder);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   634
                    } catch (UncheckedIOException e) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   635
                        protocolError(ResetFrame.PROTOCOL_ERROR, e.getMessage());
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   636
                        return;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   637
                    }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   638
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   639
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   640
                if (!(frame instanceof ResetFrame)) {
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   641
                    if (isServerInitiatedStream(streamid)) {
48523
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   642
                        if (streamid < nextPushStream) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   643
                            // trailing data on a cancelled push promise stream,
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   644
                            // reset will already have been sent, ignore
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   645
                            Log.logTrace("Ignoring cancelled push promise frame " + frame);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   646
                        } else {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   647
                            resetStream(streamid, ResetFrame.PROTOCOL_ERROR);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   648
                        }
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   649
                    } else if (streamid >= nextstreamid) {
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   650
                        // otherwise the stream has already been reset/closed
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   651
                        resetStream(streamid, ResetFrame.PROTOCOL_ERROR);
b95b08f3e1a8 8194883: Unhandleable Push Promises should be cancelled
chegar
parents: 48376
diff changeset
   652
                    }
42460
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
                return;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   655
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   656
            if (frame instanceof PushPromiseFrame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   657
                PushPromiseFrame pp = (PushPromiseFrame)frame;
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   658
                try {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   659
                    handlePushPromise(stream, pp);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   660
                } catch (UncheckedIOException e) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   661
                    protocolError(ResetFrame.PROTOCOL_ERROR, e.getMessage());
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   662
                    return;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   663
                }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   664
            } else if (frame instanceof HeaderFrame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   665
                // decode headers (or continuation)
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   666
                try {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   667
                    decodeHeaders((HeaderFrame) frame, stream.rspHeadersConsumer());
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   668
                } catch (UncheckedIOException e) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   669
                    protocolError(ResetFrame.PROTOCOL_ERROR, e.getMessage());
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   670
                    return;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
   671
                }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   672
                stream.incoming(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   673
            } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   674
                stream.incoming(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   675
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   676
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   677
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   678
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   679
    private <T> void handlePushPromise(Stream<T> parent, PushPromiseFrame pp)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   680
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   681
    {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   682
        // always decode the headers as they may affect connection-level HPACK
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   683
        // decoding state
56094
881f0ecb513f http-client-branch: (HttpClient) cleanup before JDK-8196962. Removing a legacy logging for HPACK decoding. The HPACK core provides a more robust one.
prappo
parents: 56092
diff changeset
   684
        HeaderDecoder decoder = new HeaderDecoder();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   685
        decodeHeaders(pp, decoder);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   686
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   687
        HttpRequestImpl parentReq = parent.request;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   688
        int promisedStreamid = pp.getPromisedStream();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   689
        if (promisedStreamid != nextPushStream) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   690
            resetStream(promisedStreamid, ResetFrame.PROTOCOL_ERROR);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   691
            return;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   692
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   693
            nextPushStream += 2;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   694
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   695
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   696
        HttpHeadersImpl headers = decoder.headers();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   697
        HttpRequestImpl pushReq = HttpRequestImpl.createPushRequest(parentReq, headers);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   698
        Exchange<T> pushExch = new Exchange<>(pushReq, parent.exchange.multi);
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56008
diff changeset
   699
        Stream.PushedStream<T> pushStream = createPushStream(parent, pushExch);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   700
        pushExch.exchImpl = pushStream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   701
        pushStream.registerStream(promisedStreamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   702
        parent.incoming_pushPromise(pushReq, pushStream);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   703
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   704
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   705
    private void handleConnectionFrame(Http2Frame frame)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   706
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   707
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   708
        switch (frame.type()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   709
          case SettingsFrame.TYPE:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   710
              handleSettings((SettingsFrame)frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   711
              break;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   712
          case PingFrame.TYPE:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   713
              handlePing((PingFrame)frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   714
              break;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   715
          case GoAwayFrame.TYPE:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   716
              handleGoAway((GoAwayFrame)frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   717
              break;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   718
          case WindowUpdateFrame.TYPE:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   719
              handleWindowUpdate((WindowUpdateFrame)frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   720
              break;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   721
          default:
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   722
            protocolError(ErrorFrame.PROTOCOL_ERROR);
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
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   725
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   726
    void resetStream(int streamid, int code) throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   727
        Log.logError(
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   728
            "Resetting stream {0,number,integer} with error code {1,number,integer}",
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   729
            streamid, code);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   730
        ResetFrame frame = new ResetFrame(streamid, code);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   731
        sendFrame(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   732
        closeStream(streamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   733
    }
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
    void closeStream(int streamid) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   736
        debug.log(Level.DEBUG, "Closed stream %d", streamid);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   737
        Stream<?> s = streams.remove(streamid);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   738
        if (s != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   739
            // decrement the reference count on the HttpClientImpl
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   740
            // to allow the SelectorManager thread to exit if no
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   741
            // other operation is pending and the facade is no
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   742
            // longer referenced.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   743
            client().unreference();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   744
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   745
        // ## Remove s != null. It is a hack for delayed cancellation,reset
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   746
        if (s != null && !(s instanceof Stream.PushedStream)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   747
            // Since PushStreams have no request body, then they have no
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   748
            // corresponding entry in the window controller.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   749
            windowController.removeStream(streamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   750
        }
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   751
        if (singleStream() && streams.isEmpty()) {
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   752
            // should be only 1 stream, but there might be more if server push
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   753
            close();
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   754
        }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   755
    }
48376
41ae5c69b09c 8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents: 48263
diff changeset
   756
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   757
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   758
     * Increments this connection's send Window by the amount in the given frame.
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
    private void handleWindowUpdate(WindowUpdateFrame f)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   761
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   762
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   763
        int amount = f.getUpdate();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   764
        if (amount <= 0) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   765
            // ## temporarily disable to workaround a bug in Jetty where it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   766
            // ## sends Window updates with a 0 update value.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   767
            //protocolError(ErrorFrame.PROTOCOL_ERROR);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   768
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   769
            boolean success = windowController.increaseConnectionWindow(amount);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   770
            if (!success) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   771
                protocolError(ErrorFrame.FLOW_CONTROL_ERROR);  // overflow
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   772
            }
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
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   776
    private void protocolError(int errorCode)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   777
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   778
    {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   779
        protocolError(errorCode, null);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   780
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   781
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   782
    private void protocolError(int errorCode, String msg)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   783
        throws IOException
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   784
    {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   785
        GoAwayFrame frame = new GoAwayFrame(0, errorCode);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   786
        sendFrame(frame);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   787
        shutdown(new IOException("protocol error" + (msg == null?"":(": " + msg))));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   788
    }
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
    private void handleSettings(SettingsFrame frame)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   791
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   792
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   793
        assert frame.streamid() == 0;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   794
        if (!frame.getFlag(SettingsFrame.ACK)) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   795
            int newWindowSize = frame.getParameter(INITIAL_WINDOW_SIZE);
56227
278e1c6c3e99 http-client-branch: 8198947: HttpClient fails to send successive POST requests under HTTP/2
dfuchs
parents: 56205
diff changeset
   796
            if (newWindowSize != -1) {
278e1c6c3e99 http-client-branch: 8198947: HttpClient fails to send successive POST requests under HTTP/2
dfuchs
parents: 56205
diff changeset
   797
                int oldWindowSize = serverSettings.getParameter(INITIAL_WINDOW_SIZE);
278e1c6c3e99 http-client-branch: 8198947: HttpClient fails to send successive POST requests under HTTP/2
dfuchs
parents: 56205
diff changeset
   798
                int diff = newWindowSize - oldWindowSize;
278e1c6c3e99 http-client-branch: 8198947: HttpClient fails to send successive POST requests under HTTP/2
dfuchs
parents: 56205
diff changeset
   799
                if (diff != 0) {
278e1c6c3e99 http-client-branch: 8198947: HttpClient fails to send successive POST requests under HTTP/2
dfuchs
parents: 56205
diff changeset
   800
                    windowController.adjustActiveStreams(diff);
278e1c6c3e99 http-client-branch: 8198947: HttpClient fails to send successive POST requests under HTTP/2
dfuchs
parents: 56205
diff changeset
   801
                }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   802
            }
56227
278e1c6c3e99 http-client-branch: 8198947: HttpClient fails to send successive POST requests under HTTP/2
dfuchs
parents: 56205
diff changeset
   803
278e1c6c3e99 http-client-branch: 8198947: HttpClient fails to send successive POST requests under HTTP/2
dfuchs
parents: 56205
diff changeset
   804
            serverSettings.update(frame);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   805
            sendFrame(new SettingsFrame(SettingsFrame.ACK));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   806
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   807
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   808
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   809
    private void handlePing(PingFrame frame)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   810
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   811
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   812
        frame.setFlag(PingFrame.ACK);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   813
        sendUnorderedFrame(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   814
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   815
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   816
    private void handleGoAway(GoAwayFrame frame)
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   817
        throws IOException
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   818
    {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   819
        shutdown(new IOException(
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   820
                        String.valueOf(connection.channel().getLocalAddress())
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   821
                        +": GOAWAY received"));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   822
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   823
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   824
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   825
     * Max frame size we are allowed to send
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   826
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   827
    public int getMaxSendFrameSize() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   828
        int param = serverSettings.getParameter(MAX_FRAME_SIZE);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   829
        if (param == -1) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   830
            param = DEFAULT_FRAME_SIZE;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   831
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   832
        return param;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   833
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   834
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   835
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   836
     * Max frame size we will receive
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   837
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   838
    public int getMaxReceiveFrameSize() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   839
        return clientSettings.getParameter(MAX_FRAME_SIZE);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   840
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   841
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   842
    private static final String CLIENT_PREFACE = "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n";
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
    private static final byte[] PREFACE_BYTES =
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   845
        CLIENT_PREFACE.getBytes(StandardCharsets.ISO_8859_1);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   846
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   847
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   848
     * Sends Connection preface and Settings frame with current preferred
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   849
     * values
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   850
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   851
    private void sendConnectionPreface() throws IOException {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   852
        Log.logTrace("{0}: start sending connection preface to {1}",
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   853
                     connection.channel().getLocalAddress(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   854
                     connection.address());
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   855
        SettingsFrame sf = new SettingsFrame(clientSettings);
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   856
        int initialWindowSize = sf.getParameter(INITIAL_WINDOW_SIZE);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   857
        ByteBuffer buf = framesEncoder.encodeConnectionPreface(PREFACE_BYTES, sf);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   858
        Log.logFrames(sf, "OUT");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   859
        // send preface bytes and SettingsFrame together
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   860
        HttpPublisher publisher = publisher();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   861
        publisher.enqueue(List.of(buf));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   862
        publisher.signalEnqueued();
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   863
        // mark preface sent.
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   864
        framesController.markPrefaceSent();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   865
        Log.logTrace("PREFACE_BYTES sent");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   866
        Log.logTrace("Settings Frame sent");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   867
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   868
        // send a Window update for the receive buffer we are using
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   869
        // minus the initial 64 K specified in protocol
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   870
        final int len = windowUpdater.initialWindowSize - initialWindowSize;
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   871
        if (len > 0) {
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   872
            windowUpdater.sendWindowUpdate(len);
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
   873
        }
45530
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   874
        // there will be an ACK to the windows update - which should
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   875
        // cause any pending data stored before the preface was sent to be
3c98842fddf7 8181430: HTTP/2 client might deadlock when receiving data during the initial handshake
dfuchs
parents: 44854
diff changeset
   876
        // flushed (see PrefaceController).
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   877
        Log.logTrace("finished sending connection preface");
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   878
        debug.log(Level.DEBUG, "Triggering processing of buffered data"
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   879
                  + " after sending connection preface");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   880
        subscriber.onNext(List.of(EMPTY_TRIGGER));
42460
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
     * Returns an existing Stream with given id, or null if doesn't exist
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
    @SuppressWarnings("unchecked")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   887
    <T> Stream<T> getStream(int streamid) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   888
        return (Stream<T>)streams.get(streamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   889
    }
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
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   892
     * Creates Stream with given id.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   893
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   894
    final <T> Stream<T> createStream(Exchange<T> exchange) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   895
        Stream<T> stream = new Stream<>(this, exchange, windowController);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   896
        return stream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   897
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   898
56010
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56008
diff changeset
   899
    <T> Stream.PushedStream<T> createPushStream(Stream<T> parent, Exchange<T> pushEx) {
782b2f2d1e76 http-client-branch: updated server push API
michaelm
parents: 56008
diff changeset
   900
        PushGroup<T> pg = parent.exchange.getPushGroup();
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   901
        return new Stream.PushedStream<>(pg, this, pushEx);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   902
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   903
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   904
    <T> void putStream(Stream<T> stream, int streamid) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   905
        // increment the reference count on the HttpClientImpl
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   906
        // to prevent the SelectorManager thread from exiting until
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   907
        // the stream is closed.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   908
        client().reference();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   909
        streams.put(streamid, stream);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   910
    }
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
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   913
     * Encode the headers into a List<ByteBuffer> and then create HEADERS
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   914
     * and CONTINUATION frames from the list and return the List<Http2Frame>.
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
    private List<HeaderFrame> encodeHeaders(OutgoingHeaders<Stream<?>> frame) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   917
        List<ByteBuffer> buffers = encodeHeadersImpl(
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   918
                getMaxSendFrameSize(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   919
                frame.getAttachment().getRequestPseudoHeaders(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   920
                frame.getUserHeaders(),
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   921
                frame.getSystemHeaders());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   922
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   923
        List<HeaderFrame> frames = new ArrayList<>(buffers.size());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   924
        Iterator<ByteBuffer> bufIterator = buffers.iterator();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   925
        HeaderFrame oframe = new HeadersFrame(frame.streamid(), frame.getFlags(), bufIterator.next());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   926
        frames.add(oframe);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   927
        while(bufIterator.hasNext()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   928
            oframe = new ContinuationFrame(frame.streamid(), bufIterator.next());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   929
            frames.add(oframe);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   930
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   931
        oframe.setFlag(HeaderFrame.END_HEADERS);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   932
        return frames;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   933
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   934
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   935
    // Dedicated cache for headers encoding ByteBuffer.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   936
    // There can be no concurrent access to this  buffer as all access to this buffer
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   937
    // and its content happen within a single critical code block section protected
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   938
    // by the sendLock. / (see sendFrame())
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   939
    // private final ByteBufferPool headerEncodingPool = new ByteBufferPool();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   940
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   941
    private ByteBuffer getHeaderBuffer(int maxFrameSize) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   942
        ByteBuffer buf = ByteBuffer.allocate(maxFrameSize);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   943
        buf.limit(maxFrameSize);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   944
        return buf;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   945
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   946
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   947
    /*
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   948
     * Encodes all the headers from the given HttpHeaders into the given List
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   949
     * of buffers.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   950
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   951
     * From https://tools.ietf.org/html/rfc7540#section-8.1.2 :
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   952
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   953
     *     ...Just as in HTTP/1.x, header field names are strings of ASCII
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   954
     *     characters that are compared in a case-insensitive fashion.  However,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   955
     *     header field names MUST be converted to lowercase prior to their
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   956
     *     encoding in HTTP/2...
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   957
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   958
    private List<ByteBuffer> encodeHeadersImpl(int maxFrameSize, HttpHeaders... headers) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   959
        ByteBuffer buffer = getHeaderBuffer(maxFrameSize);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   960
        List<ByteBuffer> buffers = new ArrayList<>();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   961
        for(HttpHeaders header : headers) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   962
            for (Map.Entry<String, List<String>> e : header.map().entrySet()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   963
                String lKey = e.getKey().toLowerCase();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   964
                List<String> values = e.getValue();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   965
                for (String value : values) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   966
                    hpackOut.header(lKey, value);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   967
                    while (!hpackOut.encode(buffer)) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   968
                        buffer.flip();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   969
                        buffers.add(buffer);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   970
                        buffer =  getHeaderBuffer(maxFrameSize);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   971
                    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   972
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   973
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   974
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   975
        buffer.flip();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   976
        buffers.add(buffer);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   977
        return buffers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   978
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   979
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   980
    private List<ByteBuffer> encodeHeaders(OutgoingHeaders<Stream<?>> oh, Stream<?> stream) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   981
        oh.streamid(stream.streamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   982
        if (Log.headers()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   983
            StringBuilder sb = new StringBuilder("HEADERS FRAME (stream=");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   984
            sb.append(stream.streamid).append(")\n");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   985
            Log.dumpHeaders(sb, "    ", oh.getAttachment().getRequestPseudoHeaders());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   986
            Log.dumpHeaders(sb, "    ", oh.getSystemHeaders());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   987
            Log.dumpHeaders(sb, "    ", oh.getUserHeaders());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   988
            Log.logHeaders(sb.toString());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   989
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   990
        List<HeaderFrame> frames = encodeHeaders(oh);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   991
        return encodeFrames(frames);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   992
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   993
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   994
    private List<ByteBuffer> encodeFrames(List<HeaderFrame> frames) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   995
        if (Log.frames()) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   996
            frames.forEach(f -> Log.logFrames(f, "OUT"));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   997
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   998
        return framesEncoder.encodeFrames(frames);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   999
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1000
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1001
    private Stream<?> registerNewStream(OutgoingHeaders<Stream<?>> oh) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1002
        Stream<?> stream = oh.getAttachment();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1003
        int streamid = nextstreamid;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1004
        nextstreamid += 2;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1005
        stream.registerStream(streamid);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1006
        // set outgoing window here. This allows thread sending
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1007
        // body to proceed.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1008
        windowController.registerStream(streamid, getInitialSendWindowSize());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1009
        return stream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1010
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1011
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1012
    private final Object sendlock = new Object();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1013
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1014
    void sendFrame(Http2Frame frame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1015
        try {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1016
            HttpPublisher publisher = publisher();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1017
            synchronized (sendlock) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1018
                if (frame instanceof OutgoingHeaders) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1019
                    @SuppressWarnings("unchecked")
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1020
                    OutgoingHeaders<Stream<?>> oh = (OutgoingHeaders<Stream<?>>) frame;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1021
                    Stream<?> stream = registerNewStream(oh);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1022
                    // provide protection from inserting unordered frames between Headers and Continuation
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1023
                    publisher.enqueue(encodeHeaders(oh, stream));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1024
                } else {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1025
                    publisher.enqueue(encodeFrame(frame));
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1026
                }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1027
            }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1028
            publisher.signalEnqueued();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1029
        } catch (IOException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1030
            if (!closed) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1031
                Log.logError(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1032
                shutdown(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1033
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1034
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1035
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1036
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1037
    private List<ByteBuffer> encodeFrame(Http2Frame frame) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1038
        Log.logFrames(frame, "OUT");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1039
        return framesEncoder.encodeFrame(frame);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1040
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1041
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1042
    void sendDataFrame(DataFrame frame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1043
        try {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1044
            HttpPublisher publisher = publisher();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1045
            publisher.enqueue(encodeFrame(frame));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1046
            publisher.signalEnqueued();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1047
        } catch (IOException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1048
            if (!closed) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1049
                Log.logError(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1050
                shutdown(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1051
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1052
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1053
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1054
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1055
    /*
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1056
     * Direct call of the method bypasses synchronization on "sendlock" and
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1057
     * allowed only of control frames: WindowUpdateFrame, PingFrame and etc.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1058
     * prohibited for such frames as DataFrame, HeadersFrame, ContinuationFrame.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1059
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1060
    void sendUnorderedFrame(Http2Frame frame) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1061
        try {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1062
            HttpPublisher publisher = publisher();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1063
            publisher.enqueueUnordered(encodeFrame(frame));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1064
            publisher.signalEnqueued();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1065
        } catch (IOException e) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1066
            if (!closed) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1067
                Log.logError(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1068
                shutdown(e);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1069
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1070
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1071
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1072
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1073
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1074
     * A simple tube subscriber for reading from the connection flow.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1075
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1076
    final class Http2TubeSubscriber implements TubeSubscriber {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1077
        volatile Flow.Subscription subscription;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1078
        volatile boolean completed;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1079
        volatile boolean dropped;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1080
        volatile Throwable error;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1081
        final ConcurrentLinkedQueue<ByteBuffer> queue
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1082
                = new ConcurrentLinkedQueue<>();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1083
        final SequentialScheduler scheduler =
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1084
                SequentialScheduler.synchronizedScheduler(this::processQueue);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1085
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1086
        final void processQueue() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1087
            try {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1088
                while (!queue.isEmpty() && !scheduler.isStopped()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1089
                    ByteBuffer buffer = queue.poll();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1090
                    debug.log(Level.DEBUG,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1091
                              "sending %d to Http2Connection.asyncReceive",
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1092
                              buffer.remaining());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1093
                    asyncReceive(buffer);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1094
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1095
            } catch (Throwable t) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1096
                Throwable x = error;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1097
                if (x == null) error = t;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1098
            } finally {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1099
                Throwable x = error;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1100
                if (x != null) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1101
                    debug.log(Level.DEBUG, "Stopping scheduler", x);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1102
                    scheduler.stop();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1103
                    Http2Connection.this.shutdown(x);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1104
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1105
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1106
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1107
55991
5683757b6771 http-client-branch: cleanup: add missing @Overrides
dfuchs
parents: 55983
diff changeset
  1108
        @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1109
        public void onSubscribe(Flow.Subscription subscription) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1110
            // supports being called multiple time.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1111
            // doesn't cancel the previous subscription, since that is
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1112
            // most probably the same as the new subscription.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1113
            assert this.subscription == null || dropped == false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1114
            this.subscription = subscription;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1115
            dropped = false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1116
            // TODO FIXME: request(1) should be done by the delegate.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1117
            if (!completed) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1118
                debug.log(Level.DEBUG, "onSubscribe: requesting Long.MAX_VALUE for reading");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1119
                subscription.request(Long.MAX_VALUE);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1120
            } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1121
                debug.log(Level.DEBUG, "onSubscribe: already completed");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1122
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1123
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1124
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1125
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1126
        public void onNext(List<ByteBuffer> item) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1127
            debug.log(Level.DEBUG, () -> "onNext: got " + Utils.remaining(item)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1128
                    + " bytes in " + item.size() + " buffers");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1129
            queue.addAll(item);
56040
f8eabb9a5c0f http-client-branch: SequentialScheduler cleanup
prappo
parents: 56019
diff changeset
  1130
            scheduler.runOrSchedule(client().theExecutor());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1131
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1132
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1133
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1134
        public void onError(Throwable throwable) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1135
            debug.log(Level.DEBUG, () -> "onError: " + throwable);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1136
            error = throwable;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1137
            completed = true;
56040
f8eabb9a5c0f http-client-branch: SequentialScheduler cleanup
prappo
parents: 56019
diff changeset
  1138
            scheduler.runOrSchedule(client().theExecutor());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1139
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1140
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1141
        @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1142
        public void onComplete() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1143
            debug.log(Level.DEBUG, "EOF");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1144
            error = new EOFException("EOF reached while reading");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1145
            completed = true;
56040
f8eabb9a5c0f http-client-branch: SequentialScheduler cleanup
prappo
parents: 56019
diff changeset
  1146
            scheduler.runOrSchedule(client().theExecutor());
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1147
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1148
55991
5683757b6771 http-client-branch: cleanup: add missing @Overrides
dfuchs
parents: 55983
diff changeset
  1149
        @Override
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1150
        public void dropSubscription() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1151
            debug.log(Level.DEBUG, "dropSubscription");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1152
            // we could probably set subscription to null here...
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1153
            // then we might not need the 'dropped' boolean?
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1154
            dropped = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1155
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1156
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1157
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1158
    @Override
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1159
    public final String toString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1160
        return dbgString();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1161
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1162
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1163
    final String dbgString() {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1164
        return "Http2Connection("
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1165
                    + connection.getConnectionFlow() + ")";
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1166
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
  1167
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1168
    static class HeaderDecoder extends ValidatingHeadersConsumer {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1169
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1170
        HttpHeadersImpl headers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1171
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1172
        HeaderDecoder() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1173
            this.headers = new HttpHeadersImpl();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1174
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1175
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1176
        @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1177
        public void onDecoded(CharSequence name, CharSequence value) {
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1178
            String n = name.toString();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1179
            String v = value.toString();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1180
            super.onDecoded(n, v);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1181
            headers.addHeader(n, v);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1182
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1183
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1184
        HttpHeadersImpl headers() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1185
            return headers;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1186
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1187
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1188
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1189
    /*
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1190
     * Checks RFC 7540 rules (relaxed) compliance regarding pseudo-headers.
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1191
     */
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1192
    static class ValidatingHeadersConsumer implements DecodingCallback {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1193
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1194
        private static final Set<String> PSEUDO_HEADERS =
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1195
                Set.of(":authority", ":method", ":path", ":scheme", ":status");
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1196
56205
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1197
        /** Used to check that if there are pseudo-headers, they go first */
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1198
        private boolean pseudoHeadersEnded;
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1199
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56128
diff changeset
  1200
        /**
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56128
diff changeset
  1201
         * Called when END_HEADERS was received. This consumer may be invoked
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56128
diff changeset
  1202
         * again after reset() is called, but for a whole new set of headers.
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56128
diff changeset
  1203
         */
56205
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1204
        void reset() {
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1205
            pseudoHeadersEnded = false;
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1206
        }
56166
56c52d6417d1 http-client-branch: HTTP/2 200 response headers should not be appended to the 100-continue response headers
dfuchs
parents: 56128
diff changeset
  1207
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1208
        @Override
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1209
        public void onDecoded(CharSequence name, CharSequence value)
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1210
                throws UncheckedIOException
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1211
        {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1212
            String n = name.toString();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1213
            if (n.startsWith(":")) {
56205
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1214
                if (pseudoHeadersEnded) {
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1215
                    throw newException("Unexpected pseudo-header '%s'", n);
56205
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1216
                } else if (!PSEUDO_HEADERS.contains(n)) {
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1217
                    throw newException("Unknown pseudo-header '%s'", n);
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1218
                }
56205
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1219
            } else {
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1220
                pseudoHeadersEnded = true;
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1221
                if (!Utils.isValidName(n)) {
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1222
                    throw newException("Bad header name '%s'", n);
f4c9c5920141 http-client-branch: (HttpClient) deep(er) validation of pseudo-headers
prappo
parents: 56166
diff changeset
  1223
                }
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1224
            }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1225
            String v = value.toString();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1226
            if (!Utils.isValidValue(v)) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1227
                throw newException("Bad header value '%s'", v);
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1228
            }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1229
        }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1230
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1231
        private UncheckedIOException newException(String message, String header)
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1232
        {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1233
            return new UncheckedIOException(
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1234
                    new IOException(String.format(message, header)));
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1235
        }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1236
    }
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56094
diff changeset
  1237
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1238
    static final class ConnectionWindowUpdateSender extends WindowUpdateSender {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1239
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
  1240
        final int initialWindowSize;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1241
        public ConnectionWindowUpdateSender(Http2Connection connection,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1242
                                            int initialWindowSize) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1243
            super(connection, initialWindowSize);
48263
a559b7cd1dea 8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents: 48083
diff changeset
  1244
            this.initialWindowSize = initialWindowSize;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1245
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1246
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1247
        @Override
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1248
        int getStreamId() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1249
            return 0;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1250
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1251
    }
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1252
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1253
    /**
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1254
     * Thrown when https handshake negotiates http/1.1 alpn instead of h2
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1255
     */
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1256
    static final class ALPNException extends IOException {
56060
f09e8e9bbdd2 http-client-branch: fix serializable fields in internal ALPNException
chegar
parents: 56044
diff changeset
  1257
        private static final long serialVersionUID = 0L;
f09e8e9bbdd2 http-client-branch: fix serializable fields in internal ALPNException
chegar
parents: 56044
diff changeset
  1258
        final transient AbstractAsyncSSLConnection connection;
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1259
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
  1260
        ALPNException(String msg, AbstractAsyncSSLConnection connection) {
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1261
            super(msg);
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1262
            this.connection = connection;
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1263
        }
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1264
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents: 45530
diff changeset
  1265
        AbstractAsyncSSLConnection getConnection() {
44854
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1266
            return connection;
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1267
        }
5a486e0acd29 8175814: Update default HttpClient protocol version and optional request version
michaelm
parents: 43984
diff changeset
  1268
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
  1269
}