jdk/test/sun/security/ssl/sun/net/www/protocol/https/HttpsURLConnection/B6226610.java
author ohair
Tue, 28 Dec 2010 15:53:50 -0800
changeset 7668 d4a77089c587
parent 6292 9da761dff0c7
permissions -rw-r--r--
6962318: Update copyright year Reviewed-by: xdono
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7668
d4a77089c587 6962318: Update copyright year
ohair
parents: 6292
diff changeset
     2
 * Copyright (c) 2005, 2010, 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
 * @test
6292
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    26
 * @bug 6226610 6973030
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @run main/othervm B6226610
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary HTTP tunnel connections send user headers to proxy
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/* This class includes a proxy server that processes the HTTP CONNECT request,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * and validates that the request does not have the user defined header in it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * The proxy server always returns 400 Bad Request so that the Http client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * will not try to proceed with the connection as there is no back end http server.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.net.*;
6292
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    39
import sun.net.www.MessageHeader;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
public class B6226610 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    static HeaderCheckerProxyTunnelServer proxy;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
6292
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    44
    public static void main(String[] args) throws Exception
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    {
6292
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    46
        proxy = new HeaderCheckerProxyTunnelServer();
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    47
        proxy.start();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
6292
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    49
        String hostname = InetAddress.getLocalHost().getHostName();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        try {
6292
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    52
           URL u = new URL("https://" + hostname + "/");
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    53
           System.out.println("Connecting to " + u);
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    54
           InetSocketAddress proxyAddr = new InetSocketAddress(hostname, proxy.getLocalPort());
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    55
           java.net.URLConnection c = u.openConnection(new Proxy(Proxy.Type.HTTP, proxyAddr));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
           /* I want this header to go to the destination server only, protected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            * by SSL
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
           c.setRequestProperty("X-TestHeader", "value");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
           c.connect();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
         } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            if ( e.getMessage().equals("Unable to tunnel through proxy. Proxy returns \"HTTP/1.1 400 Bad Request\"") )
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
               // OK. Proxy will always return 400 so that the main thread can terminate correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
               System.out.println(e);
6292
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    70
         } finally {
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    71
             if (proxy != null) proxy.shutdown();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
         }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
         if (HeaderCheckerProxyTunnelServer.failed)
6292
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
    75
            throw new RuntimeException("Test failed; see output");
2
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
class HeaderCheckerProxyTunnelServer extends Thread
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    public static boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    private static ServerSocket ss = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    // client requesting for a tunnel
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private Socket clientSocket = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Origin server's address and port that the client
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * wants to establish the tunnel for communication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    private InetAddress serverInetAddr;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    private int serverPort;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    public HeaderCheckerProxyTunnelServer() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
       if (ss == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
          ss = new ServerSocket(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
6292
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   102
    void shutdown() {
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   103
        try { ss.close(); } catch (IOException e) {}
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   104
    }
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   105
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public void run()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            clientSocket = ss.accept();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            processRequests();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            System.out.println("Proxy Failed: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                   ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            catch (IOException excep) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
               System.out.println("ProxyServer close error: " + excep);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
               excep.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * Returns the port on which the proxy is accepting connections.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public int getLocalPort() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return ss.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * Processes the CONNECT request
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private void processRequests() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        InputStream in = clientSocket.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        MessageHeader mheader = new MessageHeader(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        String statusLine = mheader.getValue(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (statusLine.startsWith("CONNECT")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
           // retrieve the host and port info from the status-line
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
           retrieveConnectInfo(statusLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
           if (mheader.findValue("X-TestHeader") != null) {
6292
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   145
             System.out.println("Proxy should not receive user defined headers for tunneled requests");
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   146
             failed = true;
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   147
           }
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   148
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   149
           // 6973030
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   150
           String value;
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   151
           if ((value = mheader.findValue("Proxy-Connection")) == null ||
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   152
                !value.equals("keep-alive")) {
9da761dff0c7 6973030: NTLM proxy authentication fails with https
chegar
parents: 5506
diff changeset
   153
             System.out.println("Proxy-Connection:keep-alive not being sent");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
             failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
           }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
           //This will allow the main thread to terminate without trying to perform the SSL handshake.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
           send400();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
           in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
           clientSocket.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
           ss.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            System.out.println("proxy server: processes only "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                   + "CONNECT method requests, recieved: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                                   + statusLine);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    private void send400() throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        OutputStream out = clientSocket.getOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        PrintWriter pout = new PrintWriter(out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        pout.println("HTTP/1.1 400 Bad Request");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        pout.println();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        pout.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    private void restart() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
         (new Thread(this)).start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * This method retrieves the hostname and port of the destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * that the connect request wants to establish a tunnel for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * communication.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * The input, connectStr is of the form:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *                          CONNECT server-name:server-port HTTP/1.x
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    private void retrieveConnectInfo(String connectStr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        int starti;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        int endi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        String connectInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
        String serverName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
            starti = connectStr.indexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
            endi = connectStr.lastIndexOf(' ');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            connectInfo = connectStr.substring(starti+1, endi).trim();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            // retrieve server name and port
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
            endi = connectInfo.indexOf(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
            serverName = connectInfo.substring(0, endi);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
            serverPort = Integer.parseInt(connectInfo.substring(endi+1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            throw new IOException("Proxy recieved a request: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                                        + connectStr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        serverInetAddr = InetAddress.getByName(serverName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
}