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/PlainTunnelingConnection.java
41171 views
1
/*
2
* Copyright (c) 2015, 2020, 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.InetSocketAddress;
30
import java.net.http.HttpTimeoutException;
31
import java.nio.ByteBuffer;
32
import java.nio.channels.SocketChannel;
33
import java.time.Duration;
34
import java.util.concurrent.CompletableFuture;
35
import java.util.concurrent.CompletionException;
36
import java.util.function.Function;
37
import java.net.http.HttpHeaders;
38
import jdk.internal.net.http.common.FlowTube;
39
import jdk.internal.net.http.common.MinimalFuture;
40
import static java.net.http.HttpResponse.BodyHandlers.discarding;
41
import static jdk.internal.net.http.common.Utils.ProxyHeaders;
42
43
/**
44
* A plain text socket tunnel through a proxy. Uses "CONNECT" but does not
45
* encrypt. Used by WebSocket, as well as HTTP over SSL + Proxy.
46
* Wrapped in SSLTunnelConnection or AsyncSSLTunnelConnection for encryption.
47
*/
48
final class PlainTunnelingConnection extends HttpConnection {
49
50
final PlainHttpConnection delegate;
51
final ProxyHeaders proxyHeaders;
52
final InetSocketAddress proxyAddr;
53
private volatile boolean connected;
54
55
protected PlainTunnelingConnection(InetSocketAddress addr,
56
InetSocketAddress proxy,
57
HttpClientImpl client,
58
ProxyHeaders proxyHeaders) {
59
super(addr, client);
60
this.proxyAddr = proxy;
61
this.proxyHeaders = proxyHeaders;
62
delegate = new PlainHttpConnection(proxy, client);
63
}
64
65
@Override
66
public CompletableFuture<Void> connectAsync(Exchange<?> exchange) {
67
if (debug.on()) debug.log("Connecting plain connection");
68
return delegate.connectAsync(exchange)
69
.thenCompose(unused -> delegate.finishConnect())
70
.thenCompose((Void v) -> {
71
if (debug.on()) debug.log("sending HTTP/1.1 CONNECT");
72
HttpClientImpl client = client();
73
assert client != null;
74
HttpRequestImpl req = new HttpRequestImpl("CONNECT", address, proxyHeaders);
75
MultiExchange<Void> mulEx = new MultiExchange<>(null, req,
76
client, discarding(), null, null);
77
Exchange<Void> connectExchange = mulEx.getExchange();
78
79
return connectExchange
80
.responseAsyncImpl(delegate)
81
.thenCompose((Response resp) -> {
82
CompletableFuture<Void> cf = new MinimalFuture<>();
83
if (debug.on()) debug.log("got response: %d", resp.statusCode());
84
if (resp.statusCode() == 407) {
85
return connectExchange.ignoreBody().handle((r,t) -> {
86
// close delegate after reading body: we won't
87
// be reusing that connection anyway.
88
delegate.close();
89
ProxyAuthenticationRequired authenticationRequired =
90
new ProxyAuthenticationRequired(resp);
91
cf.completeExceptionally(authenticationRequired);
92
return cf;
93
}).thenCompose(Function.identity());
94
} else if (resp.statusCode() != 200) {
95
delegate.close();
96
cf.completeExceptionally(new IOException(
97
"Tunnel failed, got: "+ resp.statusCode()));
98
} else {
99
// get the initial/remaining bytes
100
ByteBuffer b = ((Http1Exchange<?>)connectExchange.exchImpl).drainLeftOverBytes();
101
int remaining = b.remaining();
102
assert remaining == 0: "Unexpected remaining: " + remaining;
103
cf.complete(null);
104
}
105
return cf;
106
})
107
.handle((result, ex) -> {
108
if (ex == null) {
109
return MinimalFuture.completedFuture(result);
110
} else {
111
if (debug.on())
112
debug.log("tunnel failed with \"%s\"", ex.toString());
113
Throwable t = ex;
114
if (t instanceof CompletionException)
115
t = t.getCause();
116
if (t instanceof HttpTimeoutException) {
117
String msg = "proxy tunneling CONNECT request timed out";
118
t = new HttpTimeoutException(msg);
119
t.initCause(ex);
120
}
121
return MinimalFuture.<Void>failedFuture(t);
122
}
123
})
124
.thenCompose(Function.identity());
125
});
126
}
127
128
public CompletableFuture<Void> finishConnect() {
129
connected = true;
130
return MinimalFuture.completedFuture(null);
131
}
132
133
@Override
134
boolean isTunnel() { return true; }
135
136
@Override
137
HttpPublisher publisher() { return delegate.publisher(); }
138
139
@Override
140
boolean connected() {
141
return connected;
142
}
143
144
@Override
145
SocketChannel channel() {
146
return delegate.channel();
147
}
148
149
@Override
150
FlowTube getConnectionFlow() {
151
return delegate.getConnectionFlow();
152
}
153
154
@Override
155
ConnectionPool.CacheKey cacheKey() {
156
return new ConnectionPool.CacheKey(null, proxyAddr);
157
}
158
159
@Override
160
public void close() {
161
delegate.close();
162
connected = false;
163
}
164
165
@Override
166
boolean isSecure() {
167
return false;
168
}
169
170
@Override
171
boolean isProxied() {
172
return true;
173
}
174
175
@Override
176
InetSocketAddress proxy() {
177
return proxyAddr;
178
}
179
}
180
181