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.incubator.http; |
|
27 |
|
28 import jdk.incubator.http.internal.common.HttpHeadersImpl; |
|
29 import jdk.incubator.http.internal.websocket.WebSocketRequest; |
|
30 |
|
31 import java.io.IOException; |
|
32 import java.net.InetSocketAddress; |
|
33 import java.net.Proxy; |
|
34 import java.net.ProxySelector; |
|
35 import java.net.URI; |
|
36 import java.security.AccessControlContext; |
|
37 import java.security.AccessController; |
|
38 import java.security.PrivilegedAction; |
|
39 import java.time.Duration; |
|
40 import java.util.List; |
|
41 import java.util.Locale; |
|
42 import java.util.Optional; |
|
43 |
|
44 import static jdk.incubator.http.internal.common.Utils.ALLOWED_HEADERS; |
|
45 |
|
46 class HttpRequestImpl extends HttpRequest implements WebSocketRequest { |
|
47 |
|
48 private final HttpHeaders userHeaders; |
|
49 private final HttpHeadersImpl systemHeaders; |
|
50 private final URI uri; |
|
51 private volatile Proxy proxy; // ensure safe publishing |
|
52 private final InetSocketAddress authority; // only used when URI not specified |
|
53 private final String method; |
|
54 final BodyPublisher requestPublisher; |
|
55 final boolean secure; |
|
56 final boolean expectContinue; |
|
57 private volatile boolean isWebSocket; |
|
58 private volatile AccessControlContext acc; |
|
59 private final Duration timeout; // may be null |
|
60 private final Optional<HttpClient.Version> version; |
|
61 |
|
62 private static String userAgent() { |
|
63 PrivilegedAction<String> pa = () -> System.getProperty("java.version"); |
|
64 String version = AccessController.doPrivileged(pa); |
|
65 return "Java-http-client/" + version; |
|
66 } |
|
67 |
|
68 /** The value of the User-Agent header for all requests sent by the client. */ |
|
69 public static final String USER_AGENT = userAgent(); |
|
70 |
|
71 /** |
|
72 * Creates an HttpRequestImpl from the given builder. |
|
73 */ |
|
74 public HttpRequestImpl(HttpRequestBuilderImpl builder) { |
|
75 String method = builder.method(); |
|
76 this.method = method == null ? "GET" : method; |
|
77 this.userHeaders = ImmutableHeaders.of(builder.headers().map(), ALLOWED_HEADERS); |
|
78 this.systemHeaders = new HttpHeadersImpl(); |
|
79 this.uri = builder.uri(); |
|
80 assert uri != null; |
|
81 this.proxy = null; |
|
82 this.expectContinue = builder.expectContinue(); |
|
83 this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https"); |
|
84 this.requestPublisher = builder.bodyPublisher(); // may be null |
|
85 this.timeout = builder.timeout(); |
|
86 this.version = builder.version(); |
|
87 this.authority = null; |
|
88 } |
|
89 |
|
90 /** |
|
91 * Creates an HttpRequestImpl from the given request. |
|
92 */ |
|
93 public HttpRequestImpl(HttpRequest request, ProxySelector ps, AccessControlContext acc) { |
|
94 String method = request.method(); |
|
95 this.method = method == null ? "GET" : method; |
|
96 this.userHeaders = request.headers(); |
|
97 if (request instanceof HttpRequestImpl) { |
|
98 this.systemHeaders = ((HttpRequestImpl) request).systemHeaders; |
|
99 this.isWebSocket = ((HttpRequestImpl) request).isWebSocket; |
|
100 } else { |
|
101 this.systemHeaders = new HttpHeadersImpl(); |
|
102 } |
|
103 this.systemHeaders.setHeader("User-Agent", USER_AGENT); |
|
104 this.uri = request.uri(); |
|
105 if (isWebSocket) { |
|
106 // WebSocket determines and sets the proxy itself |
|
107 this.proxy = ((HttpRequestImpl) request).proxy; |
|
108 } else { |
|
109 if (ps != null) |
|
110 this.proxy = retrieveProxy(ps, uri); |
|
111 else |
|
112 this.proxy = null; |
|
113 } |
|
114 this.expectContinue = request.expectContinue(); |
|
115 this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https"); |
|
116 this.requestPublisher = request.bodyPublisher().orElse(null); |
|
117 if (acc != null && requestPublisher instanceof RequestPublishers.FilePublisher) { |
|
118 // Restricts the file publisher with the senders ACC, if any |
|
119 ((RequestPublishers.FilePublisher)requestPublisher).setAccessControlContext(acc); |
|
120 } |
|
121 this.timeout = request.timeout().orElse(null); |
|
122 this.version = request.version(); |
|
123 this.authority = null; |
|
124 } |
|
125 |
|
126 /** Creates a HttpRequestImpl using fields of an existing request impl. */ |
|
127 public HttpRequestImpl(URI uri, |
|
128 String method, |
|
129 HttpRequestImpl other) { |
|
130 this.method = method == null? "GET" : method; |
|
131 this.userHeaders = other.userHeaders; |
|
132 this.isWebSocket = other.isWebSocket; |
|
133 this.systemHeaders = other.systemHeaders; |
|
134 this.uri = uri; |
|
135 this.proxy = other.proxy; |
|
136 this.expectContinue = other.expectContinue; |
|
137 this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https"); |
|
138 this.requestPublisher = other.requestPublisher; // may be null |
|
139 this.acc = other.acc; |
|
140 this.timeout = other.timeout; |
|
141 this.version = other.version(); |
|
142 this.authority = null; |
|
143 } |
|
144 |
|
145 /* used for creating CONNECT requests */ |
|
146 HttpRequestImpl(String method, InetSocketAddress authority, HttpHeaders headers) { |
|
147 // TODO: isWebSocket flag is not specified, but the assumption is that |
|
148 // such a request will never be made on a connection that will be returned |
|
149 // to the connection pool (we might need to revisit this constructor later) |
|
150 assert "CONNECT".equalsIgnoreCase(method); |
|
151 this.method = method; |
|
152 this.systemHeaders = new HttpHeadersImpl(); |
|
153 this.userHeaders = ImmutableHeaders.of(headers); |
|
154 this.uri = URI.create("socket://" + authority.getHostString() + ":" |
|
155 + Integer.toString(authority.getPort()) + "/"); |
|
156 this.proxy = null; |
|
157 this.requestPublisher = null; |
|
158 this.authority = authority; |
|
159 this.secure = false; |
|
160 this.expectContinue = false; |
|
161 this.timeout = null; |
|
162 // The CONNECT request sent for tunneling is only used in two cases: |
|
163 // 1. websocket, which only supports HTTP/1.1 |
|
164 // 2. SSL tunneling through a HTTP/1.1 proxy |
|
165 // In either case we do not want to upgrade the connection to the proxy. |
|
166 // What we want to possibly upgrade is the tunneled connection to the |
|
167 // target server (so not the CONNECT request itself) |
|
168 this.version = Optional.of(HttpClient.Version.HTTP_1_1); |
|
169 } |
|
170 |
|
171 final boolean isConnect() { |
|
172 return "CONNECT".equalsIgnoreCase(method); |
|
173 } |
|
174 |
|
175 /** |
|
176 * Creates a HttpRequestImpl from the given set of Headers and the associated |
|
177 * "parent" request. Fields not taken from the headers are taken from the |
|
178 * parent. |
|
179 */ |
|
180 static HttpRequestImpl createPushRequest(HttpRequestImpl parent, |
|
181 HttpHeadersImpl headers) |
|
182 throws IOException |
|
183 { |
|
184 return new HttpRequestImpl(parent, headers); |
|
185 } |
|
186 |
|
187 // only used for push requests |
|
188 private HttpRequestImpl(HttpRequestImpl parent, HttpHeadersImpl headers) |
|
189 throws IOException |
|
190 { |
|
191 this.method = headers.firstValue(":method") |
|
192 .orElseThrow(() -> new IOException("No method in Push Promise")); |
|
193 String path = headers.firstValue(":path") |
|
194 .orElseThrow(() -> new IOException("No path in Push Promise")); |
|
195 String scheme = headers.firstValue(":scheme") |
|
196 .orElseThrow(() -> new IOException("No scheme in Push Promise")); |
|
197 String authority = headers.firstValue(":authority") |
|
198 .orElseThrow(() -> new IOException("No authority in Push Promise")); |
|
199 StringBuilder sb = new StringBuilder(); |
|
200 sb.append(scheme).append("://").append(authority).append(path); |
|
201 this.uri = URI.create(sb.toString()); |
|
202 this.proxy = null; |
|
203 this.userHeaders = ImmutableHeaders.of(headers.map(), ALLOWED_HEADERS); |
|
204 this.systemHeaders = parent.systemHeaders; |
|
205 this.expectContinue = parent.expectContinue; |
|
206 this.secure = parent.secure; |
|
207 this.requestPublisher = parent.requestPublisher; |
|
208 this.acc = parent.acc; |
|
209 this.timeout = parent.timeout; |
|
210 this.version = parent.version; |
|
211 this.authority = null; |
|
212 } |
|
213 |
|
214 @Override |
|
215 public String toString() { |
|
216 return (uri == null ? "" : uri.toString()) + " " + method; |
|
217 } |
|
218 |
|
219 @Override |
|
220 public HttpHeaders headers() { |
|
221 return userHeaders; |
|
222 } |
|
223 |
|
224 InetSocketAddress authority() { return authority; } |
|
225 |
|
226 void setH2Upgrade(Http2ClientImpl h2client) { |
|
227 systemHeaders.setHeader("Connection", "Upgrade, HTTP2-Settings"); |
|
228 systemHeaders.setHeader("Upgrade", "h2c"); |
|
229 systemHeaders.setHeader("HTTP2-Settings", h2client.getSettingsString()); |
|
230 } |
|
231 |
|
232 @Override |
|
233 public boolean expectContinue() { return expectContinue; } |
|
234 |
|
235 /** Retrieves the proxy, from the given ProxySelector, if there is one. */ |
|
236 private static Proxy retrieveProxy(ProxySelector ps, URI uri) { |
|
237 Proxy proxy = null; |
|
238 List<Proxy> pl = ps.select(uri); |
|
239 if (!pl.isEmpty()) { |
|
240 Proxy p = pl.get(0); |
|
241 if (p.type() == Proxy.Type.HTTP) |
|
242 proxy = p; |
|
243 } |
|
244 return proxy; |
|
245 } |
|
246 |
|
247 InetSocketAddress proxy() { |
|
248 if (proxy == null || proxy.type() != Proxy.Type.HTTP |
|
249 || method.equalsIgnoreCase("CONNECT")) { |
|
250 return null; |
|
251 } |
|
252 return (InetSocketAddress)proxy.address(); |
|
253 } |
|
254 |
|
255 boolean secure() { return secure; } |
|
256 |
|
257 @Override |
|
258 public void setProxy(Proxy proxy) { |
|
259 assert isWebSocket; |
|
260 this.proxy = proxy; |
|
261 } |
|
262 |
|
263 @Override |
|
264 public void isWebSocket(boolean is) { |
|
265 isWebSocket = is; |
|
266 } |
|
267 |
|
268 boolean isWebSocket() { |
|
269 return isWebSocket; |
|
270 } |
|
271 |
|
272 @Override |
|
273 public Optional<BodyPublisher> bodyPublisher() { |
|
274 return requestPublisher == null ? Optional.empty() |
|
275 : Optional.of(requestPublisher); |
|
276 } |
|
277 |
|
278 /** |
|
279 * Returns the request method for this request. If not set explicitly, |
|
280 * the default method for any request is "GET". |
|
281 */ |
|
282 @Override |
|
283 public String method() { return method; } |
|
284 |
|
285 @Override |
|
286 public URI uri() { return uri; } |
|
287 |
|
288 @Override |
|
289 public Optional<Duration> timeout() { |
|
290 return timeout == null ? Optional.empty() : Optional.of(timeout); |
|
291 } |
|
292 |
|
293 HttpHeaders getUserHeaders() { return userHeaders; } |
|
294 |
|
295 HttpHeadersImpl getSystemHeaders() { return systemHeaders; } |
|
296 |
|
297 @Override |
|
298 public Optional<HttpClient.Version> version() { return version; } |
|
299 |
|
300 void addSystemHeader(String name, String value) { |
|
301 systemHeaders.addHeader(name, value); |
|
302 } |
|
303 |
|
304 @Override |
|
305 public void setSystemHeader(String name, String value) { |
|
306 systemHeaders.setHeader(name, value); |
|
307 } |
|
308 |
|
309 InetSocketAddress getAddress() { |
|
310 URI uri = uri(); |
|
311 if (uri == null) { |
|
312 return authority(); |
|
313 } |
|
314 int p = uri.getPort(); |
|
315 if (p == -1) { |
|
316 if (uri.getScheme().equalsIgnoreCase("https")) { |
|
317 p = 443; |
|
318 } else { |
|
319 p = 80; |
|
320 } |
|
321 } |
|
322 final String host = uri.getHost(); |
|
323 final int port = p; |
|
324 if (proxy() == null) { |
|
325 PrivilegedAction<InetSocketAddress> pa = () -> new InetSocketAddress(host, port); |
|
326 return AccessController.doPrivileged(pa); |
|
327 } else { |
|
328 return InetSocketAddress.createUnresolved(host, port); |
|
329 } |
|
330 } |
|
331 } |
|