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
|
|
23 |
*/
|
|
24 |
package java.net.http;
|
|
25 |
|
|
26 |
import java.io.IOException;
|
|
27 |
import java.net.InetSocketAddress;
|
|
28 |
import java.net.ProxySelector;
|
|
29 |
import java.net.URI;
|
|
30 |
import java.net.http.HttpClient.Version;
|
|
31 |
import java.net.http.HttpResponse.MultiProcessor;
|
|
32 |
import java.util.concurrent.CompletableFuture;
|
|
33 |
import java.security.AccessControlContext;
|
|
34 |
import java.security.AccessController;
|
|
35 |
import static java.net.http.HttpRedirectImpl.getRedirects;
|
|
36 |
import java.util.Locale;
|
|
37 |
|
|
38 |
class HttpRequestImpl extends HttpRequest {
|
|
39 |
|
37720
|
40 |
private final ImmutableHeaders userHeaders;
|
36131
|
41 |
private final HttpHeadersImpl systemHeaders;
|
|
42 |
private final URI uri;
|
|
43 |
private InetSocketAddress authority; // only used when URI not specified
|
|
44 |
private final String method;
|
|
45 |
private final HttpClientImpl client;
|
|
46 |
private final HttpRedirectImpl followRedirects;
|
|
47 |
private final ProxySelector proxy;
|
|
48 |
final BodyProcessor requestProcessor;
|
|
49 |
final boolean secure;
|
|
50 |
final boolean expectContinue;
|
|
51 |
private final java.net.http.HttpClient.Version version;
|
|
52 |
private boolean isWebSocket;
|
|
53 |
final MultiExchange exchange;
|
|
54 |
private boolean receiving;
|
|
55 |
private AccessControlContext acc;
|
|
56 |
private final long timeval;
|
37720
|
57 |
private Stream.PushGroup<?> pushGroup;
|
36131
|
58 |
|
|
59 |
public HttpRequestImpl(HttpClientImpl client,
|
|
60 |
String method,
|
|
61 |
HttpRequestBuilderImpl builder) {
|
|
62 |
this.client = client;
|
|
63 |
this.method = method == null? "GET" : method;
|
|
64 |
this.userHeaders = builder.headers() == null ?
|
37720
|
65 |
new ImmutableHeaders() :
|
|
66 |
new ImmutableHeaders(builder.headers(), Utils.ALLOWED_HEADERS);
|
36131
|
67 |
this.followRedirects = getRedirects(builder.followRedirects() == null ?
|
|
68 |
client.followRedirects() : builder.followRedirects());
|
|
69 |
this.systemHeaders = new HttpHeadersImpl();
|
|
70 |
this.uri = builder.uri();
|
|
71 |
this.proxy = builder.proxy();
|
|
72 |
this.expectContinue = builder.expectContinue();
|
|
73 |
this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https");
|
|
74 |
this.version = builder.version();
|
|
75 |
if (builder.body() == null) {
|
|
76 |
this.requestProcessor = HttpRequest.noBody();
|
|
77 |
} else {
|
|
78 |
this.requestProcessor = builder.body();
|
|
79 |
}
|
|
80 |
this.exchange = new MultiExchange(this);
|
|
81 |
this.timeval = builder.timeval();
|
|
82 |
}
|
|
83 |
|
|
84 |
/** Creates a HttpRequestImpl using fields of an existing request impl. */
|
|
85 |
public HttpRequestImpl(URI uri,
|
|
86 |
HttpRequest request,
|
|
87 |
HttpClientImpl client,
|
|
88 |
String method,
|
|
89 |
HttpRequestImpl other) {
|
|
90 |
this.client = client;
|
|
91 |
this.method = method == null? "GET" : method;
|
37720
|
92 |
this.userHeaders = other.userHeaders;
|
36131
|
93 |
this.followRedirects = getRedirects(other.followRedirects() == null ?
|
|
94 |
client.followRedirects() : other.followRedirects());
|
|
95 |
this.systemHeaders = other.systemHeaders;
|
|
96 |
this.uri = uri;
|
|
97 |
this.expectContinue = other.expectContinue;
|
37720
|
98 |
this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https");
|
36131
|
99 |
this.requestProcessor = other.requestProcessor;
|
|
100 |
this.proxy = other.proxy;
|
|
101 |
this.version = other.version;
|
|
102 |
this.acc = other.acc;
|
|
103 |
this.exchange = new MultiExchange(this);
|
|
104 |
this.timeval = other.timeval;
|
|
105 |
}
|
|
106 |
|
|
107 |
/* used for creating CONNECT requests */
|
|
108 |
HttpRequestImpl(HttpClientImpl client,
|
|
109 |
String method,
|
|
110 |
InetSocketAddress authority) {
|
|
111 |
this.client = client;
|
|
112 |
this.method = method;
|
|
113 |
this.followRedirects = getRedirects(client.followRedirects());
|
|
114 |
this.systemHeaders = new HttpHeadersImpl();
|
37720
|
115 |
this.userHeaders = new ImmutableHeaders();
|
36131
|
116 |
this.uri = null;
|
|
117 |
this.proxy = null;
|
|
118 |
this.requestProcessor = HttpRequest.noBody();
|
|
119 |
this.version = java.net.http.HttpClient.Version.HTTP_1_1;
|
|
120 |
this.authority = authority;
|
|
121 |
this.secure = false;
|
|
122 |
this.expectContinue = false;
|
|
123 |
this.exchange = new MultiExchange(this);
|
|
124 |
this.timeval = 0; // block TODO: fix
|
|
125 |
}
|
|
126 |
|
|
127 |
@Override
|
|
128 |
public HttpClientImpl client() {
|
|
129 |
return client;
|
|
130 |
}
|
|
131 |
|
37720
|
132 |
/**
|
|
133 |
* Creates a HttpRequestImpl from the given set of Headers and the associated
|
|
134 |
* "parent" request. Fields not taken from the headers are taken from the
|
|
135 |
* parent.
|
|
136 |
*/
|
|
137 |
static HttpRequestImpl createPushRequest(HttpRequestImpl parent,
|
|
138 |
HttpHeadersImpl headers) throws IOException {
|
|
139 |
|
|
140 |
return new HttpRequestImpl(parent, headers);
|
|
141 |
}
|
|
142 |
|
|
143 |
// only used for push requests
|
|
144 |
private HttpRequestImpl(HttpRequestImpl parent, HttpHeadersImpl headers) throws IOException {
|
|
145 |
this.method = headers.firstValue(":method")
|
|
146 |
.orElseThrow(() -> new IOException("No method in Push Promise"));
|
|
147 |
String path = headers.firstValue(":path")
|
|
148 |
.orElseThrow(() -> new IOException("No path in Push Promise"));
|
|
149 |
String scheme = headers.firstValue(":scheme")
|
|
150 |
.orElseThrow(() -> new IOException("No scheme in Push Promise"));
|
|
151 |
String authority = headers.firstValue(":authority")
|
|
152 |
.orElseThrow(() -> new IOException("No authority in Push Promise"));
|
|
153 |
StringBuilder sb = new StringBuilder();
|
|
154 |
sb.append(scheme).append("://").append(authority).append(path);
|
|
155 |
this.uri = URI.create(sb.toString());
|
|
156 |
|
|
157 |
this.client = parent.client;
|
|
158 |
this.userHeaders = new ImmutableHeaders(headers, Utils.ALLOWED_HEADERS);
|
|
159 |
this.followRedirects = parent.followRedirects;
|
|
160 |
this.systemHeaders = parent.systemHeaders;
|
|
161 |
this.expectContinue = parent.expectContinue;
|
|
162 |
this.secure = parent.secure;
|
|
163 |
this.requestProcessor = parent.requestProcessor;
|
|
164 |
this.proxy = parent.proxy;
|
|
165 |
this.version = parent.version;
|
|
166 |
this.acc = parent.acc;
|
|
167 |
this.exchange = parent.exchange;
|
|
168 |
this.timeval = parent.timeval;
|
|
169 |
}
|
36131
|
170 |
|
|
171 |
@Override
|
|
172 |
public String toString() {
|
37720
|
173 |
return (uri == null ? "" : uri.toString()) + " " + method;
|
36131
|
174 |
}
|
|
175 |
|
|
176 |
@Override
|
|
177 |
public HttpHeaders headers() {
|
|
178 |
return userHeaders;
|
|
179 |
}
|
|
180 |
|
|
181 |
InetSocketAddress authority() { return authority; }
|
|
182 |
|
|
183 |
void setH2Upgrade() {
|
|
184 |
Http2ClientImpl h2client = client.client2();
|
|
185 |
systemHeaders.setHeader("Connection", "Upgrade, HTTP2-Settings");
|
|
186 |
systemHeaders.setHeader("Upgrade", "h2c");
|
|
187 |
systemHeaders.setHeader("HTTP2-Settings", h2client.getSettingsString());
|
|
188 |
}
|
|
189 |
|
|
190 |
private synchronized void receiving() {
|
|
191 |
if (receiving) {
|
|
192 |
throw new IllegalStateException("already receiving response");
|
|
193 |
}
|
|
194 |
receiving = true;
|
|
195 |
}
|
|
196 |
|
37720
|
197 |
synchronized Stream.PushGroup<?> pushGroup() {
|
|
198 |
return pushGroup;
|
|
199 |
}
|
|
200 |
|
36131
|
201 |
/*
|
|
202 |
* Response filters may result in a new HttpRequestImpl being created
|
|
203 |
* (but still associated with the same API HttpRequest) and the process
|
|
204 |
* is repeated.
|
|
205 |
*/
|
|
206 |
@Override
|
|
207 |
public HttpResponse response() throws IOException, InterruptedException {
|
|
208 |
receiving(); // TODO: update docs
|
|
209 |
if (System.getSecurityManager() != null) {
|
|
210 |
acc = AccessController.getContext();
|
|
211 |
}
|
|
212 |
return exchange.response();
|
|
213 |
}
|
|
214 |
|
|
215 |
@Override
|
|
216 |
public synchronized CompletableFuture<HttpResponse> responseAsync() {
|
|
217 |
receiving(); // TODO: update docs
|
|
218 |
if (System.getSecurityManager() != null) {
|
|
219 |
acc = AccessController.getContext();
|
|
220 |
}
|
|
221 |
return exchange.responseAsync(null)
|
|
222 |
.thenApply((r) -> (HttpResponse)r);
|
|
223 |
}
|
|
224 |
|
37720
|
225 |
|
|
226 |
@SuppressWarnings("unchecked")
|
|
227 |
@Override
|
|
228 |
public synchronized <U> CompletableFuture<U>
|
|
229 |
multiResponseAsync(MultiProcessor<U> rspproc) {
|
|
230 |
if (System.getSecurityManager() != null) {
|
|
231 |
acc = AccessController.getContext();
|
|
232 |
}
|
|
233 |
this.pushGroup = new Stream.PushGroup<>(rspproc, this);
|
|
234 |
CompletableFuture<HttpResponse> cf = pushGroup.mainResponse();
|
|
235 |
responseAsync()
|
|
236 |
.whenComplete((HttpResponse r, Throwable t) -> {
|
|
237 |
if (r != null)
|
|
238 |
cf.complete(r);
|
|
239 |
else
|
|
240 |
cf.completeExceptionally(t);
|
|
241 |
pushGroup.pushError(t);
|
|
242 |
});
|
|
243 |
return (CompletableFuture<U>)pushGroup.groupResult();
|
36131
|
244 |
}
|
|
245 |
|
|
246 |
@Override
|
|
247 |
public boolean expectContinue() { return expectContinue; }
|
|
248 |
|
|
249 |
public boolean requestHttp2() {
|
|
250 |
return version.equals(HttpClient.Version.HTTP_2);
|
|
251 |
//return client.getHttp2Allowed();
|
|
252 |
}
|
|
253 |
|
|
254 |
AccessControlContext getAccessControlContext() { return acc; }
|
|
255 |
|
|
256 |
InetSocketAddress proxy() {
|
|
257 |
ProxySelector ps = this.proxy;
|
|
258 |
if (ps == null) {
|
|
259 |
ps = client.proxy().orElse(null);
|
|
260 |
}
|
|
261 |
if (ps == null || method.equalsIgnoreCase("CONNECT")) {
|
|
262 |
return null;
|
|
263 |
}
|
|
264 |
return (InetSocketAddress)ps.select(uri).get(0).address();
|
|
265 |
}
|
|
266 |
|
|
267 |
boolean secure() { return secure; }
|
|
268 |
|
|
269 |
void isWebSocket(boolean is) {
|
|
270 |
isWebSocket = is;
|
|
271 |
}
|
|
272 |
|
|
273 |
boolean isWebSocket() {
|
|
274 |
return isWebSocket;
|
|
275 |
}
|
|
276 |
|
|
277 |
/** Returns the follow-redirects setting for this request. */
|
|
278 |
@Override
|
|
279 |
public java.net.http.HttpClient.Redirect followRedirects() {
|
|
280 |
return getRedirects(followRedirects);
|
|
281 |
}
|
|
282 |
|
|
283 |
HttpRedirectImpl followRedirectsImpl() { return followRedirects; }
|
|
284 |
|
|
285 |
/**
|
|
286 |
* Returns the request method for this request. If not set explicitly,
|
|
287 |
* the default method for any request is "GET".
|
|
288 |
*/
|
|
289 |
@Override
|
|
290 |
public String method() { return method; }
|
|
291 |
|
|
292 |
@Override
|
|
293 |
public URI uri() { return uri; }
|
|
294 |
|
37720
|
295 |
HttpHeaders getUserHeaders() { return userHeaders; }
|
36131
|
296 |
|
|
297 |
HttpHeadersImpl getSystemHeaders() { return systemHeaders; }
|
|
298 |
|
|
299 |
HttpClientImpl getClient() { return client; }
|
|
300 |
|
|
301 |
BodyProcessor requestProcessor() { return requestProcessor; }
|
|
302 |
|
|
303 |
@Override
|
|
304 |
public Version version() { return version; }
|
|
305 |
|
|
306 |
void addSystemHeader(String name, String value) {
|
|
307 |
systemHeaders.addHeader(name, value);
|
|
308 |
}
|
|
309 |
|
|
310 |
void setSystemHeader(String name, String value) {
|
|
311 |
systemHeaders.setHeader(name, value);
|
|
312 |
}
|
|
313 |
|
|
314 |
long timeval() { return timeval; }
|
|
315 |
}
|