49944
|
1 |
/*
|
|
2 |
* Copyright (c) 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.
|
|
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 non-US-ASCII chars are replaced with a sequence of
|
|
27 |
* escaped octets that represent that char in the UTF-8 character set.
|
|
28 |
* @bug 8201238
|
|
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
|
|
35 |
* @library /lib/testlibrary http2/server
|
|
36 |
* @build Http2TestServer
|
|
37 |
* @build jdk.testlibrary.SimpleSSLContext
|
|
38 |
* @compile -encoding utf-8 NonAsciiCharsInURI.java
|
|
39 |
* @run testng/othervm
|
|
40 |
* -Djdk.httpclient.HttpClient.log=reqeusts,headers
|
|
41 |
* NonAsciiCharsInURI
|
|
42 |
*/
|
|
43 |
|
|
44 |
import com.sun.net.httpserver.HttpServer;
|
|
45 |
import com.sun.net.httpserver.HttpsConfigurator;
|
|
46 |
import com.sun.net.httpserver.HttpsServer;
|
|
47 |
import java.io.IOException;
|
|
48 |
import java.io.InputStream;
|
|
49 |
import java.io.OutputStream;
|
|
50 |
import java.net.InetAddress;
|
|
51 |
import java.net.InetSocketAddress;
|
|
52 |
import java.net.URI;
|
|
53 |
import javax.net.ssl.SSLContext;
|
|
54 |
import java.net.http.HttpClient;
|
|
55 |
import java.net.http.HttpRequest;
|
|
56 |
import java.net.http.HttpResponse;
|
|
57 |
import java.net.http.HttpResponse.BodyHandlers;
|
|
58 |
import java.util.ArrayList;
|
|
59 |
import java.util.Arrays;
|
|
60 |
import java.util.List;
|
|
61 |
import jdk.testlibrary.SimpleSSLContext;
|
|
62 |
import org.testng.annotations.AfterTest;
|
|
63 |
import org.testng.annotations.BeforeTest;
|
|
64 |
import org.testng.annotations.DataProvider;
|
|
65 |
import org.testng.annotations.Test;
|
|
66 |
import static java.lang.System.err;
|
|
67 |
import static java.lang.System.out;
|
|
68 |
import static java.nio.charset.StandardCharsets.US_ASCII;
|
|
69 |
import static java.net.http.HttpClient.Builder.NO_PROXY;
|
|
70 |
import static org.testng.Assert.assertEquals;
|
|
71 |
|
|
72 |
public class NonAsciiCharsInURI implements HttpServerAdapters {
|
|
73 |
|
|
74 |
SSLContext sslContext;
|
|
75 |
HttpTestServer httpTestServer; // HTTP/1.1 [ 4 servers ]
|
|
76 |
HttpTestServer httpsTestServer; // HTTPS/1.1
|
|
77 |
HttpTestServer http2TestServer; // HTTP/2 ( h2c )
|
|
78 |
HttpTestServer https2TestServer; // HTTP/2 ( h2 )
|
|
79 |
String httpURI;
|
|
80 |
String httpsURI;
|
|
81 |
String http2URI;
|
|
82 |
String https2URI;
|
|
83 |
|
|
84 |
// € = '\u20AC' => 0xE20x820xAC
|
|
85 |
static final String[][] pathsAndQueryStrings = new String[][] {
|
|
86 |
// partial-path
|
|
87 |
{ "/001/plain" },
|
|
88 |
{ "/002/plain?plainQuery" },
|
|
89 |
{ "/003/withEuroSymbol/€" },
|
|
90 |
{ "/004/withEuroSymbol/€?euroSymbol=€" },
|
|
91 |
{ "/005/wiki/エリザベス1世_(イングランド女王)" },
|
|
92 |
{ "/006/x?url=https://ja.wikipedia.org/wiki/エリザベス1世_(イングランド女王)" },
|
|
93 |
};
|
|
94 |
|
|
95 |
@DataProvider(name = "variants")
|
|
96 |
public Object[][] variants() {
|
|
97 |
List<Object[]> list = new ArrayList<>();
|
|
98 |
|
|
99 |
for (boolean sameClient : new boolean[] { false, true }) {
|
|
100 |
Arrays.asList(pathsAndQueryStrings).stream()
|
|
101 |
.map(e -> new Object[] {httpURI + e[0], sameClient})
|
|
102 |
.forEach(list::add);
|
|
103 |
Arrays.asList(pathsAndQueryStrings).stream()
|
|
104 |
.map(e -> new Object[] {httpsURI + e[0], sameClient})
|
|
105 |
.forEach(list::add);
|
|
106 |
Arrays.asList(pathsAndQueryStrings).stream()
|
|
107 |
.map(e -> new Object[] {http2URI + e[0], sameClient})
|
|
108 |
.forEach(list::add);
|
|
109 |
Arrays.asList(pathsAndQueryStrings).stream()
|
|
110 |
.map(e -> new Object[] {https2URI + e[0], sameClient})
|
|
111 |
.forEach(list::add);
|
|
112 |
}
|
|
113 |
return list.stream().toArray(Object[][]::new);
|
|
114 |
}
|
|
115 |
|
|
116 |
static final int ITERATION_COUNT = 3; // checks upgrade and re-use
|
|
117 |
|
|
118 |
@Test(dataProvider = "variants")
|
|
119 |
void test(String uriString, boolean sameClient) throws Exception {
|
|
120 |
out.println("\n--- Starting ");
|
|
121 |
// The single-argument factory requires any illegal characters in its
|
|
122 |
// argument to be quoted and preserves any escaped octets and other
|
|
123 |
// characters that are present.
|
|
124 |
URI uri = URI.create(uriString);
|
|
125 |
|
|
126 |
HttpClient client = null;
|
|
127 |
for (int i=0; i< ITERATION_COUNT; i++) {
|
|
128 |
if (!sameClient || client == null)
|
|
129 |
client = HttpClient.newBuilder()
|
|
130 |
.proxy(NO_PROXY)
|
|
131 |
.sslContext(sslContext)
|
|
132 |
.build();
|
|
133 |
|
|
134 |
HttpRequest request = HttpRequest.newBuilder(uri).build();
|
|
135 |
HttpResponse<String> resp = client.send(request, BodyHandlers.ofString());
|
|
136 |
|
|
137 |
out.println("Got response: " + resp);
|
|
138 |
out.println("Got body: " + resp.body());
|
|
139 |
assertEquals(resp.statusCode(), 200,
|
|
140 |
"Expected 200, got:" + resp.statusCode());
|
|
141 |
|
|
142 |
// the response body should contain the toASCIIString
|
|
143 |
// representation of the URI
|
|
144 |
String expectedURIString = uri.toASCIIString();
|
|
145 |
if (!expectedURIString.contains(resp.body())) {
|
|
146 |
err.println("Test failed: " + resp);
|
|
147 |
throw new AssertionError(expectedURIString +
|
|
148 |
" does not contain '" + resp.body() + "'");
|
|
149 |
} else {
|
|
150 |
out.println("Found expected " + resp.body() + " in " + expectedURIString);
|
|
151 |
}
|
|
152 |
}
|
|
153 |
}
|
|
154 |
|
|
155 |
@Test(dataProvider = "variants")
|
|
156 |
void testAsync(String uriString, boolean sameClient) {
|
|
157 |
out.println("\n--- Starting ");
|
|
158 |
URI uri = URI.create(uriString);
|
|
159 |
|
|
160 |
HttpClient client = null;
|
|
161 |
for (int i=0; i< ITERATION_COUNT; i++) {
|
|
162 |
if (!sameClient || client == null)
|
|
163 |
client = HttpClient.newBuilder()
|
|
164 |
.proxy(NO_PROXY)
|
|
165 |
.sslContext(sslContext)
|
|
166 |
.build();
|
|
167 |
|
|
168 |
HttpRequest request = HttpRequest.newBuilder(uri).build();
|
|
169 |
|
|
170 |
client.sendAsync(request, BodyHandlers.ofString())
|
|
171 |
.thenApply(response -> {
|
|
172 |
out.println("Got response: " + response);
|
|
173 |
out.println("Got body: " + response.body());
|
|
174 |
assertEquals(response.statusCode(), 200);
|
|
175 |
return response.body(); })
|
|
176 |
.thenAccept(body -> {
|
|
177 |
// the response body should contain the toASCIIString
|
|
178 |
// representation of the URI
|
|
179 |
String expectedURIString = uri.toASCIIString();
|
|
180 |
if (!expectedURIString.contains(body)) {
|
|
181 |
err.println("Test failed: " + body);
|
|
182 |
throw new AssertionError(expectedURIString +
|
|
183 |
" does not contain '" + body + "'");
|
|
184 |
} else {
|
|
185 |
out.println("Found expected " + body + " in "
|
|
186 |
+ expectedURIString);
|
|
187 |
} })
|
|
188 |
.join();
|
|
189 |
}
|
|
190 |
}
|
|
191 |
|
|
192 |
static String serverAuthority(HttpTestServer server) {
|
|
193 |
return InetAddress.getLoopbackAddress().getHostName() + ":"
|
|
194 |
+ server.getAddress().getPort();
|
|
195 |
}
|
|
196 |
|
|
197 |
@BeforeTest
|
|
198 |
public void setup() throws Exception {
|
|
199 |
sslContext = new SimpleSSLContext().get();
|
|
200 |
if (sslContext == null)
|
|
201 |
throw new AssertionError("Unexpected null sslContext");
|
|
202 |
|
|
203 |
HttpTestHandler handler = new HttpUriStringHandler();
|
|
204 |
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
|
|
205 |
httpTestServer = HttpTestServer.of(HttpServer.create(sa, 0));
|
|
206 |
httpTestServer.addHandler(handler, "/http1");
|
|
207 |
httpURI = "http://" + serverAuthority(httpTestServer) + "/http1";
|
|
208 |
|
|
209 |
HttpsServer httpsServer = HttpsServer.create(sa, 0);
|
|
210 |
httpsServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
|
|
211 |
httpsTestServer = HttpTestServer.of(httpsServer);
|
|
212 |
httpsTestServer.addHandler(handler, "/https1");
|
|
213 |
httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1";
|
|
214 |
|
|
215 |
http2TestServer = HttpTestServer.of(new Http2TestServer("localhost", false, 0));
|
|
216 |
http2TestServer.addHandler(handler, "/http2");
|
|
217 |
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2";
|
|
218 |
|
50681
|
219 |
https2TestServer = HttpTestServer.of(new Http2TestServer("localhost", true, sslContext));
|
49944
|
220 |
https2TestServer.addHandler(handler, "/https2");
|
|
221 |
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2";
|
|
222 |
|
|
223 |
httpTestServer.start();
|
|
224 |
httpsTestServer.start();
|
|
225 |
http2TestServer.start();
|
|
226 |
https2TestServer.start();
|
|
227 |
}
|
|
228 |
|
|
229 |
@AfterTest
|
|
230 |
public void teardown() throws Exception {
|
|
231 |
httpTestServer.stop();
|
|
232 |
httpsTestServer.stop();
|
|
233 |
http2TestServer.stop();
|
|
234 |
https2TestServer.stop();
|
|
235 |
}
|
|
236 |
|
|
237 |
/** A handler that returns, as its body, the exact received request URI. */
|
|
238 |
static class HttpUriStringHandler implements HttpTestHandler {
|
|
239 |
@Override
|
|
240 |
public void handle(HttpTestExchange t) throws IOException {
|
|
241 |
String uri = t.getRequestURI().toString();
|
|
242 |
out.println("Http1UriStringHandler received, uri: " + uri);
|
|
243 |
try (InputStream is = t.getRequestBody();
|
|
244 |
OutputStream os = t.getResponseBody()) {
|
|
245 |
is.readAllBytes();
|
|
246 |
byte[] bytes = uri.getBytes(US_ASCII);
|
|
247 |
t.sendResponseHeaders(200, bytes.length);
|
|
248 |
os.write(bytes);
|
|
249 |
}
|
|
250 |
}
|
|
251 |
}
|
|
252 |
}
|