jdk/test/sun/net/www/protocol/http/ProxyTunnelServer.java
author msheppar
Sat, 19 Jan 2013 08:39:20 +0000
changeset 15276 bbddb82e66ce
parent 5506 202f599c92aa
child 23010 6dadb192ad81
permissions -rw-r--r--
8006568: HTTP protocol handler NLTM Authentication should use Base64 API Reviewed-by: chegar, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2002, 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
 * It is used by the TunnelThroughProxy test.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.net.*;
15276
bbddb82e66ce 8006568: HTTP protocol handler NLTM Authentication should use Base64 API
msheppar
parents: 5506
diff changeset
    34
import java.util.Base64;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.net.ssl.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import javax.net.ServerSocketFactory;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import sun.net.www.*;
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private static ServerSocket ss = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
     * holds the registered user's username and password
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
     * only one such entry is maintained
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private String userPlusPass;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    // client requesting for a tunnel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    private Socket clientSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Origin server's address and port that the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * wants to establish the tunnel for communication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private InetAddress serverInetAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    private int serverPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * denote whether the proxy needs to authorize
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * CONNECT requests.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    static boolean needAuth = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    public ProxyTunnelServer() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        if (ss == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
          ss = (ServerSocket) ServerSocketFactory.getDefault().
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
          createServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    public void needUserAuth(boolean auth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        needAuth = auth;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * register users with the proxy, by providing username and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * password. The username and password are used for authorizing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * user when a CONNECT request is made and needAuth is set to true.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    public void setUserAuth(String uname, String passwd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        userPlusPass = uname + ":" + passwd;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            clientSocket = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            processRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            System.out.println("Proxy Failed: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            catch (IOException excep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                System.out.println("ProxyServer close error: " + excep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                excep.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * Processes the CONNECT requests, if needAuth is set to true, then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * the name and password are extracted from the Proxy-Authorization header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * of the request. They are checked against the one that is registered,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * if there is a match, connection is set in tunneling mode. If
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * needAuth is set to false, Proxy-Authorization checks are not made
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    private void processRequests() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        InputStream in = clientSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        MessageHeader mheader = new MessageHeader(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        String statusLine = mheader.getValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        if (statusLine.startsWith("CONNECT")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            // retrieve the host and port info from the status-line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            // retrieveConnectInfo(statusLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (needAuth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                String authInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                if ((authInfo = mheader.findValue("Proxy-Authorization"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                                         != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                   if (authenticate(authInfo)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        needAuth = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                        System.out.println(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
                                "Proxy: client authentication successful");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
            respondForConnect(needAuth);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // connection set to the tunneling mode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            if (!needAuth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
                // doTunnel();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
                 * done with tunneling, we process only one successful
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                 * tunneling request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
                // we may get another request with Proxy-Authorization set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
                in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
                clientSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
                restart();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            System.out.println("proxy server: processes only "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                                   + "CONNECT method requests, recieved: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                                   + statusLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    private void respondForConnect(boolean needAuth) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        OutputStream out = clientSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        PrintWriter pout = new PrintWriter(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        if (needAuth) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            pout.println("HTTP/1.1 407 Proxy Auth Required");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            pout.println("Proxy-Authenticate: Basic realm=\"WallyWorld\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            pout.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            pout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            pout.println("HTTP/1.1 500 Server Error");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            pout.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            pout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    private void restart() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
         (new Thread(this)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    /*sc
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * note: Tunneling has to be provided in both directions, i.e
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * from client->server and server->client, even if the application
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * data may be unidirectional, SSL handshaking data flows in either
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * direction.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private void doTunnel() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
        Socket serverSocket = new Socket(serverInetAddr, serverPort);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        ProxyTunnel clientToServer = new ProxyTunnel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                                clientSocket, serverSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        ProxyTunnel serverToClient = new ProxyTunnel(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
                                serverSocket, clientSocket);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        clientToServer.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        serverToClient.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        System.out.println("Proxy: Started tunneling.......");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        clientToServer.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        serverToClient.join();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        System.out.println("Proxy: Finished tunneling........");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        clientToServer.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        serverToClient.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * This inner class provides unidirectional data flow through the sockets
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * by continuously copying bytes from the input socket onto the output
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * socket, until both sockets are open and EOF has not been received.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    class ProxyTunnel extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        Socket sockIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        Socket sockOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
        InputStream input;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        OutputStream output;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        public ProxyTunnel(Socket sockIn, Socket sockOut)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            this.sockIn = sockIn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            this.sockOut = sockOut;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
            input = sockIn.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
            output = sockOut.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
        public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            int BUFFER_SIZE = 400;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            byte[] buf = new byte[BUFFER_SIZE];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
            int count = 0;  // keep track of the amount of data transfer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
                while ((bytesRead = input.read(buf)) >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                    output.write(buf, 0, bytesRead);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
                    output.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
                    count += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                 * The peer end has closed the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                 * we will close the tunnel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
              }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
        public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                if (!sockIn.isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                    sockIn.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                if (!sockOut.isClosed())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                    sockOut.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
            } catch (IOException ignored) { }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     ***************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *                  helper methods follow
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     ***************************************************************
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * This method retrieves the hostname and port of the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * that the connect request wants to establish a tunnel for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * communication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * The input, connectStr is of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     *                          CONNECT server-name:server-port HTTP/1.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    private void retrieveConnectInfo(String connectStr) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        int starti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        int endi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        String connectInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        String serverName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            starti = connectStr.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
            endi = connectStr.lastIndexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            connectInfo = connectStr.substring(starti+1, endi).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // retrieve server name and port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            endi = connectInfo.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            serverName = connectInfo.substring(0, endi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
            serverPort = Integer.parseInt(connectInfo.substring(endi+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            throw new IOException("Proxy recieved a request: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                                        + connectStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        serverInetAddr = InetAddress.getByName(serverName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    public int getPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        return ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * do "basic" authentication, authInfo is of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *                                  Basic <encoded username":"password>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * reference RFC 2617
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    private boolean authenticate(String authInfo) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        boolean matched = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            authInfo.trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            int ind = authInfo.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            String recvdUserPlusPass = authInfo.substring(ind + 1).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            // extract encoded (username:passwd
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            if (userPlusPass.equals(
15276
bbddb82e66ce 8006568: HTTP protocol handler NLTM Authentication should use Base64 API
msheppar
parents: 5506
diff changeset
   299
                                new String(Base64.getDecoder().decode(recvdUserPlusPass))
bbddb82e66ce 8006568: HTTP protocol handler NLTM Authentication should use Base64 API
msheppar
parents: 5506
diff changeset
   300
                                )) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                matched = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
              throw new IOException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                "Proxy received invalid Proxy-Authorization value: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                 + authInfo);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        return matched;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
}