author | jlaskey |
Thu, 14 Nov 2019 12:39:56 -0400 | |
branch | JDK-8193209-branch |
changeset 59087 | effb66aab08b |
parent 54579 | 270557b396eb |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
53256
bd8df96decba
8216498: Confusing and unneeded wrapping of SSLHandshakeException
dfuchs
parents:
52554
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, 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 | 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 | 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 | 32 |
import javax.net.ssl.ExtendedSSLSession; |
50681 | 33 |
import javax.net.ssl.SSLException; |
53256
bd8df96decba
8216498: Confusing and unneeded wrapping of SSLHandshakeException
dfuchs
parents:
52554
diff
changeset
|
34 |
import javax.net.ssl.SSLHandshakeException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import javax.net.ssl.SSLParameters; |
49765 | 36 |
import javax.net.ssl.SSLSession; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.io.ByteArrayOutputStream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.io.Closeable; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.io.IOException; |
48083 | 40 |
import java.io.PrintStream; |
43730 | 41 |
import java.io.UncheckedIOException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
import java.io.UnsupportedEncodingException; |
48083 | 43 |
import java.lang.System.Logger.Level; |
50681 | 44 |
import java.net.ConnectException; |
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
45 |
import java.net.InetSocketAddress; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
46 |
import java.net.URI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
import java.net.URLPermission; |
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
48 |
import java.net.http.HttpClient; |
49765 | 49 |
import java.net.http.HttpHeaders; |
50681 | 50 |
import java.net.http.HttpTimeoutException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
import java.nio.ByteBuffer; |
49944 | 52 |
import java.nio.CharBuffer; |
53 |
import java.nio.charset.CharacterCodingException; |
|
43730 | 54 |
import java.nio.charset.Charset; |
49944 | 55 |
import java.nio.charset.CodingErrorAction; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
import java.nio.charset.StandardCharsets; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
import java.security.AccessController; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
import java.security.PrivilegedAction; |
49944 | 59 |
import java.text.Normalizer; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
import java.util.Arrays; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
import java.util.Collection; |
49765 | 62 |
import java.util.Collections; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
import java.util.List; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
import java.util.Set; |
49765 | 65 |
import java.util.TreeSet; |
50681 | 66 |
import java.util.concurrent.CompletableFuture; |
48083 | 67 |
import java.util.concurrent.CompletionException; |
68 |
import java.util.concurrent.ExecutionException; |
|
49765 | 69 |
import java.util.function.BiPredicate; |
50681 | 70 |
import java.util.function.Function; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
import java.util.function.Predicate; |
48083 | 72 |
import java.util.function.Supplier; |
49765 | 73 |
import java.util.stream.Collectors; |
48083 | 74 |
import java.util.stream.Stream; |
75 |
||
49765 | 76 |
import static java.lang.String.format; |
48083 | 77 |
import static java.util.stream.Collectors.joining; |
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
78 |
import jdk.internal.net.http.HttpRequestImpl; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
81 |
* Miscellaneous utilities |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
82 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
83 |
public final class Utils { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
84 |
|
48083 | 85 |
public static final boolean ASSERTIONSENABLED; |
49765 | 86 |
|
48083 | 87 |
static { |
88 |
boolean enabled = false; |
|
89 |
assert enabled = true; |
|
90 |
ASSERTIONSENABLED = enabled; |
|
91 |
} |
|
49765 | 92 |
|
48083 | 93 |
// public static final boolean TESTING; |
94 |
// static { |
|
95 |
// if (ASSERTIONSENABLED) { |
|
96 |
// PrivilegedAction<String> action = () -> System.getProperty("test.src"); |
|
97 |
// TESTING = AccessController.doPrivileged(action) != null; |
|
98 |
// } else TESTING = false; |
|
99 |
// } |
|
100 |
public static final boolean DEBUG = // Revisit: temporary dev flag. |
|
101 |
getBooleanProperty(DebugLogger.HTTP_NAME, false); |
|
49765 | 102 |
public static final boolean DEBUG_WS = // Revisit: temporary dev flag. |
103 |
getBooleanProperty(DebugLogger.WS_NAME, false); |
|
48083 | 104 |
public static final boolean DEBUG_HPACK = // Revisit: temporary dev flag. |
105 |
getBooleanProperty(DebugLogger.HPACK_NAME, false); |
|
106 |
public static final boolean TESTING = DEBUG; |
|
107 |
||
49765 | 108 |
public static final boolean isHostnameVerificationDisabled = // enabled by default |
109 |
hostnameVerificationDisabledValue(); |
|
110 |
||
111 |
private static boolean hostnameVerificationDisabledValue() { |
|
112 |
String prop = getProperty("jdk.internal.httpclient.disableHostnameVerification"); |
|
113 |
if (prop == null) |
|
114 |
return false; |
|
115 |
return prop.isEmpty() ? true : Boolean.parseBoolean(prop); |
|
116 |
} |
|
117 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
118 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
119 |
* 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
|
120 |
* 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
|
121 |
* 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
|
122 |
*/ |
48083 | 123 |
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
|
124 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
125 |
public static final int BUFSIZE = getIntegerNetProperty( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
126 |
"jdk.httpclient.bufsize", DEFAULT_BUFSIZE |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
127 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
128 |
|
50681 | 129 |
public static final BiPredicate<String,String> ACCEPT_ALL = (x,y) -> true; |
130 |
||
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
131 |
private static final Set<String> DISALLOWED_HEADERS_SET = getDisallowedHeaders(); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
132 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
133 |
private static Set<String> getDisallowedHeaders() { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
134 |
Set<String> headers = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
135 |
headers.addAll(Set.of("connection", "content-length", "expect", "host", "upgrade")); |
49765 | 136 |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
137 |
String v = getNetProperty("jdk.httpclient.allowRestrictedHeaders"); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
138 |
if (v != null) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
139 |
// any headers found are removed from set. |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
140 |
String[] tokens = v.trim().split(","); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
141 |
for (String token : tokens) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
142 |
headers.remove(token); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
143 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
144 |
return Collections.unmodifiableSet(headers); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
145 |
} else { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
146 |
return Collections.unmodifiableSet(headers); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
147 |
} |
49765 | 148 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
149 |
|
50681 | 150 |
public static final BiPredicate<String, String> |
151 |
ALLOWED_HEADERS = (header, unused) -> !DISALLOWED_HEADERS_SET.contains(header); |
|
49765 | 152 |
|
50681 | 153 |
public static final BiPredicate<String, String> VALIDATE_USER_HEADER = |
154 |
(name, value) -> { |
|
155 |
assert name != null : "null header name"; |
|
156 |
assert value != null : "null header value"; |
|
49765 | 157 |
if (!isValidName(name)) { |
158 |
throw newIAE("invalid header name: \"%s\"", name); |
|
159 |
} |
|
50681 | 160 |
if (!Utils.ALLOWED_HEADERS.test(name, null)) { |
49765 | 161 |
throw newIAE("restricted header name: \"%s\"", name); |
162 |
} |
|
50681 | 163 |
if (!isValidValue(value)) { |
164 |
throw newIAE("invalid header value for %s: \"%s\"", name, value); |
|
49765 | 165 |
} |
166 |
return true; |
|
167 |
}; |
|
168 |
||
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
169 |
// Headers that are not generally restricted, and can therefore be set by users, |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
170 |
// but can in some contexts be overridden by the implementation. |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
171 |
// Currently, only contains "Authorization" which will |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
172 |
// be overridden, when an Authenticator is set on the HttpClient. |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
173 |
// Needs to be BiPred<String,String> to fit with general form of predicates |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
174 |
// used by caller. |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
175 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
176 |
public static final BiPredicate<String, String> CONTEXT_RESTRICTED(HttpClient client) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
177 |
return (k, v) -> client.authenticator() == null || |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
178 |
! (k.equalsIgnoreCase("Authorization") |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
179 |
&& k.equalsIgnoreCase("Proxy-Authorization")); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
180 |
} |
54579
270557b396eb
8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents:
53521
diff
changeset
|
181 |
private static final BiPredicate<String, String> HOST_RESTRICTED = (k,v) -> !"host".equalsIgnoreCase(k); |
270557b396eb
8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents:
53521
diff
changeset
|
182 |
public static final BiPredicate<String, String> PROXY_TUNNEL_RESTRICTED(HttpClient client) { |
270557b396eb
8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents:
53521
diff
changeset
|
183 |
return CONTEXT_RESTRICTED(client).and(HOST_RESTRICTED); |
270557b396eb
8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents:
53521
diff
changeset
|
184 |
} |
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
185 |
|
54579
270557b396eb
8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents:
53521
diff
changeset
|
186 |
private static final Predicate<String> IS_HOST = "host"::equalsIgnoreCase; |
49765 | 187 |
private static final Predicate<String> IS_PROXY_HEADER = (k) -> |
188 |
k != null && k.length() > 6 && "proxy-".equalsIgnoreCase(k.substring(0,6)); |
|
189 |
private static final Predicate<String> NO_PROXY_HEADER = |
|
190 |
IS_PROXY_HEADER.negate(); |
|
191 |
private static final Predicate<String> ALL_HEADERS = (s) -> true; |
|
192 |
||
193 |
private static final Set<String> PROXY_AUTH_DISABLED_SCHEMES; |
|
194 |
private static final Set<String> PROXY_AUTH_TUNNEL_DISABLED_SCHEMES; |
|
195 |
static { |
|
196 |
String proxyAuthDisabled = |
|
197 |
getNetProperty("jdk.http.auth.proxying.disabledSchemes"); |
|
198 |
String proxyAuthTunnelDisabled = |
|
199 |
getNetProperty("jdk.http.auth.tunneling.disabledSchemes"); |
|
200 |
PROXY_AUTH_DISABLED_SCHEMES = |
|
201 |
proxyAuthDisabled == null ? Set.of() : |
|
202 |
Stream.of(proxyAuthDisabled.split(",")) |
|
203 |
.map(String::trim) |
|
204 |
.filter((s) -> !s.isEmpty()) |
|
205 |
.collect(Collectors.toUnmodifiableSet()); |
|
206 |
PROXY_AUTH_TUNNEL_DISABLED_SCHEMES = |
|
207 |
proxyAuthTunnelDisabled == null ? Set.of() : |
|
208 |
Stream.of(proxyAuthTunnelDisabled.split(",")) |
|
209 |
.map(String::trim) |
|
210 |
.filter((s) -> !s.isEmpty()) |
|
211 |
.collect(Collectors.toUnmodifiableSet()); |
|
212 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
213 |
|
50681 | 214 |
public static <T> CompletableFuture<T> wrapForDebug(Logger logger, String name, CompletableFuture<T> cf) { |
215 |
if (logger.on()) { |
|
216 |
return cf.handle((r,t) -> { |
|
217 |
logger.log("%s completed %s", name, t == null ? "successfully" : t ); |
|
218 |
return cf; |
|
219 |
}).thenCompose(Function.identity()); |
|
220 |
} else { |
|
221 |
return cf; |
|
222 |
} |
|
223 |
} |
|
224 |
||
49765 | 225 |
private static final String WSPACES = " \t\r\n"; |
226 |
private static final boolean isAllowedForProxy(String name, |
|
50681 | 227 |
String value, |
49765 | 228 |
Set<String> disabledSchemes, |
229 |
Predicate<String> allowedKeys) { |
|
230 |
if (!allowedKeys.test(name)) return false; |
|
231 |
if (disabledSchemes.isEmpty()) return true; |
|
232 |
if (name.equalsIgnoreCase("proxy-authorization")) { |
|
233 |
if (value.isEmpty()) return false; |
|
234 |
for (String scheme : disabledSchemes) { |
|
235 |
int slen = scheme.length(); |
|
50681 | 236 |
int vlen = value.length(); |
237 |
if (vlen == slen) { |
|
238 |
if (value.equalsIgnoreCase(scheme)) { |
|
239 |
return false; |
|
240 |
} |
|
241 |
} else if (vlen > slen) { |
|
242 |
if (value.substring(0,slen).equalsIgnoreCase(scheme)) { |
|
243 |
int c = value.codePointAt(slen); |
|
244 |
if (WSPACES.indexOf(c) > -1 |
|
245 |
|| Character.isSpaceChar(c) |
|
246 |
|| Character.isWhitespace(c)) { |
|
49765 | 247 |
return false; |
248 |
} |
|
249 |
} |
|
250 |
} |
|
251 |
} |
|
252 |
} |
|
253 |
return true; |
|
254 |
} |
|
255 |
||
50681 | 256 |
public static final BiPredicate<String, String> PROXY_TUNNEL_FILTER = |
49765 | 257 |
(s,v) -> isAllowedForProxy(s, v, PROXY_AUTH_TUNNEL_DISABLED_SCHEMES, |
54579
270557b396eb
8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents:
53521
diff
changeset
|
258 |
// Allows Proxy-* and Host headers when establishing the tunnel. |
270557b396eb
8222527: HttpClient doesn't send HOST header when tunelling HTTP/1.1 through http proxy
dfuchs
parents:
53521
diff
changeset
|
259 |
IS_PROXY_HEADER.or(IS_HOST)); |
50681 | 260 |
public static final BiPredicate<String, String> PROXY_FILTER = |
49765 | 261 |
(s,v) -> isAllowedForProxy(s, v, PROXY_AUTH_DISABLED_SCHEMES, |
262 |
ALL_HEADERS); |
|
50681 | 263 |
public static final BiPredicate<String, String> NO_PROXY_HEADERS_FILTER = |
49765 | 264 |
(n,v) -> Utils.NO_PROXY_HEADER.test(n); |
265 |
||
266 |
||
267 |
public static boolean proxyHasDisabledSchemes(boolean tunnel) { |
|
268 |
return tunnel ? ! PROXY_AUTH_TUNNEL_DISABLED_SCHEMES.isEmpty() |
|
269 |
: ! PROXY_AUTH_DISABLED_SCHEMES.isEmpty(); |
|
270 |
} |
|
271 |
||
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
53256
diff
changeset
|
272 |
// WebSocket connection Upgrade headers |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
53256
diff
changeset
|
273 |
private static final String HEADER_CONNECTION = "Connection"; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
53256
diff
changeset
|
274 |
private static final String HEADER_UPGRADE = "Upgrade"; |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
53256
diff
changeset
|
275 |
|
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
53256
diff
changeset
|
276 |
public static final void setWebSocketUpgradeHeaders(HttpRequestImpl request) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
53256
diff
changeset
|
277 |
request.setSystemHeader(HEADER_UPGRADE, "websocket"); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
53256
diff
changeset
|
278 |
request.setSystemHeader(HEADER_CONNECTION, "Upgrade"); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
53256
diff
changeset
|
279 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
53256
diff
changeset
|
280 |
|
49765 | 281 |
public static IllegalArgumentException newIAE(String message, Object... args) { |
282 |
return new IllegalArgumentException(format(message, args)); |
|
283 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
284 |
public static ByteBuffer getBuffer() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
285 |
return ByteBuffer.allocate(BUFSIZE); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
286 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
287 |
|
48083 | 288 |
public static Throwable getCompletionCause(Throwable x) { |
289 |
if (!(x instanceof CompletionException) |
|
290 |
&& !(x instanceof ExecutionException)) return x; |
|
291 |
final Throwable cause = x.getCause(); |
|
49765 | 292 |
if (cause == null) { |
293 |
throw new InternalError("Unexpected null cause", x); |
|
294 |
} |
|
295 |
return cause; |
|
48083 | 296 |
} |
297 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
298 |
public static IOException getIOException(Throwable t) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
299 |
if (t instanceof IOException) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
300 |
return (IOException) t; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
301 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
Throwable cause = t.getCause(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
if (cause != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
return getIOException(cause); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
305 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
306 |
return new IOException(t); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
307 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
|
50681 | 309 |
/** |
310 |
* Adds a more specific exception detail message, based on the given |
|
311 |
* exception type and the message supplier. This is primarily to present |
|
312 |
* more descriptive messages in IOExceptions that may be visible to calling |
|
313 |
* code. |
|
314 |
* |
|
315 |
* @return a possibly new exception that has as its detail message, the |
|
316 |
* message from the messageSupplier, and the given throwable as its |
|
317 |
* cause. Otherwise returns the given throwable |
|
318 |
*/ |
|
319 |
public static Throwable wrapWithExtraDetail(Throwable t, |
|
320 |
Supplier<String> messageSupplier) { |
|
321 |
if (!(t instanceof IOException)) |
|
322 |
return t; |
|
323 |
||
53256
bd8df96decba
8216498: Confusing and unneeded wrapping of SSLHandshakeException
dfuchs
parents:
52554
diff
changeset
|
324 |
if (t instanceof SSLHandshakeException) |
bd8df96decba
8216498: Confusing and unneeded wrapping of SSLHandshakeException
dfuchs
parents:
52554
diff
changeset
|
325 |
return t; // no need to decorate |
bd8df96decba
8216498: Confusing and unneeded wrapping of SSLHandshakeException
dfuchs
parents:
52554
diff
changeset
|
326 |
|
50681 | 327 |
String msg = messageSupplier.get(); |
328 |
if (msg == null) |
|
329 |
return t; |
|
330 |
||
331 |
if (t instanceof ConnectionExpiredException) { |
|
53256
bd8df96decba
8216498: Confusing and unneeded wrapping of SSLHandshakeException
dfuchs
parents:
52554
diff
changeset
|
332 |
if (t.getCause() instanceof SSLHandshakeException) |
bd8df96decba
8216498: Confusing and unneeded wrapping of SSLHandshakeException
dfuchs
parents:
52554
diff
changeset
|
333 |
return t; // no need to decorate |
50681 | 334 |
IOException ioe = new IOException(msg, t.getCause()); |
335 |
t = new ConnectionExpiredException(ioe); |
|
336 |
} else { |
|
337 |
IOException ioe = new IOException(msg, t); |
|
338 |
t = ioe; |
|
339 |
} |
|
340 |
return t; |
|
341 |
} |
|
342 |
||
48083 | 343 |
private Utils() { } |
344 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
345 |
/** |
48083 | 346 |
* Returns the security permissions required to connect to the proxy, or |
347 |
* {@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
|
348 |
*/ |
48083 | 349 |
public static URLPermission permissionForProxy(InetSocketAddress proxyAddress) { |
350 |
if (proxyAddress == null) |
|
351 |
return null; |
|
352 |
||
353 |
StringBuilder sb = new StringBuilder(); |
|
354 |
sb.append("socket://") |
|
355 |
.append(proxyAddress.getHostString()).append(":") |
|
356 |
.append(proxyAddress.getPort()); |
|
357 |
String urlString = sb.toString(); |
|
358 |
return new URLPermission(urlString, "CONNECT"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
/** |
48083 | 362 |
* 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
|
363 |
*/ |
48083 | 364 |
public static URLPermission permissionForServer(URI uri, |
365 |
String method, |
|
366 |
Stream<String> headers) { |
|
367 |
String urlString = new StringBuilder() |
|
368 |
.append(uri.getScheme()).append("://") |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
369 |
.append(uri.getRawAuthority()) |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
370 |
.append(uri.getRawPath()).toString(); |
48083 | 371 |
|
372 |
StringBuilder actionStringBuilder = new StringBuilder(method); |
|
373 |
String collected = headers.collect(joining(",")); |
|
374 |
if (!collected.isEmpty()) { |
|
375 |
actionStringBuilder.append(":").append(collected); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
376 |
} |
48083 | 377 |
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
|
378 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
379 |
|
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 |
// ABNF primitives defined in RFC 7230 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
382 |
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
|
383 |
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
|
384 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
385 |
static { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
386 |
char[] allowedTokenChars = |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
387 |
("!#$%&'*+-.^_`|~0123456789" + |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
388 |
"abcdefghijklmnopqrstuvwxyz" + |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
389 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
390 |
for (char c : allowedTokenChars) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
391 |
tchar[c] = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
392 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
393 |
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
|
394 |
fieldvchar[c] = true; |
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 |
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
|
397 |
} |
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 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
400 |
* Validates a RFC 7230 field-name. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
401 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
402 |
public static boolean isValidName(String token) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
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
|
404 |
char c = token.charAt(i); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
405 |
if (c > 255 || !tchar[c]) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
406 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
407 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
408 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
409 |
return !token.isEmpty(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
410 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
411 |
|
49765 | 412 |
public static class ServerName { |
413 |
ServerName(String name, boolean isLiteral) { |
|
414 |
this.name = name; |
|
415 |
this.isLiteral = isLiteral; |
|
416 |
} |
|
417 |
||
418 |
final String name; |
|
419 |
final boolean isLiteral; |
|
420 |
||
421 |
public String getName() { |
|
422 |
return name; |
|
423 |
} |
|
424 |
||
425 |
public boolean isLiteral() { |
|
426 |
return isLiteral; |
|
427 |
} |
|
428 |
} |
|
429 |
||
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
430 |
/** |
49765 | 431 |
* Analyse the given address and determine if it is literal or not, |
432 |
* returning the address in String form. |
|
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
433 |
*/ |
49765 | 434 |
public static ServerName getServerName(InetSocketAddress addr) { |
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
435 |
String host = addr.getHostString(); |
49765 | 436 |
byte[] literal = IPAddressUtil.textToNumericFormatV4(host); |
437 |
if (literal == null) { |
|
438 |
// not IPv4 literal. Check IPv6 |
|
439 |
literal = IPAddressUtil.textToNumericFormatV6(host); |
|
440 |
return new ServerName(host, literal != null); |
|
441 |
} else { |
|
442 |
return new ServerName(host, true); |
|
443 |
} |
|
444 |
} |
|
445 |
||
446 |
private static boolean isLoopbackLiteral(byte[] bytes) { |
|
447 |
if (bytes.length == 4) { |
|
448 |
return bytes[0] == 127; |
|
449 |
} else if (bytes.length == 16) { |
|
450 |
for (int i=0; i<14; i++) |
|
451 |
if (bytes[i] != 0) |
|
452 |
return false; |
|
453 |
if (bytes[15] != 1) |
|
454 |
return false; |
|
455 |
return true; |
|
456 |
} else |
|
457 |
throw new InternalError(); |
|
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
458 |
} |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
459 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
460 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
461 |
* Validates a RFC 7230 field-value. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
462 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
463 |
* "Obsolete line folding" rule |
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 |
* obs-fold = CRLF 1*( SP / HTAB ) |
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 |
* is not permitted! |
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 |
public static boolean isValidValue(String token) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
470 |
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
|
471 |
char c = token.charAt(i); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
472 |
if (c > 255) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
473 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
474 |
} |
49765 | 475 |
if (c == ' ' || c == '\t') { |
476 |
continue; |
|
477 |
} else if (!fieldvchar[c]) { |
|
478 |
return false; // forbidden byte |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
479 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
480 |
} |
49765 | 481 |
return true; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
482 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
483 |
|
49765 | 484 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
485 |
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
|
486 |
return AccessController.doPrivileged((PrivilegedAction<Integer>) () -> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
487 |
NetProperties.getInteger(name, defaultValue)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
488 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
489 |
|
49765 | 490 |
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
|
491 |
return AccessController.doPrivileged((PrivilegedAction<String>) () -> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
492 |
NetProperties.get(name)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
493 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
494 |
|
49765 | 495 |
public static boolean getBooleanProperty(String name, boolean def) { |
48083 | 496 |
return AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> |
497 |
Boolean.parseBoolean(System.getProperty(name, String.valueOf(def)))); |
|
498 |
} |
|
499 |
||
49765 | 500 |
public static String getProperty(String name) { |
501 |
return AccessController.doPrivileged((PrivilegedAction<String>) () -> |
|
502 |
System.getProperty(name)); |
|
503 |
} |
|
504 |
||
505 |
public static int getIntegerProperty(String name, int defaultValue) { |
|
506 |
return AccessController.doPrivileged((PrivilegedAction<Integer>) () -> |
|
507 |
Integer.parseInt(System.getProperty(name, String.valueOf(defaultValue)))); |
|
508 |
} |
|
509 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
510 |
public static SSLParameters copySSLParameters(SSLParameters p) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
511 |
SSLParameters p1 = new SSLParameters(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
512 |
p1.setAlgorithmConstraints(p.getAlgorithmConstraints()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
513 |
p1.setCipherSuites(p.getCipherSuites()); |
43730 | 514 |
// JDK 8 EXCL START |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
515 |
p1.setEnableRetransmissions(p.getEnableRetransmissions()); |
43730 | 516 |
p1.setMaximumPacketSize(p.getMaximumPacketSize()); |
517 |
// JDK 8 EXCL END |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
518 |
p1.setEndpointIdentificationAlgorithm(p.getEndpointIdentificationAlgorithm()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
519 |
p1.setNeedClientAuth(p.getNeedClientAuth()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
520 |
String[] protocols = p.getProtocols(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
521 |
if (protocols != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
522 |
p1.setProtocols(protocols.clone()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
523 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
524 |
p1.setSNIMatchers(p.getSNIMatchers()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
525 |
p1.setServerNames(p.getServerNames()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
526 |
p1.setUseCipherSuitesOrder(p.getUseCipherSuitesOrder()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
527 |
p1.setWantClientAuth(p.getWantClientAuth()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
528 |
return p1; |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
531 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
532 |
* 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
|
533 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
534 |
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
|
535 |
buffer.limit(buffer.position()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
536 |
buffer.position(mark); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
537 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
538 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
539 |
public static String stackTrace(Throwable t) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
540 |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
541 |
String s = null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
542 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
543 |
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
|
544 |
t.printStackTrace(p); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
545 |
s = bos.toString("US-ASCII"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
546 |
} catch (UnsupportedEncodingException ex) { |
48083 | 547 |
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
|
548 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
549 |
return s; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
550 |
} |
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 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
553 |
* 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
|
554 |
* Return number of bytes copied |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
555 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
556 |
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
|
557 |
int srcLen = src.remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
558 |
int dstLen = dst.remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
559 |
if (srcLen > dstLen) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
560 |
int diff = srcLen - dstLen; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
561 |
int limit = src.limit(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
562 |
src.limit(limit - diff); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
563 |
dst.put(src); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
564 |
src.limit(limit); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
565 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
566 |
dst.put(src); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
567 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
568 |
return srcLen - src.remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
569 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
570 |
|
48083 | 571 |
/** Threshold beyond which data is no longer copied into the current |
572 |
* buffer, if that buffer has enough unused space. */ |
|
573 |
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
|
574 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
575 |
/** |
48083 | 576 |
* Adds the data from buffersToAdd to currentList. Either 1) appends the |
577 |
* data from a particular buffer to the last buffer in the list ( if |
|
578 |
* there is enough unused space ), or 2) adds it to the list. |
|
579 |
* |
|
580 |
* @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
|
581 |
*/ |
48083 | 582 |
public static long accumulateBuffers(List<ByteBuffer> currentList, |
583 |
List<ByteBuffer> buffersToAdd) { |
|
584 |
long accumulatedBytes = 0; |
|
585 |
for (ByteBuffer bufferToAdd : buffersToAdd) { |
|
586 |
int remaining = bufferToAdd.remaining(); |
|
587 |
if (remaining <= 0) |
|
588 |
continue; |
|
589 |
int listSize = currentList.size(); |
|
590 |
if (listSize == 0) { |
|
591 |
currentList.add(bufferToAdd); |
|
592 |
accumulatedBytes = remaining; |
|
593 |
continue; |
|
594 |
} |
|
595 |
||
596 |
ByteBuffer lastBuffer = currentList.get(listSize - 1); |
|
597 |
int freeSpace = lastBuffer.capacity() - lastBuffer.limit(); |
|
598 |
if (remaining <= COPY_THRESHOLD && freeSpace >= remaining) { |
|
599 |
// append the new data to the unused space in the last buffer |
|
600 |
int position = lastBuffer.position(); |
|
601 |
int limit = lastBuffer.limit(); |
|
602 |
lastBuffer.position(limit); |
|
603 |
lastBuffer.limit(limit + remaining); |
|
604 |
lastBuffer.put(bufferToAdd); |
|
605 |
lastBuffer.position(position); |
|
606 |
} else { |
|
607 |
currentList.add(bufferToAdd); |
|
608 |
} |
|
609 |
accumulatedBytes += remaining; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
610 |
} |
48083 | 611 |
return accumulatedBytes; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
612 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
613 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
614 |
public static ByteBuffer copy(ByteBuffer src) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
615 |
ByteBuffer dst = ByteBuffer.allocate(src.remaining()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
616 |
dst.put(src); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
617 |
dst.flip(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
618 |
return dst; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
619 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
620 |
|
50681 | 621 |
public static ByteBuffer copyAligned(ByteBuffer src) { |
622 |
int len = src.remaining(); |
|
623 |
int size = ((len + 7) >> 3) << 3; |
|
624 |
assert size >= len; |
|
625 |
ByteBuffer dst = ByteBuffer.allocate(size); |
|
626 |
dst.put(src); |
|
627 |
dst.flip(); |
|
628 |
return dst; |
|
629 |
} |
|
630 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
631 |
public static String dump(Object... objects) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
632 |
return Arrays.toString(objects); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
633 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
634 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
635 |
public static String stringOf(Collection<?> source) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
636 |
// 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
|
637 |
// 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
|
638 |
return Arrays.toString(source.toArray()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
639 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
640 |
|
48083 | 641 |
public static long remaining(ByteBuffer[] bufs) { |
642 |
long remain = 0; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
643 |
for (ByteBuffer buf : bufs) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
644 |
remain += buf.remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
645 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
646 |
return remain; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
647 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
648 |
|
48083 | 649 |
public static boolean hasRemaining(List<ByteBuffer> bufs) { |
650 |
synchronized (bufs) { |
|
651 |
for (ByteBuffer buf : bufs) { |
|
652 |
if (buf.hasRemaining()) |
|
653 |
return true; |
|
654 |
} |
|
655 |
} |
|
656 |
return false; |
|
657 |
} |
|
658 |
||
659 |
public static long remaining(List<ByteBuffer> bufs) { |
|
660 |
long remain = 0; |
|
661 |
synchronized (bufs) { |
|
662 |
for (ByteBuffer buf : bufs) { |
|
663 |
remain += buf.remaining(); |
|
664 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
665 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
666 |
return remain; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
667 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
668 |
|
48083 | 669 |
public static int remaining(List<ByteBuffer> bufs, int max) { |
670 |
long remain = 0; |
|
671 |
synchronized (bufs) { |
|
672 |
for (ByteBuffer buf : bufs) { |
|
673 |
remain += buf.remaining(); |
|
674 |
if (remain > max) { |
|
675 |
throw new IllegalArgumentException("too many bytes"); |
|
676 |
} |
|
677 |
} |
|
678 |
} |
|
679 |
return (int) remain; |
|
680 |
} |
|
681 |
||
682 |
public static int remaining(ByteBuffer[] refs, int max) { |
|
683 |
long remain = 0; |
|
684 |
for (ByteBuffer b : refs) { |
|
685 |
remain += b.remaining(); |
|
686 |
if (remain > max) { |
|
687 |
throw new IllegalArgumentException("too many bytes"); |
|
688 |
} |
|
689 |
} |
|
690 |
return (int) remain; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
691 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
692 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
693 |
public static void close(Closeable... closeables) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
694 |
for (Closeable c : closeables) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
695 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
696 |
c.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
697 |
} catch (IOException ignored) { } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
698 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
699 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
700 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
701 |
// 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
|
702 |
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
|
703 |
public static final ByteBuffer[] EMPTY_BB_ARRAY = new ByteBuffer[0]; |
48083 | 704 |
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
|
705 |
|
48703
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48083
diff
changeset
|
706 |
/** |
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48083
diff
changeset
|
707 |
* 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
|
708 |
* 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
|
709 |
* ( 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
|
710 |
* 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
|
711 |
* 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
|
712 |
*/ |
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48083
diff
changeset
|
713 |
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
|
714 |
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
|
715 |
final int limit = buffer.limit(); |
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48083
diff
changeset
|
716 |
if (index != limit) { |
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48083
diff
changeset
|
717 |
// additional data in the buffer |
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48083
diff
changeset
|
718 |
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
|
719 |
} else { |
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48083
diff
changeset
|
720 |
// 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
|
721 |
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
|
722 |
} |
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48083
diff
changeset
|
723 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
724 |
ByteBuffer newb = buffer.slice(); |
48703
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48083
diff
changeset
|
725 |
buffer.position(index); |
3eae36c6baa5
8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
48083
diff
changeset
|
726 |
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
|
727 |
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
|
728 |
return newb; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
729 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
730 |
|
43730 | 731 |
/** |
732 |
* Get the Charset from the Content-encoding header. Defaults to |
|
733 |
* UTF_8 |
|
734 |
*/ |
|
735 |
public static Charset charsetFrom(HttpHeaders headers) { |
|
49765 | 736 |
String type = headers.firstValue("Content-type") |
737 |
.orElse("text/html; charset=utf-8"); |
|
738 |
int i = type.indexOf(";"); |
|
739 |
if (i >= 0) type = type.substring(i+1); |
|
43730 | 740 |
try { |
49765 | 741 |
HeaderParser parser = new HeaderParser(type); |
742 |
String value = parser.findValue("charset"); |
|
743 |
if (value == null) return StandardCharsets.UTF_8; |
|
744 |
return Charset.forName(value); |
|
745 |
} catch (Throwable x) { |
|
746 |
Log.logTrace("Can't find charset in \"{0}\" ({1})", type, x); |
|
43730 | 747 |
return StandardCharsets.UTF_8; |
748 |
} |
|
749 |
} |
|
750 |
||
751 |
public static UncheckedIOException unchecked(IOException e) { |
|
752 |
return new UncheckedIOException(e); |
|
753 |
} |
|
48083 | 754 |
|
755 |
/** |
|
756 |
* Get a logger for debug HTTP traces. |
|
757 |
* |
|
758 |
* The logger should only be used with levels whose severity is |
|
759 |
* {@code <= DEBUG}. By default, this logger will forward all messages |
|
760 |
* logged to an internal logger named "jdk.internal.httpclient.debug". |
|
761 |
* In addition, if the property -Djdk.internal.httpclient.debug=true is set, |
|
762 |
* it will print the messages on stderr. |
|
763 |
* The logger will add some decoration to the printed message, in the form of |
|
764 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
765 |
* |
|
766 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
767 |
* (e.g: "SocketTube(3)", or "Http2Connection(SocketTube(3))") |
|
768 |
* |
|
769 |
* @return A logger for HTTP internal debug traces |
|
770 |
*/ |
|
771 |
public static Logger getDebugLogger(Supplier<String> dbgTag) { |
|
772 |
return getDebugLogger(dbgTag, DEBUG); |
|
773 |
} |
|
774 |
||
775 |
/** |
|
776 |
* Get a logger for debug HTTP traces.The logger should only be used |
|
777 |
* with levels whose severity is {@code <= DEBUG}. |
|
778 |
* |
|
779 |
* By default, this logger will forward all messages logged to an internal |
|
780 |
* logger named "jdk.internal.httpclient.debug". |
|
781 |
* In addition, if the message severity level is >= to |
|
782 |
* the provided {@code errLevel} it will print the messages on stderr. |
|
783 |
* The logger will add some decoration to the printed message, in the form of |
|
784 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
785 |
* |
|
786 |
* @apiNote To obtain a logger that will always print things on stderr in |
|
787 |
* addition to forwarding to the internal logger, use |
|
788 |
* {@code getDebugLogger(this::dbgTag, Level.ALL);}. |
|
789 |
* This is also equivalent to calling |
|
790 |
* {@code getDebugLogger(this::dbgTag, true);}. |
|
791 |
* To obtain a logger that will only forward to the internal logger, |
|
792 |
* use {@code getDebugLogger(this::dbgTag, Level.OFF);}. |
|
793 |
* This is also equivalent to calling |
|
794 |
* {@code getDebugLogger(this::dbgTag, false);}. |
|
795 |
* |
|
796 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
797 |
* (e.g: "SocketTube(3)", or "Http2Connection(SocketTube(3))") |
|
798 |
* @param errLevel The level above which messages will be also printed on |
|
799 |
* stderr (in addition to be forwarded to the internal logger). |
|
800 |
* |
|
801 |
* @return A logger for HTTP internal debug traces |
|
802 |
*/ |
|
803 |
static Logger getDebugLogger(Supplier<String> dbgTag, Level errLevel) { |
|
804 |
return DebugLogger.createHttpLogger(dbgTag, Level.OFF, errLevel); |
|
805 |
} |
|
806 |
||
807 |
/** |
|
808 |
* Get a logger for debug HTTP traces.The logger should only be used |
|
809 |
* with levels whose severity is {@code <= DEBUG}. |
|
810 |
* |
|
811 |
* By default, this logger will forward all messages logged to an internal |
|
812 |
* logger named "jdk.internal.httpclient.debug". |
|
813 |
* In addition, the provided boolean {@code on==true}, it will print the |
|
814 |
* messages on stderr. |
|
815 |
* The logger will add some decoration to the printed message, in the form of |
|
816 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
817 |
* |
|
818 |
* @apiNote To obtain a logger that will always print things on stderr in |
|
819 |
* addition to forwarding to the internal logger, use |
|
820 |
* {@code getDebugLogger(this::dbgTag, true);}. |
|
821 |
* This is also equivalent to calling |
|
822 |
* {@code getDebugLogger(this::dbgTag, Level.ALL);}. |
|
823 |
* To obtain a logger that will only forward to the internal logger, |
|
824 |
* use {@code getDebugLogger(this::dbgTag, false);}. |
|
825 |
* This is also equivalent to calling |
|
826 |
* {@code getDebugLogger(this::dbgTag, Level.OFF);}. |
|
827 |
* |
|
828 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
829 |
* (e.g: "SocketTube(3)", or "Http2Connection(SocketTube(3))") |
|
830 |
* @param on Whether messages should also be printed on |
|
831 |
* stderr (in addition to be forwarded to the internal logger). |
|
832 |
* |
|
833 |
* @return A logger for HTTP internal debug traces |
|
834 |
*/ |
|
835 |
public static Logger getDebugLogger(Supplier<String> dbgTag, boolean on) { |
|
836 |
Level errLevel = on ? Level.ALL : Level.OFF; |
|
837 |
return getDebugLogger(dbgTag, errLevel); |
|
838 |
} |
|
839 |
||
840 |
/** |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
841 |
* Return the host string from a HttpRequestImpl |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
842 |
* |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
843 |
* @param request |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
844 |
* @return |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
845 |
*/ |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
846 |
public static String hostString(HttpRequestImpl request) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
847 |
URI uri = request.uri(); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
848 |
int port = uri.getPort(); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
849 |
String host = uri.getHost(); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
850 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
851 |
boolean defaultPort; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
852 |
if (port == -1) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
853 |
defaultPort = true; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
854 |
} else if (uri.getScheme().equalsIgnoreCase("https")) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
855 |
defaultPort = port == 443; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
856 |
} else { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
857 |
defaultPort = port == 80; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
858 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
859 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
860 |
if (defaultPort) { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
861 |
return host; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
862 |
} else { |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
863 |
return host + ":" + Integer.toString(port); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
864 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
865 |
} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
866 |
|
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52106
diff
changeset
|
867 |
/** |
48083 | 868 |
* Get a logger for debug HPACK traces.The logger should only be used |
869 |
* with levels whose severity is {@code <= DEBUG}. |
|
870 |
* |
|
871 |
* By default, this logger will forward all messages logged to an internal |
|
872 |
* logger named "jdk.internal.httpclient.hpack.debug". |
|
873 |
* In addition, if the message severity level is >= to |
|
49765 | 874 |
* the provided {@code errLevel} it will print the messages on stderr. |
48083 | 875 |
* The logger will add some decoration to the printed message, in the form of |
876 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
877 |
* |
|
49765 | 878 |
* @apiNote To obtain a logger that will always print things on stderr in |
48083 | 879 |
* addition to forwarding to the internal logger, use |
880 |
* {@code getHpackLogger(this::dbgTag, Level.ALL);}. |
|
881 |
* This is also equivalent to calling |
|
882 |
* {@code getHpackLogger(this::dbgTag, true);}. |
|
883 |
* To obtain a logger that will only forward to the internal logger, |
|
884 |
* use {@code getHpackLogger(this::dbgTag, Level.OFF);}. |
|
885 |
* This is also equivalent to calling |
|
886 |
* {@code getHpackLogger(this::dbgTag, false);}. |
|
887 |
* |
|
888 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
889 |
* (e.g: "Http2Connection(SocketTube(3))/hpack.Decoder(3)") |
|
49765 | 890 |
* @param errLevel The level above which messages will be also printed on |
891 |
* stderr (in addition to be forwarded to the internal logger). |
|
48083 | 892 |
* |
893 |
* @return A logger for HPACK internal debug traces |
|
894 |
*/ |
|
49765 | 895 |
public static Logger getHpackLogger(Supplier<String> dbgTag, Level errLevel) { |
896 |
Level outLevel = Level.OFF; |
|
48083 | 897 |
return DebugLogger.createHpackLogger(dbgTag, outLevel, errLevel); |
898 |
} |
|
899 |
||
900 |
/** |
|
901 |
* Get a logger for debug HPACK traces.The logger should only be used |
|
902 |
* with levels whose severity is {@code <= DEBUG}. |
|
903 |
* |
|
904 |
* By default, this logger will forward all messages logged to an internal |
|
905 |
* logger named "jdk.internal.httpclient.hpack.debug". |
|
906 |
* In addition, the provided boolean {@code on==true}, it will print the |
|
49765 | 907 |
* messages on stderr. |
48083 | 908 |
* The logger will add some decoration to the printed message, in the form of |
909 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
910 |
* |
|
49765 | 911 |
* @apiNote To obtain a logger that will always print things on stderr in |
48083 | 912 |
* addition to forwarding to the internal logger, use |
913 |
* {@code getHpackLogger(this::dbgTag, true);}. |
|
914 |
* This is also equivalent to calling |
|
915 |
* {@code getHpackLogger(this::dbgTag, Level.ALL);}. |
|
916 |
* To obtain a logger that will only forward to the internal logger, |
|
917 |
* use {@code getHpackLogger(this::dbgTag, false);}. |
|
918 |
* This is also equivalent to calling |
|
919 |
* {@code getHpackLogger(this::dbgTag, Level.OFF);}. |
|
920 |
* |
|
921 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
922 |
* (e.g: "Http2Connection(SocketTube(3))/hpack.Decoder(3)") |
|
923 |
* @param on Whether messages should also be printed on |
|
49765 | 924 |
* stderr (in addition to be forwarded to the internal logger). |
48083 | 925 |
* |
926 |
* @return A logger for HPACK internal debug traces |
|
927 |
*/ |
|
928 |
public static Logger getHpackLogger(Supplier<String> dbgTag, boolean on) { |
|
49765 | 929 |
Level errLevel = on ? Level.ALL : Level.OFF; |
930 |
return getHpackLogger(dbgTag, errLevel); |
|
931 |
} |
|
932 |
||
933 |
/** |
|
934 |
* Get a logger for debug WebSocket traces.The logger should only be used |
|
935 |
* with levels whose severity is {@code <= DEBUG}. |
|
936 |
* |
|
937 |
* By default, this logger will forward all messages logged to an internal |
|
938 |
* logger named "jdk.internal.httpclient.websocket.debug". |
|
939 |
* In addition, if the message severity level is >= to |
|
940 |
* the provided {@code errLevel} it will print the messages on stderr. |
|
941 |
* The logger will add some decoration to the printed message, in the form of |
|
942 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
943 |
* |
|
944 |
* @apiNote To obtain a logger that will always print things on stderr in |
|
945 |
* addition to forwarding to the internal logger, use |
|
946 |
* {@code getWebSocketLogger(this::dbgTag, Level.ALL);}. |
|
947 |
* This is also equivalent to calling |
|
948 |
* {@code getWSLogger(this::dbgTag, true);}. |
|
949 |
* To obtain a logger that will only forward to the internal logger, |
|
950 |
* use {@code getWebSocketLogger(this::dbgTag, Level.OFF);}. |
|
951 |
* This is also equivalent to calling |
|
952 |
* {@code getWSLogger(this::dbgTag, false);}. |
|
953 |
* |
|
954 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
955 |
* (e.g: "WebSocket(3)") |
|
956 |
* @param errLevel The level above which messages will be also printed on |
|
957 |
* stderr (in addition to be forwarded to the internal logger). |
|
958 |
* |
|
959 |
* @return A logger for HPACK internal debug traces |
|
960 |
*/ |
|
961 |
public static Logger getWebSocketLogger(Supplier<String> dbgTag, Level errLevel) { |
|
962 |
Level outLevel = Level.OFF; |
|
963 |
return DebugLogger.createWebSocketLogger(dbgTag, outLevel, errLevel); |
|
964 |
} |
|
965 |
||
966 |
/** |
|
967 |
* Get a logger for debug WebSocket traces.The logger should only be used |
|
968 |
* with levels whose severity is {@code <= DEBUG}. |
|
969 |
* |
|
970 |
* By default, this logger will forward all messages logged to an internal |
|
971 |
* logger named "jdk.internal.httpclient.websocket.debug". |
|
972 |
* In addition, the provided boolean {@code on==true}, it will print the |
|
973 |
* messages on stderr. |
|
974 |
* The logger will add some decoration to the printed message, in the form of |
|
975 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
976 |
* |
|
977 |
* @apiNote To obtain a logger that will always print things on stderr in |
|
978 |
* addition to forwarding to the internal logger, use |
|
979 |
* {@code getWebSocketLogger(this::dbgTag, true);}. |
|
980 |
* This is also equivalent to calling |
|
981 |
* {@code getWebSocketLogger(this::dbgTag, Level.ALL);}. |
|
982 |
* To obtain a logger that will only forward to the internal logger, |
|
983 |
* use {@code getWebSocketLogger(this::dbgTag, false);}. |
|
984 |
* This is also equivalent to calling |
|
985 |
* {@code getHpackLogger(this::dbgTag, Level.OFF);}. |
|
986 |
* |
|
987 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
988 |
* (e.g: "WebSocket(3)") |
|
989 |
* @param on Whether messages should also be printed on |
|
990 |
* stderr (in addition to be forwarded to the internal logger). |
|
991 |
* |
|
992 |
* @return A logger for WebSocket internal debug traces |
|
993 |
*/ |
|
994 |
public static Logger getWebSocketLogger(Supplier<String> dbgTag, boolean on) { |
|
995 |
Level errLevel = on ? Level.ALL : Level.OFF; |
|
996 |
return getWebSocketLogger(dbgTag, errLevel); |
|
997 |
} |
|
998 |
||
999 |
/** |
|
1000 |
* SSLSessions returned to user are wrapped in an immutable object |
|
1001 |
*/ |
|
1002 |
public static SSLSession immutableSession(SSLSession session) { |
|
1003 |
if (session instanceof ExtendedSSLSession) |
|
1004 |
return new ImmutableExtendedSSLSession((ExtendedSSLSession)session); |
|
1005 |
else |
|
1006 |
return new ImmutableSSLSession(session); |
|
1007 |
} |
|
1008 |
||
1009 |
/** |
|
1010 |
* Enabled by default. May be disabled for testing. Use with care |
|
1011 |
*/ |
|
1012 |
public static boolean isHostnameVerificationDisabled() { |
|
1013 |
return isHostnameVerificationDisabled; |
|
1014 |
} |
|
1015 |
||
1016 |
public static InetSocketAddress resolveAddress(InetSocketAddress address) { |
|
1017 |
if (address != null && address.isUnresolved()) { |
|
1018 |
// The default proxy selector may select a proxy whose address is |
|
1019 |
// unresolved. We must resolve the address before connecting to it. |
|
1020 |
address = new InetSocketAddress(address.getHostString(), address.getPort()); |
|
1021 |
} |
|
1022 |
return address; |
|
1023 |
} |
|
1024 |
||
50681 | 1025 |
public static Throwable toConnectException(Throwable e) { |
1026 |
if (e == null) return null; |
|
1027 |
e = getCompletionCause(e); |
|
1028 |
if (e instanceof ConnectException) return e; |
|
1029 |
if (e instanceof SecurityException) return e; |
|
1030 |
if (e instanceof SSLException) return e; |
|
1031 |
if (e instanceof Error) return e; |
|
1032 |
if (e instanceof HttpTimeoutException) return e; |
|
1033 |
Throwable cause = e; |
|
1034 |
e = new ConnectException(e.getMessage()); |
|
1035 |
e.initCause(cause); |
|
1036 |
return e; |
|
1037 |
} |
|
1038 |
||
49765 | 1039 |
/** |
1040 |
* Returns the smallest (closest to zero) positive number {@code m} (which |
|
1041 |
* is also a power of 2) such that {@code n <= m}. |
|
1042 |
* <pre>{@code |
|
1043 |
* n pow2Size(n) |
|
1044 |
* ----------------------- |
|
1045 |
* 0 1 |
|
1046 |
* 1 1 |
|
1047 |
* 2 2 |
|
1048 |
* 3 4 |
|
1049 |
* 4 4 |
|
1050 |
* 5 8 |
|
1051 |
* 6 8 |
|
1052 |
* 7 8 |
|
1053 |
* 8 8 |
|
1054 |
* 9 16 |
|
1055 |
* 10 16 |
|
1056 |
* ... ... |
|
1057 |
* 2147483647 1073741824 |
|
1058 |
* } </pre> |
|
1059 |
* |
|
1060 |
* The result is capped at {@code 1 << 30} as beyond that int wraps. |
|
1061 |
* |
|
1062 |
* @param n |
|
1063 |
* capacity |
|
1064 |
* |
|
1065 |
* @return the size of the array |
|
1066 |
* @apiNote Used to size arrays in circular buffers (rings), usually in |
|
1067 |
* order to squeeze extra performance substituting {@code %} operation for |
|
1068 |
* {@code &}, which is up to 2 times faster. |
|
1069 |
*/ |
|
1070 |
public static int pow2Size(int n) { |
|
1071 |
if (n < 0) { |
|
1072 |
throw new IllegalArgumentException(); |
|
1073 |
} else if (n == 0) { |
|
1074 |
return 1; |
|
1075 |
} else if (n >= (1 << 30)) { // 2^31 is a negative int |
|
1076 |
return 1 << 30; |
|
1077 |
} else { |
|
1078 |
return 1 << (32 - Integer.numberOfLeadingZeros(n - 1)); |
|
1079 |
} |
|
48083 | 1080 |
} |
49944 | 1081 |
|
1082 |
// -- toAsciiString-like support to encode path and query URI segments |
|
1083 |
||
1084 |
private static final char[] hexDigits = { |
|
1085 |
'0', '1', '2', '3', '4', '5', '6', '7', |
|
1086 |
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F' |
|
1087 |
}; |
|
1088 |
||
1089 |
private static void appendEscape(StringBuilder sb, byte b) { |
|
1090 |
sb.append('%'); |
|
1091 |
sb.append(hexDigits[(b >> 4) & 0x0f]); |
|
1092 |
sb.append(hexDigits[(b >> 0) & 0x0f]); |
|
1093 |
} |
|
1094 |
||
1095 |
// Encodes all characters >= \u0080 into escaped, normalized UTF-8 octets, |
|
1096 |
// assuming that s is otherwise legal |
|
1097 |
// |
|
1098 |
public static String encode(String s) { |
|
1099 |
int n = s.length(); |
|
1100 |
if (n == 0) |
|
1101 |
return s; |
|
1102 |
||
1103 |
// First check whether we actually need to encode |
|
1104 |
for (int i = 0;;) { |
|
1105 |
if (s.charAt(i) >= '\u0080') |
|
1106 |
break; |
|
1107 |
if (++i >= n) |
|
1108 |
return s; |
|
1109 |
} |
|
1110 |
||
1111 |
String ns = Normalizer.normalize(s, Normalizer.Form.NFC); |
|
1112 |
ByteBuffer bb = null; |
|
1113 |
try { |
|
1114 |
bb = StandardCharsets.UTF_8.newEncoder() |
|
1115 |
.onMalformedInput(CodingErrorAction.REPORT) |
|
1116 |
.onUnmappableCharacter(CodingErrorAction.REPORT) |
|
1117 |
.encode(CharBuffer.wrap(ns)); |
|
1118 |
} catch (CharacterCodingException x) { |
|
1119 |
assert false : x; |
|
1120 |
} |
|
1121 |
||
1122 |
StringBuilder sb = new StringBuilder(); |
|
1123 |
while (bb.hasRemaining()) { |
|
1124 |
int b = bb.get() & 0xff; |
|
1125 |
if (b >= 0x80) |
|
1126 |
appendEscape(sb, (byte)b); |
|
1127 |
else |
|
1128 |
sb.append((char)b); |
|
1129 |
} |
|
1130 |
return sb.toString(); |
|
1131 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1132 |
} |