36131
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
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
|
39730
|
23 |
* questions.
|
36131
|
24 |
*/
|
39730
|
25 |
|
36131
|
26 |
package java.net.http;
|
|
27 |
|
|
28 |
import java.io.IOException;
|
|
29 |
import java.net.InetSocketAddress;
|
|
30 |
import java.net.URI;
|
|
31 |
import java.util.concurrent.CompletableFuture;
|
|
32 |
import java.util.Collections;
|
|
33 |
import java.util.LinkedList;
|
|
34 |
import java.util.List;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* Encapsulates one HTTP/1.1 request/responseAsync exchange.
|
|
38 |
*/
|
|
39 |
class Http1Exchange extends ExchangeImpl {
|
|
40 |
|
|
41 |
final HttpRequestImpl request; // main request
|
|
42 |
final List<CompletableFuture<?>> operations; // used for cancel
|
|
43 |
final Http1Request requestAction;
|
|
44 |
volatile Http1Response response;
|
|
45 |
final HttpConnection connection;
|
|
46 |
final HttpClientImpl client;
|
|
47 |
final ExecutorWrapper executor;
|
|
48 |
|
|
49 |
@Override
|
|
50 |
public String toString() {
|
|
51 |
return request.toString();
|
|
52 |
}
|
|
53 |
|
|
54 |
HttpRequestImpl request() {
|
|
55 |
return request;
|
|
56 |
}
|
|
57 |
|
|
58 |
Http1Exchange(Exchange exchange, HttpConnection connection)
|
|
59 |
throws IOException
|
|
60 |
{
|
|
61 |
super(exchange);
|
|
62 |
this.request = exchange.request();
|
|
63 |
this.client = request.client();
|
|
64 |
this.executor = client.executorWrapper();
|
|
65 |
this.operations = Collections.synchronizedList(new LinkedList<>());
|
|
66 |
if (connection != null) {
|
|
67 |
this.connection = connection;
|
|
68 |
} else {
|
37720
|
69 |
InetSocketAddress addr = Utils.getAddress(request);
|
36131
|
70 |
this.connection = HttpConnection.getConnection(addr, request);
|
|
71 |
}
|
|
72 |
this.requestAction = new Http1Request(request, this.connection);
|
|
73 |
}
|
|
74 |
|
|
75 |
|
|
76 |
HttpConnection connection() {
|
|
77 |
return connection;
|
|
78 |
}
|
|
79 |
|
|
80 |
@Override
|
|
81 |
<T> T responseBody(HttpResponse.BodyProcessor<T> processor)
|
|
82 |
throws IOException
|
|
83 |
{
|
|
84 |
return responseBody(processor, true);
|
|
85 |
}
|
|
86 |
|
|
87 |
<T> T responseBody(HttpResponse.BodyProcessor<T> processor,
|
|
88 |
boolean return2Cache)
|
|
89 |
throws IOException
|
|
90 |
{
|
|
91 |
try {
|
|
92 |
T body = response.readBody(processor, return2Cache);
|
|
93 |
return body;
|
|
94 |
} catch (Throwable t) {
|
|
95 |
connection.close();
|
|
96 |
throw t;
|
|
97 |
}
|
|
98 |
}
|
|
99 |
|
|
100 |
@Override
|
|
101 |
<T> CompletableFuture<T> responseBodyAsync(HttpResponse.BodyProcessor<T> processor) {
|
|
102 |
CompletableFuture<T> cf = new CompletableFuture<>();
|
|
103 |
request.client()
|
|
104 |
.executorWrapper()
|
|
105 |
.execute(() -> {
|
|
106 |
try {
|
|
107 |
T body = responseBody(processor);
|
|
108 |
cf.complete(body);
|
|
109 |
} catch (Throwable e) {
|
|
110 |
cf.completeExceptionally(e);
|
|
111 |
}
|
|
112 |
},
|
|
113 |
() -> response.response.getAccessControlContext()); // TODO: fix
|
|
114 |
return cf;
|
|
115 |
}
|
|
116 |
|
|
117 |
@Override
|
|
118 |
void sendHeadersOnly() throws IOException, InterruptedException {
|
|
119 |
try {
|
|
120 |
if (!connection.connected()) {
|
|
121 |
connection.connect();
|
|
122 |
}
|
|
123 |
requestAction.sendHeadersOnly();
|
|
124 |
} catch (Throwable e) {
|
|
125 |
connection.close();
|
|
126 |
throw e;
|
|
127 |
}
|
|
128 |
}
|
|
129 |
|
|
130 |
@Override
|
|
131 |
void sendBody() throws IOException {
|
|
132 |
try {
|
|
133 |
requestAction.continueRequest();
|
|
134 |
} catch (Throwable e) {
|
|
135 |
connection.close();
|
|
136 |
throw e;
|
|
137 |
}
|
|
138 |
}
|
|
139 |
|
|
140 |
@Override
|
|
141 |
HttpResponseImpl getResponse() throws IOException {
|
|
142 |
try {
|
|
143 |
response = new Http1Response(connection, this);
|
|
144 |
response.readHeaders();
|
|
145 |
return response.response();
|
|
146 |
} catch (Throwable t) {
|
|
147 |
connection.close();
|
|
148 |
throw t;
|
|
149 |
}
|
|
150 |
}
|
|
151 |
|
|
152 |
@Override
|
|
153 |
void sendRequest() throws IOException, InterruptedException {
|
|
154 |
try {
|
|
155 |
if (!connection.connected()) {
|
|
156 |
connection.connect();
|
|
157 |
}
|
|
158 |
requestAction.sendRequest();
|
|
159 |
} catch (Throwable t) {
|
|
160 |
connection.close();
|
|
161 |
throw t;
|
|
162 |
}
|
|
163 |
}
|
|
164 |
|
|
165 |
private void closeConnection() {
|
|
166 |
connection.close();
|
|
167 |
}
|
|
168 |
|
|
169 |
@Override
|
|
170 |
CompletableFuture<Void> sendHeadersAsync() {
|
|
171 |
if (!connection.connected()) {
|
|
172 |
CompletableFuture<Void> op = connection.connectAsync()
|
|
173 |
.thenCompose(this::sendHdrsAsyncImpl)
|
|
174 |
.whenComplete((Void b, Throwable t) -> {
|
|
175 |
if (t != null)
|
|
176 |
closeConnection();
|
|
177 |
});
|
|
178 |
operations.add(op);
|
|
179 |
return op;
|
|
180 |
} else {
|
|
181 |
return sendHdrsAsyncImpl(null);
|
|
182 |
}
|
|
183 |
}
|
|
184 |
|
|
185 |
private CompletableFuture<Void> sendHdrsAsyncImpl(Void v) {
|
|
186 |
CompletableFuture<Void> cf = new CompletableFuture<>();
|
|
187 |
executor.execute(() -> {
|
|
188 |
try {
|
|
189 |
requestAction.sendHeadersOnly();
|
|
190 |
cf.complete(null);
|
|
191 |
} catch (Throwable e) {
|
|
192 |
cf.completeExceptionally(e);
|
|
193 |
connection.close();
|
|
194 |
}
|
|
195 |
},
|
37720
|
196 |
request::getAccessControlContext);
|
36131
|
197 |
operations.add(cf);
|
|
198 |
return cf;
|
|
199 |
}
|
|
200 |
|
|
201 |
/**
|
|
202 |
* Cancel checks to see if request and responseAsync finished already.
|
|
203 |
* If not it closes the connection and completes all pending operations
|
|
204 |
*/
|
|
205 |
@Override
|
|
206 |
synchronized void cancel() {
|
|
207 |
if (requestAction != null && requestAction.finished()
|
|
208 |
&& response != null && response.finished()) {
|
|
209 |
return;
|
|
210 |
}
|
|
211 |
connection.close();
|
|
212 |
IOException e = new IOException("Request cancelled");
|
|
213 |
int count = 0;
|
|
214 |
for (CompletableFuture<?> cf : operations) {
|
|
215 |
cf.completeExceptionally(e);
|
|
216 |
count++;
|
|
217 |
}
|
|
218 |
Log.logError("Http1Exchange.cancel: count=" + count);
|
|
219 |
}
|
|
220 |
|
|
221 |
CompletableFuture<HttpResponseImpl> getResponseAsyncImpl(Void v) {
|
|
222 |
CompletableFuture<HttpResponseImpl> cf = new CompletableFuture<>();
|
|
223 |
try {
|
|
224 |
response = new Http1Response(connection, Http1Exchange.this);
|
|
225 |
response.readHeaders();
|
|
226 |
cf.complete(response.response());
|
|
227 |
} catch (IOException e) {
|
|
228 |
cf.completeExceptionally(e);
|
|
229 |
}
|
|
230 |
return cf;
|
|
231 |
}
|
|
232 |
|
|
233 |
@Override
|
|
234 |
CompletableFuture<HttpResponseImpl> getResponseAsync(Void v) {
|
|
235 |
CompletableFuture<HttpResponseImpl> cf =
|
|
236 |
connection.whenReceivingResponse()
|
|
237 |
.thenCompose(this::getResponseAsyncImpl);
|
|
238 |
|
|
239 |
operations.add(cf);
|
|
240 |
return cf;
|
|
241 |
}
|
|
242 |
|
|
243 |
@Override
|
|
244 |
CompletableFuture<Void> sendBodyAsync() {
|
|
245 |
final CompletableFuture<Void> cf = new CompletableFuture<>();
|
|
246 |
executor.execute(() -> {
|
|
247 |
try {
|
|
248 |
requestAction.continueRequest();
|
|
249 |
cf.complete(null);
|
|
250 |
} catch (Throwable e) {
|
|
251 |
cf.completeExceptionally(e);
|
|
252 |
connection.close();
|
|
253 |
}
|
37720
|
254 |
}, request::getAccessControlContext);
|
36131
|
255 |
operations.add(cf);
|
|
256 |
return cf;
|
|
257 |
}
|
|
258 |
|
|
259 |
@Override
|
|
260 |
CompletableFuture<Void> sendRequestAsync() {
|
|
261 |
CompletableFuture<Void> op;
|
|
262 |
if (!connection.connected()) {
|
|
263 |
op = connection.connectAsync()
|
|
264 |
.thenCompose(this::sendRequestAsyncImpl)
|
|
265 |
.whenComplete((Void v, Throwable t) -> {
|
|
266 |
if (t != null) {
|
|
267 |
closeConnection();
|
|
268 |
}
|
|
269 |
});
|
|
270 |
} else {
|
|
271 |
op = sendRequestAsyncImpl(null);
|
|
272 |
}
|
|
273 |
operations.add(op);
|
|
274 |
return op;
|
|
275 |
}
|
|
276 |
|
|
277 |
CompletableFuture<Void> sendRequestAsyncImpl(Void v) {
|
|
278 |
CompletableFuture<Void> cf = new CompletableFuture<>();
|
|
279 |
executor.execute(() -> {
|
|
280 |
try {
|
|
281 |
requestAction.sendRequest();
|
|
282 |
cf.complete(null);
|
|
283 |
} catch (Throwable e) {
|
|
284 |
cf.completeExceptionally(e);
|
|
285 |
connection.close();
|
|
286 |
}
|
37720
|
287 |
}, request::getAccessControlContext);
|
36131
|
288 |
operations.add(cf);
|
|
289 |
return cf;
|
|
290 |
}
|
|
291 |
}
|