author | chegar |
Fri, 09 Mar 2018 16:52:31 +0000 | |
branch | http-client-branch |
changeset 56270 | 5c861402c69e |
parent 56263 | 4933a477d628 |
child 56271 | e1eab097c6d7 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
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 |
|
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
26 |
package jdk.internal.net.http.common; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56054
diff
changeset
|
28 |
import java.net.http.HttpHeaders; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import sun.net.NetProperties; |
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
30 |
import sun.net.util.IPAddressUtil; |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
31 |
import sun.net.www.HeaderParser; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.io.ByteArrayOutputStream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
34 |
import java.io.Closeable; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.io.IOException; |
48083 | 36 |
import java.io.PrintStream; |
43730 | 37 |
import java.io.UncheckedIOException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.io.UnsupportedEncodingException; |
48083 | 39 |
import java.lang.System.Logger; |
40 |
import java.lang.System.Logger.Level; |
|
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
41 |
import java.net.InetSocketAddress; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
42 |
import java.net.URI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
43 |
import java.net.URLPermission; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
import java.nio.ByteBuffer; |
43730 | 45 |
import java.nio.charset.Charset; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
46 |
import java.nio.charset.StandardCharsets; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
import java.security.AccessController; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
import java.security.PrivilegedAction; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
import java.util.Arrays; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
import java.util.Collection; |
56033
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
51 |
import java.util.Collections; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
import java.util.List; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
import java.util.Set; |
56033
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
54 |
import java.util.TreeSet; |
48083 | 55 |
import java.util.concurrent.CompletionException; |
56 |
import java.util.concurrent.ExecutionException; |
|
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
57 |
import java.util.function.BiPredicate; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
import java.util.function.Predicate; |
48083 | 59 |
import java.util.function.Supplier; |
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
60 |
import java.util.stream.Collectors; |
48083 | 61 |
import java.util.stream.Stream; |
56126
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
62 |
import javax.net.ssl.SSLParameters; |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
63 |
import javax.net.ssl.SSLSession; |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
64 |
import javax.net.ssl.ExtendedSSLSession; |
48083 | 65 |
|
66 |
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
|
67 |
|
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 |
* Miscellaneous utilities |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
70 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
public final class Utils { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
|
48083 | 73 |
public static final boolean ASSERTIONSENABLED; |
74 |
static { |
|
75 |
boolean enabled = false; |
|
76 |
assert enabled = true; |
|
77 |
ASSERTIONSENABLED = enabled; |
|
78 |
} |
|
79 |
// public static final boolean TESTING; |
|
80 |
// static { |
|
81 |
// if (ASSERTIONSENABLED) { |
|
82 |
// PrivilegedAction<String> action = () -> System.getProperty("test.src"); |
|
83 |
// TESTING = AccessController.doPrivileged(action) != null; |
|
84 |
// } else TESTING = false; |
|
85 |
// } |
|
86 |
public static final boolean DEBUG = // Revisit: temporary dev flag. |
|
87 |
getBooleanProperty(DebugLogger.HTTP_NAME, false); |
|
88 |
public static final boolean DEBUG_HPACK = // Revisit: temporary dev flag. |
|
89 |
getBooleanProperty(DebugLogger.HPACK_NAME, false); |
|
90 |
public static final boolean TESTING = DEBUG; |
|
91 |
||
56137
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
92 |
public static final boolean isHostnameVerificationDisabled = // enabled by default |
56270
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
93 |
hostnameVerificationDisabledValue(); |
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
94 |
|
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
95 |
private static boolean hostnameVerificationDisabledValue() { |
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
96 |
String prop = getProperty("jdk.internal.http.disableHostnameVerification"); |
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
97 |
if (prop == null) |
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
98 |
return false; |
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
99 |
return prop.isEmpty() ? true : Boolean.parseBoolean(prop); |
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
100 |
} |
56137
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
101 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
102 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
103 |
* 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
|
104 |
* 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
|
105 |
* 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
|
106 |
*/ |
48083 | 107 |
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
|
108 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
public static final int BUFSIZE = getIntegerNetProperty( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
"jdk.httpclient.bufsize", DEFAULT_BUFSIZE |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
111 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
112 |
|
56033
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
113 |
private static final Set<String> DISALLOWED_HEADERS_SET; |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
114 |
static { |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
115 |
// A case insensitive TreeSet of strings. |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
116 |
TreeSet<String> treeSet = new TreeSet<>(String.CASE_INSENSITIVE_ORDER); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
117 |
treeSet.addAll(Set.of("connection", "content-length", |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
118 |
"date", "expect", "from", "host", "origin", |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56036
diff
changeset
|
119 |
"referer", "upgrade", |
56033
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
120 |
"via", "warning")); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
121 |
DISALLOWED_HEADERS_SET = Collections.unmodifiableSet(treeSet); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
122 |
} |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56036
diff
changeset
|
123 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
public static final Predicate<String> |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56036
diff
changeset
|
125 |
ALLOWED_HEADERS = header -> !DISALLOWED_HEADERS_SET.contains(header); |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56036
diff
changeset
|
126 |
|
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
127 |
private static final Predicate<String> IS_PROXY_HEADER = (k) -> |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56036
diff
changeset
|
128 |
k != null && k.length() > 6 && "proxy-".equalsIgnoreCase(k.substring(0,6)); |
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
129 |
private static final Predicate<String> NO_PROXY_HEADER = |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
56036
diff
changeset
|
130 |
IS_PROXY_HEADER.negate(); |
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
131 |
private static final Predicate<String> ALL_HEADERS = (s) -> true; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
132 |
|
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
133 |
private static final Set<String> PROXY_AUTH_DISABLED_SCHEMES; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
134 |
private static final Set<String> PROXY_AUTH_TUNNEL_DISABLED_SCHEMES; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
135 |
static { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
136 |
String proxyAuthDisabled = |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
137 |
getNetProperty("jdk.http.auth.proxying.disabledSchemes"); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
138 |
String proxyAuthTunnelDisabled = |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
139 |
getNetProperty("jdk.http.auth.tunneling.disabledSchemes"); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
140 |
PROXY_AUTH_DISABLED_SCHEMES = |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
141 |
proxyAuthDisabled == null ? Set.of() : |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
142 |
Stream.of(proxyAuthDisabled.split(",")) |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
143 |
.map(String::trim) |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
144 |
.filter((s) -> !s.isEmpty()) |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
145 |
.collect(Collectors.toUnmodifiableSet()); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
146 |
PROXY_AUTH_TUNNEL_DISABLED_SCHEMES = |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
147 |
proxyAuthTunnelDisabled == null ? Set.of() : |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
148 |
Stream.of(proxyAuthTunnelDisabled.split(",")) |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
149 |
.map(String::trim) |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
150 |
.filter((s) -> !s.isEmpty()) |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
151 |
.collect(Collectors.toUnmodifiableSet()); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
152 |
} |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
153 |
|
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
154 |
private static final String WSPACES = " \t\r\n"; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
155 |
private static final boolean isAllowedForProxy(String name, |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
156 |
List<String> value, |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
157 |
Set<String> disabledSchemes, |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
158 |
Predicate<String> allowedKeys) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
159 |
if (!allowedKeys.test(name)) return false; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
160 |
if (disabledSchemes.isEmpty()) return true; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
161 |
if (name.equalsIgnoreCase("proxy-authorization")) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
162 |
if (value.isEmpty()) return false; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
163 |
for (String scheme : disabledSchemes) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
164 |
int slen = scheme.length(); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
165 |
for (String v : value) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
166 |
int vlen = v.length(); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
167 |
if (vlen == slen) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
168 |
if (v.equalsIgnoreCase(scheme)) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
169 |
return false; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
170 |
} |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
171 |
} else if (vlen > slen) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
172 |
if (v.substring(0,slen).equalsIgnoreCase(scheme)) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
173 |
int c = v.codePointAt(slen); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
174 |
if (WSPACES.indexOf(c) > -1 |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
175 |
|| Character.isSpaceChar(c) |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
176 |
|| Character.isWhitespace(c)) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
177 |
return false; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
178 |
} |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
179 |
} |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
180 |
} |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
181 |
} |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
182 |
} |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
183 |
} |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
184 |
return true; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
185 |
} |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
186 |
|
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
187 |
public static final BiPredicate<String, List<String>> PROXY_TUNNEL_FILTER = |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
188 |
(s,v) -> isAllowedForProxy(s, v, PROXY_AUTH_TUNNEL_DISABLED_SCHEMES, |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
189 |
IS_PROXY_HEADER); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
190 |
public static final BiPredicate<String, List<String>> PROXY_FILTER = |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
191 |
(s,v) -> isAllowedForProxy(s, v, PROXY_AUTH_DISABLED_SCHEMES, |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
192 |
ALL_HEADERS); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
193 |
public static final BiPredicate<String, List<String>> NO_PROXY_HEADERS_FILTER = |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
194 |
(n,v) -> Utils.NO_PROXY_HEADER.test(n); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
195 |
|
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
196 |
|
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
197 |
public static boolean proxyHasDisabledSchemes(boolean tunnel) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
198 |
return tunnel ? ! PROXY_AUTH_TUNNEL_DISABLED_SCHEMES.isEmpty() |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
199 |
: ! PROXY_AUTH_DISABLED_SCHEMES.isEmpty(); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56041
diff
changeset
|
200 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
201 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
202 |
public static ByteBuffer getBuffer() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
203 |
return ByteBuffer.allocate(BUFSIZE); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
204 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
205 |
|
48083 | 206 |
public static Throwable getCompletionCause(Throwable x) { |
207 |
if (!(x instanceof CompletionException) |
|
208 |
&& !(x instanceof ExecutionException)) return x; |
|
209 |
final Throwable cause = x.getCause(); |
|
56024
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
56009
diff
changeset
|
210 |
if (cause == null) { |
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
56009
diff
changeset
|
211 |
throw new InternalError("Unexpected null cause", x); |
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
56009
diff
changeset
|
212 |
} |
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
56009
diff
changeset
|
213 |
return cause; |
48083 | 214 |
} |
215 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
public static IOException getIOException(Throwable t) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
if (t instanceof IOException) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
218 |
return (IOException) t; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
219 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
220 |
Throwable cause = t.getCause(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
221 |
if (cause != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
222 |
return getIOException(cause); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
return new IOException(t); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
|
48083 | 227 |
private Utils() { } |
228 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
/** |
48083 | 230 |
* Returns the security permissions required to connect to the proxy, or |
231 |
* {@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
|
232 |
*/ |
48083 | 233 |
public static URLPermission permissionForProxy(InetSocketAddress proxyAddress) { |
234 |
if (proxyAddress == null) |
|
235 |
return null; |
|
236 |
||
237 |
StringBuilder sb = new StringBuilder(); |
|
238 |
sb.append("socket://") |
|
239 |
.append(proxyAddress.getHostString()).append(":") |
|
240 |
.append(proxyAddress.getPort()); |
|
241 |
String urlString = sb.toString(); |
|
242 |
return new URLPermission(urlString, "CONNECT"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
244 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
245 |
/** |
48083 | 246 |
* 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
|
247 |
*/ |
48083 | 248 |
public static URLPermission permissionForServer(URI uri, |
249 |
String method, |
|
250 |
Stream<String> headers) { |
|
251 |
String urlString = new StringBuilder() |
|
252 |
.append(uri.getScheme()).append("://") |
|
253 |
.append(uri.getAuthority()) |
|
254 |
.append(uri.getPath()).toString(); |
|
255 |
||
256 |
StringBuilder actionStringBuilder = new StringBuilder(method); |
|
257 |
String collected = headers.collect(joining(",")); |
|
258 |
if (!collected.isEmpty()) { |
|
259 |
actionStringBuilder.append(":").append(collected); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
260 |
} |
48083 | 261 |
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
|
262 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
263 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
264 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
265 |
// ABNF primitives defined in RFC 7230 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
266 |
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
|
267 |
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
|
268 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
269 |
static { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
270 |
char[] allowedTokenChars = |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
271 |
("!#$%&'*+-.^_`|~0123456789" + |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
272 |
"abcdefghijklmnopqrstuvwxyz" + |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
273 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
274 |
for (char c : allowedTokenChars) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
275 |
tchar[c] = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
276 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
277 |
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
|
278 |
fieldvchar[c] = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
279 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
280 |
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
|
281 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
282 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
283 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
284 |
* Validates a RFC 7230 field-name. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
285 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
286 |
public static boolean isValidName(String token) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
287 |
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
|
288 |
char c = token.charAt(i); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
289 |
if (c > 255 || !tchar[c]) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
290 |
return false; |
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 |
return !token.isEmpty(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
294 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
295 |
|
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
296 |
/** |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
297 |
* If the address was created with a domain name, then return |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
298 |
* the domain name string. If created with a literal IP address |
56126
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
299 |
* then return null except if the literal is loopback. |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
300 |
* We do this to avoid doing a reverse lookup |
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
301 |
* Used to populate the TLS SNI parameter. So, SNI is only set |
56126
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
302 |
* when a domain name was supplied except in case of loopback |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
303 |
* where we return "localhost". |
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
304 |
*/ |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
305 |
public static String getServerName(InetSocketAddress addr) { |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
306 |
String host = addr.getHostString(); |
56126
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
307 |
byte[] literal = IPAddressUtil.textToNumericFormatV4(host); |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
308 |
if (literal == null) { |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
309 |
// not IPv4 literal |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
310 |
literal = IPAddressUtil.textToNumericFormatV6(host); |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
311 |
if (literal == null) { |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
312 |
// not IPv6 literal. Must be domain name |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
313 |
return host; |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
314 |
} else { // check if loopback |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
315 |
if (isLoopbackLiteral(literal)) |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
316 |
return "localhost"; |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
317 |
else |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
318 |
return null; |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
319 |
} |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
320 |
} else { |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
321 |
// check if IPv4 loopback |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
322 |
if (isLoopbackLiteral(literal)) |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
323 |
return "localhost"; |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
324 |
else |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
325 |
return null; |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
326 |
} |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
327 |
} |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
328 |
|
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
329 |
private static boolean isLoopbackLiteral(byte[] bytes) { |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
330 |
if (bytes.length == 4) { |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
331 |
return bytes[0] == 127; |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
332 |
} else if (bytes.length == 16) { |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
333 |
for (int i=0; i<14; i++) |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
334 |
if (bytes[i] != 0) |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
335 |
return false; |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
336 |
if (bytes[15] != 1) |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
337 |
return false; |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
338 |
return true; |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
339 |
} else |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
340 |
throw new InternalError(); |
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
341 |
} |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
342 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
* Validates a RFC 7230 field-value. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
345 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
* "Obsolete line folding" rule |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
347 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
* obs-fold = CRLF 1*( SP / HTAB ) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
* is not permitted! |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
351 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
352 |
public static boolean isValidValue(String token) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
353 |
boolean accepted = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
354 |
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
|
355 |
char c = token.charAt(i); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
356 |
if (c > 255) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
357 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
358 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
if (accepted) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
if (c == ' ' || c == '\t') { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
accepted = false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
} else if (!fieldvchar[c]) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
363 |
return false; // forbidden byte |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
364 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
if (c != ' ' && c != '\t') { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
if (fieldvchar[c]) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
368 |
accepted = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
370 |
return false; // forbidden byte |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
371 |
} |
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 |
} |
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 |
return accepted; |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
378 |
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
|
379 |
return AccessController.doPrivileged((PrivilegedAction<Integer>) () -> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
380 |
NetProperties.getInteger(name, defaultValue)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
381 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
382 |
|
56263 | 383 |
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
|
384 |
return AccessController.doPrivileged((PrivilegedAction<String>) () -> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
385 |
NetProperties.get(name)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
386 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
387 |
|
56137
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
388 |
public static boolean getBooleanProperty(String name, boolean def) { |
48083 | 389 |
return AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> |
390 |
Boolean.parseBoolean(System.getProperty(name, String.valueOf(def)))); |
|
391 |
} |
|
392 |
||
56270
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
393 |
public static String getProperty(String name) { |
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
394 |
return AccessController.doPrivileged((PrivilegedAction<String>) () -> |
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
395 |
System.getProperty(name)); |
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
396 |
} |
5c861402c69e
http-client-branch: test update for disableHostnameVerification
chegar
parents:
56263
diff
changeset
|
397 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
398 |
public static SSLParameters copySSLParameters(SSLParameters p) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
399 |
SSLParameters p1 = new SSLParameters(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
400 |
p1.setAlgorithmConstraints(p.getAlgorithmConstraints()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
401 |
p1.setCipherSuites(p.getCipherSuites()); |
43730 | 402 |
// JDK 8 EXCL START |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
p1.setEnableRetransmissions(p.getEnableRetransmissions()); |
43730 | 404 |
p1.setMaximumPacketSize(p.getMaximumPacketSize()); |
405 |
// JDK 8 EXCL END |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
406 |
p1.setEndpointIdentificationAlgorithm(p.getEndpointIdentificationAlgorithm()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
407 |
p1.setNeedClientAuth(p.getNeedClientAuth()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
408 |
String[] protocols = p.getProtocols(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
409 |
if (protocols != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
410 |
p1.setProtocols(protocols.clone()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
411 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
412 |
p1.setSNIMatchers(p.getSNIMatchers()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
413 |
p1.setServerNames(p.getServerNames()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
414 |
p1.setUseCipherSuitesOrder(p.getUseCipherSuitesOrder()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
415 |
p1.setWantClientAuth(p.getWantClientAuth()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
416 |
return p1; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
417 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
418 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
419 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
420 |
* 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
|
421 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
422 |
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
|
423 |
buffer.limit(buffer.position()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
424 |
buffer.position(mark); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
425 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
426 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
427 |
public static String stackTrace(Throwable t) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
428 |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
429 |
String s = null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
430 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
431 |
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
|
432 |
t.printStackTrace(p); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
433 |
s = bos.toString("US-ASCII"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
434 |
} catch (UnsupportedEncodingException ex) { |
48083 | 435 |
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
|
436 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
437 |
return s; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
438 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
439 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
440 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
441 |
* 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
|
442 |
* Return number of bytes copied |
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 |
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
|
445 |
int srcLen = src.remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
446 |
int dstLen = dst.remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
447 |
if (srcLen > dstLen) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
448 |
int diff = srcLen - dstLen; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
449 |
int limit = src.limit(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
450 |
src.limit(limit - diff); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
451 |
dst.put(src); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
452 |
src.limit(limit); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
453 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
454 |
dst.put(src); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
455 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
456 |
return srcLen - src.remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
457 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
458 |
|
48083 | 459 |
/** Threshold beyond which data is no longer copied into the current |
460 |
* buffer, if that buffer has enough unused space. */ |
|
461 |
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
|
462 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
463 |
/** |
48083 | 464 |
* Adds the data from buffersToAdd to currentList. Either 1) appends the |
465 |
* data from a particular buffer to the last buffer in the list ( if |
|
466 |
* there is enough unused space ), or 2) adds it to the list. |
|
467 |
* |
|
468 |
* @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
|
469 |
*/ |
48083 | 470 |
public static long accumulateBuffers(List<ByteBuffer> currentList, |
471 |
List<ByteBuffer> buffersToAdd) { |
|
472 |
long accumulatedBytes = 0; |
|
473 |
for (ByteBuffer bufferToAdd : buffersToAdd) { |
|
474 |
int remaining = bufferToAdd.remaining(); |
|
475 |
if (remaining <= 0) |
|
476 |
continue; |
|
477 |
int listSize = currentList.size(); |
|
478 |
if (listSize == 0) { |
|
479 |
currentList.add(bufferToAdd); |
|
480 |
accumulatedBytes = remaining; |
|
481 |
continue; |
|
482 |
} |
|
483 |
||
484 |
ByteBuffer lastBuffer = currentList.get(listSize - 1); |
|
485 |
int freeSpace = lastBuffer.capacity() - lastBuffer.limit(); |
|
486 |
if (remaining <= COPY_THRESHOLD && freeSpace >= remaining) { |
|
487 |
// append the new data to the unused space in the last buffer |
|
488 |
int position = lastBuffer.position(); |
|
489 |
int limit = lastBuffer.limit(); |
|
490 |
lastBuffer.position(limit); |
|
491 |
lastBuffer.limit(limit + remaining); |
|
492 |
lastBuffer.put(bufferToAdd); |
|
493 |
lastBuffer.position(position); |
|
494 |
} else { |
|
495 |
currentList.add(bufferToAdd); |
|
496 |
} |
|
497 |
accumulatedBytes += remaining; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
498 |
} |
48083 | 499 |
return accumulatedBytes; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
500 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
501 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
502 |
public static ByteBuffer copy(ByteBuffer src) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
503 |
ByteBuffer dst = ByteBuffer.allocate(src.remaining()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
504 |
dst.put(src); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
505 |
dst.flip(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
506 |
return dst; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
507 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
508 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
509 |
public static String dump(Object... objects) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
510 |
return Arrays.toString(objects); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
511 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
512 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
513 |
public static String stringOf(Collection<?> source) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
514 |
// 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
|
515 |
// 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
|
516 |
return Arrays.toString(source.toArray()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
517 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
518 |
|
48083 | 519 |
public static long remaining(ByteBuffer[] bufs) { |
520 |
long remain = 0; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
521 |
for (ByteBuffer buf : bufs) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
522 |
remain += buf.remaining(); |
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 |
return remain; |
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 |
|
48083 | 527 |
public static boolean hasRemaining(List<ByteBuffer> bufs) { |
528 |
synchronized (bufs) { |
|
529 |
for (ByteBuffer buf : bufs) { |
|
530 |
if (buf.hasRemaining()) |
|
531 |
return true; |
|
532 |
} |
|
533 |
} |
|
534 |
return false; |
|
535 |
} |
|
536 |
||
537 |
public static long remaining(List<ByteBuffer> bufs) { |
|
538 |
long remain = 0; |
|
539 |
synchronized (bufs) { |
|
540 |
for (ByteBuffer buf : bufs) { |
|
541 |
remain += buf.remaining(); |
|
542 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
543 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
544 |
return remain; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
545 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
546 |
|
48083 | 547 |
public static int remaining(List<ByteBuffer> bufs, int max) { |
548 |
long remain = 0; |
|
549 |
synchronized (bufs) { |
|
550 |
for (ByteBuffer buf : bufs) { |
|
551 |
remain += buf.remaining(); |
|
552 |
if (remain > max) { |
|
553 |
throw new IllegalArgumentException("too many bytes"); |
|
554 |
} |
|
555 |
} |
|
556 |
} |
|
557 |
return (int) remain; |
|
558 |
} |
|
559 |
||
560 |
public static long remaining(ByteBufferReference[] refs) { |
|
561 |
long remain = 0; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
562 |
for (ByteBufferReference ref : refs) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
563 |
remain += ref.get().remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
564 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
565 |
return remain; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
566 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
567 |
|
48083 | 568 |
public static int remaining(ByteBufferReference[] refs, int max) { |
569 |
long remain = 0; |
|
570 |
for (ByteBufferReference ref : refs) { |
|
571 |
remain += ref.get().remaining(); |
|
572 |
if (remain > max) { |
|
573 |
throw new IllegalArgumentException("too many bytes"); |
|
574 |
} |
|
575 |
} |
|
576 |
return (int) remain; |
|
577 |
} |
|
578 |
||
579 |
public static int remaining(ByteBuffer[] refs, int max) { |
|
580 |
long remain = 0; |
|
581 |
for (ByteBuffer b : refs) { |
|
582 |
remain += b.remaining(); |
|
583 |
if (remain > max) { |
|
584 |
throw new IllegalArgumentException("too many bytes"); |
|
585 |
} |
|
586 |
} |
|
587 |
return (int) remain; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
588 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
589 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
590 |
public static void close(Closeable... closeables) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
591 |
for (Closeable c : closeables) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
592 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
593 |
c.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
594 |
} catch (IOException ignored) { } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
595 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
596 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
597 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
598 |
// 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
|
599 |
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
|
600 |
public static final ByteBuffer[] EMPTY_BB_ARRAY = new ByteBuffer[0]; |
48083 | 601 |
public static final List<ByteBuffer> EMPTY_BB_LIST = List.of(); |
602 |
public static final ByteBufferReference[] EMPTY_BBR_ARRAY = new ByteBufferReference[0]; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
603 |
|
56036
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
604 |
/** |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
605 |
* Returns a slice of size {@code amount} from the given buffer. If the |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
606 |
* buffer contains more data than {@code amount}, then the slice's capacity |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
607 |
* ( and, but not just, its limit ) is set to {@code amount}. If the buffer |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
608 |
* does not contain more data than {@code amount}, then the slice's capacity |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
609 |
* will be the same as the given buffer's capacity. |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
610 |
*/ |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
611 |
public static ByteBuffer sliceWithLimitedCapacity(ByteBuffer buffer, int amount) { |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
612 |
final int index = buffer.position() + amount; |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
613 |
final int limit = buffer.limit(); |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
614 |
if (index != limit) { |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
615 |
// additional data in the buffer |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
616 |
buffer.limit(index); // ensures that the slice does not go beyond |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
617 |
} else { |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
618 |
// no additional data in the buffer |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
619 |
buffer.limit(buffer.capacity()); // allows the slice full capacity |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
620 |
} |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
621 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
622 |
ByteBuffer newb = buffer.slice(); |
56036
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
623 |
buffer.position(index); |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
624 |
buffer.limit(limit); // restore the original buffer's limit |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
625 |
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
|
626 |
return newb; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
627 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
628 |
|
43730 | 629 |
/** |
630 |
* Get the Charset from the Content-encoding header. Defaults to |
|
631 |
* UTF_8 |
|
632 |
*/ |
|
633 |
public static Charset charsetFrom(HttpHeaders headers) { |
|
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
634 |
String type = headers.firstValue("Content-type") |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
635 |
.orElse("text/html; charset=utf-8"); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
636 |
int i = type.indexOf(";"); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
637 |
if (i >= 0) type = type.substring(i+1); |
43730 | 638 |
try { |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
639 |
HeaderParser parser = new HeaderParser(type); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
640 |
String value = parser.findValue("charset"); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
641 |
if (value == null) return StandardCharsets.UTF_8; |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
642 |
return Charset.forName(value); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
643 |
} catch (Throwable x) { |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
644 |
Log.logTrace("Can't find charset in \"{0}\" ({1})", type, x); |
43730 | 645 |
return StandardCharsets.UTF_8; |
646 |
} |
|
647 |
} |
|
648 |
||
649 |
public static UncheckedIOException unchecked(IOException e) { |
|
650 |
return new UncheckedIOException(e); |
|
651 |
} |
|
48083 | 652 |
|
653 |
/** |
|
654 |
* Get a logger for debug HTTP traces. |
|
655 |
* |
|
656 |
* The logger should only be used with levels whose severity is |
|
657 |
* {@code <= DEBUG}. By default, this logger will forward all messages |
|
658 |
* logged to an internal logger named "jdk.internal.httpclient.debug". |
|
659 |
* In addition, if the property -Djdk.internal.httpclient.debug=true is set, |
|
660 |
* it will print the messages on stderr. |
|
661 |
* The logger will add some decoration to the printed message, in the form of |
|
662 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
663 |
* |
|
664 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
665 |
* (e.g: "SocketTube(3)", or "Http2Connection(SocketTube(3))") |
|
666 |
* |
|
667 |
* @return A logger for HTTP internal debug traces |
|
668 |
*/ |
|
669 |
public static Logger getDebugLogger(Supplier<String> dbgTag) { |
|
670 |
return getDebugLogger(dbgTag, DEBUG); |
|
671 |
} |
|
672 |
||
673 |
/** |
|
674 |
* Get a logger for debug HTTP traces.The logger should only be used |
|
675 |
* with levels whose severity is {@code <= DEBUG}. |
|
676 |
* |
|
677 |
* By default, this logger will forward all messages logged to an internal |
|
678 |
* logger named "jdk.internal.httpclient.debug". |
|
679 |
* In addition, if the message severity level is >= to |
|
680 |
* the provided {@code errLevel} it will print the messages on stderr. |
|
681 |
* The logger will add some decoration to the printed message, in the form of |
|
682 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
683 |
* |
|
684 |
* @apiNote To obtain a logger that will always print things on stderr in |
|
685 |
* addition to forwarding to the internal logger, use |
|
686 |
* {@code getDebugLogger(this::dbgTag, Level.ALL);}. |
|
687 |
* This is also equivalent to calling |
|
688 |
* {@code getDebugLogger(this::dbgTag, true);}. |
|
689 |
* To obtain a logger that will only forward to the internal logger, |
|
690 |
* use {@code getDebugLogger(this::dbgTag, Level.OFF);}. |
|
691 |
* This is also equivalent to calling |
|
692 |
* {@code getDebugLogger(this::dbgTag, false);}. |
|
693 |
* |
|
694 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
695 |
* (e.g: "SocketTube(3)", or "Http2Connection(SocketTube(3))") |
|
696 |
* @param errLevel The level above which messages will be also printed on |
|
697 |
* stderr (in addition to be forwarded to the internal logger). |
|
698 |
* |
|
699 |
* @return A logger for HTTP internal debug traces |
|
700 |
*/ |
|
701 |
static Logger getDebugLogger(Supplier<String> dbgTag, Level errLevel) { |
|
702 |
return DebugLogger.createHttpLogger(dbgTag, Level.OFF, errLevel); |
|
703 |
} |
|
704 |
||
705 |
/** |
|
706 |
* Get a logger for debug HTTP traces.The logger should only be used |
|
707 |
* with levels whose severity is {@code <= DEBUG}. |
|
708 |
* |
|
709 |
* By default, this logger will forward all messages logged to an internal |
|
710 |
* logger named "jdk.internal.httpclient.debug". |
|
711 |
* In addition, the provided boolean {@code on==true}, it will print the |
|
712 |
* messages on stderr. |
|
713 |
* The logger will add some decoration to the printed message, in the form of |
|
714 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
715 |
* |
|
716 |
* @apiNote To obtain a logger that will always print things on stderr in |
|
717 |
* addition to forwarding to the internal logger, use |
|
718 |
* {@code getDebugLogger(this::dbgTag, true);}. |
|
719 |
* This is also equivalent to calling |
|
720 |
* {@code getDebugLogger(this::dbgTag, Level.ALL);}. |
|
721 |
* To obtain a logger that will only forward to the internal logger, |
|
722 |
* use {@code getDebugLogger(this::dbgTag, false);}. |
|
723 |
* This is also equivalent to calling |
|
724 |
* {@code getDebugLogger(this::dbgTag, Level.OFF);}. |
|
725 |
* |
|
726 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
727 |
* (e.g: "SocketTube(3)", or "Http2Connection(SocketTube(3))") |
|
728 |
* @param on Whether messages should also be printed on |
|
729 |
* stderr (in addition to be forwarded to the internal logger). |
|
730 |
* |
|
731 |
* @return A logger for HTTP internal debug traces |
|
732 |
*/ |
|
733 |
public static Logger getDebugLogger(Supplier<String> dbgTag, boolean on) { |
|
734 |
Level errLevel = on ? Level.ALL : Level.OFF; |
|
735 |
return getDebugLogger(dbgTag, errLevel); |
|
736 |
} |
|
737 |
||
738 |
/** |
|
739 |
* Get a logger for debug HPACK traces.The logger should only be used |
|
740 |
* with levels whose severity is {@code <= DEBUG}. |
|
741 |
* |
|
742 |
* By default, this logger will forward all messages logged to an internal |
|
743 |
* logger named "jdk.internal.httpclient.hpack.debug". |
|
744 |
* In addition, if the message severity level is >= to |
|
745 |
* the provided {@code outLevel} it will print the messages on stdout. |
|
746 |
* The logger will add some decoration to the printed message, in the form of |
|
747 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
748 |
* |
|
749 |
* @apiNote To obtain a logger that will always print things on stdout in |
|
750 |
* addition to forwarding to the internal logger, use |
|
751 |
* {@code getHpackLogger(this::dbgTag, Level.ALL);}. |
|
752 |
* This is also equivalent to calling |
|
753 |
* {@code getHpackLogger(this::dbgTag, true);}. |
|
754 |
* To obtain a logger that will only forward to the internal logger, |
|
755 |
* use {@code getHpackLogger(this::dbgTag, Level.OFF);}. |
|
756 |
* This is also equivalent to calling |
|
757 |
* {@code getHpackLogger(this::dbgTag, false);}. |
|
758 |
* |
|
759 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
760 |
* (e.g: "Http2Connection(SocketTube(3))/hpack.Decoder(3)") |
|
761 |
* @param outLevel The level above which messages will be also printed on |
|
762 |
* stdout (in addition to be forwarded to the internal logger). |
|
763 |
* |
|
764 |
* @return A logger for HPACK internal debug traces |
|
765 |
*/ |
|
766 |
public static Logger getHpackLogger(Supplier<String> dbgTag, Level outLevel) { |
|
767 |
Level errLevel = Level.OFF; |
|
768 |
return DebugLogger.createHpackLogger(dbgTag, outLevel, errLevel); |
|
769 |
} |
|
770 |
||
771 |
/** |
|
772 |
* Get a logger for debug HPACK traces.The logger should only be used |
|
773 |
* with levels whose severity is {@code <= DEBUG}. |
|
774 |
* |
|
775 |
* By default, this logger will forward all messages logged to an internal |
|
776 |
* logger named "jdk.internal.httpclient.hpack.debug". |
|
777 |
* In addition, the provided boolean {@code on==true}, it will print the |
|
778 |
* messages on stdout. |
|
779 |
* The logger will add some decoration to the printed message, in the form of |
|
780 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
781 |
* |
|
782 |
* @apiNote To obtain a logger that will always print things on stdout in |
|
783 |
* addition to forwarding to the internal logger, use |
|
784 |
* {@code getHpackLogger(this::dbgTag, true);}. |
|
785 |
* This is also equivalent to calling |
|
786 |
* {@code getHpackLogger(this::dbgTag, Level.ALL);}. |
|
787 |
* To obtain a logger that will only forward to the internal logger, |
|
788 |
* use {@code getHpackLogger(this::dbgTag, false);}. |
|
789 |
* This is also equivalent to calling |
|
790 |
* {@code getHpackLogger(this::dbgTag, Level.OFF);}. |
|
791 |
* |
|
792 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
793 |
* (e.g: "Http2Connection(SocketTube(3))/hpack.Decoder(3)") |
|
794 |
* @param on Whether messages should also be printed on |
|
795 |
* stdout (in addition to be forwarded to the internal logger). |
|
796 |
* |
|
797 |
* @return A logger for HPACK internal debug traces |
|
798 |
*/ |
|
799 |
public static Logger getHpackLogger(Supplier<String> dbgTag, boolean on) { |
|
800 |
Level outLevel = on ? Level.ALL : Level.OFF; |
|
801 |
return getHpackLogger(dbgTag, outLevel); |
|
802 |
} |
|
56126
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
803 |
|
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
804 |
/** |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
805 |
* SSLSessions returned to user are wrapped in an immutable object |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
806 |
*/ |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
807 |
public static SSLSession immutableSession(SSLSession session) { |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
808 |
if (session instanceof ExtendedSSLSession) |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
809 |
return new ImmutableExtendedSSLSession((ExtendedSSLSession)session); |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
810 |
else |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
811 |
return new ImmutableSSLSession(session); |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56092
diff
changeset
|
812 |
} |
56137
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
813 |
|
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
814 |
/** |
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
815 |
* Enabled by default. May be disabled for testing. Use with care |
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
816 |
*/ |
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
817 |
public static boolean isHostnameVerificationDisabled() { |
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
818 |
return isHostnameVerificationDisabled; |
dd867826d55b
http-client-branch: added system property to disable hostname verification for testing
michaelm
parents:
56126
diff
changeset
|
819 |
} |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56137
diff
changeset
|
820 |
|
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56137
diff
changeset
|
821 |
public static InetSocketAddress resolveAddress(InetSocketAddress address) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56137
diff
changeset
|
822 |
if (address != null && address.isUnresolved()) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56137
diff
changeset
|
823 |
// The default proxy selector may select a proxy whose address is |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56137
diff
changeset
|
824 |
// unresolved. We must resolve the address before connecting to it. |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56137
diff
changeset
|
825 |
address = new InetSocketAddress(address.getHostString(), address.getPort()); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56137
diff
changeset
|
826 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56137
diff
changeset
|
827 |
return address; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56137
diff
changeset
|
828 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
829 |
} |