author | coleenp |
Mon, 08 Jan 2018 12:02:48 -0500 | |
changeset 48620 | 7f97d35fac6e |
parent 48083 | b1c1b4ef4be2 |
child 55973 | 4d9b002587db |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
45258
a72369e2e758
8180498: Remove httpclient internal APIs which supply ByteBuffers to read calls
michaelm
parents:
44854
diff
changeset
|
2 |
* Copyright (c) 2015, 2017, 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 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
26 |
package jdk.incubator.http; |
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; |
48083 | 34 |
import java.util.IdentityHashMap; |
35 |
import java.util.List; |
|
36 |
import java.util.Map; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
37 |
import java.util.concurrent.CompletableFuture; |
48083 | 38 |
import java.util.concurrent.CompletionStage; |
39 |
import java.util.concurrent.ConcurrentLinkedDeque; |
|
40 |
import java.util.concurrent.Flow; |
|
41 |
import jdk.incubator.http.HttpClient.Version; |
|
42 |
import jdk.incubator.http.internal.common.Demand; |
|
43 |
import jdk.incubator.http.internal.common.FlowTube; |
|
44 |
import jdk.incubator.http.internal.common.SequentialScheduler; |
|
45 |
import jdk.incubator.http.internal.common.SequentialScheduler.DeferredCompleter; |
|
46 |
import jdk.incubator.http.internal.common.Log; |
|
47 |
import jdk.incubator.http.internal.common.Utils; |
|
48 |
import static jdk.incubator.http.HttpClient.Version.HTTP_2; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
* 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
|
52 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
* Subtypes are: |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
54 |
* 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
|
55 |
* PlainProxyConnection: plain text proxy connection |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
* PlainTunnelingConnection: opens plain text (CONNECT) tunnel to server |
48083 | 57 |
* AsyncSSLConnection: TLS channel direct to server |
58 |
* 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
|
59 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
abstract class HttpConnection implements Closeable { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
|
48083 | 62 |
static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag. |
63 |
final System.Logger debug = Utils.getDebugLogger(this::dbgString, DEBUG); |
|
64 |
final static System.Logger DEBUG_LOGGER = Utils.getDebugLogger( |
|
65 |
() -> "HttpConnection(SocketTube(?))", DEBUG); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
|
48083 | 67 |
/** 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
|
68 |
final InetSocketAddress address; |
48083 | 69 |
private final HttpClientImpl client; |
70 |
private final TrailingOperations trailingOperations; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
71 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
HttpConnection(InetSocketAddress address, HttpClientImpl client) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
73 |
this.address = address; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
this.client = client; |
48083 | 75 |
trailingOperations = new TrailingOperations(); |
76 |
} |
|
77 |
||
78 |
private static final class TrailingOperations { |
|
79 |
private final Map<CompletionStage<?>, Boolean> operations = |
|
80 |
new IdentityHashMap<>(); |
|
81 |
void add(CompletionStage<?> cf) { |
|
82 |
synchronized(operations) { |
|
83 |
cf.whenComplete((r,t)-> remove(cf)); |
|
84 |
operations.put(cf, Boolean.TRUE); |
|
85 |
} |
|
86 |
} |
|
87 |
boolean remove(CompletionStage<?> cf) { |
|
88 |
synchronized(operations) { |
|
89 |
return operations.remove(cf); |
|
90 |
} |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
final void addTrailingOperation(CompletionStage<?> cf) { |
|
95 |
trailingOperations.add(cf); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
96 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
|
48083 | 98 |
// final void removeTrailingOperation(CompletableFuture<?> cf) { |
99 |
// trailingOperations.remove(cf); |
|
100 |
// } |
|
101 |
||
102 |
final HttpClientImpl client() { |
|
103 |
return client; |
|
104 |
} |
|
105 |
||
106 |
//public abstract void connect() throws IOException, InterruptedException; |
|
107 |
||
108 |
public abstract CompletableFuture<Void> connectAsync(); |
|
109 |
||
110 |
/** Tells whether, or not, this connection is connected to its destination. */ |
|
111 |
abstract boolean connected(); |
|
112 |
||
113 |
/** Tells whether, or not, this connection is secure ( over SSL ) */ |
|
114 |
abstract boolean isSecure(); |
|
115 |
||
116 |
/** Tells whether, or not, this connection is proxied. */ |
|
117 |
abstract boolean isProxied(); |
|
118 |
||
119 |
/** Tells whether, or not, this connection is open. */ |
|
120 |
final boolean isOpen() { |
|
121 |
return channel().isOpen() && |
|
122 |
(connected() ? !getConnectionFlow().isFinished() : true); |
|
123 |
} |
|
124 |
||
125 |
interface HttpPublisher extends FlowTube.TubePublisher { |
|
126 |
void enqueue(List<ByteBuffer> buffers) throws IOException; |
|
127 |
void enqueueUnordered(List<ByteBuffer> buffers) throws IOException; |
|
128 |
void signalEnqueued() throws IOException; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
129 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
/** |
48083 | 132 |
* Returns the HTTP publisher associated with this connection. May be null |
133 |
* if invoked before connecting. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
134 |
*/ |
48083 | 135 |
abstract HttpPublisher publisher(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
136 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
/** |
48083 | 138 |
* Factory for retrieving HttpConnections. A connection can be retrieved |
139 |
* from the connection pool, or a new one created if none available. |
|
140 |
* |
|
141 |
* The given {@code addr} is the ultimate destination. Any proxies, |
|
142 |
* etc, are determined from the request. Returns a concrete instance which |
|
143 |
* is one of the following: |
|
144 |
* {@link PlainHttpConnection} |
|
145 |
* {@link PlainTunnelingConnection} |
|
146 |
* |
|
147 |
* The returned connection, if not from the connection pool, must have its, |
|
148 |
* connect() or connectAsync() method invoked, which ( when it completes |
|
149 |
* 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
|
150 |
*/ |
48083 | 151 |
public static HttpConnection getConnection(InetSocketAddress addr, |
152 |
HttpClientImpl client, |
|
153 |
HttpRequestImpl request, |
|
154 |
Version version) { |
|
155 |
HttpConnection c = null; |
|
156 |
InetSocketAddress proxy = request.proxy(); |
|
157 |
if (proxy != null && proxy.isUnresolved()) { |
|
158 |
// The default proxy selector may select a proxy whose address is |
|
159 |
// unresolved. We must resolve the address before connecting to it. |
|
160 |
proxy = new InetSocketAddress(proxy.getHostString(), proxy.getPort()); |
|
161 |
} |
|
162 |
boolean secure = request.secure(); |
|
163 |
ConnectionPool pool = client.connectionPool(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
164 |
|
48083 | 165 |
if (!secure) { |
166 |
c = pool.getConnection(false, addr, proxy); |
|
167 |
if (c != null && c.isOpen() /* may have been eof/closed when in the pool */) { |
|
168 |
final HttpConnection conn = c; |
|
169 |
DEBUG_LOGGER.log(Level.DEBUG, () -> conn.getConnectionFlow() |
|
170 |
+ ": plain connection retrieved from HTTP/1.1 pool"); |
|
171 |
return c; |
|
172 |
} else { |
|
173 |
return getPlainConnection(addr, proxy, request, client); |
|
174 |
} |
|
175 |
} else { // secure |
|
176 |
if (version != HTTP_2) { // only HTTP/1.1 connections are in the pool |
|
177 |
c = pool.getConnection(true, addr, proxy); |
|
178 |
} |
|
179 |
if (c != null && c.isOpen()) { |
|
180 |
final HttpConnection conn = c; |
|
181 |
DEBUG_LOGGER.log(Level.DEBUG, () -> conn.getConnectionFlow() |
|
182 |
+ ": SSL connection retrieved from HTTP/1.1 pool"); |
|
183 |
return c; |
|
184 |
} else { |
|
185 |
String[] alpn = null; |
|
186 |
if (version == HTTP_2) { |
|
187 |
alpn = new String[] { "h2", "http/1.1" }; |
|
188 |
} |
|
189 |
return getSSLConnection(addr, proxy, alpn, client); |
|
190 |
} |
|
191 |
} |
|
192 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
193 |
|
48083 | 194 |
private static HttpConnection getSSLConnection(InetSocketAddress addr, |
195 |
InetSocketAddress proxy, |
|
196 |
String[] alpn, |
|
197 |
HttpClientImpl client) { |
|
198 |
if (proxy != null) |
|
199 |
return new AsyncSSLTunnelConnection(addr, client, alpn, proxy); |
|
200 |
else |
|
201 |
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
|
202 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
203 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
204 |
/* 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
|
205 |
* for proxied WebSocket */ |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
206 |
private static HttpConnection getPlainConnection(InetSocketAddress addr, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
207 |
InetSocketAddress proxy, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
208 |
HttpRequestImpl request, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
209 |
HttpClientImpl client) { |
48083 | 210 |
if (request.isWebSocket() && proxy != null) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
211 |
return new PlainTunnelingConnection(addr, proxy, client); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
212 |
|
48083 | 213 |
if (proxy == null) |
214 |
return new PlainHttpConnection(addr, client); |
|
215 |
else |
|
216 |
return new PlainProxyConnection(proxy, client); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
218 |
|
48083 | 219 |
void closeOrReturnToCache(HttpHeaders hdrs) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
220 |
if (hdrs == null) { |
48083 | 221 |
// 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
|
222 |
close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
223 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
if (!isOpen()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
} |
48083 | 228 |
HttpClientImpl client = client(); |
229 |
if (client == null) { |
|
230 |
close(); |
|
231 |
return; |
|
232 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
233 |
ConnectionPool pool = client.connectionPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
234 |
boolean keepAlive = hdrs.firstValue("Connection") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
.map((s) -> !s.equalsIgnoreCase("close")) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
236 |
.orElse(true); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
237 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
if (keepAlive) { |
48083 | 239 |
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
|
240 |
pool.returnToPool(this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
241 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
244 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
245 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
246 |
abstract SocketChannel channel(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
247 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
248 |
final InetSocketAddress address() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
249 |
return address; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
250 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
251 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
252 |
abstract ConnectionPool.CacheKey cacheKey(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
253 |
|
48083 | 254 |
// // overridden in SSL only |
255 |
// SSLParameters sslParameters() { |
|
256 |
// return null; |
|
257 |
// } |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
258 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
259 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
260 |
* 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
|
261 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
262 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
263 |
public abstract void close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
264 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
265 |
abstract void shutdownInput() throws IOException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
266 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
267 |
abstract void shutdownOutput() throws IOException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
268 |
|
48083 | 269 |
// Support for WebSocket/RawChannelImpl which unfortunately |
270 |
// still depends on synchronous read/writes. |
|
271 |
// It should be removed when RawChannelImpl moves to using asynchronous APIs. |
|
272 |
abstract static class DetachedConnectionChannel implements Closeable { |
|
273 |
DetachedConnectionChannel() {} |
|
274 |
abstract SocketChannel channel(); |
|
275 |
abstract long write(ByteBuffer[] buffers, int start, int number) |
|
276 |
throws IOException; |
|
277 |
abstract void shutdownInput() throws IOException; |
|
278 |
abstract void shutdownOutput() throws IOException; |
|
279 |
abstract ByteBuffer read() throws IOException; |
|
280 |
@Override |
|
281 |
public abstract void close(); |
|
282 |
@Override |
|
283 |
public String toString() { |
|
284 |
return this.getClass().getSimpleName() + ": " + channel().toString(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
285 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
286 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
287 |
|
48083 | 288 |
// Support for WebSocket/RawChannelImpl which unfortunately |
289 |
// still depends on synchronous read/writes. |
|
290 |
// It should be removed when RawChannelImpl moves to using asynchronous APIs. |
|
291 |
abstract DetachedConnectionChannel detachChannel(); |
|
292 |
||
293 |
abstract FlowTube getConnectionFlow(); |
|
294 |
||
295 |
/** |
|
296 |
* A publisher that makes it possible to publish (write) |
|
297 |
* ordered (normal priority) and unordered (high priority) |
|
298 |
* buffers downstream. |
|
299 |
*/ |
|
300 |
final class PlainHttpPublisher implements HttpPublisher { |
|
301 |
final Object reading; |
|
302 |
PlainHttpPublisher() { |
|
303 |
this(new Object()); |
|
304 |
} |
|
305 |
PlainHttpPublisher(Object readingLock) { |
|
306 |
this.reading = readingLock; |
|
307 |
} |
|
308 |
final ConcurrentLinkedDeque<List<ByteBuffer>> queue = new ConcurrentLinkedDeque<>(); |
|
309 |
volatile Flow.Subscriber<? super List<ByteBuffer>> subscriber; |
|
310 |
volatile HttpWriteSubscription subscription; |
|
311 |
final SequentialScheduler writeScheduler = |
|
312 |
new SequentialScheduler(this::flushTask); |
|
313 |
@Override |
|
314 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> subscriber) { |
|
315 |
synchronized (reading) { |
|
316 |
//assert this.subscription == null; |
|
317 |
//assert this.subscriber == null; |
|
318 |
if (subscription == null) { |
|
319 |
subscription = new HttpWriteSubscription(); |
|
320 |
} |
|
321 |
this.subscriber = subscriber; |
|
322 |
} |
|
323 |
// TODO: should we do this in the flow? |
|
324 |
subscriber.onSubscribe(subscription); |
|
325 |
signal(); |
|
326 |
} |
|
327 |
||
328 |
void flushTask(DeferredCompleter completer) { |
|
329 |
try { |
|
330 |
HttpWriteSubscription sub = subscription; |
|
331 |
if (sub != null) sub.flush(); |
|
332 |
} finally { |
|
333 |
completer.complete(); |
|
334 |
} |
|
335 |
} |
|
336 |
||
337 |
void signal() { |
|
338 |
writeScheduler.runOrSchedule(); |
|
339 |
} |
|
340 |
||
341 |
final class HttpWriteSubscription implements Flow.Subscription { |
|
342 |
final Demand demand = new Demand(); |
|
343 |
||
344 |
@Override |
|
345 |
public void request(long n) { |
|
346 |
if (n <= 0) throw new IllegalArgumentException("non-positive request"); |
|
347 |
demand.increase(n); |
|
348 |
debug.log(Level.DEBUG, () -> "HttpPublisher: got request of " |
|
349 |
+ n + " from " |
|
350 |
+ getConnectionFlow()); |
|
351 |
writeScheduler.runOrSchedule(); |
|
352 |
} |
|
353 |
||
354 |
@Override |
|
355 |
public void cancel() { |
|
356 |
debug.log(Level.DEBUG, () -> "HttpPublisher: cancelled by " |
|
357 |
+ getConnectionFlow()); |
|
358 |
} |
|
359 |
||
360 |
void flush() { |
|
361 |
while (!queue.isEmpty() && demand.tryDecrement()) { |
|
362 |
List<ByteBuffer> elem = queue.poll(); |
|
363 |
debug.log(Level.DEBUG, () -> "HttpPublisher: sending " |
|
364 |
+ Utils.remaining(elem) + " bytes (" |
|
365 |
+ elem.size() + " buffers) to " |
|
366 |
+ getConnectionFlow()); |
|
367 |
subscriber.onNext(elem); |
|
368 |
} |
|
369 |
} |
|
370 |
} |
|
371 |
||
372 |
@Override |
|
373 |
public void enqueue(List<ByteBuffer> buffers) throws IOException { |
|
374 |
queue.add(buffers); |
|
375 |
int bytes = buffers.stream().mapToInt(ByteBuffer::remaining).sum(); |
|
376 |
debug.log(Level.DEBUG, "added %d bytes to the write queue", bytes); |
|
377 |
} |
|
378 |
||
379 |
@Override |
|
380 |
public void enqueueUnordered(List<ByteBuffer> buffers) throws IOException { |
|
381 |
// Unordered frames are sent before existing frames. |
|
382 |
int bytes = buffers.stream().mapToInt(ByteBuffer::remaining).sum(); |
|
383 |
queue.addFirst(buffers); |
|
384 |
debug.log(Level.DEBUG, "inserted %d bytes in the write queue", bytes); |
|
385 |
} |
|
386 |
||
387 |
@Override |
|
388 |
public void signalEnqueued() throws IOException { |
|
389 |
debug.log(Level.DEBUG, "signalling the publisher of the write queue"); |
|
390 |
signal(); |
|
391 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
392 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
393 |
|
48083 | 394 |
String dbgTag = null; |
395 |
final String dbgString() { |
|
396 |
FlowTube flow = getConnectionFlow(); |
|
397 |
String tag = dbgTag; |
|
398 |
if (tag == null && flow != null) { |
|
399 |
dbgTag = tag = this.getClass().getSimpleName() + "(" + flow + ")"; |
|
400 |
} else if (tag == null) { |
|
401 |
tag = this.getClass().getSimpleName() + "(?)"; |
|
402 |
} |
|
403 |
return tag; |
|
404 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
405 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
406 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
407 |
public String toString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
408 |
return "HttpConnection: " + channel().toString(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
409 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
410 |
} |