test/jdk/java/net/httpclient/DigestEchoServer.java
author chegar
Mon, 26 Feb 2018 18:18:49 +0000
branchhttp-client-branch
changeset 56186 261b5b524dd3
parent 56136 3b58e5bacad6
child 56233 1753108d07b9
permissions -rw-r--r--
http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
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.  Oracle designates this
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    10
 *
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    11
 * 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
    12
 * 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
    13
 * 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
    14
 * 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
    15
 * accompanied this code).
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    16
 *
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    17
 * 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
    18
 * 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
    19
 * 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
    20
 *
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    21
 * 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
    22
 * 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
    23
 * questions.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    24
 */
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    25
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    26
import com.sun.net.httpserver.BasicAuthenticator;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    27
import com.sun.net.httpserver.HttpServer;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    28
import com.sun.net.httpserver.HttpsConfigurator;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    29
import com.sun.net.httpserver.HttpsParameters;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    30
import com.sun.net.httpserver.HttpsServer;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    31
import java.io.IOException;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    32
import java.io.InputStream;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    33
import java.io.OutputStream;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    34
import java.io.OutputStreamWriter;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    35
import java.io.PrintWriter;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    36
import java.io.Writer;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    37
import java.math.BigInteger;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    38
import java.net.Authenticator;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    39
import java.net.HttpURLConnection;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    40
import java.net.InetSocketAddress;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    41
import java.net.MalformedURLException;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    42
import java.net.PasswordAuthentication;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    43
import java.net.ServerSocket;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    44
import java.net.Socket;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    45
import java.net.URI;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    46
import java.net.URISyntaxException;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    47
import java.net.URL;
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
    48
import java.nio.charset.StandardCharsets;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    49
import java.security.MessageDigest;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    50
import java.security.NoSuchAlgorithmException;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    51
import java.time.Instant;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    52
import java.util.ArrayList;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    53
import java.util.Arrays;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    54
import java.util.Base64;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    55
import java.util.List;
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
    56
import java.util.Locale;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    57
import java.util.Objects;
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
    58
import java.util.Optional;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    59
import java.util.Random;
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
    60
import java.util.StringTokenizer;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    61
import java.util.concurrent.CompletableFuture;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    62
import java.util.concurrent.CopyOnWriteArrayList;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    63
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
    64
import java.util.stream.Collectors;
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
    65
import java.util.stream.Stream;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    66
import javax.net.ssl.SSLContext;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    67
import sun.net.www.HeaderParser;
56089
42208b2f224e http-client-branch: move to standard package and module name
chegar
parents: 56070
diff changeset
    68
import java.net.http.HttpClient.Version;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    69
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    70
/**
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    71
 * A simple HTTP server that supports Basic or Digest authentication.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    72
 * By default this server will echo back whatever is present
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    73
 * in the request body. Note that the Digest authentication is
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    74
 * a test implementation implemented only for tests purposes.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    75
 * @author danielfuchs
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    76
 */
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
    77
