jdk/src/java.httpclient/share/classes/java/net/http/HttpClientImpl.java
author chegar
Mon, 18 Jul 2016 15:34:22 +0100
changeset 39730 196f4e25d9f5
parent 38322 f6f9d3ec14ba
permissions -rw-r--r--
8160993: Fix headers in the java/net/http package Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     1
/*
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     4
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    10
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    15
 * accompanied this code).
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    16
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    20
 *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
39730
196f4e25d9f5 8160993: Fix headers in the java/net/http package
chegar
parents: 38322
diff changeset
    23
 * questions.
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    24
 */
39730
196f4e25d9f5 8160993: Fix headers in the java/net/http package
chegar
parents: 38322
diff changeset
    25
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    26
package java.net.http;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    27
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    28
import javax.net.ssl.SSLContext;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    29
import javax.net.ssl.SSLParameters;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    30
import java.io.IOException;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    31
import java.net.Authenticator;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    32
import java.net.CookieManager;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    33
import java.net.ProxySelector;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    34
import java.net.URI;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    35
import java.nio.ByteBuffer;
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
    36
import java.nio.channels.ClosedChannelException;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    37
import java.nio.channels.SelectableChannel;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    38
import java.nio.channels.SelectionKey;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    39
import java.nio.channels.Selector;
38322
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
    40
import java.nio.channels.SocketChannel;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    41
import java.security.NoSuchAlgorithmException;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    42
import java.util.ArrayList;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    43
import java.util.Iterator;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    44
import java.util.LinkedList;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    45
import java.util.List;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    46
import java.util.ListIterator;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    47
import java.util.Optional;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    48
import java.util.Set;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    49
import java.util.concurrent.ExecutorService;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    50
import java.util.concurrent.Executors;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    51
import java.util.concurrent.ThreadFactory;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    52
import java.util.stream.Stream;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    53
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    54
import static java.net.http.Utils.BUFSIZE;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    55
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    56
/**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    57
 * Client implementation. Contains all configuration information and also
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    58
 * the selector manager thread which allows async events to be registered
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    59
 * and delivered when they occur. See AsyncEvent.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    60
 */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    61
class HttpClientImpl extends HttpClient implements BufferHandler {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    62
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    63
    private static final ThreadFactory defaultFactory =
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    64
            (r -> new Thread(null, r, "HttpClient_worker", 0, true));
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
    65
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    66
    private final CookieManager cookieManager;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    67
    private final Redirect followRedirects;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    68
    private final ProxySelector proxySelector;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    69
    private final Authenticator authenticator;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    70
    private final Version version;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    71
    private boolean pipelining = false;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    72
    private final ConnectionPool connections;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    73
    private final ExecutorWrapper executor;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    74
    // Security parameters
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    75
    private final SSLContext sslContext;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    76
    private final SSLParameters sslParams;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    77
    private final SelectorManager selmgr;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    78
    private final FilterFactory filters;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    79
    private final Http2ClientImpl client2;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    80
    private final LinkedList<TimeoutEvent> timeouts;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    81
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    82
    public static HttpClientImpl create(HttpClientBuilderImpl builder) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    83
        HttpClientImpl impl = new HttpClientImpl(builder);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    84
        impl.start();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    85
        return impl;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    86
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    87
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    88
    private HttpClientImpl(HttpClientBuilderImpl builder) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    89
        if (builder.sslContext == null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    90
            try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    91
                sslContext = SSLContext.getDefault();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    92
            } catch (NoSuchAlgorithmException ex) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    93
                throw new InternalError(ex);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    94
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    95
        } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    96
            sslContext = builder.sslContext;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    97
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    98
        ExecutorService ex = builder.executor;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    99
        if (ex == null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   100
            ex = Executors.newCachedThreadPool((r) -> {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   101
                Thread t = defaultFactory.newThread(r);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   102
                t.setDaemon(true);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   103
                return t;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   104
            });
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   105
        } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   106
            ex = builder.executor;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   107
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   108
        client2 = new Http2ClientImpl(this);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   109
        executor = ExecutorWrapper.wrap(ex);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   110
        cookieManager = builder.cookieManager;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   111
        followRedirects = builder.followRedirects == null ?
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   112
                Redirect.NEVER : builder.followRedirects;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   113
        this.proxySelector = builder.proxy;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   114
        authenticator = builder.authenticator;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   115
        version = builder.version;
