src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/internal/common/SSLFlowDelegate.java
author dfuchs
Mon, 04 Dec 2017 13:00:05 +0000
branchhttp-client-branch
changeset 55946 cfa4f84b7fcc
parent 55942 8d4770c22b63
child 55950 5e1707e5a254
permissions -rw-r--r--
http-client-branch: 8192962: issue with BUFFER_UNDERFLOW
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     1
/*
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     4
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    10
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    15
 * accompanied this code).
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    16
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    20
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    23
 * questions.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    24
 */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    25
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    26
package jdk.incubator.http.internal.common;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    27
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    28
import java.io.IOException;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    29
import java.lang.System.Logger.Level;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    30
import java.nio.ByteBuffer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    31
import java.util.concurrent.Executor;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    32
import java.util.concurrent.Flow;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    33
import java.util.concurrent.Flow.Subscriber;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    34
import java.util.List;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    35
import java.util.ArrayList;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    36
import java.util.Collections;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    37
import java.util.Iterator;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    38
import java.util.LinkedList;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    39
import java.util.concurrent.CompletableFuture;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    40
import java.util.concurrent.ConcurrentLinkedQueue;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    41
import java.util.concurrent.atomic.AtomicInteger;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    42
import javax.net.ssl.SSLEngine;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    43
import javax.net.ssl.SSLEngineResult;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    44
import javax.net.ssl.SSLEngineResult.HandshakeStatus;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    45
import javax.net.ssl.SSLEngineResult.Status;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    46
import javax.net.ssl.SSLException;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
    47
import jdk.incubator.http.internal.common.SubscriberWrapper.SchedulingAction;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    48
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    49
/**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    50
 * Implements SSL using two SubscriberWrappers.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    51
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    52
 * <p> Constructor takes two Flow.Subscribers: one that receives the network
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    53
 * data (after it has been encrypted by SSLFlowDelegate) data, and one that
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    54
 * receives the application data (before it has been encrypted by SSLFlowDelegate).
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    55
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    56
 * <p> Methods upstreamReader() and upstreamWriter() return the corresponding
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    57
 * Flow.Subscribers containing Flows for the encrypted/decrypted upstream data.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    58
 * See diagram below.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    59
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    60
 * <p> How Flow.Subscribers are used in this class, and where they come from:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    61
 * <pre>
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    62
 * {@code
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    63
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    64
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    65
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    66
 * --------->  data flow direction
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    67
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    68
 *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    69
 *                         +------------------+
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    70
 *        upstreamWriter   |                  | downWriter
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    71
 *        ---------------> |                  | ------------>
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    72
 *  obtained from this     |                  | supplied to constructor
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    73
 *                         | SSLFlowDelegate  |
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    74
 *        downReader       |                  | upstreamReader
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    75
 *        <--------------- |                  | <--------------
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    76
 * supplied to constructor |                  | obtained from this
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    77
 *                         +------------------+
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    78
 * }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    79
 * </pre>
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    80
 */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    81
