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