author | chegar |
Tue, 17 Apr 2018 08:54:17 -0700 | |
changeset 49765 | ee6f7a61f3a5 |
parent 48376 | src/jdk.incubator.httpclient/share/classes/jdk/incubator/http/Http2ClientImpl.java@41ae5c69b09c |
child 49944 | 4690a2871b44 |
child 56451 | 9585061fdb04 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
49765 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
49765 | 26 |
package jdk.internal.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.net.InetSocketAddress; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.net.URI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
30 |
import java.util.Base64; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.util.Collections; |
48083 | 32 |
import java.util.HashSet; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.util.Map; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
34 |
import java.util.Set; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
35 |
import java.util.concurrent.ConcurrentHashMap; |
48083 | 36 |
import java.util.concurrent.CompletableFuture; |
49765 | 37 |
import jdk.internal.net.http.common.Log; |
38 |
import jdk.internal.net.http.common.Logger; |
|
39 |
import jdk.internal.net.http.common.MinimalFuture; |
|
40 |
import jdk.internal.net.http.common.Utils; |
|
41 |
import jdk.internal.net.http.frame.SettingsFrame; |
|
42 |
import static jdk.internal.net.http.frame.SettingsFrame.INITIAL_WINDOW_SIZE; |
|
43 |
import static jdk.internal.net.http.frame.SettingsFrame.ENABLE_PUSH; |
|
44 |
import static jdk.internal.net.http.frame.SettingsFrame.HEADER_TABLE_SIZE; |
|
45 |
import static jdk.internal.net.http.frame.SettingsFrame.MAX_CONCURRENT_STREAMS; |
|
46 |
import static jdk.internal.net.http.frame.SettingsFrame.MAX_FRAME_SIZE; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
47 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
49 |
* Http2 specific aspects of HttpClientImpl |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
51 |
class Http2ClientImpl { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
|
49765 | 53 |
final static Logger debug = |
54 |
Utils.getDebugLogger("Http2ClientImpl"::toString, Utils.DEBUG); |
|
48083 | 55 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
56 |
private final HttpClientImpl client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
57 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
Http2ClientImpl(HttpClientImpl client) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
59 |
this.client = client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
62 |
/* Map key is "scheme:host:port" */ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
63 |
private final Map<String,Http2Connection> connections = new ConcurrentHashMap<>(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
64 |
|
48376
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
65 |
private final Set<String> failures = Collections.synchronizedSet(new HashSet<>()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
67 |
/** |
48376
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
68 |
* When HTTP/2 requested only. The following describes the aggregate behavior including the |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
69 |
* calling code. In all cases, the HTTP2 connection cache |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
70 |
* is checked first for a suitable connection and that is returned if available. |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
71 |
* If not, a new connection is opened, except in https case when a previous negotiate failed. |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
72 |
* In that case, we want to continue using http/1.1. When a connection is to be opened and |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
73 |
* if multiple requests are sent in parallel then each will open a new connection. |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
74 |
* |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
75 |
* If negotiation/upgrade succeeds then |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
76 |
* one connection will be put in the cache and the others will be closed |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
77 |
* after the initial request completes (not strictly necessary for h2, only for h2c) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
* |
48376
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
79 |
* If negotiate/upgrade fails, then any opened connections remain open (as http/1.1) |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
80 |
* and will be used and cached in the http/1 cache. Note, this method handles the |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
81 |
* https failure case only (by completing the CF with an ALPN exception, handled externally) |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
82 |
* The h2c upgrade is handled externally also. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
83 |
* |
48376
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
84 |
* Specific CF behavior of this method. |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
85 |
* 1. completes with ALPN exception: h2 negotiate failed for first time. failure recorded. |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
86 |
* 2. completes with other exception: failure not recorded. Caller must handle |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
87 |
* 3. completes normally with null: no connection in cache for h2c or h2 failed previously |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
88 |
* 4. completes normally with connection: h2 or h2c connection in cache. Use it. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
89 |
*/ |
48083 | 90 |
CompletableFuture<Http2Connection> getConnectionFor(HttpRequestImpl req) { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
URI uri = req.uri(); |
48083 | 92 |
InetSocketAddress proxy = req.proxy(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
93 |
String key = Http2Connection.keyFor(uri, proxy); |
48083 | 94 |
|
48376
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
95 |
synchronized (this) { |
48083 | 96 |
Http2Connection connection = connections.get(key); |
49765 | 97 |
if (connection != null) { |
98 |
if (connection.closed) { |
|
99 |
if (debug.on()) |
|
100 |
debug.log("removing found closed connection: %s", connection); |
|
101 |
connections.remove(key); |
|
102 |
} else { |
|
103 |
// fast path if connection already exists |
|
104 |
if (debug.on()) |
|
105 |
debug.log("found connection in the pool: %s", connection); |
|
106 |
return MinimalFuture.completedFuture(connection); |
|
107 |
} |
|
48083 | 108 |
} |
109 |
||
48376
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
110 |
if (!req.secure() || failures.contains(key)) { |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
111 |
// secure: negotiate failed before. Use http/1.1 |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
112 |
// !secure: no connection available in cache. Attempt upgrade |
49765 | 113 |
if (debug.on()) debug.log("not found in connection pool"); |
48083 | 114 |
return MinimalFuture.completedFuture(null); |
115 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
116 |
} |
48083 | 117 |
return Http2Connection |
118 |
.createAsync(req, this) |
|
119 |
.whenComplete((conn, t) -> { |
|
48376
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
120 |
synchronized (Http2ClientImpl.this) { |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
121 |
if (conn != null) { |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
122 |
offerConnection(conn); |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
123 |
} else { |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
124 |
Throwable cause = Utils.getCompletionCause(t); |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
125 |
if (cause instanceof Http2Connection.ALPNException) |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
126 |
failures.add(key); |
48083 | 127 |
} |
128 |
} |
|
129 |
}); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
130 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
131 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
132 |
/* |
48376
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
133 |
* Cache the given connection, if no connection to the same |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
134 |
* destination exists. If one exists, then we let the initial stream |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
135 |
* complete but allow it to close itself upon completion. |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
136 |
* This situation should not arise with https because the request |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
137 |
* has not been sent as part of the initial alpn negotiation |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
138 |
*/ |
48376
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
139 |
boolean offerConnection(Http2Connection c) { |
49765 | 140 |
if (debug.on()) debug.log("offering to the connection pool: %s", c); |
141 |
if (c.closed) { |
|
142 |
if (debug.on()) |
|
143 |
debug.log("skipping offered closed connection: %s", c); |
|
48376
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
144 |
return false; |
41ae5c69b09c
8192966: HttpClient should reuse TCP connection for h2c connections
michaelm
parents:
48263
diff
changeset
|
145 |
} |
49765 | 146 |
|
147 |
String key = c.key(); |
|
148 |
synchronized(this) { |
|
149 |
Http2Connection c1 = connections.putIfAbsent(key, c); |
|
150 |
if (c1 != null) { |
|
151 |
c.setSingleStream(true); |
|
152 |
if (debug.on()) |
|
153 |
debug.log("existing entry in connection pool for %s", key); |
|
154 |
return false; |
|
155 |
} |
|
156 |
if (debug.on()) |
|
157 |
debug.log("put in the connection pool: %s", c); |
|
158 |
return true; |
|
159 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
160 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
161 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
162 |
void deleteConnection(Http2Connection c) { |
49765 | 163 |
if (debug.on()) |
164 |
debug.log("removing from the connection pool: %s", c); |
|
165 |
synchronized (this) { |
|
166 |
connections.remove(c.key()); |
|
167 |
if (debug.on()) |
|
168 |
debug.log("removed from the connection pool: %s", c); |
|
169 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
170 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
171 |
|
48083 | 172 |
void stop() { |
49765 | 173 |
if (debug.on()) debug.log("stopping"); |
48083 | 174 |
connections.values().forEach(this::close); |
175 |
connections.clear(); |
|
176 |
} |
|
177 |
||
178 |
private void close(Http2Connection h2c) { |
|
179 |
try { h2c.close(); } catch (Throwable t) {} |
|
180 |
} |
|
181 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
182 |
HttpClientImpl client() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
return client; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
184 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
185 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
186 |
/** Returns the client settings as a base64 (url) encoded string */ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
187 |
String getSettingsString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
SettingsFrame sf = getClientSettings(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
189 |
byte[] settings = sf.toByteArray(); // without the header |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
190 |
Base64.Encoder encoder = Base64.getUrlEncoder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
191 |
.withoutPadding(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
return encoder.encodeToString(settings); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
193 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
195 |
private static final int K = 1024; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
196 |
|
48263
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
197 |
private static int getParameter(String property, int min, int max, int defaultValue) { |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
198 |
int value = Utils.getIntegerNetProperty(property, defaultValue); |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
199 |
// use default value if misconfigured |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
200 |
if (value < min || value > max) { |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
201 |
Log.logError("Property value for {0}={1} not in [{2}..{3}]: " + |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
202 |
"using default={4}", property, value, min, max, defaultValue); |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
203 |
value = defaultValue; |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
204 |
} |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
205 |
return value; |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
206 |
} |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
207 |
|
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
208 |
// used for the connection window, to have a connection window size |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
209 |
// bigger than the initial stream window size. |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
210 |
int getConnectionWindowSize(SettingsFrame clientSettings) { |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
211 |
// Maximum size is 2^31-1. Don't allow window size to be less |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
212 |
// than the stream window size. HTTP/2 specify a default of 64 * K -1, |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
213 |
// but we use 2^26 by default for better performance. |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
214 |
int streamWindow = clientSettings.getParameter(INITIAL_WINDOW_SIZE); |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
215 |
|
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
216 |
// The default is the max between the stream window size |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
217 |
// and the connection window size. |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
218 |
int defaultValue = Math.min(Integer.MAX_VALUE, |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
219 |
Math.max(streamWindow, K*K*32)); |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
220 |
|
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
221 |
return getParameter( |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
222 |
"jdk.httpclient.connectionWindowSize", |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
223 |
streamWindow, Integer.MAX_VALUE, defaultValue); |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
224 |
} |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
225 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
SettingsFrame getClientSettings() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
SettingsFrame frame = new SettingsFrame(); |
48263
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
228 |
// default defined for HTTP/2 is 4 K, we use 16 K. |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
229 |
frame.setParameter(HEADER_TABLE_SIZE, getParameter( |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
230 |
"jdk.httpclient.hpack.maxheadertablesize", |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
231 |
0, Integer.MAX_VALUE, 16 * K)); |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
232 |
// O: does not accept push streams. 1: accepts push streams. |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
233 |
frame.setParameter(ENABLE_PUSH, getParameter( |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
234 |
"jdk.httpclient.enablepush", |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
235 |
0, 1, 1)); |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
236 |
// HTTP/2 recommends to set the number of concurrent streams |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
237 |
// no lower than 100. We use 100. 0 means no stream would be |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
238 |
// accepted. That would render the client to be non functional, |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
239 |
// so we won't let 0 be configured for our Http2ClientImpl. |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
240 |
frame.setParameter(MAX_CONCURRENT_STREAMS, getParameter( |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
241 |
"jdk.httpclient.maxstreams", |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
242 |
1, Integer.MAX_VALUE, 100)); |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
243 |
// Maximum size is 2^31-1. Don't allow window size to be less |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
244 |
// than the minimum frame size as this is likely to be a |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
245 |
// configuration error. HTTP/2 specify a default of 64 * K -1, |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
246 |
// but we use 16 M for better performance. |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
247 |
frame.setParameter(INITIAL_WINDOW_SIZE, getParameter( |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
248 |
"jdk.httpclient.windowsize", |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
249 |
16 * K, Integer.MAX_VALUE, 16*K*K)); |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
250 |
// HTTP/2 specify a minimum size of 16 K, a maximum size of 2^24-1, |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
251 |
// and a default of 16 K. We use 16 K as default. |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
252 |
frame.setParameter(MAX_FRAME_SIZE, getParameter( |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
253 |
"jdk.httpclient.maxframesize", |
a559b7cd1dea
8193370: Provide more user friendly defaults for HTTP/2 client settings
dfuchs
parents:
48083
diff
changeset
|
254 |
16 * K, 16 * K * K -1, 16 * K)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
255 |
return frame; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
256 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
257 |
} |