author | jboes |
Fri, 08 Nov 2019 11:15:16 +0000 | |
changeset 59029 | 3786a0962570 |
parent 53698 | 68ca8b201925 |
permissions | -rw-r--r-- |
50681 | 1 |
/* |
53698
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
2 |
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved. |
50681 | 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. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
26 |
* @summary Verify that some special headers - such as User-Agent |
|
27 |
* can be specified by the caller. |
|
53698
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
28 |
* @bug 8203771 8218546 |
50681 | 29 |
* @modules java.base/sun.net.www.http |
30 |
* java.net.http/jdk.internal.net.http.common |
|
31 |
* java.net.http/jdk.internal.net.http.frame |
|
32 |
* java.net.http/jdk.internal.net.http.hpack |
|
33 |
* java.logging |
|
34 |
* jdk.httpserver |
|
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
52106
diff
changeset
|
35 |
* @library /test/lib http2/server |
50681 | 36 |
* @build Http2TestServer HttpServerAdapters SpecialHeadersTest |
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
52106
diff
changeset
|
37 |
* @build jdk.test.lib.net.SimpleSSLContext |
50681 | 38 |
* @run testng/othervm |
39 |
* -Djdk.httpclient.HttpClient.log=requests,headers,errors |
|
40 |
* SpecialHeadersTest |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
41 |
* @run testng/othervm -Djdk.httpclient.allowRestrictedHeaders=Host |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
42 |
* -Djdk.httpclient.HttpClient.log=requests,headers,errors |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
43 |
* SpecialHeadersTest |
50681 | 44 |
*/ |
45 |
||
46 |
import com.sun.net.httpserver.HttpServer; |
|
47 |
import com.sun.net.httpserver.HttpsConfigurator; |
|
48 |
import com.sun.net.httpserver.HttpsServer; |
|
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
52106
diff
changeset
|
49 |
import jdk.test.lib.net.SimpleSSLContext; |
50681 | 50 |
import org.testng.annotations.AfterTest; |
51 |
import org.testng.annotations.BeforeTest; |
|
52 |
import org.testng.annotations.DataProvider; |
|
53 |
import org.testng.annotations.Test; |
|
54 |
||
55 |
import javax.net.ssl.SSLContext; |
|
56 |
import java.io.IOException; |
|
57 |
import java.io.InputStream; |
|
58 |
import java.io.OutputStream; |
|
59 |
import java.net.InetAddress; |
|
60 |
import java.net.InetSocketAddress; |
|
61 |
import java.net.URI; |
|
62 |
import java.net.http.HttpClient; |
|
52106
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
63 |
import java.net.http.HttpHeaders; |
50681 | 64 |
import java.net.http.HttpRequest; |
65 |
import java.net.http.HttpResponse; |
|
66 |
import java.net.http.HttpResponse.BodyHandlers; |
|
52106
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
67 |
import java.time.Duration; |
50681 | 68 |
import java.util.ArrayList; |
69 |
import java.util.Arrays; |
|
70 |
import java.util.List; |
|
71 |
import java.util.Locale; |
|
72 |
import java.util.Map; |
|
52106
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
73 |
import java.util.Optional; |
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
74 |
import java.util.function.Function; |
50681 | 75 |
import static java.lang.System.out; |
76 |
import static java.net.http.HttpClient.Builder.NO_PROXY; |
|
53698
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
77 |
import static java.net.http.HttpClient.Version.HTTP_2; |
50681 | 78 |
import static java.nio.charset.StandardCharsets.US_ASCII; |
52106
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
79 |
import org.testng.Assert; |
50681 | 80 |
import static org.testng.Assert.assertEquals; |
53698
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
81 |
import static org.testng.Assert.assertTrue; |
50681 | 82 |
|
83 |
public class SpecialHeadersTest implements HttpServerAdapters { |
|
84 |
||
85 |
SSLContext sslContext; |
|
86 |
HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ] |
|
87 |
HttpTestServer httpsTestServer; // HTTPS/1.1 |
|
88 |
HttpTestServer http2TestServer; // HTTP/2 ( h2c ) |
|
89 |
HttpTestServer https2TestServer; // HTTP/2 ( h2 ) |
|
90 |
String httpURI; |
|
91 |
String httpsURI; |
|
92 |
String http2URI; |
|
93 |
String https2URI; |
|
94 |
||
95 |
static final String[][] headerNamesAndValues = new String[][]{ |
|
96 |
{"User-Agent: <DEFAULT>"}, |
|
97 |
{"User-Agent: camel-cased"}, |
|
98 |
{"user-agent: all-lower-case"}, |
|
99 |
{"user-Agent: mixed"}, |
|
52106
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
100 |
// headers which were restricted before and are now allowable |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
101 |
{"referer: lower"}, |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
102 |
{"Referer: normal"}, |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
103 |
{"REFERER: upper"}, |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
104 |
{"origin: lower"}, |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
105 |
{"Origin: normal"}, |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
106 |
{"ORIGIN: upper"}, |
50681 | 107 |
}; |
108 |
||
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
109 |
// Needs net.property enabled for this part of test |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
110 |
static final String[][] headerNamesAndValues1 = new String[][]{ |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
111 |
{"Host: <DEFAULT>"}, |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
112 |
{"Host: camel-cased"}, |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
113 |
{"host: all-lower-case"}, |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
114 |
{"hoSt: mixed"} |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
115 |
}; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
116 |
|
50681 | 117 |
@DataProvider(name = "variants") |
118 |
public Object[][] variants() { |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
119 |
String prop = System.getProperty("jdk.httpclient.allowRestrictedHeaders"); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
120 |
boolean hostTest = prop != null && prop.equalsIgnoreCase("host"); |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
121 |
final String[][] testInput = hostTest ? headerNamesAndValues1 : headerNamesAndValues; |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
122 |
|
50681 | 123 |
List<Object[]> list = new ArrayList<>(); |
124 |
||
125 |
for (boolean sameClient : new boolean[] { false, true }) { |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
126 |
Arrays.asList(testInput).stream() |
50681 | 127 |
.map(e -> new Object[] {httpURI, e[0], sameClient}) |
128 |
.forEach(list::add); |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
129 |
Arrays.asList(testInput).stream() |
50681 | 130 |
.map(e -> new Object[] {httpsURI, e[0], sameClient}) |
131 |
.forEach(list::add); |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
132 |
Arrays.asList(testInput).stream() |
50681 | 133 |
.map(e -> new Object[] {http2URI, e[0], sameClient}) |
134 |
.forEach(list::add); |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
135 |
Arrays.asList(testInput).stream() |
50681 | 136 |
.map(e -> new Object[] {https2URI, e[0], sameClient}) |
137 |
.forEach(list::add); |
|
138 |
} |
|
139 |
return list.stream().toArray(Object[][]::new); |
|
140 |
} |
|
141 |
||
142 |
static final int ITERATION_COUNT = 3; // checks upgrade and re-use |
|
143 |
||
144 |
static String userAgent() { |
|
145 |
return "Java-http-client/" + System.getProperty("java.version"); |
|
146 |
} |
|
147 |
||
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
148 |
static final Map<String, Function<URI,String>> DEFAULTS = Map.of( |
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
149 |
"USER-AGENT", u -> userAgent(), "HOST", u -> u.getRawAuthority()); |
50681 | 150 |
|
151 |
@Test(dataProvider = "variants") |
|
53698
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
152 |
void test(String uriString, |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
153 |
String headerNameAndValue, |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
154 |
boolean sameClient) |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
155 |
throws Exception |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
156 |
{ |
50681 | 157 |
out.println("\n--- Starting "); |
158 |
||
159 |
int index = headerNameAndValue.indexOf(":"); |
|
160 |
String name = headerNameAndValue.substring(0, index); |
|
161 |
String v = headerNameAndValue.substring(index+1).trim(); |
|
162 |
String key = name.toUpperCase(Locale.ROOT); |
|
163 |
boolean useDefault = "<DEFAULT>".equals(v); |
|
164 |
||
165 |
URI uri = URI.create(uriString+"?name="+key); |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
166 |
String value = useDefault ? DEFAULTS.get(key).apply(uri) : v; |
50681 | 167 |
|
168 |
HttpClient client = null; |
|
169 |
for (int i=0; i< ITERATION_COUNT; i++) { |
|
170 |
if (!sameClient || client == null) |
|
171 |
client = HttpClient.newBuilder() |
|
172 |
.proxy(NO_PROXY) |
|
173 |
.sslContext(sslContext) |
|
174 |
.build(); |
|
175 |
||
176 |
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri); |
|
177 |
if (!useDefault) { |
|
178 |
requestBuilder.header(name, value); |
|
179 |
} |
|
180 |
HttpRequest request = requestBuilder.build(); |
|
181 |
HttpResponse<String> resp = client.send(request, BodyHandlers.ofString()); |
|
182 |
||
183 |
out.println("Got response: " + resp); |
|
184 |
out.println("Got body: " + resp.body()); |
|
185 |
assertEquals(resp.statusCode(), 200, |
|
186 |
"Expected 200, got:" + resp.statusCode()); |
|
187 |
||
53698
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
188 |
boolean isInitialRequest = i == 0; |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
189 |
boolean isSecure = uri.getScheme().equalsIgnoreCase("https"); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
190 |
boolean isHTTP2 = resp.version() == HTTP_2; |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
191 |
boolean isNotH2CUpgrade = isSecure || (sameClient == true && !isInitialRequest); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
192 |
boolean isDefaultHostHeader = name.equalsIgnoreCase("host") && useDefault; |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
193 |
|
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
194 |
// By default, HTTP/2 sets the `:authority:` pseudo-header, instead |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
195 |
// of the `Host` header. Therefore, there should be no "X-Host" |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
196 |
// header in the response, except the response to the h2c Upgrade |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
197 |
// request which will have been sent through HTTP/1.1. |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
198 |
|
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
199 |
if (isDefaultHostHeader && isHTTP2 && isNotH2CUpgrade) { |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
200 |
assertTrue(resp.headers().firstValue("X-" + key).isEmpty()); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
201 |
assertTrue(resp.headers().allValues("X-" + key).isEmpty()); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
202 |
out.println("No X-" + key + " header received, as expected"); |
50681 | 203 |
} else { |
53698
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
204 |
String receivedHeaderString = value == null ? null |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
205 |
: resp.headers().firstValue("X-"+key).get(); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
206 |
out.println("Got X-" + key + ": " + resp.headers().allValues("X-"+key)); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
207 |
if (value != null) { |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
208 |
assertEquals(receivedHeaderString, value); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
209 |
assertEquals(resp.headers().allValues("X-"+key), List.of(value)); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
210 |
} else { |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
211 |
assertEquals(resp.headers().allValues("X-"+key).size(), 0); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
212 |
} |
50681 | 213 |
} |
214 |
} |
|
215 |
} |
|
216 |
||
217 |
@Test(dataProvider = "variants") |
|
53698
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
218 |
void testHomeMadeIllegalHeader(String uriString, |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
219 |
String headerNameAndValue, |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
220 |
boolean sameClient) |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
221 |
throws Exception |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
222 |
{ |
52106
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
223 |
out.println("\n--- Starting "); |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
224 |
final URI uri = URI.create(uriString); |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
225 |
|
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
226 |
HttpClient client = HttpClient.newBuilder() |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
227 |
.proxy(NO_PROXY) |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
228 |
.sslContext(sslContext) |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
229 |
.build(); |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
230 |
|
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
231 |
// Test a request which contains an illegal header created |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
232 |
HttpRequest req = new HttpRequest() { |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
233 |
@Override public Optional<BodyPublisher> bodyPublisher() { |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
234 |
return Optional.of(BodyPublishers.noBody()); |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
235 |
} |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
236 |
@Override public String method() { |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
237 |
return "GET"; |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
238 |
} |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
239 |
@Override public Optional<Duration> timeout() { |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
240 |
return Optional.empty(); |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
241 |
} |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
242 |
@Override public boolean expectContinue() { |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
243 |
return false; |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
244 |
} |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
245 |
@Override public URI uri() { |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
246 |
return uri; |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
247 |
} |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
248 |
@Override public Optional<HttpClient.Version> version() { |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
249 |
return Optional.empty(); |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
250 |
} |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
251 |
@Override public HttpHeaders headers() { |
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
252 |
Map<String, List<String>> map = Map.of("upgrade", List.of("http://foo.com")); |
52106
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
253 |
return HttpHeaders.of(map, (x, y) -> true); |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
254 |
} |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
255 |
}; |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
256 |
|
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
257 |
try { |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
258 |
HttpResponse<String> response = client.send(req, BodyHandlers.ofString()); |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
259 |
Assert.fail("Unexpected reply: " + response); |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
260 |
} catch (IllegalArgumentException ee) { |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
261 |
out.println("Got IAE as expected"); |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
262 |
} |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
263 |
} |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
264 |
|
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
265 |
@Test(dataProvider = "variants") |
50681 | 266 |
void testAsync(String uriString, String headerNameAndValue, boolean sameClient) { |
267 |
out.println("\n--- Starting "); |
|
268 |
int index = headerNameAndValue.indexOf(":"); |
|
269 |
String name = headerNameAndValue.substring(0, index); |
|
270 |
String v = headerNameAndValue.substring(index+1).trim(); |
|
271 |
String key = name.toUpperCase(Locale.ROOT); |
|
272 |
boolean useDefault = "<DEFAULT>".equals(v); |
|
273 |
||
274 |
URI uri = URI.create(uriString+"?name="+key); |
|
52554
5f1ca46703f9
8213189: Make restricted headers in HTTP Client configurable and remove Date by default
michaelm
parents:
52121
diff
changeset
|
275 |
String value = useDefault ? DEFAULTS.get(key).apply(uri) : v; |
50681 | 276 |
|
277 |
HttpClient client = null; |
|
278 |
for (int i=0; i< ITERATION_COUNT; i++) { |
|
279 |
if (!sameClient || client == null) |
|
280 |
client = HttpClient.newBuilder() |
|
281 |
.proxy(NO_PROXY) |
|
282 |
.sslContext(sslContext) |
|
283 |
.build(); |
|
284 |
||
285 |
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder(uri); |
|
286 |
if (!useDefault) { |
|
287 |
requestBuilder.header(name, value); |
|
288 |
} |
|
289 |
HttpRequest request = requestBuilder.build(); |
|
290 |
||
53698
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
291 |
boolean isInitialRequest = i == 0; |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
292 |
boolean isSecure = uri.getScheme().equalsIgnoreCase("https"); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
293 |
boolean isNotH2CUpgrade = isSecure || (sameClient == true && !isInitialRequest); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
294 |
boolean isDefaultHostHeader = name.equalsIgnoreCase("host") && useDefault; |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
295 |
|
50681 | 296 |
client.sendAsync(request, BodyHandlers.ofString()) |
297 |
.thenApply(response -> { |
|
298 |
out.println("Got response: " + response); |
|
299 |
out.println("Got body: " + response.body()); |
|
300 |
assertEquals(response.statusCode(), 200); |
|
301 |
return response;}) |
|
302 |
.thenAccept(resp -> { |
|
53698
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
303 |
// By default, HTTP/2 sets the `:authority:` pseudo-header, instead |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
304 |
// of the `Host` header. Therefore, there should be no "X-Host" |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
305 |
// header in the response, except the response to the h2c Upgrade |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
306 |
// request which will have been sent through HTTP/1.1. |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
307 |
|
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
308 |
if (isDefaultHostHeader && resp.version() == HTTP_2 && isNotH2CUpgrade) { |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
309 |
assertTrue(resp.headers().firstValue("X-" + key).isEmpty()); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
310 |
assertTrue(resp.headers().allValues("X-" + key).isEmpty()); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
311 |
out.println("No X-" + key + " header received, as expected"); |
50681 | 312 |
} else { |
53698
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
313 |
String receivedHeaderString = value == null ? null |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
314 |
: resp.headers().firstValue("X-"+key).get(); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
315 |
out.println("Got X-" + key + ": " + resp.headers().allValues("X-"+key)); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
316 |
if (value != null) { |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
317 |
assertEquals(receivedHeaderString, value); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
318 |
assertEquals(resp.headers().allValues("X-" + key), List.of(value)); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
319 |
} else { |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
320 |
assertEquals(resp.headers().allValues("X-" + key).size(), 1); |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
321 |
} |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
322 |
} |
68ca8b201925
8218546: Unable to connect to https://google.com using java.net.HttpClient
chegar
parents:
52554
diff
changeset
|
323 |
}) |
50681 | 324 |
.join(); |
325 |
} |
|
326 |
} |
|
327 |
||
328 |
static String serverAuthority(HttpTestServer server) { |
|
329 |
return InetAddress.getLoopbackAddress().getHostName() + ":" |
|
330 |
+ server.getAddress().getPort(); |
|
331 |
} |
|
332 |
||
333 |
@BeforeTest |
|
334 |
public void setup() throws Exception { |
|
335 |
sslContext = new SimpleSSLContext().get(); |
|
336 |
if (sslContext == null) |
|
337 |
throw new AssertionError("Unexpected null sslContext"); |
|
338 |
||
339 |
HttpTestHandler handler = new HttpUriStringHandler(); |
|
340 |
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); |
|
341 |
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0)); |
|
342 |
httpTestServer.addHandler(handler, "/http1"); |
|
343 |
httpURI = "http://" + serverAuthority(httpTestServer) + "/http1"; |
|
344 |
||
345 |
HttpsServer httpsServer = HttpsServer.create(sa, 0); |
|
346 |
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext)); |
|
347 |
httpsTestServer = HttpTestServer.of(httpsServer); |
|
348 |
httpsTestServer.addHandler(handler, "/https1"); |
|
349 |
httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1"; |
|
350 |
||
351 |
http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0)); |
|
352 |
http2TestServer.addHandler(handler, "/http2"); |
|
353 |
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2"; |
|
354 |
||
355 |
https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext)); |
|
356 |
https2TestServer.addHandler(handler, "/https2"); |
|
357 |
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2"; |
|
358 |
||
359 |
httpTestServer.start(); |
|
360 |
httpsTestServer.start(); |
|
361 |
http2TestServer.start(); |
|
362 |
https2TestServer.start(); |
|
363 |
} |
|
364 |
||
365 |
@AfterTest |
|
366 |
public void teardown() throws Exception { |
|
367 |
httpTestServer.stop(); |
|
368 |
httpsTestServer.stop(); |
|
369 |
http2TestServer.stop(); |
|
370 |
https2TestServer.stop(); |
|
371 |
} |
|
372 |
||
52106
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
373 |
/** A handler that returns, as its body, the exact received request URI. |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
374 |
* The header whose name is in the URI query and is set in the request is |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
375 |
* returned in the response with its name prefixed by X- |
76d526565453
8203850: java.net.http HTTP client should allow specifying Origin and Referer headers
michaelm
parents:
50681
diff
changeset
|
376 |
*/ |
50681 | 377 |
static class HttpUriStringHandler implements HttpTestHandler { |
378 |
@Override |
|
379 |
public void handle(HttpTestExchange t) throws IOException { |
|
380 |
URI uri = t.getRequestURI(); |
|
381 |
String uriString = uri.toString(); |
|
382 |
out.println("Http1UriStringHandler received, uri: " + uriString); |
|
383 |
String query = uri.getQuery(); |
|
384 |
String headerName = query.substring(query.indexOf("=")+1).trim(); |
|
385 |
try (InputStream is = t.getRequestBody(); |
|
386 |
OutputStream os = t.getResponseBody()) { |
|
387 |
is.readAllBytes(); |
|
388 |
byte[] bytes = uriString.getBytes(US_ASCII); |
|
389 |
t.getRequestHeaders().keySet().stream() |
|
390 |
.filter(headerName::equalsIgnoreCase) |
|
391 |
.forEach(h -> { |
|
392 |
for (String v : t.getRequestHeaders().get(headerName)) { |
|
393 |
t.getResponseHeaders().addHeader("X-"+h, v); |
|
394 |
} |
|
395 |
}); |
|
396 |
t.sendResponseHeaders(200, bytes.length); |
|
397 |
os.write(bytes); |
|
398 |
} |
|
399 |
} |
|
400 |
} |
|
401 |
} |