test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTestServer.java
author fyuan
Mon, 09 Oct 2017 17:37:15 +0800
changeset 47322 d94b2c6e26a9
parent 47216 71c04702a3d5
child 52387 8c0b1894d524
permissions -rw-r--r--
8187700: SetAuthenticator tests should handle the proxy port Reviewed-by: dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     1
/*
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     4
 *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    10
 *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    15
 * accompanied this code).
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    16
 *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    20
 *
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    23
 * questions.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    24
 */
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    25
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    26
import com.sun.net.httpserver.BasicAuthenticator;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    27
import com.sun.net.httpserver.Filter;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    28
import com.sun.net.httpserver.Headers;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    29
import com.sun.net.httpserver.HttpContext;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    30
import com.sun.net.httpserver.HttpExchange;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    31
import com.sun.net.httpserver.HttpHandler;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    32
import com.sun.net.httpserver.HttpServer;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    33
import com.sun.net.httpserver.HttpsConfigurator;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    34
import com.sun.net.httpserver.HttpsParameters;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    35
import com.sun.net.httpserver.HttpsServer;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    36
import java.io.IOException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    37
import java.io.InputStream;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    38
import java.io.OutputStream;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    39
import java.io.OutputStreamWriter;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    40
import java.io.PrintWriter;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    41
import java.io.Writer;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    42
import java.math.BigInteger;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    43
import java.net.HttpURLConnection;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    44
import java.net.InetAddress;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    45
import java.net.InetSocketAddress;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    46
import java.net.MalformedURLException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    47
import java.net.ServerSocket;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    48
import java.net.Socket;
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
    49
import java.net.SocketAddress;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    50
import java.net.URL;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    51
import java.security.MessageDigest;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    52
import java.security.NoSuchAlgorithmException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    53
import java.time.Instant;
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
    54
import java.util.ArrayList;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    55
import java.util.Arrays;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    56
import java.util.Base64;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    57
import java.util.List;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    58
import java.util.Objects;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    59
import java.util.Random;
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
    60
