test/jdk/java/net/httpclient/HttpsTunnelTest.java
author chegar
Tue, 18 Jun 2019 18:38:53 +0100
changeset 55405 17f24c0e2f01
parent 54579 270557b396eb
permissions -rw-r--r--
8226319: Add forgotten test/jdk/java/net/httpclient/BodySubscribersTest.java Reviewed-by: dfuchs, prappo
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     1
/*
54579
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
     2
 * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     4
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     8
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    13
 * accompanied this code).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    14
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    18
 *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    21
 * questions.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    22
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    23
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    24
import com.sun.net.httpserver.HttpsConfigurator;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    25
import com.sun.net.httpserver.HttpsServer;
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 49765
diff changeset
    26
import jdk.test.lib.net.SimpleSSLContext;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    27
import javax.net.ssl.SSLContext;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    28
import java.net.InetAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    29
import java.net.InetSocketAddress;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    30
import java.net.ProxySelector;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    31
import java.net.URI;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    32
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    33
import java.net.http.HttpClient.Version;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    34
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    35
import java.net.http.HttpResponse;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    36
import java.net.http.HttpResponse.BodyHandlers;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    37
import java.util.Arrays;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    38
import java.util.List;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    39
import java.util.stream.Collectors;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    40
import java.util.stream.Stream;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    41
import static java.lang.String.format;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    42
import static java.lang.System.out;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    43
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    44
/**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    45
 * @test
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    46
 * @summary This test verifies that if an h2 connection going through a
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    47
 *          proxy P is downgraded to HTTP/1.1, then a new h2 request
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    48
 *          going to a different host through the same proxy will not
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    49
 *          be preemptively downgraded. That, is the stack should attempt
54579
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
    50
 *          a new h2 connection to the new host. It also verifies that
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
    51
 *          the stack sends the appropriate "host" header to the proxy.
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
    52
 * @bug 8196967 8222527
52121
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 49765
diff changeset
    53
 * @library /test/lib http2/server
934969c63223 8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents: 49765
diff changeset
    54
 * @build jdk.test.lib.net.SimpleSSLContext HttpServerAdapters DigestEchoServer HttpsTunnelTest
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    55
 * @modules java.net.http/jdk.internal.net.http.common
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    56
 *          java.net.http/jdk.internal.net.http.frame
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    57
 *          java.net.http/jdk.internal.net.http.hpack
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    58
 *          java.logging
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    59
 *          java.base/sun.net.www.http
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    60
 *          java.base/sun.net.www
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    61
 *          java.base/sun.net
54579
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
    62
 * @run main/othervm -Dtest.requiresHost=true
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
    63
 *                   -Djdk.httpclient.HttpClient.log=headers
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
    64
 *                   -Djdk.internal.httpclient.debug=true HttpsTunnelTest
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
    65
 * @run main/othervm -Dtest.requiresHost=true
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
    66
 *                   -Djdk.httpclient.allowRestrictedHeaders=host
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
    67
 *                   -Djdk.httpclient.HttpClient.log=headers
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
    68
 *                   -Djdk.internal.httpclient.debug=true HttpsTunnelTest
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
    69
 *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    70
 */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    71
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    72
public class HttpsTunnelTest implements HttpServerAdapters {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    73
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    74
    static final String data[] = {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    75
        "Lorem ipsum",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    76
        "dolor sit amet",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    77
        "consectetur adipiscing elit, sed do eiusmod tempor",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    78
        "quis nostrud exercitation ullamco",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    79
        "laboris nisi",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    80
        "ut",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    81
        "aliquip ex ea commodo consequat." +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    82
        "Duis aute irure dolor in reprehenderit in voluptate velit esse" +
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    83
        "cillum dolore eu fugiat nulla pariatur.",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    84
        "Excepteur sint occaecat cupidatat non proident."
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    85
    };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    86
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    87
    static final SSLContext context;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    88
    static {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    89
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    90
            context = new SimpleSSLContext().get();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    91
            SSLContext.setDefault(context);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    92
        } catch (Exception x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    93
            throw new ExceptionInInitializerError(x);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    94
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    95
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    96
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    97
    HttpsTunnelTest() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    98
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
    99
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   100
    public HttpClient newHttpClient(ProxySelector ps) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   101
        HttpClient.Builder builder = HttpClient
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   102
                .newBuilder()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   103
                .sslContext(context)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   104
                .proxy(ps);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   105
        return builder.build();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   106
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   107
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   108
    public static void main(String[] args) throws Exception {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   109
        InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   110
        HttpsServer server1 = HttpsServer.create(sa, 0);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   111
        server1.setHttpsConfigurator(new HttpsConfigurator(context));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   112
        HttpTestServer http1Server =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   113
                HttpTestServer.of(server1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   114
        http1Server.addHandler(new HttpTestEchoHandler(), "/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   115
        http1Server.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   116
        HttpTestServer http2Server = HttpTestServer.of(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   117
                new Http2TestServer("localhost", true, 0));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   118
        http2Server.addHandler(new HttpTestEchoHandler(), "/");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   119
        http2Server.start();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   120
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   121
        DigestEchoServer.TunnelingProxy proxy = DigestEchoServer.createHttpsProxyTunnel(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   122
                DigestEchoServer.HttpAuthSchemeType.NONE);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   123
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   124
        try {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   125
            URI uri1 = new URI("https://" + http1Server.serverAuthority() + "/foo/https1");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   126
            URI uri2 = new URI("https://" + http2Server.serverAuthority() + "/foo/https2");
54579
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   127
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   128
            boolean provideCustomHost = "host".equalsIgnoreCase(
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   129
                    System.getProperty("jdk.httpclient.allowRestrictedHeaders",""));
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   130
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   131
            String customHttp1Host = null, customHttp2Host = null;
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   132
            if (provideCustomHost) {
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   133
                customHttp1Host = makeCustomHostString(http1Server, uri1);
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   134
                out.println("HTTP/1.1: <" + uri1 + "> [custom host: " + customHttp1Host + "]");
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   135
                customHttp2Host = makeCustomHostString(http2Server, uri2);
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   136
                out.println("HTTP/2:   <" + uri2 + "> [custom host: " + customHttp2Host + "]");
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   137
            }
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   138
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   139
            ProxySelector ps = ProxySelector.of(proxy.getProxyAddress());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   140
                    //HttpClient.Builder.NO_PROXY;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   141
            HttpsTunnelTest test = new HttpsTunnelTest();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   142
            HttpClient client = test.newHttpClient(ps);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   143
            out.println("Proxy is: " + ps.select(uri2));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   144
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   145
            List<String> lines = List.of(Arrays.copyOfRange(data, 0, data.length));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   146
            assert lines.size() == data.length;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   147
            String body = lines.stream().collect(Collectors.joining("\r\n"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   148
            HttpRequest.BodyPublisher reqBody = HttpRequest.BodyPublishers.ofString(body);
54579
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   149
            HttpRequest.Builder req1Builder = HttpRequest
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   150
                    .newBuilder(uri1)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   151
                    .version(Version.HTTP_2)
54579
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   152
                    .POST(reqBody);
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   153
            if (provideCustomHost) req1Builder.header("host", customHttp1Host);
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   154
            HttpRequest req1 = req1Builder.build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   155
            out.println("\nPosting to HTTP/1.1 server at: " + req1);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   156
            HttpResponse<Stream<String>> response = client.send(req1, BodyHandlers.ofLines());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   157
            out.println("Checking response...");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   158
            if (response.statusCode() != 200) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   159
                throw new RuntimeException("Unexpected status code: " + response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   160
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   161
            if (response.version() != Version.HTTP_1_1) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   162
                throw new RuntimeException("Unexpected protocol version: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   163
                        + response.version());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   164
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   165
            List<String> respLines = response.body().collect(Collectors.toList());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   166
            if (!lines.equals(respLines)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   167
                throw new RuntimeException("Unexpected response 1: " + respLines);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   168
            }
54579
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   169
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   170
            HttpRequest.BodyPublisher reqBody2 = HttpRequest.BodyPublishers.ofString(body);
54579
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   171
            HttpRequest.Builder req2Builder = HttpRequest
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   172
                    .newBuilder(uri2)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   173
                    .version(Version.HTTP_2)
54579
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   174
                    .POST(reqBody2);
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   175
            if (provideCustomHost) req2Builder.header("host", customHttp2Host);
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   176
            HttpRequest req2 = req2Builder.build();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   177
            out.println("\nPosting to HTTP/2 server at: " + req2);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   178
            response = client.send(req2, BodyHandlers.ofLines());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   179
            out.println("Checking response...");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   180
            if (response.statusCode() != 200) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   181
                throw new RuntimeException("Unexpected status code: " + response);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   182
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   183
            if (response.version() != Version.HTTP_2) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   184
                throw new RuntimeException("Unexpected protocol version: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   185
                        + response.version());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   186
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   187
            respLines = response.body().collect(Collectors.toList());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   188
            if (!lines.equals(respLines)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   189
                throw new RuntimeException("Unexpected response 2: " + respLines);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   190
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   191
        } catch(Throwable t) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   192
            out.println("Unexpected exception: exiting: " + t);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   193
            t.printStackTrace();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   194
            throw t;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   195
        } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   196
            proxy.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   197
            http1Server.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   198
            http2Server.stop();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   199
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   200
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   201
54579
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   202
    /**
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   203
     * Builds a custom host string that is different to what is in the URI
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   204
     * authority, that is textually different than what the stack would
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   205
     * send. For CONNECT we should ignore any custom host settings.
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   206
     * The tunnelling proxy will fail with badRequest 400 if it receives
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   207
     * the custom host instead of the expected URI authority string.
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   208
     * @param  server The target server.
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   209
     * @param  uri    The URI to the target server
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   210
     * @return a host value for the custom host header.
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   211
     */
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   212
    static final String makeCustomHostString(HttpTestServer server, URI uri) {
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   213
        String customHttpHost;
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   214
        if (server.serverAuthority().contains("localhost")) {
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   215
            customHttpHost = InetAddress.getLoopbackAddress().getHostAddress();
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   216
        } else {
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   217
            customHttpHost = InetAddress.getLoopbackAddress().getHostName();
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   218
        }
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   219
        if (customHttpHost.contains(":")) customHttpHost = "[" + customHttpHost + "]";
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   220
        if (uri.getPort() != -1) customHttpHost = customHttpHost + ":" + uri.getPort();
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   221
        return customHttpHost;
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   222
    }
270557b396eb 8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents: 52121
diff changeset
   223
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents:
diff changeset
   224
}