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/PostThruProxy.java
41161 views
1
/*
2
* Copyright (c) 2001, 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
import java.io.*;
25
import java.net.*;
26
import java.security.KeyStore;
27
import javax.net.*;
28
import javax.net.ssl.*;
29
30
import jdk.test.lib.net.URIBuilder;
31
32
/*
33
* @test
34
* @bug 4423074
35
* @modules java.base/sun.net.www
36
* @summary This test case is written to test the https POST through a proxy.
37
* There is no proxy authentication done. It includes a simple server
38
* that serves http POST method requests in secure channel, and a client
39
* that makes https POST request through a proxy.
40
* @library /test/lib
41
* @compile OriginServer.java ProxyTunnelServer.java
42
* @run main/othervm PostThruProxy
43
*/
44
public class PostThruProxy {
45
46
private static final String TEST_SRC = System.getProperty("test.src", ".");
47
private static final int TIMEOUT = 30000;
48
49
/*
50
* Where do we find the keystores?
51
*/
52
static String pathToStores = "../../../../../../javax/net/ssl/etc";
53
static String keyStoreFile = "keystore";
54
static String trustStoreFile = "truststore";
55
static String passwd = "passphrase";
56
57
private static int serverPort = 0;
58
private static ProxyTunnelServer pserver;
59
private static TestServer server;
60
static final String RESPONSE_MSG = "Https POST thru proxy is successful";
61
62
/*
63
* The TestServer implements a OriginServer that
64
* processes HTTP requests and responses.
65
*/
66
static class TestServer extends OriginServer {
67
public TestServer(ServerSocket ss) throws Exception {
68
super(ss);
69
}
70
71
/*
72
* Returns an array of bytes containing the bytes for
73
* the data sent in the response.
74
*
75
* @return bytes for the data in the response
76
*/
77
public byte[] getBytes() {
78
return RESPONSE_MSG.getBytes();
79
}
80
}
81
82
/*
83
* Main method to create the server and client
84
*/
85
public static void main(String args[]) throws Exception {
86
87
String keyFilename = TEST_SRC + "/" + pathToStores + "/" + keyStoreFile;
88
String trustFilename = TEST_SRC + "/" + pathToStores + "/"
89
+ trustStoreFile;
90
91
System.setProperty("javax.net.ssl.keyStore", keyFilename);
92
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
93
System.setProperty("javax.net.ssl.trustStore", trustFilename);
94
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
95
96
InetAddress loopback = InetAddress.getLoopbackAddress();
97
boolean useSSL = true;
98
/*
99
* setup the server
100
*/
101
try {
102
ServerSocketFactory ssf = getServerSocketFactory(useSSL);
103
ServerSocket ss = ssf.createServerSocket(serverPort, 0, loopback);
104
ss.setSoTimeout(TIMEOUT); // 30 seconds
105
serverPort = ss.getLocalPort();
106
server = new TestServer(ss);
107
System.out.println("Server started at: " + ss);
108
} catch (Exception e) {
109
System.out.println("Server side failed:" +
110
e.getMessage());
111
throw e;
112
}
113
// trigger the client
114
try {
115
doClientSide();
116
} catch (Exception e) {
117
System.out.println("Client side failed: " +
118
e.getMessage());
119
throw e;
120
}
121
long connectCount = pserver.getConnectCount();
122
if (connectCount == 0) {
123
throw new AssertionError("Proxy was not used!");
124
} else {
125
System.out.println("Proxy CONNECT count: " + connectCount);
126
}
127
}
128
129
private static ServerSocketFactory getServerSocketFactory
130
(boolean useSSL) throws Exception {
131
if (useSSL) {
132
// set up key manager to do server authentication
133
SSLContext ctx = SSLContext.getInstance("TLS");
134
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
135
KeyStore ks = KeyStore.getInstance("JKS");
136
char[] passphrase = passwd.toCharArray();
137
138
ks.load(new FileInputStream(System.getProperty(
139
"javax.net.ssl.keyStore")), passphrase);
140
kmf.init(ks, passphrase);
141
ctx.init(kmf.getKeyManagers(), null, null);
142
143
return ctx.getServerSocketFactory();
144
} else {
145
return ServerSocketFactory.getDefault();
146
}
147
}
148
149
/*
150
* Message to be posted
151
*/
152
static String postMsg = "Testing HTTP post on a https server";
153
154
static void doClientSide() throws Exception {
155
HostnameVerifier reservedHV =
156
HttpsURLConnection.getDefaultHostnameVerifier();
157
try {
158
/*
159
* setup up a proxy
160
*/
161
SocketAddress pAddr = setupProxy();
162
163
/*
164
* we want to avoid URLspoofCheck failures in cases where the cert
165
* DN name does not match the hostname in the URL.
166
*/
167
HttpsURLConnection.setDefaultHostnameVerifier(
168
new NameVerifier());
169
URL url = URIBuilder.newBuilder()
170
.scheme("https")
171
.loopback()
172
.port(serverPort)
173
.toURL();
174
175
Proxy p = new Proxy(Proxy.Type.HTTP, pAddr);
176
System.out.println("Client connecting to: " + url);
177
System.out.println("Through proxy: " + pAddr);
178
HttpsURLConnection https = (HttpsURLConnection)url.openConnection(p);
179
https.setConnectTimeout(TIMEOUT);
180
https.setReadTimeout(TIMEOUT);
181
https.setDoOutput(true);
182
https.setRequestMethod("POST");
183
PrintStream ps = null;
184
try {
185
ps = new PrintStream(https.getOutputStream());
186
ps.println(postMsg);
187
ps.flush();
188
if (https.getResponseCode() != 200) {
189
throw new RuntimeException("test Failed");
190
}
191
ps.close();
192
193
// clear the pipe
194
BufferedReader in = new BufferedReader(
195
new InputStreamReader(
196
https.getInputStream()));
197
String inputLine;
198
boolean msgFound = false;
199
while ((inputLine = in.readLine()) != null) {
200
System.out.println("Client received: " + inputLine);
201
if (inputLine.contains(RESPONSE_MSG)) msgFound = true;
202
}
203
in.close();
204
if (!msgFound) {
205
throw new RuntimeException("POST message not found.");
206
}
207
} catch (SSLException e) {
208
if (ps != null)
209
ps.close();
210
throw e;
211
} catch (SocketTimeoutException e) {
212
System.out.println("Client can not get response in time: "
213
+ e.getMessage());
214
}
215
} finally {
216
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
217
}
218
}
219
220
static class NameVerifier implements HostnameVerifier {
221
public boolean verify(String hostname, SSLSession session) {
222
return true;
223
}
224
}
225
226
static SocketAddress setupProxy() throws IOException {
227
InetAddress loopback = InetAddress.getLoopbackAddress();
228
pserver = new ProxyTunnelServer(loopback);
229
230
// disable proxy authentication
231
pserver.needUserAuth(false);
232
pserver.start();
233
return new InetSocketAddress(loopback, pserver.getPort());
234
}
235
236
}
237
238