36131
|
1 |
/*
|
37720
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
36131
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
*/
|
|
24 |
package java.net.http;
|
|
25 |
|
37720
|
26 |
import java.io.IOException;
|
|
27 |
import java.net.InetSocketAddress;
|
|
28 |
import java.net.URI;
|
|
29 |
import static java.net.http.SettingsFrame.INITIAL_WINDOW_SIZE;
|
|
30 |
import static java.net.http.SettingsFrame.ENABLE_PUSH;
|
|
31 |
import static java.net.http.SettingsFrame.HEADER_TABLE_SIZE;
|
|
32 |
import static java.net.http.SettingsFrame.MAX_CONCURRENT_STREAMS;
|
|
33 |
import static java.net.http.SettingsFrame.MAX_FRAME_SIZE;
|
|
34 |
import java.util.Base64;
|
|
35 |
import java.util.Collections;
|
|
36 |
import java.util.HashMap;
|
|
37 |
import java.util.HashSet;
|
|
38 |
import java.util.Map;
|
|
39 |
import java.util.Set;
|
|
40 |
|
|
41 |
/**
|
|
42 |
* Http2 specific aspects of HttpClientImpl
|
|
43 |
*/
|
36131
|
44 |
class Http2ClientImpl {
|
37720
|
45 |
|
|
46 |
final private HttpClientImpl client;
|
|
47 |
|
|
48 |
Http2ClientImpl(HttpClientImpl client) {
|
|
49 |
this.client = client;
|
|
50 |
}
|
|
51 |
|
|
52 |
/* Map key is "scheme:host:port" */
|
|
53 |
final private Map<String,Http2Connection> connections =
|
|
54 |
Collections.synchronizedMap(new HashMap<>());
|
|
55 |
|
|
56 |
final private Set<String> opening = Collections.synchronizedSet(new HashSet<>());
|
|
57 |
|
|
58 |
synchronized boolean haveConnectionFor(URI uri, InetSocketAddress proxy) {
|
|
59 |
return connections.containsKey(Http2Connection.keyFor(uri,proxy));
|
|
60 |
}
|
|
61 |
|
|
62 |
/**
|
|
63 |
* If a https request then blocks and waits until a connection is opened.
|
|
64 |
* Returns null if the request is 'http' as a different (upgrade)
|
|
65 |
* mechanism is used.
|
|
66 |
*
|
|
67 |
* Only one connection per destination is created. Blocks when opening
|
|
68 |
* connection, or when waiting for connection to be opened.
|
|
69 |
* First thread opens the connection and notifies the others when done.
|
|
70 |
*
|
|
71 |
* If the request is secure (https) then we open the connection here.
|
|
72 |
* If not, then the more complicated upgrade from 1.1 to 2 happens (not here)
|
|
73 |
* In latter case, when the Http2Connection is connected, putConnection() must
|
|
74 |
* be called to store it.
|
|
75 |
*/
|
|
76 |
Http2Connection getConnectionFor(HttpRequestImpl req)
|
|
77 |
throws IOException, InterruptedException {
|
|
78 |
URI uri = req.uri();
|
|
79 |
InetSocketAddress proxy = req.proxy();
|
|
80 |
String key = Http2Connection.keyFor(uri, proxy);
|
|
81 |
Http2Connection connection;
|
|
82 |
synchronized (opening) {
|
|
83 |
while ((connection = connections.get(key)) == null) {
|
|
84 |
if (!req.secure()) {
|
|
85 |
return null;
|
|
86 |
}
|
|
87 |
if (!opening.contains(key)) {
|
|
88 |
opening.add(key);
|
|
89 |
break;
|
|
90 |
} else {
|
|
91 |
opening.wait();
|
|
92 |
}
|
|
93 |
}
|
|
94 |
}
|
|
95 |
if (connection != null) {
|
|
96 |
return connection;
|
|
97 |
}
|
|
98 |
// we are opening the connection here blocking until it is done.
|
|
99 |
connection = new Http2Connection(req);
|
|
100 |
synchronized (opening) {
|
|
101 |
connections.put(key, connection);
|
|
102 |
opening.remove(key);
|
|
103 |
opening.notifyAll();
|
|
104 |
}
|
|
105 |
return connection;
|
|
106 |
}
|
|
107 |
|
|
108 |
|
|
109 |
/*
|
|
110 |
* TODO: If there isn't a connection to the same destination, then
|
|
111 |
* store it. If there is already a connection, then close it
|
|
112 |
*/
|
|
113 |
synchronized void putConnection(Http2Connection c) {
|
|
114 |
String key = c.key();
|
|
115 |
connections.put(key, c);
|
|
116 |
}
|
|
117 |
|
|
118 |
synchronized void deleteConnection(Http2Connection c) {
|
|
119 |
String key = c.key();
|
|
120 |
connections.remove(key);
|
|
121 |
}
|
|
122 |
|
|
123 |
HttpClientImpl client() {
|
|
124 |
return client;
|
|
125 |
}
|
|
126 |
|
|
127 |
/** Returns the client settings as a base64 (url) encoded string */
|
|
128 |
String getSettingsString() {
|
|
129 |
SettingsFrame sf = getClientSettings();
|
|
130 |
ByteBufferGenerator bg = new ByteBufferGenerator(client);
|
|
131 |
sf.writeOutgoing(bg);
|
|
132 |
byte[] settings = bg.asByteArray(9); // without the header
|
|
133 |
Base64.Encoder encoder = Base64.getUrlEncoder()
|
|
134 |
.withoutPadding();
|
|
135 |
return encoder.encodeToString(settings);
|
|
136 |
}
|
|
137 |
|
|
138 |
private static final int K = 1024;
|
|
139 |
|
|
140 |
SettingsFrame getClientSettings() {
|
|
141 |
SettingsFrame frame = new SettingsFrame();
|
|
142 |
frame.setParameter(HEADER_TABLE_SIZE, Utils.getIntegerNetProperty(
|
|
143 |
"java.net.httpclient.hpack.maxheadertablesize", 16 * K));
|
|
144 |
frame.setParameter(ENABLE_PUSH, Utils.getIntegerNetProperty(
|
|
145 |
"java.net.httpclient.enablepush", 1));
|
|
146 |
frame.setParameter(MAX_CONCURRENT_STREAMS, Utils.getIntegerNetProperty(
|
|
147 |
"java.net.httpclient.maxstreams", 16));
|
|
148 |
frame.setParameter(INITIAL_WINDOW_SIZE, Utils.getIntegerNetProperty(
|
|
149 |
"java.net.httpclient.windowsize", 32 * K));
|
|
150 |
frame.setParameter(MAX_FRAME_SIZE, Utils.getIntegerNetProperty(
|
|
151 |
"java.net.httpclient.maxframesize", 16 * K));
|
|
152 |
frame.computeLength();
|
|
153 |
return frame;
|
36131
|
154 |
}
|
|
155 |
}
|