src/java.net.http/share/classes/jdk/internal/net/http/common/Utils.java
author chegar
Wed, 02 May 2018 02:36:17 -0700
changeset 49944 4690a2871b44
parent 49765 ee6f7a61f3a5
child 50681 4254bed3c09d
child 56507 2294c51eae30
permissions -rw-r--r--
8202423: Small HTTP Client refresh Reviewed-by: chegar, dfuchs, michaelm, prappo Contributed-by: Chris Hegarty <chris.hegarty@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Michael McMahon <michael.x.mcmahon@oracle.com>, Pavel Rappo <pavel.rappo@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     1
/*
48703
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
     2
 * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     4
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    10
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    15
 * accompanied this code).
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    16
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    20
 *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    23
 * questions.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    24
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    25
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    26
package jdk.internal.net.http.common;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    27
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    28
import sun.net.NetProperties;
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43730
diff changeset
    29
import sun.net.util.IPAddressUtil;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    30
import sun.net.www.HeaderParser;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    31
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    32
import javax.net.ssl.ExtendedSSLSession;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    33
import javax.net.ssl.SSLParameters;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    34
import javax.net.ssl.SSLSession;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    35
import java.io.ByteArrayOutputStream;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    36
import java.io.Closeable;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    37
import java.io.IOException;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    38
import java.io.PrintStream;
43730
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
    39
import java.io.UncheckedIOException;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    40
import java.io.UnsupportedEncodingException;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    41
import java.lang.System.Logger.Level;
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43730
diff changeset
    42
import java.net.InetSocketAddress;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    43
import java.net.URI;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    44
import java.net.URLPermission;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    45
import java.net.http.HttpHeaders;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    46
import java.nio.ByteBuffer;
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
    47
import java.nio.CharBuffer;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
    48
import java.nio.charset.CharacterCodingException;
43730
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
    49
import java.nio.charset.Charset;
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
    50
import java.nio.charset.CodingErrorAction;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    51
import java.nio.charset.StandardCharsets;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    52
import java.security.AccessController;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    53
import java.security.PrivilegedAction;
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
    54
import java.text.Normalizer;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    55
import java.util.Arrays;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    56
import java.util.Collection;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    57
import java.util.Collections;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    58
import java.util.List;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    59
import java.util.Set;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    60
import java.util.TreeSet;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    61
import java.util.concurrent.CompletionException;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    62
import java.util.concurrent.ExecutionException;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    63
import java.util.function.BiPredicate;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    64
import java.util.function.Predicate;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    65
import java.util.function.Supplier;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    66
import java.util.stream.Collectors;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    67
import java.util.stream.Stream;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    68
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    69
import static java.lang.String.format;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    70
import static java.util.Objects.requireNonNull;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    71
import static java.util.stream.Collectors.joining;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    72
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    73
/**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    74
 * Miscellaneous utilities
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    75
 */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    76