37799
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   116
        if (builder.sslParams == null)
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   117
            sslParams = getDefaultParams(sslContext);
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   118
        else
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   119
            sslParams = builder.sslParams;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   120
        connections = new ConnectionPool();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   121
        connections.start();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   122
        timeouts = new LinkedList<>();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   123
        try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   124
            selmgr = new SelectorManager();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   125
        } catch (IOException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   126
            // unlikely
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   127
            throw new InternalError(e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   128
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   129
        selmgr.setDaemon(true);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   130
        filters = new FilterFactory();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   131
        initFilters();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   132
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   133
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   134
    private void start() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   135
        selmgr.start();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   136
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   137
37799
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   138
    private static SSLParameters getDefaultParams(SSLContext ctx) {
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   139
        SSLParameters params = ctx.getSupportedSSLParameters();
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   140
        params.setProtocols(new String[]{"TLSv1.2"});
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   141
        return params;
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   142
    }
635c430d5a99 8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents: 37720
diff changeset
   143
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   144
    /**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   145
     * Wait for activity on given exchange (assuming blocking = false).
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   146
     * It's a no-op if blocking = true. In particular, the following occurs
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   147
     * in the SelectorManager thread.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   148
     *
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   149
     *  1) mark the connection non-blocking
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   150
     *  2) add to selector
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   151
     *  3) If selector fires for this exchange then
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   152
     *  4)   - mark connection as blocking
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   153
     *  5)   - call AsyncEvent.handle()
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   154
     *
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   155
     * If exchange needs to block again, then call registerEvent() again
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   156
     */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   157
    void registerEvent(AsyncEvent exchange) throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   158
        selmgr.register(exchange);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   159
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   160
38322
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   161
    /**
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   162
     * Only used from RawChannel to disconnect the channel from
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   163
     * the selector
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   164
     */
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   165
    void cancelRegistration(SocketChannel s) {
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   166
        selmgr.cancel(s);
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   167
    }
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   168
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   169
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   170
    Http2ClientImpl client2() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   171
        return client2;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   172
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   173
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   174
    /**
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   175
     * We keep one size of buffer on free list. That size may increase
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   176
     * depending on demand. If that happens we dispose of free buffers
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   177
     * that are smaller than new size.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   178
     */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   179
    private final LinkedList<ByteBuffer> freelist = new LinkedList<>();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   180
    int currentSize = BUFSIZE;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   181
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   182
    @Override
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   183
    public synchronized ByteBuffer getBuffer(int size) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   184
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   185
        ByteBuffer buf;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   186
        if (size == -1)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   187
            size = currentSize;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   188
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   189
        if (size > currentSize)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   190
            currentSize = size;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   191
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   192
        while (!freelist.isEmpty()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   193
            buf = freelist.removeFirst();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   194
            if (buf.capacity() < currentSize)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   195
                continue;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   196
            buf.clear();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   197
            return buf;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   198
        }
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   199
        return ByteBuffer.allocate(size);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   200
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   201
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   202
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   203
    public synchronized void returnBuffer(ByteBuffer buffer) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   204
        freelist.add(buffer);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   205
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   206
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   207
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   208
    public synchronized void setMinBufferSize(int n) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   209
        currentSize = Math.max(n, currentSize);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   210
    }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   211
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   212
    // Main loop for this client's selector
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   213
    private final class SelectorManager extends Thread {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   214
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   215
        private final Selector selector;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   216
        private volatile boolean closed;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   217
        private final List<AsyncEvent> readyList;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   218
        private final List<AsyncEvent> registrations;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   219
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   220
        SelectorManager() throws IOException {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   221
            super(null, null, "SelectorManager", 0, false);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   222
            readyList = new ArrayList<>();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   223
            registrations = new ArrayList<>();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   224
            selector = Selector.open();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   225
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   226
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   227
        // This returns immediately. So caller not allowed to send/receive
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   228
        // on connection.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   229
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   230
        synchronized void register(AsyncEvent e) throws IOException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   231
            registrations.add(e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   232
            selector.wakeup();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   233
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   234
38322
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   235
        synchronized void cancel(SocketChannel e) {
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   236
            SelectionKey key = e.keyFor(selector);
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   237
            if (key != null)
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   238
                key.cancel();
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   239
            selector.wakeup();
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   240
        }
f6f9d3ec14ba 8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents: 37799
diff changeset
   241
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   242
        void wakeupSelector() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   243
            selector.wakeup();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   244
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   245
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   246
        synchronized void shutdown() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   247
            closed = true;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   248
            try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   249
                selector.close();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   250
            } catch (IOException ignored) { }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   251
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   252
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   253
        @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   254
        public void run() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   255
            try {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   256
                while (!Thread.currentThread().isInterrupted()) {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   257
                    synchronized (this) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   258
                        for (AsyncEvent exchange : registrations) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   259
                            SelectableChannel c = exchange.channel();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   260
                            try {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   261
                                c.configureBlocking(false);
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   262
                                SelectionKey key = c.keyFor(selector);
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   263
                                SelectorAttachment sa;
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   264
                                if (key == null) {
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   265
                                    sa = new SelectorAttachment(c, selector);
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   266
                                } else {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   267
                                    sa = (SelectorAttachment) key.attachment();
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   268
                                }
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   269
                                sa.register(exchange);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   270
                            } catch (IOException e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   271
                                Log.logError("HttpClientImpl: " + e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   272
                                c.close();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   273
                                // let the exchange deal with it
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   274
                                handleEvent(exchange);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   275
                            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   276
                        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   277
                        registrations.clear();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   278
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   279
                    long timeval = getTimeoutValue();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   280
                    long now = System.currentTimeMillis();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   281
                    //debugPrint(selector);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   282
                    int n = selector.select(timeval);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   283
                    if (n == 0) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   284
                        signalTimeouts(now);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   285
                        continue;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   286
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   287
                    Set<SelectionKey> keys = selector.selectedKeys();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   288
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   289
                    for (SelectionKey key : keys) {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   290
                        SelectorAttachment sa = (SelectorAttachment) key.attachment();
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   291
                        int eventsOccurred = key.readyOps();
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   292
                        sa.events(eventsOccurred).forEach(readyList::add);
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   293
                        sa.resetInterestOps(eventsOccurred);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   294
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   295
                    selector.selectNow(); // complete cancellation
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   296
                    selector.selectedKeys().clear();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   297
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   298
                    for (AsyncEvent exchange : readyList) {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   299
                        if (exchange.blocking()) {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   300
                            exchange.channel().configureBlocking(true);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   301
                        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   302
                        executor.synchronize();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   303
                        handleEvent(exchange); // will be delegated to executor
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   304
                    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   305
                    readyList.clear();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   306
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   307
            } catch (Throwable e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   308
                if (!closed) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   309
                    // This terminates thread. So, better just print stack trace
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   310
                    String err = Utils.stackTrace(e);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   311
                    Log.logError("HttpClientImpl: fatal error: " + err);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   312
                }
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   313
            } finally {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   314
                shutdown();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   315
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   316
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   317
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   318
        void debugPrint(Selector selector) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   319
            System.err.println("Selector: debugprint start");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   320
            Set<SelectionKey> keys = selector.keys();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   321
            for (SelectionKey key : keys) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   322
                SelectableChannel c = key.channel();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   323
                int ops = key.interestOps();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   324
                System.err.printf("selector chan:%s ops:%d\n", c, ops);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   325
            }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   326
            System.err.println("Selector: debugprint end");
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   327
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   328
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   329
        void handleEvent(AsyncEvent e) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   330
            if (closed) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   331
                e.abort();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   332
            } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   333
                e.handle();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   334
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   335
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   336
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   337
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   338
    /**
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   339
     * Tracks multiple user level registrations associated with one NIO
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   340
     * registration (SelectionKey). In this implementation, registrations
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   341
     * are one-off and when an event is posted the registration is cancelled
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   342
     * until explicitly registered again.
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   343
     *
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   344
     * <p> No external synchronization required as this class is only used
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   345
     * by the SelectorManager thread. One of these objects required per
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   346
     * connection.
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   347
     */
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   348
    private static class SelectorAttachment {
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   349
        private final SelectableChannel chan;
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   350
        private final Selector selector;
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   351
        private final ArrayList<AsyncEvent> pending;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   352
        private int interestOps;
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   353
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   354
        SelectorAttachment(SelectableChannel chan, Selector selector) {
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   355
            this.pending = new ArrayList<>();
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   356
            this.chan = chan;
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   357
            this.selector = selector;
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   358
        }
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   359
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   360
        void register(AsyncEvent e) throws ClosedChannelException {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   361
            int newOps = e.interestOps();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   362
            boolean reRegister = (interestOps & newOps) != newOps;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   363
            interestOps |= newOps;
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   364
            pending.add(e);
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   365
            if (reRegister) {
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   366
                // first time registration happens here also
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   367
                chan.register(selector, interestOps, this);
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   368
            }
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   369
        }
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   370
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   371
        /**
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   372
         * Returns a Stream<AsyncEvents> containing only events that are
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   373
         * registered with the given {@code interestOps}.
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   374
         */
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   375
        Stream<AsyncEvent> events(int interestOps) {
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   376
            return pending.stream()
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   377
                    .filter(ev -> (ev.interestOps() & interestOps) != 0);
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   378
        }
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   379
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   380
        /**
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   381
         * Removes any events with the given {@code interestOps}, and if no
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   382
         * events remaining, cancels the associated SelectionKey.
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   383
         */
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   384
        void resetInterestOps(int interestOps) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   385
            int newOps = 0;
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   386
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   387
            Iterator<AsyncEvent> itr = pending.iterator();
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   388
            while (itr.hasNext()) {
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   389
                AsyncEvent event = itr.next();
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   390
                int evops = event.interestOps();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   391
                if (event.repeating()) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   392
                    newOps |= evops;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   393
                    continue;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   394
                }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   395
                if ((evops & interestOps) != 0) {
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   396
                    itr.remove();
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   397
                } else {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   398
                    newOps |= evops;
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   399
                }
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   400
            }
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   401
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   402
            this.interestOps = newOps;
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   403
            SelectionKey key = chan.keyFor(selector);
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   404
            if (newOps == 0) {
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   405
                key.cancel();
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   406
            } else {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   407
                key.interestOps(newOps);
36434
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   408
            }
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   409
        }
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   410
    }
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   411
3fd8dee1b158 8151299: Http client SelectorManager overwriting read and write events
michaelm
parents: 36131
diff changeset
   412
    /**
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   413
     * Creates a HttpRequest associated with this group.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   414
     *
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   415
     * @throws IllegalStateException
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   416
     *         if the group has been stopped
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   417
     */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   418
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   419
    public HttpRequestBuilderImpl request() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   420
        return new HttpRequestBuilderImpl(this, null);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   421
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   422
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   423
    /**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   424
     * Creates a HttpRequest associated with this group.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   425
     *
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   426
     * @throws IllegalStateException
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   427
     *         if the group has been stopped
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   428
     */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   429
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   430
    public HttpRequestBuilderImpl request(URI uri) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   431
        return new HttpRequestBuilderImpl(this, uri);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   432
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   433
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   434
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   435
    public SSLContext sslContext() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   436
        Utils.checkNetPermission("getSSLContext");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   437
        return sslContext;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   438
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   439
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   440
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   441
    public Optional<SSLParameters> sslParameters() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   442
        return Optional.ofNullable(sslParams);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   443
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   444
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   445
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   446
    public Optional<Authenticator> authenticator() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   447
        return Optional.ofNullable(authenticator);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   448
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   449
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   450
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   451
    public ExecutorService executorService() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   452
        return executor.userExecutor();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   453
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   454
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   455
    ExecutorWrapper executorWrapper() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   456
        return executor;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   457
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   458
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   459
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   460
    public boolean pipelining() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   461
        return this.pipelining;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   462
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   463
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   464
    ConnectionPool connectionPool() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   465
        return connections;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   466
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   467
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   468
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   469
    public Redirect followRedirects() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   470
        return followRedirects;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   471
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   472
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   473
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   474
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   475
    public Optional<CookieManager> cookieManager() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   476
        return Optional.ofNullable(cookieManager);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   477
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   478
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   479
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   480
    public Optional<ProxySelector> proxy() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   481
        return Optional.ofNullable(this.proxySelector);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   482
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   483
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   484
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   485
    public Version version() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   486
        return version;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   487
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   488
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   489
    //private final HashMap<String, Boolean> http2NotSupported = new HashMap<>();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   490
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   491
    boolean getHttp2Allowed() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   492
        return version.equals(Version.HTTP_2);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   493
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   494
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   495
    private void initFilters() {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   496
        addFilter(AuthenticationFilter.class);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   497
        addFilter(RedirectFilter.class);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   498
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   499
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   500
    private void addFilter(Class<? extends HeaderFilter> f) {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   501
        filters.addFilter(f);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   502
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   503
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   504
    final List<HeaderFilter> filterChain() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   505
        return filters.getFilterChain();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   506
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   507
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   508
    // Timer controls. Timers are implemented through timed Selector.select()
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   509
    // calls.
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   510
    synchronized void registerTimer(TimeoutEvent event) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   511
        long elapse = event.timevalMillis();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   512
        ListIterator<TimeoutEvent> iter = timeouts.listIterator();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   513
        long listval = 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   514
        event.delta = event.timeval; // in case list empty
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   515
        TimeoutEvent next;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   516
        while (iter.hasNext()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   517
            next = iter.next();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   518
            listval += next.delta;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   519
            if (elapse < listval) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   520
                listval -= next.delta;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   521
                event.delta = elapse - listval;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   522
                next.delta -= event.delta;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   523
                iter.previous();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   524
                break;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   525
            } else if (!iter.hasNext()) {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   526
                event.delta = event.timeval - listval;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   527
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   528
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   529
        iter.add(event);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   530
        selmgr.wakeupSelector();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   531
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   532
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   533
    private synchronized void signalTimeouts(long then) {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   534
        if (timeouts.isEmpty()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   535
            return;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   536
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   537
        long now = System.currentTimeMillis();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   538
        long duration = now - then;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   539
        ListIterator<TimeoutEvent> iter = timeouts.listIterator();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   540
        TimeoutEvent event = iter.next();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   541
        long delta = event.delta;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   542
        if (duration < delta) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   543
            event.delta -= duration;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   544
            return;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   545
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   546
        event.handle();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   547
        iter.remove();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   548
        while (iter.hasNext()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   549
            event = iter.next();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   550
            if (event.delta == 0) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   551
                event.handle();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   552
                iter.remove();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   553
            } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   554
                event.delta += delta;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   555
                break;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   556
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   557
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   558
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   559
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   560
    synchronized void cancelTimer(TimeoutEvent event) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   561
        ListIterator<TimeoutEvent> iter = timeouts.listIterator();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   562
        while (iter.hasNext()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   563
            TimeoutEvent ev = iter.next();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   564
            if (event == ev) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   565
                if (iter.hasNext()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   566
                    // adjust
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   567
                    TimeoutEvent next = iter.next();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   568
                    next.delta += ev.delta;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   569
                    iter.previous();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   570
                }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   571
                iter.remove();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   572
            }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   573
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   574
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   575
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   576
    // used for the connection window
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   577
    int getReceiveBufferSize() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   578
        return Utils.getIntegerNetProperty(
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   579
                "java.net.httpclient.connectionWindowSize", 256 * 1024
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   580
        );
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   581
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   582
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   583
    // returns 0 meaning block forever, or a number of millis to block for
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36434
diff changeset
   584
    private synchronized long getTimeoutValue() {
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   585
        if (timeouts.isEmpty()) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   586
            return 0;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   587
        } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   588
            return timeouts.get(0).delta;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   589
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   590
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   591
}