author | jwilhelm |
Thu, 12 Sep 2019 03:21:11 +0200 | |
changeset 58094 | 0f6c749acd15 |
parent 53521 | 41fa3e6f2785 |
child 58758 | 2b13d126a2d8 |
permissions | -rw-r--r-- |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
1 |
/* |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
2 |
* Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved. |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
4 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. Oracle designates this |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
8 |
* particular file as subject to the "Classpath" exception as provided |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
9 |
* by Oracle in the LICENSE file that accompanied this code. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
10 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
15 |
* accompanied this code). |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
16 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
17 |
* You should have received a copy of the GNU General Public License version |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
20 |
* |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
22 |
* or visit www.oracle.com if you need additional information or have any |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
23 |
* questions. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
24 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
25 |
|
49765 | 26 |
package jdk.internal.net.http; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
27 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
28 |
import java.io.IOException; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
29 |
import java.net.InetSocketAddress; |
48083 | 30 |
import java.net.Proxy; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
31 |
import java.net.ProxySelector; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
32 |
import java.net.URI; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
33 |
import java.security.AccessControlContext; |
48083 | 34 |
import java.security.AccessController; |
35 |
import java.security.PrivilegedAction; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
36 |
import java.time.Duration; |
48083 | 37 |
import java.util.List; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
38 |
import java.util.Locale; |
49765 | 39 |
import java.util.Objects; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
40 |
import java.util.Optional; |
49765 | 41 |
import java.net.http.HttpClient; |
42 |
import java.net.http.HttpHeaders; |
|
43 |
import java.net.http.HttpRequest; |
|
50681 | 44 |
import jdk.internal.net.http.common.HttpHeadersBuilder; |
49765 | 45 |
import jdk.internal.net.http.common.Utils; |
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
46 |
import jdk.internal.net.http.websocket.OpeningHandshake; |
49765 | 47 |
import jdk.internal.net.http.websocket.WebSocketRequest; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
48 |
|
49765 | 49 |
import static jdk.internal.net.http.common.Utils.ALLOWED_HEADERS; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
50 |
|
50681 | 51 |
public class HttpRequestImpl extends HttpRequest implements WebSocketRequest { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
52 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
53 |
private final HttpHeaders userHeaders; |
50681 | 54 |
private final HttpHeadersBuilder systemHeadersBuilder; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
55 |
private final URI uri; |
49765 | 56 |
private volatile Proxy proxy; // ensure safe publishing |
57 |
private final InetSocketAddress authority; // only used when URI not specified |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
58 |
private final String method; |
48083 | 59 |
final BodyPublisher requestPublisher; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
60 |
final boolean secure; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
61 |
final boolean expectContinue; |
49765 | 62 |
private volatile boolean isWebSocket; |
63 |
private volatile AccessControlContext acc; |
|
48083 | 64 |
private final Duration timeout; // may be null |
44854
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
43999
diff
changeset
|
65 |
private final Optional<HttpClient.Version> version; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
66 |
|
48083 | 67 |
private static String userAgent() { |
68 |
PrivilegedAction<String> pa = () -> System.getProperty("java.version"); |
|
69 |
String version = AccessController.doPrivileged(pa); |
|
70 |
return "Java-http-client/" + version; |
|
71 |
} |
|
72 |
||
73 |
/** The value of the User-Agent header for all requests sent by the client. */ |
|
74 |
public static final String USER_AGENT = userAgent(); |
|
75 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
76 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
77 |
* Creates an HttpRequestImpl from the given builder. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
78 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
79 |
public HttpRequestImpl(HttpRequestBuilderImpl builder) { |
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
45119
diff
changeset
|
80 |
String method = builder.method(); |
48083 | 81 |
this.method = method == null ? "GET" : method; |
50681 | 82 |
this.userHeaders = HttpHeaders.of(builder.headersBuilder().map(), ALLOWED_HEADERS); |
83 |
this.systemHeadersBuilder = new HttpHeadersBuilder(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
84 |
this.uri = builder.uri(); |
48083 | 85 |
assert uri != null; |
86 |
this.proxy = null; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
87 |
this.expectContinue = builder.expectContinue(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
88 |
this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https"); |
48083 | 89 |
this.requestPublisher = builder.bodyPublisher(); // may be null |
90 |
this.timeout = builder.timeout(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
91 |
this.version = builder.version(); |
49765 | 92 |
this.authority = null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
93 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
94 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
95 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
96 |
* Creates an HttpRequestImpl from the given request. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
97 |
*/ |
49765 | 98 |
public HttpRequestImpl(HttpRequest request, ProxySelector ps) { |
45713
ee3f2cbfe23a
8182589: TLS SNI in new Java 9 client is not available
michaelm
parents:
45119
diff
changeset
|
99 |
String method = request.method(); |
49765 | 100 |
if (method != null && !Utils.isValidName(method)) |
101 |
throw new IllegalArgumentException("illegal method \"" |
|
102 |
+ method.replace("\n","\\n") |
|
103 |
.replace("\r", "\\r") |
|
104 |
.replace("\t", "\\t") |
|
105 |
+ "\""); |
|
106 |
URI requestURI = Objects.requireNonNull(request.uri(), |
|
107 |
"uri must be non null"); |
|
108 |
Duration timeout = request.timeout().orElse(null); |
|
48083 | 109 |
this.method = method == null ? "GET" : method; |
50681 | 110 |
this.userHeaders = HttpHeaders.of(request.headers().map(), Utils.VALIDATE_USER_HEADER); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
111 |
if (request instanceof HttpRequestImpl) { |
49765 | 112 |
// all cases exception WebSocket should have a new system headers |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
42460
diff
changeset
|
113 |
this.isWebSocket = ((HttpRequestImpl) request).isWebSocket; |
49765 | 114 |
if (isWebSocket) { |
50681 | 115 |
this.systemHeadersBuilder = ((HttpRequestImpl)request).systemHeadersBuilder; |
49765 | 116 |
} else { |
50681 | 117 |
this.systemHeadersBuilder = new HttpHeadersBuilder(); |
49765 | 118 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
119 |
} else { |
49765 | 120 |
HttpRequestBuilderImpl.checkURI(requestURI); |
121 |
checkTimeout(timeout); |
|
50681 | 122 |
this.systemHeadersBuilder = new HttpHeadersBuilder(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
123 |
} |
50681 | 124 |
if (!userHeaders.firstValue("User-Agent").isPresent()) { |
125 |
this.systemHeadersBuilder.setHeader("User-Agent", USER_AGENT); |
|
126 |
} |
|
49765 | 127 |
this.uri = requestURI; |
48083 | 128 |
if (isWebSocket) { |
129 |
// WebSocket determines and sets the proxy itself |
|
130 |
this.proxy = ((HttpRequestImpl) request).proxy; |
|
131 |
} else { |
|
132 |
if (ps != null) |
|
133 |
this.proxy = retrieveProxy(ps, uri); |
|
134 |
else |
|
135 |
this.proxy = null; |
|
136 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
137 |
this.expectContinue = request.expectContinue(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
138 |
this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https"); |
48083 | 139 |
this.requestPublisher = request.bodyPublisher().orElse(null); |
49765 | 140 |
this.timeout = timeout; |
141 |
this.version = request.version(); |
|
142 |
this.authority = null; |
|
143 |
} |
|
144 |
||
145 |
private static void checkTimeout(Duration duration) { |
|
146 |
if (duration != null) { |
|
147 |
if (duration.isNegative() || Duration.ZERO.equals(duration)) |
|
148 |
throw new IllegalArgumentException("Invalid duration: " + duration); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
149 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
150 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
151 |
|
49765 | 152 |
/** Returns a new instance suitable for redirection. */ |
153 |
public static HttpRequestImpl newInstanceForRedirection(URI uri, |
|
154 |
String method, |
|
155 |
HttpRequestImpl other) { |
|
156 |
return new HttpRequestImpl(uri, method, other); |
|
157 |
} |
|
158 |
||
159 |
/** Returns a new instance suitable for authentication. */ |
|
160 |
public static HttpRequestImpl newInstanceForAuthentication(HttpRequestImpl other) { |
|
53521
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
161 |
HttpRequestImpl request = new HttpRequestImpl(other.uri(), other.method(), other); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
162 |
if (request.isWebSocket()) { |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
163 |
Utils.setWebSocketUpgradeHeaders(request); |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
164 |
} |
41fa3e6f2785
8217429: WebSocket over authenticating proxy fails to send Upgrade headers
chegar
parents:
50681
diff
changeset
|
165 |
return request; |
49765 | 166 |
} |
167 |
||
168 |
/** |
|
169 |
* Creates a HttpRequestImpl using fields of an existing request impl. |
|
170 |
* The newly created HttpRequestImpl does not copy the system headers. |
|
171 |
*/ |
|
172 |
private HttpRequestImpl(URI uri, |
|
173 |
String method, |
|
174 |
HttpRequestImpl other) { |
|
175 |
assert method == null || Utils.isValidName(method); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
176 |
this.method = method == null? "GET" : method; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
177 |
this.userHeaders = other.userHeaders; |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
42460
diff
changeset
|
178 |
this.isWebSocket = other.isWebSocket; |
50681 | 179 |
this.systemHeadersBuilder = new HttpHeadersBuilder(); |
180 |
if (!userHeaders.firstValue("User-Agent").isPresent()) { |
|
181 |
this.systemHeadersBuilder.setHeader("User-Agent", USER_AGENT); |
|
182 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
183 |
this.uri = uri; |
48083 | 184 |
this.proxy = other.proxy; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
185 |
this.expectContinue = other.expectContinue; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
186 |
this.secure = uri.getScheme().toLowerCase(Locale.US).equals("https"); |
48083 | 187 |
this.requestPublisher = other.requestPublisher; // may be null |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
188 |
this.acc = other.acc; |
48083 | 189 |
this.timeout = other.timeout; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
190 |
this.version = other.version(); |
49765 | 191 |
this.authority = null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
192 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
193 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
194 |
/* used for creating CONNECT requests */ |
49765 | 195 |
HttpRequestImpl(String method, InetSocketAddress authority, HttpHeaders headers) { |
43999
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
42460
diff
changeset
|
196 |
// TODO: isWebSocket flag is not specified, but the assumption is that |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
42460
diff
changeset
|
197 |
// such a request will never be made on a connection that will be returned |
4cc44dd9f14f
8164625: Pooled HttpConnection should be removed during close
prappo
parents:
42460
diff
changeset
|
198 |
// to the connection pool (we might need to revisit this constructor later) |
49765 | 199 |
assert "CONNECT".equalsIgnoreCase(method); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
200 |
this.method = method; |
50681 | 201 |
this.systemHeadersBuilder = new HttpHeadersBuilder(); |
202 |
this.userHeaders = headers; |
|
48083 | 203 |
this.uri = URI.create("socket://" + authority.getHostString() + ":" |
204 |
+ Integer.toString(authority.getPort()) + "/"); |
|
205 |
this.proxy = null; |
|
206 |
this.requestPublisher = null; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
207 |
this.authority = authority; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
208 |
this.secure = false; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
209 |
this.expectContinue = false; |
48083 | 210 |
this.timeout = null; |
211 |
// The CONNECT request sent for tunneling is only used in two cases: |
|
212 |
// 1. websocket, which only supports HTTP/1.1 |
|
213 |
// 2. SSL tunneling through a HTTP/1.1 proxy |
|
214 |
// In either case we do not want to upgrade the connection to the proxy. |
|
215 |
// What we want to possibly upgrade is the tunneled connection to the |
|
216 |
// target server (so not the CONNECT request itself) |
|
217 |
this.version = Optional.of(HttpClient.Version.HTTP_1_1); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
218 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
219 |
|
49765 | 220 |
final boolean isConnect() { |
221 |
return "CONNECT".equalsIgnoreCase(method); |
|
222 |
} |
|
223 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
224 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
225 |
* Creates a HttpRequestImpl from the given set of Headers and the associated |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
226 |
* "parent" request. Fields not taken from the headers are taken from the |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
227 |
* parent. |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
228 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
229 |
static HttpRequestImpl createPushRequest(HttpRequestImpl parent, |
50681 | 230 |
HttpHeaders headers) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
231 |
throws IOException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
232 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
233 |
return new HttpRequestImpl(parent, headers); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
234 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
235 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
236 |
// only used for push requests |
50681 | 237 |
private HttpRequestImpl(HttpRequestImpl parent, HttpHeaders headers) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
238 |
throws IOException |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
239 |
{ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
240 |
this.method = headers.firstValue(":method") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
241 |
.orElseThrow(() -> new IOException("No method in Push Promise")); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
242 |
String path = headers.firstValue(":path") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
243 |
.orElseThrow(() -> new IOException("No path in Push Promise")); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
244 |
String scheme = headers.firstValue(":scheme") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
245 |
.orElseThrow(() -> new IOException("No scheme in Push Promise")); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
246 |
String authority = headers.firstValue(":authority") |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
247 |
.orElseThrow(() -> new IOException("No authority in Push Promise")); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
248 |
StringBuilder sb = new StringBuilder(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
249 |
sb.append(scheme).append("://").append(authority).append(path); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
250 |
this.uri = URI.create(sb.toString()); |
48083 | 251 |
this.proxy = null; |
50681 | 252 |
this.userHeaders = HttpHeaders.of(headers.map(), ALLOWED_HEADERS); |
253 |
this.systemHeadersBuilder = parent.systemHeadersBuilder; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
254 |
this.expectContinue = parent.expectContinue; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
255 |
this.secure = parent.secure; |
48083 | 256 |
this.requestPublisher = parent.requestPublisher; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
257 |
this.acc = parent.acc; |
48083 | 258 |
this.timeout = parent.timeout; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
259 |
this.version = parent.version; |
49765 | 260 |
this.authority = null; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
261 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
262 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
263 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
264 |
public String toString() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
265 |
return (uri == null ? "" : uri.toString()) + " " + method; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
266 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
267 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
268 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
269 |
public HttpHeaders headers() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
270 |
return userHeaders; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
271 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
272 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
273 |
InetSocketAddress authority() { return authority; } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
274 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
275 |
void setH2Upgrade(Http2ClientImpl h2client) { |
50681 | 276 |
systemHeadersBuilder.setHeader("Connection", "Upgrade, HTTP2-Settings"); |
277 |
systemHeadersBuilder.setHeader("Upgrade", "h2c"); |
|
278 |
systemHeadersBuilder.setHeader("HTTP2-Settings", h2client.getSettingsString()); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
279 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
280 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
281 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
282 |
public boolean expectContinue() { return expectContinue; } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
283 |
|
48083 | 284 |
/** Retrieves the proxy, from the given ProxySelector, if there is one. */ |
285 |
private static Proxy retrieveProxy(ProxySelector ps, URI uri) { |
|
286 |
Proxy proxy = null; |
|
287 |
List<Proxy> pl = ps.select(uri); |
|
288 |
if (!pl.isEmpty()) { |
|
289 |
Proxy p = pl.get(0); |
|
290 |
if (p.type() == Proxy.Type.HTTP) |
|
291 |
proxy = p; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
292 |
} |
48083 | 293 |
return proxy; |
294 |
} |
|
295 |
||
296 |
InetSocketAddress proxy() { |
|
297 |
if (proxy == null || proxy.type() != Proxy.Type.HTTP |
|
298 |
|| method.equalsIgnoreCase("CONNECT")) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
299 |
return null; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
300 |
} |
48083 | 301 |
return (InetSocketAddress)proxy.address(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
302 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
303 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
304 |
boolean secure() { return secure; } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
305 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
306 |
@Override |
48083 | 307 |
public void setProxy(Proxy proxy) { |
308 |
assert isWebSocket; |
|
309 |
this.proxy = proxy; |
|
310 |
} |
|
311 |
||
312 |
@Override |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
313 |
public void isWebSocket(boolean is) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
314 |
isWebSocket = is; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
315 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
316 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
317 |
boolean isWebSocket() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
318 |
return isWebSocket; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
319 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
320 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
321 |
@Override |
48083 | 322 |
public Optional<BodyPublisher> bodyPublisher() { |
323 |
return requestPublisher == null ? Optional.empty() |
|
324 |
: Optional.of(requestPublisher); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
325 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
326 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
327 |
/** |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
328 |
* Returns the request method for this request. If not set explicitly, |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
329 |
* the default method for any request is "GET". |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
330 |
*/ |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
331 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
332 |
public String method() { return method; } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
333 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
334 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
335 |
public URI uri() { return uri; } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
336 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
337 |
@Override |
48083 | 338 |
public Optional<Duration> timeout() { |
339 |
return timeout == null ? Optional.empty() : Optional.of(timeout); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
340 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
341 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
342 |
HttpHeaders getUserHeaders() { return userHeaders; } |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
343 |
|
50681 | 344 |
HttpHeadersBuilder getSystemHeadersBuilder() { return systemHeadersBuilder; } |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
345 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
346 |
@Override |
44854
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
43999
diff
changeset
|
347 |
public Optional<HttpClient.Version> version() { return version; } |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
348 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
349 |
void addSystemHeader(String name, String value) { |
50681 | 350 |
systemHeadersBuilder.addHeader(name, value); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
351 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
352 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
353 |
@Override |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
354 |
public void setSystemHeader(String name, String value) { |
50681 | 355 |
systemHeadersBuilder.setHeader(name, value); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
356 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
357 |
|
48083 | 358 |
InetSocketAddress getAddress() { |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
359 |
URI uri = uri(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
360 |
if (uri == null) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
361 |
return authority(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
362 |
} |
48083 | 363 |
int p = uri.getPort(); |
364 |
if (p == -1) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
365 |
if (uri.getScheme().equalsIgnoreCase("https")) { |
48083 | 366 |
p = 443; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
367 |
} else { |
48083 | 368 |
p = 80; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
369 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
370 |
} |
48083 | 371 |
final String host = uri.getHost(); |
372 |
final int port = p; |
|
373 |
if (proxy() == null) { |
|
374 |
PrivilegedAction<InetSocketAddress> pa = () -> new InetSocketAddress(host, port); |
|
375 |
return AccessController.doPrivileged(pa); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
376 |
} else { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
377 |
return InetSocketAddress.createUnresolved(host, port); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
378 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
379 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
diff
changeset
|
380 |
} |