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