author | michaelm |
Mon, 16 May 2016 16:04:14 +0100 | |
changeset 38322 | f6f9d3ec14ba |
parent 37799 | 635c430d5a99 |
child 39730 | 196f4e25d9f5 |
permissions | -rw-r--r-- |
36131 | 1 |
/* |
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. Oracle designates this |
|
8 |
* particular file as subject to the "Classpath" exception as provided |
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
|
10 |
* |
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
*/ |
|
24 |
package java.net.http; |
|
25 |
||
37720 | 26 |
import javax.net.ssl.SSLContext; |
27 |
import javax.net.ssl.SSLParameters; |
|
36131 | 28 |
import java.io.IOException; |
29 |
import java.net.Authenticator; |
|
30 |
import java.net.CookieManager; |
|
31 |
import java.net.ProxySelector; |
|
32 |
import java.net.URI; |
|
33 |
import java.nio.ByteBuffer; |
|
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
34 |
import java.nio.channels.ClosedChannelException; |
36131 | 35 |
import java.nio.channels.SelectableChannel; |
36 |
import java.nio.channels.SelectionKey; |
|
37 |
import java.nio.channels.Selector; |
|
38322
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
38 |
import java.nio.channels.SocketChannel; |
37720 | 39 |
import java.security.NoSuchAlgorithmException; |
40 |
import java.util.ArrayList; |
|
41 |
import java.util.Iterator; |
|
42 |
import java.util.LinkedList; |
|
43 |
import java.util.List; |
|
44 |
import java.util.ListIterator; |
|
45 |
import java.util.Optional; |
|
46 |
import java.util.Set; |
|
36131 | 47 |
import java.util.concurrent.ExecutorService; |
48 |
import java.util.concurrent.Executors; |
|
49 |
import java.util.concurrent.ThreadFactory; |
|
37720 | 50 |
import java.util.stream.Stream; |
51 |
||
52 |
import static java.net.http.Utils.BUFSIZE; |
|
36131 | 53 |
|
54 |
/** |
|
55 |
* Client implementation. Contains all configuration information and also |
|
56 |
* the selector manager thread which allows async events to be registered |
|
57 |
* and delivered when they occur. See AsyncEvent. |
|
58 |
*/ |
|
59 |
class HttpClientImpl extends HttpClient implements BufferHandler { |
|
60 |
||
37720 | 61 |
private static final ThreadFactory defaultFactory = |
62 |
(r -> new Thread(null, r, "HttpClient_worker", 0, true)); |
|
63 |
||
36131 | 64 |
private final CookieManager cookieManager; |
65 |
private final Redirect followRedirects; |
|
66 |
private final ProxySelector proxySelector; |
|
67 |
private final Authenticator authenticator; |
|
68 |
private final Version version; |
|
69 |
private boolean pipelining = false; |
|
70 |
private final ConnectionPool connections; |
|
71 |
private final ExecutorWrapper executor; |
|
72 |
// Security parameters |
|
73 |
private final SSLContext sslContext; |
|
74 |
private final SSLParameters sslParams; |
|
75 |
private final SelectorManager selmgr; |
|
76 |
private final FilterFactory filters; |
|
77 |
private final Http2ClientImpl client2; |
|
78 |
private final LinkedList<TimeoutEvent> timeouts; |
|
79 |
||
80 |
public static HttpClientImpl create(HttpClientBuilderImpl builder) { |
|
81 |
HttpClientImpl impl = new HttpClientImpl(builder); |
|
82 |
impl.start(); |
|
83 |
return impl; |
|
84 |
} |
|
85 |
||
86 |
private HttpClientImpl(HttpClientBuilderImpl builder) { |
|
87 |
if (builder.sslContext == null) { |
|
88 |
try { |
|
89 |
sslContext = SSLContext.getDefault(); |
|
90 |
} catch (NoSuchAlgorithmException ex) { |
|
91 |
throw new InternalError(ex); |
|
92 |
} |
|
93 |
} else { |
|
94 |
sslContext = builder.sslContext; |
|
95 |
} |
|
96 |
ExecutorService ex = builder.executor; |
|
97 |
if (ex == null) { |
|
98 |
ex = Executors.newCachedThreadPool((r) -> { |
|
99 |
Thread t = defaultFactory.newThread(r); |
|
100 |
t.setDaemon(true); |
|
101 |
return t; |
|
102 |
}); |
|
103 |
} else { |
|
104 |
ex = builder.executor; |
|
105 |
} |
|
106 |
client2 = new Http2ClientImpl(this); |
|
107 |
executor = ExecutorWrapper.wrap(ex); |
|
108 |
cookieManager = builder.cookieManager; |
|
109 |
followRedirects = builder.followRedirects == null ? |
|
110 |
Redirect.NEVER : builder.followRedirects; |
|
111 |
this.proxySelector = builder.proxy; |
|
112 |
authenticator = builder.authenticator; |
|
113 |
version = builder.version; |
|
37799
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
114 |
if (builder.sslParams == null) |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
115 |
sslParams = getDefaultParams(sslContext); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
116 |
else |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
117 |
sslParams = builder.sslParams; |
36131 | 118 |
connections = new ConnectionPool(); |
119 |
connections.start(); |
|
120 |
timeouts = new LinkedList<>(); |
|
121 |
try { |
|
122 |
selmgr = new SelectorManager(); |
|
123 |
} catch (IOException e) { |
|
124 |
// unlikely |
|
125 |
throw new InternalError(e); |
|
126 |
} |
|
127 |
selmgr.setDaemon(true); |
|
128 |
filters = new FilterFactory(); |
|
129 |
initFilters(); |
|
130 |
} |
|
131 |
||
132 |
private void start() { |
|
133 |
selmgr.start(); |
|
134 |
} |
|
135 |
||
37799
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
136 |
private static SSLParameters getDefaultParams(SSLContext ctx) { |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
137 |
SSLParameters params = ctx.getSupportedSSLParameters(); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
138 |
params.setProtocols(new String[]{"TLSv1.2"}); |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
139 |
return params; |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
140 |
} |
635c430d5a99
8153572: [JEP 110] IOException (connection closed for reading) is thrown when try to connect HTTPS service
michaelm
parents:
37720
diff
changeset
|
141 |
|
36131 | 142 |
/** |
143 |
* Wait for activity on given exchange (assuming blocking = false). |
|
144 |
* It's a no-op if blocking = true. In particular, the following occurs |
|
145 |
* in the SelectorManager thread. |
|
146 |
* |
|
147 |
* 1) mark the connection non-blocking |
|
148 |
* 2) add to selector |
|
149 |
* 3) If selector fires for this exchange then |
|
150 |
* 4) - mark connection as blocking |
|
151 |
* 5) - call AsyncEvent.handle() |
|
152 |
* |
|
37720 | 153 |
* If exchange needs to block again, then call registerEvent() again |
36131 | 154 |
*/ |
155 |
void registerEvent(AsyncEvent exchange) throws IOException { |
|
156 |
selmgr.register(exchange); |
|
157 |
} |
|
158 |
||
38322
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
159 |
/** |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
160 |
* Only used from RawChannel to disconnect the channel from |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
161 |
* the selector |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
162 |
*/ |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
163 |
void cancelRegistration(SocketChannel s) { |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
164 |
selmgr.cancel(s); |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
165 |
} |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
166 |
|
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
167 |
|
36131 | 168 |
Http2ClientImpl client2() { |
169 |
return client2; |
|
170 |
} |
|
171 |
||
37720 | 172 |
/** |
173 |
* We keep one size of buffer on free list. That size may increase |
|
174 |
* depending on demand. If that happens we dispose of free buffers |
|
175 |
* that are smaller than new size. |
|
176 |
*/ |
|
177 |
private final LinkedList<ByteBuffer> freelist = new LinkedList<>(); |
|
178 |
int currentSize = BUFSIZE; |
|
36131 | 179 |
|
180 |
@Override |
|
37720 | 181 |
public synchronized ByteBuffer getBuffer(int size) { |
182 |
||
183 |
ByteBuffer buf; |
|
184 |
if (size == -1) |
|
185 |
size = currentSize; |
|
186 |
||
187 |
if (size > currentSize) |
|
188 |
currentSize = size; |
|
189 |
||
190 |
while (!freelist.isEmpty()) { |
|
191 |
buf = freelist.removeFirst(); |
|
192 |
if (buf.capacity() < currentSize) |
|
193 |
continue; |
|
194 |
buf.clear(); |
|
195 |
return buf; |
|
36131 | 196 |
} |
37720 | 197 |
return ByteBuffer.allocate(size); |
36131 | 198 |
} |
199 |
||
200 |
@Override |
|
201 |
public synchronized void returnBuffer(ByteBuffer buffer) { |
|
202 |
freelist.add(buffer); |
|
203 |
} |
|
204 |
||
37720 | 205 |
@Override |
206 |
public synchronized void setMinBufferSize(int n) { |
|
207 |
currentSize = Math.max(n, currentSize); |
|
208 |
} |
|
36131 | 209 |
|
210 |
// Main loop for this client's selector |
|
37720 | 211 |
private final class SelectorManager extends Thread { |
36131 | 212 |
|
37720 | 213 |
private final Selector selector; |
214 |
private volatile boolean closed; |
|
215 |
private final List<AsyncEvent> readyList; |
|
216 |
private final List<AsyncEvent> registrations; |
|
36131 | 217 |
|
218 |
SelectorManager() throws IOException { |
|
37720 | 219 |
super(null, null, "SelectorManager", 0, false); |
220 |
readyList = new ArrayList<>(); |
|
221 |
registrations = new ArrayList<>(); |
|
36131 | 222 |
selector = Selector.open(); |
223 |
} |
|
224 |
||
225 |
// This returns immediately. So caller not allowed to send/receive |
|
226 |
// on connection. |
|
227 |
||
228 |
synchronized void register(AsyncEvent e) throws IOException { |
|
229 |
registrations.add(e); |
|
230 |
selector.wakeup(); |
|
231 |
} |
|
232 |
||
38322
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
233 |
synchronized void cancel(SocketChannel e) { |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
234 |
SelectionKey key = e.keyFor(selector); |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
235 |
if (key != null) |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
236 |
key.cancel(); |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
237 |
selector.wakeup(); |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
238 |
} |
f6f9d3ec14ba
8156825: java/net/httpclient/BasicWebSocketAPITest.java failed with java.lang.AssertionError
michaelm
parents:
37799
diff
changeset
|
239 |
|
36131 | 240 |
void wakeupSelector() { |
241 |
selector.wakeup(); |
|
242 |
} |
|
243 |
||
244 |
synchronized void shutdown() { |
|
245 |
closed = true; |
|
246 |
try { |
|
247 |
selector.close(); |
|
37720 | 248 |
} catch (IOException ignored) { } |
36131 | 249 |
} |
250 |
||
251 |
@Override |
|
252 |
public void run() { |
|
253 |
try { |
|
37720 | 254 |
while (!Thread.currentThread().isInterrupted()) { |
36131 | 255 |
synchronized (this) { |
256 |
for (AsyncEvent exchange : registrations) { |
|
257 |
SelectableChannel c = exchange.channel(); |
|
258 |
try { |
|
259 |
c.configureBlocking(false); |
|
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
260 |
SelectionKey key = c.keyFor(selector); |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
261 |
SelectorAttachment sa; |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
262 |
if (key == null) { |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
263 |
sa = new SelectorAttachment(c, selector); |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
264 |
} else { |
37720 | 265 |
sa = (SelectorAttachment) key.attachment(); |
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
266 |
} |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
267 |
sa.register(exchange); |
36131 | 268 |
} catch (IOException e) { |
269 |
Log.logError("HttpClientImpl: " + e); |
|
270 |
c.close(); |
|
271 |
// let the exchange deal with it |
|
272 |
handleEvent(exchange); |
|
273 |
} |
|
274 |
} |
|
275 |
registrations.clear(); |
|
276 |
} |
|
277 |
long timeval = getTimeoutValue(); |
|
278 |
long now = System.currentTimeMillis(); |
|
37720 | 279 |
//debugPrint(selector); |
36131 | 280 |
int n = selector.select(timeval); |
281 |
if (n == 0) { |
|
282 |
signalTimeouts(now); |
|
283 |
continue; |
|
284 |
} |
|
285 |
Set<SelectionKey> keys = selector.selectedKeys(); |
|
286 |
||
287 |
for (SelectionKey key : keys) { |
|
37720 | 288 |
SelectorAttachment sa = (SelectorAttachment) key.attachment(); |
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
289 |
int eventsOccurred = key.readyOps(); |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
290 |
sa.events(eventsOccurred).forEach(readyList::add); |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
291 |
sa.resetInterestOps(eventsOccurred); |
36131 | 292 |
} |
293 |
selector.selectNow(); // complete cancellation |
|
294 |
selector.selectedKeys().clear(); |
|
295 |
||
296 |
for (AsyncEvent exchange : readyList) { |
|
37720 | 297 |
if (exchange.blocking()) { |
36131 | 298 |
exchange.channel().configureBlocking(true); |
299 |
} |
|
300 |
executor.synchronize(); |
|
301 |
handleEvent(exchange); // will be delegated to executor |
|
302 |
} |
|
303 |
readyList.clear(); |
|
304 |
} |
|
305 |
} catch (Throwable e) { |
|
306 |
if (!closed) { |
|
307 |
// This terminates thread. So, better just print stack trace |
|
308 |
String err = Utils.stackTrace(e); |
|
309 |
Log.logError("HttpClientImpl: fatal error: " + err); |
|
310 |
} |
|
37720 | 311 |
} finally { |
312 |
shutdown(); |
|
36131 | 313 |
} |
314 |
} |
|
315 |
||
37720 | 316 |
void debugPrint(Selector selector) { |
317 |
System.err.println("Selector: debugprint start"); |
|
318 |
Set<SelectionKey> keys = selector.keys(); |
|
319 |
for (SelectionKey key : keys) { |
|
320 |
SelectableChannel c = key.channel(); |
|
321 |
int ops = key.interestOps(); |
|
322 |
System.err.printf("selector chan:%s ops:%d\n", c, ops); |
|
323 |
} |
|
324 |
System.err.println("Selector: debugprint end"); |
|
325 |
} |
|
326 |
||
36131 | 327 |
void handleEvent(AsyncEvent e) { |
328 |
if (closed) { |
|
329 |
e.abort(); |
|
330 |
} else { |
|
331 |
e.handle(); |
|
332 |
} |
|
333 |
} |
|
334 |
} |
|
335 |
||
336 |
/** |
|
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
337 |
* Tracks multiple user level registrations associated with one NIO |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
338 |
* registration (SelectionKey). In this implementation, registrations |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
339 |
* are one-off and when an event is posted the registration is cancelled |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
340 |
* until explicitly registered again. |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
341 |
* |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
342 |
* <p> No external synchronization required as this class is only used |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
343 |
* by the SelectorManager thread. One of these objects required per |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
344 |
* connection. |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
345 |
*/ |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
346 |
private static class SelectorAttachment { |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
347 |
private final SelectableChannel chan; |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
348 |
private final Selector selector; |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
349 |
private final ArrayList<AsyncEvent> pending; |
37720 | 350 |
private int interestOps; |
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
351 |
|
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
352 |
SelectorAttachment(SelectableChannel chan, Selector selector) { |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
353 |
this.pending = new ArrayList<>(); |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
354 |
this.chan = chan; |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
355 |
this.selector = selector; |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
356 |
} |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
357 |
|
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
358 |
void register(AsyncEvent e) throws ClosedChannelException { |
37720 | 359 |
int newOps = e.interestOps(); |
360 |
boolean reRegister = (interestOps & newOps) != newOps; |
|
361 |
interestOps |= newOps; |
|
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
362 |
pending.add(e); |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
363 |
if (reRegister) { |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
364 |
// first time registration happens here also |
37720 | 365 |
chan.register(selector, interestOps, this); |
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
366 |
} |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
367 |
} |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
368 |
|
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
369 |
/** |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
370 |
* Returns a Stream<AsyncEvents> containing only events that are |
37720 | 371 |
* registered with the given {@code interestOps}. |
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
372 |
*/ |
37720 | 373 |
Stream<AsyncEvent> events(int interestOps) { |
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
374 |
return pending.stream() |
37720 | 375 |
.filter(ev -> (ev.interestOps() & interestOps) != 0); |
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
376 |
} |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
377 |
|
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
378 |
/** |
37720 | 379 |
* Removes any events with the given {@code interestOps}, and if no |
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
380 |
* events remaining, cancels the associated SelectionKey. |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
381 |
*/ |
37720 | 382 |
void resetInterestOps(int interestOps) { |
383 |
int newOps = 0; |
|
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
384 |
|
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
385 |
Iterator<AsyncEvent> itr = pending.iterator(); |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
386 |
while (itr.hasNext()) { |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
387 |
AsyncEvent event = itr.next(); |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
388 |
int evops = event.interestOps(); |
37720 | 389 |
if (event.repeating()) { |
390 |
newOps |= evops; |
|
391 |
continue; |
|
392 |
} |
|
393 |
if ((evops & interestOps) != 0) { |
|
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
394 |
itr.remove(); |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
395 |
} else { |
37720 | 396 |
newOps |= evops; |
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
397 |
} |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
398 |
} |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
399 |
|
37720 | 400 |
this.interestOps = newOps; |
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
401 |
SelectionKey key = chan.keyFor(selector); |
37720 | 402 |
if (newOps == 0) { |
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
403 |
key.cancel(); |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
404 |
} else { |
37720 | 405 |
key.interestOps(newOps); |
36434
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
406 |
} |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
407 |
} |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
408 |
} |
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
409 |
|
3fd8dee1b158
8151299: Http client SelectorManager overwriting read and write events
michaelm
parents:
36131
diff
changeset
|
410 |
/** |
36131 | 411 |
* Creates a HttpRequest associated with this group. |
412 |
* |
|
37720 | 413 |
* @throws IllegalStateException |
414 |
* if the group has been stopped |
|
36131 | 415 |
*/ |
416 |
@Override |
|
417 |
public HttpRequestBuilderImpl request() { |
|
418 |
return new HttpRequestBuilderImpl(this, null); |
|
419 |
} |
|
420 |
||
421 |
/** |
|
422 |
* Creates a HttpRequest associated with this group. |
|
423 |
* |
|
37720 | 424 |
* @throws IllegalStateException |
425 |
* if the group has been stopped |
|
36131 | 426 |
*/ |
427 |
@Override |
|
428 |
public HttpRequestBuilderImpl request(URI uri) { |
|
429 |
return new HttpRequestBuilderImpl(this, uri); |
|
430 |
} |
|
431 |
||
432 |
@Override |
|
433 |
public SSLContext sslContext() { |
|
434 |
Utils.checkNetPermission("getSSLContext"); |
|
435 |
return sslContext; |
|
436 |
} |
|
437 |
||
438 |
@Override |
|
439 |
public Optional<SSLParameters> sslParameters() { |
|
440 |
return Optional.ofNullable(sslParams); |
|
441 |
} |
|
442 |
||
443 |
@Override |
|
444 |
public Optional<Authenticator> authenticator() { |
|
445 |
return Optional.ofNullable(authenticator); |
|
446 |
} |
|
447 |
||
448 |
@Override |
|
449 |
public ExecutorService executorService() { |
|
450 |
return executor.userExecutor(); |
|
451 |
} |
|
452 |
||
453 |
ExecutorWrapper executorWrapper() { |
|
454 |
return executor; |
|
455 |
} |
|
456 |
||
457 |
@Override |
|
458 |
public boolean pipelining() { |
|
459 |
return this.pipelining; |
|
460 |
} |
|
461 |
||
462 |
ConnectionPool connectionPool() { |
|
463 |
return connections; |
|
464 |
} |
|
465 |
||
466 |
@Override |
|
467 |
public Redirect followRedirects() { |
|
468 |
return followRedirects; |
|
469 |
} |
|
470 |
||
471 |
||
472 |
@Override |
|
473 |
public Optional<CookieManager> cookieManager() { |
|
474 |
return Optional.ofNullable(cookieManager); |
|
475 |
} |
|
476 |
||
477 |
@Override |
|
478 |
public Optional<ProxySelector> proxy() { |
|
479 |
return Optional.ofNullable(this.proxySelector); |
|
480 |
} |
|
481 |
||
482 |
@Override |
|
483 |
public Version version() { |
|
484 |
return version; |
|
485 |
} |
|
486 |
||
487 |
//private final HashMap<String, Boolean> http2NotSupported = new HashMap<>(); |
|
488 |
||
489 |
boolean getHttp2Allowed() { |
|
490 |
return version.equals(Version.HTTP_2); |
|
491 |
} |
|
492 |
||
37720 | 493 |
private void initFilters() { |
36131 | 494 |
addFilter(AuthenticationFilter.class); |
495 |
addFilter(RedirectFilter.class); |
|
496 |
} |
|
497 |
||
37720 | 498 |
private void addFilter(Class<? extends HeaderFilter> f) { |
36131 | 499 |
filters.addFilter(f); |
500 |
} |
|
501 |
||
502 |
final List<HeaderFilter> filterChain() { |
|
503 |
return filters.getFilterChain(); |
|
504 |
} |
|
505 |
||
506 |
// Timer controls. Timers are implemented through timed Selector.select() |
|
507 |
// calls. |
|
508 |
synchronized void registerTimer(TimeoutEvent event) { |
|
509 |
long elapse = event.timevalMillis(); |
|
510 |
ListIterator<TimeoutEvent> iter = timeouts.listIterator(); |
|
511 |
long listval = 0; |
|
512 |
event.delta = event.timeval; // in case list empty |
|
513 |
TimeoutEvent next; |
|
514 |
while (iter.hasNext()) { |
|
515 |
next = iter.next(); |
|
516 |
listval += next.delta; |
|
517 |
if (elapse < listval) { |
|
518 |
listval -= next.delta; |
|
519 |
event.delta = elapse - listval; |
|
520 |
next.delta -= event.delta; |
|
521 |
iter.previous(); |
|
522 |
break; |
|
523 |
} else if (!iter.hasNext()) { |
|
37720 | 524 |
event.delta = event.timeval - listval; |
36131 | 525 |
} |
526 |
} |
|
527 |
iter.add(event); |
|
528 |
selmgr.wakeupSelector(); |
|
529 |
} |
|
530 |
||
37720 | 531 |
private synchronized void signalTimeouts(long then) { |
36131 | 532 |
if (timeouts.isEmpty()) { |
533 |
return; |
|
534 |
} |
|
535 |
long now = System.currentTimeMillis(); |
|
536 |
long duration = now - then; |
|
537 |
ListIterator<TimeoutEvent> iter = timeouts.listIterator(); |
|
538 |
TimeoutEvent event = iter.next(); |
|
539 |
long delta = event.delta; |
|
540 |
if (duration < delta) { |
|
541 |
event.delta -= duration; |
|
542 |
return; |
|
543 |
} |
|
544 |
event.handle(); |
|
545 |
iter.remove(); |
|
546 |
while (iter.hasNext()) { |
|
547 |
event = iter.next(); |
|
548 |
if (event.delta == 0) { |
|
549 |
event.handle(); |
|
550 |
iter.remove(); |
|
551 |
} else { |
|
552 |
event.delta += delta; |
|
553 |
break; |
|
554 |
} |
|
555 |
} |
|
556 |
} |
|
557 |
||
558 |
synchronized void cancelTimer(TimeoutEvent event) { |
|
559 |
ListIterator<TimeoutEvent> iter = timeouts.listIterator(); |
|
560 |
while (iter.hasNext()) { |
|
561 |
TimeoutEvent ev = iter.next(); |
|
562 |
if (event == ev) { |
|
563 |
if (iter.hasNext()) { |
|
564 |
// adjust |
|
565 |
TimeoutEvent next = iter.next(); |
|
566 |
next.delta += ev.delta; |
|
567 |
iter.previous(); |
|
568 |
} |
|
569 |
iter.remove(); |
|
570 |
} |
|
571 |
} |
|
572 |
} |
|
573 |
||
574 |
// used for the connection window |
|
575 |
int getReceiveBufferSize() { |
|
576 |
return Utils.getIntegerNetProperty( |
|
37720 | 577 |
"java.net.httpclient.connectionWindowSize", 256 * 1024 |
36131 | 578 |
); |
579 |
} |
|
580 |
||
581 |
// returns 0 meaning block forever, or a number of millis to block for |
|
37720 | 582 |
private synchronized long getTimeoutValue() { |
36131 | 583 |
if (timeouts.isEmpty()) { |
584 |
return 0; |
|
585 |
} else { |
|
586 |
return timeouts.get(0).delta; |
|
587 |
} |
|
588 |
} |
|
589 |
} |