Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/protocol/https/HttpsURLConnection/PostThruProxy.java
38889 views
1
/*
2
* Copyright (c) 2001, 2016, 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.testlibrary.OutputAnalyzer;
31
import jdk.testlibrary.ProcessTools;
32
33
/*
34
* @test
35
* @bug 4423074
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 /lib/testlibrary
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
59
/*
60
* The TestServer implements a OriginServer that
61
* processes HTTP requests and responses.
62
*/
63
static class TestServer extends OriginServer {
64
public TestServer(ServerSocket ss) throws Exception {
65
super(ss);
66
}
67
68
/*
69
* Returns an array of bytes containing the bytes for
70
* the data sent in the response.
71
*
72
* @return bytes for the data in the response
73
*/
74
public byte[] getBytes() {
75
return "Https POST thru proxy is successful".
76
getBytes();
77
}
78
}
79
80
/*
81
* Main method to create the server and client
82
*/
83
public static void main(String args[]) throws Exception {
84
String keyFilename = TEST_SRC + "/" + pathToStores + "/" + keyStoreFile;
85
String trustFilename = TEST_SRC + "/" + pathToStores + "/"
86
+ trustStoreFile;
87
88
System.setProperty("javax.net.ssl.keyStore", keyFilename);
89
System.setProperty("javax.net.ssl.keyStorePassword", passwd);
90
System.setProperty("javax.net.ssl.trustStore", trustFilename);
91
System.setProperty("javax.net.ssl.trustStorePassword", passwd);
92
93
boolean useSSL = true;
94
/*
95
* setup the server
96
*/
97
try {
98
ServerSocketFactory ssf = getServerSocketFactory(useSSL);
99
ServerSocket ss = ssf.createServerSocket(serverPort);
100
ss.setSoTimeout(TIMEOUT); // 30 seconds
101
serverPort = ss.getLocalPort();
102
new TestServer(ss);
103
} catch (Exception e) {
104
System.out.println("Server side failed:" +
105
e.getMessage());
106
throw e;
107
}
108
// trigger the client
109
try {
110
doClientSide();
111
} catch (Exception e) {
112
System.out.println("Client side failed: " +
113
e.getMessage());
114
throw e;
115
}
116
}
117
118
private static ServerSocketFactory getServerSocketFactory
119
(boolean useSSL) throws Exception {
120
if (useSSL) {
121
// set up key manager to do server authentication
122
SSLContext ctx = SSLContext.getInstance("TLS");
123
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
124
KeyStore ks = KeyStore.getInstance("JKS");
125
char[] passphrase = passwd.toCharArray();
126
127
ks.load(new FileInputStream(System.getProperty(
128
"javax.net.ssl.keyStore")), passphrase);
129
kmf.init(ks, passphrase);
130
ctx.init(kmf.getKeyManagers(), null, null);
131
132
return ctx.getServerSocketFactory();
133
} else {
134
return ServerSocketFactory.getDefault();
135
}
136
}
137
138
/*
139
* Message to be posted
140
*/
141
static String postMsg = "Testing HTTP post on a https server";
142
143
static void doClientSide() throws Exception {
144
HostnameVerifier reservedHV =
145
HttpsURLConnection.getDefaultHostnameVerifier();
146
try {
147
/*
148
* setup up a proxy
149
*/
150
SocketAddress pAddr = setupProxy();
151
152
/*
153
* we want to avoid URLspoofCheck failures in cases where the cert
154
* DN name does not match the hostname in the URL.
155
*/
156
HttpsURLConnection.setDefaultHostnameVerifier(
157
new NameVerifier());
158
URL url = new URL("https://" + getHostname() +":" + serverPort);
159
160
Proxy p = new Proxy(Proxy.Type.HTTP, pAddr);
161
HttpsURLConnection https = (HttpsURLConnection)url.openConnection(p);
162
https.setConnectTimeout(TIMEOUT);
163
https.setReadTimeout(TIMEOUT);
164
https.setDoOutput(true);
165
https.setRequestMethod("POST");
166
PrintStream ps = null;
167
try {
168
ps = new PrintStream(https.getOutputStream());
169
ps.println(postMsg);
170
ps.flush();
171
if (https.getResponseCode() != 200) {
172
throw new RuntimeException("test Failed");
173
}
174
ps.close();
175
176
// clear the pipe
177
BufferedReader in = new BufferedReader(
178
new InputStreamReader(
179
https.getInputStream()));
180
String inputLine;
181
while ((inputLine = in.readLine()) != null)
182
System.out.println("Client received: " + inputLine);
183
in.close();
184
} catch (SSLException e) {
185
if (ps != null)
186
ps.close();
187
throw e;
188
} catch (SocketTimeoutException e) {
189
System.out.println("Client can not get response in time: "
190
+ e.getMessage());
191
}
192
} finally {
193
HttpsURLConnection.setDefaultHostnameVerifier(reservedHV);
194
}
195
}
196
197
static class NameVerifier implements HostnameVerifier {
198
public boolean verify(String hostname, SSLSession session) {
199
return true;
200
}
201
}
202
203
static SocketAddress setupProxy() throws IOException {
204
ProxyTunnelServer pserver = new ProxyTunnelServer();
205
206
// disable proxy authentication
207
pserver.needUserAuth(false);
208
pserver.start();
209
return new InetSocketAddress("localhost", pserver.getPort());
210
}
211
212
private static String getHostname() {
213
try {
214
OutputAnalyzer oa = ProcessTools.executeCommand("hostname");
215
return oa.getOutput().trim();
216
} catch (Throwable e) {
217
throw new RuntimeException("Get hostname failed.", e);
218
}
219
}
220
}
221
222