test/jdk/java/net/httpclient/DigestEchoClient.java
author dfuchs
Mon, 05 Feb 2018 15:51:09 +0000
branchhttp-client-branch
changeset 56070 66a9c3185028
parent 56054 352e845ae744
child 56081 20c6742e5545
permissions -rw-r--r--
http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
     1
/*
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
     4
 *
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
     7
 * published by the Free Software Foundation.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
     8
 *
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    13
 * accompanied this code).
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    14
 *
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    18
 *
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    21
 * questions.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    22
 */
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    23
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    24
import java.io.IOException;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    25
import java.io.UncheckedIOException;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    26
import java.math.BigInteger;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    27
import java.net.ProxySelector;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    28
import java.net.URI;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    29
import java.nio.charset.StandardCharsets;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    30
import java.security.NoSuchAlgorithmException;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    31
import java.util.Arrays;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    32
import java.util.Base64;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    33
import java.util.EnumSet;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    34
import java.util.List;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    35
import java.util.Optional;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    36
import java.util.Random;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    37
import java.util.concurrent.CompletableFuture;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    38
import java.util.concurrent.CompletionException;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    39
import java.util.concurrent.ConcurrentHashMap;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    40
import java.util.concurrent.ConcurrentMap;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    41
import java.util.concurrent.atomic.AtomicInteger;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    42
import java.util.concurrent.atomic.AtomicLong;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    43
import java.util.stream.Collectors;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    44
import java.util.stream.Stream;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    45
import javax.net.ssl.SSLContext;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    46
import javax.net.ServerSocketFactory;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    47
import javax.net.ssl.SSLServerSocketFactory;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    48
import jdk.incubator.http.HttpClient;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    49
import jdk.incubator.http.HttpClient.Version;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    50
import jdk.incubator.http.HttpRequest;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    51
import jdk.incubator.http.HttpResponse;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    52
import jdk.testlibrary.SimpleSSLContext;
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    53
import sun.net.NetProperties;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    54
import sun.net.www.HeaderParser;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    55
import static java.lang.System.out;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    56
import static java.lang.String.format;
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
    57
