author | chegar |
Sat, 24 Feb 2018 11:27:11 +0000 | |
branch | http-client-branch |
changeset 56167 | 96fa4f49a9ff |
parent 56165 | 8a6065d830b9 |
child 56235 | 6218673d7fa0 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56074
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; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import javax.net.ssl.SSLContext; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import javax.net.ssl.SSLParameters; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.io.IOException; |
48083 | 31 |
import java.lang.System.Logger.Level; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.lang.ref.WeakReference; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.net.Authenticator; |
48083 | 34 |
import java.net.CookieHandler; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.net.ProxySelector; |
48083 | 36 |
import java.nio.channels.CancelledKeyException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.nio.channels.ClosedChannelException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.nio.channels.SelectableChannel; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.nio.channels.SelectionKey; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
import java.nio.channels.Selector; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
41 |
import java.nio.channels.SocketChannel; |
48083 | 42 |
import java.security.AccessControlContext; |
43 |
import java.security.AccessController; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
44 |
import java.security.NoSuchAlgorithmException; |
48083 | 45 |
import java.security.PrivilegedAction; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
46 |
import java.time.Instant; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
import java.time.temporal.ChronoUnit; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
import java.util.ArrayList; |
48083 | 49 |
import java.util.HashSet; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
import java.util.Iterator; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
import java.util.List; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
import java.util.Optional; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
import java.util.Set; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
import java.util.TreeSet; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
import java.util.concurrent.CompletableFuture; |
48083 | 56 |
import java.util.concurrent.ExecutionException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
import java.util.concurrent.Executor; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
import java.util.concurrent.Executors; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
import java.util.concurrent.ThreadFactory; |
48083 | 60 |
import java.util.concurrent.atomic.AtomicInteger; |
61 |
import java.util.concurrent.atomic.AtomicLong; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
import java.util.stream.Stream; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56081
diff
changeset
|
63 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56081
diff
changeset
|
64 |
import java.net.http.HttpRequest; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56081
diff
changeset
|
65 |
import java.net.http.HttpResponse; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56081
diff
changeset
|
66 |
import java.net.http.HttpResponse.BodyHandler; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56081
diff
changeset
|
67 |
import java.net.http.HttpResponse.PushPromiseHandler; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56081
diff
changeset
|
68 |
import java.net.http.WebSocket; |
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
69 |
import jdk.internal.net.http.common.Log; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
70 |
import jdk.internal.net.http.common.Pair; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
71 |
import jdk.internal.net.http.common.Utils; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
72 |
import jdk.internal.net.http.websocket.BuilderImpl; |
48083 | 73 |
import jdk.internal.misc.InnocuousThread; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
75 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
76 |
* Client implementation. Contains all configuration information and also |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
* the selector manager thread which allows async events to be registered |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
* and delivered when they occur. See AsyncEvent. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
class HttpClientImpl extends HttpClient { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
81 |
|
48083 | 82 |
static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag. |
83 |
static final boolean DEBUGELAPSED = Utils.TESTING || DEBUG; // Revisit: temporary dev flag. |
|
84 |
static final boolean DEBUGTIMEOUT = false; // Revisit: temporary dev flag. |
|
85 |
final System.Logger debug = Utils.getDebugLogger(this::dbgString, DEBUG); |
|
86 |
final System.Logger debugelapsed = Utils.getDebugLogger(this::dbgString, DEBUGELAPSED); |
|
87 |
final System.Logger debugtimeout = Utils.getDebugLogger(this::dbgString, DEBUGTIMEOUT); |
|
88 |
static final AtomicLong CLIENT_IDS = new AtomicLong(); |
|
89 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
90 |
// Define the default factory as a static inner class |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
// that embeds all the necessary logic to avoid |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
92 |
// the risk of using a lambda that might keep a reference on the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
93 |
// HttpClient instance from which it was created (helps with |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
94 |
// heapdump analysis). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
private static final class DefaultThreadFactory implements ThreadFactory { |
48083 | 96 |
private final String namePrefix; |
97 |
private final AtomicInteger nextId = new AtomicInteger(); |
|
98 |
||
99 |
DefaultThreadFactory(long clientID) { |
|
100 |
namePrefix = "HttpClient-" + clientID + "-Worker-"; |
|
101 |
} |
|
102 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
103 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
104 |
public Thread newThread(Runnable r) { |
48083 | 105 |
String name = namePrefix + nextId.getAndIncrement(); |
106 |
Thread t; |
|
107 |
if (System.getSecurityManager() == null) { |
|
108 |
t = new Thread(null, r, name, 0, false); |
|
109 |
} else { |
|
110 |
t = InnocuousThread.newThread(name, r); |
|
111 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
112 |
t.setDaemon(true); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
113 |
return t; |
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 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
116 |
|
48083 | 117 |
private final CookieHandler cookieHandler; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
118 |
private final Redirect followRedirects; |
48083 | 119 |
private final Optional<ProxySelector> userProxySelector; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
120 |
private final ProxySelector proxySelector; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
121 |
private final Authenticator authenticator; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
122 |
private final Version version; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
private final ConnectionPool connections; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
124 |
private final Executor executor; |
48083 | 125 |
private final boolean isDefaultExecutor; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
126 |
// Security parameters |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
127 |
private final SSLContext sslContext; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
128 |
private final SSLParameters sslParams; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
129 |
private final SelectorManager selmgr; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
private final FilterFactory filters; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
private final Http2ClientImpl client2; |
48083 | 132 |
private final long id; |
133 |
private final String dbgTag; |
|
134 |
||
135 |
// This reference is used to keep track of the facade HttpClient |
|
136 |
// that was returned to the application code. |
|
137 |
// It makes it possible to know when the application no longer |
|
138 |
// holds any reference to the HttpClient. |
|
139 |
// Unfortunately, this information is not enough to know when |
|
140 |
// to exit the SelectorManager thread. Because of the asynchronous |
|
141 |
// nature of the API, we also need to wait until all pending operations |
|
142 |
// have completed. |
|
143 |
private final WeakReference<HttpClientFacade> facadeRef; |
|
144 |
||
145 |
// This counter keeps track of the number of operations pending |
|
146 |
// on the HttpClient. The SelectorManager thread will wait |
|
147 |
// until there are no longer any pending operations and the |
|
148 |
// facadeRef is cleared before exiting. |
|
149 |
// |
|
150 |
// The pendingOperationCount is incremented every time a send/sendAsync |
|
151 |
// operation is invoked on the HttpClient, and is decremented when |
|
152 |
// the HttpResponse<T> object is returned to the user. |
|
153 |
// However, at this point, the body may not have been fully read yet. |
|
154 |
// This is the case when the response T is implemented as a streaming |
|
155 |
// subscriber (such as an InputStream). |
|
156 |
// |
|
157 |
// To take care of this issue the pendingOperationCount will additionally |
|
158 |
// be incremented/decremented in the following cases: |
|
159 |
// |
|
160 |
// 1. For HTTP/2 it is incremented when a stream is added to the |
|
161 |
// Http2Connection streams map, and decreased when the stream is removed |
|
162 |
// from the map. This should also take care of push promises. |
|
163 |
// 2. For WebSocket the count is increased when creating a |
|
164 |
// DetachedConnectionChannel for the socket, and decreased |
|
165 |
// when the the channel is closed. |
|
166 |
// In addition, the HttpClient facade is passed to the WebSocket builder, |
|
167 |
// (instead of the client implementation delegate). |
|
168 |
// 3. For HTTP/1.1 the count is incremented before starting to parse the body |
|
169 |
// response, and decremented when the parser has reached the end of the |
|
170 |
// response body flow. |
|
171 |
// |
|
172 |
// This should ensure that the selector manager thread remains alive until |
|
173 |
// the response has been fully received or the web socket is closed. |
|
174 |
private final AtomicLong pendingOperationCount = new AtomicLong(); |
|
175 |
private final AtomicLong pendingWebSocketCount = new AtomicLong(); |
|
176 |
private final AtomicLong pendingHttpRequestCount = new AtomicLong(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
177 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
178 |
/** A Set of, deadline first, ordered timeout events. */ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
179 |
private final TreeSet<TimeoutEvent> timeouts; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
180 |
|
48083 | 181 |
/** |
182 |
* This is a bit tricky: |
|
183 |
* 1. an HttpClientFacade has a final HttpClientImpl field. |
|
184 |
* 2. an HttpClientImpl has a final WeakReference<HttpClientFacade> field, |
|
185 |
* where the referent is the facade created for that instance. |
|
186 |
* 3. We cannot just create the HttpClientFacade in the HttpClientImpl |
|
187 |
* constructor, because it would be only weakly referenced and could |
|
188 |
* be GC'ed before we can return it. |
|
189 |
* The solution is to use an instance of SingleFacadeFactory which will |
|
190 |
* allow the caller of new HttpClientImpl(...) to retrieve the facade |
|
191 |
* after the HttpClientImpl has been created. |
|
192 |
*/ |
|
193 |
private static final class SingleFacadeFactory { |
|
194 |
HttpClientFacade facade; |
|
195 |
HttpClientFacade createFacade(HttpClientImpl impl) { |
|
196 |
assert facade == null; |
|
197 |
return (facade = new HttpClientFacade(impl)); |
|
198 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
199 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
200 |
|
48083 | 201 |
static HttpClientFacade create(HttpClientBuilderImpl builder) { |
202 |
SingleFacadeFactory facadeFactory = new SingleFacadeFactory(); |
|
203 |
HttpClientImpl impl = new HttpClientImpl(builder, facadeFactory); |
|
204 |
impl.start(); |
|
205 |
assert facadeFactory.facade != null; |
|
206 |
assert impl.facadeRef.get() == facadeFactory.facade; |
|
207 |
return facadeFactory.facade; |
|
208 |
} |
|
209 |
||
210 |
private HttpClientImpl(HttpClientBuilderImpl builder, |
|
211 |
SingleFacadeFactory facadeFactory) { |
|
212 |
id = CLIENT_IDS.incrementAndGet(); |
|
213 |
dbgTag = "HttpClientImpl(" + id +")"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
214 |
if (builder.sslContext == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
215 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
sslContext = SSLContext.getDefault(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
} catch (NoSuchAlgorithmException ex) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
218 |
throw new InternalError(ex); |
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 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
221 |
sslContext = builder.sslContext; |
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 |
Executor ex = builder.executor; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
if (ex == null) { |
48083 | 225 |
ex = Executors.newCachedThreadPool(new DefaultThreadFactory(id)); |
226 |
isDefaultExecutor = true; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
ex = builder.executor; |
48083 | 229 |
isDefaultExecutor = false; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
} |
48083 | 231 |
facadeRef = new WeakReference<>(facadeFactory.createFacade(this)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
232 |
client2 = new Http2ClientImpl(this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
233 |
executor = ex; |
48083 | 234 |
cookieHandler = builder.cookieHandler; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
followRedirects = builder.followRedirects == null ? |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
236 |
Redirect.NEVER : builder.followRedirects; |
48083 | 237 |
this.userProxySelector = Optional.ofNullable(builder.proxy); |
238 |
this.proxySelector = userProxySelector |
|
239 |
.orElseGet(HttpClientImpl::getDefaultProxySelector); |
|
240 |
debug.log(Level.DEBUG, "proxySelector is %s (user-supplied=%s)", |
|
241 |
this.proxySelector, userProxySelector.isPresent()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
authenticator = builder.authenticator; |
44854
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
44639
diff
changeset
|
243 |
if (builder.version == null) { |
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
44639
diff
changeset
|
244 |
version = HttpClient.Version.HTTP_2; |
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
44639
diff
changeset
|
245 |
} else { |
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
44639
diff
changeset
|
246 |
version = builder.version; |
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
44639
diff
changeset
|
247 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
248 |
if (builder.sslParams == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
249 |
sslParams = getDefaultParams(sslContext); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
250 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
251 |
sslParams = builder.sslParams; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
252 |
} |
48083 | 253 |
connections = new ConnectionPool(id); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
254 |
connections.start(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
255 |
timeouts = new TreeSet<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
256 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
257 |
selmgr = new SelectorManager(this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
258 |
} catch (IOException e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
259 |
// unlikely |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
260 |
throw new InternalError(e); |
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 |
selmgr.setDaemon(true); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
263 |
filters = new FilterFactory(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
264 |
initFilters(); |
48083 | 265 |
assert facadeRef.get() != null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
266 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
267 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
268 |
private void start() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
269 |
selmgr.start(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
270 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
271 |
|
48083 | 272 |
// Called from the SelectorManager thread, just before exiting. |
273 |
// Clears the HTTP/1.1 and HTTP/2 cache, ensuring that the connections |
|
274 |
// that may be still lingering there are properly closed (and their |
|
275 |
// possibly still opened SocketChannel released). |
|
276 |
private void stop() { |
|
277 |
// Clears HTTP/1.1 cache and close its connections |
|
278 |
connections.stop(); |
|
279 |
// Clears HTTP/2 cache and close its connections. |
|
280 |
client2.stop(); |
|
281 |
} |
|
282 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
283 |
private static SSLParameters getDefaultParams(SSLContext ctx) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
284 |
SSLParameters params = ctx.getSupportedSSLParameters(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
285 |
params.setProtocols(new String[]{"TLSv1.2"}); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
286 |
return params; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
287 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
288 |
|
48083 | 289 |
private static ProxySelector getDefaultProxySelector() { |
290 |
PrivilegedAction<ProxySelector> action = ProxySelector::getDefault; |
|
291 |
return AccessController.doPrivileged(action); |
|
292 |
} |
|
293 |
||
294 |
// Returns the facade that was returned to the application code. |
|
295 |
// May be null if that facade is no longer referenced. |
|
296 |
final HttpClientFacade facade() { |
|
297 |
return facadeRef.get(); |
|
298 |
} |
|
299 |
||
300 |
// Increments the pendingOperationCount. |
|
301 |
final long reference() { |
|
302 |
pendingHttpRequestCount.incrementAndGet(); |
|
303 |
return pendingOperationCount.incrementAndGet(); |
|
304 |
} |
|
305 |
||
306 |
// Decrements the pendingOperationCount. |
|
307 |
final long unreference() { |
|
308 |
final long count = pendingOperationCount.decrementAndGet(); |
|
309 |
final long httpCount = pendingHttpRequestCount.decrementAndGet(); |
|
310 |
final long webSocketCount = pendingWebSocketCount.get(); |
|
311 |
if (count == 0 && facade() == null) { |
|
312 |
selmgr.wakeupSelector(); |
|
313 |
} |
|
314 |
assert httpCount >= 0 : "count of HTTP operations < 0"; |
|
315 |
assert webSocketCount >= 0 : "count of WS operations < 0"; |
|
316 |
assert count >= 0 : "count of pending operations < 0"; |
|
317 |
return count; |
|
318 |
} |
|
319 |
||
320 |
// Increments the pendingOperationCount. |
|
321 |
final long webSocketOpen() { |
|
322 |
pendingWebSocketCount.incrementAndGet(); |
|
323 |
return pendingOperationCount.incrementAndGet(); |
|
324 |
} |
|
325 |
||
326 |
// Decrements the pendingOperationCount. |
|
327 |
final long webSocketClose() { |
|
328 |
final long count = pendingOperationCount.decrementAndGet(); |
|
329 |
final long webSocketCount = pendingWebSocketCount.decrementAndGet(); |
|
330 |
final long httpCount = pendingHttpRequestCount.get(); |
|
331 |
if (count == 0 && facade() == null) { |
|
332 |
selmgr.wakeupSelector(); |
|
333 |
} |
|
334 |
assert httpCount >= 0 : "count of HTTP operations < 0"; |
|
335 |
assert webSocketCount >= 0 : "count of WS operations < 0"; |
|
336 |
assert count >= 0 : "count of pending operations < 0"; |
|
337 |
return count; |
|
338 |
} |
|
339 |
||
340 |
// Returns the pendingOperationCount. |
|
341 |
final long referenceCount() { |
|
342 |
return pendingOperationCount.get(); |
|
343 |
} |
|
344 |
||
345 |
// Called by the SelectorManager thread to figure out whether it's time |
|
346 |
// to terminate. |
|
347 |
final boolean isReferenced() { |
|
348 |
HttpClient facade = facade(); |
|
349 |
return facade != null || referenceCount() > 0; |
|
350 |
} |
|
351 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
352 |
/** |
48083 | 353 |
* Wait for activity on given exchange. |
354 |
* The following occurs in the SelectorManager thread. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
355 |
* |
48083 | 356 |
* 1) add to selector |
357 |
* 2) If selector fires for this exchange then |
|
358 |
* call AsyncEvent.handle() |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
* |
48083 | 360 |
* If exchange needs to change interest ops, then call registerEvent() again. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
void registerEvent(AsyncEvent exchange) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
363 |
selmgr.register(exchange); |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
366 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
* Only used from RawChannel to disconnect the channel from |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
368 |
* the selector |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
370 |
void cancelRegistration(SocketChannel s) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
371 |
selmgr.cancel(s); |
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 |
|
48083 | 374 |
/** |
375 |
* Allows an AsyncEvent to modify its interestOps. |
|
376 |
* @param event The modified event. |
|
377 |
*/ |
|
378 |
void eventUpdated(AsyncEvent event) throws ClosedChannelException { |
|
379 |
assert !(event instanceof AsyncTriggerEvent); |
|
380 |
selmgr.eventUpdated(event); |
|
381 |
} |
|
382 |
||
383 |
boolean isSelectorThread() { |
|
384 |
return Thread.currentThread() == selmgr; |
|
385 |
} |
|
42460
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 |
Http2ClientImpl client2() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
388 |
return client2; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
389 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
390 |
|
48083 | 391 |
private void debugCompleted(String tag, long startNanos, HttpRequest req) { |
392 |
if (debugelapsed.isLoggable(Level.DEBUG)) { |
|
393 |
debugelapsed.log(Level.DEBUG, () -> tag + " elapsed " |
|
394 |
+ (System.nanoTime() - startNanos)/1000_000L |
|
395 |
+ " millis for " + req.method() |
|
396 |
+ " to " + req.uri()); |
|
397 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
398 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
399 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
400 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
401 |
public <T> HttpResponse<T> |
48083 | 402 |
send(HttpRequest req, BodyHandler<T> responseHandler) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
403 |
throws IOException, InterruptedException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
404 |
{ |
48083 | 405 |
try { |
406 |
return sendAsync(req, responseHandler).get(); |
|
407 |
} catch (ExecutionException e) { |
|
408 |
Throwable t = e.getCause(); |
|
409 |
if (t instanceof Error) |
|
410 |
throw (Error)t; |
|
411 |
if (t instanceof RuntimeException) |
|
412 |
throw (RuntimeException)t; |
|
413 |
else if (t instanceof IOException) |
|
414 |
throw Utils.getIOException(t); |
|
415 |
else |
|
416 |
throw new InternalError("Unexpected exception", t); |
|
417 |
} |
|
42460
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 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
421 |
public <T> CompletableFuture<HttpResponse<T>> |
48083 | 422 |
sendAsync(HttpRequest userRequest, BodyHandler<T> responseHandler) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
423 |
{ |
56010 | 424 |
return sendAsync(userRequest, responseHandler, null); |
425 |
} |
|
426 |
||
427 |
||
428 |
@Override |
|
429 |
public <T> CompletableFuture<HttpResponse<T>> |
|
430 |
sendAsync(HttpRequest userRequest, |
|
431 |
BodyHandler<T> responseHandler, |
|
432 |
PushPromiseHandler<T> pushPromiseHandler) |
|
433 |
{ |
|
48083 | 434 |
AccessControlContext acc = null; |
435 |
if (System.getSecurityManager() != null) |
|
436 |
acc = AccessController.getContext(); |
|
437 |
||
438 |
// Clone the, possibly untrusted, HttpRequest |
|
439 |
HttpRequestImpl requestImpl = new HttpRequestImpl(userRequest, proxySelector, acc); |
|
440 |
if (requestImpl.method().equals("CONNECT")) |
|
441 |
throw new IllegalArgumentException("Unsupported method CONNECT"); |
|
442 |
||
443 |
long start = DEBUGELAPSED ? System.nanoTime() : 0; |
|
444 |
reference(); |
|
445 |
try { |
|
446 |
debugelapsed.log(Level.DEBUG, "ClientImpl (async) send %s", userRequest); |
|
447 |
||
56010 | 448 |
MultiExchange<T> mex = new MultiExchange<>(userRequest, |
48083 | 449 |
requestImpl, |
450 |
this, |
|
451 |
responseHandler, |
|
56010 | 452 |
pushPromiseHandler, |
48083 | 453 |
acc); |
454 |
CompletableFuture<HttpResponse<T>> res = |
|
455 |
mex.responseAsync().whenComplete((b,t) -> unreference()); |
|
456 |
if (DEBUGELAPSED) { |
|
457 |
res = res.whenComplete( |
|
458 |
(b,t) -> debugCompleted("ClientImpl (async)", start, userRequest)); |
|
459 |
} |
|
460 |
// makes sure that any dependent actions happen in the executor |
|
461 |
if (acc != null) { |
|
462 |
res.whenCompleteAsync((r, t) -> { /* do nothing */}, |
|
463 |
new PrivilegedExecutor(executor, acc)); |
|
464 |
} |
|
465 |
||
466 |
return res; |
|
467 |
} catch(Throwable t) { |
|
468 |
unreference(); |
|
469 |
debugCompleted("ClientImpl (async)", start, userRequest); |
|
470 |
throw t; |
|
471 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
472 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
473 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
474 |
// Main loop for this client's selector |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
475 |
private final static class SelectorManager extends Thread { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
476 |
|
48083 | 477 |
// For testing purposes we have an internal System property that |
478 |
// can control the frequency at which the selector manager will wake |
|
479 |
// up when there are no pending operations. |
|
480 |
// Increasing the frequency (shorter delays) might allow the selector |
|
481 |
// to observe that the facade is no longer referenced and might allow |
|
482 |
// the selector thread to terminate more timely - for when nothing is |
|
483 |
// ongoing it will only check for that condition every NODEADLINE ms. |
|
484 |
// To avoid misuse of the property, the delay that can be specified |
|
485 |
// is comprised between [MIN_NODEADLINE, MAX_NODEADLINE], and its default |
|
486 |
// value if unspecified (or <= 0) is DEF_NODEADLINE = 3000ms |
|
487 |
// The property is -Djdk.httpclient.internal.selector.timeout=<millis> |
|
488 |
private static final int MIN_NODEADLINE = 1000; // ms |
|
489 |
private static final int MAX_NODEADLINE = 1000 * 1200; // ms |
|
490 |
private static final int DEF_NODEADLINE = 3000; // ms |
|
491 |
private static final long NODEADLINE; // default is DEF_NODEADLINE ms |
|
492 |
static { |
|
493 |
// ensure NODEADLINE is initialized with some valid value. |
|
494 |
long deadline = Utils.getIntegerNetProperty( |
|
495 |
"jdk.httpclient.internal.selector.timeout", |
|
496 |
DEF_NODEADLINE); // millis |
|
497 |
if (deadline <= 0) deadline = DEF_NODEADLINE; |
|
498 |
deadline = Math.max(deadline, MIN_NODEADLINE); |
|
499 |
NODEADLINE = Math.min(deadline, MAX_NODEADLINE); |
|
500 |
} |
|
501 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
502 |
private final Selector selector; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
503 |
private volatile boolean closed; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
504 |
private final List<AsyncEvent> registrations; |
48083 | 505 |
private final System.Logger debug; |
506 |
private final System.Logger debugtimeout; |
|
507 |
HttpClientImpl owner; |
|
508 |
ConnectionPool pool; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
509 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
510 |
SelectorManager(HttpClientImpl ref) throws IOException { |
48083 | 511 |
super(null, null, "HttpClient-" + ref.id + "-SelectorManager", 0, false); |
512 |
owner = ref; |
|
513 |
debug = ref.debug; |
|
514 |
debugtimeout = ref.debugtimeout; |
|
515 |
pool = ref.connectionPool(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
516 |
registrations = new ArrayList<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
517 |
selector = Selector.open(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
518 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
519 |
|
48083 | 520 |
void eventUpdated(AsyncEvent e) throws ClosedChannelException { |
521 |
if (Thread.currentThread() == this) { |
|
522 |
SelectionKey key = e.channel().keyFor(selector); |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
523 |
if (key != null && key.isValid()) { |
56081
20c6742e5545
http-client-branch: 8196848: SplitResponseSSL test failing intermitently (but rarely) with HttpClientImpl shutting down.
dfuchs
parents:
56079
diff
changeset
|
524 |
SelectorAttachment sa = (SelectorAttachment) key.attachment(); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
525 |
sa.register(e); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
526 |
} else if (e.interestOps() != 0){ |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
527 |
// We don't care about paused events. |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
528 |
// These are actually handled by |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
529 |
// SelectorAttachment::resetInterestOps later on. |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
530 |
// But if we reach here when trying to resume an |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
531 |
// event then it's better to fail fast. |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
532 |
debug.log(Level.DEBUG, "No key for channel"); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
533 |
e.abort(new IOException("No key for channel")); |
56081
20c6742e5545
http-client-branch: 8196848: SplitResponseSSL test failing intermitently (but rarely) with HttpClientImpl shutting down.
dfuchs
parents:
56079
diff
changeset
|
534 |
} |
48083 | 535 |
} else { |
536 |
register(e); |
|
537 |
} |
|
538 |
} |
|
539 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
540 |
// This returns immediately. So caller not allowed to send/receive |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
541 |
// on connection. |
48083 | 542 |
synchronized void register(AsyncEvent e) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
543 |
registrations.add(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
544 |
selector.wakeup(); |
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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
547 |
synchronized void cancel(SocketChannel e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
548 |
SelectionKey key = e.keyFor(selector); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
549 |
if (key != null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
550 |
key.cancel(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
551 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
552 |
selector.wakeup(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
553 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
554 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
555 |
void wakeupSelector() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
556 |
selector.wakeup(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
557 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
558 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
559 |
synchronized void shutdown() { |
48083 | 560 |
debug.log(Level.DEBUG, "SelectorManager shutting down"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
561 |
closed = true; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
562 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
563 |
selector.close(); |
48083 | 564 |
} catch (IOException ignored) { |
565 |
} finally { |
|
566 |
owner.stop(); |
|
567 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
568 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
569 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
570 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
571 |
public void run() { |
48083 | 572 |
List<Pair<AsyncEvent,IOException>> errorList = new ArrayList<>(); |
573 |
List<AsyncEvent> readyList = new ArrayList<>(); |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
574 |
List<Runnable> resetList = new ArrayList<>(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
575 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
576 |
while (!Thread.currentThread().isInterrupted()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
577 |
synchronized (this) { |
48083 | 578 |
assert errorList.isEmpty(); |
579 |
assert readyList.isEmpty(); |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
580 |
assert resetList.isEmpty(); |
48083 | 581 |
for (AsyncEvent event : registrations) { |
582 |
if (event instanceof AsyncTriggerEvent) { |
|
583 |
readyList.add(event); |
|
584 |
continue; |
|
585 |
} |
|
586 |
SelectableChannel chan = event.channel(); |
|
587 |
SelectionKey key = null; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
588 |
try { |
48083 | 589 |
key = chan.keyFor(selector); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
590 |
SelectorAttachment sa; |
45531
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
591 |
if (key == null || !key.isValid()) { |
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
592 |
if (key != null) { |
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
593 |
// key is canceled. |
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
594 |
// invoke selectNow() to purge it |
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
595 |
// before registering the new event. |
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
596 |
selector.selectNow(); |
fb3dbffad37b
8180044: java/net/httpclient/ManyRequests.java failed due to timeout
dfuchs
parents:
44854
diff
changeset
|
597 |
} |
48083 | 598 |
sa = new SelectorAttachment(chan, selector); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
599 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
600 |
sa = (SelectorAttachment) key.attachment(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
601 |
} |
48083 | 602 |
// may throw IOE if channel closed: that's OK |
603 |
sa.register(event); |
|
604 |
if (!chan.isOpen()) { |
|
605 |
throw new IOException("Channel closed"); |
|
606 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
607 |
} catch (IOException e) { |
48083 | 608 |
Log.logTrace("HttpClientImpl: " + e); |
609 |
debug.log(Level.DEBUG, () -> |
|
610 |
"Got " + e.getClass().getName() |
|
611 |
+ " while handling" |
|
612 |
+ " registration events"); |
|
613 |
chan.close(); |
|
614 |
// let the event abort deal with it |
|
615 |
errorList.add(new Pair<>(event, e)); |
|
616 |
if (key != null) { |
|
617 |
key.cancel(); |
|
618 |
selector.selectNow(); |
|
619 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
620 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
621 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
622 |
registrations.clear(); |
48083 | 623 |
selector.selectedKeys().clear(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
624 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
625 |
|
48083 | 626 |
for (AsyncEvent event : readyList) { |
627 |
assert event instanceof AsyncTriggerEvent; |
|
628 |
event.handle(); |
|
629 |
} |
|
630 |
readyList.clear(); |
|
631 |
||
632 |
for (Pair<AsyncEvent,IOException> error : errorList) { |
|
633 |
// an IOException was raised and the channel closed. |
|
634 |
handleEvent(error.first, error.second); |
|
635 |
} |
|
636 |
errorList.clear(); |
|
637 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
638 |
// Check whether client is still alive, and if not, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
639 |
// gracefully stop this thread |
48083 | 640 |
if (!owner.isReferenced()) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
641 |
Log.logTrace("HttpClient no longer referenced. Exiting..."); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
642 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
643 |
} |
48083 | 644 |
|
645 |
// Timeouts will have milliseconds granularity. It is important |
|
646 |
// to handle them in a timely fashion. |
|
647 |
long nextTimeout = owner.purgeTimeoutsAndReturnNextDeadline(); |
|
648 |
debugtimeout.log(Level.DEBUG, "next timeout: %d", nextTimeout); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
649 |
|
48083 | 650 |
// Keep-alive have seconds granularity. It's not really an |
651 |
// issue if we keep connections linger a bit more in the keep |
|
652 |
// alive cache. |
|
653 |
long nextExpiry = pool.purgeExpiredConnectionsAndReturnNextDeadline(); |
|
654 |
debugtimeout.log(Level.DEBUG, "next expired: %d", nextExpiry); |
|
655 |
||
656 |
assert nextTimeout >= 0; |
|
657 |
assert nextExpiry >= 0; |
|
658 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
659 |
// Don't wait for ever as it might prevent the thread to |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
660 |
// stop gracefully. millis will be 0 if no deadline was found. |
48083 | 661 |
if (nextTimeout <= 0) nextTimeout = NODEADLINE; |
662 |
||
663 |
// Clip nextExpiry at NODEADLINE limit. The default |
|
664 |
// keep alive is 1200 seconds (half an hour) - we don't |
|
665 |
// want to wait that long. |
|
666 |
if (nextExpiry <= 0) nextExpiry = NODEADLINE; |
|
667 |
else nextExpiry = Math.min(NODEADLINE, nextExpiry); |
|
668 |
||
669 |
// takes the least of the two. |
|
670 |
long millis = Math.min(nextExpiry, nextTimeout); |
|
671 |
||
672 |
debugtimeout.log(Level.DEBUG, "Next deadline is %d", |
|
673 |
(millis == 0 ? NODEADLINE : millis)); |
|
674 |
//debugPrint(selector); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
675 |
int n = selector.select(millis == 0 ? NODEADLINE : millis); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
676 |
if (n == 0) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
677 |
// Check whether client is still alive, and if not, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
678 |
// gracefully stop this thread |
48083 | 679 |
if (!owner.isReferenced()) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
680 |
Log.logTrace("HttpClient no longer referenced. Exiting..."); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
681 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
682 |
} |
48083 | 683 |
owner.purgeTimeoutsAndReturnNextDeadline(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
684 |
continue; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
685 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
686 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
687 |
Set<SelectionKey> keys = selector.selectedKeys(); |
48083 | 688 |
assert errorList.isEmpty(); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
689 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
690 |
for (SelectionKey key : keys) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
691 |
SelectorAttachment sa = (SelectorAttachment) key.attachment(); |
48083 | 692 |
if (!key.isValid()) { |
693 |
IOException ex = sa.chan.isOpen() |
|
694 |
? new IOException("Invalid key") |
|
695 |
: new ClosedChannelException(); |
|
696 |
sa.pending.forEach(e -> errorList.add(new Pair<>(e,ex))); |
|
697 |
sa.pending.clear(); |
|
698 |
continue; |
|
699 |
} |
|
700 |
||
701 |
int eventsOccurred; |
|
702 |
try { |
|
703 |
eventsOccurred = key.readyOps(); |
|
704 |
} catch (CancelledKeyException ex) { |
|
705 |
IOException io = Utils.getIOException(ex); |
|
706 |
sa.pending.forEach(e -> errorList.add(new Pair<>(e,io))); |
|
707 |
sa.pending.clear(); |
|
708 |
continue; |
|
709 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
710 |
sa.events(eventsOccurred).forEach(readyList::add); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
711 |
resetList.add(() -> sa.resetInterestOps(eventsOccurred)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
712 |
} |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
713 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
714 |
selector.selectNow(); // complete cancellation |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
715 |
selector.selectedKeys().clear(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
716 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
717 |
// handle selected events |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
718 |
readyList.forEach((e) -> handleEvent(e, null)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
719 |
readyList.clear(); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
720 |
|
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
721 |
// handle errors (closed channels etc...) |
48083 | 722 |
errorList.forEach((p) -> handleEvent(p.first, p.second)); |
723 |
errorList.clear(); |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
724 |
|
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
725 |
// reset interest ops for selected channels |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
726 |
resetList.forEach(r -> r.run()); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
727 |
resetList.clear(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
728 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
729 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
730 |
} catch (Throwable e) { |
48083 | 731 |
//e.printStackTrace(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
732 |
if (!closed) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
733 |
// This terminates thread. So, better just print stack trace |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
734 |
String err = Utils.stackTrace(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
735 |
Log.logError("HttpClientImpl: fatal error: " + err); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
736 |
} |
48083 | 737 |
debug.log(Level.DEBUG, "shutting down", e); |
738 |
if (Utils.ASSERTIONSENABLED && !debug.isLoggable(Level.DEBUG)) { |
|
739 |
e.printStackTrace(System.err); // always print the stack |
|
740 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
741 |
} finally { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
742 |
shutdown(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
743 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
744 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
745 |
|
48083 | 746 |
// void debugPrint(Selector selector) { |
747 |
// System.err.println("Selector: debugprint start"); |
|
748 |
// Set<SelectionKey> keys = selector.keys(); |
|
749 |
// for (SelectionKey key : keys) { |
|
750 |
// SelectableChannel c = key.channel(); |
|
751 |
// int ops = key.interestOps(); |
|
752 |
// System.err.printf("selector chan:%s ops:%d\n", c, ops); |
|
753 |
// } |
|
754 |
// System.err.println("Selector: debugprint end"); |
|
755 |
// } |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
756 |
|
48083 | 757 |
/** Handles the given event. The given ioe may be null. */ |
758 |
void handleEvent(AsyncEvent event, IOException ioe) { |
|
759 |
if (closed || ioe != null) { |
|
760 |
event.abort(ioe); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
761 |
} else { |
48083 | 762 |
event.handle(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
763 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
764 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
765 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
766 |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
767 |
final String debugInterestOps(SelectableChannel channel) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
768 |
try { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
769 |
SelectionKey key = channel.keyFor(selmgr.selector); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
770 |
if (key == null) return "channel not registered with selector"; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
771 |
String keyInterestOps = key.isValid() |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
772 |
? "key.interestOps=" + key.interestOps() : "invalid key"; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
773 |
return String.format("channel registered with selector, %s, sa.interestOps=%s", |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
774 |
keyInterestOps, |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
775 |
((SelectorAttachment)key.attachment()).interestOps); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
776 |
} catch (Throwable t) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
777 |
return String.valueOf(t); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
778 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
779 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
780 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
781 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
782 |
* Tracks multiple user level registrations associated with one NIO |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
783 |
* registration (SelectionKey). In this implementation, registrations |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
784 |
* are one-off and when an event is posted the registration is cancelled |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
785 |
* until explicitly registered again. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
786 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
787 |
* <p> No external synchronization required as this class is only used |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
788 |
* by the SelectorManager thread. One of these objects required per |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
789 |
* connection. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
790 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
791 |
private static class SelectorAttachment { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
792 |
private final SelectableChannel chan; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
793 |
private final Selector selector; |
48083 | 794 |
private final Set<AsyncEvent> pending; |
795 |
private final static System.Logger debug = |
|
796 |
Utils.getDebugLogger("SelectorAttachment"::toString, DEBUG); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
797 |
private int interestOps; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
798 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
799 |
SelectorAttachment(SelectableChannel chan, Selector selector) { |
48083 | 800 |
this.pending = new HashSet<>(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
801 |
this.chan = chan; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
802 |
this.selector = selector; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
803 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
804 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
805 |
void register(AsyncEvent e) throws ClosedChannelException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
806 |
int newOps = e.interestOps(); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
807 |
// re register interest if we are not already interested |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
808 |
// in the event. If the event is paused, then the pause will |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
809 |
// be taken into account later when resetInterestOps is called. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
810 |
boolean reRegister = (interestOps & newOps) != newOps; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
811 |
interestOps |= newOps; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
812 |
pending.add(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
813 |
if (reRegister) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
814 |
// first time registration happens here also |
56081
20c6742e5545
http-client-branch: 8196848: SplitResponseSSL test failing intermitently (but rarely) with HttpClientImpl shutting down.
dfuchs
parents:
56079
diff
changeset
|
815 |
try { |
20c6742e5545
http-client-branch: 8196848: SplitResponseSSL test failing intermitently (but rarely) with HttpClientImpl shutting down.
dfuchs
parents:
56079
diff
changeset
|
816 |
chan.register(selector, interestOps, this); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
817 |
} catch (Throwable x) { |
56081
20c6742e5545
http-client-branch: 8196848: SplitResponseSSL test failing intermitently (but rarely) with HttpClientImpl shutting down.
dfuchs
parents:
56079
diff
changeset
|
818 |
abortPending(x); |
20c6742e5545
http-client-branch: 8196848: SplitResponseSSL test failing intermitently (but rarely) with HttpClientImpl shutting down.
dfuchs
parents:
56079
diff
changeset
|
819 |
} |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
820 |
} else if (!chan.isOpen()) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
821 |
abortPending(new ClosedChannelException()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
822 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
823 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
824 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
825 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
826 |
* Returns a Stream<AsyncEvents> containing only events that are |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
827 |
* registered with the given {@code interestOps}. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
828 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
829 |
Stream<AsyncEvent> events(int interestOps) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
830 |
return pending.stream() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
831 |
.filter(ev -> (ev.interestOps() & interestOps) != 0); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
832 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
833 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
834 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
835 |
* Removes any events with the given {@code interestOps}, and if no |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
836 |
* events remaining, cancels the associated SelectionKey. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
837 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
838 |
void resetInterestOps(int interestOps) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
839 |
int newOps = 0; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
840 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
841 |
Iterator<AsyncEvent> itr = pending.iterator(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
842 |
while (itr.hasNext()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
843 |
AsyncEvent event = itr.next(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
844 |
int evops = event.interestOps(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
845 |
if (event.repeating()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
846 |
newOps |= evops; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
847 |
continue; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
848 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
849 |
if ((evops & interestOps) != 0) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
850 |
itr.remove(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
851 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
852 |
newOps |= evops; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
853 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
854 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
855 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
856 |
this.interestOps = newOps; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
857 |
SelectionKey key = chan.keyFor(selector); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
858 |
if (newOps == 0 && key != null && pending.isEmpty()) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
859 |
key.cancel(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
860 |
} else { |
48083 | 861 |
try { |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
862 |
if (key == null || !key.isValid()) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
863 |
throw new CancelledKeyException(); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
864 |
} |
48083 | 865 |
key.interestOps(newOps); |
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
866 |
// double check after |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
867 |
if (!chan.isOpen()) { |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
868 |
abortPending(new ClosedChannelException()); |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
869 |
return; |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
870 |
} |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56092
diff
changeset
|
871 |
assert key.interestOps() == newOps; |
48083 | 872 |
} catch (CancelledKeyException x) { |
873 |
// channel may have been closed |
|
874 |
debug.log(Level.DEBUG, "key cancelled for " + chan); |
|
875 |
abortPending(x); |
|
876 |
} |
|
877 |
} |
|
878 |
} |
|
879 |
||
880 |
void abortPending(Throwable x) { |
|
881 |
if (!pending.isEmpty()) { |
|
882 |
AsyncEvent[] evts = pending.toArray(new AsyncEvent[0]); |
|
883 |
pending.clear(); |
|
884 |
IOException io = Utils.getIOException(x); |
|
885 |
for (AsyncEvent event : evts) { |
|
886 |
event.abort(io); |
|
887 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
888 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
889 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
890 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
891 |
|
48083 | 892 |
/*package-private*/ SSLContext theSSLContext() { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
893 |
return sslContext; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
894 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
895 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
896 |
@Override |
48083 | 897 |
public SSLContext sslContext() { |
898 |
return sslContext; |
|
899 |
} |
|
900 |
||
901 |
@Override |
|
902 |
public SSLParameters sslParameters() { |
|
903 |
return Utils.copySSLParameters(sslParams); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
904 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
905 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
906 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
907 |
public Optional<Authenticator> authenticator() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
908 |
return Optional.ofNullable(authenticator); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
909 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
910 |
|
48083 | 911 |
/*package-private*/ final Executor theExecutor() { |
912 |
return executor; |
|
913 |
} |
|
914 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
915 |
@Override |
48083 | 916 |
public final Optional<Executor> executor() { |
917 |
return isDefaultExecutor ? Optional.empty() : Optional.of(executor); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
918 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
919 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
920 |
ConnectionPool connectionPool() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
921 |
return connections; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
922 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
923 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
924 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
925 |
public Redirect followRedirects() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
926 |
return followRedirects; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
927 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
928 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
929 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
930 |
@Override |
48083 | 931 |
public Optional<CookieHandler> cookieHandler() { |
932 |
return Optional.ofNullable(cookieHandler); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
933 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
934 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
935 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
936 |
public Optional<ProxySelector> proxy() { |
48083 | 937 |
return this.userProxySelector; |
938 |
} |
|
939 |
||
940 |
// Return the effective proxy that this client uses. |
|
941 |
ProxySelector proxySelector() { |
|
942 |
return proxySelector; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
943 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
944 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
945 |
@Override |
48083 | 946 |
public WebSocket.Builder newWebSocketBuilder() { |
947 |
// Make sure to pass the HttpClientFacade to the WebSocket builder. |
|
948 |
// This will ensure that the facade is not released before the |
|
949 |
// WebSocket has been created, at which point the pendingOperationCount |
|
950 |
// will have been incremented by the DetachedConnectionChannel |
|
951 |
// (see PlainHttpConnection.detachChannel()) |
|
952 |
return new BuilderImpl(this.facade(), proxySelector); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
953 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
954 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
955 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
956 |
public Version version() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
957 |
return version; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
958 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
959 |
|
48083 | 960 |
String dbgString() { |
961 |
return dbgTag; |
|
962 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
963 |
|
48083 | 964 |
@Override |
965 |
public String toString() { |
|
966 |
// Used by tests to get the client's id and compute the |
|
967 |
// name of the SelectorManager thread. |
|
968 |
return super.toString() + ("(" + id + ")"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
969 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
970 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
971 |
private void initFilters() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
972 |
addFilter(AuthenticationFilter.class); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
973 |
addFilter(RedirectFilter.class); |
48083 | 974 |
if (this.cookieHandler != null) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
975 |
addFilter(CookieFilter.class); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
976 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
977 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
978 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
979 |
private void addFilter(Class<? extends HeaderFilter> f) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
980 |
filters.addFilter(f); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
981 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
982 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
983 |
final List<HeaderFilter> filterChain() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
984 |
return filters.getFilterChain(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
985 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
986 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
987 |
// Timer controls. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
988 |
// Timers are implemented through timed Selector.select() calls. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
989 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
990 |
synchronized void registerTimer(TimeoutEvent event) { |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
991 |
Log.logTrace("Registering timer {0}", event); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
992 |
timeouts.add(event); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
993 |
selmgr.wakeupSelector(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
994 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
995 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
996 |
synchronized void cancelTimer(TimeoutEvent event) { |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
997 |
Log.logTrace("Canceling timer {0}", event); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
998 |
timeouts.remove(event); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
999 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1000 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1001 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1002 |
* Purges ( handles ) timer events that have passed their deadline, and |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1003 |
* returns the amount of time, in milliseconds, until the next earliest |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1004 |
* event. A return value of 0 means that there are no events. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1005 |
*/ |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1006 |
private long purgeTimeoutsAndReturnNextDeadline() { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1007 |
long diff = 0L; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1008 |
List<TimeoutEvent> toHandle = null; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1009 |
int remaining = 0; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1010 |
// enter critical section to retrieve the timeout event to handle |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1011 |
synchronized(this) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1012 |
if (timeouts.isEmpty()) return 0L; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1013 |
|
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1014 |
Instant now = Instant.now(); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1015 |
Iterator<TimeoutEvent> itr = timeouts.iterator(); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1016 |
while (itr.hasNext()) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1017 |
TimeoutEvent event = itr.next(); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1018 |
diff = now.until(event.deadline(), ChronoUnit.MILLIS); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1019 |
if (diff <= 0) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1020 |
itr.remove(); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1021 |
toHandle = (toHandle == null) ? new ArrayList<>() : toHandle; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1022 |
toHandle.add(event); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1023 |
} else { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1024 |
break; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1025 |
} |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1026 |
} |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1027 |
remaining = timeouts.size(); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1028 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1029 |
|
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1030 |
// can be useful for debugging |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1031 |
if (toHandle != null && Log.trace()) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1032 |
Log.logTrace("purgeTimeoutsAndReturnNextDeadline: handling " |
56074 | 1033 |
+ toHandle.size() + " events, " |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1034 |
+ "remaining " + remaining |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1035 |
+ ", next deadline: " + (diff < 0 ? 0L : diff)); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1036 |
} |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1037 |
|
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1038 |
// handle timeout events out of critical section |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1039 |
if (toHandle != null) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1040 |
Throwable failed = null; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1041 |
for (TimeoutEvent event : toHandle) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1042 |
try { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1043 |
Log.logTrace("Firing timer {0}", event); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1044 |
event.handle(); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1045 |
} catch (Error | RuntimeException e) { |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1046 |
// Not expected. Handle remaining events then throw... |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1047 |
// If e is an OOME or SOE it might simply trigger a new |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1048 |
// error from here - but in this case there's not much we |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1049 |
// could do anyway. Just let it flow... |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1050 |
if (failed == null) failed = e; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1051 |
else failed.addSuppressed(e); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1052 |
Log.logTrace("Failed to handle event {0}: {1}", event, e); |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1053 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1054 |
} |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1055 |
if (failed instanceof Error) throw (Error) failed; |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1056 |
if (failed instanceof RuntimeException) throw (RuntimeException) failed; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1057 |
} |
44639
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1058 |
|
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1059 |
// return time to wait until next event. 0L if there's no more events. |
5c2838d882a5
8178147: Race conditions in timeout handling code in http/2 incubator client
dfuchs
parents:
43984
diff
changeset
|
1060 |
return diff < 0 ? 0L : diff; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1061 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1062 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1063 |
// used for the connection window |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1064 |
int getReceiveBufferSize() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1065 |
return Utils.getIntegerNetProperty( |
48263
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
1066 |
"jdk.httpclient.receiveBufferSize", 2 * 1024 * 1024 |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1067 |
); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1068 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1069 |
} |