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