Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/java.net.http/share/classes/jdk/internal/net/http/PlainHttpConnection.java
41171 views
1
/*
2
* Copyright (c) 2015, 2021, 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
* questions.
24
*/
25
26
package jdk.internal.net.http;
27
28
import java.io.IOException;
29
import java.net.ConnectException;
30
import java.net.InetSocketAddress;
31
import java.net.StandardSocketOptions;
32
import java.nio.channels.SelectableChannel;
33
import java.nio.channels.SelectionKey;
34
import java.nio.channels.SocketChannel;
35
import java.security.AccessController;
36
import java.security.PrivilegedActionException;
37
import java.security.PrivilegedExceptionAction;
38
import java.time.Duration;
39
import java.time.Instant;
40
import java.util.concurrent.CompletableFuture;
41
import java.util.function.Function;
42
43
import jdk.internal.net.http.common.FlowTube;
44
import jdk.internal.net.http.common.Log;
45
import jdk.internal.net.http.common.MinimalFuture;
46
import jdk.internal.net.http.common.Utils;
47
48
/**
49
* Plain raw TCP connection direct to destination.
50
* The connection operates in asynchronous non-blocking mode.
51
* All reads and writes are done non-blocking.
52
*/
53
class PlainHttpConnection extends HttpConnection {
54
55
private final Object reading = new Object();
56
protected final SocketChannel chan;
57
private final SocketTube tube; // need SocketTube to call signalClosed().
58
private final PlainHttpPublisher writePublisher = new PlainHttpPublisher(reading);
59
private volatile boolean connected;
60
private boolean closed;
61
private volatile ConnectTimerEvent connectTimerEvent; // may be null
62
private volatile int unsuccessfulAttempts;
63
64
// Indicates whether a connection attempt has succeeded or should be retried.
65
// If the attempt failed, and shouldn't be retried, there will be an exception
66
// instead.
67
private enum ConnectState { SUCCESS, RETRY }
68
69
70
/**
71
* Returns a ConnectTimerEvent iff there is a connect timeout duration,
72
* otherwise null.
73
*/
74
private ConnectTimerEvent newConnectTimer(Exchange<?> exchange,
75
CompletableFuture<?> cf) {
76
Duration duration = exchange.remainingConnectTimeout().orElse(null);
77
if (duration != null) {
78
ConnectTimerEvent cte = new ConnectTimerEvent(duration, exchange, cf);
79
return cte;
80
}
81
return null;
82
}
83
84
final class ConnectTimerEvent extends TimeoutEvent {
85
private final CompletableFuture<?> cf;
86
private final Exchange<?> exchange;
87
88
ConnectTimerEvent(Duration duration,
89
Exchange<?> exchange,
90
CompletableFuture<?> cf) {
91
super(duration);
92
this.exchange = exchange;
93
this.cf = cf;
94
}
95
96
@Override
97
public void handle() {
98
if (debug.on()) {
99
debug.log("HTTP connect timed out");
100
}
101
ConnectException ce = new ConnectException("HTTP connect timed out");
102
exchange.multi.cancel(ce);
103
client().theExecutor().execute(() -> cf.completeExceptionally(ce));
104
}
105
106
@Override
107
public String toString() {
108
return "ConnectTimerEvent, " + super.toString();
109
}
110
}
111
112
final class ConnectEvent extends AsyncEvent {
113
private final CompletableFuture<ConnectState> cf;
114
private final Exchange<?> exchange;
115
116
ConnectEvent(CompletableFuture<ConnectState> cf, Exchange<?> exchange) {
117
this.cf = cf;
118
this.exchange = exchange;
119
}
120
121
@Override
122
public SelectableChannel channel() {
123
return chan;
124
}
125
126
@Override
127
public int interestOps() {
128
return SelectionKey.OP_CONNECT;
129
}
130
131
@Override
132
public void handle() {
133
try {
134
assert !connected : "Already connected";
135
assert !chan.isBlocking() : "Unexpected blocking channel";
136
if (debug.on())
137
debug.log("ConnectEvent: finishing connect");
138
boolean finished = chan.finishConnect();
139
if (debug.on())
140
debug.log("ConnectEvent: connect finished: %s, cancelled: %s, Local addr: %s",
141
finished, exchange.multi.requestCancelled(), chan.getLocalAddress());
142
assert finished || exchange.multi.requestCancelled() : "Expected channel to be connected";
143
// complete async since the event runs on the SelectorManager thread
144
cf.completeAsync(() -> ConnectState.SUCCESS, client().theExecutor());
145
} catch (Throwable e) {
146
if (canRetryConnect(e)) {
147
unsuccessfulAttempts++;
148
cf.completeAsync(() -> ConnectState.RETRY, client().theExecutor());
149
return;
150
}
151
Throwable t = Utils.toConnectException(e);
152
client().theExecutor().execute( () -> cf.completeExceptionally(t));
153
close();
154
}
155
}
156
157
@Override
158
public void abort(IOException ioe) {
159
client().theExecutor().execute( () -> cf.completeExceptionally(ioe));
160
close();
161
}
162
}
163
164
@SuppressWarnings("removal")
165
@Override
166
public CompletableFuture<Void> connectAsync(Exchange<?> exchange) {
167
CompletableFuture<ConnectState> cf = new MinimalFuture<>();
168
try {
169
assert !connected : "Already connected";
170
assert !chan.isBlocking() : "Unexpected blocking channel";
171
boolean finished;
172
173
if (connectTimerEvent == null) {
174
connectTimerEvent = newConnectTimer(exchange, cf);
175
if (connectTimerEvent != null) {
176
if (debug.on())
177
debug.log("registering connect timer: " + connectTimerEvent);
178
client().registerTimer(connectTimerEvent);
179
}
180
}
181
182
PrivilegedExceptionAction<Boolean> pa =
183
() -> chan.connect(Utils.resolveAddress(address));
184
try {
185
finished = AccessController.doPrivileged(pa);
186
} catch (PrivilegedActionException e) {
187
throw e.getCause();
188
}
189
if (finished) {
190
if (debug.on()) debug.log("connect finished without blocking");
191
cf.complete(ConnectState.SUCCESS);
192
} else {
193
if (debug.on()) debug.log("registering connect event");
194
client().registerEvent(new ConnectEvent(cf, exchange));
195
}
196
cf = exchange.checkCancelled(cf, this);
197
} catch (Throwable throwable) {
198
cf.completeExceptionally(Utils.toConnectException(throwable));
199
try {
200
close();
201
} catch (Exception x) {
202
if (debug.on())
203
debug.log("Failed to close channel after unsuccessful connect");
204
}
205
}
206
return cf.handle((r,t) -> checkRetryConnect(r, t,exchange))
207
.thenCompose(Function.identity());
208
}
209
210
/**
211
* On some platforms, a ConnectEvent may be raised and a ConnectionException
212
* may occur with the message "Connection timed out: no further information"
213
* before our actual connection timeout has expired. In this case, this
214
* method will be called with a {@code connect} state of {@code ConnectState.RETRY)}
215
* and we will retry once again.
216
* @param connect indicates whether the connection was successful or should be retried
217
* @param failed the failure if the connection failed
218
* @param exchange the exchange
219
* @return a completable future that will take care of retrying the connection if needed.
220
*/
221
private CompletableFuture<Void> checkRetryConnect(ConnectState connect, Throwable failed, Exchange<?> exchange) {
222
// first check if the connection failed
223
if (failed != null) return MinimalFuture.failedFuture(failed);
224
// then check if the connection should be retried
225
if (connect == ConnectState.RETRY) {
226
int attempts = unsuccessfulAttempts;
227
assert attempts <= 1;
228
if (debug.on())
229
debug.log("Retrying connect after %d attempts", attempts);
230
return connectAsync(exchange);
231
}
232
// Otherwise, the connection was successful;
233
assert connect == ConnectState.SUCCESS;
234
return MinimalFuture.completedFuture(null);
235
}
236
237
private boolean canRetryConnect(Throwable e) {
238
if (!MultiExchange.RETRY_CONNECT) return false;
239
if (!(e instanceof ConnectException)) return false;
240
if (unsuccessfulAttempts > 0) return false;
241
ConnectTimerEvent timer = connectTimerEvent;
242
if (timer == null) return true;
243
return timer.deadline().isAfter(Instant.now());
244
}
245
246
@Override
247
public CompletableFuture<Void> finishConnect() {
248
assert connected == false;
249
if (debug.on()) debug.log("finishConnect, setting connected=true");
250
connected = true;
251
if (connectTimerEvent != null)
252
client().cancelTimer(connectTimerEvent);
253
return MinimalFuture.completedFuture(null);
254
}
255
256
@Override
257
SocketChannel channel() {
258
return chan;
259
}
260
261
@Override
262
final FlowTube getConnectionFlow() {
263
return tube;
264
}
265
266
PlainHttpConnection(InetSocketAddress addr, HttpClientImpl client) {
267
super(addr, client);
268
try {
269
this.chan = SocketChannel.open();
270
chan.configureBlocking(false);
271
if (debug.on()) {
272
int bufsize = getSoReceiveBufferSize();
273
debug.log("Initial receive buffer size is: %d", bufsize);
274
bufsize = getSoSendBufferSize();
275
debug.log("Initial send buffer size is: %d", bufsize);
276
}
277
if (trySetReceiveBufferSize(client.getReceiveBufferSize())) {
278
if (debug.on()) {
279
int bufsize = getSoReceiveBufferSize();
280
debug.log("Receive buffer size configured: %d", bufsize);
281
}
282
}
283
if (trySetSendBufferSize(client.getSendBufferSize())) {
284
if (debug.on()) {
285
int bufsize = getSoSendBufferSize();
286
debug.log("Send buffer size configured: %d", bufsize);
287
}
288
}
289
chan.setOption(StandardSocketOptions.TCP_NODELAY, true);
290
// wrap the channel in a Tube for async reading and writing
291
tube = new SocketTube(client(), chan, Utils::getBuffer);
292
} catch (IOException e) {
293
throw new InternalError(e);
294
}
295
}
296
297
private int getSoReceiveBufferSize() {
298
try {
299
return chan.getOption(StandardSocketOptions.SO_RCVBUF);
300
} catch (IOException x) {
301
if (debug.on())
302
debug.log("Failed to get initial receive buffer size on %s", chan);
303
}
304
return 0;
305
}
306
307
private int getSoSendBufferSize() {
308
try {
309
return chan.getOption(StandardSocketOptions.SO_SNDBUF);
310
} catch (IOException x) {
311
if (debug.on())
312
debug.log("Failed to get initial receive buffer size on %s", chan);
313
}
314
return 0;
315
}
316
317
private boolean trySetReceiveBufferSize(int bufsize) {
318
try {
319
if (bufsize > 0) {
320
chan.setOption(StandardSocketOptions.SO_RCVBUF, bufsize);
321
return true;
322
}
323
} catch (IOException x) {
324
if (debug.on())
325
debug.log("Failed to set receive buffer size to %d on %s",
326
bufsize, chan);
327
}
328
return false;
329
}
330
331
private boolean trySetSendBufferSize(int bufsize) {
332
try {
333
if (bufsize > 0) {
334
chan.setOption(StandardSocketOptions.SO_SNDBUF, bufsize);
335
return true;
336
}
337
} catch (IOException x) {
338
if (debug.on())
339
debug.log("Failed to set send buffer size to %d on %s",
340
bufsize, chan);
341
}
342
return false;
343
}
344
345
@Override
346
HttpPublisher publisher() { return writePublisher; }
347
348
349
@Override
350
public String toString() {
351
return "PlainHttpConnection: " + super.toString();
352
}
353
354
/**
355
* Closes this connection
356
*/
357
@Override
358
public void close() {
359
synchronized (this) {
360
if (closed) {
361
return;
362
}
363
closed = true;
364
}
365
try {
366
Log.logTrace("Closing: " + toString());
367
if (debug.on())
368
debug.log("Closing channel: " + client().debugInterestOps(chan));
369
if (connectTimerEvent != null)
370
client().cancelTimer(connectTimerEvent);
371
chan.close();
372
tube.signalClosed();
373
} catch (IOException e) {
374
Log.logTrace("Closing resulted in " + e);
375
}
376
}
377
378
379
@Override
380
ConnectionPool.CacheKey cacheKey() {
381
return new ConnectionPool.CacheKey(address, null);
382
}
383
384
@Override
385
synchronized boolean connected() {
386
return connected;
387
}
388
389
390
@Override
391
boolean isSecure() {
392
return false;
393
}
394
395
@Override
396
boolean isProxied() {
397
return false;
398
}
399
400
@Override
401
InetSocketAddress proxy() {
402
return null;
403
}
404
}
405
406