jdk/test/java/net/Socket/HttpProxy.java
author coleenp
Wed, 17 Jun 2015 21:44:48 +0000
changeset 31362 8957ccbb5821
parent 30820 0d4717a011d3
permissions -rw-r--r--
8098821: Crash in system dictionary initialization with shared strings Summary: map string regions after the compressed class base is known Reviewed-by: iklam, dcubed Contributed-by: coleen.phillimore@oracle.com, mikhailo.seledtsov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16061
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
     1
/*
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
     2
 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
     4
 *
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
     8
 *
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    13
 * accompanied this code).
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    14
 *
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    18
 *
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    21
 * questions.
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    22
 */
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    23
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    24
/*
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    25
 * @test
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    26
 * @bug 6370908
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    27
 * @summary Add support for HTTP_CONNECT proxy in Socket class
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 16061
diff changeset
    28
 * @modules java.base/sun.net.www
16061
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    29
 */
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    30
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    31
import java.io.IOException;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    32
import java.io.InputStream;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    33
import java.io.OutputStream;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    34
import java.io.PrintWriter;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    35
import static java.lang.System.out;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    36
import java.net.InetAddress;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    37
import java.net.InetSocketAddress;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    38
import java.net.Proxy;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    39
import java.net.ServerSocket;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    40
import java.net.Socket;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    41
import sun.net.www.MessageHeader;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    42
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    43
public class HttpProxy {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    44
    final String proxyHost;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    45
    final int proxyPort;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    46
    static final int SO_TIMEOUT = 15000;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    47
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    48
    public static void main(String[] args) throws Exception {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    49
        String host;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    50
        int port;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    51
        if (args.length == 0) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    52
            // Start internal proxy
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    53
            ConnectProxyTunnelServer proxy = new ConnectProxyTunnelServer();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    54
            proxy.start();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    55
            host = "localhost";
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    56
            port = proxy.getLocalPort();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    57
            out.println("Running with internal proxy: " + host + ":" + port);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    58
        } else if (args.length == 2) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    59
            host = args[0];
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    60
            port = Integer.valueOf(args[1]);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    61
            out.println("Running against specified proxy server: " + host + ":" + port);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    62
        } else {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    63
            System.err.println("Usage: java HttpProxy [<proxy host> <proxy port>]");
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    64
            return;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    65
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    66
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    67
        HttpProxy p = new HttpProxy(host, port);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    68
        p.test();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    69
    }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    70
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    71
    public HttpProxy(String proxyHost, int proxyPort) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    72
        this.proxyHost = proxyHost;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    73
        this.proxyPort = proxyPort;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    74
    }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    75
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    76
    void test() throws Exception {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    77
        InetSocketAddress proxyAddress = new InetSocketAddress(proxyHost, proxyPort);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    78
        Proxy httpProxy = new Proxy(Proxy.Type.HTTP, proxyAddress);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    79
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    80
        try (ServerSocket ss = new ServerSocket(0);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    81
             Socket sock = new Socket(httpProxy)) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    82
            sock.setSoTimeout(SO_TIMEOUT);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    83
            sock.setTcpNoDelay(false);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    84
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    85
            InetSocketAddress externalAddress =
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    86
                new InetSocketAddress(InetAddress.getLocalHost(), ss.getLocalPort());
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    87
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    88
            out.println("Trying to connect to server socket on " + externalAddress);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    89
            sock.connect(externalAddress);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    90
            try (Socket externalSock = ss.accept()) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    91
                // perform some simple checks
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    92
                check(sock.isBound(), "Socket is not bound");
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    93
                check(sock.isConnected(), "Socket is not connected");
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    94
                check(!sock.isClosed(), "Socket should not be closed");
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    95
                check(sock.getSoTimeout() == SO_TIMEOUT,
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    96
                        "Socket should have a previously set timeout");
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    97
                check(sock.getTcpNoDelay() ==  false, "NODELAY should be false");
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    98
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
    99
                simpleDataExchange(sock, externalSock);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   100
            }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   101
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   102
    }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   103
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   104
    static void check(boolean condition, String message) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   105
        if (!condition) out.println(message);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   106
    }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   107
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   108
    static Exception unexpected(Exception e) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   109
        out.println("Unexcepted Exception: " + e);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   110
        e.printStackTrace();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   111
        return e;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   112
    }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   113
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   114
    // performs a simple exchange of data between the two sockets
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   115
    // and throws an exception if there is any problem.
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   116
    void simpleDataExchange(Socket s1, Socket s2) throws Exception {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   117
        try (final InputStream i1 = s1.getInputStream();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   118
             final InputStream i2 = s2.getInputStream();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   119
             final OutputStream o1 = s1.getOutputStream();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   120
             final OutputStream o2 = s2.getOutputStream()) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   121
            startSimpleWriter("simpleWriter1", o1, 100);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   122
            startSimpleWriter("simpleWriter2", o2, 200);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   123
            simpleRead(i2, 100);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   124
            simpleRead(i1, 200);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   125
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   126
    }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   127
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   128
    void startSimpleWriter(String threadName, final OutputStream os, final int start) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   129
        (new Thread(new Runnable() {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   130
            public void run() {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   131
                try { simpleWrite(os, start); }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   132
                catch (Exception e) {unexpected(e); }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   133
            }}, threadName)).start();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   134
    }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   135
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   136
    void simpleWrite(OutputStream os, int start) throws Exception {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   137
        byte b[] = new byte[2];
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   138
        for (int i=start; i<start+100; i++) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   139
            b[0] = (byte) (i / 256);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   140
            b[1] = (byte) (i % 256);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   141
            os.write(b);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   142
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   143
    }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   144
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   145
    void simpleRead(InputStream is, int start) throws Exception {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   146
        byte b[] = new byte [2];
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   147
        for (int i=start; i<start+100; i++) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   148
            int x = is.read(b);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   149
            if (x == 1)
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   150
                x += is.read(b,1,1);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   151
            if (x!=2)
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   152
                throw new Exception("read error");
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   153
            int r = bytes(b[0], b[1]);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   154
            if (r != i)
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   155
                throw new Exception("read " + r + " expected " +i);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   156
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   157
    }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   158
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   159
    int bytes(byte b1, byte b2) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   160
        int i1 = (int)b1 & 0xFF;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   161
        int i2 = (int)b2 & 0xFF;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   162
        return i1 * 256 + i2;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   163
    }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   164
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   165
    static class ConnectProxyTunnelServer extends Thread {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   166
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   167
        private final ServerSocket ss;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   168
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   169
        public ConnectProxyTunnelServer() throws IOException {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   170
            ss = new ServerSocket(0);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   171
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   172
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   173
        @Override
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   174
        public void run() {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   175
            try (Socket clientSocket = ss.accept()) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   176
                processRequest(clientSocket);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   177
            } catch (Exception e) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   178
                out.println("Proxy Failed: " + e);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   179
                e.printStackTrace();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   180
            } finally {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   181
                try { ss.close(); } catch (IOException x) { unexpected(x); }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   182
            }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   183
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   184
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   185
        /**
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   186
         * Returns the port on which the proxy is accepting connections.
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   187
         */
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   188
        public int getLocalPort() {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   189
            return ss.getLocalPort();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   190
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   191
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   192
        /*
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   193
         * Processes the CONNECT request
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   194
         */
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   195
        private void processRequest(Socket clientSocket) throws Exception {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   196
            MessageHeader mheader = new MessageHeader(clientSocket.getInputStream());
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   197
            String statusLine = mheader.getValue(0);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   198
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   199
            if (!statusLine.startsWith("CONNECT")) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   200
                out.println("proxy server: processes only "
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   201
                                  + "CONNECT method requests, recieved: "
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   202
                                  + statusLine);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   203
                return;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   204
            }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   205
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   206
            // retrieve the host and port info from the status-line
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   207
            InetSocketAddress serverAddr = getConnectInfo(statusLine);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   208
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   209
            //open socket to the server
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   210
            try (Socket serverSocket = new Socket(serverAddr.getAddress(),
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   211
                                                  serverAddr.getPort())) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   212
                Forwarder clientFW = new Forwarder(clientSocket.getInputStream(),
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   213
                                                   serverSocket.getOutputStream());
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   214
                Thread clientForwarderThread = new Thread(clientFW, "ClientForwarder");
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   215
                clientForwarderThread.start();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   216
                send200(clientSocket);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   217
                Forwarder serverFW = new Forwarder(serverSocket.getInputStream(),
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   218
                                                   clientSocket.getOutputStream());
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   219
                serverFW.run();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   220
                clientForwarderThread.join();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   221
            }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   222
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   223
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   224
        private void send200(Socket clientSocket) throws IOException {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   225
            OutputStream out = clientSocket.getOutputStream();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   226
            PrintWriter pout = new PrintWriter(out);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   227
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   228
            pout.println("HTTP/1.1 200 OK");
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   229
            pout.println();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   230
            pout.flush();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   231
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   232
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   233
        /*
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   234
         * This method retrieves the hostname and port of the tunnel destination
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   235
         * from the request line.
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   236
         * @param connectStr
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   237
         *        of the form: <i>CONNECT server-name:server-port HTTP/1.x</i>
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   238
         */
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   239
        static InetSocketAddress getConnectInfo(String connectStr)
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   240
            throws Exception
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   241
        {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   242
            try {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   243
                int starti = connectStr.indexOf(' ');
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   244
                int endi = connectStr.lastIndexOf(' ');
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   245
                String connectInfo = connectStr.substring(starti+1, endi).trim();
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   246
                // retrieve server name and port
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   247
                endi = connectInfo.indexOf(':');
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   248
                String name = connectInfo.substring(0, endi);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   249
                int port = Integer.parseInt(connectInfo.substring(endi+1));
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   250
                return new InetSocketAddress(name, port);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   251
            } catch (Exception e) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   252
                out.println("Proxy recieved a request: " + connectStr);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   253
                throw unexpected(e);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   254
            }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   255
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   256
    }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   257
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   258
    /* Reads from the given InputStream and writes to the given OutputStream */
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   259
    static class Forwarder implements Runnable
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   260
    {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   261
        private final InputStream in;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   262
        private final OutputStream os;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   263
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   264
        Forwarder(InputStream in, OutputStream os) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   265
            this.in = in;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   266
            this.os = os;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   267
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   268
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   269
        @Override
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   270
        public void run() {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   271
            try {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   272
                byte[] ba = new byte[1024];
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   273
                int count;
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   274
                while ((count = in.read(ba)) != -1) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   275
                    os.write(ba, 0, count);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   276
                }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   277
            } catch (IOException e) {
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   278
                unexpected(e);
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   279
            }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   280
        }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   281
    }
c80133bafef0 6370908: Add support for HTTP_CONNECT proxy in Socket class
chegar
parents:
diff changeset
   282
}