author | chegar |
Tue, 23 Jan 2018 13:21:59 +0000 | |
branch | http-client-branch |
changeset 56036 | 89a688549f5d |
parent 56033 | db102c5ca88a |
child 56041 | b4b5e09ef3cc |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
package jdk.incubator.http.internal.common; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
48083 | 28 |
import jdk.incubator.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 javax.net.ssl.SSLParameters; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
34 |
import java.io.ByteArrayOutputStream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.io.Closeable; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.io.IOException; |
48083 | 37 |
import java.io.PrintStream; |
43730 | 38 |
import java.io.UncheckedIOException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.io.UnsupportedEncodingException; |
48083 | 40 |
import java.lang.System.Logger; |
41 |
import java.lang.System.Logger.Level; |
|
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
42 |
import java.net.InetSocketAddress; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
43 |
import java.net.URI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
import java.net.URLPermission; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
45 |
import java.nio.ByteBuffer; |
43730 | 46 |
import java.nio.charset.Charset; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
import java.nio.charset.StandardCharsets; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
import java.security.AccessController; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
import java.security.PrivilegedAction; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
import java.util.Arrays; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
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
|
52 |
import java.util.Collections; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
import java.util.List; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
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
|
55 |
import java.util.TreeSet; |
48083 | 56 |
import java.util.concurrent.CompletionException; |
57 |
import java.util.concurrent.ExecutionException; |
|
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; |
60 |
import java.util.stream.Stream; |
|
61 |
||
62 |
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
|
63 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
65 |
* Miscellaneous utilities |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
67 |
public final class Utils { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
68 |
|
48083 | 69 |
public static final boolean ASSERTIONSENABLED; |
70 |
static { |
|
71 |
boolean enabled = false; |
|
72 |
assert enabled = true; |
|
73 |
ASSERTIONSENABLED = enabled; |
|
74 |
} |
|
75 |
// public static final boolean TESTING; |
|
76 |
// static { |
|
77 |
// if (ASSERTIONSENABLED) { |
|
78 |
// PrivilegedAction<String> action = () -> System.getProperty("test.src"); |
|
79 |
// TESTING = AccessController.doPrivileged(action) != null; |
|
80 |
// } else TESTING = false; |
|
81 |
// } |
|
82 |
public static final boolean DEBUG = // Revisit: temporary dev flag. |
|
83 |
getBooleanProperty(DebugLogger.HTTP_NAME, false); |
|
84 |
public static final boolean DEBUG_HPACK = // Revisit: temporary dev flag. |
|
85 |
getBooleanProperty(DebugLogger.HPACK_NAME, false); |
|
86 |
public static final boolean TESTING = DEBUG; |
|
87 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
* 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
|
90 |
* 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
|
91 |
* 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
|
92 |
*/ |
48083 | 93 |
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
|
94 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
public static final int BUFSIZE = getIntegerNetProperty( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
96 |
"jdk.httpclient.bufsize", DEFAULT_BUFSIZE |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
98 |
|
56033
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
99 |
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
|
100 |
static { |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
101 |
// 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
|
102 |
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
|
103 |
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
|
104 |
"date", "expect", "from", "host", "origin", |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
105 |
"proxy-authorization", "referer", "upgrade", |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
106 |
"via", "warning")); |
db102c5ca88a
http-client-branch: 8194729: Java 9's HttpClient doesn't allow custom 'Authorization' headers
dfuchs
parents:
56024
diff
changeset
|
107 |
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
|
108 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
109 |
public static final Predicate<String> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
110 |
ALLOWED_HEADERS = header -> !Utils.DISALLOWED_HEADERS_SET.contains(header); |
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 |
public static ByteBuffer getBuffer() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
113 |
return ByteBuffer.allocate(BUFSIZE); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
114 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
115 |
|
48083 | 116 |
public static Throwable getCompletionCause(Throwable x) { |
117 |
if (!(x instanceof CompletionException) |
|
118 |
&& !(x instanceof ExecutionException)) return x; |
|
119 |
final Throwable cause = x.getCause(); |
|
56024
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
56009
diff
changeset
|
120 |
if (cause == null) { |
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
56009
diff
changeset
|
121 |
throw new InternalError("Unexpected null cause", x); |
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
56009
diff
changeset
|
122 |
} |
de352132c7e8
http-client-branch: (WebSocket) a number of tests for exceptional completion
prappo
parents:
56009
diff
changeset
|
123 |
return cause; |
48083 | 124 |
} |
125 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
126 |
public static IOException getIOException(Throwable t) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
127 |
if (t instanceof IOException) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
128 |
return (IOException) t; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
129 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
Throwable cause = t.getCause(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
if (cause != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
return getIOException(cause); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
133 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
return new IOException(t); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
|
48083 | 137 |
private Utils() { } |
138 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
/** |
48083 | 140 |
* Returns the security permissions required to connect to the proxy, or |
141 |
* {@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
|
142 |
*/ |
48083 | 143 |
public static URLPermission permissionForProxy(InetSocketAddress proxyAddress) { |
144 |
if (proxyAddress == null) |
|
145 |
return null; |
|
146 |
||
147 |
StringBuilder sb = new StringBuilder(); |
|
148 |
sb.append("socket://") |
|
149 |
.append(proxyAddress.getHostString()).append(":") |
|
150 |
.append(proxyAddress.getPort()); |
|
151 |
String urlString = sb.toString(); |
|
152 |
return new URLPermission(urlString, "CONNECT"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
153 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
154 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
155 |
/** |
48083 | 156 |
* 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
|
157 |
*/ |
48083 | 158 |
public static URLPermission permissionForServer(URI uri, |
159 |
String method, |
|
160 |
Stream<String> headers) { |
|
161 |
String urlString = new StringBuilder() |
|
162 |
.append(uri.getScheme()).append("://") |
|
163 |
.append(uri.getAuthority()) |
|
164 |
.append(uri.getPath()).toString(); |
|
165 |
||
166 |
StringBuilder actionStringBuilder = new StringBuilder(method); |
|
167 |
String collected = headers.collect(joining(",")); |
|
168 |
if (!collected.isEmpty()) { |
|
169 |
actionStringBuilder.append(":").append(collected); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
170 |
} |
48083 | 171 |
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
|
172 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
173 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
174 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
175 |
// ABNF primitives defined in RFC 7230 |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
176 |
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
|
177 |
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
|
178 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
179 |
static { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
180 |
char[] allowedTokenChars = |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
181 |
("!#$%&'*+-.^_`|~0123456789" + |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
182 |
"abcdefghijklmnopqrstuvwxyz" + |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
"ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
184 |
for (char c : allowedTokenChars) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
185 |
tchar[c] = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
186 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
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
|
188 |
fieldvchar[c] = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
189 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
190 |
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
|
191 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
193 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
* Validates a RFC 7230 field-name. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
195 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
196 |
public static boolean isValidName(String token) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
197 |
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
|
198 |
char c = token.charAt(i); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
199 |
if (c > 255 || !tchar[c]) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
200 |
return false; |
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 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
203 |
return !token.isEmpty(); |
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 |
|
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
206 |
/** |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
207 |
* 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
|
208 |
* the domain name string. If created with a literal IP address |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
209 |
* then return null. We do this to avoid doing a reverse lookup |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
210 |
* Used to populate the TLS SNI parameter. So, SNI is only set |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
211 |
* when a domain name was supplied. |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
212 |
*/ |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
213 |
public static String getServerName(InetSocketAddress addr) { |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
214 |
String host = addr.getHostString(); |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
215 |
if (IPAddressUtil.textToNumericFormatV4(host) != null) |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
216 |
return null; |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
217 |
if (IPAddressUtil.textToNumericFormatV6(host) != null) |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
218 |
return null; |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
219 |
return host; |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
220 |
} |
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
43730
diff
changeset
|
221 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
222 |
/* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
* Validates a RFC 7230 field-value. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
* "Obsolete line folding" rule |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
* obs-fold = CRLF 1*( SP / HTAB ) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
* is not permitted! |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
public static boolean isValidValue(String token) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
232 |
boolean accepted = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
233 |
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
|
234 |
char c = token.charAt(i); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
if (c > 255) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
236 |
return false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
if (accepted) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
239 |
if (c == ' ' || c == '\t') { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
240 |
accepted = false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
241 |
} else if (!fieldvchar[c]) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
return false; // forbidden byte |
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 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
245 |
if (c != ' ' && c != '\t') { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
246 |
if (fieldvchar[c]) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
247 |
accepted = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
248 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
249 |
return false; // forbidden byte |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
250 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
251 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
252 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
253 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
254 |
return accepted; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
255 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
256 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
257 |
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
|
258 |
return AccessController.doPrivileged((PrivilegedAction<Integer>) () -> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
259 |
NetProperties.getInteger(name, defaultValue)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
260 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
261 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
262 |
static String getNetProperty(String name) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
263 |
return AccessController.doPrivileged((PrivilegedAction<String>) () -> |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
264 |
NetProperties.get(name)); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
265 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
266 |
|
48083 | 267 |
static boolean getBooleanProperty(String name, boolean def) { |
268 |
return AccessController.doPrivileged((PrivilegedAction<Boolean>) () -> |
|
269 |
Boolean.parseBoolean(System.getProperty(name, String.valueOf(def)))); |
|
270 |
} |
|
271 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
272 |
public static SSLParameters copySSLParameters(SSLParameters p) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
273 |
SSLParameters p1 = new SSLParameters(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
274 |
p1.setAlgorithmConstraints(p.getAlgorithmConstraints()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
275 |
p1.setCipherSuites(p.getCipherSuites()); |
43730 | 276 |
// JDK 8 EXCL START |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
277 |
p1.setEnableRetransmissions(p.getEnableRetransmissions()); |
43730 | 278 |
p1.setMaximumPacketSize(p.getMaximumPacketSize()); |
279 |
// JDK 8 EXCL END |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
280 |
p1.setEndpointIdentificationAlgorithm(p.getEndpointIdentificationAlgorithm()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
281 |
p1.setNeedClientAuth(p.getNeedClientAuth()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
282 |
String[] protocols = p.getProtocols(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
283 |
if (protocols != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
284 |
p1.setProtocols(protocols.clone()); |
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 |
p1.setSNIMatchers(p.getSNIMatchers()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
287 |
p1.setServerNames(p.getServerNames()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
288 |
p1.setUseCipherSuitesOrder(p.getUseCipherSuitesOrder()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
289 |
p1.setWantClientAuth(p.getWantClientAuth()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
290 |
return p1; |
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 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
294 |
* 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
|
295 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
296 |
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
|
297 |
buffer.limit(buffer.position()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
298 |
buffer.position(mark); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
299 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
300 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
301 |
public static String stackTrace(Throwable t) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
String s = null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
305 |
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
|
306 |
t.printStackTrace(p); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
307 |
s = bos.toString("US-ASCII"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
} catch (UnsupportedEncodingException ex) { |
48083 | 309 |
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
|
310 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
311 |
return s; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
312 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
313 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
314 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
315 |
* 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
|
316 |
* Return number of bytes copied |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
317 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
318 |
public static int copy(ByteBuffer src, ByteBuffer dst) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
319 |
int srcLen = src.remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
320 |
int dstLen = dst.remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
321 |
if (srcLen > dstLen) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
322 |
int diff = srcLen - dstLen; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
323 |
int limit = src.limit(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
324 |
src.limit(limit - diff); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
325 |
dst.put(src); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
326 |
src.limit(limit); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
327 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
328 |
dst.put(src); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
330 |
return srcLen - src.remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
331 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
332 |
|
48083 | 333 |
/** Threshold beyond which data is no longer copied into the current |
334 |
* buffer, if that buffer has enough unused space. */ |
|
335 |
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
|
336 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
337 |
/** |
48083 | 338 |
* Adds the data from buffersToAdd to currentList. Either 1) appends the |
339 |
* data from a particular buffer to the last buffer in the list ( if |
|
340 |
* there is enough unused space ), or 2) adds it to the list. |
|
341 |
* |
|
342 |
* @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
|
343 |
*/ |
48083 | 344 |
public static long accumulateBuffers(List<ByteBuffer> currentList, |
345 |
List<ByteBuffer> buffersToAdd) { |
|
346 |
long accumulatedBytes = 0; |
|
347 |
for (ByteBuffer bufferToAdd : buffersToAdd) { |
|
348 |
int remaining = bufferToAdd.remaining(); |
|
349 |
if (remaining <= 0) |
|
350 |
continue; |
|
351 |
int listSize = currentList.size(); |
|
352 |
if (listSize == 0) { |
|
353 |
currentList.add(bufferToAdd); |
|
354 |
accumulatedBytes = remaining; |
|
355 |
continue; |
|
356 |
} |
|
357 |
||
358 |
ByteBuffer lastBuffer = currentList.get(listSize - 1); |
|
359 |
int freeSpace = lastBuffer.capacity() - lastBuffer.limit(); |
|
360 |
if (remaining <= COPY_THRESHOLD && freeSpace >= remaining) { |
|
361 |
// append the new data to the unused space in the last buffer |
|
362 |
int position = lastBuffer.position(); |
|
363 |
int limit = lastBuffer.limit(); |
|
364 |
lastBuffer.position(limit); |
|
365 |
lastBuffer.limit(limit + remaining); |
|
366 |
lastBuffer.put(bufferToAdd); |
|
367 |
lastBuffer.position(position); |
|
368 |
} else { |
|
369 |
currentList.add(bufferToAdd); |
|
370 |
} |
|
371 |
accumulatedBytes += remaining; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
372 |
} |
48083 | 373 |
return accumulatedBytes; |
42460
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
376 |
public static ByteBuffer copy(ByteBuffer src) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
377 |
ByteBuffer dst = ByteBuffer.allocate(src.remaining()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
378 |
dst.put(src); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
379 |
dst.flip(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
380 |
return dst; |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
383 |
public static String dump(Object... objects) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
384 |
return Arrays.toString(objects); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
385 |
} |
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 |
public static String stringOf(Collection<?> source) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
388 |
// 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
|
389 |
// 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
|
390 |
return Arrays.toString(source.toArray()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
391 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
392 |
|
48083 | 393 |
public static long remaining(ByteBuffer[] bufs) { |
394 |
long remain = 0; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
395 |
for (ByteBuffer buf : bufs) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
396 |
remain += buf.remaining(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
397 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
398 |
return remain; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
399 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
400 |
|
48083 | 401 |
public static boolean hasRemaining(List<ByteBuffer> bufs) { |
402 |
synchronized (bufs) { |
|
403 |
for (ByteBuffer buf : bufs) { |
|
404 |
if (buf.hasRemaining()) |
|
405 |
return true; |
|
406 |
} |
|
407 |
} |
|
408 |
return false; |
|
409 |
} |
|
410 |
||
411 |
public static long remaining(List<ByteBuffer> bufs) { |
|
412 |
long remain = 0; |
|
413 |
synchronized (bufs) { |
|
414 |
for (ByteBuffer buf : bufs) { |
|
415 |
remain += buf.remaining(); |
|
416 |
} |
|
42460
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 |
return remain; |
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 |
|
48083 | 421 |
public static int remaining(List<ByteBuffer> bufs, int max) { |
422 |
long remain = 0; |
|
423 |
synchronized (bufs) { |
|
424 |
for (ByteBuffer buf : bufs) { |
|
425 |
remain += buf.remaining(); |
|
426 |
if (remain > max) { |
|
427 |
throw new IllegalArgumentException("too many bytes"); |
|
428 |
} |
|
429 |
} |
|
430 |
} |
|
431 |
return (int) remain; |
|
432 |
} |
|
433 |
||
434 |
public static long remaining(ByteBufferReference[] refs) { |
|
435 |
long remain = 0; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
436 |
for (ByteBufferReference ref : refs) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
437 |
remain += ref.get().remaining(); |
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 |
return remain; |
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 |
|
48083 | 442 |
public static int remaining(ByteBufferReference[] refs, int max) { |
443 |
long remain = 0; |
|
444 |
for (ByteBufferReference ref : refs) { |
|
445 |
remain += ref.get().remaining(); |
|
446 |
if (remain > max) { |
|
447 |
throw new IllegalArgumentException("too many bytes"); |
|
448 |
} |
|
449 |
} |
|
450 |
return (int) remain; |
|
451 |
} |
|
452 |
||
453 |
public static int remaining(ByteBuffer[] refs, int max) { |
|
454 |
long remain = 0; |
|
455 |
for (ByteBuffer b : refs) { |
|
456 |
remain += b.remaining(); |
|
457 |
if (remain > max) { |
|
458 |
throw new IllegalArgumentException("too many bytes"); |
|
459 |
} |
|
460 |
} |
|
461 |
return (int) remain; |
|
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
464 |
public static void close(Closeable... closeables) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
465 |
for (Closeable c : closeables) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
466 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
467 |
c.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
468 |
} catch (IOException ignored) { } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
469 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
470 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
471 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
472 |
// 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
|
473 |
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
|
474 |
public static final ByteBuffer[] EMPTY_BB_ARRAY = new ByteBuffer[0]; |
48083 | 475 |
public static final List<ByteBuffer> EMPTY_BB_LIST = List.of(); |
476 |
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
|
477 |
|
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
|
478 |
/** |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
479 |
* 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
|
480 |
* 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
|
481 |
* ( 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
|
482 |
* 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
|
483 |
* 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
|
484 |
*/ |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
485 |
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
|
486 |
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
|
487 |
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
|
488 |
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
|
489 |
// 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
|
490 |
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
|
491 |
} 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
|
492 |
// 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
|
493 |
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
|
494 |
} |
89a688549f5d
http-client-branch: Import of 8195823: Buffers given to response body subscribers should not contain unprocessed HTTP data
chegar
parents:
56033
diff
changeset
|
495 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
496 |
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
|
497 |
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
|
498 |
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
|
499 |
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
|
500 |
return newb; |
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 |
|
43730 | 503 |
/** |
504 |
* Get the Charset from the Content-encoding header. Defaults to |
|
505 |
* UTF_8 |
|
506 |
*/ |
|
507 |
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
|
508 |
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
|
509 |
.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
|
510 |
int i = type.indexOf(";"); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
511 |
if (i >= 0) type = type.substring(i+1); |
43730 | 512 |
try { |
56009
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
513 |
HeaderParser parser = new HeaderParser(type); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
514 |
String value = parser.findValue("charset"); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
515 |
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
|
516 |
return Charset.forName(value); |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
517 |
} catch (Throwable x) { |
cf8792f51dee
http-client-branch: Adds some convenience methods to parse body responses as lines
dfuchs
parents:
55973
diff
changeset
|
518 |
Log.logTrace("Can't find charset in \"{0}\" ({1})", type, x); |
43730 | 519 |
return StandardCharsets.UTF_8; |
520 |
} |
|
521 |
} |
|
522 |
||
523 |
public static UncheckedIOException unchecked(IOException e) { |
|
524 |
return new UncheckedIOException(e); |
|
525 |
} |
|
48083 | 526 |
|
527 |
/** |
|
528 |
* Get a logger for debug HTTP traces. |
|
529 |
* |
|
530 |
* The logger should only be used with levels whose severity is |
|
531 |
* {@code <= DEBUG}. By default, this logger will forward all messages |
|
532 |
* logged to an internal logger named "jdk.internal.httpclient.debug". |
|
533 |
* In addition, if the property -Djdk.internal.httpclient.debug=true is set, |
|
534 |
* it will print the messages on stderr. |
|
535 |
* The logger will add some decoration to the printed message, in the form of |
|
536 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
537 |
* |
|
538 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
539 |
* (e.g: "SocketTube(3)", or "Http2Connection(SocketTube(3))") |
|
540 |
* |
|
541 |
* @return A logger for HTTP internal debug traces |
|
542 |
*/ |
|
543 |
public static Logger getDebugLogger(Supplier<String> dbgTag) { |
|
544 |
return getDebugLogger(dbgTag, DEBUG); |
|
545 |
} |
|
546 |
||
547 |
/** |
|
548 |
* Get a logger for debug HTTP traces.The logger should only be used |
|
549 |
* with levels whose severity is {@code <= DEBUG}. |
|
550 |
* |
|
551 |
* By default, this logger will forward all messages logged to an internal |
|
552 |
* logger named "jdk.internal.httpclient.debug". |
|
553 |
* In addition, if the message severity level is >= to |
|
554 |
* the provided {@code errLevel} it will print the messages on stderr. |
|
555 |
* The logger will add some decoration to the printed message, in the form of |
|
556 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
557 |
* |
|
558 |
* @apiNote To obtain a logger that will always print things on stderr in |
|
559 |
* addition to forwarding to the internal logger, use |
|
560 |
* {@code getDebugLogger(this::dbgTag, Level.ALL);}. |
|
561 |
* This is also equivalent to calling |
|
562 |
* {@code getDebugLogger(this::dbgTag, true);}. |
|
563 |
* To obtain a logger that will only forward to the internal logger, |
|
564 |
* use {@code getDebugLogger(this::dbgTag, Level.OFF);}. |
|
565 |
* This is also equivalent to calling |
|
566 |
* {@code getDebugLogger(this::dbgTag, false);}. |
|
567 |
* |
|
568 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
569 |
* (e.g: "SocketTube(3)", or "Http2Connection(SocketTube(3))") |
|
570 |
* @param errLevel The level above which messages will be also printed on |
|
571 |
* stderr (in addition to be forwarded to the internal logger). |
|
572 |
* |
|
573 |
* @return A logger for HTTP internal debug traces |
|
574 |
*/ |
|
575 |
static Logger getDebugLogger(Supplier<String> dbgTag, Level errLevel) { |
|
576 |
return DebugLogger.createHttpLogger(dbgTag, Level.OFF, errLevel); |
|
577 |
} |
|
578 |
||
579 |
/** |
|
580 |
* Get a logger for debug HTTP traces.The logger should only be used |
|
581 |
* with levels whose severity is {@code <= DEBUG}. |
|
582 |
* |
|
583 |
* By default, this logger will forward all messages logged to an internal |
|
584 |
* logger named "jdk.internal.httpclient.debug". |
|
585 |
* In addition, the provided boolean {@code on==true}, it will print the |
|
586 |
* messages on stderr. |
|
587 |
* The logger will add some decoration to the printed message, in the form of |
|
588 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
589 |
* |
|
590 |
* @apiNote To obtain a logger that will always print things on stderr in |
|
591 |
* addition to forwarding to the internal logger, use |
|
592 |
* {@code getDebugLogger(this::dbgTag, true);}. |
|
593 |
* This is also equivalent to calling |
|
594 |
* {@code getDebugLogger(this::dbgTag, Level.ALL);}. |
|
595 |
* To obtain a logger that will only forward to the internal logger, |
|
596 |
* use {@code getDebugLogger(this::dbgTag, false);}. |
|
597 |
* This is also equivalent to calling |
|
598 |
* {@code getDebugLogger(this::dbgTag, Level.OFF);}. |
|
599 |
* |
|
600 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
601 |
* (e.g: "SocketTube(3)", or "Http2Connection(SocketTube(3))") |
|
602 |
* @param on Whether messages should also be printed on |
|
603 |
* stderr (in addition to be forwarded to the internal logger). |
|
604 |
* |
|
605 |
* @return A logger for HTTP internal debug traces |
|
606 |
*/ |
|
607 |
public static Logger getDebugLogger(Supplier<String> dbgTag, boolean on) { |
|
608 |
Level errLevel = on ? Level.ALL : Level.OFF; |
|
609 |
return getDebugLogger(dbgTag, errLevel); |
|
610 |
} |
|
611 |
||
612 |
/** |
|
613 |
* Get a logger for debug HPACK traces.The logger should only be used |
|
614 |
* with levels whose severity is {@code <= DEBUG}. |
|
615 |
* |
|
616 |
* By default, this logger will forward all messages logged to an internal |
|
617 |
* logger named "jdk.internal.httpclient.hpack.debug". |
|
618 |
* In addition, if the message severity level is >= to |
|
619 |
* the provided {@code outLevel} it will print the messages on stdout. |
|
620 |
* The logger will add some decoration to the printed message, in the form of |
|
621 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
622 |
* |
|
623 |
* @apiNote To obtain a logger that will always print things on stdout in |
|
624 |
* addition to forwarding to the internal logger, use |
|
625 |
* {@code getHpackLogger(this::dbgTag, Level.ALL);}. |
|
626 |
* This is also equivalent to calling |
|
627 |
* {@code getHpackLogger(this::dbgTag, true);}. |
|
628 |
* To obtain a logger that will only forward to the internal logger, |
|
629 |
* use {@code getHpackLogger(this::dbgTag, Level.OFF);}. |
|
630 |
* This is also equivalent to calling |
|
631 |
* {@code getHpackLogger(this::dbgTag, false);}. |
|
632 |
* |
|
633 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
634 |
* (e.g: "Http2Connection(SocketTube(3))/hpack.Decoder(3)") |
|
635 |
* @param outLevel The level above which messages will be also printed on |
|
636 |
* stdout (in addition to be forwarded to the internal logger). |
|
637 |
* |
|
638 |
* @return A logger for HPACK internal debug traces |
|
639 |
*/ |
|
640 |
public static Logger getHpackLogger(Supplier<String> dbgTag, Level outLevel) { |
|
641 |
Level errLevel = Level.OFF; |
|
642 |
return DebugLogger.createHpackLogger(dbgTag, outLevel, errLevel); |
|
643 |
} |
|
644 |
||
645 |
/** |
|
646 |
* Get a logger for debug HPACK traces.The logger should only be used |
|
647 |
* with levels whose severity is {@code <= DEBUG}. |
|
648 |
* |
|
649 |
* By default, this logger will forward all messages logged to an internal |
|
650 |
* logger named "jdk.internal.httpclient.hpack.debug". |
|
651 |
* In addition, the provided boolean {@code on==true}, it will print the |
|
652 |
* messages on stdout. |
|
653 |
* The logger will add some decoration to the printed message, in the form of |
|
654 |
* {@code <Level>:[<thread-name>] [<elapsed-time>] <dbgTag>: <formatted message>} |
|
655 |
* |
|
656 |
* @apiNote To obtain a logger that will always print things on stdout in |
|
657 |
* addition to forwarding to the internal logger, use |
|
658 |
* {@code getHpackLogger(this::dbgTag, true);}. |
|
659 |
* This is also equivalent to calling |
|
660 |
* {@code getHpackLogger(this::dbgTag, Level.ALL);}. |
|
661 |
* To obtain a logger that will only forward to the internal logger, |
|
662 |
* use {@code getHpackLogger(this::dbgTag, false);}. |
|
663 |
* This is also equivalent to calling |
|
664 |
* {@code getHpackLogger(this::dbgTag, Level.OFF);}. |
|
665 |
* |
|
666 |
* @param dbgTag A lambda that returns a string that identifies the caller |
|
667 |
* (e.g: "Http2Connection(SocketTube(3))/hpack.Decoder(3)") |
|
668 |
* @param on Whether messages should also be printed on |
|
669 |
* stdout (in addition to be forwarded to the internal logger). |
|
670 |
* |
|
671 |
* @return A logger for HPACK internal debug traces |
|
672 |
*/ |
|
673 |
public static Logger getHpackLogger(Supplier<String> dbgTag, boolean on) { |
|
674 |
Level outLevel = on ? Level.ALL : Level.OFF; |
|
675 |
return getHpackLogger(dbgTag, outLevel); |
|
676 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
677 |
} |