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