author | chegar |
Wed, 11 Apr 2018 15:00:35 +0100 | |
branch | http-client-branch |
changeset 56417 | 312811f70c43 |
parent 56335 | 7e56c39fa1fa |
child 56437 | f8b3f053cfbb |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
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 |
|
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
26 |
package jdk.internal.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.Closeable; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.io.IOException; |
48083 | 30 |
import java.lang.System.Logger.Level; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.net.InetSocketAddress; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.nio.ByteBuffer; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.nio.channels.SocketChannel; |
56049
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
34 |
import java.util.Arrays; |
48083 | 35 |
import java.util.IdentityHashMap; |
36 |
import java.util.List; |
|
37 |
import java.util.Map; |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
38 |
import java.util.TreeMap; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
39 |
import java.util.concurrent.CompletableFuture; |
48083 | 40 |
import java.util.concurrent.CompletionStage; |
41 |
import java.util.concurrent.ConcurrentLinkedDeque; |
|
42 |
import java.util.concurrent.Flow; |
|
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
43 |
import java.util.function.BiPredicate; |
56049
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
44 |
import java.util.function.Predicate; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
45 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
46 |
import java.net.http.HttpClient.Version; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
47 |
import java.net.http.HttpHeaders; |
56092
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
48 |
import jdk.internal.net.http.common.Demand; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
49 |
import jdk.internal.net.http.common.FlowTube; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
50 |
import jdk.internal.net.http.common.SequentialScheduler; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
51 |
import jdk.internal.net.http.common.SequentialScheduler.DeferredCompleter; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
52 |
import jdk.internal.net.http.common.Log; |
fd85b2bf2b0d
http-client-branch: move implementation to jdk.internal.net.http
chegar
parents:
56089
diff
changeset
|
53 |
import jdk.internal.net.http.common.Utils; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56079
diff
changeset
|
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 |
|
48083 | 68 |
static final boolean DEBUG = Utils.DEBUG; // Revisit: temporary dev flag. |
69 |
final System.Logger debug = Utils.getDebugLogger(this::dbgString, DEBUG); |
|
70 |
final static System.Logger DEBUG_LOGGER = Utils.getDebugLogger( |
|
71 |
() -> "HttpConnection(SocketTube(?))", DEBUG); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
72 |
|
48083 | 73 |
/** The address this connection is connected to. Could be a server or a proxy. */ |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
74 |
final InetSocketAddress address; |
48083 | 75 |
private final HttpClientImpl client; |
76 |
private final TrailingOperations trailingOperations; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
HttpConnection(InetSocketAddress address, HttpClientImpl client) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
this.address = address; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
80 |
this.client = client; |
48083 | 81 |
trailingOperations = new TrailingOperations(); |
82 |
} |
|
83 |
||
84 |
private static final class TrailingOperations { |
|
85 |
private final Map<CompletionStage<?>, Boolean> operations = |
|
86 |
new IdentityHashMap<>(); |
|
87 |
void add(CompletionStage<?> cf) { |
|
88 |
synchronized(operations) { |
|
89 |
cf.whenComplete((r,t)-> remove(cf)); |
|
90 |
operations.put(cf, Boolean.TRUE); |
|
91 |
} |
|
92 |
} |
|
93 |
boolean remove(CompletionStage<?> cf) { |
|
94 |
synchronized(operations) { |
|
95 |
return operations.remove(cf); |
|
96 |
} |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
final void addTrailingOperation(CompletionStage<?> cf) { |
|
101 |
trailingOperations.add(cf); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
102 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
103 |
|
48083 | 104 |
// final void removeTrailingOperation(CompletableFuture<?> cf) { |
105 |
// trailingOperations.remove(cf); |
|
106 |
// } |
|
107 |
||
108 |
final HttpClientImpl client() { |
|
109 |
return client; |
|
110 |
} |
|
111 |
||
112 |
//public abstract void connect() throws IOException, InterruptedException; |
|
113 |
||
114 |
public abstract CompletableFuture<Void> connectAsync(); |
|
115 |
||
116 |
/** Tells whether, or not, this connection is connected to its destination. */ |
|
117 |
abstract boolean connected(); |
|
118 |
||
119 |
/** Tells whether, or not, this connection is secure ( over SSL ) */ |
|
120 |
abstract boolean isSecure(); |
|
121 |
||
56128
249a863b0aca
http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
56092
diff
changeset
|
122 |
/** |
249a863b0aca
http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
56092
diff
changeset
|
123 |
* Tells whether, or not, this connection is proxied. |
249a863b0aca
http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
56092
diff
changeset
|
124 |
* Returns true for tunnel connections, or clear connection to |
249a863b0aca
http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
56092
diff
changeset
|
125 |
* any host through proxy. |
249a863b0aca
http-client-branch: 8196967: HTTP1/HTTP2 Proxy Issue
dfuchs
parents:
56092
diff
changeset
|
126 |
*/ |
48083 | 127 |
abstract boolean isProxied(); |
128 |
||
129 |
/** Tells whether, or not, this connection is open. */ |
|
130 |
final boolean isOpen() { |
|
131 |
return channel().isOpen() && |
|
132 |
(connected() ? !getConnectionFlow().isFinished() : true); |
|
133 |
} |
|
134 |
||
135 |
interface HttpPublisher extends FlowTube.TubePublisher { |
|
136 |
void enqueue(List<ByteBuffer> buffers) throws IOException; |
|
137 |
void enqueueUnordered(List<ByteBuffer> buffers) throws IOException; |
|
138 |
void signalEnqueued() throws IOException; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
139 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
141 |
/** |
48083 | 142 |
* Returns the HTTP publisher associated with this connection. May be null |
143 |
* if invoked before connecting. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
144 |
*/ |
48083 | 145 |
abstract HttpPublisher publisher(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
146 |
|
56049
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
147 |
// HTTP/2 MUST use TLS version 1.2 or higher for HTTP/2 over TLS |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
148 |
private static final Predicate<String> testRequiredHTTP2TLSVersion = proto -> |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
149 |
proto.equals("TLSv1.2") || proto.equals("TLSv1.3"); |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
150 |
|
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
151 |
/** |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
152 |
* Returns true if the given client's SSL parameter protocols contains at |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
153 |
* least one TLS version that HTTP/2 requires. |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
154 |
*/ |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
155 |
private static final boolean hasRequiredHTTP2TLSVersion(HttpClient client) { |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
156 |
String[] protos = client.sslParameters().getProtocols(); |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
157 |
if (protos != null) { |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
158 |
return Arrays.stream(protos).filter(testRequiredHTTP2TLSVersion).findAny().isPresent(); |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
159 |
} else { |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
160 |
return false; |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
161 |
} |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
162 |
} |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
163 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
164 |
/** |
48083 | 165 |
* Factory for retrieving HttpConnections. A connection can be retrieved |
166 |
* from the connection pool, or a new one created if none available. |
|
167 |
* |
|
168 |
* The given {@code addr} is the ultimate destination. Any proxies, |
|
169 |
* etc, are determined from the request. Returns a concrete instance which |
|
170 |
* is one of the following: |
|
171 |
* {@link PlainHttpConnection} |
|
172 |
* {@link PlainTunnelingConnection} |
|
173 |
* |
|
174 |
* The returned connection, if not from the connection pool, must have its, |
|
175 |
* connect() or connectAsync() method invoked, which ( when it completes |
|
176 |
* successfully ) renders the connection usable for requests. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
177 |
*/ |
48083 | 178 |
public static HttpConnection getConnection(InetSocketAddress addr, |
179 |
HttpClientImpl client, |
|
180 |
HttpRequestImpl request, |
|
181 |
Version version) { |
|
56165
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56128
diff
changeset
|
182 |
// The default proxy selector may select a proxy whose address is |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56128
diff
changeset
|
183 |
// unresolved. We must resolve the address before connecting to it. |
8a6065d830b9
http-client-branch: Add a test for BodySubscribers throwing in getBody() or returning exceptionally completed CFs
dfuchs
parents:
56128
diff
changeset
|
184 |
InetSocketAddress proxy = Utils.resolveAddress(request.proxy()); |
48083 | 185 |
HttpConnection c = null; |
186 |
boolean secure = request.secure(); |
|
187 |
ConnectionPool pool = client.connectionPool(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
|
48083 | 189 |
if (!secure) { |
190 |
c = pool.getConnection(false, addr, proxy); |
|
191 |
if (c != null && c.isOpen() /* may have been eof/closed when in the pool */) { |
|
192 |
final HttpConnection conn = c; |
|
193 |
DEBUG_LOGGER.log(Level.DEBUG, () -> conn.getConnectionFlow() |
|
194 |
+ ": plain connection retrieved from HTTP/1.1 pool"); |
|
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; |
|
205 |
DEBUG_LOGGER.log(Level.DEBUG, () -> conn.getConnectionFlow() |
|
206 |
+ ": SSL connection retrieved from HTTP/1.1 pool"); |
|
207 |
return c; |
|
208 |
} else { |
|
209 |
String[] alpn = null; |
|
56049
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
210 |
if (version == HTTP_2 && hasRequiredHTTP2TLSVersion(client)) { |
48083 | 211 |
alpn = new String[] { "h2", "http/1.1" }; |
212 |
} |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
213 |
return getSSLConnection(addr, proxy, alpn, request, client); |
48083 | 214 |
} |
215 |
} |
|
216 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
217 |
|
48083 | 218 |
private static HttpConnection getSSLConnection(InetSocketAddress addr, |
219 |
InetSocketAddress proxy, |
|
220 |
String[] alpn, |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
221 |
HttpRequestImpl request, |
48083 | 222 |
HttpClientImpl client) { |
223 |
if (proxy != null) |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
224 |
return new AsyncSSLTunnelConnection(addr, client, alpn, proxy, |
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
225 |
proxyTunnelHeaders(request)); |
48083 | 226 |
else |
227 |
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
|
228 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
|
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
230 |
/** |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
231 |
* This method is used to build a filter that will accept or |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
232 |
* veto (header-name, value) tuple for transmission on the |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
233 |
* wire. |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
234 |
* The filter is applied to the headers when sending the headers |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
235 |
* to the remote party. |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
236 |
* Which tuple is accepted/vetoed depends on: |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
237 |
* <pre> |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
238 |
* - whether the connection is a tunnel connection |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
239 |
* [talking to a server through a proxy tunnel] |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
240 |
* - whether the method is CONNECT |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
241 |
* [establishing a CONNECT tunnel through a proxy] |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
242 |
* - whether the request is using a proxy |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
243 |
* (and the connection is not a tunnel) |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
244 |
* [talking to a server through a proxy] |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
245 |
* - whether the request is a direct connection to |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
246 |
* a server (no tunnel, no proxy). |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
247 |
* </pre> |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
248 |
* @param request |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
249 |
* @return |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
250 |
*/ |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
251 |
BiPredicate<String,List<String>> headerFilter(HttpRequestImpl request) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
252 |
if (isTunnel()) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
253 |
// talking to a server through a proxy tunnel |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
254 |
// don't send proxy-* headers to a plain server |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
255 |
assert !request.isConnect(); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
256 |
return Utils.NO_PROXY_HEADERS_FILTER; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
257 |
} else if (request.isConnect()) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
258 |
// establishing a proxy tunnel |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
259 |
// check for proxy tunnel disabled schemes |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
260 |
// assert !this.isTunnel(); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
261 |
assert request.proxy() == null; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
262 |
return Utils.PROXY_TUNNEL_FILTER; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
263 |
} else if (request.proxy() != null) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
264 |
// talking to a server through a proxy (no tunnel) |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
265 |
// check for proxy disabled schemes |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
266 |
// assert !isTunnel() && !request.isConnect(); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
267 |
return Utils.PROXY_FILTER; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
268 |
} else { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
269 |
// talking to a server directly (no tunnel, no proxy) |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
270 |
// don't send proxy-* headers to a plain server |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
271 |
// assert request.proxy() == null && !request.isConnect(); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
272 |
return Utils.NO_PROXY_HEADERS_FILTER; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
273 |
} |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
274 |
} |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
275 |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
276 |
// Composes a new immutable HttpHeaders that combines the |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
277 |
// user and system header but only keeps those headers that |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
278 |
// start with "proxy-" |
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
279 |
private static HttpHeaders proxyTunnelHeaders(HttpRequestImpl request) { |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
280 |
Map<String, List<String>> combined = new TreeMap<>(String.CASE_INSENSITIVE_ORDER); |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
281 |
combined.putAll(request.getSystemHeaders().map()); |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
282 |
combined.putAll(request.headers().map()); // let user override system |
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
283 |
|
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
284 |
// keep only proxy-* - and also strip authorization headers |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
285 |
// for disabled schemes |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
286 |
return ImmutableHeaders.of(combined, Utils.PROXY_TUNNEL_FILTER); |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
287 |
} |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
288 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
289 |
/* 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
|
290 |
* for proxied WebSocket */ |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
291 |
private static HttpConnection getPlainConnection(InetSocketAddress addr, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
292 |
InetSocketAddress proxy, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
293 |
HttpRequestImpl request, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
294 |
HttpClientImpl client) { |
48083 | 295 |
if (request.isWebSocket() && proxy != null) |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
296 |
return new PlainTunnelingConnection(addr, proxy, client, |
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
297 |
proxyTunnelHeaders(request)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
298 |
|
48083 | 299 |
if (proxy == null) |
300 |
return new PlainHttpConnection(addr, client); |
|
301 |
else |
|
302 |
return new PlainProxyConnection(proxy, client); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
|
48083 | 305 |
void closeOrReturnToCache(HttpHeaders hdrs) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
306 |
if (hdrs == null) { |
48083 | 307 |
// 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
|
308 |
close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
309 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
310 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
311 |
if (!isOpen()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
312 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
313 |
} |
48083 | 314 |
HttpClientImpl client = client(); |
315 |
if (client == null) { |
|
316 |
close(); |
|
317 |
return; |
|
318 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
319 |
ConnectionPool pool = client.connectionPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
320 |
boolean keepAlive = hdrs.firstValue("Connection") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
321 |
.map((s) -> !s.equalsIgnoreCase("close")) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
322 |
.orElse(true); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
323 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
324 |
if (keepAlive) { |
48083 | 325 |
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
|
326 |
pool.returnToPool(this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
327 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
328 |
close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
} |
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 |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
332 |
/* Tells whether or not this connection is a tunnel through a proxy */ |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
333 |
boolean isTunnel() { return false; } |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
334 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
335 |
abstract SocketChannel channel(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
336 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
337 |
final InetSocketAddress address() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
338 |
return address; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
339 |
} |
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 |
abstract ConnectionPool.CacheKey cacheKey(); |
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 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
344 |
* 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
|
345 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
347 |
public abstract void close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
|
48083 | 349 |
abstract FlowTube getConnectionFlow(); |
350 |
||
351 |
/** |
|
352 |
* A publisher that makes it possible to publish (write) |
|
353 |
* ordered (normal priority) and unordered (high priority) |
|
354 |
* buffers downstream. |
|
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<>(); |
|
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
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); |
|
405 |
debug.log(Level.DEBUG, () -> "HttpPublisher: got request of " |
|
406 |
+ n + " from " |
|
407 |
+ getConnectionFlow()); |
|
408 |
writeScheduler.runOrSchedule(); |
|
409 |
} |
|
410 |
||
411 |
@Override |
|
412 |
public void cancel() { |
|
413 |
debug.log(Level.DEBUG, () -> "HttpPublisher: cancelled by " |
|
414 |
+ getConnectionFlow()); |
|
415 |
} |
|
416 |
||
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
417 |
private boolean isEmpty() { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
418 |
return queue.isEmpty() && priority.isEmpty(); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
419 |
} |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
420 |
|
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
421 |
private List<ByteBuffer> poll() { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
422 |
List<ByteBuffer> elem = priority.poll(); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
423 |
return elem == null ? queue.poll() : elem; |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
424 |
} |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
425 |
|
48083 | 426 |
void flush() { |
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
427 |
while (!isEmpty() && demand.tryDecrement()) { |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
428 |
List<ByteBuffer> elem = poll(); |
48083 | 429 |
debug.log(Level.DEBUG, () -> "HttpPublisher: sending " |
430 |
+ Utils.remaining(elem) + " bytes (" |
|
431 |
+ elem.size() + " buffers) to " |
|
432 |
+ getConnectionFlow()); |
|
433 |
subscriber.onNext(elem); |
|
434 |
} |
|
435 |
} |
|
436 |
} |
|
437 |
||
438 |
@Override |
|
439 |
public void enqueue(List<ByteBuffer> buffers) throws IOException { |
|
440 |
queue.add(buffers); |
|
441 |
int bytes = buffers.stream().mapToInt(ByteBuffer::remaining).sum(); |
|
442 |
debug.log(Level.DEBUG, "added %d bytes to the write queue", bytes); |
|
443 |
} |
|
444 |
||
445 |
@Override |
|
446 |
public void enqueueUnordered(List<ByteBuffer> buffers) throws IOException { |
|
447 |
// Unordered frames are sent before existing frames. |
|
448 |
int bytes = buffers.stream().mapToInt(ByteBuffer::remaining).sum(); |
|
56252
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
449 |
priority.add(buffers); |
e4b05854c51f
http-client-branch: Fix race candition triggered by HTTP/1.1 connection reuse in SocketTube::connectFlows
dfuchs
parents:
56165
diff
changeset
|
450 |
debug.log(Level.DEBUG, "added %d bytes in the priority write queue", bytes); |
48083 | 451 |
} |
452 |
||
453 |
@Override |
|
454 |
public void signalEnqueued() throws IOException { |
|
455 |
debug.log(Level.DEBUG, "signalling the publisher of the write queue"); |
|
456 |
signal(); |
|
457 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
458 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
459 |
|
48083 | 460 |
String dbgTag = null; |
461 |
final String dbgString() { |
|
462 |
FlowTube flow = getConnectionFlow(); |
|
463 |
String tag = dbgTag; |
|
464 |
if (tag == null && flow != null) { |
|
465 |
dbgTag = tag = this.getClass().getSimpleName() + "(" + flow + ")"; |
|
466 |
} else if (tag == null) { |
|
467 |
tag = this.getClass().getSimpleName() + "(?)"; |
|
468 |
} |
|
469 |
return tag; |
|
470 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
471 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
472 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
473 |
public String toString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
474 |
return "HttpConnection: " + channel().toString(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
475 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
476 |
} |