public final class Utils {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
    77
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    78
    public static final boolean ASSERTIONSENABLED;
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    79
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    80
    static {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    81
        boolean enabled = false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    82
        assert enabled = true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    83
        ASSERTIONSENABLED = enabled;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    84
    }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    85
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    86
//    public static final boolean TESTING;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    87
//    static {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    88
//        if (ASSERTIONSENABLED) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    89
//            PrivilegedAction<String> action = () -> System.getProperty("test.src");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    90
//            TESTING = AccessController.doPrivileged(action) != null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    91
//        } else TESTING = false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    92
//    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    93
    public static final boolean DEBUG = // Revisit: temporary dev flag.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    94
            getBooleanProperty(DebugLogger.HTTP_NAME, false);
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    95
    public static final boolean DEBUG_WS = // Revisit: temporary dev flag.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
    96
            getBooleanProperty(DebugLogger.WS_NAME, false);
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    97
    public static final boolean DEBUG_HPACK = // Revisit: temporary dev flag.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    98
            getBooleanProperty(DebugLogger.HPACK_NAME, false);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
    99
    public static final boolean TESTING = DEBUG;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   100
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   101
    public static final boolean isHostnameVerificationDisabled = // enabled by default
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   102
            hostnameVerificationDisabledValue();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   103
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   104
    private static boolean hostnameVerificationDisabledValue() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   105
        String prop = getProperty("jdk.internal.httpclient.disableHostnameVerification");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   106
        if (prop == null)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   107
            return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   108
        return prop.isEmpty() ? true : Boolean.parseBoolean(prop);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   109
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   110
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   111
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   112
     * Allocated buffer size. Must never be higher than 16K. But can be lower
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   113
     * if smaller allocation units preferred. HTTP/2 mandates that all
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   114
     * implementations support frame payloads of at least 16K.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   115
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   116
    private static final int DEFAULT_BUFSIZE = 16 * 1024;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   117
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   118
    public static final int BUFSIZE = getIntegerNetProperty(
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   119
            "jdk.httpclient.bufsize", DEFAULT_BUFSIZE
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   120
    );
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   121
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   122
    private static final Set<String> DISALLOWED_HEADERS_SET;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   123
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   124
    static {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   125
        // A case insensitive TreeSet of strings.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   126
        TreeSet<String> treeSet = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   127
        treeSet.addAll(Set.of("connection", "content-length",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   128
                "date", "expect", "from", "host", "origin",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   129
                "referer", "upgrade",
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   130
                "via", "warning"));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   131
        DISALLOWED_HEADERS_SET = Collections.unmodifiableSet(treeSet);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   132
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   133
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   134
    public static final Predicate<String>
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   135
            ALLOWED_HEADERS = header -> !DISALLOWED_HEADERS_SET.contains(header);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   136
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   137
    public static final BiPredicate<String, List<String>> VALIDATE_USER_HEADER =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   138
            (name, lv) -> {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   139
                requireNonNull(name, "header name");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   140
                requireNonNull(lv, "header values");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   141
                if (!isValidName(name)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   142
                    throw newIAE("invalid header name: \"%s\"", name);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   143
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   144
                if (!Utils.ALLOWED_HEADERS.test(name)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   145
                    throw newIAE("restricted header name: \"%s\"", name);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   146
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   147
                for (String value : lv) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   148
                    requireNonNull(value, "header value");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   149
                    if (!isValidValue(value)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   150
                        throw newIAE("invalid header value for %s: \"%s\"", name, value);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   151
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   152
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   153
                return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   154
            };
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   155
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   156
    private static final Predicate<String> IS_PROXY_HEADER = (k) ->
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   157
            k != null && k.length() > 6 && "proxy-".equalsIgnoreCase(k.substring(0,6));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   158
    private static final Predicate<String> NO_PROXY_HEADER =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   159
            IS_PROXY_HEADER.negate();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   160
    private static final Predicate<String> ALL_HEADERS = (s) -> true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   161
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   162
    private static final Set<String> PROXY_AUTH_DISABLED_SCHEMES;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   163
    private static final Set<String> PROXY_AUTH_TUNNEL_DISABLED_SCHEMES;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   164
    static {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   165
        String proxyAuthDisabled =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   166
                getNetProperty("jdk.http.auth.proxying.disabledSchemes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   167
        String proxyAuthTunnelDisabled =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   168
                getNetProperty("jdk.http.auth.tunneling.disabledSchemes");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   169
        PROXY_AUTH_DISABLED_SCHEMES =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   170
                proxyAuthDisabled == null ? Set.of() :
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   171
                        Stream.of(proxyAuthDisabled.split(","))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   172
                                .map(String::trim)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   173
                                .filter((s) -> !s.isEmpty())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   174
                                .collect(Collectors.toUnmodifiableSet());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   175
        PROXY_AUTH_TUNNEL_DISABLED_SCHEMES =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   176
                proxyAuthTunnelDisabled == null ? Set.of() :
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   177
                        Stream.of(proxyAuthTunnelDisabled.split(","))
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   178
                                .map(String::trim)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   179
                                .filter((s) -> !s.isEmpty())
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   180
                                .collect(Collectors.toUnmodifiableSet());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   181
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   182
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   183
    private static final String WSPACES = " \t\r\n";
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   184
    private static final boolean isAllowedForProxy(String name,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   185
                                                   List<String> value,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   186
                                                   Set<String> disabledSchemes,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   187
                                                   Predicate<String> allowedKeys) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   188
        if (!allowedKeys.test(name)) return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   189
        if (disabledSchemes.isEmpty()) return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   190
        if (name.equalsIgnoreCase("proxy-authorization")) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   191
            if (value.isEmpty()) return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   192
            for (String scheme : disabledSchemes) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   193
                int slen = scheme.length();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   194
                for (String v : value) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   195
                    int vlen = v.length();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   196
                    if (vlen == slen) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   197
                        if (v.equalsIgnoreCase(scheme)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   198
                            return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   199
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   200
                    } else if (vlen > slen) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   201
                        if (v.substring(0,slen).equalsIgnoreCase(scheme)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   202
                            int c = v.codePointAt(slen);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   203
                            if (WSPACES.indexOf(c) > -1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   204
                                    || Character.isSpaceChar(c)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   205
                                    || Character.isWhitespace(c)) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   206
                                return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   207
                            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   208
                        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   209
                    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   210
                }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   211
            }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   212
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   213
        return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   214
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   215
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   216
    public static final BiPredicate<String, List<String>> PROXY_TUNNEL_FILTER =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   217
            (s,v) -> isAllowedForProxy(s, v, PROXY_AUTH_TUNNEL_DISABLED_SCHEMES,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   218
                    IS_PROXY_HEADER);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   219
    public static final BiPredicate<String, List<String>> PROXY_FILTER =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   220
            (s,v) -> isAllowedForProxy(s, v, PROXY_AUTH_DISABLED_SCHEMES,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   221
                    ALL_HEADERS);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   222
    public static final BiPredicate<String, List<String>> NO_PROXY_HEADERS_FILTER =
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   223
            (n,v) -> Utils.NO_PROXY_HEADER.test(n);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   224
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   225
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   226
    public static boolean proxyHasDisabledSchemes(boolean tunnel) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   227
        return tunnel ? ! PROXY_AUTH_TUNNEL_DISABLED_SCHEMES.isEmpty()
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   228
                      : ! PROXY_AUTH_DISABLED_SCHEMES.isEmpty();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   229
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   230
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   231
    public static IllegalArgumentException newIAE(String message, Object... args) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   232
        return new IllegalArgumentException(format(message, args));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   233
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   234
    public static ByteBuffer getBuffer() {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   235
        return ByteBuffer.allocate(BUFSIZE);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   236
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   237
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   238
    public static Throwable getCompletionCause(Throwable x) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   239
        if (!(x instanceof CompletionException)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   240
                && !(x instanceof ExecutionException)) return x;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   241
        final Throwable cause = x.getCause();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   242
        if (cause == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   243
            throw new InternalError("Unexpected null cause", x);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   244
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   245
        return cause;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   246
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   247
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   248
    public static IOException getIOException(Throwable t) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   249
        if (t instanceof IOException) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   250
            return (IOException) t;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   251
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   252
        Throwable cause = t.getCause();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   253
        if (cause != null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   254
            return getIOException(cause);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   255
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   256
        return new IOException(t);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   257
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   258
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   259
    private Utils() { }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   260
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   261
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   262
     * Returns the security permissions required to connect to the proxy, or
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   263
     * {@code null} if none is required or applicable.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   264
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   265
    public static URLPermission permissionForProxy(InetSocketAddress proxyAddress) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   266
        if (proxyAddress == null)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   267
            return null;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   268
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   269
        StringBuilder sb = new StringBuilder();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   270
        sb.append("socket://")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   271
          .append(proxyAddress.getHostString()).append(":")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   272
          .append(proxyAddress.getPort());
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   273
        String urlString = sb.toString();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   274
        return new URLPermission(urlString, "CONNECT");
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   275
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   276
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   277
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   278
     * Returns the security permission required for the given details.
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   279
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   280
    public static URLPermission permissionForServer(URI uri,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   281
                                                    String method,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   282
                                                    Stream<String> headers) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   283
        String urlString = new StringBuilder()
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   284
                .append(uri.getScheme()).append("://")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   285
                .append(uri.getAuthority())
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   286
                .append(uri.getPath()).toString();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   287
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   288
        StringBuilder actionStringBuilder = new StringBuilder(method);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   289
        String collected = headers.collect(joining(","));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   290
        if (!collected.isEmpty()) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   291
            actionStringBuilder.append(":").append(collected);
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   292
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   293
        return new URLPermission(urlString, actionStringBuilder.toString());
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   294
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   295
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   296
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   297
    // ABNF primitives defined in RFC 7230
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   298
    private static final boolean[] tchar      = new boolean[256];
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   299
    private static final boolean[] fieldvchar = new boolean[256];
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   300
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   301
    static {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   302
        char[] allowedTokenChars =
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   303
                ("!#$%&'*+-.^_`|~0123456789" +
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   304
                 "abcdefghijklmnopqrstuvwxyz" +
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   305
                 "ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   306
        for (char c : allowedTokenChars) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   307
            tchar[c] = true;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   308
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   309
        for (char c = 0x21; c < 0xFF; c++) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   310
            fieldvchar[c] = true;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   311
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   312
        fieldvchar[0x7F] = false; // a little hole (DEL) in the range
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   313
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   314
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   315
    /*
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   316
     * Validates a RFC 7230 field-name.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   317
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   318
    public static boolean isValidName(String token) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   319
        for (int i = 0; i < token.length(); i++) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   320
            char c = token.charAt(i);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   321
            if (c > 255 || !tchar[c]) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   322
                return false;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   323
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   324
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   325
        return !token.isEmpty();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   326
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   327
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   328
    public static class ServerName {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   329
        ServerName(String name, boolean isLiteral) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   330
            this.name = name;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   331
            this.isLiteral = isLiteral;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   332
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   333
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   334
        final String name;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   335
        final boolean isLiteral;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   336
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   337
        public String getName() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   338
            return name;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   339
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   340
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   341
        public boolean isLiteral() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   342
            return isLiteral;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   343
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   344
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   345
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43730
diff changeset
   346
    /**
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   347
     * Analyse the given address and determine if it is literal or not,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   348
     * returning the address in String form.
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43730
diff changeset
   349
     */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   350
    public static ServerName getServerName(InetSocketAddress addr) {
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43730
diff changeset
   351
        String host = addr.getHostString();
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   352
        byte[] literal = IPAddressUtil.textToNumericFormatV4(host);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   353
        if (literal == null) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   354
            // not IPv4 literal. Check IPv6
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   355
            literal = IPAddressUtil.textToNumericFormatV6(host);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   356
            return new ServerName(host, literal != null);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   357
        } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   358
            return new ServerName(host, true);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   359
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   360
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   361
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   362
    private static boolean isLoopbackLiteral(byte[] bytes) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   363
        if (bytes.length == 4) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   364
            return bytes[0] == 127;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   365
        } else if (bytes.length == 16) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   366
            for (int i=0; i<14; i++)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   367
                if (bytes[i] != 0)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   368
                    return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   369
            if (bytes[15] != 1)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   370
                return false;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   371
            return true;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   372
        } else
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   373
            throw new InternalError();
45713
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43730
diff changeset
   374
    }
ee3f2cbfe23a 8182589: TLS SNI in new Java 9 client is not available
michaelm
parents: 43730
diff changeset
   375
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   376
    /*
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   377
     * Validates a RFC 7230 field-value.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   378
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   379
     * "Obsolete line folding" rule
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   380
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   381
     *     obs-fold = CRLF 1*( SP / HTAB )
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   382
     *
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   383
     * is not permitted!
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   384
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   385
    public static boolean isValidValue(String token) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   386
        for (int i = 0; i < token.length(); i++) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   387
            char c = token.charAt(i);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   388
            if (c > 255) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   389
                return false;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   390
            }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   391
            if (c == ' ' || c == '\t') {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   392
                continue;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   393
            } else if (!fieldvchar[c]) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   394
                return false; // forbidden byte
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   395
            }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   396
        }
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   397
        return true;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   398
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   399
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   400
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   401
    public static int getIntegerNetProperty(String name, int defaultValue) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   402
        return AccessController.doPrivileged((PrivilegedAction<Integer>) () ->
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   403
                NetProperties.getInteger(name, defaultValue));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   404
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   405
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   406
    public static String getNetProperty(String name) {
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   407
        return AccessController.doPrivileged((PrivilegedAction<String>) () ->
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   408
                NetProperties.get(name));
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   409
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   410
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   411
    public static boolean getBooleanProperty(String name, boolean def) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   412
        return AccessController.doPrivileged((PrivilegedAction<Boolean>) () ->
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   413
                Boolean.parseBoolean(System.getProperty(name, String.valueOf(def))));
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   414
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   415
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   416
    public static String getProperty(String name) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   417
        return AccessController.doPrivileged((PrivilegedAction<String>) () ->
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   418
                System.getProperty(name));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   419
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   420
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   421
    public static int getIntegerProperty(String name, int defaultValue) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   422
        return AccessController.doPrivileged((PrivilegedAction<Integer>) () ->
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   423
                Integer.parseInt(System.getProperty(name, String.valueOf(defaultValue))));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   424
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   425
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   426
    public static SSLParameters copySSLParameters(SSLParameters p) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   427
        SSLParameters p1 = new SSLParameters();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   428
        p1.setAlgorithmConstraints(p.getAlgorithmConstraints());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   429
        p1.setCipherSuites(p.getCipherSuites());
43730
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   430
        // JDK 8 EXCL START
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   431
        p1.setEnableRetransmissions(p.getEnableRetransmissions());
43730
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   432
        p1.setMaximumPacketSize(p.getMaximumPacketSize());
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   433
        // JDK 8 EXCL END
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   434
        p1.setEndpointIdentificationAlgorithm(p.getEndpointIdentificationAlgorithm());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   435
        p1.setNeedClientAuth(p.getNeedClientAuth());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   436
        String[] protocols = p.getProtocols();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   437
        if (protocols != null) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   438
            p1.setProtocols(protocols.clone());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   439
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   440
        p1.setSNIMatchers(p.getSNIMatchers());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   441
        p1.setServerNames(p.getServerNames());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   442
        p1.setUseCipherSuitesOrder(p.getUseCipherSuitesOrder());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   443
        p1.setWantClientAuth(p.getWantClientAuth());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   444
        return p1;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   445
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   446
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   447
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   448
     * Set limit to position, and position to mark.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   449
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   450
    public static void flipToMark(ByteBuffer buffer, int mark) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   451
        buffer.limit(buffer.position());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   452
        buffer.position(mark);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   453
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   454
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   455
    public static String stackTrace(Throwable t) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   456
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   457
        String s = null;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   458
        try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   459
            PrintStream p = new PrintStream(bos, true, "US-ASCII");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   460
            t.printStackTrace(p);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   461
            s = bos.toString("US-ASCII");
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   462
        } catch (UnsupportedEncodingException ex) {
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   463
            throw new InternalError(ex); // Can't happen
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   464
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   465
        return s;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   466
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   467
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   468
    /**
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   469
     * Copies as much of src to dst as possible.
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   470
     * Return number of bytes copied
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   471
     */
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   472
    public static int copy(ByteBuffer src, ByteBuffer dst) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   473
        int srcLen = src.remaining();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   474
        int dstLen = dst.remaining();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   475
        if (srcLen > dstLen) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   476
            int diff = srcLen - dstLen;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   477
            int limit = src.limit();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   478
            src.limit(limit - diff);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   479
            dst.put(src);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   480
            src.limit(limit);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   481
        } else {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   482
            dst.put(src);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   483
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   484
        return srcLen - src.remaining();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   485
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   486
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   487
    /** Threshold beyond which data is no longer copied into the current
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   488
     * buffer, if that buffer has enough unused space. */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   489
    private static final int COPY_THRESHOLD = 8192;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   490
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   491
    /**
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   492
     * Adds the data from buffersToAdd to currentList. Either 1) appends the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   493
     * data from a particular buffer to the last buffer in the list ( if
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   494
     * there is enough unused space ), or 2) adds it to the list.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   495
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   496
     * @return the number of bytes added
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   497
     */
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   498
    public static long accumulateBuffers(List<ByteBuffer> currentList,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   499
                                         List<ByteBuffer> buffersToAdd) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   500
        long accumulatedBytes = 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   501
        for (ByteBuffer bufferToAdd : buffersToAdd) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   502
            int remaining = bufferToAdd.remaining();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   503
            if (remaining <= 0)
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   504
                continue;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   505
            int listSize = currentList.size();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   506
            if (listSize == 0) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   507
                currentList.add(bufferToAdd);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   508
                accumulatedBytes = remaining;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   509
                continue;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   510
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   511
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   512
            ByteBuffer lastBuffer = currentList.get(listSize - 1);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   513
            int freeSpace = lastBuffer.capacity() - lastBuffer.limit();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   514
            if (remaining <= COPY_THRESHOLD && freeSpace >= remaining) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   515
                // append the new data to the unused space in the last buffer
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   516
                int position = lastBuffer.position();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   517
                int limit = lastBuffer.limit();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   518
                lastBuffer.position(limit);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   519
                lastBuffer.limit(limit + remaining);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   520
                lastBuffer.put(bufferToAdd);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   521
                lastBuffer.position(position);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   522
            } else {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   523
                currentList.add(bufferToAdd);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   524
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   525
            accumulatedBytes += remaining;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   526
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   527
        return accumulatedBytes;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   528
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   529
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   530
    public static ByteBuffer copy(ByteBuffer src) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   531
        ByteBuffer dst = ByteBuffer.allocate(src.remaining());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   532
        dst.put(src);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   533
        dst.flip();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   534
        return dst;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   535
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   536
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   537
    public static String dump(Object... objects) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   538
        return Arrays.toString(objects);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   539
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   540
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   541
    public static String stringOf(Collection<?> source) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   542
        // We don't know anything about toString implementation of this
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   543
        // collection, so let's create an array
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   544
        return Arrays.toString(source.toArray());
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   545
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   546
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   547
    public static long remaining(ByteBuffer[] bufs) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   548
        long remain = 0;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   549
        for (ByteBuffer buf : bufs) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   550
            remain += buf.remaining();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   551
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   552
        return remain;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   553
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   554
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   555
    public static boolean hasRemaining(List<ByteBuffer> bufs) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   556
        synchronized (bufs) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   557
            for (ByteBuffer buf : bufs) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   558
                if (buf.hasRemaining())
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   559
                    return true;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   560
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   561
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   562
        return false;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   563
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   564
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   565
    public static long remaining(List<ByteBuffer> bufs) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   566
        long remain = 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   567
        synchronized (bufs) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   568
            for (ByteBuffer buf : bufs) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   569
                remain += buf.remaining();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   570
            }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   571
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   572
        return remain;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   573
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   574
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   575
    public static int remaining(List<ByteBuffer> bufs, int max) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   576
        long remain = 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   577
        synchronized (bufs) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   578
            for (ByteBuffer buf : bufs) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   579
                remain += buf.remaining();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   580
                if (remain > max) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   581
                    throw new IllegalArgumentException("too many bytes");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   582
                }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   583
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   584
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   585
        return (int) remain;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   586
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   587
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   588
    public static int remaining(ByteBuffer[] refs, int max) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   589
        long remain = 0;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   590
        for (ByteBuffer b : refs) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   591
            remain += b.remaining();
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   592
            if (remain > max) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   593
                throw new IllegalArgumentException("too many bytes");
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   594
            }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   595
        }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   596
        return (int) remain;
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   597
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   598
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   599
    public static void close(Closeable... closeables) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   600
        for (Closeable c : closeables) {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   601
            try {
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   602
                c.close();
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   603
            } catch (IOException ignored) { }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   604
        }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   605
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   606
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   607
    // Put all these static 'empty' singletons here
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   608
    public static final ByteBuffer EMPTY_BYTEBUFFER = ByteBuffer.allocate(0);
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   609
    public static final ByteBuffer[] EMPTY_BB_ARRAY = new ByteBuffer[0];
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   610
    public static final List<ByteBuffer> EMPTY_BB_LIST = List.of();
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   611
48703
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   612
    /**
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   613
     * Returns a slice of size {@code amount} from the given buffer. If the
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   614
     * buffer contains more data than {@code amount}, then the slice's capacity
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   615
     * ( and, but not just, its limit ) is set to {@code amount}. If the buffer
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   616
     * does not contain more data than {@code amount}, then the slice's capacity
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   617
     * will be the same as the given buffer's capacity.
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   618
     */
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   619
    public static ByteBuffer sliceWithLimitedCapacity(ByteBuffer buffer, int amount) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   620
        final int index = buffer.position() + amount;
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   621
        final int limit = buffer.limit();
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   622
        if (index != limit) {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   623
            // additional data in the buffer
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   624
            buffer.limit(index);  // ensures that the slice does not go beyond
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   625
        } else {
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   626
            // no additional data in the buffer
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   627
            buffer.limit(buffer.capacity());  // allows the slice full capacity
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   628
        }
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   629
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   630
        ByteBuffer newb = buffer.slice();
48703
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   631
        buffer.position(index);
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   632
        buffer.limit(limit);    // restore the original buffer's limit
3eae36c6baa5 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents: 48083
diff changeset
   633
        newb.limit(amount);     // slices limit to amount (capacity may be greater)
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   634
        return newb;
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   635
    }
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   636
43730
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   637
    /**
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   638
     * Get the Charset from the Content-encoding header. Defaults to
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   639
     * UTF_8
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   640
     */
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   641
    public static Charset charsetFrom(HttpHeaders headers) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   642
        String type = headers.firstValue("Content-type")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   643
                .orElse("text/html; charset=utf-8");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   644
        int i = type.indexOf(";");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   645
        if (i >= 0) type = type.substring(i+1);
43730
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   646
        try {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   647
            HeaderParser parser = new HeaderParser(type);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   648
            String value = parser.findValue("charset");
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   649
            if (value == null) return StandardCharsets.UTF_8;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   650
            return Charset.forName(value);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   651
        } catch (Throwable x) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   652
            Log.logTrace("Can't find charset in \"{0}\" ({1})", type, x);
43730
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   653
            return StandardCharsets.UTF_8;
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   654
        }
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   655
    }
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   656
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   657
    public static UncheckedIOException unchecked(IOException e) {
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   658
        return new UncheckedIOException(e);
b6847c320451 8174745: Httpclient source update for JDK 8
michaelm
parents: 42460
diff changeset
   659
    }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   660
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   661
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   662
     * Get a logger for debug HTTP traces.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   663
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   664
     * The logger should only be used with levels whose severity is
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   665
     * {@code <= DEBUG}. By default, this logger will forward all messages
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   666
     * logged to an internal logger named "jdk.internal.httpclient.debug".
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   667
     * In addition, if the property -Djdk.internal.httpclient.debug=true is set,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   668
     * it will print the messages on stderr.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   669
     * The logger will add some decoration to the printed message, in the form of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   670
     * {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   671
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   672
     * @param dbgTag A lambda that returns a string that identifies the caller
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   673
     *               (e.g: "SocketTube(3)", or "Http2Connection(SocketTube(3))")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   674
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   675
     * @return A logger for HTTP internal debug traces
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   676
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   677
    public static Logger getDebugLogger(Supplier<String> dbgTag) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   678
        return getDebugLogger(dbgTag, DEBUG);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   679
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   680
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   681
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   682
     * Get a logger for debug HTTP traces.The logger should only be used
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   683
     * with levels whose severity is {@code <= DEBUG}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   684
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   685
     * By default, this logger will forward all messages logged to an internal
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   686
     * logger named "jdk.internal.httpclient.debug".
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   687
     * In addition, if the message severity level is >= to
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   688
     * the provided {@code errLevel} it will print the messages on stderr.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   689
     * The logger will add some decoration to the printed message, in the form of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   690
     * {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   691
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   692
     * @apiNote To obtain a logger that will always print things on stderr in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   693
     *          addition to forwarding to the internal logger, use
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   694
     *          {@code getDebugLogger(this::dbgTag, Level.ALL);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   695
     *          This is also equivalent to calling
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   696
     *          {@code getDebugLogger(this::dbgTag, true);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   697
     *          To obtain a logger that will only forward to the internal logger,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   698
     *          use {@code getDebugLogger(this::dbgTag, Level.OFF);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   699
     *          This is also equivalent to calling
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   700
     *          {@code getDebugLogger(this::dbgTag, false);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   701
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   702
     * @param dbgTag A lambda that returns a string that identifies the caller
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   703
     *               (e.g: "SocketTube(3)", or "Http2Connection(SocketTube(3))")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   704
     * @param errLevel The level above which messages will be also printed on
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   705
     *               stderr (in addition to be forwarded to the internal logger).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   706
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   707
     * @return A logger for HTTP internal debug traces
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   708
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   709
    static Logger getDebugLogger(Supplier<String> dbgTag, Level errLevel) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   710
        return DebugLogger.createHttpLogger(dbgTag, Level.OFF, errLevel);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   711
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   712
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   713
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   714
     * Get a logger for debug HTTP traces.The logger should only be used
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   715
     * with levels whose severity is {@code <= DEBUG}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   716
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   717
     * By default, this logger will forward all messages logged to an internal
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   718
     * logger named "jdk.internal.httpclient.debug".
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   719
     * In addition, the provided boolean {@code on==true}, it will print the
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   720
     * messages on stderr.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   721
     * The logger will add some decoration to the printed message, in the form of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   722
     * {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   723
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   724
     * @apiNote To obtain a logger that will always print things on stderr in
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   725
     *          addition to forwarding to the internal logger, use
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   726
     *          {@code getDebugLogger(this::dbgTag, true);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   727
     *          This is also equivalent to calling
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   728
     *          {@code getDebugLogger(this::dbgTag, Level.ALL);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   729
     *          To obtain a logger that will only forward to the internal logger,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   730
     *          use {@code getDebugLogger(this::dbgTag, false);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   731
     *          This is also equivalent to calling
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   732
     *          {@code getDebugLogger(this::dbgTag, Level.OFF);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   733
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   734
     * @param dbgTag A lambda that returns a string that identifies the caller
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   735
     *               (e.g: "SocketTube(3)", or "Http2Connection(SocketTube(3))")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   736
     * @param on  Whether messages should also be printed on
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   737
     *               stderr (in addition to be forwarded to the internal logger).
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   738
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   739
     * @return A logger for HTTP internal debug traces
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   740
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   741
    public static Logger getDebugLogger(Supplier<String> dbgTag, boolean on) {
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   742
        Level errLevel = on ? Level.ALL : Level.OFF;
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   743
        return getDebugLogger(dbgTag, errLevel);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   744
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   745
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   746
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   747
     * Get a logger for debug HPACK traces.The logger should only be used
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   748
     * with levels whose severity is {@code <= DEBUG}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   749
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   750
     * By default, this logger will forward all messages logged to an internal
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   751
     * logger named "jdk.internal.httpclient.hpack.debug".
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   752
     * In addition, if the message severity level is >= to
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   753
     * the provided {@code errLevel} it will print the messages on stderr.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   754
     * The logger will add some decoration to the printed message, in the form of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   755
     * {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   756
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   757
     * @apiNote To obtain a logger that will always print things on stderr in
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   758
     *          addition to forwarding to the internal logger, use
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   759
     *          {@code getHpackLogger(this::dbgTag, Level.ALL);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   760
     *          This is also equivalent to calling
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   761
     *          {@code getHpackLogger(this::dbgTag, true);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   762
     *          To obtain a logger that will only forward to the internal logger,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   763
     *          use {@code getHpackLogger(this::dbgTag, Level.OFF);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   764
     *          This is also equivalent to calling
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   765
     *          {@code getHpackLogger(this::dbgTag, false);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   766
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   767
     * @param dbgTag A lambda that returns a string that identifies the caller
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   768
     *               (e.g: "Http2Connection(SocketTube(3))/hpack.Decoder(3)")
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   769
     * @param errLevel The level above which messages will be also printed on
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   770
     *               stderr (in addition to be forwarded to the internal logger).
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   771
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   772
     * @return A logger for HPACK internal debug traces
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   773
     */
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   774
    public static Logger getHpackLogger(Supplier<String> dbgTag, Level errLevel) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   775
        Level outLevel = Level.OFF;
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   776
        return DebugLogger.createHpackLogger(dbgTag, outLevel, errLevel);
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   777
    }
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   778
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   779
    /**
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   780
     * Get a logger for debug HPACK traces.The logger should only be used
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   781
     * with levels whose severity is {@code <= DEBUG}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   782
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   783
     * By default, this logger will forward all messages logged to an internal
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   784
     * logger named "jdk.internal.httpclient.hpack.debug".
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   785
     * In addition, the provided boolean {@code on==true}, it will print the
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   786
     * messages on stderr.
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   787
     * The logger will add some decoration to the printed message, in the form of
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   788
     * {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>}
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   789
     *
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   790
     * @apiNote To obtain a logger that will always print things on stderr in
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   791
     *          addition to forwarding to the internal logger, use
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   792
     *          {@code getHpackLogger(this::dbgTag, true);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   793
     *          This is also equivalent to calling
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   794
     *          {@code getHpackLogger(this::dbgTag, Level.ALL);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   795
     *          To obtain a logger that will only forward to the internal logger,
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   796
     *          use {@code getHpackLogger(this::dbgTag, false);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   797
     *          This is also equivalent to calling
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   798
     *          {@code getHpackLogger(this::dbgTag, Level.OFF);}.
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   799
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   800
     * @param dbgTag A lambda that returns a string that identifies the caller
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   801
     *               (e.g: "Http2Connection(SocketTube(3))/hpack.Decoder(3)")
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   802
     * @param on  Whether messages should also be printed on
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   803
     *            stderr (in addition to be forwarded to the internal logger).
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   804
     *
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   805
     * @return A logger for HPACK internal debug traces
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   806
     */
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   807
    public static Logger getHpackLogger(Supplier<String> dbgTag, boolean on) {
49765
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   808
        Level errLevel = on ? Level.ALL : Level.OFF;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   809
        return getHpackLogger(dbgTag, errLevel);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   810
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   811
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   812
    /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   813
     * Get a logger for debug WebSocket traces.The logger should only be used
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   814
     * with levels whose severity is {@code <= DEBUG}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   815
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   816
     * By default, this logger will forward all messages logged to an internal
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   817
     * logger named "jdk.internal.httpclient.websocket.debug".
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   818
     * In addition, if the message severity level is >= to
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   819
     * the provided {@code errLevel} it will print the messages on stderr.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   820
     * The logger will add some decoration to the printed message, in the form of
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   821
     * {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   822
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   823
     * @apiNote To obtain a logger that will always print things on stderr in
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   824
     *          addition to forwarding to the internal logger, use
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   825
     *          {@code getWebSocketLogger(this::dbgTag, Level.ALL);}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   826
     *          This is also equivalent to calling
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   827
     *          {@code getWSLogger(this::dbgTag, true);}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   828
     *          To obtain a logger that will only forward to the internal logger,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   829
     *          use {@code getWebSocketLogger(this::dbgTag, Level.OFF);}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   830
     *          This is also equivalent to calling
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   831
     *          {@code getWSLogger(this::dbgTag, false);}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   832
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   833
     * @param dbgTag A lambda that returns a string that identifies the caller
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   834
     *               (e.g: "WebSocket(3)")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   835
     * @param errLevel The level above which messages will be also printed on
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   836
     *               stderr (in addition to be forwarded to the internal logger).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   837
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   838
     * @return A logger for HPACK internal debug traces
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   839
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   840
    public static Logger getWebSocketLogger(Supplier<String> dbgTag, Level errLevel) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   841
        Level outLevel = Level.OFF;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   842
        return DebugLogger.createWebSocketLogger(dbgTag, outLevel, errLevel);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   843
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   844
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   845
    /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   846
     * Get a logger for debug WebSocket traces.The logger should only be used
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   847
     * with levels whose severity is {@code <= DEBUG}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   848
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   849
     * By default, this logger will forward all messages logged to an internal
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   850
     * logger named "jdk.internal.httpclient.websocket.debug".
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   851
     * In addition, the provided boolean {@code on==true}, it will print the
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   852
     * messages on stderr.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   853
     * The logger will add some decoration to the printed message, in the form of
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   854
     * {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>}
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   855
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   856
     * @apiNote To obtain a logger that will always print things on stderr in
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   857
     *          addition to forwarding to the internal logger, use
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   858
     *          {@code getWebSocketLogger(this::dbgTag, true);}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   859
     *          This is also equivalent to calling
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   860
     *          {@code getWebSocketLogger(this::dbgTag, Level.ALL);}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   861
     *          To obtain a logger that will only forward to the internal logger,
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   862
     *          use {@code getWebSocketLogger(this::dbgTag, false);}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   863
     *          This is also equivalent to calling
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   864
     *          {@code getHpackLogger(this::dbgTag, Level.OFF);}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   865
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   866
     * @param dbgTag A lambda that returns a string that identifies the caller
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   867
     *               (e.g: "WebSocket(3)")
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   868
     * @param on  Whether messages should also be printed on
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   869
     *            stderr (in addition to be forwarded to the internal logger).
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   870
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   871
     * @return A logger for WebSocket internal debug traces
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   872
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   873
    public static Logger getWebSocketLogger(Supplier<String> dbgTag, boolean on) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   874
        Level errLevel = on ? Level.ALL : Level.OFF;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   875
        return getWebSocketLogger(dbgTag, errLevel);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   876
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   877
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   878
    /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   879
     * SSLSessions returned to user are wrapped in an immutable object
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   880
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   881
    public static SSLSession immutableSession(SSLSession session) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   882
        if (session instanceof ExtendedSSLSession)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   883
            return new ImmutableExtendedSSLSession((ExtendedSSLSession)session);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   884
        else
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   885
            return new ImmutableSSLSession(session);
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   886
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   887
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   888
    /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   889
     * Enabled by default. May be disabled for testing. Use with care
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   890
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   891
    public static boolean isHostnameVerificationDisabled() {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   892
        return isHostnameVerificationDisabled;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   893
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   894
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   895
    public static InetSocketAddress resolveAddress(InetSocketAddress address) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   896
        if (address != null && address.isUnresolved()) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   897
            // The default proxy selector may select a proxy whose  address is
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   898
            // unresolved. We must resolve the address before connecting to it.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   899
            address = new InetSocketAddress(address.getHostString(), address.getPort());
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   900
        }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   901
        return address;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   902
    }
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   903
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   904
    /**
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   905
     * Returns the smallest (closest to zero) positive number {@code m} (which
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   906
     * is also a power of 2) such that {@code n <= m}.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   907
     * <pre>{@code
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   908
     *          n  pow2Size(n)
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   909
     * -----------------------
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   910
     *          0           1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   911
     *          1           1
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   912
     *          2           2
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   913
     *          3           4
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   914
     *          4           4
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   915
     *          5           8
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   916
     *          6           8
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   917
     *          7           8
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   918
     *          8           8
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   919
     *          9          16
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   920
     *         10          16
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   921
     *        ...         ...
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   922
     * 2147483647  1073741824
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   923
     * } </pre>
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   924
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   925
     * The result is capped at {@code 1 << 30} as beyond that int wraps.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   926
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   927
     * @param n
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   928
     *         capacity
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   929
     *
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   930
     * @return the size of the array
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   931
     * @apiNote Used to size arrays in circular buffers (rings), usually in
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   932
     * order to squeeze extra performance substituting {@code %} operation for
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   933
     * {@code &}, which is up to 2 times faster.
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   934
     */
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   935
    public static int pow2Size(int n) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   936
        if (n < 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   937
            throw new IllegalArgumentException();
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   938
        } else if (n == 0) {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   939
            return 1;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   940
        } else if (n >= (1 << 30)) { // 2^31 is a negative int
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   941
            return 1 << 30;
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   942
        } else {
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   943
            return 1 << (32 - Integer.numberOfLeadingZeros(n - 1));
ee6f7a61f3a5 8197564: HTTP Client implementation
chegar
parents: 48703
diff changeset
   944
        }
48083
b1c1b4ef4be2 8191494: Refresh incubating HTTP Client
chegar
parents: 47216
diff changeset
   945
    }
49944
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   946
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   947
    // -- toAsciiString-like support to encode path and query URI segments
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   948
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   949
    private static final char[] hexDigits = {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   950
            '0', '1', '2', '3', '4', '5', '6', '7',
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   951
            '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   952
    };
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   953
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   954
    private static void appendEscape(StringBuilder sb, byte b) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   955
        sb.append('%');
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   956
        sb.append(hexDigits[(b >> 4) & 0x0f]);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   957
        sb.append(hexDigits[(b >> 0) & 0x0f]);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   958
    }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   959
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   960
    // Encodes all characters >= \u0080 into escaped, normalized UTF-8 octets,
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   961
    // assuming that s is otherwise legal
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   962
    //
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   963
    public static String encode(String s) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   964
        int n = s.length();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   965
        if (n == 0)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   966
            return s;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   967
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   968
        // First check whether we actually need to encode
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   969
        for (int i = 0;;) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   970
            if (s.charAt(i) >= '\u0080')
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   971
                break;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   972
            if (++i >= n)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   973
                return s;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   974
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   975
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   976
        String ns = Normalizer.normalize(s, Normalizer.Form.NFC);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   977
        ByteBuffer bb = null;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   978
        try {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   979
            bb = StandardCharsets.UTF_8.newEncoder()
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   980
                    .onMalformedInput(CodingErrorAction.REPORT)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   981
                    .onUnmappableCharacter(CodingErrorAction.REPORT)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   982
                    .encode(CharBuffer.wrap(ns));
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   983
        } catch (CharacterCodingException x) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   984
            assert false : x;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   985
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   986
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   987
        StringBuilder sb = new StringBuilder();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   988
        while (bb.hasRemaining()) {
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   989
            int b = bb.get() & 0xff;
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   990
            if (b >= 0x80)
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   991
                appendEscape(sb, (byte)b);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   992
            else
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   993
                sb.append((char)b);
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   994
        }
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   995
        return sb.toString();
4690a2871b44 8202423: Small HTTP Client refresh
chegar
parents: 49765
diff changeset
   996
    }
42460
7133f144981a 8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff changeset
   997
}