Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/EscapedOctetsInURI.java
41152 views
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 Preserve URI component escaped octets when converting to HTTP headers
27
* @bug 8198716
28
* @modules java.base/sun.net.www.http
29
* java.net.http/jdk.internal.net.http.common
30
* java.net.http/jdk.internal.net.http.frame
31
* java.net.http/jdk.internal.net.http.hpack
32
* java.logging
33
* jdk.httpserver
34
* @library /test/lib http2/server
35
* @build Http2TestServer
36
* @build jdk.test.lib.net.SimpleSSLContext
37
* @run testng/othervm
38
* -Djdk.httpclient.HttpClient.log=reqeusts,headers
39
* EscapedOctetsInURI
40
*/
41
42
import com.sun.net.httpserver.HttpExchange;
43
import com.sun.net.httpserver.HttpHandler;
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.test.lib.net.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.out;
67
import static java.nio.charset.StandardCharsets.US_ASCII;
68
import static java.net.http.HttpClient.Builder.NO_PROXY;
69
import static org.testng.Assert.assertEquals;
70
71
public class EscapedOctetsInURI {
72
73
SSLContext sslContext;
74
HttpServer httpTestServer; // HTTP/1.1 [ 4 servers ]
75
HttpsServer httpsTestServer; // HTTPS/1.1
76
Http2TestServer http2TestServer; // HTTP/2 ( h2c )
77
Http2TestServer https2TestServer; // HTTP/2 ( h2 )
78
String httpURI;
79
String httpsURI;
80
String http2URI;
81
String https2URI;
82
83
static final String[][] pathsAndQueryStrings = new String[][] {
84
// partial-path URI query
85
{ "/001/noSpace", "?noQuotedOctets" },
86
{ "/002/noSpace", "?name=chegar,address=Dublin%20Ireland", },
87
{ "/003/noSpace", "?target=http%3A%2F%2Fwww.w3.org%2Fns%2Foa%23hasBody" },
88
89
{ "/010/with%20space", "?noQuotedOctets" },
90
{ "/011/with%20space", "?name=chegar,address=Dublin%20Ireland" },
91
{ "/012/with%20space", "?target=http%3A%2F%2Fwww.w3.org%2Fns%2Foa%23hasBody" },
92
};
93
94
@DataProvider(name = "variants")
95
public Object[][] variants() {
96
List<Object[]> list = new ArrayList<>();
97
98
for (boolean sameClient : new boolean[] { false, true }) {
99
Arrays.asList(pathsAndQueryStrings).stream()
100
.map(e -> new Object[] {httpURI + e[0] + e[1], sameClient})
101
.forEach(list::add);
102
Arrays.asList(pathsAndQueryStrings).stream()
103
.map(e -> new Object[] {httpsURI + e[0] + e[1], sameClient})
104
.forEach(list::add);
105
Arrays.asList(pathsAndQueryStrings).stream()
106
.map(e -> new Object[] {http2URI + e[0] + e[1], sameClient})
107
.forEach(list::add);
108
Arrays.asList(pathsAndQueryStrings).stream()
109
.map(e -> new Object[] {https2URI + e[0] + e[1], sameClient})
110
.forEach(list::add);
111
}
112
return list.stream().toArray(Object[][]::new);
113
}
114
115
static final int ITERATION_COUNT = 3; // checks upgrade and re-use
116
117
@Test(dataProvider = "variants")
118
void test(String uriString, boolean sameClient) throws Exception {
119
System.out.println("\n--- Starting ");
120
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 exact escaped request URI
143
URI retrievedURI = URI.create(resp.body());
144
assertEquals(retrievedURI.getRawPath(), uri.getRawPath());
145
assertEquals(retrievedURI.getRawQuery(), uri.getRawQuery());
146
}
147
}
148
149
@Test(dataProvider = "variants")
150
void testAsync(String uriString, boolean sameClient) {
151
System.out.println("\n--- Starting ");
152
URI uri = URI.create(uriString);
153
154
HttpClient client = null;
155
for (int i=0; i< ITERATION_COUNT; i++) {
156
if (!sameClient || client == null)
157
client = HttpClient.newBuilder()
158
.proxy(NO_PROXY)
159
.sslContext(sslContext)
160
.build();
161
162
HttpRequest request = HttpRequest.newBuilder(uri).build();
163
164
client.sendAsync(request, BodyHandlers.ofString())
165
.thenApply(response -> {
166
out.println("Got response: " + response);
167
out.println("Got body: " + response.body());
168
assertEquals(response.statusCode(), 200);
169
return response.body(); })
170
.thenApply(body -> URI.create(body))
171
.thenAccept(retrievedURI -> {
172
// the body should contain the exact escaped request URI
173
assertEquals(retrievedURI.getRawPath(), uri.getRawPath());
174
assertEquals(retrievedURI.getRawQuery(), uri.getRawQuery()); })
175
.join();
176
}
177
}
178
179
static String serverAuthority(HttpServer server) {
180
return InetAddress.getLoopbackAddress().getHostName() + ":"
181
+ server.getAddress().getPort();
182
}
183
184
@BeforeTest
185
public void setup() throws Exception {
186
sslContext = new SimpleSSLContext().get();
187
if (sslContext == null)
188
throw new AssertionError("Unexpected null sslContext");
189
190
InetSocketAddress sa = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0);
191
httpTestServer = HttpServer.create(sa, 0);
192
httpTestServer.createContext("/http1", new Http1ASCIIUriStringHandler());
193
httpURI = "http://" + serverAuthority(httpTestServer) + "/http1";
194
195
httpsTestServer = HttpsServer.create(sa, 0);
196
httpsTestServer.setHttpsConfigurator(new HttpsConfigurator(sslContext));
197
httpsTestServer.createContext("/https1", new Http1ASCIIUriStringHandler());
198
httpsURI = "https://" + serverAuthority(httpsTestServer) + "/https1";
199
200
http2TestServer = new Http2TestServer("localhost", false, 0);
201
http2TestServer.addHandler(new HttpASCIIUriStringHandler(), "/http2");
202
http2URI = "http://" + http2TestServer.serverAuthority() + "/http2";
203
204
https2TestServer = new Http2TestServer("localhost", true, sslContext);
205
https2TestServer.addHandler(new HttpASCIIUriStringHandler(), "/https2");
206
https2URI = "https://" + https2TestServer.serverAuthority() + "/https2";
207
208
httpTestServer.start();
209
httpsTestServer.start();
210
http2TestServer.start();
211
https2TestServer.start();
212
}
213
214
@AfterTest
215
public void teardown() throws Exception {
216
httpTestServer.stop(0);
217
httpsTestServer.stop(0);
218
http2TestServer.stop();
219
https2TestServer.stop();
220
}
221
222
/** A handler that returns as its body the exact escaped request URI. */
223
static class Http1ASCIIUriStringHandler implements HttpHandler {
224
@Override
225
public void handle(HttpExchange t) throws IOException {
226
String asciiUriString = t.getRequestURI().toASCIIString();
227
out.println("Http1ASCIIUriString received, asciiUriString: " + asciiUriString);
228
try (InputStream is = t.getRequestBody();
229
OutputStream os = t.getResponseBody()) {
230
is.readAllBytes();
231
byte[] bytes = asciiUriString.getBytes(US_ASCII);
232
t.sendResponseHeaders(200, bytes.length);
233
os.write(bytes);
234
}
235
}
236
}
237
238
/** A handler that returns as its body the exact escaped request URI. */
239
static class HttpASCIIUriStringHandler implements Http2Handler {
240
@Override
241
public void handle(Http2TestExchange t) throws IOException {
242
String asciiUriString = t.getRequestURI().toASCIIString();
243
out.println("Http2ASCIIUriString received, asciiUriString: " + asciiUriString);
244
try (InputStream is = t.getRequestBody();
245
OutputStream os = t.getResponseBody()) {
246
is.readAllBytes();
247
byte[] bytes = asciiUriString.getBytes(US_ASCII);
248
t.sendResponseHeaders(200, bytes.length);
249
os.write(bytes);
250
}
251
}
252
}
253
}
254
255