jdk/test/sun/net/www/protocol/https/HttpsURLConnection/ProxyTunnelServer.java
author mli
Tue, 03 Jan 2017 21:05:46 -0800
changeset 42989 224e91ad76c4
parent 23052 241885315119
permissions -rw-r--r--
8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout Summary: The fix sets timeout for the server and the client, and ignore SocketTimeoutException. Reviewed-by: chegar Contributed-by: John Jiang <sha.jiang@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
42989
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
     2
 * Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * This class includes a proxy server that processes HTTP CONNECT requests,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * and tunnels the data from the client to the server, once the CONNECT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * request is accepted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * The proxy server processes only one transaction, i.e  a CONNECT request
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * followed by the corresponding tunnel data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.net.ServerSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.net.www.*;
16020
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 5506
diff changeset
    37
import java.util.Base64;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
public class ProxyTunnelServer extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
42989
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
    41
    private static final int TIMEOUT = 30000;
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
    42
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static ServerSocket ss = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     * holds the registered user's username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * only one such entry is maintained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private String userPlusPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    // client requesting for a tunnel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private Socket clientSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * Origin server's address and port that the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * wants to establish the tunnel for communication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private InetAddress serverInetAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private int serverPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * denote whether the proxy needs to authorize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * CONNECT requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    static boolean needAuth = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public ProxyTunnelServer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (ss == null) {
42989
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
    68
            ss = (ServerSocket) ServerSocketFactory.getDefault()
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
    69
                    .createServerSocket(0);
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
    70
            ss.setSoTimeout(TIMEOUT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public void needUserAuth(boolean auth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        needAuth = auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * register users with the proxy, by providing username and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * password. The username and password are used for authorizing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * user when a CONNECT request is made and needAuth is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    public void setUserAuth(String uname, String passwd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        userPlusPass = uname + ":" + passwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            clientSocket = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            processRequests();
42989
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
    91
        } catch (SocketTimeoutException e) {
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
    92
            System.out.println(
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
    93
                    "Proxy can not get response in time: " + e.getMessage());
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            System.out.println("Proxy Failed: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            catch (IOException excep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                System.out.println("ProxyServer close error: " + excep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                excep.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Processes the CONNECT requests, if needAuth is set to true, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * the name and password are extracted from the Proxy-Authorization header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * of the request. They are checked against the one that is registered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * if there is a match, connection is set in tunneling mode. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * needAuth is set to false, Proxy-Authorization checks are not made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    private void processRequests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        InputStream in = clientSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        MessageHeader mheader = new MessageHeader(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        String statusLine = mheader.getValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (statusLine.startsWith("CONNECT")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            // retrieve the host and port info from the status-line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            retrieveConnectInfo(statusLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            if (needAuth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                String authInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                if ((authInfo = mheader.findValue("Proxy-Authorization"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                                         != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                   if (authenticate(authInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                        needAuth = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                                "Proxy: client authentication successful");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            respondForConnect(needAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            // connection set to the tunneling mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            if (!needAuth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
                doTunnel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                 * done with tunneling, we process only one successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                 * tunneling request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
                ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                // we may get another request with Proxy-Authorization set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                clientSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            System.out.println("proxy server: processes only "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                   + "CONNECT method requests, recieved: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                   + statusLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    private void respondForConnect(boolean needAuth) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        OutputStream out = clientSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        PrintWriter pout = new PrintWriter(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        if (needAuth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            pout.println("HTTP/1.1 407 Proxy Auth Required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            pout.println("Proxy-Authenticate: Basic realm=\"WallyWorld\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            pout.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            pout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            pout.println("HTTP/1.1 200 OK");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            pout.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
            pout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    private void restart() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
         (new Thread(this)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * note: Tunneling has to be provided in both directions, i.e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * from client->server and server->client, even if the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     * data may be unidirectional, SSL handshaking data flows in either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    private void doTunnel() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        Socket serverSocket = new Socket(serverInetAddr, serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        ProxyTunnel clientToServer = new ProxyTunnel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                                clientSocket, serverSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
        ProxyTunnel serverToClient = new ProxyTunnel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                                serverSocket, clientSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        clientToServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        serverToClient.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        System.out.println("Proxy: Started tunneling.......");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
42989
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
   196
        clientToServer.join(TIMEOUT);
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
   197
        serverToClient.join(TIMEOUT);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        System.out.println("Proxy: Finished tunneling........");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        clientToServer.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
        serverToClient.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * This inner class provides unidirectional data flow through the sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * by continuously copying bytes from the input socket onto the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     * socket, until both sockets are open and EOF has not been received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
    class ProxyTunnel extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        Socket sockIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        Socket sockOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
        InputStream input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        OutputStream output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        public ProxyTunnel(Socket sockIn, Socket sockOut)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            this.sockIn = sockIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            this.sockOut = sockOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            input = sockIn.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            output = sockOut.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            int BUFFER_SIZE = 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            byte[] buf = new byte[BUFFER_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
            int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                while ((bytesRead = input.read(buf)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    output.write(buf, 0, bytesRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                    output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                 * The peer end has closed the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                 * we will close the tunnel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
42989
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
   242
        private void close() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                if (!sockIn.isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    sockIn.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                if (!sockOut.isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
                    sockOut.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            } catch (IOException ignored) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     ***************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     *                  helper methods follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     ***************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * This method retrieves the hostname and port of the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * that the connect request wants to establish a tunnel for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     * communication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * The input, connectStr is of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     *                          CONNECT server-name:server-port HTTP/1.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private void retrieveConnectInfo(String connectStr) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        int starti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        int endi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        String connectInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        String serverName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            starti = connectStr.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            endi = connectStr.lastIndexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            connectInfo = connectStr.substring(starti+1, endi).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            // retrieve server name and port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            endi = connectInfo.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
            serverName = connectInfo.substring(0, endi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
            serverPort = Integer.parseInt(connectInfo.substring(endi+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
            throw new IOException("Proxy recieved a request: "
42989
224e91ad76c4 8170641: sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.sh fails with timeout
mli
parents: 23052
diff changeset
   281
                                        + connectStr, e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        serverInetAddr = InetAddress.getByName(serverName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        return ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * do "basic" authentication, authInfo is of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *                                  Basic <encoded username":"password>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * reference RFC 2617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
    private boolean authenticate(String authInfo) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
        boolean matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            authInfo.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
            int ind = authInfo.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            String recvdUserPlusPass = authInfo.substring(ind + 1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // extract encoded (username:passwd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            if (userPlusPass.equals(
16020
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 5506
diff changeset
   303
                            new String( Base64.getMimeDecoder()
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 5506
diff changeset
   304
                                        .decode(recvdUserPlusPass))))
b57c48f16179 8006182: cleanup to use java.util.Base64 in java security component, providers, and regression tests
msheppar
parents: 5506
diff changeset
   305
            {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                matched = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
              throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                "Proxy received invalid Proxy-Authorization value: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                 + authInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
        return matched;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
}