public class SSLFlowDelegate {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    82
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    83
    static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    84
    final System.Logger debug =
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    85
            Utils.getDebugLogger(this::dbgString, DEBUG);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    86
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    87
    final Executor exec;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    88
    final Reader reader;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    89
    final Writer writer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    90
    final SSLEngine engine;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    91
    final String tubeName; // hack
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    92
    final CompletableFuture<Void> cf;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    93
    final CompletableFuture<String> alpnCF; // completes on initial handshake
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    94
    final static ByteBuffer SENTINEL = Utils.EMPTY_BYTEBUFFER;
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
    95
    volatile boolean close_notify_received;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    96
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    97
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    98
     * Creates an SSLFlowDelegate fed from two Flow.Subscribers. Each
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
    99
     * Flow.Subscriber requires an associated {@link CompletableFuture}
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   100
     * for errors that need to be signaled from downstream to upstream.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   101
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   102
    public SSLFlowDelegate(SSLEngine engine,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   103
                           Executor exec,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   104
                           Subscriber<? super List<ByteBuffer>> downReader,
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   105
                           Subscriber<? super List<ByteBuffer>> downWriter)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   106
    {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   107
        this.tubeName = String.valueOf(downWriter);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   108
        this.reader = new Reader();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   109
        this.writer = new Writer();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   110
        this.engine = engine;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   111
        this.exec = exec;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   112
        this.handshakeState = new AtomicInteger(NOT_HANDSHAKING);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   113
        this.cf = CompletableFuture.allOf(reader.completion(), writer.completion())
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   114
                                   .thenRun(this::normalStop);
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   115
        this.alpnCF = new MinimalFuture<>();
55798
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   116
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   117
        // connect the Reader to the downReader and the
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   118
        // Writer to the downWriter.
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   119
        connect(downReader, downWriter);
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   120
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   121
        //Monitor.add(this::monitor);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   122
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   123
55798
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   124
    /**
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   125
     * Returns true if the SSLFlowDelegate has detected a TLS
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   126
     * close_notify from the server.
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   127
     * @return true, if a close_notify was detected.
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   128
     */
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   129
    public boolean closeNotifyReceived() {
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   130
        return close_notify_received;
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   131
    }
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   132
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   133
    /**
55798
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   134
     * Connects the read sink (downReader) to the SSLFlowDelegate Reader,
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   135
     * and the write sink (downWriter) to the SSLFlowDelegate Writer.
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   136
     * Called from within the constructor. Overwritten by SSLTube.
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   137
     *
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   138
     * @param downReader  The left hand side read sink (typically, the
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   139
     *                    HttpConnection read subscriber).
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   140
     * @param downWriter  The right hand side write sink (typically
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   141
     *                    the SocketTube write subscriber).
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   142
     */
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   143
    void connect(Subscriber<? super List<ByteBuffer>> downReader,
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   144
                 Subscriber<? super List<ByteBuffer>> downWriter) {
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   145
        this.reader.subscribe(downReader);
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   146
        this.writer.subscribe(downWriter);
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   147
    }
fa84be3c77e4 http-client-branch: make SSLTube connect to SocketTube using FlowTube::connectFlows
dfuchs
parents: 55792
diff changeset
   148
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   149
   /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   150
    * Returns a CompletableFuture<String> which completes after
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   151
    * the initial handshake completes, and which contains the negotiated
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   152
    * alpn.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   153
    */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   154
    public CompletableFuture<String> alpn() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   155
        return alpnCF;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   156
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   157
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   158
    private void setALPN() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   159
        // Handshake is finished. So, can retrieve the ALPN now
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   160
        if (alpnCF.isDone())
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   161
            return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   162
        String alpn = engine.getApplicationProtocol();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   163
        debug.log(Level.DEBUG, "setALPN = %s", alpn);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   164
        alpnCF.complete(alpn);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   165
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   166
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   167
    public String monitor() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   168
        StringBuilder sb = new StringBuilder();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   169
        sb.append("SSL: HS state: " + states(handshakeState));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   170
        sb.append(" Engine state: " + engine.getHandshakeStatus().toString());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   171
        sb.append(" LL : ");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   172
        synchronized(stateList) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   173
            for (String s: stateList) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   174
                sb.append(s).append(" ");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   175
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   176
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   177
        sb.append("\r\n");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   178
        sb.append("Reader:: ").append(reader.toString());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   179
        sb.append("\r\n");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   180
        sb.append("Writer:: ").append(writer.toString());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   181
        sb.append("\r\n===================================");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   182
        return sb.toString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   183
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   184
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   185
    protected SchedulingAction enterReadScheduling() {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   186
        return SchedulingAction.CONTINUE;
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   187
    }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   188
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   189
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   190
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   191
     * Processing function for incoming data. Pass it thru SSLEngine.unwrap().
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   192
     * Any decrypted buffers returned to be passed downstream.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   193
     * Status codes:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   194
     *     NEED_UNWRAP: do nothing. Following incoming data will contain
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   195
     *                  any required handshake data
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   196
     *     NEED_WRAP: call writer.addData() with empty buffer
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   197
     *     NEED_TASK: delegate task to executor
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   198
     *     BUFFER_OVERFLOW: allocate larger output buffer. Repeat unwrap
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   199
     *     BUFFER_UNDERFLOW: keep buffer and wait for more data
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   200
     *     OK: return generated buffers.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   201
     *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   202
     * Upstream subscription strategy is to try and keep no more than
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   203
     * TARGET_BUFSIZE bytes in readBuf
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   204
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   205
    class Reader extends SubscriberWrapper {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   206
        final SequentialScheduler scheduler;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   207
        static final int TARGET_BUFSIZE = 16 * 1024;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   208
        volatile ByteBuffer readBuf;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   209
        volatile boolean completing = false;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   210
        final Object readBufferLock = new Object();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   211
        final System.Logger debugr =
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   212
            Utils.getDebugLogger(this::dbgString, DEBUG);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   213
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   214
        class ReaderDownstreamPusher implements Runnable {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   215
            @Override public void run() { processData(); }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   216
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   217
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   218
        Reader() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   219
            super();
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   220
            scheduler = SequentialScheduler.synchronizedScheduler(
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   221
                                                new ReaderDownstreamPusher());
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   222
            this.readBuf = ByteBuffer.allocate(1024);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   223
            readBuf.limit(0); // keep in read mode
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   224
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   225
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   226
        protected SchedulingAction enterScheduling() {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   227
            return enterReadScheduling();
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   228
        }
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   229
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   230
        public final String dbgString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   231
            return "SSL Reader(" + tubeName + ")";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   232
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   233
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   234
        /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   235
         * entry point for buffers delivered from upstream Subscriber
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   236
         */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   237
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   238
        public void incoming(List<ByteBuffer> buffers, boolean complete) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   239
            debugr.log(Level.DEBUG, () -> "Adding " + Utils.remaining(buffers)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   240
                        + " bytes to read buffer");
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   241
            addToReadBuf(buffers, complete);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   242
            scheduler.runOrSchedule();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   243
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   244
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   245
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   246
        public String toString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   247
            return "READER: " + super.toString() + " readBuf: " + readBuf.toString()
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   248
                    + " count: " + count.toString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   249
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   250
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   251
        private void reallocReadBuf() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   252
            int sz = readBuf.capacity();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   253
            ByteBuffer newb = ByteBuffer.allocate(sz*2);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   254
            readBuf.flip();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   255
            Utils.copy(readBuf, newb);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   256
            readBuf = newb;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   257
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   258
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   259
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   260
        protected long upstreamWindowUpdate(long currentWindow, long downstreamQsize) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   261
            if (readBuf.remaining() > TARGET_BUFSIZE) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   262
                return 0;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   263
            } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   264
                return super.upstreamWindowUpdate(currentWindow, downstreamQsize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   265
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   266
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   267
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   268
        // readBuf is kept ready for reading outside of this method
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   269
        private void addToReadBuf(List<ByteBuffer> buffers, boolean complete) {
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   270
            synchronized (readBufferLock) {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   271
                for (ByteBuffer buf : buffers) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   272
                    readBuf.compact();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   273
                    while (readBuf.remaining() < buf.remaining())
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   274
                        reallocReadBuf();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   275
                    readBuf.put(buf);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   276
                    readBuf.flip();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   277
                }
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   278
                if (complete) {
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   279
                    this.completing = complete;
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   280
                }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   281
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   282
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   283
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   284
        void schedule() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   285
            scheduler.runOrSchedule();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   286
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   287
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   288
        void stop() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   289
            debugr.log(Level.DEBUG, "stop");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   290
            scheduler.stop();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   291
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   292
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   293
        AtomicInteger count = new AtomicInteger(0);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   294
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   295
        // work function where it all happens
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   296
        void processData() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   297
            try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   298
                debugr.log(Level.DEBUG, () -> "processData: " + readBuf.remaining()
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   299
                           + " bytes to unwrap "
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   300
                           + states(handshakeState)
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   301
                           + ", " + engine.getHandshakeStatus());
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   302
                int len;
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   303
                boolean completing = false;
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   304
                while ((len = readBuf.remaining()) > 0) {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   305
                    boolean handshaking = false;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   306
                    try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   307
                        EngineResult result;
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   308
                        synchronized (readBufferLock) {
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   309
                            completing = this.completing;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   310
                            result = unwrapBuffer(readBuf);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   311
                            debugr.log(Level.DEBUG, "Unwrapped: %s", result.result);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   312
                        }
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   313
                        if (result.bytesProduced() > 0) {
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   314
                            debugr.log(Level.DEBUG, "sending %d", result.bytesProduced());
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   315
                            count.addAndGet(result.bytesProduced());
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   316
                            outgoing(result.destBuffer, false);
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   317
                        }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   318
                        if (result.status() == Status.BUFFER_UNDERFLOW) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   319
                            debugr.log(Level.DEBUG, "BUFFER_UNDERFLOW");
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   320
                            // not enough data in the read buffer...
55946
cfa4f84b7fcc http-client-branch: 8192962: issue with BUFFER_UNDERFLOW
dfuchs
parents: 55942
diff changeset
   321
                            requestMore();
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   322
                            synchronized (readBufferLock) {
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   323
                                // check if we have received some data
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   324
                                if (readBuf.remaining() > len) continue;
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   325
                                return;
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   326
                            }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   327
                        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   328
                        if (completing && result.status() == Status.CLOSED) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   329
                            debugr.log(Level.DEBUG, "Closed: completing");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   330
                            outgoing(Utils.EMPTY_BB_LIST, true);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   331
                            return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   332
                        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   333
                        if (result.handshaking() && !completing) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   334
                            debugr.log(Level.DEBUG, "handshaking");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   335
                            doHandshake(result, READER);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   336
                            resumeActivity();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   337
                            handshaking = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   338
                        } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   339
                            if ((handshakeState.getAndSet(NOT_HANDSHAKING) & ~DOING_TASKS) == HANDSHAKING) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   340
                                setALPN();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   341
                                handshaking = false;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   342
                                resumeActivity();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   343
                            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   344
                        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   345
                    } catch (IOException ex) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   346
                        errorCommon(ex);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   347
                        handleError(ex);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   348
                    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   349
                    if (handshaking && !completing)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   350
                        return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   351
                }
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   352
                if (!completing) {
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   353
                    synchronized (readBufferLock) {
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   354
                        completing = this.completing && !readBuf.hasRemaining();
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   355
                    }
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   356
                }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   357
                if (completing) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   358
                    debugr.log(Level.DEBUG, "completing");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   359
                    // Complete the alpnCF, if not already complete, regardless of
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   360
                    // whether or not the ALPN is available, there will be no more
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   361
                    // activity.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   362
                    setALPN();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   363
                    outgoing(Utils.EMPTY_BB_LIST, true);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   364
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   365
            } catch (Throwable ex) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   366
                errorCommon(ex);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   367
                handleError(ex);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   368
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   369
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   370
    }
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   371
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   372
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   373
     * Returns a CompletableFuture which completes after all activity
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   374
     * in the delegate is terminated (whether normally or exceptionally).
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   375
     *
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   376
     * @return
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   377
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   378
    public CompletableFuture<Void> completion() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   379
        return cf;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   380
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   381
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   382
    public interface Monitorable {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   383
        public String getInfo();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   384
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   385
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   386
    public static class Monitor extends Thread {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   387
        final List<Monitorable> list;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   388
        static Monitor themon;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   389
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   390
        static {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   391
            themon = new Monitor();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   392
            themon.start(); // uncomment to enable Monitor
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   393
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   394
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   395
        Monitor() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   396
            super("Monitor");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   397
            setDaemon(true);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   398
            list = Collections.synchronizedList(new LinkedList<>());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   399
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   400
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   401
        void addTarget(Monitorable o) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   402
            list.add(o);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   403
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   404
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   405
        public static void add(Monitorable o) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   406
            themon.addTarget(o);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   407
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   408
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   409
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   410
        public void run() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   411
            System.out.println("Monitor starting");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   412
            while (true) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   413
                try {Thread.sleep(20*1000); } catch (Exception e) {}
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   414
                synchronized (list) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   415
                    for (Monitorable o : list) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   416
                        System.out.println(o.getInfo());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   417
                        System.out.println("-------------------------");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   418
                    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   419
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   420
                System.out.println("--o-o-o-o-o-o-o-o-o-o-o-o-o-o-");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   421
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   422
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   423
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   424
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   425
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   426
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   427
     * Processing function for outgoing data. Pass it thru SSLEngine.wrap()
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   428
     * Any encrypted buffers generated are passed downstream to be written.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   429
     * Status codes:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   430
     *     NEED_UNWRAP: call reader.addData() with empty buffer
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   431
     *     NEED_WRAP: call addData() with empty buffer
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   432
     *     NEED_TASK: delegate task to executor
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   433
     *     BUFFER_OVERFLOW: allocate larger output buffer. Repeat wrap
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   434
     *     BUFFER_UNDERFLOW: shouldn't happen on writing side
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   435
     *     OK: return generated buffers
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   436
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   437
    class Writer extends SubscriberWrapper {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   438
        final SequentialScheduler scheduler;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   439
        // queues of buffers received from upstream waiting
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   440
        // to be processed by the SSLEngine
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   441
        final List<ByteBuffer> writeList;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   442
        final System.Logger debugw =
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   443
            Utils.getDebugLogger(this::dbgString, DEBUG);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   444
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   445
        class WriterDownstreamPusher extends SequentialScheduler.CompleteRestartableTask {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   446
            @Override public void run() { processData(); }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   447
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   448
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   449
        Writer() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   450
            super();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   451
            writeList = Collections.synchronizedList(new LinkedList<>());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   452
            scheduler = new SequentialScheduler(new WriterDownstreamPusher());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   453
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   454
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   455
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   456
        protected void incoming(List<ByteBuffer> buffers, boolean complete) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   457
            assert complete ? buffers ==  Utils.EMPTY_BB_LIST : true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   458
            assert buffers != Utils.EMPTY_BB_LIST ? complete == false : true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   459
            if (complete) {
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   460
                debugw.log(Level.DEBUG, "adding SENTINEL");
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   461
                writeList.add(SENTINEL);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   462
            } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   463
                writeList.addAll(buffers);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   464
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   465
            debugw.log(Level.DEBUG, () -> "added " + buffers.size()
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   466
                        + " (" + Utils.remaining(buffers)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   467
                        + " bytes) to the writeList");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   468
            scheduler.runOrSchedule();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   469
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   470
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   471
        public final String dbgString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   472
            return "SSL Writer(" + tubeName + ")";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   473
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   474
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   475
        protected void onSubscribe() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   476
            doHandshake(EngineResult.INIT, INIT);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   477
            resumeActivity();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   478
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   479
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   480
        void schedule() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   481
            scheduler.runOrSchedule();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   482
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   483
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   484
        void stop() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   485
            debugw.log(Level.DEBUG, "stop");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   486
            scheduler.stop();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   487
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   488
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   489
        @Override
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   490
        public boolean closing() {
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   491
            return closeNotifyReceived();
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   492
        }
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   493
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   494
        private boolean isCompleting() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   495
            synchronized(writeList) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   496
                int lastIndex = writeList.size() - 1;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   497
                if (lastIndex < 0)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   498
                    return false;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   499
                return writeList.get(lastIndex) == SENTINEL;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   500
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   501
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   502
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   503
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   504
        protected long upstreamWindowUpdate(long currentWindow, long downstreamQsize) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   505
            if (writeList.size() > 10)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   506
                return 0;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   507
            else
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   508
                return super.upstreamWindowUpdate(currentWindow, downstreamQsize);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   509
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   510
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   511
        private boolean hsTriggered() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   512
            synchronized(writeList) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   513
                for (ByteBuffer b : writeList)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   514
                    if (b == HS_TRIGGER)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   515
                        return true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   516
                return false;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   517
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   518
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   519
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   520
        private void processData() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   521
            boolean completing = isCompleting();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   522
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   523
            try {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   524
                debugw.log(Level.DEBUG, () -> "processData(" + Utils.remaining(writeList) + ")");
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   525
                while (Utils.remaining(writeList) > 0 || hsTriggered()
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   526
                        || needWrap()) {
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   527
                    ByteBuffer[] outbufs = writeList.toArray(Utils.EMPTY_BB_ARRAY);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   528
                    EngineResult result = wrapBuffers(outbufs);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   529
                    debugw.log(Level.DEBUG, "wrapBuffer returned %s", result.result);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   530
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   531
                    if (result.status() == Status.CLOSED) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   532
                        if (result.bytesProduced() <= 0)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   533
                            return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   534
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   535
                        completing = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   536
                        // There could still be some outgoing data in outbufs.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   537
                        writeList.add(SENTINEL);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   538
                    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   539
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   540
                    boolean handshaking = false;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   541
                    if (result.handshaking()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   542
                        debugw.log(Level.DEBUG, "handshaking");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   543
                        doHandshake(result, WRITER);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   544
                        handshaking = true;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   545
                    } else {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   546
                        if ((handshakeState.getAndSet(NOT_HANDSHAKING) & ~DOING_TASKS) == HANDSHAKING) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   547
                            setALPN();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   548
                            resumeActivity();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   549
                        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   550
                    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   551
                    cleanList(writeList); // tidy up the source list
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   552
                    sendResultBytes(result);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   553
                    if (handshaking && !completing) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   554
                        if (writeList.isEmpty() && !result.needUnwrap()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   555
                            writer.addData(HS_TRIGGER);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   556
                        }
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   557
                        if (needWrap()) continue;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   558
                        return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   559
                    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   560
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   561
                if (completing && Utils.remaining(writeList) == 0) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   562
                    /*
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   563
                    System.out.println("WRITER DOO 3");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   564
                    engine.closeOutbound();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   565
                    EngineResult result = wrapBuffers(Utils.EMPTY_BB_ARRAY);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   566
                    sendResultBytes(result);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   567
                    */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   568
                    outgoing(Utils.EMPTY_BB_LIST, true);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   569
                    return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   570
                }
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   571
                if (writeList.isEmpty() && needWrap()) {
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   572
                    writer.addData(HS_TRIGGER);
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   573
                }
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   574
            } catch (Throwable ex) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   575
                handleError(ex);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   576
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   577
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   578
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   579
        private boolean needWrap() {
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   580
            return engine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP;
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   581
        }
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   582
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   583
        private void sendResultBytes(EngineResult result) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   584
            if (result.bytesProduced() > 0) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   585
                debugw.log(Level.DEBUG, "Sending %d bytes downstream",
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   586
                           result.bytesProduced());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   587
                outgoing(result.destBuffer, false);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   588
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   589
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   590
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   591
        @Override
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   592
        public String toString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   593
            return "WRITER: " + super.toString() +
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   594
                    " writeList size " + Integer.toString(writeList.size());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   595
                    //" writeList: " + writeList.toString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   596
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   597
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   598
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   599
    private void handleError(Throwable t) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   600
        debug.log(Level.DEBUG, "handleError", t);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   601
        cf.completeExceptionally(t);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   602
        // no-op if already completed
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   603
        alpnCF.completeExceptionally(t);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   604
        reader.stop();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   605
        writer.stop();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   606
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   607
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   608
    private void normalStop() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   609
        reader.stop();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   610
        writer.stop();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   611
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   612
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   613
    private void cleanList(List<ByteBuffer> l) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   614
        synchronized (l) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   615
            Iterator<ByteBuffer> iter = l.iterator();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   616
            while (iter.hasNext()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   617
                ByteBuffer b = iter.next();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   618
                if (!b.hasRemaining()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   619
                    iter.remove();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   620
                }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   621
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   622
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   623
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   624
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   625
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   626
     * States for handshake. We avoid races when accessing/updating the AtomicInt
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   627
     * because updates always schedule an additional call to both the read()
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   628
     * and write() functions.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   629
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   630
    private static final int NOT_HANDSHAKING = 0;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   631
    private static final int HANDSHAKING = 1;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   632
    private static final int INIT = 2;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   633
    private static final int DOING_TASKS = 4; // bit added to above state
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   634
    private static final ByteBuffer HS_TRIGGER = ByteBuffer.allocate(0);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   635
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   636
    private static final int READER = 1;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   637
    private static final int WRITER = 2;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   638
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   639
    private static String states(AtomicInteger state) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   640
        int s = state.get();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   641
        StringBuilder sb = new StringBuilder();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   642
        int x = s & ~DOING_TASKS;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   643
        switch (x) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   644
            case NOT_HANDSHAKING:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   645
                sb.append(" NOT_HANDSHAKING ");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   646
                break;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   647
            case HANDSHAKING:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   648
                sb.append(" HANDSHAKING ");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   649
                break;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   650
            case INIT:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   651
                sb.append(" INIT ");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   652
                break;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   653
            default:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   654
                throw new InternalError();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   655
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   656
        if ((s & DOING_TASKS) > 0)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   657
            sb.append("|DOING_TASKS");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   658
        return sb.toString();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   659
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   660
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   661
    private void resumeActivity() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   662
        reader.schedule();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   663
        writer.schedule();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   664
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   665
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   666
    final AtomicInteger handshakeState;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   667
    final ConcurrentLinkedQueue<String> stateList = new ConcurrentLinkedQueue<>();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   668
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   669
    private void doHandshake(EngineResult r, int caller) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   670
        int s = handshakeState.getAndAccumulate(HANDSHAKING, (current, update) -> update | (current & DOING_TASKS));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   671
        stateList.add(r.handshakeStatus().toString());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   672
        stateList.add(Integer.toString(caller));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   673
        switch (r.handshakeStatus()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   674
            case NEED_TASK:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   675
                if ((s & DOING_TASKS) > 0) // someone else was doing tasks
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   676
                    return;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   677
                List<Runnable> tasks = obtainTasks();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   678
                executeTasks(tasks);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   679
                break;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   680
            case NEED_WRAP:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   681
                writer.addData(HS_TRIGGER);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   682
                break;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   683
            case NEED_UNWRAP:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   684
            case NEED_UNWRAP_AGAIN:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   685
                // do nothing else
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   686
                break;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   687
            default:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   688
                throw new InternalError("Unexpected handshake status:"
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   689
                                        + r.handshakeStatus());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   690
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   691
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   692
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   693
    private List<Runnable> obtainTasks() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   694
        List<Runnable> l = new ArrayList<>();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   695
        Runnable r;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   696
        while ((r = engine.getDelegatedTask()) != null) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   697
            l.add(r);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   698
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   699
        return l;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   700
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   701
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   702
    private void executeTasks(List<Runnable> tasks) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   703
        exec.execute(() -> {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   704
            handshakeState.getAndUpdate((current) -> current | DOING_TASKS);
55942
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   705
            List<Runnable> nextTasks = tasks;
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   706
            do {
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   707
                try {
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   708
                    nextTasks.forEach((r) -> {
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   709
                        r.run();
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   710
                    });
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   711
                    if (engine.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   712
                        nextTasks = obtainTasks();
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   713
                    } else break;
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   714
                } catch (Throwable t) {
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   715
                    handleError(t);
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   716
                }
8d4770c22b63 http-client-barnch: fixed a few issues discovered while stress testing and a race condition in SSLFlowDelegate
dfuchs
parents: 55909
diff changeset
   717
            } while(true);
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   718
            handshakeState.getAndUpdate((current) -> current & ~DOING_TASKS);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   719
            writer.addData(HS_TRIGGER);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   720
            resumeActivity();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   721
        });
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   722
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   723
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   724
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   725
    EngineResult unwrapBuffer(ByteBuffer src) throws IOException {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   726
        ByteBuffer dst = getAppBuffer();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   727
        while (true) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   728
            SSLEngineResult sslResult = engine.unwrap(src, dst);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   729
            switch (sslResult.getStatus()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   730
                case BUFFER_OVERFLOW:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   731
                    // may happen only if app size buffer was changed.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   732
                    // get it again if app buffer size changed
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   733
                    int appSize = engine.getSession().getApplicationBufferSize();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   734
                    ByteBuffer b = ByteBuffer.allocate(appSize + dst.position());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   735
                    dst.flip();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   736
                    b.put(dst);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   737
                    dst = b;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   738
                    break;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   739
                case CLOSED:
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   740
                    return doClosure(new EngineResult(sslResult));
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   741
                case BUFFER_UNDERFLOW:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   742
                    // handled implicitly by compaction/reallocation of readBuf
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   743
                    return new EngineResult(sslResult);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   744
                case OK:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   745
                     dst.flip();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   746
                     return new EngineResult(sslResult, dst);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   747
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   748
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   749
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   750
55792
0936888d5a4a http-client-branch: (cleanup) unused imports, unused methods, removed 1 class; typos;
prappo
parents: 55768
diff changeset
   751
    // FIXME: acknowledge a received CLOSE request from peer
55909
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   752
    EngineResult doClosure(EngineResult r) throws IOException {
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   753
        debug.log(Level.DEBUG,
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   754
                "doClosure(%s): %s [isOutboundDone: %s, isInboundDone: %s]",
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   755
                r.result, engine.getHandshakeStatus(),
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   756
                engine.isOutboundDone(), engine.isInboundDone());
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   757
        if (engine.getHandshakeStatus() == HandshakeStatus.NEED_WRAP) {
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   758
            // we have received TLS close_notify and need to send
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   759
            // an acknowledgement back. We're calling doHandshake
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   760
            // to finish the close handshake.
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   761
            if (engine.isInboundDone() && !engine.isOutboundDone()) {
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   762
                debug.log(Level.DEBUG, "doClosure: close_notify received");
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   763
                close_notify_received = true;
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   764
                doHandshake(r, READER);
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   765
            }
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   766
        }
583695a0ed6a http-client-branch: review comment: SSLTubeTest is ignored (and some associated fix and cleanup)
dfuchs
parents: 55798
diff changeset
   767
        return r;
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   768
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   769
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   770
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   771
     * Returns the upstream Flow.Subscriber of the reading (incoming) side.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   772
     * This flow must be given the encrypted data read from upstream (eg socket)
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   773
     * before it is decrypted.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   774
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   775
    public Flow.Subscriber<List<ByteBuffer>> upstreamReader() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   776
        return reader;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   777
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   778
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   779
    /**
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   780
     * Returns the upstream Flow.Subscriber of the writing (outgoing) side.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   781
     * This flow contains the plaintext data before it is encrypted.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   782
     */
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   783
    public Flow.Subscriber<List<ByteBuffer>> upstreamWriter() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   784
        return writer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   785
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   786
55768
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   787
    public boolean resumeReader() {
8674257c75ce http-client-branch: SSLTube should call onSubscribe in the flow
dfuchs
parents: 55763
diff changeset
   788
        return reader.signalScheduling();
55763
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   789
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   790
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   791
    public void resetReaderDemand() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   792
        reader.resetDownstreamDemand();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   793
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   794
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   795
    static class EngineResult {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   796
        final SSLEngineResult result;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   797
        final ByteBuffer destBuffer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   798
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   799
        // normal result
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   800
        EngineResult(SSLEngineResult result) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   801
            this(result, null);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   802
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   803
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   804
        EngineResult(SSLEngineResult result, ByteBuffer destBuffer) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   805
            this.result = result;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   806
            this.destBuffer = destBuffer;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   807
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   808
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   809
        // Special result used to trigger handshaking in constructor
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   810
        static EngineResult INIT =
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   811
            new EngineResult(
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   812
                new SSLEngineResult(SSLEngineResult.Status.OK, HandshakeStatus.NEED_WRAP, 0, 0));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   813
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   814
        boolean handshaking() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   815
            HandshakeStatus s = result.getHandshakeStatus();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   816
            return s != HandshakeStatus.FINISHED
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   817
                   && s != HandshakeStatus.NOT_HANDSHAKING
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   818
                   && result.getStatus() != Status.CLOSED;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   819
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   820
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   821
        boolean needUnwrap() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   822
            HandshakeStatus s = result.getHandshakeStatus();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   823
            return s == HandshakeStatus.NEED_UNWRAP;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   824
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   825
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   826
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   827
        int bytesConsumed() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   828
            return result.bytesConsumed();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   829
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   830
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   831
        int bytesProduced() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   832
            return result.bytesProduced();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   833
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   834
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   835
        SSLEngineResult.HandshakeStatus handshakeStatus() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   836
            return result.getHandshakeStatus();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   837
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   838
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   839
        SSLEngineResult.Status status() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   840
            return result.getStatus();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   841
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   842
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   843
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   844
    public ByteBuffer getNetBuffer() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   845
        return ByteBuffer.allocate(engine.getSession().getPacketBufferSize());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   846
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   847
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   848
    private ByteBuffer getAppBuffer() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   849
        return ByteBuffer.allocate(engine.getSession().getApplicationBufferSize());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   850
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   851
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   852
    final String dbgString() {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   853
        return "SSLFlowDelegate(" + tubeName + ")";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   854
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   855
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   856
    @SuppressWarnings("fallthrough")
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   857
    EngineResult wrapBuffers(ByteBuffer[] src) throws SSLException {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   858
        debug.log(Level.DEBUG, () -> "wrapping "
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   859
                    + Utils.remaining(src) + " bytes");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   860
        ByteBuffer dst = getNetBuffer();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   861
        while (true) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   862
            SSLEngineResult sslResult = engine.wrap(src, dst);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   863
            debug.log(Level.DEBUG, () -> "SSLResult: " + sslResult);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   864
            switch (sslResult.getStatus()) {
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   865
                case BUFFER_OVERFLOW:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   866
                    // Shouldn't happen. We allocated buffer with packet size
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   867
                    // get it again if net buffer size was changed
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   868
                    debug.log(Level.DEBUG, "BUFFER_OVERFLOW");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   869
                    int appSize = engine.getSession().getApplicationBufferSize();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   870
                    ByteBuffer b = ByteBuffer.allocate(appSize + dst.position());
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   871
                    dst.flip();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   872
                    b.put(dst);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   873
                    dst = b;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   874
                    break; // try again
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   875
                case CLOSED:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   876
                    debug.log(Level.DEBUG, "CLOSED");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   877
                    // fallthrough. There could be some remaining data in dst.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   878
                    // CLOSED will be handled by the caller.
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   879
                case OK:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   880
                    dst.flip();
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   881
                    final ByteBuffer dest = dst;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   882
                    debug.log(Level.DEBUG, () -> "OK => produced: "
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   883
                                           + dest.remaining()
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   884
                                           + " not wrapped: "
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   885
                                           + Utils.remaining(src));
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   886
                    return new EngineResult(sslResult, dest);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   887
                case BUFFER_UNDERFLOW:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   888
                    // Shouldn't happen.  Doesn't returns when wrap()
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   889
                    // underflow handled externally
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   890
                    // assert false : "Buffer Underflow";
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   891
                    debug.log(Level.DEBUG, "BUFFER_UNDERFLOW");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   892
                    return new EngineResult(sslResult);
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   893
                default:
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   894
                    debug.log(Level.DEBUG, "ASSERT");
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   895
                    assert false;
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   896
            }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   897
        }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   898
    }
634d8e14c172 http-client-branch: intial load from jdk10/sandbox
chegar
parents:
diff changeset
   899
}