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