author | chegar |
Tue, 06 Feb 2018 14:10:28 +0000 | |
branch | http-client-branch |
changeset 56079 | d23b02f37fce |
parent 56054 | src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/HttpConnection.java@352e845ae744 |
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 |
|
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56054
diff
changeset
|
26 |
package jdk.incubator.http.internal; |
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; |
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56054
diff
changeset
|
45 |
import jdk.incubator.http.HttpClient; |
48083 | 46 |
import jdk.incubator.http.HttpClient.Version; |
56079
d23b02f37fce
http-client-branch: more remaining impl types to internal
chegar
parents:
56054
diff
changeset
|
47 |
import jdk.incubator.http.HttpHeaders; |
48083 | 48 |
import jdk.incubator.http.internal.common.Demand; |
49 |
import jdk.incubator.http.internal.common.FlowTube; |
|
50 |
import jdk.incubator.http.internal.common.SequentialScheduler; |
|
51 |
import jdk.incubator.http.internal.common.SequentialScheduler.DeferredCompleter; |
|
52 |
import jdk.incubator.http.internal.common.Log; |
|
53 |
import jdk.incubator.http.internal.common.Utils; |
|
54 |
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
|
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 |
||
122 |
/** Tells whether, or not, this connection is proxied. */ |
|
123 |
abstract boolean isProxied(); |
|
124 |
||
125 |
/** Tells whether, or not, this connection is open. */ |
|
126 |
final boolean isOpen() { |
|
127 |
return channel().isOpen() && |
|
128 |
(connected() ? !getConnectionFlow().isFinished() : true); |
|
129 |
} |
|
130 |
||
131 |
interface HttpPublisher extends FlowTube.TubePublisher { |
|
132 |
void enqueue(List<ByteBuffer> buffers) throws IOException; |
|
133 |
void enqueueUnordered(List<ByteBuffer> buffers) throws IOException; |
|
134 |
void signalEnqueued() throws IOException; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
135 |
} |
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 |
* Returns the HTTP publisher associated with this connection. May be null |
139 |
* if invoked before connecting. |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
140 |
*/ |
48083 | 141 |
abstract HttpPublisher publisher(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
142 |
|
56049
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
143 |
// 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
|
144 |
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
|
145 |
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
|
146 |
|
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
147 |
/** |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
148 |
* 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
|
149 |
* 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
|
150 |
*/ |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
151 |
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
|
152 |
String[] protos = client.sslParameters().getProtocols(); |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
153 |
if (protos != null) { |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
154 |
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
|
155 |
} else { |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
156 |
return false; |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
157 |
} |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
158 |
} |
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
159 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
160 |
/** |
48083 | 161 |
* Factory for retrieving HttpConnections. A connection can be retrieved |
162 |
* from the connection pool, or a new one created if none available. |
|
163 |
* |
|
164 |
* The given {@code addr} is the ultimate destination. Any proxies, |
|
165 |
* etc, are determined from the request. Returns a concrete instance which |
|
166 |
* is one of the following: |
|
167 |
* {@link PlainHttpConnection} |
|
168 |
* {@link PlainTunnelingConnection} |
|
169 |
* |
|
170 |
* The returned connection, if not from the connection pool, must have its, |
|
171 |
* connect() or connectAsync() method invoked, which ( when it completes |
|
172 |
* 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
|
173 |
*/ |
48083 | 174 |
public static HttpConnection getConnection(InetSocketAddress addr, |
175 |
HttpClientImpl client, |
|
176 |
HttpRequestImpl request, |
|
177 |
Version version) { |
|
178 |
HttpConnection c = null; |
|
179 |
InetSocketAddress proxy = request.proxy(); |
|
180 |
if (proxy != null && proxy.isUnresolved()) { |
|
181 |
// The default proxy selector may select a proxy whose address is |
|
182 |
// unresolved. We must resolve the address before connecting to it. |
|
183 |
proxy = new InetSocketAddress(proxy.getHostString(), proxy.getPort()); |
|
184 |
} |
|
185 |
boolean secure = request.secure(); |
|
186 |
ConnectionPool pool = client.connectionPool(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
|
48083 | 188 |
if (!secure) { |
189 |
c = pool.getConnection(false, addr, proxy); |
|
190 |
if (c != null && c.isOpen() /* may have been eof/closed when in the pool */) { |
|
191 |
final HttpConnection conn = c; |
|
192 |
DEBUG_LOGGER.log(Level.DEBUG, () -> conn.getConnectionFlow() |
|
193 |
+ ": plain connection retrieved from HTTP/1.1 pool"); |
|
194 |
return c; |
|
195 |
} else { |
|
196 |
return getPlainConnection(addr, proxy, request, client); |
|
197 |
} |
|
198 |
} else { // secure |
|
199 |
if (version != HTTP_2) { // only HTTP/1.1 connections are in the pool |
|
200 |
c = pool.getConnection(true, addr, proxy); |
|
201 |
} |
|
202 |
if (c != null && c.isOpen()) { |
|
203 |
final HttpConnection conn = c; |
|
204 |
DEBUG_LOGGER.log(Level.DEBUG, () -> conn.getConnectionFlow() |
|
205 |
+ ": SSL connection retrieved from HTTP/1.1 pool"); |
|
206 |
return c; |
|
207 |
} else { |
|
208 |
String[] alpn = null; |
|
56049
cf81ddfb20a8
http-client-branch: only add h2 ALPN if TLSv1.2 or greater
chegar
parents:
56041
diff
changeset
|
209 |
if (version == HTTP_2 && hasRequiredHTTP2TLSVersion(client)) { |
48083 | 210 |
alpn = new String[] { "h2", "http/1.1" }; |
211 |
} |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
212 |
return getSSLConnection(addr, proxy, alpn, request, client); |
48083 | 213 |
} |
214 |
} |
|
215 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
216 |
|
48083 | 217 |
private static HttpConnection getSSLConnection(InetSocketAddress addr, |
218 |
InetSocketAddress proxy, |
|
219 |
String[] alpn, |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
220 |
HttpRequestImpl request, |
48083 | 221 |
HttpClientImpl client) { |
222 |
if (proxy != null) |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
223 |
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
|
224 |
proxyTunnelHeaders(request)); |
48083 | 225 |
else |
226 |
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
|
227 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
|
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
229 |
/** |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
230 |
* 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
|
231 |
* 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
|
232 |
* wire. |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
233 |
* 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
|
234 |
* to the remote party. |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
235 |
* 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
|
236 |
* <pre> |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
237 |
* - 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
|
238 |
* [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
|
239 |
* - whether the method is CONNECT |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
240 |
* [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
|
241 |
* - 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
|
242 |
* (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
|
243 |
* [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
|
244 |
* - 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
|
245 |
* 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
|
246 |
* </pre> |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
247 |
* @param request |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
248 |
* @return |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
249 |
*/ |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
250 |
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
|
251 |
if (isTunnel()) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
252 |
// 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
|
253 |
// 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
|
254 |
assert !request.isConnect(); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
255 |
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
|
256 |
} else if (request.isConnect()) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
257 |
// establishing a proxy tunnel |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
258 |
// 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
|
259 |
// assert !this.isTunnel(); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
260 |
assert request.proxy() == null; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
261 |
return Utils.PROXY_TUNNEL_FILTER; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
262 |
} else if (request.proxy() != null) { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
263 |
// 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
|
264 |
// check for proxy disabled schemes |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
265 |
// assert !isTunnel() && !request.isConnect(); |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
266 |
return Utils.PROXY_FILTER; |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
267 |
} else { |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
268 |
// 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
|
269 |
// 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
|
270 |
// 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
|
271 |
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
|
272 |
} |
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 |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
275 |
// 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
|
276 |
// 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
|
277 |
// start with "proxy-" |
56054
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
278 |
private static HttpHeaders proxyTunnelHeaders(HttpRequestImpl request) { |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
279 |
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
|
280 |
combined.putAll(request.getSystemHeaders().map()); |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
281 |
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
|
282 |
|
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
283 |
// 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
|
284 |
// for disabled schemes |
352e845ae744
http-client-branch: honor legacy jdk.http.auth.* property when handling proxy-authorization
dfuchs
parents:
56049
diff
changeset
|
285 |
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
|
286 |
} |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
287 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
288 |
/* 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
|
289 |
* for proxied WebSocket */ |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
290 |
private static HttpConnection getPlainConnection(InetSocketAddress addr, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
291 |
InetSocketAddress proxy, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
292 |
HttpRequestImpl request, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
293 |
HttpClientImpl client) { |
48083 | 294 |
if (request.isWebSocket() && proxy != null) |
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
295 |
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
|
296 |
proxyTunnelHeaders(request)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
297 |
|
48083 | 298 |
if (proxy == null) |
299 |
return new PlainHttpConnection(addr, client); |
|
300 |
else |
|
301 |
return new PlainProxyConnection(proxy, client); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
|
48083 | 304 |
void closeOrReturnToCache(HttpHeaders hdrs) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
305 |
if (hdrs == null) { |
48083 | 306 |
// 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
|
307 |
close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
308 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
309 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
310 |
if (!isOpen()) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
311 |
return; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
312 |
} |
48083 | 313 |
HttpClientImpl client = client(); |
314 |
if (client == null) { |
|
315 |
close(); |
|
316 |
return; |
|
317 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
318 |
ConnectionPool pool = client.connectionPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
319 |
boolean keepAlive = hdrs.firstValue("Connection") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
320 |
.map((s) -> !s.equalsIgnoreCase("close")) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
321 |
.orElse(true); |
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 (keepAlive) { |
48083 | 324 |
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
|
325 |
pool.returnToPool(this); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
326 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
327 |
close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
328 |
} |
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 |
|
56041
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
331 |
/* 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
|
332 |
boolean isTunnel() { return false; } |
b4b5e09ef3cc
http-client-branch: make it possible to supply proxy-authorization headers
dfuchs
parents:
55973
diff
changeset
|
333 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
334 |
abstract SocketChannel channel(); |
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 |
final InetSocketAddress address() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
337 |
return address; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
338 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
339 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
340 |
abstract ConnectionPool.CacheKey cacheKey(); |
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 |
* 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
|
344 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
345 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
public abstract void close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
347 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
abstract void shutdownInput() throws IOException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
350 |
abstract void shutdownOutput() throws IOException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
351 |
|
48083 | 352 |
// Support for WebSocket/RawChannelImpl which unfortunately |
353 |
// still depends on synchronous read/writes. |
|
354 |
// It should be removed when RawChannelImpl moves to using asynchronous APIs. |
|
355 |
abstract static class DetachedConnectionChannel implements Closeable { |
|
356 |
DetachedConnectionChannel() {} |
|
357 |
abstract SocketChannel channel(); |
|
358 |
abstract long write(ByteBuffer[] buffers, int start, int number) |
|
359 |
throws IOException; |
|
360 |
abstract void shutdownInput() throws IOException; |
|
361 |
abstract void shutdownOutput() throws IOException; |
|
362 |
abstract ByteBuffer read() throws IOException; |
|
363 |
@Override |
|
364 |
public abstract void close(); |
|
365 |
@Override |
|
366 |
public String toString() { |
|
367 |
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
|
368 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
370 |
|
48083 | 371 |
// Support for WebSocket/RawChannelImpl which unfortunately |
372 |
// still depends on synchronous read/writes. |
|
373 |
// It should be removed when RawChannelImpl moves to using asynchronous APIs. |
|
374 |
abstract DetachedConnectionChannel detachChannel(); |
|
375 |
||
376 |
abstract FlowTube getConnectionFlow(); |
|
377 |
||
378 |
/** |
|
379 |
* A publisher that makes it possible to publish (write) |
|
380 |
* ordered (normal priority) and unordered (high priority) |
|
381 |
* buffers downstream. |
|
382 |
*/ |
|
383 |
final class PlainHttpPublisher implements HttpPublisher { |
|
384 |
final Object reading; |
|
385 |
PlainHttpPublisher() { |
|
386 |
this(new Object()); |
|
387 |
} |
|
388 |
PlainHttpPublisher(Object readingLock) { |
|
389 |
this.reading = readingLock; |
|
390 |
} |
|
391 |
final ConcurrentLinkedDeque<List<ByteBuffer>> queue = new ConcurrentLinkedDeque<>(); |
|
392 |
volatile Flow.Subscriber<? super List<ByteBuffer>> subscriber; |
|
393 |
volatile HttpWriteSubscription subscription; |
|
394 |
final SequentialScheduler writeScheduler = |
|
395 |
new SequentialScheduler(this::flushTask); |
|
396 |
@Override |
|
397 |
public void subscribe(Flow.Subscriber<? super List<ByteBuffer>> subscriber) { |
|
398 |
synchronized (reading) { |
|
399 |
//assert this.subscription == null; |
|
400 |
//assert this.subscriber == null; |
|
401 |
if (subscription == null) { |
|
402 |
subscription = new HttpWriteSubscription(); |
|
403 |
} |
|
404 |
this.subscriber = subscriber; |
|
405 |
} |
|
406 |
// TODO: should we do this in the flow? |
|
407 |
subscriber.onSubscribe(subscription); |
|
408 |
signal(); |
|
409 |
} |
|
410 |
||
411 |
void flushTask(DeferredCompleter completer) { |
|
412 |
try { |
|
413 |
HttpWriteSubscription sub = subscription; |
|
414 |
if (sub != null) sub.flush(); |
|
415 |
} finally { |
|
416 |
completer.complete(); |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
void signal() { |
|
421 |
writeScheduler.runOrSchedule(); |
|
422 |
} |
|
423 |
||
424 |
final class HttpWriteSubscription implements Flow.Subscription { |
|
425 |
final Demand demand = new Demand(); |
|
426 |
||
427 |
@Override |
|
428 |
public void request(long n) { |
|
429 |
if (n <= 0) throw new IllegalArgumentException("non-positive request"); |
|
430 |
demand.increase(n); |
|
431 |
debug.log(Level.DEBUG, () -> "HttpPublisher: got request of " |
|
432 |
+ n + " from " |
|
433 |
+ getConnectionFlow()); |
|
434 |
writeScheduler.runOrSchedule(); |
|
435 |
} |
|
436 |
||
437 |
@Override |
|
438 |
public void cancel() { |
|
439 |
debug.log(Level.DEBUG, () -> "HttpPublisher: cancelled by " |
|
440 |
+ getConnectionFlow()); |
|
441 |
} |
|
442 |
||
443 |
void flush() { |
|
444 |
while (!queue.isEmpty() && demand.tryDecrement()) { |
|
445 |
List<ByteBuffer> elem = queue.poll(); |
|
446 |
debug.log(Level.DEBUG, () -> "HttpPublisher: sending " |
|
447 |
+ Utils.remaining(elem) + " bytes (" |
|
448 |
+ elem.size() + " buffers) to " |
|
449 |
+ getConnectionFlow()); |
|
450 |
subscriber.onNext(elem); |
|
451 |
} |
|
452 |
} |
|
453 |
} |
|
454 |
||
455 |
@Override |
|
456 |
public void enqueue(List<ByteBuffer> buffers) throws IOException { |
|
457 |
queue.add(buffers); |
|
458 |
int bytes = buffers.stream().mapToInt(ByteBuffer::remaining).sum(); |
|
459 |
debug.log(Level.DEBUG, "added %d bytes to the write queue", bytes); |
|
460 |
} |
|
461 |
||
462 |
@Override |
|
463 |
public void enqueueUnordered(List<ByteBuffer> buffers) throws IOException { |
|
464 |
// Unordered frames are sent before existing frames. |
|
465 |
int bytes = buffers.stream().mapToInt(ByteBuffer::remaining).sum(); |
|
466 |
queue.addFirst(buffers); |
|
467 |
debug.log(Level.DEBUG, "inserted %d bytes in the write queue", bytes); |
|
468 |
} |
|
469 |
||
470 |
@Override |
|
471 |
public void signalEnqueued() throws IOException { |
|
472 |
debug.log(Level.DEBUG, "signalling the publisher of the write queue"); |
|
473 |
signal(); |
|
474 |
} |
|
42460
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 |
|
48083 | 477 |
String dbgTag = null; |
478 |
final String dbgString() { |
|
479 |
FlowTube flow = getConnectionFlow(); |
|
480 |
String tag = dbgTag; |
|
481 |
if (tag == null && flow != null) { |
|
482 |
dbgTag = tag = this.getClass().getSimpleName() + "(" + flow + ")"; |
|
483 |
} else if (tag == null) { |
|
484 |
tag = this.getClass().getSimpleName() + "(?)"; |
|
485 |
} |
|
486 |
return tag; |
|
487 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
488 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
489 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
490 |
public String toString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
491 |
return "HttpConnection: " + channel().toString(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
492 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
493 |
} |