import static jdk.incubator.http.HttpResponse.BodyHandler.asLines;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    58
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    59
/**
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    60
 * @test
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    61
 * @summary this test verifies that a client may provides authorization
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    62
 *          headers directly when connecting with a server.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    63
 * @bug 8087112
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
    64
 * @library /lib/testlibrary http2/server
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
    65
 * @build jdk.testlibrary.SimpleSSLContext HttpServerAdapters DigestEchoServer DigestEchoClient
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
    66
 * @modules jdk.incubator.httpclient/jdk.incubator.http.internal.common
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
    67
 *          jdk.incubator.httpclient/jdk.incubator.http.internal.frame
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
    68
 *          jdk.incubator.httpclient/jdk.incubator.http.internal.hpack
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
    69
 *          java.logging
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
    70
 *          java.base/sun.net.www.http
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    71
 *          java.base/sun.net.www
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    72
 *          java.base/sun.net
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    73
 * @run main/othervm DigestEchoClient
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    74
 * @run main/othervm -Djdk.http.auth.proxying.disabledSchemes=
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    75
 *                   -Djdk.http.auth.tunneling.disabledSchemes=
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
    76
 *                   DigestEchoClient
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    77
 */
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    78
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    79
public class DigestEchoClient {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    80
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    81
    static final String data[] = {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    82
        "Lorem ipsum",
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    83
        "dolor sit amet",
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    84
        "consectetur adipiscing elit, sed do eiusmod tempor",
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    85
        "quis nostrud exercitation ullamco",
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    86
        "laboris nisi",
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    87
        "ut",
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    88
        "aliquip ex ea commodo consequat." +
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    89
        "Duis aute irure dolor in reprehenderit in voluptate velit esse" +
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    90
        "cillum dolore eu fugiat nulla pariatur.",
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    91
        "Excepteur sint occaecat cupidatat non proident."
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    92
    };
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    93
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    94
    static final AtomicLong serverCount = new AtomicLong();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    95
    static final class EchoServers {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    96
        final DigestEchoServer.HttpAuthType authType;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    97
        final DigestEchoServer.HttpAuthSchemeType authScheme;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    98
        final String protocolScheme;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    99
        final String key;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   100
        final DigestEchoServer server;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   101
        final Version serverVersion;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   102
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   103
        private EchoServers(DigestEchoServer server,
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   104
                    Version version,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   105
                    String protocolScheme,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   106
                    DigestEchoServer.HttpAuthType authType,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   107
                    DigestEchoServer.HttpAuthSchemeType authScheme) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   108
            this.authType = authType;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   109
            this.authScheme = authScheme;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   110
            this.protocolScheme = protocolScheme;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   111
            this.key = key(version, protocolScheme, authType, authScheme);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   112
            this.server = server;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   113
            this.serverVersion = version;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   114
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   115
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   116
        static String key(Version version,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   117
                          String protocolScheme,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   118
                          DigestEchoServer.HttpAuthType authType,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   119
                          DigestEchoServer.HttpAuthSchemeType authScheme) {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   120
            return String.format("%s:%s:%s:%s", version, protocolScheme, authType, authScheme);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   121
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   122
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   123
        private static EchoServers create(Version version,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   124
                                   String protocolScheme,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   125
                                   DigestEchoServer.HttpAuthType authType,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   126
                                   DigestEchoServer.HttpAuthSchemeType authScheme) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   127
            try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   128
                serverCount.incrementAndGet();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   129
                DigestEchoServer server =
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   130
                    DigestEchoServer.create(version, protocolScheme, authType, authScheme);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   131
                return new EchoServers(server, version, protocolScheme, authType, authScheme);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   132
            } catch (IOException x) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   133
                throw new UncheckedIOException(x);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   134
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   135
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   136
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   137
        public static DigestEchoServer of(Version version,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   138
                                    String protocolScheme,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   139
                                    DigestEchoServer.HttpAuthType authType,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   140
                                    DigestEchoServer.HttpAuthSchemeType authScheme) {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   141
            String key = key(version, protocolScheme, authType, authScheme);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   142
            return servers.computeIfAbsent(key, (k) ->
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   143
                    create(version, protocolScheme, authType, authScheme)).server;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   144
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   145
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   146
        public static void stop() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   147
            for (EchoServers s : servers.values()) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   148
                s.server.stop();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   149
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   150
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   151
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   152
        private static final ConcurrentMap<String, EchoServers> servers = new ConcurrentHashMap<>();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   153
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   154
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   155
    final static String PROXY_DISABLED = NetProperties.get("jdk.http.auth.proxying.disabledSchemes");
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   156
    final static String TUNNEL_DISABLED = NetProperties.get("jdk.http.auth.tunneling.disabledSchemes");
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   157
    static {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   158
        System.out.println("jdk.http.auth.proxying.disabledSchemes=" + PROXY_DISABLED);
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   159
        System.out.println("jdk.http.auth.tunneling.disabledSchemes=" + TUNNEL_DISABLED);
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   160
    }
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   161
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   162
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   163
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   164
    static final AtomicInteger NC = new AtomicInteger();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   165
    static final Random random = new Random();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   166
    static final SSLContext context;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   167
    static {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   168
        try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   169
            context = new SimpleSSLContext().get();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   170
            SSLContext.setDefault(context);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   171
        } catch (Exception x) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   172
            throw new ExceptionInInitializerError(x);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   173
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   174
    }
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   175
    static final List<Boolean> BOOLEANS = List.of(true, false);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   176
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   177
    final ServerSocketFactory factory;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   178
    final boolean useSSL;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   179
    final DigestEchoServer.HttpAuthSchemeType authScheme;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   180
    final DigestEchoServer.HttpAuthType authType;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   181
    DigestEchoClient(boolean useSSL,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   182
                     DigestEchoServer.HttpAuthSchemeType authScheme,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   183
                     DigestEchoServer.HttpAuthType authType)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   184
            throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   185
        this.useSSL = useSSL;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   186
        this.authScheme = authScheme;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   187
        this.authType = authType;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   188
        factory = useSSL ? SSLServerSocketFactory.getDefault()
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   189
                         : ServerSocketFactory.getDefault();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   190
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   191
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   192
    static final AtomicLong clientCount = new AtomicLong();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   193
    public HttpClient newHttpClient(DigestEchoServer server) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   194
        clientCount.incrementAndGet();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   195
        HttpClient.Builder builder = HttpClient.newBuilder();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   196
        if (useSSL) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   197
            builder.sslContext(context);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   198
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   199
        switch (authScheme) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   200
            case BASIC:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   201
                builder = builder.authenticator(DigestEchoServer.AUTHENTICATOR);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   202
                break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   203
            case BASICSERVER:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   204
                // don't set the authenticator: we will handle the header ourselves.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   205
                // builder = builder.authenticator(DigestEchoServer.AUTHENTICATOR);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   206
                break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   207
            default:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   208
                break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   209
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   210
        switch (authType) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   211
            case PROXY:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   212
                builder = builder.proxy(ProxySelector.of(server.getProxyAddress()));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   213
                break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   214
            case PROXY305:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   215
                builder = builder.proxy(ProxySelector.of(server.getProxyAddress()));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   216
                builder = builder.followRedirects(HttpClient.Redirect.SAME_PROTOCOL);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   217
                break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   218
            case SERVER307:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   219
                builder = builder.followRedirects(HttpClient.Redirect.SAME_PROTOCOL);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   220
                break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   221
            default:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   222
                break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   223
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   224
        return builder.build();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   225
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   226
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   227
    public static List<Version> serverVersions(Version clientVersion) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   228
        if (clientVersion == Version.HTTP_1_1) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   229
            return List.of(clientVersion);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   230
        } else {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   231
            return List.of(Version.values());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   232
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   233
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   234
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   235
    public static List<Version> clientVersions() {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   236
        return List.of(Version.values());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   237
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   238
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   239
    public static List<Boolean> expectContinue(Version serverVersion) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   240
        if (serverVersion == Version.HTTP_1_1) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   241
            return BOOLEANS;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   242
        } else {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   243
            // our test HTTP/2 server does not support Expect: 100-Continue
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   244
            return List.of(Boolean.FALSE);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   245
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   246
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   247
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   248
    public static void main(String[] args) throws Exception {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   249
        boolean useSSL = false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   250
        EnumSet<DigestEchoServer.HttpAuthType> types =
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   251
                EnumSet.complementOf(EnumSet.of(DigestEchoServer.HttpAuthType.PROXY305));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   252
        if (args != null && args.length >= 1) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   253
            useSSL = "SSL".equals(args[0]);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   254
            if (args.length > 1) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   255
                List<DigestEchoServer.HttpAuthType> httpAuthTypes =
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   256
                        Stream.of(Arrays.copyOfRange(args, 1, args.length))
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   257
                                .map(DigestEchoServer.HttpAuthType::valueOf)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   258
                                .collect(Collectors.toList());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   259
                types = EnumSet.copyOf(httpAuthTypes);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   260
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   261
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   262
        try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   263
            for (DigestEchoServer.HttpAuthType authType : types) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   264
                // The test server does not support PROXY305 properly
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   265
                if (authType == DigestEchoServer.HttpAuthType.PROXY305) continue;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   266
                EnumSet<DigestEchoServer.HttpAuthSchemeType> basics =
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   267
                        EnumSet.of(DigestEchoServer.HttpAuthSchemeType.BASICSERVER,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   268
                                DigestEchoServer.HttpAuthSchemeType.BASIC);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   269
                for (DigestEchoServer.HttpAuthSchemeType authScheme : basics) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   270
                    DigestEchoClient dec = new DigestEchoClient(useSSL,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   271
                            authScheme,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   272
                            authType);
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   273
                    for (Version clientVersion : clientVersions()) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   274
                        for (Version serverVersion : serverVersions(clientVersion)) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   275
                            for (boolean expectContinue : expectContinue(serverVersion)) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   276
                                for (boolean async : BOOLEANS) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   277
                                    for (boolean preemptive : BOOLEANS) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   278
                                        dec.testBasic(clientVersion,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   279
                                                serverVersion, async,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   280
                                                expectContinue, preemptive);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   281
                                    }
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   282
                                }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   283
                            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   284
                        }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   285
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   286
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   287
                EnumSet<DigestEchoServer.HttpAuthSchemeType> digests =
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   288
                        EnumSet.of(DigestEchoServer.HttpAuthSchemeType.DIGEST);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   289
                for (DigestEchoServer.HttpAuthSchemeType authScheme : digests) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   290
                    DigestEchoClient dec = new DigestEchoClient(useSSL,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   291
                            authScheme,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   292
                            authType);
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   293
                    for (Version clientVersion : clientVersions()) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   294
                        for (Version serverVersion : serverVersions(clientVersion)) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   295
                            for (boolean expectContinue : expectContinue(serverVersion)) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   296
                                for (boolean async : BOOLEANS) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   297
                                    dec.testDigest(clientVersion, serverVersion,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   298
                                            async, expectContinue);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   299
                                }
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   300
                            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   301
                        }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   302
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   303
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   304
            }
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   305
        } catch(Throwable t) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   306
            System.out.println("Unexpected exception: exiting: " + t);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   307
            t.printStackTrace();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   308
            throw t;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   309
        } finally {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   310
            EchoServers.stop();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   311
            System.out.println(" ---------------------------------------------------------- ");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   312
            System.out.println(String.format("DigestEchoClient %s %s", useSSL ? "SSL" : "CLEAR", types));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   313
            System.out.println(String.format("Created %d clients and %d servers",
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   314
                    clientCount.get(), serverCount.get()));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   315
            System.out.println(String.format("basics:  %d requests sent, %d ns / req",
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   316
                    basicCount.get(), basics.get()));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   317
            System.out.println(String.format("digests: %d requests sent, %d ns / req",
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   318
                    digestCount.get(), digests.get()));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   319
            System.out.println(" ---------------------------------------------------------- ");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   320
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   321
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   322
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   323
    boolean isSchemeDisabled() {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   324
        String disabledSchemes;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   325
        if (isProxy(authType)) {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   326
            disabledSchemes = useSSL
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   327
                    ? TUNNEL_DISABLED
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   328
                    : PROXY_DISABLED;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   329
        } else return false;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   330
        if (disabledSchemes == null
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   331
                || disabledSchemes.isEmpty()) {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   332
            return false;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   333
        }
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   334
        String scheme;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   335
        switch (authScheme) {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   336
            case DIGEST:
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   337
                scheme = "Digest";
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   338
                break;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   339
            case BASIC:
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   340
                scheme = "Basic";
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   341
                break;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   342
            case BASICSERVER:
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   343
                scheme = "Basic";
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   344
                break;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   345
            case NONE:
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   346
                return false;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   347
            default:
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   348
                throw new InternalError("Unknown auth scheme: " + authScheme);
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   349
        }
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   350
        return Stream.of(disabledSchemes.split(","))
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   351
                .map(String::trim)
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   352
                .filter(scheme::equalsIgnoreCase)
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   353
                .findAny()
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   354
                .isPresent();
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   355
    }
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   356
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   357
    final static AtomicLong basics = new AtomicLong();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   358
    final static AtomicLong basicCount = new AtomicLong();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   359
    // @Test
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   360
    void testBasic(Version clientVersion, Version serverVersion, boolean async,
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   361
                   boolean expectContinue, boolean preemptive)
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   362
        throws Exception
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   363
    {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   364
        final boolean addHeaders = authScheme == DigestEchoServer.HttpAuthSchemeType.BASICSERVER;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   365
        // !preemptive has no meaning if we don't handle the authorization
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   366
        // headers ourselves
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   367
        if (!preemptive && !addHeaders) return;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   368
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   369
        out.println(format("*** testBasic: client: %s, server: %s, async: %s, useSSL: %s, " +
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   370
                        "authScheme: %s, authType: %s, expectContinue: %s preemptive: %s***",
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   371
                clientVersion, serverVersion, async, useSSL, authScheme, authType,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   372
                expectContinue, preemptive));
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   373
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   374
        DigestEchoServer server = EchoServers.of(serverVersion,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   375
                useSSL ? "https" : "http", authType, authScheme);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   376
        URI uri = DigestEchoServer.uri(useSSL ? "https" : "http",
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   377
                server.getServerAddress(), "/foo/");
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   378
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   379
        HttpClient client = newHttpClient(server);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   380
        HttpResponse<String> r;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   381
        CompletableFuture<HttpResponse<String>> cf1;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   382
        String auth = null;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   383
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   384
        try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   385
            for (int i=0; i<data.length; i++) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   386
                out.println(DigestEchoServer.now() + " ----- iteration " + i + " -----");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   387
                List<String> lines = List.of(Arrays.copyOfRange(data, 0, i+1));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   388
                assert lines.size() == i + 1;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   389
                String body = lines.stream().collect(Collectors.joining("\r\n"));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   390
                HttpRequest.BodyPublisher reqBody = HttpRequest.BodyPublisher.fromString(body);
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   391
                HttpRequest.Builder builder = HttpRequest.newBuilder(uri).version(clientVersion)
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   392
                        .POST(reqBody).expectContinue(expectContinue);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   393
                boolean isTunnel = isProxy(authType) && useSSL;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   394
                if (addHeaders) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   395
                    // handle authentication ourselves
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   396
                    assert !client.authenticator().isPresent();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   397
                    if (auth == null) auth = "Basic " + getBasicAuth("arthur");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   398
                    try {
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   399
                        if ((i > 0 || preemptive)
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   400
                                && (!isTunnel || i == 0 || isSchemeDisabled())) {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   401
                            // In case of a SSL tunnel through proxy then only the
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   402
                            // first request should require proxy authorization
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   403
                            // Though this might be invalidated if the server decides
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   404
                            // to close the connection...
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   405
                            out.println(String.format("%s adding %s: %s",
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   406
                                    DigestEchoServer.now(),
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   407
                                    authorizationKey(authType),
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   408
                                    auth));
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   409
                            builder = builder.header(authorizationKey(authType), auth);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   410
                        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   411
                    } catch (IllegalArgumentException x) {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   412
                        throw x;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   413
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   414
                } else {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   415
                    // let the stack do the authentication
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   416
                    assert client.authenticator().isPresent();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   417
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   418
                long start = System.nanoTime();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   419
                HttpRequest request = builder.build();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   420
                HttpResponse<Stream<String>> resp;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   421
                try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   422
                    if (async) {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   423
                        resp = client.sendAsync(request, asLines()).join();
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   424
                    } else {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   425
                        resp = client.send(request, asLines());
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   426
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   427
                } catch (Throwable t) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   428
                    long stop = System.nanoTime();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   429
                    synchronized (basicCount) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   430
                        long n = basicCount.getAndIncrement();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   431
                        basics.set((basics.get() * n + (stop - start)) / (n + 1));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   432
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   433
                    // unwrap CompletionException
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   434
                    if (t instanceof CompletionException) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   435
                        assert t.getCause() != null;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   436
                        t = t.getCause();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   437
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   438
                    throw new RuntimeException("Unexpected exception: " + t, t);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   439
                }
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   440
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   441
                if (addHeaders && !preemptive && (i==0 || isSchemeDisabled())) {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   442
                    assert resp.statusCode() == 401 || resp.statusCode() == 407;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   443
                    System.out.println(String.format("%s received: adding header %s: %s",
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   444
                            resp.statusCode(), authorizationKey(authType), auth));
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   445
                    request = HttpRequest.newBuilder(uri).version(clientVersion)
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   446
                            .POST(reqBody).header(authorizationKey(authType), auth).build();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   447
                    if (async) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   448
                        resp = client.sendAsync(request, asLines()).join();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   449
                    } else {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   450
                        resp = client.send(request, asLines());
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   451
                    }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   452
                }
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   453
                final List<String> respLines;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   454
                try {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   455
                    if (isSchemeDisabled()) {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   456
                        if (resp.statusCode() != 407) {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   457
                            throw new RuntimeException("expected 407 not received");
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   458
                        }
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   459
                        System.out.println("Scheme disabled for [" + authType
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   460
                                + ", " + authScheme
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   461
                                + ", " + (useSSL ? "HTTP" : "HTTPS")
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   462
                                + "]: Received expected " + resp.statusCode());
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   463
                        continue;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   464
                    } else {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   465
                        System.out.println("Scheme enabled for [" + authType
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   466
                                + ", " + authScheme
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   467
                                + ", " + (useSSL ? "HTTPS" : "HTTP")
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   468
                                + "]: Expecting 200, response is: " + resp);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   469
                        assert resp.statusCode() == 200 : "200 expected, received " + resp;
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   470
                        respLines = resp.body().collect(Collectors.toList());
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   471
                    }
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   472
                } finally {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   473
                    long stop = System.nanoTime();
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   474
                    synchronized (basicCount) {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   475
                        long n = basicCount.getAndIncrement();
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   476
                        basics.set((basics.get() * n + (stop - start)) / (n + 1));
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   477
                    }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   478
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   479
                if (!lines.equals(respLines)) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   480
                    throw new RuntimeException("Unexpected response: " + respLines);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   481
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   482
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   483
        } finally {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   484
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   485
        System.out.println("OK");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   486
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   487
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   488
    String getBasicAuth(String username) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   489
        StringBuilder builder = new StringBuilder(username);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   490
        builder.append(':');
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   491
        for (char c : DigestEchoServer.AUTHENTICATOR.getPassword(username)) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   492
            builder.append(c);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   493
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   494
        return Base64.getEncoder().encodeToString(builder.toString().getBytes(StandardCharsets.UTF_8));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   495
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   496
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   497
    final static AtomicLong digests = new AtomicLong();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   498
    final static AtomicLong digestCount = new AtomicLong();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   499
    // @Test
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   500
    void testDigest(Version clientVersion, Version serverVersion,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   501
                    boolean async, boolean expectContinue)
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   502
            throws Exception
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   503
    {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   504
        out.println(format("*** testDigest: client: %s, server: %s, async: %s, useSSL: %s, " +
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   505
                        "authScheme: %s, authType: %s, expectContinue: %s  ***",
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   506
                clientVersion, serverVersion, async, useSSL,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   507
                authScheme, authType, expectContinue));
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   508
        DigestEchoServer server = EchoServers.of(serverVersion,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   509
                useSSL ? "https" : "http", authType, authScheme);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   510
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   511
        URI uri = DigestEchoServer.uri(useSSL ? "https" : "http",
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   512
                server.getServerAddress(), "/foo/");
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   513
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   514
        HttpClient client = newHttpClient(server);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   515
        HttpResponse<String> r;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   516
        CompletableFuture<HttpResponse<String>> cf1;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   517
        byte[] cnonce = new byte[16];
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   518
        String cnonceStr = null;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   519
        DigestEchoServer.DigestResponse challenge = null;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   520
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   521
        try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   522
            for (int i=0; i<data.length; i++) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   523
                out.println(DigestEchoServer.now() + "----- iteration " + i + " -----");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   524
                List<String> lines = List.of(Arrays.copyOfRange(data, 0, i+1));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   525
                assert lines.size() == i + 1;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   526
                String body = lines.stream().collect(Collectors.joining("\r\n"));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   527
                HttpRequest.BodyPublisher reqBody = HttpRequest.BodyPublisher.fromString(body);
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   528
                HttpRequest.Builder reqBuilder = HttpRequest
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   529
                        .newBuilder(uri).version(clientVersion).POST(reqBody)
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   530
                        .expectContinue(expectContinue);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   531
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   532
                boolean isTunnel = isProxy(authType) && useSSL;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   533
                String digestMethod = isTunnel ? "CONNECT" : "POST";
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   534
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   535
                // In case of a tunnel connection only the first request
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   536
                // which establishes the tunnel needs to authenticate with
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   537
                // the proxy.
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   538
                if (challenge != null && (!isTunnel || isSchemeDisabled())) {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   539
                    assert cnonceStr != null;
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   540
                    String auth = digestResponse(uri, digestMethod, challenge, cnonceStr);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   541
                    try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   542
                        reqBuilder = reqBuilder.header(authorizationKey(authType), auth);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   543
                    } catch (IllegalArgumentException x) {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   544
                        throw x;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   545
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   546
                }
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   547
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   548
                long start = System.nanoTime();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   549
                HttpRequest request = reqBuilder.build();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   550
                HttpResponse<Stream<String>> resp;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   551
                if (async) {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   552
                    resp = client.sendAsync(request, asLines()).join();
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   553
                } else {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   554
                    resp = client.send(request, asLines());
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   555
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   556
                System.out.println(resp);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   557
                assert challenge != null || resp.statusCode() == 401 || resp.statusCode() == 407;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   558
                if (resp.statusCode() == 401 || resp.statusCode() == 407) {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   559
                    // This assert may need to be relaxed if our server happened to
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   560
                    // decide to close the tunnel connection, in which case we would
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   561
                    // receive 407 again...
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   562
                    assert challenge == null || !isTunnel || isSchemeDisabled()
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   563
                            : "No proxy auth should be required after establishing an SSL tunnel";
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   564
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   565
                    System.out.println("Received " + resp.statusCode() + " answering challenge...");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   566
                    random.nextBytes(cnonce);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   567
                    cnonceStr = new BigInteger(1, cnonce).toString(16);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   568
                    System.out.println("Response headers: " + resp.headers());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   569
                    Optional<String> authenticateOpt = resp.headers().firstValue(authenticateKey(authType));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   570
                    String authenticate = authenticateOpt.orElseThrow(
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   571
                            () -> new RuntimeException(authenticateKey(authType) + ": not found"));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   572
                    assert authenticate.startsWith("Digest ");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   573
                    HeaderParser hp = new HeaderParser(authenticate.substring("Digest ".length()));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   574
                    String qop = hp.findValue("qop");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   575
                    String nonce = hp.findValue("nonce");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   576
                    if (qop == null && nonce == null) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   577
                        throw new RuntimeException("QOP and NONCE not found");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   578
                    }
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   579
                    challenge = DigestEchoServer.DigestResponse
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   580
                            .create(authenticate.substring("Digest ".length()));
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   581
                    String auth = digestResponse(uri, digestMethod, challenge, cnonceStr);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   582
                    try {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   583
                        request = HttpRequest.newBuilder(uri).version(clientVersion)
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   584
                            .POST(reqBody).header(authorizationKey(authType), auth).build();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   585
                    } catch (IllegalArgumentException x) {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   586
                        throw x;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   587
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   588
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   589
                    if (async) {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   590
                        resp = client.sendAsync(request, asLines()).join();
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   591
                    } else {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   592
                        resp = client.send(request, asLines());
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   593
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   594
                    System.out.println(resp);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   595
                }
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   596
                final List<String> respLines;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   597
                try {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   598
                    if (isSchemeDisabled()) {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   599
                        if (resp.statusCode() != 407) {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   600
                            throw new RuntimeException("expected 407 not received");
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   601
                        }
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   602
                        System.out.println("Scheme disabled for [" + authType
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   603
                                + ", " + authScheme +
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   604
                                ", " + (useSSL ? "HTTP" : "HTTPS")
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   605
                                + "]: Received expected " + resp.statusCode());
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   606
                        continue;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   607
                    } else {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   608
                        assert resp.statusCode() == 200;
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   609
                        respLines = resp.body().collect(Collectors.toList());
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   610
                    }
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   611
                } finally {
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   612
                    long stop = System.nanoTime();
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   613
                    synchronized (digestCount) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   614
                        long n = digestCount.getAndIncrement();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   615
                        digests.set((digests.get() * n + (stop - start)) / (n + 1));
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
   616
                    }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   617
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   618
                if (!lines.equals(respLines)) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   619
                    throw new RuntimeException("Unexpected response: " + respLines);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   620
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   621
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   622
        } finally {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   623
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   624
        System.out.println("OK");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   625
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   626
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   627
    // WARNING: This is not a full fledged implementation of DIGEST.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   628
    // It does contain bugs and inaccuracy.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   629
    static String digestResponse(URI uri, String method, DigestEchoServer.DigestResponse challenge, String cnonce)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   630
            throws NoSuchAlgorithmException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   631
        int nc = NC.incrementAndGet();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   632
        DigestEchoServer.DigestResponse response1 = new DigestEchoServer.DigestResponse("earth",
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   633
                "arthur", challenge.nonce, cnonce, String.valueOf(nc), uri.toASCIIString(),
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   634
                challenge.algorithm, challenge.qop, challenge.opaque, null);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   635
        String response = DigestEchoServer.DigestResponse.computeDigest(true, method,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   636
                DigestEchoServer.AUTHENTICATOR.getPassword("arthur"), response1);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   637
        String auth = "Digest username=\"arthur\", realm=\"earth\""
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   638
                + ", response=\"" + response + "\", uri=\""+uri.toASCIIString()+"\""
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   639
                + ", qop=\"" + response1.qop + "\", cnonce=\"" + response1.cnonce
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   640
                + "\", nc=\"" + nc + "\", nonce=\"" + response1.nonce + "\"";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   641
        if (response1.opaque != null) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   642
            auth = auth + ", opaque=\"" + response1.opaque + "\"";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   643
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   644
        return auth;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   645
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   646
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   647
    static String authenticateKey(DigestEchoServer.HttpAuthType authType) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   648
        switch (authType) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   649
            case SERVER: return "www-authenticate";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   650
            case SERVER307: return "www-authenticate";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   651
            case PROXY: return "proxy-authenticate";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   652
            case PROXY305: return "proxy-authenticate";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   653
            default: throw new InternalError("authType: " + authType);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   654
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   655
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   656
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   657
    static String authorizationKey(DigestEchoServer.HttpAuthType authType) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   658
        switch (authType) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   659
            case SERVER: return "authorization";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   660
            case SERVER307: return "Authorization";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   661
            case PROXY: return "Proxy-Authorization";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   662
            case PROXY305: return "proxy-Authorization";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   663
            default: throw new InternalError("authType: " + authType);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   664
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   665
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   666
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   667
    static boolean isProxy(DigestEchoServer.HttpAuthType authType) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   668
        switch (authType) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   669
            case SERVER: return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   670
            case SERVER307: return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   671
            case PROXY: return true;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   672
            case PROXY305: return true;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   673
            default: throw new InternalError("authType: " + authType);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   674
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   675
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   676
}