test/jdk/java/net/httpclient/http2/ProxyTest2.java
author chegar
Tue, 17 Apr 2018 08:54:17 -0700
changeset 49765 ee6f7a61f3a5
parent 48083 b1c1b4ef4be2
child 52121 934969c63223
child 56451 9585061fdb04
permissions -rw-r--r--
8197564: HTTP Client implementation Reviewed-by: chegar, dfuchs, michaelm, prappo Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>
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;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    52
import java.net.http.HttpClient;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    53
import java.net.http.HttpRequest;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
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.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
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
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    66
 *          java.net.http/jdk.internal.net.http.common
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
    67
 *          java.net.http/jdk.internal.net.http.frame
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
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
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   149
                = client.send(request, HttpResponse.BodyHandlers.ofString());
46157
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;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   168
        final CopyOnWriteArrayList<CompletableFuture<Void>> connectionCFs
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   169
                = new CopyOnWriteArrayList<>();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
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);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
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 {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   179
            ss.setReuseAddress(false);
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   180
            ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   181
            accept.start();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   182
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   183
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   184
        // Pipe the input stream to the output stream.
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   185
        private synchronized Thread pipe(InputStream is, OutputStream os,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   186
                                         char tag, CompletableFuture<Void> end) {
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   187
            return new Thread("TunnelPipe("+tag+")") {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   188
                @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   189
                public void run() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   190
                    try {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   191
                        try {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   192
                            int c;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   193
                            while ((c = is.read()) != -1) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   194
                                os.write(c);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   195
                                os.flush();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   196
                                // if DEBUG prints a + or a - for each transferred
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   197
                                // character.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   198
                                if (DEBUG) System.out.print(tag);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   199
                            }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   200
                            is.close();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   201
                        } finally {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   202
                            os.close();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   203
                        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   204
                    } catch (IOException ex) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   205
                        if (DEBUG) ex.printStackTrace(System.out);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   206
                    } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   207
                        end.complete(null);
46157
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
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   213
        public InetSocketAddress getAddress() {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   214
            return new InetSocketAddress( InetAddress.getLoopbackAddress(), ss.getLocalPort());
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   215
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   216
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   217
        // This is a bit shaky. It doesn't handle continuation
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   218
        // lines, but our client shouldn't send any.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   219
        // Read a line from the input stream, swallowing the final
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   220
        // \r\n sequence. Stops at the first \n, doesn't complain
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   221
        // if it wasn't preceded by '\r'.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   222
        //
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   223
        String readLine(InputStream r) throws IOException {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   224
            StringBuilder b = new StringBuilder();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   225
            int c;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   226
            while ((c = r.read()) != -1) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   227
                if (c == '\n') break;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   228
                b.appendCodePoint(c);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   229
            }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   230
            if (b.codePointAt(b.length() -1) == '\r') {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   231
                b.delete(b.length() -1, b.length());
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   232
            }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   233
            return b.toString();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   234
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   235
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   236
        public void accept() {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   237
            Socket clientConnection = null;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   238
            try {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   239
                while (!stopped) {
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   240
                    System.out.println("Tunnel: Waiting for client");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   241
                    Socket toClose;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   242
                    try {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   243
                        toClose = clientConnection = ss.accept();
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   244
                    } catch (IOException io) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   245
                        if (DEBUG) io.printStackTrace(System.out);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   246
                        break;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   247
                    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   248
                    System.out.println("Tunnel: Client accepted");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   249
                    Socket targetConnection = null;
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   250
                    InputStream  ccis = clientConnection.getInputStream();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   251
                    OutputStream ccos = clientConnection.getOutputStream();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   252
                    Writer w = new OutputStreamWriter(ccos, "UTF-8");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   253
                    PrintWriter pw = new PrintWriter(w);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   254
                    System.out.println("Tunnel: Reading request line");
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   255
                    String requestLine = readLine(ccis);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   256
                    System.out.println("Tunnel: Request status line: " + requestLine);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   257
                    if (requestLine.startsWith("CONNECT ")) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   258
                        // We should probably check that the next word following
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   259
                        // CONNECT is the host:port of our HTTPS serverImpl.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   260
                        // Some improvement for a followup!
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   261
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   262
                        // Read all headers until we find the empty line that
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   263
                        // signals the end of all headers.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   264
                        while(!requestLine.equals("")) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   265
                            System.out.println("Tunnel: Reading header: "
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   266
                                    + (requestLine = readLine(ccis)));
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   267
                        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   268
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   269
                        // Open target connection
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   270
                        targetConnection = new Socket(
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   271
                                InetAddress.getLoopbackAddress(),
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   272
                                serverImpl.getAddress().getPort());
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   273
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   274
                        // Then send the 200 OK response to the client
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   275
                        System.out.println("Tunnel: Sending "
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   276
                                + "HTTP/1.1 200 OK\r\n\r\n");
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   277
                        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
   278
                        pw.flush();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   279
                    } else {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   280
                        // This should not happen. If it does then just print an
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   281
                        // error - both on out and err, and close the accepted
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   282
                        // socket
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   283
                        System.out.println("WARNING: Tunnel: Unexpected status line: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   284
                                + requestLine + " received by "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   285
                                + ss.getLocalSocketAddress()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   286
                                + " from "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   287
                                + toClose.getRemoteSocketAddress()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   288
                                + " - closing accepted socket");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   289
                        // Print on err
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   290
                        System.err.println("WARNING: Tunnel: Unexpected status line: "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   291
                                + requestLine + " received by "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   292
                                + ss.getLocalSocketAddress()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   293
                                + " from "
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   294
                                + toClose.getRemoteSocketAddress());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   295
                        // close accepted socket.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   296
                        toClose.close();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   297
                        System.err.println("Tunnel: accepted socket closed.");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   298
                        continue;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   299
                    }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   300
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   301
                    // Pipe the input stream of the client connection to the
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   302
                    // output stream of the target connection and conversely.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   303
                    // Now the client and target will just talk to each other.
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   304
                    System.out.println("Tunnel: Starting tunnel pipes");
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   305
                    CompletableFuture<Void> end, end1, end2;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   306
                    Thread t1 = pipe(ccis, targetConnection.getOutputStream(), '+',
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   307
                            end1 = new CompletableFuture<>());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   308
                    Thread t2 = pipe(targetConnection.getInputStream(), ccos, '-',
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   309
                            end2 = new CompletableFuture<>());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   310
                    end = CompletableFuture.allOf(end1, end2);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   311
                    end.whenComplete(
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   312
                            (r,t) -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   313
                                try { toClose.close(); } catch (IOException x) { }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   314
                                finally {connectionCFs.remove(end);}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   315
                            });
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   316
                    connectionCFs.add(end);
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   317
                    t1.start();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   318
                    t2.start();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   319
                }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   320
            } catch (Throwable ex) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   321
                try {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   322
                    ss.close();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   323
                } catch (IOException ex1) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   324
                    ex.addSuppressed(ex1);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   325
                }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   326
                ex.printStackTrace(System.err);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   327
            } finally {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   328
                System.out.println("Tunnel: exiting (stopped=" + stopped + ")");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   329
                connectionCFs.forEach(cf -> cf.complete(null));
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   330
            }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   331
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   332
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   333
        public void stop() throws IOException {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48083
diff changeset
   334
            stopped = true;
46157
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   335
            ss.close();
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   336
        }
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
    static class Configurator extends HttpsConfigurator {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   341
        public Configurator(SSLContext ctx) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   342
            super(ctx);
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   343
        }
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   344
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   345
        @Override
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   346
        public void configure (HttpsParameters params) {
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   347
            params.setSSLParameters (getSSLContext().getSupportedSSLParameters());
f3c2dcb8d8fe 8181422: ClassCastException in HTTP Client
dfuchs
parents:
diff changeset
   348
        }
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
}