jdk/src/java.httpclient/share/classes/java/net/http/HttpRequestImpl.java
author michaelm
Sat, 30 Apr 2016 00:30:31 +0100
changeset 37720 45cd7cc65382
parent 36131 379db4b2f95d
child 39730 196f4e25d9f5
permissions -rw-r--r--
8087124: HTTP/2 implementation Reviewed-by: chegar
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    26
import java.io.IOException;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    27
import java.net.InetSocketAddress;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    28
import java.net.ProxySelector;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    29
import java.net.URI;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    30
import java.net.http.HttpClient.Version;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    31
import java.net.http.HttpResponse.MultiProcessor;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    32
import java.util.concurrent.CompletableFuture;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    33
import java.security.AccessControlContext;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    34
import java.security.AccessController;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    35
import static java.net.http.HttpRedirectImpl.getRedirects;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    36
import java.util.Locale;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    37
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    38
class HttpRequestImpl extends HttpRequest {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    39
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
    40
    private final ImmutableHeaders userHeaders;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    41
    private final HttpHeadersImpl systemHeaders;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    42
    private final URI uri;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    43
    private InetSocketAddress authority; // only used when URI not specified
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    44
    private final String method;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    45
    private final HttpClientImpl client;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    46
    private final HttpRedirectImpl followRedirects;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    47
    private final ProxySelector proxy;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    48
    final BodyProcessor requestProcessor;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    49
    final boolean secure;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    50
    final boolean expectContinue;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    51
    private final java.net.http.HttpClient.Version version;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    52
    private boolean isWebSocket;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    53
    final MultiExchange exchange;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    54
    private boolean receiving;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    55
    private AccessControlContext acc;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    56
    private final long timeval;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
    57
    private Stream.PushGroup<?> pushGroup;
36131
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
    public HttpRequestImpl(HttpClientImpl client,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    60
                           String method,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    61
                           HttpRequestBuilderImpl builder) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    62
        this.client = client;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    63
        this.method = method == null? "GET" : method;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    64
        this.userHeaders = builder.headers() == null ?
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
    65
                new ImmutableHeaders() :
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
    66
                new ImmutableHeaders(builder.headers(), Utils.ALLOWED_HEADERS);
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    67
        this.followRedirects = getRedirects(builder.followRedirects() == null ?
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    68
                client.followRedirects() : builder.followRedirects());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    69
        this.systemHeaders = new HttpHeadersImpl();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    70
        this.uri = builder.uri();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    71
        this.proxy = builder.proxy();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    72
        this.expectContinue = builder.expectContinue();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    73
        this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    74
        this.version = builder.version();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    75
        if (builder.body() == null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    76
            this.requestProcessor = HttpRequest.noBody();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    77
        } else {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    78
            this.requestProcessor = builder.body();
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
        this.exchange = new MultiExchange(this);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    81
        this.timeval = builder.timeval();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    82
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    83
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    84
    /** Creates a HttpRequestImpl using fields of an existing request impl. */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    85
    public HttpRequestImpl(URI uri,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    86
                           HttpRequest request,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    87
                           HttpClientImpl client,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    88
                           String method,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    89
                           HttpRequestImpl other) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    90
        this.client = client;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    91
        this.method = method == null? "GET" : method;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
    92
        this.userHeaders = other.userHeaders;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    93
        this.followRedirects = getRedirects(other.followRedirects() == null ?
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    94
                client.followRedirects() : other.followRedirects());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    95
        this.systemHeaders = other.systemHeaders;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    96
        this.uri = uri;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    97
        this.expectContinue = other.expectContinue;
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
    98
        this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https");
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
    99
        this.requestProcessor = other.requestProcessor;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   100
        this.proxy = other.proxy;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   101
        this.version = other.version;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   102
        this.acc = other.acc;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   103
        this.exchange = new MultiExchange(this);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   104
        this.timeval = other.timeval;
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   107
    /* used for creating CONNECT requests  */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   108
    HttpRequestImpl(HttpClientImpl client,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   109
                    String method,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   110
                    InetSocketAddress authority) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   111
        this.client = client;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   112
        this.method = method;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   113
        this.followRedirects = getRedirects(client.followRedirects());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   114
        this.systemHeaders = new HttpHeadersImpl();
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   115
        this.userHeaders = new ImmutableHeaders();
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   116
        this.uri = null;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   117
        this.proxy = null;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   118
        this.requestProcessor = HttpRequest.noBody();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   119
        this.version = java.net.http.HttpClient.Version.HTTP_1_1;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   120
        this.authority = authority;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   121
        this.secure = false;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   122
        this.expectContinue = false;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   123
        this.exchange = new MultiExchange(this);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   124
        this.timeval = 0; // block TODO: fix
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   125
    }
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
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   128
    public HttpClientImpl client() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   129
        return client;
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
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   132
    /**
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   133
     * Creates a HttpRequestImpl from the given set of Headers and the associated
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   134
     * "parent" request. Fields not taken from the headers are taken from the
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   135
     * parent.
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   136
     */
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   137
    static HttpRequestImpl createPushRequest(HttpRequestImpl parent,
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   138
            HttpHeadersImpl headers) throws IOException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   139
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   140
        return new HttpRequestImpl(parent, headers);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   141
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   142
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   143
    // only used for push requests
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   144
    private HttpRequestImpl(HttpRequestImpl parent, HttpHeadersImpl headers) throws IOException {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   145
        this.method = headers.firstValue(":method")
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   146
                .orElseThrow(() -> new IOException("No method in Push Promise"));
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   147
        String path = headers.firstValue(":path")
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   148
                .orElseThrow(() -> new IOException("No path in Push Promise"));
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   149
        String scheme = headers.firstValue(":scheme")
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   150
                .orElseThrow(() -> new IOException("No scheme in Push Promise"));
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   151
        String authority = headers.firstValue(":authority")
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   152
                .orElseThrow(() -> new IOException("No authority in Push Promise"));
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   153
        StringBuilder sb = new StringBuilder();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   154
        sb.append(scheme).append("://").append(authority).append(path);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   155
        this.uri = URI.create(sb.toString());
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   156
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   157
        this.client = parent.client;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   158
        this.userHeaders = new ImmutableHeaders(headers, Utils.ALLOWED_HEADERS);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   159
        this.followRedirects = parent.followRedirects;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   160
        this.systemHeaders = parent.systemHeaders;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   161
        this.expectContinue = parent.expectContinue;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   162
        this.secure = parent.secure;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   163
        this.requestProcessor = parent.requestProcessor;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   164
        this.proxy = parent.proxy;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   165
        this.version = parent.version;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   166
        this.acc = parent.acc;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   167
        this.exchange = parent.exchange;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   168
        this.timeval = parent.timeval;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   169
    }
36131
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
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   172
    public String toString() {
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   173
        return (uri == null ? "" : uri.toString()) + " " + method;
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   174
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   175
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   176
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   177
    public HttpHeaders headers() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   178
        return userHeaders;
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   181
    InetSocketAddress authority() { return authority; }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   182
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   183
    void setH2Upgrade() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   184
        Http2ClientImpl h2client = client.client2();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   185
        systemHeaders.setHeader("Connection", "Upgrade, HTTP2-Settings");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   186
        systemHeaders.setHeader("Upgrade", "h2c");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   187
        systemHeaders.setHeader("HTTP2-Settings", h2client.getSettingsString());
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   188
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   189
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   190
    private synchronized void receiving() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   191
        if (receiving) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   192
            throw new IllegalStateException("already receiving response");
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   193
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   194
        receiving = true;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   195
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   196
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   197
    synchronized Stream.PushGroup<?> pushGroup() {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   198
        return pushGroup;
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   199
    }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   200
36131
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
     * Response filters may result in a new HttpRequestImpl being created
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   203
     * (but still associated with the same API HttpRequest) and the process
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   204
     * is repeated.
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
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   207
    public HttpResponse response() throws IOException, InterruptedException {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   208
        receiving(); // TODO: update docs
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   209
        if (System.getSecurityManager() != null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   210
            acc = AccessController.getContext();
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
        return exchange.response();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   213
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   214
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   215
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   216
    public synchronized CompletableFuture<HttpResponse> responseAsync() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   217
        receiving(); // TODO: update docs
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   218
        if (System.getSecurityManager() != null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   219
            acc = AccessController.getContext();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   220
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   221
        return exchange.responseAsync(null)
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   222
            .thenApply((r) -> (HttpResponse)r);
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
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   225
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   226
    @SuppressWarnings("unchecked")
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   227
    @Override
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   228
    public synchronized <U> CompletableFuture<U>
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   229
    multiResponseAsync(MultiProcessor<U> rspproc) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   230
        if (System.getSecurityManager() != null) {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   231
            acc = AccessController.getContext();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   232
        }
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   233
        this.pushGroup = new Stream.PushGroup<>(rspproc, this);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   234
        CompletableFuture<HttpResponse> cf = pushGroup.mainResponse();
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   235
        responseAsync()
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   236
            .whenComplete((HttpResponse r, Throwable t) -> {
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   237
                if (r != null)
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   238
                    cf.complete(r);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   239
                else
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   240
                    cf.completeExceptionally(t);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   241
                pushGroup.pushError(t);
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   242
            });
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   243
        return (CompletableFuture<U>)pushGroup.groupResult();
36131
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
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   247
    public boolean expectContinue() { return expectContinue; }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   248
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   249
    public boolean requestHttp2() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   250
        return version.equals(HttpClient.Version.HTTP_2);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   251
        //return client.getHttp2Allowed();
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
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   254
    AccessControlContext getAccessControlContext() { return acc; }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   255
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   256
    InetSocketAddress proxy() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   257
        ProxySelector ps = this.proxy;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   258
        if (ps == null) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   259
            ps = client.proxy().orElse(null);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   260
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   261
        if (ps == null || method.equalsIgnoreCase("CONNECT")) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   262
            return null;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   263
        }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   264
        return (InetSocketAddress)ps.select(uri).get(0).address();
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   265
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   266
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   267
    boolean secure() { return secure; }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   268
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   269
    void isWebSocket(boolean is) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   270
        isWebSocket = is;
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   271
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   272
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   273
    boolean isWebSocket() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   274
        return isWebSocket;
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
    /** Returns the follow-redirects setting for this request. */
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   278
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   279
    public java.net.http.HttpClient.Redirect followRedirects() {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   280
        return getRedirects(followRedirects);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   281
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   282
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   283
    HttpRedirectImpl followRedirectsImpl() { return followRedirects; }
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
    /**
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   286
     * Returns the request method for this request. If not set explicitly,
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   287
     * the default method for any request is "GET".
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
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   290
    public String method() { return method; }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   291
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   292
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   293
    public URI uri() { return uri; }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   294
37720
45cd7cc65382 8087124: HTTP/2 implementation
michaelm
parents: 36131
diff changeset
   295
    HttpHeaders getUserHeaders() { return userHeaders; }
36131
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   296
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   297
    HttpHeadersImpl getSystemHeaders() { return systemHeaders; }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   298
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   299
    HttpClientImpl getClient() { return client; }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   300
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   301
    BodyProcessor requestProcessor() { return requestProcessor; }
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
    @Override
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   304
    public Version version() { return version; }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   305
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   306
    void addSystemHeader(String name, String value) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   307
        systemHeaders.addHeader(name, value);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   308
    }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   309
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   310
    void setSystemHeader(String name, String value) {
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   311
        systemHeaders.setHeader(name, value);
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   312
    }
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
    long timeval() { return timeval; }
379db4b2f95d 8087112: HTTP API and HTTP/1.1 implementation
michaelm
parents:
diff changeset
   315
}