test/jdk/java/net/httpclient/http2/ProxyTest2.java
author chegar
Wed, 07 Feb 2018 21:45:37 +0000
branchhttp-client-branch
changeset 56092 fd85b2bf2b0d
parent 56089 42208b2f224e
child 56167 96fa4f49a9ff
permissions -rw-r--r--
http-client-branch: move implementation to jdk.internal.net.http
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     1
/*
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     4
 *
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     8
 *
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    14
 *
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    18
 *
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    21
 * questions.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    22
 */
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    23
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    24
import com.sun.net.httpserver.HttpContext;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    25
import com.sun.net.httpserver.HttpExchange;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    26
import com.sun.net.httpserver.HttpHandler;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    27
import com.sun.net.httpserver.HttpServer;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    28
import com.sun.net.httpserver.HttpsConfigurator;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    29
import com.sun.net.httpserver.HttpsParameters;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    30
import com.sun.net.httpserver.HttpsServer;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    31
import java.io.IOException;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    32
import java.io.InputStream;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    33
import java.io.OutputStream;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    34
import java.io.OutputStreamWriter;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    35
import java.io.PrintWriter;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    36
import java.io.Writer;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    37
import java.net.HttpURLConnection;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    38
import java.net.InetAddress;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    39
import java.net.InetSocketAddress;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    40
import java.net.Proxy;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    41
import java.net.ProxySelector;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    42
import java.net.ServerSocket;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    43
import java.net.Socket;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    44
import java.net.URI;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    45
import java.net.URISyntaxException;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    46
import java.nio.charset.StandardCharsets;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    47
import java.security.NoSuchAlgorithmException;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    48
import javax.net.ssl.HostnameVerifier;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    49
import javax.net.ssl.HttpsURLConnection;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    50
import javax.net.ssl.SSLContext;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    51
import javax.net.ssl.SSLSession;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56076
diff changeset
    52
import java.net.http.HttpClient;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56076
diff changeset
    53
import java.net.http.HttpRequest;
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56076
diff changeset
    54