public abstract class DigestEchoServer implements HttpServerAdapters {
56033
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 static final boolean DEBUG =
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    80
            Boolean.parseBoolean(System.getProperty("test.debug", "false"));
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
    81
    public enum HttpAuthType {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
    82
        SERVER, PROXY, SERVER307, PROXY305
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
    83
        /* add PROXY_AND_SERVER and SERVER_PROXY_NONE */
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
    84
    };
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    85
    public enum HttpAuthSchemeType { NONE, BASICSERVER, BASIC, DIGEST };
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    86
    public static final HttpAuthType DEFAULT_HTTP_AUTH_TYPE = HttpAuthType.SERVER;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    87
    public static final String DEFAULT_PROTOCOL_TYPE = "https";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    88
    public static final HttpAuthSchemeType DEFAULT_SCHEME_TYPE = HttpAuthSchemeType.DIGEST;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    89
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    90
    public static class HttpTestAuthenticator extends Authenticator {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    91
        private final String realm;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    92
        private final String username;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    93
        // Used to prevent incrementation of 'count' when calling the
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    94
        // authenticator from the server side.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    95
        private final ThreadLocal<Boolean> skipCount = new ThreadLocal<>();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    96
        // count will be incremented every time getPasswordAuthentication()
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    97
        // is called from the client side.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    98
        final AtomicInteger count = new AtomicInteger();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
    99
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   100
        public HttpTestAuthenticator(String realm, String username) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   101
            this.realm = realm;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   102
            this.username = username;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   103
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   104
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   105
        protected PasswordAuthentication getPasswordAuthentication() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   106
            if (skipCount.get() == null || skipCount.get().booleanValue() == false) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   107
                System.out.println("Authenticator called: " + count.incrementAndGet());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   108
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   109
            return new PasswordAuthentication(getUserName(),
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   110
                    new char[] {'d','e','n', 't'});
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   111
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   112
        // Called by the server side to get the password of the user
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   113
        // being authentified.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   114
        public final char[] getPassword(String user) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   115
            if (user.equals(username)) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   116
                skipCount.set(Boolean.TRUE);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   117
                try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   118
                    return getPasswordAuthentication().getPassword();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   119
                } finally {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   120
                    skipCount.set(Boolean.FALSE);
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
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   123
            throw new SecurityException("User unknown: " + user);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   124
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   125
        public final String getUserName() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   126
            return username;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   127
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   128
        public final String getRealm() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   129
            return realm;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   130
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   131
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   132
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   133
    public static final HttpTestAuthenticator AUTHENTICATOR;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   134
    static {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   135
        AUTHENTICATOR = new HttpTestAuthenticator("earth", "arthur");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   136
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   137
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   138
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   139
    final HttpTestServer       serverImpl; // this server endpoint
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   140
    final DigestEchoServer     redirect;   // the target server where to redirect 3xx
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   141
    final HttpTestHandler      delegate;   // unused
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   142
    final String               key;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   143
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   144
    DigestEchoServer(String key,
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   145
                             HttpTestServer server,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   146
                             DigestEchoServer target,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   147
                             HttpTestHandler delegate) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   148
        this.key = key;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   149
        this.serverImpl = server;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   150
        this.redirect = target;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   151
        this.delegate = delegate;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   152
    }
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
    public static void main(String[] args)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   155
            throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   156
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   157
        DigestEchoServer server = create(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
   158
                DEFAULT_PROTOCOL_TYPE,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   159
                DEFAULT_HTTP_AUTH_TYPE,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   160
                AUTHENTICATOR,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   161
                DEFAULT_SCHEME_TYPE);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   162
        try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   163
            System.out.println("Server created at " + server.getAddress());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   164
            System.out.println("Strike <Return> to exit");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   165
            System.in.read();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   166
        } finally {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   167
            System.out.println("stopping server");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   168
            server.stop();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   169
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   170
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   171
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   172
    private static String toString(HttpTestHeaders headers) {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   173
        return headers.entrySet().stream()
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   174
                .map((e) -> e.getKey() + ": " + e.getValue())
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   175
                .collect(Collectors.joining("\n"));
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
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   178
    public static DigestEchoServer 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
   179
                                          String protocol,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   180
                                          HttpAuthType authType,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   181
                                          HttpAuthSchemeType schemeType)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   182
            throws IOException {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   183
        return create(version, protocol, authType, AUTHENTICATOR, schemeType);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   184
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   185
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   186
    public static DigestEchoServer 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
   187
                                          String protocol,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   188
                                          HttpAuthType authType,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   189
                                          HttpTestAuthenticator auth,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   190
                                          HttpAuthSchemeType schemeType)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   191
            throws IOException {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   192
        return create(version, protocol, authType, auth, schemeType, null);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   193
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   194
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   195
    public static DigestEchoServer 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
   196
                                        String protocol,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   197
                                        HttpAuthType authType,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   198
                                        HttpTestAuthenticator auth,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   199
                                        HttpAuthSchemeType schemeType,
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   200
                                        HttpTestHandler delegate)
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   201
            throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   202
        Objects.requireNonNull(authType);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   203
        Objects.requireNonNull(auth);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   204
        switch(authType) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   205
            // A server that performs Server Digest authentication.
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   206
            case SERVER: return createServer(version, protocol, authType, auth,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   207
                                             schemeType, delegate, "/");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   208
            // A server that pretends to be a Proxy and performs
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   209
            // Proxy Digest authentication. If protocol is HTTPS,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   210
            // then this will create a HttpsProxyTunnel that will
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   211
            // handle the CONNECT request for tunneling.
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   212
            case PROXY: return createProxy(version, protocol, authType, auth,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   213
                                           schemeType, delegate, "/");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   214
            // A server that sends 307 redirect to a server that performs
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   215
            // Digest authentication.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   216
            // Note: 301 doesn't work here because it transforms POST into GET.
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   217
            case SERVER307: return createServerAndRedirect(version,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   218
                                                        protocol,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   219
                                                        HttpAuthType.SERVER,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   220
                                                        auth, schemeType,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   221
                                                        delegate, 307);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   222
            // A server that sends 305 redirect to a proxy that performs
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   223
            // Digest authentication.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   224
            // Note: this is not correctly stubbed/implemented in this test.
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   225
            case PROXY305:  return createServerAndRedirect(version,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   226
                                                        protocol,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   227
                                                        HttpAuthType.PROXY,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   228
                                                        auth, schemeType,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   229
                                                        delegate, 305);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   230
            default:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   231
                throw new InternalError("Unknown server type: " + authType);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   232
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   233
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   234
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   235
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   236
    /**
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   237
     * The SocketBindableFactory ensures that the local port used by an HttpServer
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   238
     * or a proxy ServerSocket previously created by the current test/VM will not
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   239
     * get reused by a subsequent test in the same VM.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   240
     * This is to avoid having the test client trying to reuse cached connections.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   241
     */
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   242
    private static abstract class SocketBindableFactory<B> {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   243
        private static final int MAX = 10;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   244
        private static final CopyOnWriteArrayList<String> addresses =
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   245
                new CopyOnWriteArrayList<>();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   246
        protected B createInternal() throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   247
            final int max = addresses.size() + MAX;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   248
            final List<B> toClose = new ArrayList<>();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   249
            try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   250
                for (int i = 1; i <= max; i++) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   251
                    B bindable = createBindable();
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   252
                    InetSocketAddress address = getAddress(bindable);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   253
                    String key = "127.0.0.1:" + address.getPort();
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   254
                    if (addresses.addIfAbsent(key)) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   255
                        System.out.println("Socket bound to: " + key
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   256
                                + " after " + i + " attempt(s)");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   257
                        return bindable;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   258
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   259
                    System.out.println("warning: address " + key
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   260
                            + " already used. Retrying bind.");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   261
                    // keep the port bound until we get a port that we haven't
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   262
                    // used already
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   263
                    toClose.add(bindable);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   264
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   265
            } finally {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   266
                // if we had to retry, then close the socket we're not
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   267
                // going to use.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   268
                for (B b : toClose) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   269
                    try { close(b); } catch (Exception x) { /* ignore */ }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   270
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   271
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   272
            throw new IOException("Couldn't bind socket after " + max + " attempts: "
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   273
                    + "addresses used before: " + addresses);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   274
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   275
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   276
        protected abstract B createBindable() throws IOException;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   277
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   278
        protected abstract InetSocketAddress getAddress(B bindable);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   279
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   280
        protected abstract void close(B bindable) throws IOException;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   281
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   282
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   283
    /*
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   284
     * Used to create ServerSocket for a proxy.
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
    private static final class ServerSocketFactory
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   287
    extends SocketBindableFactory<ServerSocket> {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   288
        private static final ServerSocketFactory instance = new ServerSocketFactory();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   289
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   290
        static ServerSocket create() throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   291
            return instance.createInternal();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   292
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   293
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   294
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   295
        protected ServerSocket createBindable() throws IOException {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   296
            return new ServerSocket(0);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   297
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   298
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   299
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   300
        protected InetSocketAddress getAddress(ServerSocket socket) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   301
            return new InetSocketAddress(socket.getInetAddress(), socket.getLocalPort());
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
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   305
        protected void close(ServerSocket socket) throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   306
            socket.close();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   307
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   308
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   309
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   310
    /*
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   311
     * Used to create HttpServer
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   312
     */
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   313
    private static abstract class H1ServerFactory<S extends HttpServer>
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   314
            extends SocketBindableFactory<S> {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   315
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   316
        protected S createBindable() throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   317
            S server = newHttpServer();
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   318
            server.bind(new InetSocketAddress( 0), 0);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   319
            return server;
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
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   323
        protected InetSocketAddress getAddress(S server) {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   324
            return server.getAddress();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   325
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   326
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   327
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   328
        protected void close(S server) throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   329
            server.stop(1);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   330
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   331
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   332
        /*
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   333
         * Returns a HttpServer or a HttpsServer in different subclasses.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   334
         */
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   335
        protected abstract S newHttpServer() throws IOException;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   336
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   337
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   338
    /*
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   339
     * Used to create Http2TestServer
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   340
     */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   341
    private static abstract class H2ServerFactory<S extends Http2TestServer>
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   342
            extends SocketBindableFactory<S> {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   343
        @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   344
        protected S createBindable() throws IOException {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   345
            final S server;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   346
            try {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   347
                server = newHttpServer();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   348
            } catch (IOException io) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   349
                throw io;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   350
            } catch (Exception x) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   351
                throw new IOException(x);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   352
            }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   353
            return server;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   354
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   355
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   356
        @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   357
        protected InetSocketAddress getAddress(S server) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   358
            return server.getAddress();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   359
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   360
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   361
        @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   362
        protected void close(S server) throws IOException {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   363
            server.stop();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   364
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   365
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   366
        /*
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   367
         * Returns a HttpServer or a HttpsServer in different subclasses.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   368
         */
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   369
        protected abstract S newHttpServer() throws Exception;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   370
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   371
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   372
    private static final class Http2ServerFactory extends H2ServerFactory<Http2TestServer> {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   373
        private static final Http2ServerFactory instance = new Http2ServerFactory();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   374
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   375
        static Http2TestServer create() throws IOException {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   376
            return instance.createInternal();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   377
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   378
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   379
        @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   380
        protected Http2TestServer newHttpServer() throws Exception {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   381
            return new Http2TestServer("127.0.0.1", false, 0);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   382
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   383
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   384
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   385
    private static final class Https2ServerFactory extends H2ServerFactory<Http2TestServer> {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   386
        private static final Https2ServerFactory instance = new Https2ServerFactory();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   387
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   388
        static Http2TestServer create() throws IOException {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   389
            return instance.createInternal();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   390
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   391
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   392
        @Override
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   393
        protected Http2TestServer newHttpServer() throws Exception {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   394
            return new Http2TestServer("127.0.0.1", true, 0);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   395
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   396
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   397
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   398
    private static final class Http1ServerFactory extends H1ServerFactory<HttpServer> {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   399
        private static final Http1ServerFactory instance = new Http1ServerFactory();
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   400
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   401
        static HttpServer create() throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   402
            return instance.createInternal();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   403
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   404
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   405
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   406
        protected HttpServer newHttpServer() throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   407
            return HttpServer.create();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   408
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   409
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   410
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   411
    private static final class Https1ServerFactory extends H1ServerFactory<HttpsServer> {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   412
        private static final Https1ServerFactory instance = new Https1ServerFactory();
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
        static HttpsServer create() throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   415
            return instance.createInternal();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   416
        }
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
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   419
        protected HttpsServer newHttpServer() throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   420
            return HttpsServer.create();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   421
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   422
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   423
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   424
    static Http2TestServer createHttp2Server(String protocol) throws IOException {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   425
        final Http2TestServer server;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   426
        if ("http".equalsIgnoreCase(protocol)) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   427
            server = Http2ServerFactory.create();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   428
        } else if ("https".equalsIgnoreCase(protocol)) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   429
            server = Https2ServerFactory.create();
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   430
        } else {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   431
            throw new InternalError("unsupported protocol: " + protocol);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   432
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   433
        return server;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   434
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   435
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   436
    static HttpTestServer createHttpServer(Version version, String protocol)
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   437
            throws IOException
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   438
    {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   439
        switch(version) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   440
            case 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
   441
                return HttpTestServer.of(createHttp1Server(protocol));
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   442
            case HTTP_2:
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   443
                return HttpTestServer.of(createHttp2Server(protocol));
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   444
            default:
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   445
                throw new InternalError("Unexpected version: " + version);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   446
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   447
    }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   448
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   449
    static HttpServer createHttp1Server(String protocol) throws IOException {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   450
        final HttpServer server;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   451
        if ("http".equalsIgnoreCase(protocol)) {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   452
            server = Http1ServerFactory.create();
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   453
        } else if ("https".equalsIgnoreCase(protocol)) {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   454
            server = configure(Https1ServerFactory.create());
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   455
        } else {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   456
            throw new InternalError("unsupported protocol: " + protocol);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   457
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   458
        return server;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   459
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   460
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   461
    static HttpsServer configure(HttpsServer server) throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   462
        try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   463
            SSLContext ctx = SSLContext.getDefault();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   464
            server.setHttpsConfigurator(new Configurator(ctx));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   465
        } catch (NoSuchAlgorithmException ex) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   466
            throw new IOException(ex);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   467
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   468
        return server;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   469
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   470
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   471
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   472
    static void setContextAuthenticator(HttpTestContext ctxt,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   473
                                        HttpTestAuthenticator auth) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   474
        final String realm = auth.getRealm();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   475
        com.sun.net.httpserver.Authenticator authenticator =
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   476
            new BasicAuthenticator(realm) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   477
                @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   478
                public boolean checkCredentials(String username, String pwd) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   479
                    return auth.getUserName().equals(username)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   480
                           && new String(auth.getPassword(username)).equals(pwd);
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
        ctxt.setAuthenticator(authenticator);
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
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   486
    public static DigestEchoServer createServer(Version version,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   487
                                        String protocol,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   488
                                        HttpAuthType authType,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   489
                                        HttpTestAuthenticator auth,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   490
                                        HttpAuthSchemeType schemeType,
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   491
                                        HttpTestHandler delegate,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   492
                                        String path)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   493
            throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   494
        Objects.requireNonNull(authType);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   495
        Objects.requireNonNull(auth);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   496
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   497
        HttpTestServer impl = createHttpServer(version, protocol);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   498
        String key = String.format("DigestEchoServer[PID=%s,PORT=%s]:%s:%s:%s:%s",
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   499
                ProcessHandle.current().pid(),
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   500
                impl.getAddress().getPort(),
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   501
                version, protocol, authType, schemeType);
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   502
        final DigestEchoServer server = new DigestEchoServerImpl(key, impl, null, delegate);
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   503
        final HttpTestHandler handler =
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   504
                server.createHandler(schemeType, auth, authType, false);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   505
        HttpTestContext context = impl.addHandler(handler, path);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   506
        server.configureAuthentication(context, schemeType, auth, authType);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   507
        impl.start();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   508
        return server;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   509
    }
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
    public static DigestEchoServer createProxy(Version version,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   512
                                        String protocol,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   513
                                        HttpAuthType authType,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   514
                                        HttpTestAuthenticator auth,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   515
                                        HttpAuthSchemeType schemeType,
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   516
                                        HttpTestHandler delegate,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   517
                                        String path)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   518
            throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   519
        Objects.requireNonNull(authType);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   520
        Objects.requireNonNull(auth);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   521
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   522
        if (version == Version.HTTP_2 && protocol.equalsIgnoreCase("http")) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   523
            System.out.println("WARNING: can't use HTTP/1.1 proxy with unsecure HTTP/2 server");
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   524
            version = 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
   525
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   526
        HttpTestServer impl = createHttpServer(version, protocol);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   527
        String key = String.format("DigestEchoServer[PID=%s,PORT=%s]:%s:%s:%s:%s",
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   528
                ProcessHandle.current().pid(),
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   529
                impl.getAddress().getPort(),
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   530
                version, protocol, authType, schemeType);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   531
        final DigestEchoServer server = "https".equalsIgnoreCase(protocol)
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   532
                ? new HttpsProxyTunnel(key, impl, null, delegate)
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   533
                : new DigestEchoServerImpl(key, impl, null, delegate);
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   534
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   535
        final HttpTestHandler hh = server.createHandler(HttpAuthSchemeType.NONE,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   536
                                         null, HttpAuthType.SERVER,
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   537
                                         server instanceof HttpsProxyTunnel);
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   538
        HttpTestContext ctxt = impl.addHandler(hh, path);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   539
        server.configureAuthentication(ctxt, schemeType, auth, authType);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   540
        impl.start();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   541
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   542
        return server;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   543
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   544
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   545
    public static DigestEchoServer createServerAndRedirect(
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   546
                                        Version version,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   547
                                        String protocol,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   548
                                        HttpAuthType targetAuthType,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   549
                                        HttpTestAuthenticator auth,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   550
                                        HttpAuthSchemeType schemeType,
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   551
                                        HttpTestHandler targetDelegate,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   552
                                        int code300)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   553
            throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   554
        Objects.requireNonNull(targetAuthType);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   555
        Objects.requireNonNull(auth);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   556
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   557
        // The connection between client and proxy can only
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   558
        // be a plain connection: SSL connection to proxy
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   559
        // is not supported by our client connection.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   560
        String targetProtocol = targetAuthType == HttpAuthType.PROXY
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   561
                                          ? "http"
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   562
                                          : protocol;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   563
        DigestEchoServer redirectTarget =
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   564
                (targetAuthType == HttpAuthType.PROXY)
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   565
                ? createProxy(version, protocol, targetAuthType,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   566
                              auth, schemeType, targetDelegate, "/")
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   567
                : createServer(version, targetProtocol, targetAuthType,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   568
                               auth, schemeType, targetDelegate, "/");
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   569
        HttpTestServer impl = createHttpServer(version, protocol);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   570
        String key = String.format("RedirectingServer[PID=%s,PORT=%s]:%s:%s:%s:%s",
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   571
                ProcessHandle.current().pid(),
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   572
                impl.getAddress().getPort(),
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   573
                version, protocol,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   574
                HttpAuthType.SERVER, code300)
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   575
                + "->" + redirectTarget.key;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   576
        final DigestEchoServer redirectingServer =
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   577
                 new DigestEchoServerImpl(key, impl, redirectTarget, null);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   578
        InetSocketAddress redirectAddr = redirectTarget.getAddress();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   579
        URL locationURL = url(targetProtocol, redirectAddr, "/");
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   580
        final HttpTestHandler hh = redirectingServer.create300Handler(key, locationURL,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   581
                                             HttpAuthType.SERVER, code300);
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   582
        impl.addHandler(hh,"/");
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   583
        impl.start();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   584
        return redirectingServer;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   585
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   586
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   587
    public abstract InetSocketAddress getServerAddress();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   588
    public abstract InetSocketAddress getProxyAddress();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   589
    public abstract InetSocketAddress getAddress();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   590
    public abstract void stop();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   591
    public abstract Version getServerVersion();
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   592
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   593
    private static class DigestEchoServerImpl extends DigestEchoServer {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   594
        DigestEchoServerImpl(String key,
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   595
                             HttpTestServer server,
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   596
                             DigestEchoServer target,
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   597
                             HttpTestHandler delegate) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   598
            super(key, Objects.requireNonNull(server), target, delegate);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   599
        }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   600
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   601
        public InetSocketAddress getAddress() {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   602
            return new InetSocketAddress("127.0.0.1",
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   603
                    serverImpl.getAddress().getPort());
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   604
        }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   605
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   606
        public InetSocketAddress getServerAddress() {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   607
            return new InetSocketAddress("127.0.0.1",
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   608
                    serverImpl.getAddress().getPort());
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   609
        }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   610
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   611
        public InetSocketAddress getProxyAddress() {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   612
            return new InetSocketAddress("127.0.0.1",
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   613
                    serverImpl.getAddress().getPort());
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   614
        }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   615
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   616
        public Version getServerVersion() {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   617
            return serverImpl.getVersion();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   618
        }
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   619
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   620
        public void stop() {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   621
            serverImpl.stop();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   622
            if (redirect != null) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   623
                redirect.stop();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
   624
            }
56033
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
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   628
    protected void writeResponse(HttpTestExchange he) throws IOException {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   629
        if (delegate == null) {
56136
3b58e5bacad6 http-client-branch: Fixed chunked encoding in DigestEchoServer
dfuchs
parents: 56128
diff changeset
   630
            he.sendResponseHeaders(HttpURLConnection.HTTP_OK, -1);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   631
            he.getResponseBody().write(he.getRequestBody().readAllBytes());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   632
        } else {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   633
            delegate.handle(he);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   634
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   635
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   636
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   637
    private HttpTestHandler createHandler(HttpAuthSchemeType schemeType,
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   638
                                      HttpTestAuthenticator auth,
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   639
                                      HttpAuthType authType,
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
   640
                                      boolean tunelled) {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   641
        return new HttpNoAuthHandler(key, authType, tunelled);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   642
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   643
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   644
    void configureAuthentication(HttpTestContext ctxt,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   645
                                 HttpAuthSchemeType schemeType,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   646
                                 HttpTestAuthenticator auth,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   647
                                 HttpAuthType authType) {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   648
        switch(schemeType) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   649
            case DIGEST:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   650
                // DIGEST authentication is handled by the handler.
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   651
                ctxt.addFilter(new HttpDigestFilter(key, auth, authType));
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   652
                break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   653
            case BASIC:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   654
                // BASIC authentication is handled by the filter.
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   655
                ctxt.addFilter(new HttpBasicFilter(key, auth, authType));
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   656
                break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   657
            case BASICSERVER:
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 PROXY: case PROXY305:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   660
                        // HttpServer can't support Proxy-type authentication
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   661
                        // => we do as if BASIC had been specified, and we will
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   662
                        //    handle authentication in the handler.
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   663
                        ctxt.addFilter(new HttpBasicFilter(key, auth, authType));
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   664
                        break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   665
                    case SERVER: case SERVER307:
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   666
                        if (ctxt.getVersion() == 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
   667
                            // Basic authentication is handled by HttpServer
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   668
                            // directly => the filter should not perform
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   669
                            // authentication again.
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   670
                            setContextAuthenticator(ctxt, auth);
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   671
                            ctxt.addFilter(new HttpNoAuthFilter(key, authType));
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   672
                        } else {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   673
                            ctxt.addFilter(new HttpBasicFilter(key, auth, authType));
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   674
                        }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   675
                        break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   676
                    default:
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   677
                        throw new InternalError(key + ": Invalid combination scheme="
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   678
                             + schemeType + " authType=" + authType);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   679
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   680
            case NONE:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   681
                // No authentication at all.
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   682
                ctxt.addFilter(new HttpNoAuthFilter(key, authType));
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   683
                break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   684
            default:
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   685
                throw new InternalError(key + ": No such scheme: " + schemeType);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   686
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   687
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   688
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   689
    private HttpTestHandler create300Handler(String key, URL proxyURL,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   690
                                             HttpAuthType type, int code300)
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   691
            throws MalformedURLException
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   692
    {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   693
        return new Http3xxHandler(key, proxyURL, type, code300);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   694
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   695
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   696
    // Abstract HTTP filter class.
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   697
    private abstract static class AbstractHttpFilter extends HttpTestFilter {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   698
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   699
        final HttpAuthType authType;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   700
        final String type;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   701
        public AbstractHttpFilter(HttpAuthType authType, String type) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   702
            this.authType = authType;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   703
            this.type = type;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   704
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   705
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   706
        String getLocation() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   707
            return "Location";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   708
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   709
        String getAuthenticate() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   710
            return authType == HttpAuthType.PROXY
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   711
                    ? "Proxy-Authenticate" : "WWW-Authenticate";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   712
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   713
        String getAuthorization() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   714
            return authType == HttpAuthType.PROXY
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   715
                    ? "Proxy-Authorization" : "Authorization";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   716
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   717
        int getUnauthorizedCode() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   718
            return authType == HttpAuthType.PROXY
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   719
                    ? HttpURLConnection.HTTP_PROXY_AUTH
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   720
                    : HttpURLConnection.HTTP_UNAUTHORIZED;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   721
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   722
        String getKeepAlive() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   723
            return "keep-alive";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   724
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   725
        String getConnection() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   726
            return authType == HttpAuthType.PROXY
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   727
                    ? "Proxy-Connection" : "Connection";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   728
        }
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   729
        protected abstract boolean isAuthentified(HttpTestExchange he) throws IOException;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   730
        protected abstract void requestAuthentication(HttpTestExchange he) throws IOException;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   731
        protected void accept(HttpTestExchange he, HttpChain chain) throws IOException {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   732
            chain.doFilter(he);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   733
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   734
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   735
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   736
        public String description() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   737
            return "Filter for " + type;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   738
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   739
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   740
        public void doFilter(HttpTestExchange he, HttpChain chain) throws IOException {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   741
            try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   742
                System.out.println(type + ": Got " + he.getRequestMethod()
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   743
                    + ": " + he.getRequestURI()
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   744
                    + "\n" + DigestEchoServer.toString(he.getRequestHeaders()));
56186
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56136
diff changeset
   745
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56136
diff changeset
   746
                // Assert only a single value for Expect. Not directly related
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56136
diff changeset
   747
                // to digest authentication, but verifies good client behaviour.
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56136
diff changeset
   748
                List<String> expectValues = he.getRequestHeaders().get("Expect");
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56136
diff changeset
   749
                if (expectValues != null && expectValues.size() > 1) {
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56136
diff changeset
   750
                    throw new IOException("Expect:  " + expectValues);
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56136
diff changeset
   751
                }
261b5b524dd3 http-client-branch: fix issues with redirect including Upgrade header when it should not, Expect containing more than one value, and SECURE redirect policy
chegar
parents: 56136
diff changeset
   752
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   753
                if (!isAuthentified(he)) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   754
                    try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   755
                        requestAuthentication(he);
56136
3b58e5bacad6 http-client-branch: Fixed chunked encoding in DigestEchoServer
dfuchs
parents: 56128
diff changeset
   756
                        he.sendResponseHeaders(getUnauthorizedCode(), -1);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   757
                        System.out.println(type
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   758
                            + ": Sent back " + getUnauthorizedCode());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   759
                    } finally {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   760
                        he.close();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   761
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   762
                } else {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   763
                    accept(he, chain);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   764
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   765
            } catch (RuntimeException | Error | IOException t) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   766
               System.err.println(type
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   767
                    + ": Unexpected exception while handling request: " + t);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   768
               t.printStackTrace(System.err);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   769
               he.close();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   770
               throw t;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   771
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   772
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   773
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   774
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   775
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   776
    // 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
   777
    // 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
   778
    final static class DigestResponse {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   779
        final String realm;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   780
        final String username;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   781
        final String nonce;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   782
        final String cnonce;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   783
        final String nc;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   784
        final String uri;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   785
        final String algorithm;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   786
        final String response;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   787
        final String qop;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   788
        final String opaque;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   789
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   790
        public DigestResponse(String realm, String username, String nonce,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   791
                              String cnonce, String nc, String uri,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   792
                              String algorithm, String qop, String opaque,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   793
                              String response) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   794
            this.realm = realm;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   795
            this.username = username;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   796
            this.nonce = nonce;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   797
            this.cnonce = cnonce;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   798
            this.nc = nc;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   799
            this.uri = uri;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   800
            this.algorithm = algorithm;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   801
            this.qop = qop;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   802
            this.opaque = opaque;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   803
            this.response = response;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   804
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   805
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   806
        String getAlgorithm(String defval) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   807
            return algorithm == null ? defval : algorithm;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   808
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   809
        String getQoP(String defval) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   810
            return qop == null ? defval : qop;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   811
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   812
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   813
        // Code stolen from DigestAuthentication:
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   814
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   815
        private static final char charArray[] = {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   816
            '0', '1', '2', '3', '4', '5', '6', '7',
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   817
            '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   818
        };
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   819
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   820
        private static String encode(String src, char[] passwd, MessageDigest md) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   821
            try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   822
                md.update(src.getBytes("ISO-8859-1"));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   823
            } catch (java.io.UnsupportedEncodingException uee) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   824
                assert false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   825
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   826
            if (passwd != null) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   827
                byte[] passwdBytes = new byte[passwd.length];
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   828
                for (int i=0; i<passwd.length; i++)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   829
                    passwdBytes[i] = (byte)passwd[i];
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   830
                md.update(passwdBytes);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   831
                Arrays.fill(passwdBytes, (byte)0x00);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   832
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   833
            byte[] digest = md.digest();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   834
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   835
            StringBuilder res = new StringBuilder(digest.length * 2);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   836
            for (int i = 0; i < digest.length; i++) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   837
                int hashchar = ((digest[i] >>> 4) & 0xf);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   838
                res.append(charArray[hashchar]);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   839
                hashchar = (digest[i] & 0xf);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   840
                res.append(charArray[hashchar]);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   841
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   842
            return res.toString();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   843
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   844
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   845
        public static String computeDigest(boolean isRequest,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   846
                                            String reqMethod,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   847
                                            char[] password,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   848
                                            DigestResponse params)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   849
            throws NoSuchAlgorithmException
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   850
        {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   851
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   852
            String A1, HashA1;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   853
            String algorithm = params.getAlgorithm("MD5");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   854
            boolean md5sess = algorithm.equalsIgnoreCase ("MD5-sess");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   855
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   856
            MessageDigest md = MessageDigest.getInstance(md5sess?"MD5":algorithm);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   857
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   858
            if (params.username == null) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   859
                throw new IllegalArgumentException("missing username");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   860
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   861
            if (params.realm == null) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   862
                throw new IllegalArgumentException("missing realm");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   863
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   864
            if (params.uri == null) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   865
                throw new IllegalArgumentException("missing uri");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   866
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   867
            if (params.nonce == null) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   868
                throw new IllegalArgumentException("missing nonce");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   869
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   870
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   871
            A1 = params.username + ":" + params.realm + ":";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   872
            HashA1 = encode(A1, password, md);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   873
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   874
            String A2;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   875
            if (isRequest) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   876
                A2 = reqMethod + ":" + params.uri;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   877
            } else {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   878
                A2 = ":" + params.uri;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   879
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   880
            String HashA2 = encode(A2, null, md);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   881
            String combo, finalHash;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   882
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   883
            if ("auth".equals(params.qop)) { /* RRC2617 when qop=auth */
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   884
                if (params.cnonce == null) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   885
                    throw new IllegalArgumentException("missing nonce");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   886
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   887
                if (params.nc == null) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   888
                    throw new IllegalArgumentException("missing nonce");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   889
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   890
                combo = HashA1+ ":" + params.nonce + ":" + params.nc + ":" +
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   891
                            params.cnonce + ":auth:" +HashA2;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   892
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   893
            } else { /* for compatibility with RFC2069 */
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   894
                combo = HashA1 + ":" +
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   895
                           params.nonce + ":" +
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   896
                           HashA2;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   897
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   898
            finalHash = encode(combo, null, md);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   899
            return finalHash;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   900
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   901
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   902
        public static DigestResponse create(String raw) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   903
            String username, realm, nonce, nc, uri, response, cnonce,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   904
                   algorithm, qop, opaque;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   905
            HeaderParser parser = new HeaderParser(raw);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   906
            username = parser.findValue("username");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   907
            realm = parser.findValue("realm");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   908
            nonce = parser.findValue("nonce");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   909
            nc = parser.findValue("nc");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   910
            uri = parser.findValue("uri");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   911
            cnonce = parser.findValue("cnonce");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   912
            response = parser.findValue("response");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   913
            algorithm = parser.findValue("algorithm");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   914
            qop = parser.findValue("qop");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   915
            opaque = parser.findValue("opaque");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   916
            return new DigestResponse(realm, username, nonce, cnonce, nc, uri,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   917
                                      algorithm, qop, opaque, response);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   918
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   919
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   920
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   921
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   922
    private static class HttpNoAuthFilter extends AbstractHttpFilter {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   923
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   924
        static String type(String key, HttpAuthType authType) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   925
            String type = authType == HttpAuthType.SERVER
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   926
                    ? "NoAuth Server Filter" : "NoAuth Proxy Filter";
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   927
            return "["+type+"]:"+key;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   928
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   929
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   930
        public HttpNoAuthFilter(String key, HttpAuthType authType) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   931
            super(authType, type(key, authType));
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   932
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   933
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   934
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   935
        protected boolean isAuthentified(HttpTestExchange he) throws IOException {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   936
            return true;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   937
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   938
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   939
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   940
        protected void requestAuthentication(HttpTestExchange he) throws IOException {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   941
            throw new InternalError("Should not com here");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   942
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   943
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   944
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   945
        public String description() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   946
            return "Passthrough Filter";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   947
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   948
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   949
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   950
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   951
    // An HTTP Filter that performs Basic authentication
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   952
    private static class HttpBasicFilter extends AbstractHttpFilter {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   953
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   954
        static String type(String key, HttpAuthType authType) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   955
            String type = authType == HttpAuthType.SERVER
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   956
                    ? "Basic Server Filter" : "Basic Proxy Filter";
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   957
            return "["+type+"]:"+key;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   958
        }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   959
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   960
        private final HttpTestAuthenticator auth;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   961
        public HttpBasicFilter(String key, HttpTestAuthenticator auth,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   962
                               HttpAuthType authType) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   963
            super(authType, type(key, authType));
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   964
            this.auth = auth;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   965
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   966
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   967
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   968
        protected void requestAuthentication(HttpTestExchange he)
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   969
            throws IOException {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   970
            he.getResponseHeaders().addHeader(getAuthenticate(),
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   971
                 "Basic realm=\"" + auth.getRealm() + "\"");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   972
            System.out.println(type + ": Requesting Basic Authentication "
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   973
                 + he.getResponseHeaders().firstValue(getAuthenticate()));
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   974
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   975
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   976
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
   977
        protected boolean isAuthentified(HttpTestExchange he) {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   978
            if (he.getRequestHeaders().containsKey(getAuthorization())) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   979
                List<String> authorization =
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   980
                    he.getRequestHeaders().get(getAuthorization());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   981
                for (String a : authorization) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   982
                    System.out.println(type + ": processing " + a);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   983
                    int sp = a.indexOf(' ');
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   984
                    if (sp < 0) return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   985
                    String scheme = a.substring(0, sp);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   986
                    if (!"Basic".equalsIgnoreCase(scheme)) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   987
                        System.out.println(type + ": Unsupported scheme '"
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   988
                                           + scheme +"'");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   989
                        return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   990
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   991
                    if (a.length() <= sp+1) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   992
                        System.out.println(type + ": value too short for '"
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   993
                                            + scheme +"'");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   994
                        return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   995
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   996
                    a = a.substring(sp+1);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   997
                    return validate(a);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   998
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
   999
                return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1000
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1001
            return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1002
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1003
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1004
        boolean validate(String a) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1005
            byte[] b = Base64.getDecoder().decode(a);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1006
            String userpass = new String (b);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1007
            int colon = userpass.indexOf (':');
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1008
            String uname = userpass.substring (0, colon);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1009
            String pass = userpass.substring (colon+1);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1010
            return auth.getUserName().equals(uname) &&
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1011
                   new String(auth.getPassword(uname)).equals(pass);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1012
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1013
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1014
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1015
        public String description() {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1016
            return "Filter for BASIC authentication: " + type;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1017
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1018
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1019
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1020
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1021
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1022
    // An HTTP Filter that performs Digest authentication
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1023
    // 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
  1024
    // It does contain bugs and inaccuracy.
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1025
    private static class HttpDigestFilter extends AbstractHttpFilter {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1026
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1027
        static String type(String key, HttpAuthType authType) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1028
            String type = authType == HttpAuthType.SERVER
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1029
                    ? "Digest Server Filter" : "Digest Proxy Filter";
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1030
            return "["+type+"]:"+key;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1031
        }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1032
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1033
        // This is a very basic DIGEST - used only for the purpose of testing
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1034
        // the client implementation. Therefore we can get away with never
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1035
        // updating the server nonce as it makes the implementation of the
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1036
        // server side digest simpler.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1037
        private final HttpTestAuthenticator auth;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1038
        private final byte[] nonce;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1039
        private final String ns;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1040
        public HttpDigestFilter(String key, HttpTestAuthenticator auth, HttpAuthType authType) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1041
            super(authType, type(key, authType));
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1042
            this.auth = auth;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1043
            nonce = new byte[16];
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1044
            new Random(Instant.now().toEpochMilli()).nextBytes(nonce);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1045
            ns = new BigInteger(1, nonce).toString(16);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1046
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1047
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1048
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1049
        protected void requestAuthentication(HttpTestExchange he)
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56089
diff changeset
  1050
                throws IOException {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56089
diff changeset
  1051
            String separator;
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56089
diff changeset
  1052
            Version v = he.getExchangeVersion();
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56089
diff changeset
  1053
            if (v == Version.HTTP_1_1) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56089
diff changeset
  1054
                separator = "\r\n    ";
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56089
diff changeset
  1055
            } else if (v == Version.HTTP_2) {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56089
diff changeset
  1056
                separator = " ";
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56089
diff changeset
  1057
            } else {
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56089
diff changeset
  1058
                throw new InternalError(String.valueOf(v));
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56089
diff changeset
  1059
            }
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1060
            he.getResponseHeaders().addHeader(getAuthenticate(),
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1061
                 "Digest realm=\"" + auth.getRealm() + "\","
56104
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56089
diff changeset
  1062
                 + separator + "qop=\"auth\","
3420c1bdd254 http-client-branch: (HttpClient) JDK-fixed 8196962
prappo
parents: 56089
diff changeset
  1063
                 + separator + "nonce=\"" + ns +"\"");
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1064
            System.out.println(type + ": Requesting Digest Authentication "
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1065
                 + he.getResponseHeaders()
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1066
                    .firstValue(getAuthenticate())
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1067
                    .orElse("null"));
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1068
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1069
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1070
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1071
        protected boolean isAuthentified(HttpTestExchange he) {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1072
            if (he.getRequestHeaders().containsKey(getAuthorization())) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1073
                List<String> authorization = he.getRequestHeaders().get(getAuthorization());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1074
                for (String a : authorization) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1075
                    System.out.println(type + ": processing " + a);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1076
                    int sp = a.indexOf(' ');
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1077
                    if (sp < 0) return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1078
                    String scheme = a.substring(0, sp);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1079
                    if (!"Digest".equalsIgnoreCase(scheme)) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1080
                        System.out.println(type + ": Unsupported scheme '" + scheme +"'");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1081
                        return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1082
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1083
                    if (a.length() <= sp+1) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1084
                        System.out.println(type + ": value too short for '" + scheme +"'");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1085
                        return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1086
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1087
                    a = a.substring(sp+1);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1088
                    DigestResponse dgr = DigestResponse.create(a);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1089
                    return validate(he.getRequestURI(), he.getRequestMethod(), dgr);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1090
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1091
                return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1092
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1093
            return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1094
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1095
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1096
        boolean validate(URI uri, String reqMethod, DigestResponse dg) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1097
            if (!"MD5".equalsIgnoreCase(dg.getAlgorithm("MD5"))) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1098
                System.out.println(type + ": Unsupported algorithm "
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1099
                                   + dg.algorithm);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1100
                return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1101
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1102
            if (!"auth".equalsIgnoreCase(dg.getQoP("auth"))) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1103
                System.out.println(type + ": Unsupported qop "
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1104
                                   + dg.qop);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1105
                return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1106
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1107
            try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1108
                if (!dg.nonce.equals(ns)) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1109
                    System.out.println(type + ": bad nonce returned by client: "
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1110
                                    + nonce + " expected " + ns);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1111
                    return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1112
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1113
                if (dg.response == null) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1114
                    System.out.println(type + ": missing digest response.");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1115
                    return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1116
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1117
                char[] pa = auth.getPassword(dg.username);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1118
                return verify(uri, reqMethod, dg, pa);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1119
            } catch(IllegalArgumentException | SecurityException
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1120
                    | NoSuchAlgorithmException e) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1121
                System.out.println(type + ": " + e.getMessage());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1122
                return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1123
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1124
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1125
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1126
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1127
        boolean verify(URI uri, String reqMethod, DigestResponse dg, char[] pw)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1128
            throws NoSuchAlgorithmException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1129
            String response = DigestResponse.computeDigest(true, reqMethod, pw, dg);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1130
            if (!dg.response.equals(response)) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1131
                System.out.println(type + ": bad response returned by client: "
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1132
                                    + dg.response + " expected " + response);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1133
                return false;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1134
            } else {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1135
                // A real server would also verify the uri=<request-uri>
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1136
                // parameter - but this is just a test...
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1137
                System.out.println(type + ": verified response " + response);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1138
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1139
            return true;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1140
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1141
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1142
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1143
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1144
        public String description() {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1145
            return "Filter for DIGEST authentication: " + type;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1146
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1147
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1148
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1149
    // Abstract HTTP handler class.
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1150
    private abstract static class AbstractHttpHandler implements HttpTestHandler {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1151
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1152
        final HttpAuthType authType;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1153
        final String type;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1154
        public AbstractHttpHandler(HttpAuthType authType, String type) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1155
            this.authType = authType;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1156
            this.type = type;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1157
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1158
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1159
        String getLocation() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1160
            return "Location";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1161
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1162
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1163
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1164
        public void handle(HttpTestExchange he) throws IOException {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1165
            try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1166
                sendResponse(he);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1167
            } catch (RuntimeException | Error | IOException t) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1168
               System.err.println(type
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1169
                    + ": Unexpected exception while handling request: " + t);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1170
               t.printStackTrace(System.err);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1171
               throw t;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1172
            } finally {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1173
                he.close();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1174
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1175
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1176
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1177
        protected abstract void sendResponse(HttpTestExchange he) throws IOException;
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1178
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1179
    }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1180
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1181
    static String stype(String type, String key, HttpAuthType authType, boolean tunnelled) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1182
        type = type + (authType == HttpAuthType.SERVER
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1183
                       ? " Server" : " Proxy")
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1184
                + (tunnelled ? " Tunnelled" : "");
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1185
        return "["+type+"]:"+key;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1186
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1187
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1188
    private class HttpNoAuthHandler extends AbstractHttpHandler {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1189
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1190
        // true if this server is behind a proxy tunnel.
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1191
        final boolean tunnelled;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1192
        public HttpNoAuthHandler(String key, HttpAuthType authType, boolean tunnelled) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1193
            super(authType, stype("NoAuth", key, authType, tunnelled));
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1194
            this.tunnelled = tunnelled;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1195
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1196
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1197
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1198
        protected void sendResponse(HttpTestExchange he) throws IOException {
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1199
            if (DEBUG) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1200
                System.out.println(type + ": headers are: "
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1201
                        + DigestEchoServer.toString(he.getRequestHeaders()));
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1202
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1203
            if (authType == HttpAuthType.SERVER && tunnelled) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1204
                // Verify that the client doesn't send us proxy-* headers
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1205
                // used to establish the proxy tunnel
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1206
                Optional<String> proxyAuth = he.getRequestHeaders()
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1207
                        .keySet().stream()
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1208
                        .filter("proxy-authorization"::equalsIgnoreCase)
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1209
                        .findAny();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1210
                if (proxyAuth.isPresent()) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1211
                    System.out.println(type + " found "
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1212
                            + proxyAuth.get() + ": failing!");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1213
                    throw new IOException(proxyAuth.get()
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1214
                            + " found by " + type + " for "
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1215
                            + he.getRequestURI());
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1216
                }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1217
            }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1218
            DigestEchoServer.this.writeResponse(he);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1219
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1220
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1221
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1222
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1223
    // A dummy HTTP Handler that redirects all incoming requests
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1224
    // by sending a back 3xx response code (301, 305, 307 etc..)
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1225
    private class Http3xxHandler extends AbstractHttpHandler {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1226
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1227
        private final URL redirectTargetURL;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1228
        private final int code3XX;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1229
        public Http3xxHandler(String key, URL proxyURL, HttpAuthType authType, int code300) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1230
            super(authType, stype("Server" + code300, key, authType, false));
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1231
            this.redirectTargetURL = proxyURL;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1232
            this.code3XX = code300;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1233
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1234
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1235
        int get3XX() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1236
            return code3XX;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1237
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1238
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1239
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1240
        public void sendResponse(HttpTestExchange he) throws IOException {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1241
            System.out.println(type + ": Got " + he.getRequestMethod()
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1242
                    + ": " + he.getRequestURI()
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1243
                    + "\n" + DigestEchoServer.toString(he.getRequestHeaders()));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1244
            System.out.println(type + ": Redirecting to "
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1245
                               + (authType == HttpAuthType.PROXY305
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1246
                                    ? "proxy" : "server"));
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1247
            he.getResponseHeaders().addHeader(getLocation(),
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1248
                redirectTargetURL.toExternalForm().toString());
56136
3b58e5bacad6 http-client-branch: Fixed chunked encoding in DigestEchoServer
dfuchs
parents: 56128
diff changeset
  1249
            he.sendResponseHeaders(get3XX(), -1);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1250
            System.out.println(type + ": Sent back " + get3XX() + " "
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1251
                 + getLocation() + ": " + redirectTargetURL.toExternalForm().toString());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1252
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1253
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1254
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1255
    static class Configurator extends HttpsConfigurator {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1256
        public Configurator(SSLContext ctx) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1257
            super(ctx);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1258
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1259
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1260
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1261
        public void configure (HttpsParameters params) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1262
            params.setSSLParameters (getSSLContext().getSupportedSSLParameters());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1263
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1264
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1265
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1266
    static final long start = System.nanoTime();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1267
    public static String now() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1268
        long now = System.nanoTime() - start;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1269
        long secs = now / 1000_000_000;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1270
        long mill = (now % 1000_000_000) / 1000_000;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1271
        long nan = now % 1000_000;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1272
        return String.format("[%d s, %d ms, %d ns] ", secs, mill, nan);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1273
    }
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1274
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1275
    static class  ProxyAuthorization {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1276
        final HttpAuthSchemeType schemeType;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1277
        final HttpTestAuthenticator authenticator;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1278
        private final byte[] nonce;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1279
        private final String ns;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1280
        private final String key;
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1281
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1282
        ProxyAuthorization(String key, HttpAuthSchemeType schemeType, HttpTestAuthenticator auth) {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1283
            this.key = key;
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1284
            this.schemeType = schemeType;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1285
            this.authenticator = auth;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1286
            nonce = new byte[16];
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1287
            new Random(Instant.now().toEpochMilli()).nextBytes(nonce);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1288
            ns = new BigInteger(1, nonce).toString(16);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1289
        }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1290
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1291
        String doBasic(Optional<String> authorization) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1292
            String offset = "proxy-authorization: basic ";
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1293
            String authstring = authorization.orElse("");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1294
            if (!authstring.toLowerCase(Locale.US).startsWith(offset)) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1295
                return "Proxy-Authenticate: BASIC " + "realm=\""
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1296
                        + authenticator.getRealm() +"\"";
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1297
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1298
            authstring = authstring
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1299
                    .substring(offset.length())
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1300
                    .trim();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1301
            byte[] base64 = Base64.getDecoder().decode(authstring);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1302
            String up = new String(base64, StandardCharsets.UTF_8);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1303
            int colon = up.indexOf(':');
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1304
            if (colon < 1) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1305
                return "Proxy-Authenticate: BASIC " + "realm=\""
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1306
                        + authenticator.getRealm() +"\"";
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1307
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1308
            String u = up.substring(0, colon);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1309
            String p = up.substring(colon+1);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1310
            char[] pw = authenticator.getPassword(u);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1311
            if (!p.equals(new String(pw))) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1312
                return "Proxy-Authenticate: BASIC " + "realm=\""
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1313
                        + authenticator.getRealm() +"\"";
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1314
            }
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1315
            System.out.println(now() + key + " Proxy basic authentication success");
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1316
            return null;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1317
        }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1318
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1319
        String doDigest(Optional<String> authorization) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1320
            String offset = "proxy-authorization: digest ";
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1321
            String authstring = authorization.orElse("");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1322
            if (!authstring.toLowerCase(Locale.US).startsWith(offset)) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1323
                return "Proxy-Authenticate: " +
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1324
                        "Digest realm=\"" + authenticator.getRealm() + "\","
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1325
                        + "\r\n    qop=\"auth\","
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1326
                        + "\r\n    nonce=\"" + ns +"\"";
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1327
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1328
            authstring = authstring
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1329
                    .substring(offset.length())
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1330
                    .trim();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1331
            boolean validated = false;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1332
            try {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1333
                DigestResponse dgr = DigestResponse.create(authstring);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1334
                validated = validate("CONNECT", dgr);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1335
            } catch (Throwable t) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1336
                t.printStackTrace();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1337
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1338
            if (!validated) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1339
                return "Proxy-Authenticate: " +
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1340
                        "Digest realm=\"" + authenticator.getRealm() + "\","
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1341
                        + "\r\n    qop=\"auth\","
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1342
                        + "\r\n    nonce=\"" + ns +"\"";
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1343
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1344
            return null;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1345
        }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1346
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1347
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1348
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1349
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1350
        boolean validate(String reqMethod, DigestResponse dg) {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1351
            String type = now() + this.getClass().getSimpleName() + ":" + key;
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1352
            if (!"MD5".equalsIgnoreCase(dg.getAlgorithm("MD5"))) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1353
                System.out.println(type + ": Unsupported algorithm "
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1354
                        + dg.algorithm);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1355
                return false;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1356
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1357
            if (!"auth".equalsIgnoreCase(dg.getQoP("auth"))) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1358
                System.out.println(type + ": Unsupported qop "
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1359
                        + dg.qop);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1360
                return false;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1361
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1362
            try {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1363
                if (!dg.nonce.equals(ns)) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1364
                    System.out.println(type + ": bad nonce returned by client: "
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1365
                            + nonce + " expected " + ns);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1366
                    return false;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1367
                }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1368
                if (dg.response == null) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1369
                    System.out.println(type + ": missing digest response.");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1370
                    return false;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1371
                }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1372
                char[] pa = authenticator.getPassword(dg.username);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1373
                return verify(type, reqMethod, dg, pa);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1374
            } catch(IllegalArgumentException | SecurityException
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1375
                    | NoSuchAlgorithmException e) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1376
                System.out.println(type + ": " + e.getMessage());
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1377
                return false;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1378
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1379
        }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1380
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1381
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1382
        boolean verify(String type, String reqMethod, DigestResponse dg, char[] pw)
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1383
                throws NoSuchAlgorithmException {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1384
            String response = DigestResponse.computeDigest(true, reqMethod, pw, dg);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1385
            if (!dg.response.equals(response)) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1386
                System.out.println(type + ": bad response returned by client: "
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1387
                        + dg.response + " expected " + response);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1388
                return false;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1389
            } else {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1390
                // A real server would also verify the uri=<request-uri>
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1391
                // parameter - but this is just a test...
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1392
                System.out.println(type + ": verified response " + response);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1393
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1394
            return true;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1395
        }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1396
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1397
        public boolean authorize(StringBuilder response, String requestLine, String headers) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1398
            String message = "<html><body><p>Authorization Failed%s</p></body></html>\r\n";
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1399
            if (authenticator == null && schemeType != HttpAuthSchemeType.NONE) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1400
                message = String.format(message, " No Authenticator Set");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1401
                response.append("HTTP/1.1 407 Proxy Authentication Failed\r\n");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1402
                response.append("Content-Length: ")
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1403
                        .append(message.getBytes(StandardCharsets.UTF_8).length)
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1404
                        .append("\r\n\r\n");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1405
                response.append(message);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1406
                return false;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1407
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1408
            Optional<String> authorization = Stream.of(headers.split("\r\n"))
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1409
                    .filter((k) -> k.toLowerCase(Locale.US).startsWith("proxy-authorization:"))
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1410
                    .findFirst();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1411
            String authenticate = null;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1412
            switch(schemeType) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1413
                case BASIC:
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1414
                case BASICSERVER:
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1415
                    authenticate = doBasic(authorization);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1416
                    break;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1417
                case DIGEST:
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1418
                    authenticate = doDigest(authorization);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1419
                    break;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1420
                case NONE:
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1421
                    response.append("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1422
                    return true;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1423
                default:
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1424
                    throw new InternalError("Unknown scheme type: " + schemeType);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1425
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1426
            if (authenticate != null) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1427
                message = String.format(message, "");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1428
                response.append("HTTP/1.1 407 Proxy Authentication Required\r\n");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1429
                response.append("Content-Length: ")
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1430
                        .append(message.getBytes(StandardCharsets.UTF_8).length)
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1431
                        .append("\r\n")
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1432
                        .append(authenticate)
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1433
                        .append("\r\n\r\n");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1434
                response.append(message);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1435
                return false;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1436
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1437
            response.append("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1438
            return true;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1439
        }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1440
    }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1441
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1442
    public interface TunnelingProxy {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1443
        InetSocketAddress getProxyAddress();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1444
        void stop();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1445
    }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1446
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1447
    // This is a bit hacky: HttpsProxyTunnel is an HTTPTestServer hidden
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1448
    // behind a fake proxy that only understands CONNECT requests.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1449
    // The fake proxy is just a server socket that intercept the
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1450
    // CONNECT and then redirect streams to the real server.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1451
    static class HttpsProxyTunnel extends DigestEchoServer
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1452
            implements Runnable, TunnelingProxy {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1453
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1454
        final ServerSocket ss;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1455
        final CopyOnWriteArrayList<CompletableFuture<Void>> connectionCFs
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1456
                = new CopyOnWriteArrayList<>();
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1457
        volatile ProxyAuthorization authorization;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1458
        volatile boolean stopped;
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1459
        public HttpsProxyTunnel(String key, HttpTestServer server, DigestEchoServer target,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1460
                                HttpTestHandler delegate)
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1461
                throws IOException {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1462
            this(key, server, target, delegate, ServerSocketFactory.create());
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1463
        }
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1464
        private HttpsProxyTunnel(String key, HttpTestServer server, DigestEchoServer target,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1465
                                HttpTestHandler delegate, ServerSocket ss)
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1466
                throws IOException {
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1467
            super("HttpsProxyTunnel:" + ss.getLocalPort() + ":" + key,
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1468
                    server, target, delegate);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1469
            System.out.flush();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1470
            System.err.println("WARNING: HttpsProxyTunnel is an experimental test class");
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1471
            this.ss = ss;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1472
            start();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1473
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1474
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1475
        final void start() throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1476
            Thread t = new Thread(this, "ProxyThread");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1477
            t.setDaemon(true);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1478
            t.start();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1479
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1480
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1481
        @Override
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1482
        public Version getServerVersion() {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1483
            // serverImpl is not null when this proxy
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1484
            // serves a single server. It will be null
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1485
            // if this proxy can serve multiple servers.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1486
            if (serverImpl != null) return serverImpl.getVersion();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1487
            return null;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1488
        }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1489
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1490
        @Override
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1491
        public void stop() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1492
            stopped = true;
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1493
            if (serverImpl != null) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1494
                serverImpl.stop();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1495
            }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1496
            if (redirect != null) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1497
                redirect.stop();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1498
            }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1499
            try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1500
                ss.close();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1501
            } catch (IOException ex) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1502
                if (DEBUG) ex.printStackTrace(System.out);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1503
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1504
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1505
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1506
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1507
        @Override
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1508
        void configureAuthentication(HttpTestContext ctxt,
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1509
                                     HttpAuthSchemeType schemeType,
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1510
                                     HttpTestAuthenticator auth,
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1511
                                     HttpAuthType authType) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1512
            if (authType == HttpAuthType.PROXY || authType == HttpAuthType.PROXY305) {
56070
66a9c3185028 http-client-branch: add tests with an HTTP/2 server for custom basic and digest authentication
dfuchs
parents: 56054
diff changeset
  1513
                authorization = new ProxyAuthorization(key, schemeType, auth);
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1514
            } else {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1515
                super.configureAuthentication(ctxt, schemeType, auth, authType);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1516
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1517
        }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1518
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1519
        boolean authorize(StringBuilder response, String requestLine, String headers) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1520
            if (authorization != null) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1521
                return authorization.authorize(response, requestLine, headers);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1522
            }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1523
            response.append("HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n");
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1524
            return true;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1525
        }
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1526
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1527
        // Pipe the input stream to the output stream.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1528
        private synchronized Thread pipe(InputStream is, OutputStream os, char tag, CompletableFuture<Void> end) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1529
            return new Thread("TunnelPipe("+tag+")") {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1530
                @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1531
                public void run() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1532
                    try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1533
                        try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1534
                            int c;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1535
                            while ((c = is.read()) != -1) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1536
                                os.write(c);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1537
                                os.flush();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1538
                                // if DEBUG prints a + or a - for each transferred
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1539
                                // character.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1540
                                if (DEBUG) System.out.print(tag);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1541
                            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1542
                            is.close();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1543
                        } finally {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1544
                            os.close();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1545
                        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1546
                    } catch (IOException ex) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1547
                        if (DEBUG) ex.printStackTrace(System.out);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1548
                    } finally {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1549
                        end.complete(null);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1550
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1551
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1552
            };
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1553
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1554
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1555
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1556
        public InetSocketAddress getAddress() {
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1557
            return new InetSocketAddress("127.0.0.1",
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1558
                    ss.getLocalPort());
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1559
        }
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1560
        @Override
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1561
        public InetSocketAddress getProxyAddress() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1562
            return getAddress();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1563
        }
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1564
        @Override
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1565
        public InetSocketAddress getServerAddress() {
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1566
            // serverImpl can be null if this proxy can serve
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1567
            // multiple servers.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1568
            if (serverImpl != null) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1569
                return serverImpl.getAddress();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1570
            }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1571
            return null;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1572
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1573
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1574
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1575
        // This is a bit shaky. It doesn't handle continuation
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1576
        // lines, but our client shouldn't send any.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1577
        // Read a line from the input stream, swallowing the final
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1578
        // \r\n sequence. Stops at the first \n, doesn't complain
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1579
        // if it wasn't preceded by '\r'.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1580
        //
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1581
        String readLine(InputStream r) throws IOException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1582
            StringBuilder b = new StringBuilder();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1583
            int c;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1584
            while ((c = r.read()) != -1) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1585
                if (c == '\n') break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1586
                b.appendCodePoint(c);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1587
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1588
            if (b.codePointAt(b.length() -1) == '\r') {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1589
                b.delete(b.length() -1, b.length());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1590
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1591
            return b.toString();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1592
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1593
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1594
        @Override
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1595
        public void run() {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1596
            Socket clientConnection = null;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1597
            try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1598
                while (!stopped) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1599
                    System.out.println(now() + "Tunnel: Waiting for client");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1600
                    Socket toClose;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1601
                    try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1602
                        toClose = clientConnection = ss.accept();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1603
                    } catch (IOException io) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1604
                        if (DEBUG || !stopped) io.printStackTrace(System.out);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1605
                        break;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1606
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1607
                    System.out.println(now() + "Tunnel: Client accepted");
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1608
                    StringBuilder headers = new StringBuilder();
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1609
                    Socket targetConnection = null;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1610
                    InputStream  ccis = clientConnection.getInputStream();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1611
                    OutputStream ccos = clientConnection.getOutputStream();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1612
                    Writer w = new OutputStreamWriter(
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1613
                                   clientConnection.getOutputStream(), "UTF-8");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1614
                    PrintWriter pw = new PrintWriter(w);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1615
                    System.out.println(now() + "Tunnel: Reading request line");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1616
                    String requestLine = readLine(ccis);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1617
                    System.out.println(now() + "Tunnel: Request line: " + requestLine);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1618
                    if (requestLine.startsWith("CONNECT ")) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1619
                        // We should probably check that the next word following
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1620
                        // CONNECT is the host:port of our HTTPS serverImpl.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1621
                        // Some improvement for a followup!
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1622
                        StringTokenizer tokenizer = new StringTokenizer(requestLine);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1623
                        String connect = tokenizer.nextToken();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1624
                        assert connect.equalsIgnoreCase("connect");
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1625
                        String hostport = tokenizer.nextToken();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1626
                        InetSocketAddress targetAddress;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1627
                        try {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1628
                            URI uri = new URI("https", hostport, "/", null, null);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1629
                            int port = uri.getPort();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1630
                            port = port == -1 ? 443 : port;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1631
                            targetAddress = new InetSocketAddress(uri.getHost(), port);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1632
                            if (serverImpl != null) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1633
                                assert targetAddress.getHostString()
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1634
                                        .equalsIgnoreCase(serverImpl.getAddress().getHostString());
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1635
                                assert targetAddress.getPort() == serverImpl.getAddress().getPort();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1636
                            }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1637
                        } catch (Throwable x) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1638
                            System.err.printf("Bad target address: \"%s\" in \"%s\"%n",
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1639
                                    hostport, requestLine);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1640
                            toClose.close();
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1641
                            continue;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1642
                        }
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1643
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1644
                        // Read all headers until we find the empty line that
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1645
                        // signals the end of all headers.
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1646
                        String line = requestLine;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1647
                        while(!line.equals("")) {
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1648
                            System.out.println(now() + "Tunnel: Reading header: "
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1649
                                               + (line = readLine(ccis)));
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1650
                            headers.append(line).append("\r\n");
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1651
                        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1652
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1653
                        StringBuilder response = new StringBuilder();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1654
                        final boolean authorize = authorize(response, requestLine, headers.toString());
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1655
                        if (!authorize) {
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1656
                            System.out.println(now() + "Tunnel: Sending "
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1657
                                    + response);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1658
                            // send the 407 response
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1659
                            pw.print(response.toString());
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1660
                            pw.flush();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1661
                            toClose.close();
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1662
                            continue;
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1663
                        }
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1664
                        System.out.println(now()
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1665
                                + "Tunnel connecting to target server at "
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1666
                                + targetAddress.getAddress() + ":" + targetAddress.getPort());
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1667
                        targetConnection = new Socket(
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1668
                                targetAddress.getAddress(),
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1669
                                targetAddress.getPort());
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1670
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1671
                        // Then send the 200 OK response to the client
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1672
                        System.out.println(now() + "Tunnel: Sending "
56041
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1673
                                           + response);
b4b5e09ef3cc http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents: 56033
diff changeset
  1674
                        pw.print(response);
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1675
                        pw.flush();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1676
                    } else {
56054
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1677
                        // This should not happen. If it does then just print an
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1678
                        // error - both on out and err, and close the accepted
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1679
                        // socket
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1680
                        System.out.println("WARNING: Tunnel: Unexpected status line: "
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1681
                                + requestLine + " received by "
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1682
                                + ss.getLocalSocketAddress()
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1683
                                + " from "
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1684
                                + toClose.getRemoteSocketAddress()
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1685
                                + " - closing accepted socket");
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1686
                        // Print on err
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1687
                        System.err.println("WARNING: Tunnel: Unexpected status line: "
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1688
                                             + requestLine + " received by "
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1689
                                           + ss.getLocalSocketAddress()
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1690
                                           + " from "
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1691
                                           + toClose.getRemoteSocketAddress());
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1692
                        // close accepted socket.
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1693
                        toClose.close();
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1694
                        System.err.println("Tunnel: accepted socket closed.");
352e845ae744 http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents: 56041
diff changeset
  1695
                        continue;
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1696
                    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1697
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1698
                    // Pipe the input stream of the client connection to the
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1699
                    // output stream of the target connection and conversely.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1700
                    // Now the client and target will just talk to each other.
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1701
                    System.out.println(now() + "Tunnel: Starting tunnel pipes");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1702
                    CompletableFuture<Void> end, end1, end2;
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1703
                    Thread t1 = pipe(ccis, targetConnection.getOutputStream(), '+',
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1704
                            end1 = new CompletableFuture<>());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1705
                    Thread t2 = pipe(targetConnection.getInputStream(), ccos, '-',
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1706
                            end2 = new CompletableFuture<>());
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1707
                    end = CompletableFuture.allOf(end1, end2);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1708
                    end.whenComplete(
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1709
                            (r,t) -> {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1710
                                try { toClose.close(); } catch (IOException x) { }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1711
                                finally {connectionCFs.remove(end);}
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1712
                            });
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1713
                    connectionCFs.add(end);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1714
                    t1.start();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1715
                    t2.start();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1716
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1717
            } catch (Throwable ex) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1718
                try {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1719
                    ss.close();
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1720
                } catch (IOException ex1) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1721
                    ex.addSuppressed(ex1);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1722
                }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1723
                ex.printStackTrace(System.err);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1724
            } finally {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1725
                System.out.println(now() + "Tunnel: exiting (stopped=" + stopped + ")");
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1726
                connectionCFs.forEach(cf -> cf.complete(null));
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1727
            }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1728
        }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1729
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1730
56128
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1731
    /**
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1732
     * Creates a TunnelingProxy that can serve multiple servers.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1733
     * The server address is extracted from the CONNECT request line.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1734
     * @param authScheme The authentication scheme supported by the proxy.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1735
     *                   Typically one of DIGEST, BASIC, NONE.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1736
     * @return A new TunnelingProxy able to serve multiple servers.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1737
     * @throws IOException If the proxy could not be created.
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1738
     */
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1739
    public static TunnelingProxy createHttpsProxyTunnel(HttpAuthSchemeType authScheme)
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1740
            throws IOException {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1741
        HttpsProxyTunnel result = new HttpsProxyTunnel("", null, null, null);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1742
        if (authScheme != HttpAuthSchemeType.NONE) {
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1743
            result.configureAuthentication(null,
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1744
                                           authScheme,
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1745
                                           AUTHENTICATOR,
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1746
                                           HttpAuthType.PROXY);
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1747
        }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1748
        return result;
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1749
    }
249a863b0aca http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents: 56104
diff changeset
  1750
56033
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1751
    private static String protocol(String protocol) {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1752
        if ("http".equalsIgnoreCase(protocol)) return "http";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1753
        else if ("https".equalsIgnoreCase(protocol)) return "https";
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1754
        else throw new InternalError("Unsupported protocol: " + protocol);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1755
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1756
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1757
    public static URL url(String protocol, InetSocketAddress address,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1758
                          String path) throws MalformedURLException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1759
        return new URL(protocol(protocol),
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1760
                address.getHostString(),
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1761
                address.getPort(), path);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1762
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1763
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1764
    public static URI uri(String protocol, InetSocketAddress address,
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1765
                          String path) throws URISyntaxException {
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1766
        return new URI(protocol(protocol) + "://" +
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1767
                address.getHostString() + ":" +
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1768
                address.getPort() + path);
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1769
    }
db102c5ca88a http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
diff changeset
  1770
}