|
1 /* |
|
2 * Copyright (c) 2015, 2018, 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 |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package jdk.internal.net.http; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.util.concurrent.CompletableFuture; |
|
30 import java.util.concurrent.Executor; |
|
31 import java.util.function.Function; |
|
32 import java.net.http.HttpResponse; |
|
33 import jdk.internal.net.http.common.Logger; |
|
34 import jdk.internal.net.http.common.MinimalFuture; |
|
35 import static java.net.http.HttpClient.Version.HTTP_1_1; |
|
36 import jdk.internal.net.http.common.Utils; |
|
37 |
|
38 /** |
|
39 * Splits request so that headers and body can be sent separately with optional |
|
40 * (multiple) responses in between (e.g. 100 Continue). Also request and |
|
41 * response always sent/received in different calls. |
|
42 * |
|
43 * Synchronous and asynchronous versions of each method are provided. |
|
44 * |
|
45 * Separate implementations of this class exist for HTTP/1.1 and HTTP/2 |
|
46 * Http1Exchange (HTTP/1.1) |
|
47 * Stream (HTTP/2) |
|
48 * |
|
49 * These implementation classes are where work is allocated to threads. |
|
50 */ |
|
51 abstract class ExchangeImpl<T> { |
|
52 |
|
53 private static final Logger debug = |
|
54 Utils.getDebugLogger("ExchangeImpl"::toString, Utils.DEBUG); |
|
55 |
|
56 final Exchange<T> exchange; |
|
57 |
|
58 ExchangeImpl(Exchange<T> e) { |
|
59 // e == null means a http/2 pushed stream |
|
60 this.exchange = e; |
|
61 } |
|
62 |
|
63 final Exchange<T> getExchange() { |
|
64 return exchange; |
|
65 } |
|
66 |
|
67 |
|
68 /** |
|
69 * Returns the {@link HttpConnection} instance to which this exchange is |
|
70 * assigned. |
|
71 */ |
|
72 abstract HttpConnection connection(); |
|
73 |
|
74 /** |
|
75 * Initiates a new exchange and assigns it to a connection if one exists |
|
76 * already. connection usually null. |
|
77 */ |
|
78 static <U> CompletableFuture<? extends ExchangeImpl<U>> |
|
79 get(Exchange<U> exchange, HttpConnection connection) |
|
80 { |
|
81 if (exchange.version() == HTTP_1_1) { |
|
82 if (debug.on()) |
|
83 debug.log("get: HTTP/1.1: new Http1Exchange"); |
|
84 return createHttp1Exchange(exchange, connection); |
|
85 } else { |
|
86 Http2ClientImpl c2 = exchange.client().client2(); // #### improve |
|
87 HttpRequestImpl request = exchange.request(); |
|
88 CompletableFuture<Http2Connection> c2f = c2.getConnectionFor(request); |
|
89 if (debug.on()) |
|
90 debug.log("get: Trying to get HTTP/2 connection"); |
|
91 return c2f.handle((h2c, t) -> createExchangeImpl(h2c, t, exchange, connection)) |
|
92 .thenCompose(Function.identity()); |
|
93 } |
|
94 } |
|
95 |
|
96 private static <U> CompletableFuture<? extends ExchangeImpl<U>> |
|
97 createExchangeImpl(Http2Connection c, |
|
98 Throwable t, |
|
99 Exchange<U> exchange, |
|
100 HttpConnection connection) |
|
101 { |
|
102 if (debug.on()) |
|
103 debug.log("handling HTTP/2 connection creation result"); |
|
104 boolean secure = exchange.request().secure(); |
|
105 if (t != null) { |
|
106 if (debug.on()) |
|
107 debug.log("handling HTTP/2 connection creation failed: %s", |
|
108 (Object)t); |
|
109 t = Utils.getCompletionCause(t); |
|
110 if (t instanceof Http2Connection.ALPNException) { |
|
111 Http2Connection.ALPNException ee = (Http2Connection.ALPNException)t; |
|
112 AbstractAsyncSSLConnection as = ee.getConnection(); |
|
113 if (debug.on()) |
|
114 debug.log("downgrading to HTTP/1.1 with: %s", as); |
|
115 CompletableFuture<? extends ExchangeImpl<U>> ex = |
|
116 createHttp1Exchange(exchange, as); |
|
117 return ex; |
|
118 } else { |
|
119 if (debug.on()) |
|
120 debug.log("HTTP/2 connection creation failed " |
|
121 + "with unexpected exception: %s", (Object)t); |
|
122 return MinimalFuture.failedFuture(t); |
|
123 } |
|
124 } |
|
125 if (secure && c== null) { |
|
126 if (debug.on()) |
|
127 debug.log("downgrading to HTTP/1.1 "); |
|
128 CompletableFuture<? extends ExchangeImpl<U>> ex = |
|
129 createHttp1Exchange(exchange, null); |
|
130 return ex; |
|
131 } |
|
132 if (c == null) { |
|
133 // no existing connection. Send request with HTTP 1 and then |
|
134 // upgrade if successful |
|
135 if (debug.on()) |
|
136 debug.log("new Http1Exchange, try to upgrade"); |
|
137 return createHttp1Exchange(exchange, connection) |
|
138 .thenApply((e) -> { |
|
139 exchange.h2Upgrade(); |
|
140 return e; |
|
141 }); |
|
142 } else { |
|
143 if (debug.on()) debug.log("creating HTTP/2 streams"); |
|
144 Stream<U> s = c.createStream(exchange); |
|
145 CompletableFuture<? extends ExchangeImpl<U>> ex = MinimalFuture.completedFuture(s); |
|
146 return ex; |
|
147 } |
|
148 } |
|
149 |
|
150 private static <T> CompletableFuture<Http1Exchange<T>> |
|
151 createHttp1Exchange(Exchange<T> ex, HttpConnection as) |
|
152 { |
|
153 try { |
|
154 return MinimalFuture.completedFuture(new Http1Exchange<>(ex, as)); |
|
155 } catch (Throwable e) { |
|
156 return MinimalFuture.failedFuture(e); |
|
157 } |
|
158 } |
|
159 |
|
160 /* The following methods have separate HTTP/1.1 and HTTP/2 implementations */ |
|
161 |
|
162 abstract CompletableFuture<ExchangeImpl<T>> sendHeadersAsync(); |
|
163 |
|
164 /** Sends a request body, after request headers have been sent. */ |
|
165 abstract CompletableFuture<ExchangeImpl<T>> sendBodyAsync(); |
|
166 |
|
167 abstract CompletableFuture<T> readBodyAsync(HttpResponse.BodyHandler<T> handler, |
|
168 boolean returnConnectionToPool, |
|
169 Executor executor); |
|
170 |
|
171 /** |
|
172 * Ignore/consume the body. |
|
173 */ |
|
174 abstract CompletableFuture<Void> ignoreBody(); |
|
175 |
|
176 /** Gets the response headers. Completes before body is read. */ |
|
177 abstract CompletableFuture<Response> getResponseAsync(Executor executor); |
|
178 |
|
179 |
|
180 /** Cancels a request. Not currently exposed through API. */ |
|
181 abstract void cancel(); |
|
182 |
|
183 /** |
|
184 * Cancels a request with a cause. Not currently exposed through API. |
|
185 */ |
|
186 abstract void cancel(IOException cause); |
|
187 |
|
188 /** |
|
189 * Called when the exchange is released, so that cleanup actions may be |
|
190 * performed - such as deregistering callbacks. |
|
191 * Typically released is called during upgrade, when an HTTP/2 stream |
|
192 * takes over from an Http1Exchange, or when a new exchange is created |
|
193 * during a multi exchange before the final response body was received. |
|
194 */ |
|
195 abstract void released(); |
|
196 |
|
197 /** |
|
198 * Called when the exchange is completed, so that cleanup actions may be |
|
199 * performed - such as deregistering callbacks. |
|
200 * Typically, completed is called at the end of the exchange, when the |
|
201 * final response body has been received (or an error has caused the |
|
202 * completion of the exchange). |
|
203 */ |
|
204 abstract void completed(); |
|
205 |
|
206 /** |
|
207 * Returns true if this exchange was canceled. |
|
208 * @return true if this exchange was canceled. |
|
209 */ |
|
210 abstract boolean isCanceled(); |
|
211 |
|
212 /** |
|
213 * Returns the cause for which this exchange was canceled, if available. |
|
214 * @return the cause for which this exchange was canceled, if available. |
|
215 */ |
|
216 abstract Throwable getCancelCause(); |
|
217 } |