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