Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/https/HttpsURLConnection/B6216082.java
41161 views
1
/*
2
* Copyright (c) 2005, 2019, 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
// SunJSSE does not support dynamic system properties, no way to re-use
26
// system properties in samevm/agentvm mode.
27
//
28
29
/*
30
* @test
31
* @bug 6216082
32
* @summary Redirect problem with HttpsURLConnection using a proxy
33
* @modules java.base/sun.net.www
34
* @library .. /test/lib
35
* @build jdk.test.lib.NetworkConfiguration
36
* jdk.test.lib.Platform
37
* HttpCallback TestHttpsServer ClosedChannelList
38
* HttpTransaction TunnelProxy
39
* @key intermittent
40
* @run main/othervm B6216082
41
*/
42
43
import java.io.*;
44
import java.net.*;
45
import javax.net.ssl.*;
46
import java.util.*;
47
48
import jdk.test.lib.NetworkConfiguration;
49
50
public class B6216082 {
51
static SimpleHttpTransaction httpTrans;
52
static TestHttpsServer server;
53
static TunnelProxy proxy;
54
55
// it seems there's no proxy ever if a url points to 'localhost',
56
// even if proxy related properties are set. so we need to bind
57
// our simple http proxy and http server to a non-loopback address
58
static InetAddress firstNonLoAddress = null;
59
60
public static void main(String[] args) throws Exception {
61
HostnameVerifier reservedHV =
62
HttpsURLConnection.getDefaultHostnameVerifier();
63
try {
64
// XXX workaround for CNFE
65
Class.forName("java.nio.channels.ClosedByInterruptException");
66
if (!setupEnv()) {
67
return;
68
}
69
70
startHttpServer();
71
72
// https.proxyPort can only be set after the TunnelProxy has been
73
// created as it will use an ephemeral port.
74
System.setProperty("https.proxyPort",
75
(new Integer(proxy.getLocalPort())).toString() );
76
77
makeHttpCall();
78
79
if (httpTrans.hasBadRequest) {
80
throw new RuntimeException("Test failed : bad http request");
81
}
82
} finally {
83
if (proxy != null) {
84
proxy.terminate();
85
}
86
if (server != null) {
87
server.terminate();
88
}
89
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
90
}
91
}
92
93
/*
94
* Where do we find the keystores for ssl?
95
*/
96
static String pathToStores = "../../../../../../javax/net/ssl/etc";
97
static String keyStoreFile = "keystore";
98
static String trustStoreFile = "truststore";
99
static String passwd = "passphrase";
100
public static boolean setupEnv() throws Exception {
101
firstNonLoAddress = getNonLoAddress();
102
if (firstNonLoAddress == null) {
103
System.err.println("The test needs at least one non-loopback address to run. Quit now.");
104
return false;
105
}
106
System.out.println(firstNonLoAddress.getHostAddress());
107
// will use proxy
108
System.setProperty( "https.proxyHost", firstNonLoAddress.getHostAddress());
109
110
// setup properties to do ssl
111
String keyFilename = System.getProperty("test.src", "./") + "/" +
112
pathToStores + "/" + keyStoreFile;
113
String trustFilename = System.getProperty("test.src", "./") + "/" +
114
pathToStores + "/" + trustStoreFile;
115
116
System.setProperty("javax.net.ssl.keyStore", keyFilename);
117
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
118
System.setProperty("javax.net.ssl.trustStore", trustFilename);
119
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
120
HttpsURLConnection.setDefaultHostnameVerifier(new NameVerifier());
121
return true;
122
}
123
124
public static InetAddress getNonLoAddress() throws Exception {
125
InetAddress lh = InetAddress.getByName("localhost");
126
NetworkInterface loNIC = NetworkInterface.getByInetAddress(lh);
127
128
NetworkConfiguration nc = NetworkConfiguration.probe();
129
Optional<InetAddress> oaddr = nc.interfaces()
130
.filter(nif -> !nif.getName().equalsIgnoreCase(loNIC.getName()))
131
.flatMap(nif -> nc.addresses(nif))
132
.filter(a -> !a.isLoopbackAddress())
133
.findFirst();
134
135
return oaddr.orElseGet(() -> null);
136
}
137
138
public static void startHttpServer() throws IOException {
139
// Both the https server and the proxy let the
140
// system pick up an ephemeral port.
141
httpTrans = new SimpleHttpTransaction();
142
server = new TestHttpsServer(httpTrans, 1, 10, firstNonLoAddress, 0);
143
proxy = new TunnelProxy(1, 10, firstNonLoAddress, 0);
144
}
145
146
public static void makeHttpCall() throws Exception {
147
System.out.println("https server listen on: " + server.getLocalPort());
148
System.out.println("https proxy listen on: " + proxy.getLocalPort());
149
URL url = new URL("https" , firstNonLoAddress.getHostAddress(),
150
server.getLocalPort(), "/");
151
HttpURLConnection uc = (HttpURLConnection)url.openConnection();
152
System.out.println(uc.getResponseCode());
153
uc.disconnect();
154
}
155
156
static class NameVerifier implements HostnameVerifier {
157
public boolean verify(String hostname, SSLSession session) {
158
return true;
159
}
160
}
161
}
162
163
class SimpleHttpTransaction implements HttpCallback {
164
public boolean hasBadRequest = false;
165
166
/*
167
* Our http server which simply redirect first call
168
*/
169
public void request(HttpTransaction trans) {
170
try {
171
String path = trans.getRequestURI().getPath();
172
if (path.equals("/")) {
173
// the first call, redirect it
174
String location = "/redirect";
175
trans.addResponseHeader("Location", location);
176
trans.sendResponse(302, "Moved Temporarily");
177
} else {
178
// if the bug exsits, it'll send 2 GET commands
179
// check 2nd GET here
180
String duplicatedGet = trans.getRequestHeader(null);
181
if (duplicatedGet != null &&
182
duplicatedGet.toUpperCase().indexOf("GET") >= 0) {
183
trans.sendResponse(400, "Bad Request");
184
hasBadRequest = true;
185
} else {
186
trans.sendResponse(200, "OK");
187
}
188
}
189
} catch (Exception e) {
190
throw new RuntimeException(e);
191
}
192
}
193
}
194
195