test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTestServer.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 43508 jdk/test/java/net/HttpURLConnection/SetAuthenticator/HTTPTestServer.java@6b8bca7f0cee
child 47322 d94b2c6e26a9
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
/*
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
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;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    49
import java.net.URL;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    50
import java.security.MessageDigest;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    51
import java.security.NoSuchAlgorithmException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    52
import java.time.Instant;
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
    53
import java.util.ArrayList;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    54
import java.util.Arrays;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    55
import java.util.Base64;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    56
import java.util.List;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    57
import java.util.Objects;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    58
import java.util.Random;
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
    59
import java.util.concurrent.CopyOnWriteArrayList;
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    60
import java.util.stream.Collectors;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    61
import javax.net.ssl.SSLContext;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    62
import sun.net.www.HeaderParser;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    63
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
 * A simple HTTP server that supports Digest authentication.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    66
 * 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
    67
 * in the request body.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    68
 * @author danielfuchs
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    69
 */
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    70
public class HTTPTestServer extends HTTPTest {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    71
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    72
    final HttpServer      serverImpl; // this server endpoint
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    73
    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
    74
    final HttpHandler     delegate;   // unused
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    75
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    76
    private HTTPTestServer(HttpServer server, HTTPTestServer target,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    77
                           HttpHandler delegate) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    78
        this.serverImpl = server;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    79
        this.redirect = target;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    80
        this.delegate = delegate;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    81
    }
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
    public static void main(String[] args)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    84
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    85
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    86
           HTTPTestServer server = create(HTTPTest.DEFAULT_PROTOCOL_TYPE,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    87
                                          HTTPTest.DEFAULT_HTTP_AUTH_TYPE,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    88
                                          HTTPTest.AUTHENTICATOR,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    89
                                          HTTPTest.DEFAULT_SCHEME_TYPE);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    90
           try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    91
               System.out.println("Server created at " + server.getAddress());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    92
               System.out.println("Strike <Return> to exit");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    93
               System.in.read();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    94
           } finally {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    95
               System.out.println("stopping server");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    96
               server.stop();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
    97
           }
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
    private static String toString(Headers headers) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   101
        return headers.entrySet().stream()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   102
                .map((e) -> e.getKey() + ": " + e.getValue())
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   103
                .collect(Collectors.joining("\n"));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   104
    }
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
    public static HTTPTestServer create(HttpProtocolType protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   107
                                        HttpAuthType authType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   108
                                        HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   109
                                        HttpSchemeType schemeType)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   110
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   111
        return create(protocol, authType, auth, schemeType, null);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   112
    }
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
    public static HTTPTestServer create(HttpProtocolType protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   115
                                        HttpAuthType authType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   116
                                        HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   117
                                        HttpSchemeType schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   118
                                        HttpHandler delegate)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   119
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   120
        Objects.requireNonNull(authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   121
        Objects.requireNonNull(auth);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   122
        switch(authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   123
            // A server that performs Server Digest authentication.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   124
            case SERVER: return createServer(protocol, authType, auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   125
                                             schemeType, delegate, "/");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   126
            // 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
   127
            // Proxy Digest authentication. If protocol is HTTPS,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   128
            // then this will create a HttpsProxyTunnel that will
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   129
            // handle the CONNECT request for tunneling.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   130
            case PROXY: return createProxy(protocol, authType, auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   131
                                           schemeType, delegate, "/");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   132
            // 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
   133
            // Digest authentication.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   134
            // 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
   135
            case SERVER307: return createServerAndRedirect(protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   136
                                                        HttpAuthType.SERVER,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   137
                                                        auth, schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   138
                                                        delegate, 307);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   139
            // 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
   140
            // Digest authentication.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   141
            case PROXY305:  return createServerAndRedirect(protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   142
                                                        HttpAuthType.PROXY,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   143
                                                        auth, schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   144
                                                        delegate, 305);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   145
            default:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   146
                throw new InternalError("Unknown server type: " + authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   147
        }
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
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   150
    /**
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   151
     * The HttpServerFactory ensures that the local port used by an HttpServer
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   152
     * previously created by the current test/VM will not get reused by
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   153
     * a subsequent test in the same VM. This is to avoid having the
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   154
     * AuthCache reuse credentials from previous tests - which would
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   155
     * invalidate the assumptions made by the current test on when
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   156
     * the default authenticator should be called.
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   157
     */
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   158
    private static final class HttpServerFactory {
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   159
        private static final int MAX = 10;
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   160
        private static final CopyOnWriteArrayList<String> addresses =
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   161
            new CopyOnWriteArrayList<>();
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   162
        private static HttpServer newHttpServer(HttpProtocolType protocol)
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   163
                throws IOException {
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   164
            switch (protocol) {
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   165
               case HTTP:  return HttpServer.create();
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   166
               case HTTPS: return HttpsServer.create();
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   167
               default: throw new InternalError("Unsupported protocol " + protocol);
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   168
            }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   169
        }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   170
        static <T extends HttpServer> T create(HttpProtocolType protocol)
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   171
                throws IOException {
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   172
            final int max = addresses.size() + MAX;
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   173
            final List<HttpServer> toClose = new ArrayList<>();
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   174
            try {
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   175
                for (int i = 1; i <= max; i++) {
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   176
                    HttpServer server = newHttpServer(protocol);
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   177
                    server.bind(new InetSocketAddress("127.0.0.1", 0), 0);
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   178
                    InetSocketAddress address = server.getAddress();
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   179
                    String key = address.toString();
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   180
                    if (addresses.addIfAbsent(key)) {
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   181
                       System.out.println("Server bound to: " + key
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   182
                                          + " after " + i + " attempt(s)");
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   183
                       return (T) server;
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   184
                    }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   185
                    System.out.println("warning: address " + key
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   186
                                       + " already used. Retrying bind.");
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   187
                    // 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
   188
                    // used already
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   189
                    toClose.add(server);
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   190
                }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   191
            } finally {
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   192
                // if we had to retry, then close the servers we're not
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   193
                // going to use.
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   194
                for (HttpServer s : toClose) {
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   195
                  try { s.stop(1); } catch (Exception x) { /* ignore */ }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   196
                }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   197
            }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   198
            throw new IOException("Couldn't bind servers after " + max + " attempts: "
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   199
                                  + "addresses used before: " + addresses);
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   200
        }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   201
    }
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   202
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   203
    static HttpServer createHttpServer(HttpProtocolType protocol) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   204
        switch (protocol) {
43508
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   205
            case HTTP:  return HttpServerFactory.create(protocol);
6b8bca7f0cee 8173315: java/net/HttpURLConnection/SetAuthenticator/HTTPSetAuthenticatorTest.java fails intermittently
dfuchs
parents: 42351
diff changeset
   206
            case HTTPS: return configure(HttpServerFactory.create(protocol));
42351
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   207
            default: throw new InternalError("Unsupported protocol " + protocol);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   208
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   209
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   210
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   211
    static HttpsServer configure(HttpsServer server) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   212
        try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   213
            SSLContext ctx = SSLContext.getDefault();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   214
            server.setHttpsConfigurator(new Configurator(ctx));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   215
        } catch (NoSuchAlgorithmException ex) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   216
            throw new IOException(ex);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   217
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   218
        return server;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   219
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   220
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   221
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   222
    static void setContextAuthenticator(HttpContext ctxt,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   223
                                        HttpTestAuthenticator auth) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   224
        final String realm = auth.getRealm();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   225
        com.sun.net.httpserver.Authenticator authenticator =
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   226
            new BasicAuthenticator(realm) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   227
                @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   228
                public boolean checkCredentials(String username, String pwd) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   229
                    return auth.getUserName().equals(username)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   230
                           && new String(auth.getPassword(username)).equals(pwd);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   231
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   232
        };
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   233
        ctxt.setAuthenticator(authenticator);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   234
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   235
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   236
    public static HTTPTestServer createServer(HttpProtocolType protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   237
                                        HttpAuthType authType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   238
                                        HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   239
                                        HttpSchemeType schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   240
                                        HttpHandler delegate,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   241
                                        String path)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   242
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   243
        Objects.requireNonNull(authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   244
        Objects.requireNonNull(auth);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   245
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   246
        HttpServer impl = createHttpServer(protocol);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   247
        final HTTPTestServer server = new HTTPTestServer(impl, null, delegate);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   248
        final HttpHandler hh = server.createHandler(schemeType, auth, authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   249
        HttpContext ctxt = impl.createContext(path, hh);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   250
        server.configureAuthentication(ctxt, schemeType, auth, authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   251
        impl.start();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   252
        return server;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   253
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   254
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   255
    public static HTTPTestServer createProxy(HttpProtocolType protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   256
                                        HttpAuthType authType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   257
                                        HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   258
                                        HttpSchemeType schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   259
                                        HttpHandler delegate,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   260
                                        String path)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   261
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   262
        Objects.requireNonNull(authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   263
        Objects.requireNonNull(auth);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   264
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   265
        HttpServer impl = createHttpServer(protocol);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   266
        final HTTPTestServer server = protocol == HttpProtocolType.HTTPS
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   267
                ? new HttpsProxyTunnel(impl, null, delegate)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   268
                : new HTTPTestServer(impl, null, delegate);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   269
        final HttpHandler hh = server.createHandler(schemeType, auth, authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   270
        HttpContext ctxt = impl.createContext(path, hh);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   271
        server.configureAuthentication(ctxt, schemeType, auth, authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   272
        impl.start();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   273
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   274
        return server;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   275
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   276
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   277
    public static HTTPTestServer createServerAndRedirect(
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   278
                                        HttpProtocolType protocol,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   279
                                        HttpAuthType targetAuthType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   280
                                        HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   281
                                        HttpSchemeType schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   282
                                        HttpHandler targetDelegate,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   283
                                        int code300)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   284
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   285
        Objects.requireNonNull(targetAuthType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   286
        Objects.requireNonNull(auth);
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
        // The connection between client and proxy can only
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   289
        // be a plain connection: SSL connection to proxy
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   290
        // is not supported by our client connection.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   291
        HttpProtocolType targetProtocol = targetAuthType == HttpAuthType.PROXY
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   292
                                          ? HttpProtocolType.HTTP
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   293
                                          : protocol;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   294
        HTTPTestServer redirectTarget =
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   295
                (targetAuthType == HttpAuthType.PROXY)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   296
                ? createProxy(protocol, targetAuthType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   297
                              auth, schemeType, targetDelegate, "/")
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   298
                : createServer(targetProtocol, targetAuthType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   299
                               auth, schemeType, targetDelegate, "/");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   300
        HttpServer impl = createHttpServer(protocol);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   301
        final HTTPTestServer redirectingServer =
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   302
                 new HTTPTestServer(impl, redirectTarget, null);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   303
        InetSocketAddress redirectAddr = redirectTarget.getAddress();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   304
        URL locationURL = url(targetProtocol, redirectAddr, "/");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   305
        final HttpHandler hh = redirectingServer.create300Handler(locationURL,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   306
                                             HttpAuthType.SERVER, code300);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   307
        impl.createContext("/", hh);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   308
        impl.start();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   309
        return redirectingServer;
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
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   312
    public InetSocketAddress getAddress() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   313
        return serverImpl.getAddress();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   314
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   315
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   316
    public void stop() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   317
        serverImpl.stop(0);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   318
        if (redirect != null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   319
            redirect.stop();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   320
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   321
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   322
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   323
    protected void writeResponse(HttpExchange he) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   324
        if (delegate == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   325
            he.sendResponseHeaders(HttpURLConnection.HTTP_OK, 0);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   326
            he.getResponseBody().write(he.getRequestBody().readAllBytes());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   327
        } else {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   328
            delegate.handle(he);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   329
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   330
    }
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
    private HttpHandler createHandler(HttpSchemeType schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   333
                                      HttpTestAuthenticator auth,
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
        return new HttpNoAuthHandler(authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   336
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   337
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   338
    private void configureAuthentication(HttpContext ctxt,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   339
                            HttpSchemeType schemeType,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   340
                            HttpTestAuthenticator auth,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   341
                            HttpAuthType authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   342
        switch(schemeType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   343
            case DIGEST:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   344
                // DIGEST authentication is handled by the handler.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   345
                ctxt.getFilters().add(new HttpDigestFilter(auth, authType));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   346
                break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   347
            case BASIC:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   348
                // BASIC authentication is handled by the filter.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   349
                ctxt.getFilters().add(new HttpBasicFilter(auth, authType));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   350
                break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   351
            case BASICSERVER:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   352
                switch(authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   353
                    case PROXY: case PROXY305:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   354
                        // HttpServer can't support Proxy-type authentication
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   355
                        // => 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
   356
                        //    handle authentication in the handler.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   357
                        ctxt.getFilters().add(new HttpBasicFilter(auth, authType));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   358
                        break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   359
                    case SERVER: case SERVER307:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   360
                        // Basic authentication is handled by HttpServer
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   361
                        // directly => the filter should not perform
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   362
                        // authentication again.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   363
                        setContextAuthenticator(ctxt, auth);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   364
                        ctxt.getFilters().add(new HttpNoAuthFilter(authType));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   365
                        break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   366
                    default:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   367
                        throw new InternalError("Invalid combination scheme="
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   368
                             + schemeType + " authType=" + authType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   369
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   370
            case NONE:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   371
                // No authentication at all.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   372
                ctxt.getFilters().add(new HttpNoAuthFilter(authType));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   373
                break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   374
            default:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   375
                throw new InternalError("No such scheme: " + schemeType);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   376
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   377
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   378
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   379
    private HttpHandler create300Handler(URL proxyURL,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   380
        HttpAuthType type, int code300) throws MalformedURLException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   381
        return new Http3xxHandler(proxyURL, type, code300);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   382
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   383
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   384
    // Abstract HTTP filter class.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   385
    private abstract static class AbstractHttpFilter extends Filter {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   386
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   387
        final HttpAuthType authType;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   388
        final String type;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   389
        public AbstractHttpFilter(HttpAuthType authType, String type) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   390
            this.authType = authType;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   391
            this.type = type;
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
        String getLocation() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   395
            return "Location";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   396
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   397
        String getAuthenticate() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   398
            return authType == HttpAuthType.PROXY
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   399
                    ? "Proxy-Authenticate" : "WWW-Authenticate";
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
        String getAuthorization() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   402
            return authType == HttpAuthType.PROXY
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   403
                    ? "Proxy-Authorization" : "Authorization";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   404
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   405
        int getUnauthorizedCode() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   406
            return authType == HttpAuthType.PROXY
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   407
                    ? HttpURLConnection.HTTP_PROXY_AUTH
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   408
                    : HttpURLConnection.HTTP_UNAUTHORIZED;
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
        String getKeepAlive() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   411
            return "keep-alive";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   412
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   413
        String getConnection() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   414
            return authType == HttpAuthType.PROXY
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   415
                    ? "Proxy-Connection" : "Connection";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   416
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   417
        protected abstract boolean isAuthentified(HttpExchange he) throws IOException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   418
        protected abstract void requestAuthentication(HttpExchange he) throws IOException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   419
        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
   420
            chain.doFilter(he);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   421
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   422
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   423
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   424
        public String description() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   425
            return "Filter for " + type;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   426
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   427
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   428
        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
   429
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   430
                System.out.println(type + ": Got " + he.getRequestMethod()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   431
                    + ": " + he.getRequestURI()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   432
                    + "\n" + HTTPTestServer.toString(he.getRequestHeaders()));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   433
                if (!isAuthentified(he)) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   434
                    try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   435
                        requestAuthentication(he);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   436
                        he.sendResponseHeaders(getUnauthorizedCode(), 0);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   437
                        System.out.println(type
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   438
                            + ": Sent back " + getUnauthorizedCode());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   439
                    } finally {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   440
                        he.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   441
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   442
                } else {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   443
                    accept(he, chain);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   444
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   445
            } catch (RuntimeException | Error | IOException t) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   446
               System.err.println(type
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   447
                    + ": Unexpected exception while handling request: " + t);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   448
               t.printStackTrace(System.err);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   449
               he.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   450
               throw t;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   451
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   452
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   453
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
    private final static class DigestResponse {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   457
        final String realm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   458
        final String username;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   459
        final String nonce;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   460
        final String cnonce;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   461
        final String nc;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   462
        final String uri;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   463
        final String algorithm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   464
        final String response;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   465
        final String qop;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   466
        final String opaque;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   467
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   468
        public DigestResponse(String realm, String username, String nonce,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   469
                              String cnonce, String nc, String uri,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   470
                              String algorithm, String qop, String opaque,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   471
                              String response) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   472
            this.realm = realm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   473
            this.username = username;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   474
            this.nonce = nonce;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   475
            this.cnonce = cnonce;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   476
            this.nc = nc;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   477
            this.uri = uri;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   478
            this.algorithm = algorithm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   479
            this.qop = qop;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   480
            this.opaque = opaque;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   481
            this.response = response;
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
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   484
        String getAlgorithm(String defval) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   485
            return algorithm == null ? defval : algorithm;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   486
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   487
        String getQoP(String defval) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   488
            return qop == null ? defval : qop;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   489
        }
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
        // Code stolen from DigestAuthentication:
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   492
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   493
        private static final char charArray[] = {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   494
            '0', '1', '2', '3', '4', '5', '6', '7',
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   495
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   496
        };
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   497
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   498
        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
   499
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   500
                md.update(src.getBytes("ISO-8859-1"));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   501
            } catch (java.io.UnsupportedEncodingException uee) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   502
                assert false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   503
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   504
            if (passwd != null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   505
                byte[] passwdBytes = new byte[passwd.length];
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   506
                for (int i=0; i<passwd.length; i++)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   507
                    passwdBytes[i] = (byte)passwd[i];
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   508
                md.update(passwdBytes);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   509
                Arrays.fill(passwdBytes, (byte)0x00);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   510
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   511
            byte[] digest = md.digest();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   512
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   513
            StringBuilder res = new StringBuilder(digest.length * 2);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   514
            for (int i = 0; i < digest.length; i++) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   515
                int hashchar = ((digest[i] >>> 4) & 0xf);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   516
                res.append(charArray[hashchar]);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   517
                hashchar = (digest[i] & 0xf);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   518
                res.append(charArray[hashchar]);
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
            return res.toString();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   521
        }
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
        public static String computeDigest(boolean isRequest,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   524
                                            String reqMethod,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   525
                                            char[] password,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   526
                                            DigestResponse params)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   527
            throws NoSuchAlgorithmException
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   528
        {
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
            String A1, HashA1;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   531
            String algorithm = params.getAlgorithm("MD5");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   532
            boolean md5sess = algorithm.equalsIgnoreCase ("MD5-sess");
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
            MessageDigest md = MessageDigest.getInstance(md5sess?"MD5":algorithm);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   535
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   536
            if (params.username == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   537
                throw new IllegalArgumentException("missing username");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   538
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   539
            if (params.realm == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   540
                throw new IllegalArgumentException("missing realm");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   541
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   542
            if (params.uri == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   543
                throw new IllegalArgumentException("missing uri");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   544
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   545
            if (params.nonce == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   546
                throw new IllegalArgumentException("missing nonce");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   547
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   548
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   549
            A1 = params.username + ":" + params.realm + ":";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   550
            HashA1 = encode(A1, password, md);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   551
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   552
            String A2;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   553
            if (isRequest) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   554
                A2 = reqMethod + ":" + params.uri;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   555
            } else {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   556
                A2 = ":" + params.uri;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   557
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   558
            String HashA2 = encode(A2, null, md);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   559
            String combo, finalHash;
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
            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
   562
                if (params.cnonce == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   563
                    throw new IllegalArgumentException("missing nonce");
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
                if (params.nc == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   566
                    throw new IllegalArgumentException("missing nonce");
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
                combo = HashA1+ ":" + params.nonce + ":" + params.nc + ":" +
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   569
                            params.cnonce + ":auth:" +HashA2;
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
            } else { /* for compatibility with RFC2069 */
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   572
                combo = HashA1 + ":" +
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   573
                           params.nonce + ":" +
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   574
                           HashA2;
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
            finalHash = encode(combo, null, md);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   577
            return finalHash;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   578
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   579
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   580
        public static DigestResponse create(String raw) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   581
            String username, realm, nonce, nc, uri, response, cnonce,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   582
                   algorithm, qop, opaque;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   583
            HeaderParser parser = new HeaderParser(raw);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   584
            username = parser.findValue("username");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   585
            realm = parser.findValue("realm");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   586
            nonce = parser.findValue("nonce");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   587
            nc = parser.findValue("nc");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   588
            uri = parser.findValue("uri");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   589
            cnonce = parser.findValue("cnonce");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   590
            response = parser.findValue("response");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   591
            algorithm = parser.findValue("algorithm");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   592
            qop = parser.findValue("qop");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   593
            opaque = parser.findValue("opaque");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   594
            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
   595
                                      algorithm, qop, opaque, response);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   596
        }
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
    }
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
    private class HttpNoAuthFilter extends AbstractHttpFilter {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   601
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   602
        public HttpNoAuthFilter(HttpAuthType authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   603
            super(authType, authType == HttpAuthType.SERVER
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   604
                            ? "NoAuth Server" : "NoAuth Proxy");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   605
        }
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
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   608
        protected boolean isAuthentified(HttpExchange he) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   609
            return true;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   610
        }
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
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   613
        protected void requestAuthentication(HttpExchange he) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   614
            throw new InternalError("Should not com here");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   615
        }
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
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   618
        public String description() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   619
            return "Passthrough Filter";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   620
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   621
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
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   624
    // An HTTP Filter that performs Basic authentication
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   625
    private class HttpBasicFilter extends AbstractHttpFilter {
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
        private final HttpTestAuthenticator auth;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   628
        public HttpBasicFilter(HttpTestAuthenticator auth, HttpAuthType authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   629
            super(authType, authType == HttpAuthType.SERVER
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   630
                            ? "Basic Server" : "Basic Proxy");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   631
            this.auth = auth;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   632
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   633
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   634
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   635
        protected void requestAuthentication(HttpExchange he)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   636
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   637
            he.getResponseHeaders().add(getAuthenticate(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   638
                 "Basic realm=\"" + auth.getRealm() + "\"");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   639
            System.out.println(type + ": Requesting Basic Authentication "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   640
                 + he.getResponseHeaders().getFirst(getAuthenticate()));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   641
        }
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
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   644
        protected boolean isAuthentified(HttpExchange he) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   645
            if (he.getRequestHeaders().containsKey(getAuthorization())) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   646
                List<String> authorization =
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   647
                    he.getRequestHeaders().get(getAuthorization());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   648
                for (String a : authorization) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   649
                    System.out.println(type + ": processing " + a);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   650
                    int sp = a.indexOf(' ');
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   651
                    if (sp < 0) return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   652
                    String scheme = a.substring(0, sp);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   653
                    if (!"Basic".equalsIgnoreCase(scheme)) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   654
                        System.out.println(type + ": Unsupported scheme '"
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   655
                                           + scheme +"'");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   656
                        return false;
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
                    if (a.length() <= sp+1) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   659
                        System.out.println(type + ": value too short for '"
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   660
                                            + scheme +"'");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   661
                        return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   662
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   663
                    a = a.substring(sp+1);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   664
                    return validate(a);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   665
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   666
                return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   667
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   668
            return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   669
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   670
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   671
        boolean validate(String a) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   672
            byte[] b = Base64.getDecoder().decode(a);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   673
            String userpass = new String (b);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   674
            int colon = userpass.indexOf (':');
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   675
            String uname = userpass.substring (0, colon);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   676
            String pass = userpass.substring (colon+1);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   677
            return auth.getUserName().equals(uname) &&
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   678
                   new String(auth.getPassword(uname)).equals(pass);
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
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   681
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   682
        public String description() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   683
            return "Filter for " + type;
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
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   686
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   687
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
    // An HTTP Filter that performs Digest authentication
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   690
    private class HttpDigestFilter extends AbstractHttpFilter {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   691
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   692
        // 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
   693
        // 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
   694
        // 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
   695
        // server side digest simpler.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   696
        private final HttpTestAuthenticator auth;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   697
        private final byte[] nonce;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   698
        private final String ns;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   699
        public HttpDigestFilter(HttpTestAuthenticator auth, HttpAuthType authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   700
            super(authType, authType == HttpAuthType.SERVER
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   701
                            ? "Digest Server" : "Digest Proxy");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   702
            this.auth = auth;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   703
            nonce = new byte[16];
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   704
            new Random(Instant.now().toEpochMilli()).nextBytes(nonce);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   705
            ns = new BigInteger(1, nonce).toString(16);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   706
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   707
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   708
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   709
        protected void requestAuthentication(HttpExchange he)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   710
            throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   711
            he.getResponseHeaders().add(getAuthenticate(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   712
                 "Digest realm=\"" + auth.getRealm() + "\","
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   713
                 + "\r\n    qop=\"auth\","
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   714
                 + "\r\n    nonce=\"" + ns +"\"");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   715
            System.out.println(type + ": Requesting Digest Authentication "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   716
                 + he.getResponseHeaders().getFirst(getAuthenticate()));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   717
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   718
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   719
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   720
        protected boolean isAuthentified(HttpExchange he) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   721
            if (he.getRequestHeaders().containsKey(getAuthorization())) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   722
                List<String> authorization = he.getRequestHeaders().get(getAuthorization());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   723
                for (String a : authorization) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   724
                    System.out.println(type + ": processing " + a);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   725
                    int sp = a.indexOf(' ');
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   726
                    if (sp < 0) return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   727
                    String scheme = a.substring(0, sp);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   728
                    if (!"Digest".equalsIgnoreCase(scheme)) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   729
                        System.out.println(type + ": Unsupported scheme '" + scheme +"'");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   730
                        return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   731
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   732
                    if (a.length() <= sp+1) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   733
                        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
   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
                    a = a.substring(sp+1);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   737
                    DigestResponse dgr = DigestResponse.create(a);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   738
                    return validate(he.getRequestMethod(), dgr);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   739
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   740
                return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   741
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   742
            return false;
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
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   745
        boolean validate(String reqMethod, DigestResponse dg) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   746
            if (!"MD5".equalsIgnoreCase(dg.getAlgorithm("MD5"))) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   747
                System.out.println(type + ": Unsupported algorithm "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   748
                                   + dg.algorithm);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   749
                return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   750
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   751
            if (!"auth".equalsIgnoreCase(dg.getQoP("auth"))) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   752
                System.out.println(type + ": Unsupported qop "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   753
                                   + dg.qop);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   754
                return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   755
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   756
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   757
                if (!dg.nonce.equals(ns)) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   758
                    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
   759
                                    + nonce + " expected " + ns);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   760
                    return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   761
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   762
                if (dg.response == null) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   763
                    System.out.println(type + ": missing digest response.");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   764
                    return false;
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
                char[] pa = auth.getPassword(dg.username);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   767
                return verify(reqMethod, dg, pa);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   768
            } catch(IllegalArgumentException | SecurityException
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   769
                    | NoSuchAlgorithmException e) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   770
                System.out.println(type + ": " + e.getMessage());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   771
                return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   772
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   773
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   774
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   775
        boolean verify(String reqMethod, DigestResponse dg, char[] pw)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   776
            throws NoSuchAlgorithmException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   777
            String response = DigestResponse.computeDigest(true, reqMethod, pw, dg);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   778
            if (!dg.response.equals(response)) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   779
                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
   780
                                    + dg.response + " expected " + response);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   781
                return false;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   782
            } else {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   783
                System.out.println(type + ": verified response " + response);
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
            return true;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   786
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   787
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   788
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   789
        public String description() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   790
            return "Filter for DIGEST authentication";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   791
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   792
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   793
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   794
    // Abstract HTTP handler class.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   795
    private abstract static class AbstractHttpHandler implements HttpHandler {
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
        final HttpAuthType authType;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   798
        final String type;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   799
        public AbstractHttpHandler(HttpAuthType authType, String type) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   800
            this.authType = authType;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   801
            this.type = type;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   802
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   803
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   804
        String getLocation() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   805
            return "Location";
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   806
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   807
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   808
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   809
        public void handle(HttpExchange he) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   810
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   811
                sendResponse(he);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   812
            } catch (RuntimeException | Error | IOException t) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   813
               System.err.println(type
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   814
                    + ": Unexpected exception while handling request: " + t);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   815
               t.printStackTrace(System.err);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   816
               throw t;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   817
            } finally {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   818
                he.close();
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
        }
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
        protected abstract void sendResponse(HttpExchange he) throws IOException;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   823
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   824
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   825
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   826
    private class HttpNoAuthHandler extends AbstractHttpHandler {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   827
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   828
        public HttpNoAuthHandler(HttpAuthType authType) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   829
            super(authType, authType == HttpAuthType.SERVER
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   830
                            ? "NoAuth Server" : "NoAuth Proxy");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   831
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   832
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   833
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   834
        protected void sendResponse(HttpExchange he) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   835
            HTTPTestServer.this.writeResponse(he);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   836
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   837
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   838
    }
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
    // 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
   841
    // 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
   842
    private class Http3xxHandler extends AbstractHttpHandler {
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
        private final URL redirectTargetURL;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   845
        private final int code3XX;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   846
        public Http3xxHandler(URL proxyURL, HttpAuthType authType, int code300) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   847
            super(authType, "Server" + code300);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   848
            this.redirectTargetURL = proxyURL;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   849
            this.code3XX = code300;
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
        int get3XX() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   853
            return code3XX;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   854
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   855
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   856
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   857
        public void sendResponse(HttpExchange he) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   858
            System.out.println(type + ": Got " + he.getRequestMethod()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   859
                    + ": " + he.getRequestURI()
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   860
                    + "\n" + HTTPTestServer.toString(he.getRequestHeaders()));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   861
            System.out.println(type + ": Redirecting to "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   862
                               + (authType == HttpAuthType.PROXY305
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   863
                                    ? "proxy" : "server"));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   864
            he.getResponseHeaders().add(getLocation(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   865
                redirectTargetURL.toExternalForm().toString());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   866
            he.sendResponseHeaders(get3XX(), 0);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   867
            System.out.println(type + ": Sent back " + get3XX() + " "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   868
                 + getLocation() + ": " + redirectTargetURL.toExternalForm().toString());
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
    static class Configurator extends HttpsConfigurator {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   873
        public Configurator(SSLContext ctx) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   874
            super(ctx);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   875
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   876
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   877
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   878
        public void configure (HttpsParameters params) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   879
            params.setSSLParameters (getSSLContext().getSupportedSSLParameters());
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
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   883
    // 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
   884
    // 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
   885
    // 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
   886
    // 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
   887
    static class HttpsProxyTunnel extends HTTPTestServer
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   888
            implements Runnable {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   889
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   890
        final ServerSocket ss;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   891
        public HttpsProxyTunnel(HttpServer server, HTTPTestServer target,
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   892
                               HttpHandler delegate)
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   893
                throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   894
            super(server, target, delegate);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   895
            System.out.flush();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   896
            System.err.println("WARNING: HttpsProxyTunnel is an experimental test class");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   897
            ss = new ServerSocket(0, 0, InetAddress.getByName("127.0.0.1"));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   898
            start();
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
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   901
        final void start() throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   902
            Thread t = new Thread(this, "ProxyThread");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   903
            t.setDaemon(true);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   904
            t.start();
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
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   907
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   908
        public void stop() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   909
            super.stop();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   910
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   911
                ss.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   912
            } catch (IOException ex) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   913
                if (DEBUG) ex.printStackTrace(System.out);
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
        // Pipe the input stream to the output stream.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   918
        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
   919
            return new Thread("TunnelPipe("+tag+")") {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   920
                @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   921
                public void run() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   922
                    try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   923
                        try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   924
                            int c;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   925
                            while ((c = is.read()) != -1) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   926
                                os.write(c);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   927
                                os.flush();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   928
                                // 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
   929
                                // character.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   930
                                if (DEBUG) System.out.print(tag);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   931
                            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   932
                            is.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   933
                        } finally {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   934
                            os.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   935
                        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   936
                    } catch (IOException ex) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   937
                        if (DEBUG) ex.printStackTrace(System.out);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   938
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   939
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   940
            };
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   941
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   942
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   943
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   944
        public InetSocketAddress getAddress() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   945
            return new InetSocketAddress(ss.getInetAddress(), ss.getLocalPort());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   946
        }
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
        // 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
   949
        // lines, but our client shouldn't send any.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   950
        // 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
   951
        // \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
   952
        // if it wasn't preceded by '\r'.
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
        String readLine(InputStream r) throws IOException {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   955
            StringBuilder b = new StringBuilder();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   956
            int c;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   957
            while ((c = r.read()) != -1) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   958
                if (c == '\n') break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   959
                b.appendCodePoint(c);
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
            if (b.codePointAt(b.length() -1) == '\r') {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   962
                b.delete(b.length() -1, b.length());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   963
            }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   964
            return b.toString();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   965
        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   966
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   967
        @Override
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   968
        public void run() {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   969
            Socket clientConnection = null;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   970
            try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   971
                while (true) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   972
                    System.out.println("Tunnel: Waiting for client");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   973
                    Socket previous = clientConnection;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   974
                    try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   975
                        clientConnection = ss.accept();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   976
                    } catch (IOException io) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   977
                        if (DEBUG) io.printStackTrace(System.out);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   978
                        break;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   979
                    } finally {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   980
                        // close the previous connection
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   981
                        if (previous != null) previous.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   982
                    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   983
                    System.out.println("Tunnel: Client accepted");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   984
                    Socket targetConnection = null;
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   985
                    InputStream  ccis = clientConnection.getInputStream();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   986
                    OutputStream ccos = clientConnection.getOutputStream();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   987
                    Writer w = new OutputStreamWriter(
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   988
                                   clientConnection.getOutputStream(), "UTF-8");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   989
                    PrintWriter pw = new PrintWriter(w);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   990
                    System.out.println("Tunnel: Reading request line");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   991
                    String requestLine = readLine(ccis);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   992
                    System.out.println("Tunnel: Request line: " + requestLine);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   993
                    if (requestLine.startsWith("CONNECT ")) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   994
                        // 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
   995
                        // 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
   996
                        // Some improvement for a followup!
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   997
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
   998
                        // 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
   999
                        // signals the end of all headers.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1000
                        while(!requestLine.equals("")) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1001
                            System.out.println("Tunnel: Reading header: "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1002
                                               + (requestLine = readLine(ccis)));
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1003
                        }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1004
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1005
                        targetConnection = new Socket(
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1006
                                serverImpl.getAddress().getAddress(),
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1007
                                serverImpl.getAddress().getPort());
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1008
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1009
                        // 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
  1010
                        System.out.println("Tunnel: Sending "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1011
                                           + "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
  1012
                        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
  1013
                        pw.flush();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1014
                    } else {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1015
                        // 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
  1016
                        // deal with it.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1017
                        throw new IOException("Tunnel: Unexpected status line: "
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1018
                                             + requestLine);
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
                    // 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
  1022
                    // output stream of the target connection and conversely.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1023
                    // 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
  1024
                    System.out.println("Tunnel: Starting tunnel pipes");
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1025
                    Thread t1 = pipe(ccis, targetConnection.getOutputStream(), '+');
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1026
                    Thread t2 = pipe(targetConnection.getInputStream(), ccos, '-');
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1027
                    t1.start();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1028
                    t2.start();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1029
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1030
                    // 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
  1031
                    // accepting a new connection request.
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1032
                    t1.join();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1033
                    t2.join();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1034
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1035
            } catch (Throwable ex) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1036
                try {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1037
                    ss.close();
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1038
                } catch (IOException ex1) {
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1039
                    ex.addSuppressed(ex1);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1040
                }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1041
                ex.printStackTrace(System.err);
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1042
            }
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
    }
85ed90be0ae1 8169495: Add a method to set an Authenticator on a HttpURLConnection.
dfuchs
parents:
diff changeset
  1046
}