author | dfuchs |
Mon, 23 Apr 2018 15:45:40 +0100 | |
branch | http-client-branch |
changeset 56474 | fe2bf7b369b8 |
parent 56451 | 9585061fdb04 |
child 56619 | 57f17e890a40 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
49765 | 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 |
|
49765 | 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 java.io.Closeable; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.io.IOException; |
48083 | 30 |
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
|
31 |
import java.net.InetSocketAddress; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.nio.ByteBuffer; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.nio.channels.SocketChannel; |
49765 | 34 |
import java.util.Arrays; |
48083 | 35 |
import java.util.IdentityHashMap; |
36 |
import java.util.List; |
|
37 |
import java.util.Map; |
|
49765 | 38 |
import java.util.TreeMap; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.util.concurrent.CompletableFuture; |
48083 | 40 |
import java.util.concurrent.CompletionStage; |
41 |
import java.util.concurrent.ConcurrentLinkedDeque; |
|
42 |
import java.util.concurrent.Flow; |
|
49765 | 43 |
import java.util.function.BiPredicate; |
44 |
import java.util.function.Predicate; |
|
45 |
import java.net.http.HttpClient; |
|
46 |
import java.net.http.HttpClient.Version; |
|
47 |
import java.net.http.HttpHeaders; |
|
48 |
import jdk.internal.net.http.common.Demand; |
|
49 |
import jdk.internal.net.http.common.FlowTube; |
|
50 |
import jdk.internal.net.http.common.Logger; |
|
51 |
import jdk.internal.net.http.common.SequentialScheduler; |
|
52 |
import jdk.internal.net.http.common.SequentialScheduler.DeferredCompleter; |
|
53 |
import jdk.internal.net.http.common.Log; |
|
54 |
import jdk.internal.net.http.common.Utils; |
|
55 |
import static java.net.http.HttpClient.Version.HTTP_2; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
* Wraps socket channel layer and takes care of SSL also. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
* Subtypes are: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
* PlainHttpConnection: regular direct TCP connection to server |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
* PlainProxyConnection: plain text proxy connection |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
* PlainTunnelingConnection: opens plain text (CONNECT) tunnel to server |
48083 | 64 |
* AsyncSSLConnection: TLS channel direct to server |
65 |
* AsyncSSLTunnelConnection: TLS channel via (CONNECT) proxy tunnel |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
67 |
abstract class HttpConnection implements Closeable { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
68 |
|
49765 | 69 |
final Logger debug = Utils.getDebugLogger(this::dbgString, Utils.DEBUG); |
70 |
final static Logger DEBUG_LOGGER = Utils.getDebugLogger( |
|
71 |
() -> "HttpConnection(SocketTube(?))", Utils.DEBUG); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
|
48083 | 73 |
/** The address this connection is connected to. Could be a server or a proxy. */ |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
final InetSocketAddress address; |
48083 | 75 |
private final HttpClientImpl client; |
76 |
private final TrailingOperations trailingOperations; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
HttpConnection(InetSocketAddress address, HttpClientImpl client) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
this.address = address; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
this.client = client; |
48083 | 81 |
trailingOperations = new TrailingOperations(); |
82 |
} |
|
83 |
||
84 |
private static final class TrailingOperations { |
|
85 |
private final Map<CompletionStage<?>, Boolean> operations = |
|
86 |
new IdentityHashMap<>(); |
|
87 |
void add(CompletionStage<?> cf) { |
|
88 |
synchronized(operations) { |
|
89 |
cf.whenComplete((r,t)-> remove(cf)); |
|
90 |
operations.put(cf, Boolean.TRUE); |
|
91 |
} |
|
92 |
} |
|
93 |
boolean remove(CompletionStage<?> cf) { |
|
94 |
synchronized(operations) { |
|
95 |
return operations.remove(cf); |
|
96 |
} |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
final void addTrailingOperation(CompletionStage<?> cf) { |
|
101 |
trailingOperations.add(cf); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
102 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
103 |
|
48083 | 104 |
// final void removeTrailingOperation(CompletableFuture<?> cf) { |
105 |
// trailingOperations.remove(cf); |
|
106 |
// } |
|
107 |
||
108 |
final HttpClientImpl client() { |
|
109 |
return client; |
|
110 |
} |
|
111 |
||
112 |
//public abstract void connect() throws IOException, InterruptedException; |
|
113 |
||
114 |
public abstract CompletableFuture<Void> connectAsync(); |
|
115 |
||
116 |
/** Tells whether, or not, this connection is connected to its destination. */ |
|
117 |
abstract boolean connected(); |
|
118 |
||
119 |
/** Tells whether, or not, this connection is secure ( over SSL ) */ |
|
120 |
abstract boolean isSecure(); |
|
121 |
||
49765 | 122 |
/** |
123 |
* Tells whether, or not, this connection is proxied. |
|
124 |
* Returns true for tunnel connections, or clear connection to |
|
125 |
* any host through proxy. |
|
126 |
*/ |
|
48083 | 127 |
abstract boolean isProxied(); |
128 |
||
129 |
/** Tells whether, or not, this connection is open. */ |
|
130 |
final boolean isOpen() { |
|
131 |
return channel().isOpen() && |
|
132 |
(connected() ? !getConnectionFlow().isFinished() : true); |
|
133 |
} |
|
134 |
||
135 |
interface HttpPublisher extends FlowTube.TubePublisher { |
|
136 |
void enqueue(List<ByteBuffer> buffers) throws IOException; |
|
137 |
void enqueueUnordered(List<ByteBuffer> buffers) throws IOException; |
|
138 |
void signalEnqueued() throws IOException; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
/** |
48083 | 142 |
* Returns the HTTP publisher associated with this connection. May be null |
143 |
* if invoked before connecting. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
*/ |
48083 | 145 |
abstract HttpPublisher publisher(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
|
49765 | 147 |
// HTTP/2 MUST use TLS version 1.2 or higher for HTTP/2 over TLS |
148 |
private static final Predicate<String> testRequiredHTTP2TLSVersion = proto -> |
|
149 |
proto.equals("TLSv1.2") || proto.equals("TLSv1.3"); |
|
150 |
||
151 |
/** |
|
152 |
* Returns true if the given client's SSL parameter protocols contains at |
|
153 |
* least one TLS version that HTTP/2 requires. |
|
154 |
*/ |
|
155 |
private static final boolean hasRequiredHTTP2TLSVersion(HttpClient client) { |
|
156 |
String[] protos = client.sslParameters().getProtocols(); |
|
157 |
if (protos != null) { |
|
158 |
return Arrays.stream(protos).filter(testRequiredHTTP2TLSVersion).findAny().isPresent(); |
|
159 |
} else { |
|
160 |
return false; |
|
161 |
} |
|
162 |
} |
|
163 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
164 |
/** |
48083 | 165 |
* Factory for retrieving HttpConnections. A connection can be retrieved |
166 |
* from the connection pool, or a new one created if none available. |
|
167 |
* |
|
168 |
* The given {@code addr} is the ultimate destination. Any proxies, |
|
169 |
* etc, are determined from the request. Returns a concrete instance which |
|
170 |
* is one of the following: |
|
171 |
* {@link PlainHttpConnection} |
|
172 |
* {@link PlainTunnelingConnection} |
|
173 |
* |
|
174 |
* The returned connection, if not from the connection pool, must have its, |
|
175 |
* connect() or connectAsync() method invoked, which ( when it completes |
|
176 |
* successfully ) renders the connection usable for requests. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
177 |
*/ |
48083 | 178 |
public static HttpConnection getConnection(InetSocketAddress addr, |
179 |
HttpClientImpl client, |
|
180 |
HttpRequestImpl request, |
|
181 |
Version version) { |
|
49765 | 182 |
// The default proxy selector may select a proxy whose address is |
183 |
// unresolved. We must resolve the address before connecting to it. |
|
184 |
InetSocketAddress proxy = Utils.resolveAddress(request.proxy()); |
|
48083 | 185 |
HttpConnection c = null; |
186 |
boolean secure = request.secure(); |
|
187 |
ConnectionPool pool = client.connectionPool(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
|
48083 | 189 |
if (!secure) { |
190 |
c = pool.getConnection(false, addr, proxy); |
|
191 |
if (c != null && c.isOpen() /* may have been eof/closed when in the pool */) { |
|
192 |
final HttpConnection conn = c; |
|
49765 | 193 |
if (DEBUG_LOGGER.on()) |
194 |
DEBUG_LOGGER.log(conn.getConnectionFlow() |
|
195 |
+ ": plain connection retrieved from HTTP/1.1 pool"); |
|
48083 | 196 |
return c; |
197 |
} else { |
|
198 |
return getPlainConnection(addr, proxy, request, client); |
|
199 |
} |
|
200 |
} else { // secure |
|
201 |
if (version != HTTP_2) { // only HTTP/1.1 connections are in the pool |
|
202 |
c = pool.getConnection(true, addr, proxy); |
|
203 |
} |
|
204 |
if (c != null && c.isOpen()) { |
|
205 |
final HttpConnection conn = c; |
|
49765 | 206 |
if (DEBUG_LOGGER.on()) |
207 |
DEBUG_LOGGER.log(conn.getConnectionFlow() |
|
208 |
+ ": SSL connection retrieved from HTTP/1.1 pool"); |
|
48083 | 209 |
return c; |
210 |
} else { |
|
211 |
String[] alpn = null; |
|
49765 | 212 |
if (version == HTTP_2 && hasRequiredHTTP2TLSVersion(client)) { |
48083 | 213 |
alpn = new String[] { "h2", "http/1.1" }; |
214 |
} |
|
49765 | 215 |
return getSSLConnection(addr, proxy, alpn, request, client); |
48083 | 216 |
} |
217 |
} |
|
218 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
219 |
|
48083 | 220 |
private static HttpConnection getSSLConnection(InetSocketAddress addr, |
221 |
InetSocketAddress proxy, |
|
222 |
String[] alpn, |
|
49765 | 223 |
HttpRequestImpl request, |
48083 | 224 |
HttpClientImpl client) { |
225 |
if (proxy != null) |
|
49765 | 226 |
return new AsyncSSLTunnelConnection(addr, client, alpn, proxy, |
227 |
proxyTunnelHeaders(request)); |
|
48083 | 228 |
else |
229 |
return new AsyncSSLConnection(addr, client, alpn); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
230 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
|
49765 | 232 |
/** |
233 |
* This method is used to build a filter that will accept or |
|
234 |
* veto (header-name, value) tuple for transmission on the |
|
235 |
* wire. |
|
236 |
* The filter is applied to the headers when sending the headers |
|
237 |
* to the remote party. |
|
238 |
* Which tuple is accepted/vetoed depends on: |
|
239 |
* <pre> |
|
240 |
* - whether the connection is a tunnel connection |
|
241 |
* [talking to a server through a proxy tunnel] |
|
242 |
* - whether the method is CONNECT |
|
243 |
* [establishing a CONNECT tunnel through a proxy] |
|
244 |
* - whether the request is using a proxy |
|
245 |
* (and the connection is not a tunnel) |
|
246 |
* [talking to a server through a proxy] |
|
247 |
* - whether the request is a direct connection to |
|
248 |
* a server (no tunnel, no proxy). |
|
249 |
* </pre> |
|
250 |
* @param request |
|
251 |
* @return |
|
252 |
*/ |
|
253 |
BiPredicate<String,List<String>> headerFilter(HttpRequestImpl request) { |
|
254 |
if (isTunnel()) { |
|
255 |
// talking to a server through a proxy tunnel |
|
256 |
// don't send proxy-* headers to a plain server |
|
257 |
assert !request.isConnect(); |
|
258 |
return Utils.NO_PROXY_HEADERS_FILTER; |
|
259 |
} else if (request.isConnect()) { |
|
260 |
// establishing a proxy tunnel |
|
261 |
// check for proxy tunnel disabled schemes |
|
262 |
// assert !this.isTunnel(); |
|
263 |
assert request.proxy() == null; |
|
264 |
return Utils.PROXY_TUNNEL_FILTER; |
|
265 |
} else if (request.proxy() != null) { |
|
266 |
// talking to a server through a proxy (no tunnel) |
|
267 |
// check for proxy disabled schemes |
|
268 |
// assert !isTunnel() && !request.isConnect(); |
|
269 |
return Utils.PROXY_FILTER; |
|
270 |
} else { |
|
271 |
// talking to a server directly (no tunnel, no proxy) |
|
272 |
// don't send proxy-* headers to a plain server |
|
273 |
// assert request.proxy() == null && !request.isConnect(); |
|
274 |
return Utils.NO_PROXY_HEADERS_FILTER; |
|
275 |
} |
|
276 |
} |
|
277 |
||
278 |
// Composes a new immutable HttpHeaders that combines the |
|
279 |
// user and system header but only keeps those headers that |
|
280 |
// start with "proxy-" |
|
281 |
private static HttpHeaders proxyTunnelHeaders(HttpRequestImpl request) { |
|
282 |
Map<String, List<String>> combined = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
|
283 |
combined.putAll(request.getSystemHeaders().map()); |
|
284 |
combined.putAll(request.headers().map()); // let user override system |
|
285 |
||
286 |
// keep only proxy-* - and also strip authorization headers |
|
287 |
// for disabled schemes |
|
288 |
return ImmutableHeaders.of(combined, Utils.PROXY_TUNNEL_FILTER); |
|
289 |
} |
|
290 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
291 |
/* Returns either a plain HTTP connection or a plain tunnelling connection |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
42460
diff
changeset
|
292 |
* for proxied WebSocket */ |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
293 |
private static HttpConnection getPlainConnection(InetSocketAddress addr, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
294 |
InetSocketAddress proxy, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
295 |
HttpRequestImpl request, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
296 |
HttpClientImpl client) { |
48083 | 297 |
if (request.isWebSocket() && proxy != null) |
49765 | 298 |
return new PlainTunnelingConnection(addr, proxy, client, |
299 |
proxyTunnelHeaders(request)); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
300 |
|
48083 | 301 |
if (proxy == null) |
302 |
return new PlainHttpConnection(addr, client); |
|
303 |
else |
|
304 |
return new PlainProxyConnection(proxy, client); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
305 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
306 |
|
48083 | 307 |
void closeOrReturnToCache(HttpHeaders hdrs) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
if (hdrs == null) { |
48083 | 309 |
// the connection was closed by server, eof |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
310 |
close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
311 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
312 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
313 |
if (!isOpen()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
314 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
315 |
} |
48083 | 316 |
HttpClientImpl client = client(); |
317 |
if (client == null) { |
|
318 |
close(); |
|
319 |
return; |
|
320 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
321 |
ConnectionPool pool = client.connectionPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
322 |
boolean keepAlive = hdrs.firstValue("Connection") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
323 |
.map((s) -> !s.equalsIgnoreCase("close")) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
324 |
.orElse(true); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
325 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
326 |
if (keepAlive) { |
48083 | 327 |
Log.logTrace("Returning connection to the pool: {0}", this); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
328 |
pool.returnToPool(this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
330 |
close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
331 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
332 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
333 |
|
49765 | 334 |
/* Tells whether or not this connection is a tunnel through a proxy */ |
335 |
boolean isTunnel() { return false; } |
|
336 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
337 |
abstract SocketChannel channel(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
338 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
339 |
final InetSocketAddress address() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
340 |
return address; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
341 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
abstract ConnectionPool.CacheKey cacheKey(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
345 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
* Closes this connection, by returning the socket to its connection pool. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
347 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
public abstract void close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
|
48083 | 351 |
abstract FlowTube getConnectionFlow(); |
352 |
||
353 |
/** |
|
49765 | 354 |
* A publisher that makes it possible to publish (write) ordered (normal |
355 |
* priority) and unordered (high priority) buffers downstream. |
|
48083 | 356 |
*/ |
357 |
final class PlainHttpPublisher implements HttpPublisher { |
|
358 |
final Object reading; |
|
359 |
PlainHttpPublisher() { |
|
360 |
this(new Object()); |
|
361 |
} |
|
362 |
PlainHttpPublisher(Object readingLock) { |
|
363 |
this.reading = readingLock; |
|
364 |
} |
|
365 |
final ConcurrentLinkedDeque<List<ByteBuffer>> queue = new ConcurrentLinkedDeque<>(); |
|
49765 | 366 |
final ConcurrentLinkedDeque<List<ByteBuffer>> priority = new ConcurrentLinkedDeque<>(); |
48083 | 367 |
volatile Flow.Subscriber<? super List<ByteBuffer>> subscriber; |
368 |
volatile HttpWriteSubscription subscription; |
|
369 |
final SequentialScheduler writeScheduler = |
|
370 |
new SequentialScheduler(this::flushTask); |
|
371 |
@Override |
|
372 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> subscriber) { |
|
373 |
synchronized (reading) { |
|
374 |
//assert this.subscription == null; |
|
375 |
//assert this.subscriber == null; |
|
376 |
if (subscription == null) { |
|
377 |
subscription = new HttpWriteSubscription(); |
|
378 |
} |
|
379 |
this.subscriber = subscriber; |
|
380 |
} |
|
381 |
// TODO: should we do this in the flow? |
|
382 |
subscriber.onSubscribe(subscription); |
|
383 |
signal(); |
|
384 |
} |
|
385 |
||
386 |
void flushTask(DeferredCompleter completer) { |
|
387 |
try { |
|
388 |
HttpWriteSubscription sub = subscription; |
|
389 |
if (sub != null) sub.flush(); |
|
390 |
} finally { |
|
391 |
completer.complete(); |
|
392 |
} |
|
393 |
} |
|
394 |
||
395 |
void signal() { |
|
396 |
writeScheduler.runOrSchedule(); |
|
397 |
} |
|
398 |
||
399 |
final class HttpWriteSubscription implements Flow.Subscription { |
|
400 |
final Demand demand = new Demand(); |
|
401 |
||
402 |
@Override |
|
403 |
public void request(long n) { |
|
404 |
if (n <= 0) throw new IllegalArgumentException("non-positive request"); |
|
405 |
demand.increase(n); |
|
49765 | 406 |
if (debug.on()) |
407 |
debug.log("HttpPublisher: got request of " + n + " from " |
|
408 |
+ getConnectionFlow()); |
|
48083 | 409 |
writeScheduler.runOrSchedule(); |
410 |
} |
|
411 |
||
412 |
@Override |
|
413 |
public void cancel() { |
|
49765 | 414 |
if (debug.on()) |
415 |
debug.log("HttpPublisher: cancelled by " + getConnectionFlow()); |
|
416 |
} |
|
417 |
||
418 |
private boolean isEmpty() { |
|
419 |
return queue.isEmpty() && priority.isEmpty(); |
|
420 |
} |
|
421 |
||
422 |
private List<ByteBuffer> poll() { |
|
423 |
List<ByteBuffer> elem = priority.poll(); |
|
424 |
return elem == null ? queue.poll() : elem; |
|
48083 | 425 |
} |
426 |
||
427 |
void flush() { |
|
49765 | 428 |
while (!isEmpty() && demand.tryDecrement()) { |
429 |
List<ByteBuffer> elem = poll(); |
|
430 |
if (debug.on()) |
|
431 |
debug.log("HttpPublisher: sending " |
|
432 |
+ Utils.remaining(elem) + " bytes (" |
|
433 |
+ elem.size() + " buffers) to " |
|
434 |
+ getConnectionFlow()); |
|
48083 | 435 |
subscriber.onNext(elem); |
436 |
} |
|
437 |
} |
|
438 |
} |
|
439 |
||
440 |
@Override |
|
441 |
public void enqueue(List<ByteBuffer> buffers) throws IOException { |
|
442 |
queue.add(buffers); |
|
443 |
int bytes = buffers.stream().mapToInt(ByteBuffer::remaining).sum(); |
|
49765 | 444 |
debug.log("added %d bytes to the write queue", bytes); |
48083 | 445 |
} |
446 |
||
447 |
@Override |
|
448 |
public void enqueueUnordered(List<ByteBuffer> buffers) throws IOException { |
|
449 |
// Unordered frames are sent before existing frames. |
|
450 |
int bytes = buffers.stream().mapToInt(ByteBuffer::remaining).sum(); |
|
49765 | 451 |
priority.add(buffers); |
452 |
debug.log("added %d bytes in the priority write queue", bytes); |
|
48083 | 453 |
} |
454 |
||
455 |
@Override |
|
456 |
public void signalEnqueued() throws IOException { |
|
49765 | 457 |
debug.log("signalling the publisher of the write queue"); |
48083 | 458 |
signal(); |
459 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
460 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
461 |
|
49765 | 462 |
String dbgTag; |
48083 | 463 |
final String dbgString() { |
464 |
FlowTube flow = getConnectionFlow(); |
|
465 |
String tag = dbgTag; |
|
466 |
if (tag == null && flow != null) { |
|
467 |
dbgTag = tag = this.getClass().getSimpleName() + "(" + flow + ")"; |
|
468 |
} else if (tag == null) { |
|
469 |
tag = this.getClass().getSimpleName() + "(?)"; |
|
470 |
} |
|
471 |
return tag; |
|
472 |
} |
|
42460
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 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
475 |
public String toString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
476 |
return "HttpConnection: " + channel().toString(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
477 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
478 |
} |