jdk/test/java/net/Authenticator/B6870935.java
author tschatzl
Wed, 16 Apr 2014 10:55:26 +0200
changeset 24100 7e71ac14ec06
parent 5506 202f599c92aa
child 30820 0d4717a011d3
permissions -rw-r--r--
8027553: Change the in_cset_fast_test functionality to use the G1BiasedArray abstraction Summary: Instead of using a manually managed array for the in_cset_fast_test array, use a G1BiasedArray instance. Reviewed-by: brutisso, mgerdin
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3952
diff changeset
     2
 * Copyright (c) 2001, 2009, Oracle and/or its affiliates. All rights reserved.
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     4
 *
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     8
 *
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    13
 * accompanied this code).
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    14
 *
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3952
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3952
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3952
diff changeset
    21
 * questions.
3952
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    22
 */
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    23
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    24
/**
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    25
 * @test
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    26
 * @bug 6870935
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    27
 * @run main/othervm -Dhttp.nonProxyHosts="" -Dhttp.auth.digest.validateProxy=true B6870935
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    28
 */
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    29
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    30
import java.io.*;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    31
import java.util.*;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    32
import java.net.*;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    33
import java.security.*;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    34
import sun.net.www.*;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    35
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    36
/* This is one simple test of the RFC2617 digest authentication behavior
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    37
 * It specifically tests that the client correctly checks the returned
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    38
 * Authentication-Info header field from the server and throws an exception
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    39
 * if the password is wrong
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    40
 */
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    41
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    42
public class B6870935 {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    43
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    44
    static char[] passwd = "password".toCharArray();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    45
    static String username = "user";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    46
    static String nonce = "abcdefghijklmnopqrstuvwxyz";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    47
    static String realm = "wallyworld";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    48
    static String uri = "http://www.ibm.com";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    49
    static volatile boolean error = false;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    50
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    51
    static class DigestServer extends Thread {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    52
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    53
        ServerSocket s;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    54
        InputStream  is;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    55
        OutputStream os;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    56
        int port;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    57
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    58
        String reply1 = "HTTP/1.1 407 Proxy Authentication Required\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    59
            "Proxy-Authenticate: Digest realm=\""+realm+"\" domain=/ "+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    60
            "nonce=\""+nonce+"\" qop=\"auth\"\r\n\r\n";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    61
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    62
        String reply2 = "HTTP/1.1 200 OK\r\n" +
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    63
            "Date: Mon, 15 Jan 2001 12:18:21 GMT\r\n" +
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    64
            "Server: Apache/1.3.14 (Unix)\r\n" +
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    65
            "Content-Type: text/html; charset=iso-8859-1\r\n" +
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    66
            "Transfer-encoding: chunked\r\n\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    67
            "B\r\nHelloWorld1\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    68
            "B\r\nHelloWorld2\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    69
            "B\r\nHelloWorld3\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    70
            "B\r\nHelloWorld4\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    71
            "B\r\nHelloWorld5\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    72
            "0\r\n"+
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    73
            "Proxy-Authentication-Info: ";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    74
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    75
        DigestServer (ServerSocket y) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    76
            s = y;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    77
            port = s.getLocalPort();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    78
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    79
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    80
        public void run () {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    81
            try {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    82
                Socket s1 = s.accept ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    83
                is = s1.getInputStream ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    84
                os = s1.getOutputStream ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    85
                is.read ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    86
                os.write (reply1.getBytes());
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    87
                Thread.sleep (2000);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    88
                s1.close ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    89
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    90
                s1 = s.accept ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    91
                is = s1.getInputStream ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    92
                os = s1.getOutputStream ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    93
                is.read ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    94
                // need to get the cnonce out of the response
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    95
                MessageHeader header = new MessageHeader (is);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    96
                String raw = header.findValue ("Proxy-Authorization");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    97
                HeaderParser parser = new HeaderParser (raw);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    98
                String cnonce = parser.findValue ("cnonce");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
    99
                String cnstring = parser.findValue ("nc");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   100
                String clientrsp = parser.findValue ("response");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   101
                String expected = computeDigest(
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   102
                        true, username,passwd,realm,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   103
                        "GET", uri, nonce, cnonce, cnstring
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   104
                );
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   105
                if (!expected.equals(clientrsp)) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   106
                    s1.close ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   107
                    s.close ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   108
                    error = true;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   109
                    return;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   110
                }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   111
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   112
                String reply = reply2 + getAuthorization (
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   113
                        realm, false, uri, "GET", cnonce,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   114
                        cnstring, passwd, username
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   115
                ) +"\r\n";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   116
                os.write (reply.getBytes());
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   117
                Thread.sleep (2000);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   118
                s1.close ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   119
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   120
            catch (Exception e) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   121
                System.out.println (e);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   122
                e.printStackTrace();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   123
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   124
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   125
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   126
        private String getAuthorization (String realm, boolean isRequest, String uri, String method, String cnonce, String cnstring, char[] password, String username) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   127
            String response;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   128
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   129
            try {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   130
                response = computeDigest(isRequest, username,passwd,realm,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   131
                                            method, uri, nonce, cnonce, cnstring);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   132
            } catch (NoSuchAlgorithmException ex) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   133
                return null;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   134
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   135
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   136
            String value = "Digest"
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   137
                            + " qop=\"auth"
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   138
                            + "\", cnonce=\"" + cnonce
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   139
                            + "\", rspauth=\"" + response
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   140
                            + "\", nc=\"" + cnstring + "\"";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   141
            return (value+ "\r\n");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   142
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   143
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   144
        private String computeDigest(
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   145
                            boolean isRequest, String userName, char[] password,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   146
                            String realm, String connMethod,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   147
                            String requestURI, String nonceString,
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   148
                            String cnonce, String ncValue
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   149
                        ) throws NoSuchAlgorithmException
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   150
        {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   151
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   152
            String A1, HashA1;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   153
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   154
            MessageDigest md = MessageDigest.getInstance("MD5");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   155
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   156
            {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   157
                A1 = userName + ":" + realm + ":";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   158
                HashA1 = encode(A1, password, md);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   159
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   160
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   161
            String A2;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   162
            if (isRequest) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   163
                A2 = connMethod + ":" + requestURI;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   164
            } else {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   165
                A2 = ":" + requestURI;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   166
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   167
            String HashA2 = encode(A2, null, md);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   168
            String combo, finalHash;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   169
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   170
            { /* RRC2617 when qop=auth */
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   171
                combo = HashA1+ ":" + nonceString + ":" + ncValue + ":" +
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   172
                            cnonce + ":auth:" +HashA2;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   173
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   174
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   175
            finalHash = encode(combo, null, md);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   176
            return finalHash;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   177
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   178
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   179
        private final static char charArray[] = {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   180
            '0', '1', '2', '3', '4', '5', '6', '7',
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   181
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   182
        };
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   183
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   184
        private String encode(String src, char[] passwd, MessageDigest md) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   185
            md.update(src.getBytes());
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   186
            if (passwd != null) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   187
                byte[] passwdBytes = new byte[passwd.length];
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   188
                for (int i=0; i<passwd.length; i++)
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   189
                    passwdBytes[i] = (byte)passwd[i];
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   190
                md.update(passwdBytes);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   191
                Arrays.fill(passwdBytes, (byte)0x00);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   192
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   193
            byte[] digest = md.digest();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   194
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   195
            StringBuffer res = new StringBuffer(digest.length * 2);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   196
            for (int i = 0; i < digest.length; i++) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   197
                int hashchar = ((digest[i] >>> 4) & 0xf);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   198
                res.append(charArray[hashchar]);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   199
                hashchar = (digest[i] & 0xf);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   200
                res.append(charArray[hashchar]);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   201
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   202
            return res.toString();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   203
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   204
    }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   205
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   206
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   207
    static class MyAuthenticator extends Authenticator {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   208
        public MyAuthenticator () {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   209
            super ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   210
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   211
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   212
        public PasswordAuthentication getPasswordAuthentication ()
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   213
        {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   214
            return (new PasswordAuthentication (username, passwd));
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   215
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   216
    }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   217
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   218
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   219
    public static void main(String[] args) throws Exception {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   220
        int nLoops = 1;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   221
        int nSize = 10;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   222
        int port, n =0;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   223
        byte b[] = new byte[nSize];
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   224
        DigestServer server;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   225
        ServerSocket sock;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   226
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   227
        try {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   228
            sock = new ServerSocket (0);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   229
            port = sock.getLocalPort ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   230
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   231
        catch (Exception e) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   232
            System.out.println ("Exception: " + e);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   233
            return;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   234
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   235
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   236
        server = new DigestServer(sock);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   237
        server.start ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   238
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   239
        try  {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   240
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   241
            Authenticator.setDefault (new MyAuthenticator ());
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   242
            SocketAddress addr = new InetSocketAddress ("127.0.0.1", port);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   243
            Proxy proxy = new Proxy (Proxy.Type.HTTP, addr);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   244
            String s = "http://www.ibm.com";
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   245
            URL url = new URL(s);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   246
            java.net.URLConnection conURL =  url.openConnection(proxy);
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   247
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   248
            InputStream in = conURL.getInputStream();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   249
            int c;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   250
            while ((c = in.read ()) != -1) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   251
            }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   252
            in.close ();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   253
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   254
        catch(IOException e) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   255
            e.printStackTrace();
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   256
            error = true;
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   257
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   258
        if (error) {
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   259
            throw new RuntimeException ("Error in test");
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   260
        }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   261
    }
dc329398de30 6870935: DIGEST proxy authentication fails to connect to URLs with no trailing slash
michaelm
parents:
diff changeset
   262
}