import java.util.concurrent.CopyOnWriteArrayList;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    61
import java.util.stream.Collectors;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    62
import javax.net.ssl.SSLContext;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    63
import sun.net.www.HeaderParser;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    64
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    65
/**
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    66
 * A simple HTTP server that supports Digest authentication.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    67
 * By default this server will echo back whatever is present
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    68
 * in the request body.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    69
 * @author danielfuchs
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    70
 */
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    71
public class HTTPTestServer extends HTTPTest {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    72
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    73
    final HttpServer      serverImpl; // this server endpoint
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    74
    final HTTPTestServer  redirect;   // the target server where to redirect 3xx
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    75
    final HttpHandler     delegate;   // unused
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    76
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    77
    private HTTPTestServer(HttpServer server, HTTPTestServer target,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    78
                           HttpHandler delegate) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    79
        this.serverImpl = server;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    80
        this.redirect = target;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    81
        this.delegate = delegate;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    82
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    83
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    84
    public static void main(String[] args)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    85
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    86
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    87
           HTTPTestServer server = create(HTTPTest.DEFAULT_PROTOCOL_TYPE,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    88
                                          HTTPTest.DEFAULT_HTTP_AUTH_TYPE,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    89
                                          HTTPTest.AUTHENTICATOR,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    90
                                          HTTPTest.DEFAULT_SCHEME_TYPE);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    91
           try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    92
               System.out.println("Server created at " + server.getAddress());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    93
               System.out.println("Strike <Return> to exit");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    94
               System.in.read();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    95
           } finally {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    96
               System.out.println("stopping server");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    97
               server.stop();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    98
           }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    99
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   100
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   101
    private static String toString(Headers headers) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   102
        return headers.entrySet().stream()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   103
                .map((e) -> e.getKey() + ": " + e.getValue())
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   104
                .collect(Collectors.joining("\n"));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   105
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   106
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   107
    public static HTTPTestServer create(HttpProtocolType protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   108
                                        HttpAuthType authType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   109
                                        HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   110
                                        HttpSchemeType schemeType)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   111
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   112
        return create(protocol, authType, auth, schemeType, null);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   113
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   114
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   115
    public static HTTPTestServer create(HttpProtocolType protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   116
                                        HttpAuthType authType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   117
                                        HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   118
                                        HttpSchemeType schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   119
                                        HttpHandler delegate)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   120
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   121
        Objects.requireNonNull(authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   122
        Objects.requireNonNull(auth);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   123
        switch(authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   124
            // A server that performs Server Digest authentication.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   125
            case SERVER: return createServer(protocol, authType, auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   126
                                             schemeType, delegate, "/");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   127
            // A server that pretends to be a Proxy and performs
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   128
            // Proxy Digest authentication. If protocol is HTTPS,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   129
            // then this will create a HttpsProxyTunnel that will
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   130
            // handle the CONNECT request for tunneling.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   131
            case PROXY: return createProxy(protocol, authType, auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   132
                                           schemeType, delegate, "/");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   133
            // A server that sends 307 redirect to a server that performs
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   134
            // Digest authentication.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   135
            // Note: 301 doesn't work here because it transforms POST into GET.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   136
            case SERVER307: return createServerAndRedirect(protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   137
                                                        HttpAuthType.SERVER,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   138
                                                        auth, schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   139
                                                        delegate, 307);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   140
            // A server that sends 305 redirect to a proxy that performs
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   141
            // Digest authentication.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   142
            case PROXY305:  return createServerAndRedirect(protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   143
                                                        HttpAuthType.PROXY,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   144
                                                        auth, schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   145
                                                        delegate, 305);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   146
            default:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   147
                throw new InternalError("Unknown server type: " + authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   148
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   149
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   150
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   151
    /**
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   152
     * The SocketBindableFactory ensures that the local port used by an HttpServer
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   153
     * or a proxy ServerSocket previously created by the current test/VM will not
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   154
     * get reused by a subsequent test in the same VM. This is to avoid having the
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   155
     * AuthCache reuse credentials from previous tests - which would invalidate the
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   156
     * assumptions made by the current test on when the default authenticator should
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   157
     * be called.
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   158
     */
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   159
    private static abstract class SocketBindableFactory<B> {
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   160
        private static final int MAX = 10;
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   161
        private static final CopyOnWriteArrayList<String> addresses =
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   162
            new CopyOnWriteArrayList<>();
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   163
        protected B createInternal() throws IOException {
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   164
            final int max = addresses.size() + MAX;
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   165
            final List<B> toClose = new ArrayList<>();
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   166
            try {
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   167
                for (int i = 1; i <= max; i++) {
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   168
                    B bindable = createBindable();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   169
                    SocketAddress address = getAddress(bindable);
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   170
                    String key = address.toString();
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   171
                    if (addresses.addIfAbsent(key)) {
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   172
                       System.out.println("Socket bound to: " + key
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   173
                                          + " after " + i + " attempt(s)");
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   174
                       return bindable;
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   175
                    }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   176
                    System.out.println("warning: address " + key
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   177
                                       + " already used. Retrying bind.");
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   178
                    // keep the port bound until we get a port that we haven't
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   179
                    // used already
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   180
                    toClose.add(bindable);
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   181
                }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   182
            } finally {
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   183
                // if we had to retry, then close the socket we're not
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   184
                // going to use.
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   185
                for (B b : toClose) {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   186
                  try { close(b); } catch (Exception x) { /* ignore */ }
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   187
                }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   188
            }
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   189
            throw new IOException("Couldn't bind socket after " + max + " attempts: "
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   190
                                  + "addresses used before: " + addresses);
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   191
        }
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   192
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   193
        protected abstract B createBindable() throws IOException;
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   194
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   195
        protected abstract SocketAddress getAddress(B bindable);
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   196
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   197
        protected abstract void close(B bindable) throws IOException;
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   198
    }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   199
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   200
    /*
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   201
     * Used to create ServerSocket for a proxy.
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   202
     */
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   203
    private static final class ServerSocketFactory
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   204
            extends SocketBindableFactory<ServerSocket> {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   205
        private static final ServerSocketFactory instance = new ServerSocketFactory();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   206
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   207
        static ServerSocket create() throws IOException {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   208
            return instance.createInternal();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   209
        }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   210
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   211
        @Override
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   212
        protected ServerSocket createBindable() throws IOException {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   213
            return new ServerSocket(0, 0, InetAddress.getByName("127.0.0.1"));
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   214
        }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   215
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   216
        @Override
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   217
        protected SocketAddress getAddress(ServerSocket socket) {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   218
            return socket.getLocalSocketAddress();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   219
        }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   220
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   221
        @Override
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   222
        protected void close(ServerSocket socket) throws IOException {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   223
            socket.close();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   224
        }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   225
    }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   226
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   227
    /*
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   228
     * Used to create HttpServer for a NTLMTestServer.
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   229
     */
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   230
    private static abstract class WebServerFactory<S extends HttpServer>
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   231
            extends SocketBindableFactory<S> {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   232
        @Override
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   233
        protected S createBindable() throws IOException {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   234
            S server = newHttpServer();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   235
            server.bind(new InetSocketAddress("127.0.0.1", 0), 0);
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   236
            return server;
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   237
        }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   238
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   239
        @Override
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   240
        protected SocketAddress getAddress(S server) {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   241
            return server.getAddress();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   242
        }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   243
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   244
        @Override
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   245
        protected void close(S server) throws IOException {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   246
            server.stop(1);
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   247
        }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   248
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   249
        /*
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   250
         * Returns a HttpServer or a HttpsServer in different subclasses.
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   251
         */
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   252
        protected abstract S newHttpServer() throws IOException;
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   253
    }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   254
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   255
    private static final class HttpServerFactory extends WebServerFactory<HttpServer> {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   256
        private static final HttpServerFactory instance = new HttpServerFactory();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   257
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   258
        static HttpServer create() throws IOException {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   259
            return instance.createInternal();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   260
        }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   261
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   262
        @Override
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   263
        protected HttpServer newHttpServer() throws IOException {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   264
            return HttpServer.create();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   265
        }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   266
    }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   267
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   268
    private static final class HttpsServerFactory extends WebServerFactory<HttpsServer> {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   269
        private static final HttpsServerFactory instance = new HttpsServerFactory();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   270
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   271
        static HttpsServer create() throws IOException {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   272
            return instance.createInternal();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   273
        }
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   274
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   275
        @Override
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   276
        protected HttpsServer newHttpServer() throws IOException {
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   277
            return HttpsServer.create();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   278
        }
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   279
    }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   280
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   281
    static HttpServer createHttpServer(HttpProtocolType protocol) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   282
        switch (protocol) {
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   283
            case HTTP:  return HttpServerFactory.create();
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   284
            case HTTPS: return configure(HttpsServerFactory.create());
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   285
            default: throw new InternalError("Unsupported protocol " + protocol);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   286
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   287
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   288
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   289
    static HttpsServer configure(HttpsServer server) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   290
        try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   291
            SSLContext ctx = SSLContext.getDefault();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   292
            server.setHttpsConfigurator(new Configurator(ctx));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   293
        } catch (NoSuchAlgorithmException ex) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   294
            throw new IOException(ex);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   295
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   296
        return server;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   297
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   298
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   299
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   300
    static void setContextAuthenticator(HttpContext ctxt,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   301
                                        HttpTestAuthenticator auth) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   302
        final String realm = auth.getRealm();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   303
        com.sun.net.httpserver.Authenticator authenticator =
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   304
            new BasicAuthenticator(realm) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   305
                @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   306
                public boolean checkCredentials(String username, String pwd) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   307
                    return auth.getUserName().equals(username)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   308
                           && new String(auth.getPassword(username)).equals(pwd);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   309
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   310
        };
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   311
        ctxt.setAuthenticator(authenticator);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   312
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   313
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   314
    public static HTTPTestServer createServer(HttpProtocolType protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   315
                                        HttpAuthType authType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   316
                                        HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   317
                                        HttpSchemeType schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   318
                                        HttpHandler delegate,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   319
                                        String path)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   320
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   321
        Objects.requireNonNull(authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   322
        Objects.requireNonNull(auth);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   323
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   324
        HttpServer impl = createHttpServer(protocol);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   325
        final HTTPTestServer server = new HTTPTestServer(impl, null, delegate);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   326
        final HttpHandler hh = server.createHandler(schemeType, auth, authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   327
        HttpContext ctxt = impl.createContext(path, hh);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   328
        server.configureAuthentication(ctxt, schemeType, auth, authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   329
        impl.start();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   330
        return server;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   331
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   332
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   333
    public static HTTPTestServer createProxy(HttpProtocolType protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   334
                                        HttpAuthType authType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   335
                                        HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   336
                                        HttpSchemeType schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   337
                                        HttpHandler delegate,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   338
                                        String path)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   339
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   340
        Objects.requireNonNull(authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   341
        Objects.requireNonNull(auth);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   342
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   343
        HttpServer impl = createHttpServer(protocol);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   344
        final HTTPTestServer server = protocol == HttpProtocolType.HTTPS
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   345
                ? new HttpsProxyTunnel(impl, null, delegate)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   346
                : new HTTPTestServer(impl, null, delegate);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   347
        final HttpHandler hh = server.createHandler(schemeType, auth, authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   348
        HttpContext ctxt = impl.createContext(path, hh);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   349
        server.configureAuthentication(ctxt, schemeType, auth, authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   350
        impl.start();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   352
        return server;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   353
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   354
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   355
    public static HTTPTestServer createServerAndRedirect(
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   356
                                        HttpProtocolType protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   357
                                        HttpAuthType targetAuthType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   358
                                        HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   359
                                        HttpSchemeType schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   360
                                        HttpHandler targetDelegate,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   361
                                        int code300)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   362
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   363
        Objects.requireNonNull(targetAuthType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   364
        Objects.requireNonNull(auth);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   365
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   366
        // The connection between client and proxy can only
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   367
        // be a plain connection: SSL connection to proxy
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   368
        // is not supported by our client connection.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   369
        HttpProtocolType targetProtocol = targetAuthType == HttpAuthType.PROXY
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   370
                                          ? HttpProtocolType.HTTP
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   371
                                          : protocol;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   372
        HTTPTestServer redirectTarget =
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   373
                (targetAuthType == HttpAuthType.PROXY)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   374
                ? createProxy(protocol, targetAuthType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   375
                              auth, schemeType, targetDelegate, "/")
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   376
                : createServer(targetProtocol, targetAuthType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   377
                               auth, schemeType, targetDelegate, "/");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   378
        HttpServer impl = createHttpServer(protocol);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   379
        final HTTPTestServer redirectingServer =
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   380
                 new HTTPTestServer(impl, redirectTarget, null);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   381
        InetSocketAddress redirectAddr = redirectTarget.getAddress();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   382
        URL locationURL = url(targetProtocol, redirectAddr, "/");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   383
        final HttpHandler hh = redirectingServer.create300Handler(locationURL,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   384
                                             HttpAuthType.SERVER, code300);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   385
        impl.createContext("/", hh);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   386
        impl.start();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   387
        return redirectingServer;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   388
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   389
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   390
    public InetSocketAddress getAddress() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   391
        return serverImpl.getAddress();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   392
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   393
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   394
    public void stop() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   395
        serverImpl.stop(0);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   396
        if (redirect != null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   397
            redirect.stop();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   398
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   399
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   400
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   401
    protected void writeResponse(HttpExchange he) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   402
        if (delegate == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   403
            he.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   404
            he.getResponseBody().write(he.getRequestBody().readAllBytes());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   405
        } else {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   406
            delegate.handle(he);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   407
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   408
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   409
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   410
    private HttpHandler createHandler(HttpSchemeType schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   411
                                      HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   412
                                      HttpAuthType authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   413
        return new HttpNoAuthHandler(authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   414
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   415
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   416
    private void configureAuthentication(HttpContext ctxt,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   417
                            HttpSchemeType schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   418
                            HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   419
                            HttpAuthType authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   420
        switch(schemeType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   421
            case DIGEST:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   422
                // DIGEST authentication is handled by the handler.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   423
                ctxt.getFilters().add(new HttpDigestFilter(auth, authType));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   424
                break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   425
            case BASIC:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   426
                // BASIC authentication is handled by the filter.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   427
                ctxt.getFilters().add(new HttpBasicFilter(auth, authType));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   428
                break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   429
            case BASICSERVER:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   430
                switch(authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   431
                    case PROXY: case PROXY305:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   432
                        // HttpServer can't support Proxy-type authentication
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   433
                        // => we do as if BASIC had been specified, and we will
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   434
                        //    handle authentication in the handler.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   435
                        ctxt.getFilters().add(new HttpBasicFilter(auth, authType));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   436
                        break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   437
                    case SERVER: case SERVER307:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   438
                        // Basic authentication is handled by HttpServer
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   439
                        // directly => the filter should not perform
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   440
                        // authentication again.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   441
                        setContextAuthenticator(ctxt, auth);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   442
                        ctxt.getFilters().add(new HttpNoAuthFilter(authType));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   443
                        break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   444
                    default:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   445
                        throw new InternalError("Invalid combination scheme="
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   446
                             + schemeType + " authType=" + authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   447
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   448
            case NONE:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   449
                // No authentication at all.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   450
                ctxt.getFilters().add(new HttpNoAuthFilter(authType));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   451
                break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   452
            default:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   453
                throw new InternalError("No such scheme: " + schemeType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   454
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   455
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   456
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   457
    private HttpHandler create300Handler(URL proxyURL,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   458
        HttpAuthType type, int code300) throws MalformedURLException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   459
        return new Http3xxHandler(proxyURL, type, code300);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   460
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   461
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   462
    // Abstract HTTP filter class.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   463
    private abstract static class AbstractHttpFilter extends Filter {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   464
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   465
        final HttpAuthType authType;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   466
        final String type;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   467
        public AbstractHttpFilter(HttpAuthType authType, String type) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   468
            this.authType = authType;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   469
            this.type = type;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   470
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   471
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   472
        String getLocation() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   473
            return "Location";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   474
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   475
        String getAuthenticate() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   476
            return authType == HttpAuthType.PROXY
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   477
                    ? "Proxy-Authenticate" : "WWW-Authenticate";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   478
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   479
        String getAuthorization() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   480
            return authType == HttpAuthType.PROXY
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   481
                    ? "Proxy-Authorization" : "Authorization";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   482
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   483
        int getUnauthorizedCode() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   484
            return authType == HttpAuthType.PROXY
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   485
                    ? HttpURLConnection.HTTP_PROXY_AUTH
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   486
                    : HttpURLConnection.HTTP_UNAUTHORIZED;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   487
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   488
        String getKeepAlive() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   489
            return "keep-alive";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   490
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   491
        String getConnection() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   492
            return authType == HttpAuthType.PROXY
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   493
                    ? "Proxy-Connection" : "Connection";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   494
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   495
        protected abstract boolean isAuthentified(HttpExchange he) throws IOException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   496
        protected abstract void requestAuthentication(HttpExchange he) throws IOException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   497
        protected void accept(HttpExchange he, Chain chain) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   498
            chain.doFilter(he);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   499
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   500
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   501
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   502
        public String description() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   503
            return "Filter for " + type;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   504
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   505
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   506
        public void doFilter(HttpExchange he, Chain chain) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   507
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   508
                System.out.println(type + ": Got " + he.getRequestMethod()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   509
                    + ": " + he.getRequestURI()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   510
                    + "\n" + HTTPTestServer.toString(he.getRequestHeaders()));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   511
                if (!isAuthentified(he)) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   512
                    try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   513
                        requestAuthentication(he);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   514
                        he.sendResponseHeaders(getUnauthorizedCode(), 0);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   515
                        System.out.println(type
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   516
                            + ": Sent back " + getUnauthorizedCode());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   517
                    } finally {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   518
                        he.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   519
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   520
                } else {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   521
                    accept(he, chain);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   522
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   523
            } catch (RuntimeException | Error | IOException t) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   524
               System.err.println(type
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   525
                    + ": Unexpected exception while handling request: " + t);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   526
               t.printStackTrace(System.err);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   527
               he.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   528
               throw t;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   529
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   530
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   531
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   532
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   533
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   534
    private final static class DigestResponse {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   535
        final String realm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   536
        final String username;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   537
        final String nonce;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   538
        final String cnonce;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   539
        final String nc;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   540
        final String uri;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   541
        final String algorithm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   542
        final String response;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   543
        final String qop;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   544
        final String opaque;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   545
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   546
        public DigestResponse(String realm, String username, String nonce,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   547
                              String cnonce, String nc, String uri,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   548
                              String algorithm, String qop, String opaque,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   549
                              String response) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   550
            this.realm = realm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   551
            this.username = username;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   552
            this.nonce = nonce;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   553
            this.cnonce = cnonce;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   554
            this.nc = nc;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   555
            this.uri = uri;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   556
            this.algorithm = algorithm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   557
            this.qop = qop;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   558
            this.opaque = opaque;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   559
            this.response = response;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   560
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   561
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   562
        String getAlgorithm(String defval) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   563
            return algorithm == null ? defval : algorithm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   564
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   565
        String getQoP(String defval) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   566
            return qop == null ? defval : qop;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   567
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   568
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   569
        // Code stolen from DigestAuthentication:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   570
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   571
        private static final char charArray[] = {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   572
            '0', '1', '2', '3', '4', '5', '6', '7',
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   573
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   574
        };
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   575
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   576
        private static String encode(String src, char[] passwd, MessageDigest md) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   577
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   578
                md.update(src.getBytes("ISO-8859-1"));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   579
            } catch (java.io.UnsupportedEncodingException uee) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   580
                assert false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   581
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   582
            if (passwd != null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   583
                byte[] passwdBytes = new byte[passwd.length];
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   584
                for (int i=0; i<passwd.length; i++)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   585
                    passwdBytes[i] = (byte)passwd[i];
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   586
                md.update(passwdBytes);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   587
                Arrays.fill(passwdBytes, (byte)0x00);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   588
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   589
            byte[] digest = md.digest();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   590
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   591
            StringBuilder res = new StringBuilder(digest.length * 2);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   592
            for (int i = 0; i < digest.length; i++) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   593
                int hashchar = ((digest[i] >>> 4) & 0xf);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   594
                res.append(charArray[hashchar]);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   595
                hashchar = (digest[i] & 0xf);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   596
                res.append(charArray[hashchar]);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   597
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   598
            return res.toString();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   599
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   600
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   601
        public static String computeDigest(boolean isRequest,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   602
                                            String reqMethod,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   603
                                            char[] password,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   604
                                            DigestResponse params)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   605
            throws NoSuchAlgorithmException
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   606
        {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   607
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   608
            String A1, HashA1;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   609
            String algorithm = params.getAlgorithm("MD5");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   610
            boolean md5sess = algorithm.equalsIgnoreCase ("MD5-sess");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   611
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   612
            MessageDigest md = MessageDigest.getInstance(md5sess?"MD5":algorithm);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   613
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   614
            if (params.username == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   615
                throw new IllegalArgumentException("missing username");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   616
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   617
            if (params.realm == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   618
                throw new IllegalArgumentException("missing realm");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   619
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   620
            if (params.uri == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   621
                throw new IllegalArgumentException("missing uri");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   622
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   623
            if (params.nonce == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   624
                throw new IllegalArgumentException("missing nonce");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   625
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   626
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   627
            A1 = params.username + ":" + params.realm + ":";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   628
            HashA1 = encode(A1, password, md);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   629
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   630
            String A2;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   631
            if (isRequest) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   632
                A2 = reqMethod + ":" + params.uri;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   633
            } else {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   634
                A2 = ":" + params.uri;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   635
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   636
            String HashA2 = encode(A2, null, md);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   637
            String combo, finalHash;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   638
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   639
            if ("auth".equals(params.qop)) { /* RRC2617 when qop=auth */
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   640
                if (params.cnonce == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   641
                    throw new IllegalArgumentException("missing nonce");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   642
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   643
                if (params.nc == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   644
                    throw new IllegalArgumentException("missing nonce");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   645
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   646
                combo = HashA1+ ":" + params.nonce + ":" + params.nc + ":" +
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   647
                            params.cnonce + ":auth:" +HashA2;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   648
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   649
            } else { /* for compatibility with RFC2069 */
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   650
                combo = HashA1 + ":" +
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   651
                           params.nonce + ":" +
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   652
                           HashA2;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   653
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   654
            finalHash = encode(combo, null, md);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   655
            return finalHash;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   656
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   657
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   658
        public static DigestResponse create(String raw) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   659
            String username, realm, nonce, nc, uri, response, cnonce,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   660
                   algorithm, qop, opaque;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   661
            HeaderParser parser = new HeaderParser(raw);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   662
            username = parser.findValue("username");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   663
            realm = parser.findValue("realm");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   664
            nonce = parser.findValue("nonce");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   665
            nc = parser.findValue("nc");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   666
            uri = parser.findValue("uri");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   667
            cnonce = parser.findValue("cnonce");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   668
            response = parser.findValue("response");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   669
            algorithm = parser.findValue("algorithm");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   670
            qop = parser.findValue("qop");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   671
            opaque = parser.findValue("opaque");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   672
            return new DigestResponse(realm, username, nonce, cnonce, nc, uri,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   673
                                      algorithm, qop, opaque, response);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   674
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   675
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   676
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   677
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   678
    private class HttpNoAuthFilter extends AbstractHttpFilter {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   679
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   680
        public HttpNoAuthFilter(HttpAuthType authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   681
            super(authType, authType == HttpAuthType.SERVER
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   682
                            ? "NoAuth Server" : "NoAuth Proxy");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   683
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   684
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   685
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   686
        protected boolean isAuthentified(HttpExchange he) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   687
            return true;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   688
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   689
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   690
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   691
        protected void requestAuthentication(HttpExchange he) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   692
            throw new InternalError("Should not com here");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   693
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   694
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   695
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   696
        public String description() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   697
            return "Passthrough Filter";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   698
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   699
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   700
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   701
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   702
    // An HTTP Filter that performs Basic authentication
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   703
    private class HttpBasicFilter extends AbstractHttpFilter {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   704
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   705
        private final HttpTestAuthenticator auth;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   706
        public HttpBasicFilter(HttpTestAuthenticator auth, HttpAuthType authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   707
            super(authType, authType == HttpAuthType.SERVER
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   708
                            ? "Basic Server" : "Basic Proxy");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   709
            this.auth = auth;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   710
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   711
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   712
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   713
        protected void requestAuthentication(HttpExchange he)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   714
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   715
            he.getResponseHeaders().add(getAuthenticate(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   716
                 "Basic realm=\"" + auth.getRealm() + "\"");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   717
            System.out.println(type + ": Requesting Basic Authentication "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   718
                 + he.getResponseHeaders().getFirst(getAuthenticate()));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   719
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   720
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   721
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   722
        protected boolean isAuthentified(HttpExchange he) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   723
            if (he.getRequestHeaders().containsKey(getAuthorization())) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   724
                List<String> authorization =
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   725
                    he.getRequestHeaders().get(getAuthorization());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   726
                for (String a : authorization) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   727
                    System.out.println(type + ": processing " + a);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   728
                    int sp = a.indexOf(' ');
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   729
                    if (sp < 0) return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   730
                    String scheme = a.substring(0, sp);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   731
                    if (!"Basic".equalsIgnoreCase(scheme)) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   732
                        System.out.println(type + ": Unsupported scheme '"
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   733
                                           + scheme +"'");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   734
                        return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   735
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   736
                    if (a.length() <= sp+1) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   737
                        System.out.println(type + ": value too short for '"
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   738
                                            + scheme +"'");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   739
                        return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   740
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   741
                    a = a.substring(sp+1);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   742
                    return validate(a);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   743
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   744
                return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   745
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   746
            return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   747
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   748
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   749
        boolean validate(String a) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   750
            byte[] b = Base64.getDecoder().decode(a);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   751
            String userpass = new String (b);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   752
            int colon = userpass.indexOf (':');
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   753
            String uname = userpass.substring (0, colon);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   754
            String pass = userpass.substring (colon+1);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   755
            return auth.getUserName().equals(uname) &&
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   756
                   new String(auth.getPassword(uname)).equals(pass);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   757
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   758
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   759
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   760
        public String description() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   761
            return "Filter for " + type;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   762
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   763
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   764
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   765
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   766
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   767
    // An HTTP Filter that performs Digest authentication
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   768
    private class HttpDigestFilter extends AbstractHttpFilter {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   769
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   770
        // This is a very basic DIGEST - used only for the purpose of testing
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   771
        // the client implementation. Therefore we can get away with never
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   772
        // updating the server nonce as it makes the implementation of the
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   773
        // server side digest simpler.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   774
        private final HttpTestAuthenticator auth;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   775
        private final byte[] nonce;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   776
        private final String ns;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   777
        public HttpDigestFilter(HttpTestAuthenticator auth, HttpAuthType authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   778
            super(authType, authType == HttpAuthType.SERVER
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   779
                            ? "Digest Server" : "Digest Proxy");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   780
            this.auth = auth;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   781
            nonce = new byte[16];
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   782
            new Random(Instant.now().toEpochMilli()).nextBytes(nonce);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   783
            ns = new BigInteger(1, nonce).toString(16);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   784
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   785
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   786
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   787
        protected void requestAuthentication(HttpExchange he)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   788
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   789
            he.getResponseHeaders().add(getAuthenticate(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   790
                 "Digest realm=\"" + auth.getRealm() + "\","
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   791
                 + "\r\n    qop=\"auth\","
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   792
                 + "\r\n    nonce=\"" + ns +"\"");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   793
            System.out.println(type + ": Requesting Digest Authentication "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   794
                 + he.getResponseHeaders().getFirst(getAuthenticate()));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   795
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   796
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   797
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   798
        protected boolean isAuthentified(HttpExchange he) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   799
            if (he.getRequestHeaders().containsKey(getAuthorization())) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   800
                List<String> authorization = he.getRequestHeaders().get(getAuthorization());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   801
                for (String a : authorization) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   802
                    System.out.println(type + ": processing " + a);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   803
                    int sp = a.indexOf(' ');
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   804
                    if (sp < 0) return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   805
                    String scheme = a.substring(0, sp);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   806
                    if (!"Digest".equalsIgnoreCase(scheme)) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   807
                        System.out.println(type + ": Unsupported scheme '" + scheme +"'");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   808
                        return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   809
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   810
                    if (a.length() <= sp+1) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   811
                        System.out.println(type + ": value too short for '" + scheme +"'");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   812
                        return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   813
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   814
                    a = a.substring(sp+1);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   815
                    DigestResponse dgr = DigestResponse.create(a);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   816
                    return validate(he.getRequestMethod(), dgr);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   817
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   818
                return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   819
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   820
            return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   821
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   822
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   823
        boolean validate(String reqMethod, DigestResponse dg) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   824
            if (!"MD5".equalsIgnoreCase(dg.getAlgorithm("MD5"))) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   825
                System.out.println(type + ": Unsupported algorithm "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   826
                                   + dg.algorithm);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   827
                return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   828
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   829
            if (!"auth".equalsIgnoreCase(dg.getQoP("auth"))) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   830
                System.out.println(type + ": Unsupported qop "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   831
                                   + dg.qop);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   832
                return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   833
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   834
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   835
                if (!dg.nonce.equals(ns)) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   836
                    System.out.println(type + ": bad nonce returned by client: "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   837
                                    + nonce + " expected " + ns);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   838
                    return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   839
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   840
                if (dg.response == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   841
                    System.out.println(type + ": missing digest response.");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   842
                    return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   843
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   844
                char[] pa = auth.getPassword(dg.username);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   845
                return verify(reqMethod, dg, pa);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   846
            } catch(IllegalArgumentException | SecurityException
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   847
                    | NoSuchAlgorithmException e) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   848
                System.out.println(type + ": " + e.getMessage());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   849
                return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   850
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   851
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   852
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   853
        boolean verify(String reqMethod, DigestResponse dg, char[] pw)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   854
            throws NoSuchAlgorithmException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   855
            String response = DigestResponse.computeDigest(true, reqMethod, pw, dg);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   856
            if (!dg.response.equals(response)) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   857
                System.out.println(type + ": bad response returned by client: "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   858
                                    + dg.response + " expected " + response);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   859
                return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   860
            } else {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   861
                System.out.println(type + ": verified response " + response);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   862
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   863
            return true;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   864
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   865
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   866
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   867
        public String description() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   868
            return "Filter for DIGEST authentication";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   869
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   870
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   871
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   872
    // Abstract HTTP handler class.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   873
    private abstract static class AbstractHttpHandler implements HttpHandler {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   874
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   875
        final HttpAuthType authType;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   876
        final String type;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   877
        public AbstractHttpHandler(HttpAuthType authType, String type) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   878
            this.authType = authType;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   879
            this.type = type;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   880
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   881
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   882
        String getLocation() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   883
            return "Location";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   884
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   885
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   886
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   887
        public void handle(HttpExchange he) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   888
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   889
                sendResponse(he);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   890
            } catch (RuntimeException | Error | IOException t) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   891
               System.err.println(type
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   892
                    + ": Unexpected exception while handling request: " + t);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   893
               t.printStackTrace(System.err);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   894
               throw t;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   895
            } finally {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   896
                he.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   897
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   898
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   899
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   900
        protected abstract void sendResponse(HttpExchange he) throws IOException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   901
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   902
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   903
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   904
    private class HttpNoAuthHandler extends AbstractHttpHandler {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   905
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   906
        public HttpNoAuthHandler(HttpAuthType authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   907
            super(authType, authType == HttpAuthType.SERVER
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   908
                            ? "NoAuth Server" : "NoAuth Proxy");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   909
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   910
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   911
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   912
        protected void sendResponse(HttpExchange he) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   913
            HTTPTestServer.this.writeResponse(he);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   914
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   915
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   916
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   917
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   918
    // A dummy HTTP Handler that redirects all incoming requests
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   919
    // by sending a back 3xx response code (301, 305, 307 etc..)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   920
    private class Http3xxHandler extends AbstractHttpHandler {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   921
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   922
        private final URL redirectTargetURL;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   923
        private final int code3XX;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   924
        public Http3xxHandler(URL proxyURL, HttpAuthType authType, int code300) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   925
            super(authType, "Server" + code300);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   926
            this.redirectTargetURL = proxyURL;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   927
            this.code3XX = code300;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   928
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   929
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   930
        int get3XX() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   931
            return code3XX;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   932
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   933
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   934
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   935
        public void sendResponse(HttpExchange he) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   936
            System.out.println(type + ": Got " + he.getRequestMethod()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   937
                    + ": " + he.getRequestURI()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   938
                    + "\n" + HTTPTestServer.toString(he.getRequestHeaders()));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   939
            System.out.println(type + ": Redirecting to "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   940
                               + (authType == HttpAuthType.PROXY305
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   941
                                    ? "proxy" : "server"));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   942
            he.getResponseHeaders().add(getLocation(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   943
                redirectTargetURL.toExternalForm().toString());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   944
            he.sendResponseHeaders(get3XX(), 0);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   945
            System.out.println(type + ": Sent back " + get3XX() + " "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   946
                 + getLocation() + ": " + redirectTargetURL.toExternalForm().toString());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   947
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   948
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   949
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   950
    static class Configurator extends HttpsConfigurator {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   951
        public Configurator(SSLContext ctx) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   952
            super(ctx);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   953
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   954
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   955
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   956
        public void configure (HttpsParameters params) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   957
            params.setSSLParameters (getSSLContext().getSupportedSSLParameters());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   958
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   959
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   960
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   961
    // This is a bit hacky: HttpsProxyTunnel is an HTTPTestServer hidden
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   962
    // behind a fake proxy that only understands CONNECT requests.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   963
    // The fake proxy is just a server socket that intercept the
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   964
    // CONNECT and then redirect streams to the real server.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   965
    static class HttpsProxyTunnel extends HTTPTestServer
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   966
            implements Runnable {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   967
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   968
        final ServerSocket ss;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   969
        public HttpsProxyTunnel(HttpServer server, HTTPTestServer target,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   970
                               HttpHandler delegate)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   971
                throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   972
            super(server, target, delegate);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   973
            System.out.flush();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   974
            System.err.println("WARNING: HttpsProxyTunnel is an experimental test class");
47322
d94b2c6e26a9 8187700: SetAuthenticator tests should handle the proxy port
fyuan
parents: 47216
diff changeset
   975
            ss = ServerSocketFactory.create();
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   976
            start();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   977
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   978
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   979
        final void start() throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   980
            Thread t = new Thread(this, "ProxyThread");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   981
            t.setDaemon(true);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   982
            t.start();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   983
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   984
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   985
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   986
        public void stop() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   987
            super.stop();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   988
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   989
                ss.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   990
            } catch (IOException ex) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   991
                if (DEBUG) ex.printStackTrace(System.out);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   992
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   993
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   994
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   995
        // Pipe the input stream to the output stream.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   996
        private synchronized Thread pipe(InputStream is, OutputStream os, char tag) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   997
            return new Thread("TunnelPipe("+tag+")") {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   998
                @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   999
                public void run() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1000
                    try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1001
                        try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1002
                            int c;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1003
                            while ((c = is.read()) != -1) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1004
                                os.write(c);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1005
                                os.flush();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1006
                                // if DEBUG prints a + or a - for each transferred
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1007
                                // character.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1008
                                if (DEBUG) System.out.print(tag);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1009
                            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1010
                            is.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1011
                        } finally {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1012
                            os.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1013
                        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1014
                    } catch (IOException ex) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1015
                        if (DEBUG) ex.printStackTrace(System.out);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1016
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1017
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1018
            };
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1019
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1020
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1021
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1022
        public InetSocketAddress getAddress() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1023
            return new InetSocketAddress(ss.getInetAddress(), ss.getLocalPort());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1024
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1025
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1026
        // This is a bit shaky. It doesn't handle continuation
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1027
        // lines, but our client shouldn't send any.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1028
        // Read a line from the input stream, swallowing the final
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1029
        // \r\n sequence. Stops at the first \n, doesn't complain
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1030
        // if it wasn't preceded by '\r'.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1031
        //
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1032
        String readLine(InputStream r) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1033
            StringBuilder b = new StringBuilder();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1034
            int c;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1035
            while ((c = r.read()) != -1) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1036
                if (c == '\n') break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1037
                b.appendCodePoint(c);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1038
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1039
            if (b.codePointAt(b.length() -1) == '\r') {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1040
                b.delete(b.length() -1, b.length());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1041
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1042
            return b.toString();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1043
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1044
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1045
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1046
        public void run() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1047
            Socket clientConnection = null;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1048
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1049
                while (true) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1050
                    System.out.println("Tunnel: Waiting for client");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1051
                    Socket previous = clientConnection;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1052
                    try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1053
                        clientConnection = ss.accept();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1054
                    } catch (IOException io) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1055
                        if (DEBUG) io.printStackTrace(System.out);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1056
                        break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1057
                    } finally {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1058
                        // close the previous connection
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1059
                        if (previous != null) previous.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1060
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1061
                    System.out.println("Tunnel: Client accepted");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1062
                    Socket targetConnection = null;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1063
                    InputStream  ccis = clientConnection.getInputStream();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1064
                    OutputStream ccos = clientConnection.getOutputStream();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1065
                    Writer w = new OutputStreamWriter(
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1066
                                   clientConnection.getOutputStream(), "UTF-8");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1067
                    PrintWriter pw = new PrintWriter(w);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1068
                    System.out.println("Tunnel: Reading request line");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1069
                    String requestLine = readLine(ccis);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1070
                    System.out.println("Tunnel: Request line: " + requestLine);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1071
                    if (requestLine.startsWith("CONNECT ")) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1072
                        // We should probably check that the next word following
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1073
                        // CONNECT is the host:port of our HTTPS serverImpl.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1074
                        // Some improvement for a followup!
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1075
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1076
                        // Read all headers until we find the empty line that
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1077
                        // signals the end of all headers.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1078
                        while(!requestLine.equals("")) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1079
                            System.out.println("Tunnel: Reading header: "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1080
                                               + (requestLine = readLine(ccis)));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1081
                        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1082
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1083
                        targetConnection = new Socket(
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1084
                                serverImpl.getAddress().getAddress(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1085
                                serverImpl.getAddress().getPort());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1086
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1087
                        // Then send the 200 OK response to the client
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1088
                        System.out.println("Tunnel: Sending "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1089
                                           + "HTTP/1.1 200 OK\r\n\r\n");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1090
                        pw.print("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1091
                        pw.flush();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1092
                    } else {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1093
                        // This should not happen. If it does let our serverImpl
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1094
                        // deal with it.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1095
                        throw new IOException("Tunnel: Unexpected status line: "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1096
                                             + requestLine);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1097
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1098
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1099
                    // Pipe the input stream of the client connection to the
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1100
                    // output stream of the target connection and conversely.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1101
                    // Now the client and target will just talk to each other.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1102
                    System.out.println("Tunnel: Starting tunnel pipes");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1103
                    Thread t1 = pipe(ccis, targetConnection.getOutputStream(), '+');
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1104
                    Thread t2 = pipe(targetConnection.getInputStream(), ccos, '-');
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1105
                    t1.start();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1106
                    t2.start();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1107
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1108
                    // We have only 1 client... wait until it has finished before
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1109
                    // accepting a new connection request.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1110
                    t1.join();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1111
                    t2.join();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1112
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1113
            } catch (Throwable ex) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1114
                try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1115
                    ss.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1116
                } catch (IOException ex1) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1117
                    ex.addSuppressed(ex1);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1118
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1119
                ex.printStackTrace(System.err);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1120
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1121
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1122
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1123
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1124
}