jdk/test/sun/net/www/http/HttpClient/B8025710.java
author chegar
Wed, 23 Apr 2014 13:19:21 +0100
changeset 24045 31a23ee57590
child 44274 b99239096fed
permissions -rw-r--r--
8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server Reviewed-by: chegar Contributed-by: Steven Lawrance <slawrance@salesforce.com>, Andreas Rieber <rieberandreas@gmail.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
24045
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
     1
/*
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
     4
 *
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
     8
 *
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    13
 * accompanied this code).
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    14
 *
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    18
 *
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    21
 * questions.
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    22
 */
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    23
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    24
import java.io.*;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    25
import java.net.*;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    26
import java.security.*;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    27
import java.security.cert.X509Certificate;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    28
import java.util.ArrayList;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    29
import java.util.concurrent.atomic.AtomicBoolean;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    30
import java.util.regex.Matcher;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    31
import java.util.regex.Pattern;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    32
import javax.net.ServerSocketFactory;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    33
import javax.net.SocketFactory;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    34
import javax.net.ssl.*;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    35
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    36
/**
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    37
 * @test
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    38
 * @bug 8025710
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    39
 * @summary Proxied https connection reuse by HttpClient can send CONNECT to the server
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    40
 */
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    41
public class B8025710 {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    42
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    43
    private final static AtomicBoolean connectInServer = new AtomicBoolean();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    44
    private static final String keystorefile =
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    45
            System.getProperty("test.src", "./")
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    46
            + "/../../../../../javax/net/ssl/etc/keystore";
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    47
    private static final String passphrase = "passphrase";
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    48
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    49
    public static void main(String[] args) throws Exception {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    50
        new B8025710().runTest();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    51
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    52
        if (connectInServer.get())
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    53
            throw new RuntimeException("TEST FAILED: server got proxy header");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    54
        else
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    55
            System.out.println("TEST PASSED");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    56
    }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    57
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    58
    private void runTest() throws Exception {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    59
        ProxyServer proxyServer = new ProxyServer();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    60
        HttpServer httpServer = new HttpServer();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    61
        httpServer.start();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    62
        proxyServer.start();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    63
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    64
        URL url = new URL("https", InetAddress.getLocalHost().getHostName(),
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    65
                httpServer.getPort(), "/");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    66
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    67
        Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyServer.getAddress());
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    68
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    69
        HttpsURLConnection.setDefaultSSLSocketFactory(createTestSSLSocketFactory());
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    70
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    71
        // Make two connections. The bug occurs when the second request is made
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    72
        for (int i = 0; i < 2; i++) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    73
            System.out.println("Client: Requesting " + url.toExternalForm()
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    74
                    + " via " + proxy.toString()
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    75
                    + " (attempt " + (i + 1) + " of 2)");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    76
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    77
            HttpsURLConnection connection =
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    78
                    (HttpsURLConnection) url.openConnection(proxy);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    79
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    80
            connection.setRequestMethod("POST");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    81
            connection.setDoInput(true);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    82
            connection.setDoOutput(true);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    83
            connection.setRequestProperty("User-Agent", "Test/1.0");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    84
            connection.getOutputStream().write("Hello, world!".getBytes("UTF-8"));
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    85
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    86
            if (connection.getResponseCode() != 200) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    87
                System.err.println("Client: Unexpected response code "
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    88
                        + connection.getResponseCode());
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    89
                break;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    90
            }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    91
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    92
            String response = readLine(connection.getInputStream());
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    93
            if (!"Hi!".equals(response)) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    94
                System.err.println("Client: Unexpected response body: "
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    95
                        + response);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    96
            }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    97
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    98
        httpServer.close();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
    99
        proxyServer.close();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   100
        httpServer.join();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   101
        proxyServer.join();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   102
    }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   103
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   104
    class ProxyServer extends Thread implements Closeable {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   105
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   106
        private final ServerSocket proxySocket;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   107
        private final Pattern connectLinePattern =
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   108
                Pattern.compile("^CONNECT ([^: ]+):([0-9]+) HTTP/[0-9.]+$");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   109
        private final String PROXY_RESPONSE =
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   110
                "HTTP/1.0 200 Connection Established\r\n"
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   111
                + "Proxy-Agent: TestProxy/1.0\r\n"
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   112
                + "\r\n";
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   113
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   114
        ProxyServer() throws Exception {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   115
            super("ProxyServer Thread");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   116
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   117
            // Create the http proxy server socket
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   118
            proxySocket = ServerSocketFactory.getDefault().createServerSocket();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   119
            proxySocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   120
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   121
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   122
        public SocketAddress getAddress() { return  proxySocket.getLocalSocketAddress(); }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   123
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   124
        @Override
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   125
        public void close() throws IOException {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   126
            proxySocket.close();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   127
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   128
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   129
        @Override
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   130
        public void run() {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   131
            ArrayList<Thread> threads = new ArrayList<>();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   132
            int connectionCount = 0;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   133
            try {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   134
                while (connectionCount++ < 2) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   135
                    final Socket clientSocket = proxySocket.accept();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   136
                    final int proxyConnectionCount = connectionCount;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   137
                    System.out.println("Proxy: NEW CONNECTION "
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   138
                            + proxyConnectionCount);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   139
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   140
                    Thread t = new Thread("ProxySocket" + proxyConnectionCount) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   141
                        @Override
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   142
                        public void run() {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   143
                            try {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   144
                                String firstLine =
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   145
                                        readHeader(clientSocket.getInputStream());
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   146
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   147
                                Matcher connectLineMatcher =
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   148
                                        connectLinePattern.matcher(firstLine);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   149
                                if (!connectLineMatcher.matches()) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   150
                                    System.out.println("Proxy: Unexpected"
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   151
                                            + " request to the proxy: "
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   152
                                            + firstLine);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   153
                                    return;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   154
                                }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   155
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   156
                                String host    = connectLineMatcher.group(1);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   157
                                String portStr = connectLineMatcher.group(2);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   158
                                int port       = Integer.parseInt(portStr);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   159
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   160
                                Socket serverSocket = SocketFactory.getDefault()
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   161
                                        .createSocket(host, port);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   162
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   163
                                clientSocket.getOutputStream()
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   164
                                        .write(PROXY_RESPONSE.getBytes("UTF-8"));
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   165
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   166
                                ProxyTunnel copyToClient =
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   167
                                        new ProxyTunnel(serverSocket, clientSocket);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   168
                                ProxyTunnel copyToServer =
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   169
                                        new ProxyTunnel(clientSocket, serverSocket);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   170
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   171
                                copyToClient.start();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   172
                                copyToServer.start();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   173
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   174
                                copyToClient.join();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   175
                                // here copyToClient.close() would not provoke the
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   176
                                // bug ( since it would trigger the retry logic in
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   177
                                // HttpURLConnction.writeRequests ), so close only
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   178
                                // the output to get the connection in this state.
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   179
                                clientSocket.shutdownOutput();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   180
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   181
                                try {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   182
                                    Thread.sleep(3000);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   183
                                } catch (InterruptedException ignored) { }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   184
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   185
                                // now close all connections to finish the test
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   186
                                copyToServer.close();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   187
                                copyToClient.close();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   188
                            } catch (IOException | NumberFormatException
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   189
                                    | InterruptedException e) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   190
                                e.printStackTrace();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   191
                            }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   192
                        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   193
                    };
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   194
                    threads.add(t);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   195
                    t.start();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   196
                }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   197
                for (Thread t: threads)
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   198
                    t.join();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   199
            } catch (IOException | InterruptedException e) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   200
                e.printStackTrace();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   201
            }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   202
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   203
    }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   204
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   205
    /**
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   206
     * This inner class provides unidirectional data flow through the sockets
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   207
     * by continuously copying bytes from the input socket onto the output
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   208
     * socket, until both sockets are open and EOF has not been received.
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   209
     */
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   210
    class ProxyTunnel extends Thread {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   211
        private final Socket sockIn;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   212
        private final Socket sockOut;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   213
        private final InputStream input;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   214
        private final OutputStream output;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   215
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   216
        public ProxyTunnel(Socket sockIn, Socket sockOut) throws IOException {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   217
            super("ProxyTunnel");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   218
            this.sockIn  = sockIn;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   219
            this.sockOut = sockOut;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   220
            input  = sockIn.getInputStream();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   221
            output = sockOut.getOutputStream();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   222
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   223
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   224
        public void run() {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   225
            byte[] buf = new byte[8192];
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   226
            int bytesRead;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   227
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   228
            try {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   229
                while ((bytesRead = input.read(buf)) >= 0) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   230
                    output.write(buf, 0, bytesRead);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   231
                    output.flush();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   232
                }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   233
            } catch (IOException ignored) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   234
                close();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   235
            }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   236
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   237
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   238
        public void close() {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   239
            try {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   240
                if (!sockIn.isClosed())
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   241
                    sockIn.close();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   242
                if (!sockOut.isClosed())
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   243
                    sockOut.close();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   244
            } catch (IOException ignored) { }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   245
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   246
    }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   247
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   248
    /**
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   249
     * the server thread
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   250
     */
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   251
    class HttpServer extends Thread implements Closeable {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   252
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   253
        private final ServerSocket serverSocket;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   254
        private final SSLSocketFactory sslSocketFactory;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   255
        private final String serverResponse =
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   256
                "HTTP/1.1 200 OK\r\n"
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   257
                + "Content-Type: text/plain\r\n"
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   258
                + "Content-Length: 3\r\n"
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   259
                + "\r\n"
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   260
                + "Hi!";
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   261
        private int connectionCount = 0;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   262
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   263
        HttpServer() throws Exception {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   264
            super("HttpServer Thread");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   265
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   266
            KeyStore ks = KeyStore.getInstance("JKS");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   267
            ks.load(new FileInputStream(keystorefile), passphrase.toCharArray());
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   268
            KeyManagerFactory factory = KeyManagerFactory.getInstance("SunX509");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   269
            factory.init(ks, passphrase.toCharArray());
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   270
            SSLContext ctx = SSLContext.getInstance("TLS");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   271
            ctx.init(factory.getKeyManagers(), null, null);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   272
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   273
            sslSocketFactory = ctx.getSocketFactory();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   274
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   275
            // Create the server that the test wants to connect to via the proxy
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   276
            serverSocket = ServerSocketFactory.getDefault().createServerSocket();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   277
            serverSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(), 0));
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   278
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   279
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   280
        public int getPort() { return  serverSocket.getLocalPort(); }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   281
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   282
        @Override
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   283
        public void close() throws IOException { serverSocket.close(); }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   284
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   285
        @Override
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   286
        public void run() {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   287
            try {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   288
                while (connectionCount++ < 2) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   289
                    Socket socket = serverSocket.accept();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   290
                    System.out.println("Server: NEW CONNECTION "
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   291
                            + connectionCount);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   292
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   293
                    SSLSocket sslSocket = (SSLSocket) sslSocketFactory
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   294
                            .createSocket(socket,null, getPort(), false);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   295
                    sslSocket.setUseClientMode(false);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   296
                    sslSocket.startHandshake();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   297
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   298
                    String firstLine = readHeader(sslSocket.getInputStream());
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   299
                    if (firstLine != null && firstLine.contains("CONNECT")) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   300
                        System.out.println("Server: BUG! HTTP CONNECT"
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   301
                                + " encountered: " + firstLine);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   302
                        connectInServer.set(true);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   303
                    }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   304
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   305
                    // write the success response, the request body is not read.
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   306
                    // close only output and keep input open.
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   307
                    OutputStream out = sslSocket.getOutputStream();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   308
                    out.write(serverResponse.getBytes("UTF-8"));
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   309
                    socket.shutdownOutput();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   310
                }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   311
            } catch (IOException e) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   312
                e.printStackTrace();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   313
            }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   314
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   315
    }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   316
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   317
    /**
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   318
     * read the header and return only the first line.
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   319
     *
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   320
     * @param inputStream the stream to read from
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   321
     * @return the first line of the stream
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   322
     * @throws IOException if reading failed
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   323
     */
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   324
    private static String readHeader(InputStream inputStream)
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   325
            throws IOException {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   326
        String line;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   327
        String firstLine = null;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   328
        while ((line = readLine(inputStream)) != null && line.length() > 0) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   329
            if (firstLine == null) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   330
                firstLine = line;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   331
            }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   332
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   333
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   334
        return firstLine;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   335
    }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   336
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   337
    /**
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   338
     * read a line from stream.
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   339
     *
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   340
     * @param inputStream the stream to read from
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   341
     * @return the line
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   342
     * @throws IOException  if reading failed
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   343
     */
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   344
    private static String readLine(InputStream inputStream)
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   345
            throws IOException {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   346
        final StringBuilder line = new StringBuilder();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   347
        int ch;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   348
        while ((ch = inputStream.read()) != -1) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   349
            if (ch == '\r') {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   350
                continue;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   351
            }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   352
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   353
            if (ch == '\n') {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   354
                break;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   355
            }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   356
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   357
            line.append((char) ch);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   358
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   359
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   360
        return line.toString();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   361
    }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   362
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   363
    private SSLSocketFactory createTestSSLSocketFactory() {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   364
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   365
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   366
            @Override
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   367
            public boolean verify(String hostname, SSLSession sslSession) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   368
                // ignore the cert's CN; it's not important to this test
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   369
                return true;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   370
            }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   371
        });
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   372
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   373
        // Set up the socket factory to use a trust manager that trusts all
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   374
        // certs, since trust validation isn't important to this test
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   375
        final TrustManager[] trustAllCertChains = new TrustManager[] {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   376
            new X509TrustManager() {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   377
                @Override
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   378
                public X509Certificate[] getAcceptedIssuers() {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   379
                    return null;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   380
                }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   381
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   382
                @Override
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   383
                public void checkClientTrusted(X509Certificate[] certs,
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   384
                        String authType) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   385
                }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   386
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   387
                @Override
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   388
                public void checkServerTrusted(X509Certificate[] certs,
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   389
                        String authType) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   390
                }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   391
            }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   392
        };
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   393
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   394
        final SSLContext sc;
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   395
        try {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   396
            sc = SSLContext.getInstance("TLS");
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   397
        } catch (NoSuchAlgorithmException e) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   398
            throw new RuntimeException(e);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   399
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   400
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   401
        try {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   402
            sc.init(null, trustAllCertChains, new java.security.SecureRandom());
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   403
        } catch (KeyManagementException e) {
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   404
            throw new RuntimeException(e);
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   405
        }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   406
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   407
        return sc.getSocketFactory();
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   408
    }
31a23ee57590 8025710: Proxied HTTPS connections reused by HttpClient can send CONNECT to the server
chegar
parents:
diff changeset
   409
}