import java.net.http.HttpResponse;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    55
import jdk.testlibrary.SimpleSSLContext;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    56
import java.util.concurrent.*;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    57
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    58
/**
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    59
 * @test
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    60
 * @bug 8181422
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    61
 * @summary  Verifies that you can access an HTTP/2 server over HTTPS by
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    62
 *           tunnelling through an HTTP/1.1 proxy.
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56076
diff changeset
    63
 * @modules java.net.http
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    64
 * @library /lib/testlibrary server
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    65
 * @modules java.base/sun.net.www.http
56092
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    66
 *          java.net.http/jdk.internal.net.http.common
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    67
 *          java.net.http/jdk.internal.net.http.frame
fd85b2bf2b0d http-client-branch: move implementation to jdk.internal.net.http
chegar
parents: 56089
diff changeset
    68
 *          java.net.http/jdk.internal.net.http.hpack
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    69
 * @build jdk.testlibrary.SimpleSSLContext ProxyTest2
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    70
 * @run main/othervm ProxyTest2
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    71
 * @author danielfuchs
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    72
 */
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    73
public class ProxyTest2 {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    74
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    75
    static {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    76
        try {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    77
            HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    78
                    public boolean verify(String hostname, SSLSession session) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    79
                        return true;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    80
                    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    81
                });
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    82
            SSLContext.setDefault(new SimpleSSLContext().get());
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    83
        } catch (IOException ex) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    84
            throw new ExceptionInInitializerError(ex);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    85
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    86
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    87
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    88
    static final String RESPONSE = "<html><body><p>Hello World!</body></html>";
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    89
    static final String PATH = "/foo/";
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    90
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    91
    static Http2TestServer createHttpsServer(ExecutorService exec) throws Exception {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    92
        Http2TestServer server = new Http2TestServer(true, 0, exec, SSLContext.getDefault());
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    93
        server.addHandler(new Http2Handler() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    94
            @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    95
            public void handle(Http2TestExchange he) throws IOException {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    96
                he.getResponseHeaders().addHeader("encoding", "UTF-8");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    97
                he.sendResponseHeaders(200, RESPONSE.length());
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    98
                he.getResponseBody().write(RESPONSE.getBytes(StandardCharsets.UTF_8));
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
    99
                he.close();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   100
            }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   101
        }, PATH);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   102
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   103
        return server;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   104
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   105
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   106
    public static void main(String[] args)
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   107
            throws Exception
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   108
    {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   109
        ExecutorService exec = Executors.newCachedThreadPool();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   110
        Http2TestServer server = createHttpsServer(exec);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   111
        server.start();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   112
        try {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   113
            // Http2TestServer over HTTPS does not support HTTP/1.1
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   114
            // => only test with a HTTP/2 client
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   115
            test(server, HttpClient.Version.HTTP_2);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   116
        } finally {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   117
            server.stop();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   118
            exec.shutdown();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   119
            System.out.println("Server stopped");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   120
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   121
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   122
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   123
    public static void test(Http2TestServer server, HttpClient.Version version)
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   124
            throws Exception
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   125
    {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   126
        System.out.println("Server is: " + server.getAddress().toString());
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   127
        URI uri = new URI("https://localhost:" + server.getAddress().getPort() + PATH + "x");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   128
        TunnelingProxy proxy = new TunnelingProxy(server);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   129
        proxy.start();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   130
        try {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   131
            System.out.println("Proxy started");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   132
            Proxy p = new Proxy(Proxy.Type.HTTP,
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   133
                    InetSocketAddress.createUnresolved("localhost", proxy.getAddress().getPort()));
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   134
            System.out.println("Setting up request with HttpClient for version: "
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   135
                    + version.name() + "URI=" + uri);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   136
            ProxySelector ps = ProxySelector.of(
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   137
                    InetSocketAddress.createUnresolved("localhost", proxy.getAddress().getPort()));
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   138
            HttpClient client = HttpClient.newBuilder()
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   139
                .version(version)
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   140
                .proxy(ps)
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   141
                .build();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   142
            HttpRequest request = HttpRequest.newBuilder()
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   143
                .uri(uri)
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   144
                .GET()
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   145
                .build();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   146
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   147
            System.out.println("Sending request with HttpClient");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   148
            HttpResponse<String> response
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   149
                = client.send(request, HttpResponse.BodyHandler.asString());
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   150
            System.out.println("Got response");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   151
            String resp = response.body();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   152
            System.out.println("Received: " + resp);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   153
            if (!RESPONSE.equals(resp)) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   154
                throw new AssertionError("Unexpected response");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   155
            }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   156
        } finally {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   157
            System.out.println("Stopping proxy");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   158
            proxy.stop();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   159
            System.out.println("Proxy stopped");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   160
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   161
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   162
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   163
    static class TunnelingProxy {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   164
        final Thread accept;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   165
        final ServerSocket ss;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   166
        final boolean DEBUG = false;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   167
        final Http2TestServer serverImpl;
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   168
        final CopyOnWriteArrayList<CompletableFuture<Void>> connectionCFs
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   169
                = new CopyOnWriteArrayList<>();
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   170
        private volatile boolean stopped;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   171
        TunnelingProxy(Http2TestServer serverImpl) throws IOException {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   172
            this.serverImpl = serverImpl;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   173
            ss = new ServerSocket();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   174
            accept = new Thread(this::accept);
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   175
            accept.setDaemon(true);
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   176
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   177
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   178
        void start() throws IOException {
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   179
            ss.bind(new InetSocketAddress(0));
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   180
            accept.start();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   181
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   182
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   183
        // Pipe the input stream to the output stream.
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   184
        private synchronized Thread pipe(InputStream is, OutputStream os,
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   185
                                         char tag, CompletableFuture<Void> end) {
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   186
            return new Thread("TunnelPipe("+tag+")") {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   187
                @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   188
                public void run() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   189
                    try {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   190
                        try {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   191
                            int c;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   192
                            while ((c = is.read()) != -1) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   193
                                os.write(c);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   194
                                os.flush();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   195
                                // if DEBUG prints a + or a - for each transferred
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   196
                                // character.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   197
                                if (DEBUG) System.out.print(tag);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   198
                            }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   199
                            is.close();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   200
                        } finally {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   201
                            os.close();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   202
                        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   203
                    } catch (IOException ex) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   204
                        if (DEBUG) ex.printStackTrace(System.out);
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   205
                    } finally {
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   206
                        end.complete(null);
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   207
                    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   208
                }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   209
            };
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   210
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   211
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   212
        public InetSocketAddress getAddress() {
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   213
            return new InetSocketAddress(
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   214
                    "localhost",
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   215
                    ss.getLocalPort());
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   216
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   217
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   218
        // This is a bit shaky. It doesn't handle continuation
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   219
        // lines, but our client shouldn't send any.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   220
        // Read a line from the input stream, swallowing the final
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   221
        // \r\n sequence. Stops at the first \n, doesn't complain
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   222
        // if it wasn't preceded by '\r'.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   223
        //
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   224
        String readLine(InputStream r) throws IOException {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   225
            StringBuilder b = new StringBuilder();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   226
            int c;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   227
            while ((c = r.read()) != -1) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   228
                if (c == '\n') break;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   229
                b.appendCodePoint(c);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   230
            }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   231
            if (b.codePointAt(b.length() -1) == '\r') {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   232
                b.delete(b.length() -1, b.length());
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   233
            }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   234
            return b.toString();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   235
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   236
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   237
        public void accept() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   238
            Socket clientConnection = null;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   239
            try {
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   240
                while (!stopped) {
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   241
                    System.out.println("Tunnel: Waiting for client");
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   242
                    Socket toClose;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   243
                    try {
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   244
                        toClose = clientConnection = ss.accept();
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   245
                    } catch (IOException io) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   246
                        if (DEBUG) io.printStackTrace(System.out);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   247
                        break;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   248
                    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   249
                    System.out.println("Tunnel: Client accepted");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   250
                    Socket targetConnection = null;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   251
                    InputStream  ccis = clientConnection.getInputStream();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   252
                    OutputStream ccos = clientConnection.getOutputStream();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   253
                    Writer w = new OutputStreamWriter(ccos, "UTF-8");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   254
                    PrintWriter pw = new PrintWriter(w);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   255
                    System.out.println("Tunnel: Reading request line");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   256
                    String requestLine = readLine(ccis);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   257
                    System.out.println("Tunnel: Request status line: " + requestLine);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   258
                    if (requestLine.startsWith("CONNECT ")) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   259
                        // We should probably check that the next word following
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   260
                        // CONNECT is the host:port of our HTTPS serverImpl.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   261
                        // Some improvement for a followup!
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   262
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   263
                        // Read all headers until we find the empty line that
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   264
                        // signals the end of all headers.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   265
                        while(!requestLine.equals("")) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   266
                            System.out.println("Tunnel: Reading header: "
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   267
                                    + (requestLine = readLine(ccis)));
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   268
                        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   269
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   270
                        // Open target connection
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   271
                        targetConnection = new Socket(
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   272
                                InetAddress.getLoopbackAddress(),
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   273
                                serverImpl.getAddress().getPort());
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   274
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   275
                        // Then send the 200 OK response to the client
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   276
                        System.out.println("Tunnel: Sending "
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   277
                                + "HTTP/1.1 200 OK\r\n\r\n");
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   278
                        pw.print("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   279
                        pw.flush();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   280
                    } else {
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   281
                        // This should not happen. If it does then just print an
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   282
                        // error - both on out and err, and close the accepted
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   283
                        // socket
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   284
                        System.out.println("WARNING: Tunnel: Unexpected status line: "
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   285
                                + requestLine + " received by "
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   286
                                + ss.getLocalSocketAddress()
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   287
                                + " from "
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   288
                                + toClose.getRemoteSocketAddress()
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   289
                                + " - closing accepted socket");
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   290
                        // Print on err
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   291
                        System.err.println("WARNING: Tunnel: Unexpected status line: "
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   292
                                + requestLine + " received by "
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   293
                                + ss.getLocalSocketAddress()
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   294
                                + " from "
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   295
                                + toClose.getRemoteSocketAddress());
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   296
                        // close accepted socket.
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   297
                        toClose.close();
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   298
                        System.err.println("Tunnel: accepted socket closed.");
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   299
                        continue;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   300
                    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   301
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   302
                    // Pipe the input stream of the client connection to the
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   303
                    // output stream of the target connection and conversely.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   304
                    // Now the client and target will just talk to each other.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   305
                    System.out.println("Tunnel: Starting tunnel pipes");
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   306
                    CompletableFuture<Void> end, end1, end2;
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   307
                    Thread t1 = pipe(ccis, targetConnection.getOutputStream(), '+',
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   308
                            end1 = new CompletableFuture<>());
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   309
                    Thread t2 = pipe(targetConnection.getInputStream(), ccos, '-',
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   310
                            end2 = new CompletableFuture<>());
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   311
                    end = CompletableFuture.allOf(end1, end2);
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   312
                    end.whenComplete(
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   313
                            (r,t) -> {
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   314
                                try { toClose.close(); } catch (IOException x) { }
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   315
                                finally {connectionCFs.remove(end);}
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   316
                            });
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   317
                    connectionCFs.add(end);
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   318
                    t1.start();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   319
                    t2.start();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   320
                }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   321
            } catch (Throwable ex) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   322
                try {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   323
                    ss.close();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   324
                } catch (IOException ex1) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   325
                    ex.addSuppressed(ex1);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   326
                }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   327
                ex.printStackTrace(System.err);
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   328
            } finally {
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   329
                System.out.println("Tunnel: exiting (stopped=" + stopped + ")");
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   330
                connectionCFs.forEach(cf -> cf.complete(null));
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   331
            }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   332
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   333
56076
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   334
        public void stop() throws IOException {
9a2855e0a796 http-client-branch: more binding to 0.0.0.0 instead of localhost/127.0.0.1
dfuchs
parents: 48083
diff changeset
   335
            stopped = true;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   336
            ss.close();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   337
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   338
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   339
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   340
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   341
    static class Configurator extends HttpsConfigurator {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   342
        public Configurator(SSLContext ctx) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   343
            super(ctx);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   344
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   345
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   346
        @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   347
        public void configure (HttpsParameters params) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   348
            params.setSSLParameters (getSSLContext().getSupportedSSLParameters());
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   349
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   350
    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   351
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   352
}