Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/protocol/http/TunnelThroughProxy.java
41159 views
1
/*
2
* Copyright (c) 2002, 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
* @test
26
* @bug 4620362
27
* @modules java.base/sun.net.www
28
* @build ProxyTunnelServer
29
* @run main/othervm TunnelThroughProxy
30
* @summary JSSE not returning proper exception on unknown host
31
*/
32
33
import java.net.*;
34
import java.io.*;
35
36
public class TunnelThroughProxy {
37
public static void main(String[] args) throws Exception {
38
nonexistingHostTest();
39
getLocalPortTest();
40
}
41
42
static void nonexistingHostTest() throws Exception {
43
ProxyTunnelServer proxy = setupProxy(false);
44
try {
45
URL u = new URL("https://www.nonexistent-site.com/");
46
URLConnection uc = u.openConnection();
47
InputStream is = uc.getInputStream();
48
is.close();
49
} catch (Exception e) {
50
if (!e.getMessage().matches(".*HTTP\\/.*500.*")) {
51
throw new RuntimeException(e);
52
}
53
} finally {
54
proxy.terminate();
55
}
56
}
57
58
59
static void getLocalPortTest() throws Exception {
60
ProxyTunnelServer proxy = setupProxy(true);
61
try {
62
int proxyPort = proxy.getPort();
63
InetAddress proxyAddress = proxy.getInetAddress();
64
ServerSocket server = new ServerSocket(0, 0, InetAddress.getLoopbackAddress());
65
int serverPort = server.getLocalPort();
66
67
Socket sock;
68
sock = new Socket(new Proxy(Proxy.Type.HTTP,
69
new InetSocketAddress(proxyAddress, proxyPort)));
70
InetSocketAddress dest = new InetSocketAddress(server.getInetAddress(), serverPort);
71
sock.connect(dest);
72
int localPort = sock.getLocalPort();
73
if (localPort == proxyPort)
74
throw new RuntimeException("Fail: socket has wrong local port");
75
// check that tunnel really works
76
Socket sock1 = server.accept();
77
OutputStream os = sock1.getOutputStream();
78
os.write(99);
79
os.flush();
80
if (sock.getInputStream().read() != 99)
81
throw new RuntimeException("Tunnel does not work");
82
} finally {
83
proxy.terminate();
84
}
85
}
86
87
static ProxyTunnelServer setupProxy(boolean makeTunnel) throws IOException {
88
InetAddress proxyAddress = InetAddress.getLoopbackAddress();
89
ProxyTunnelServer pserver = new ProxyTunnelServer(proxyAddress);
90
pserver.doTunnel(makeTunnel);
91
int proxyPort = pserver.getPort();
92
93
// disable proxy authentication
94
pserver.needUserAuth(false);
95
pserver.start();
96
System.out.printf("Setting https.proxyHost='%s'%n", proxyAddress.getHostAddress());
97
System.setProperty("https.proxyHost", proxyAddress.getHostAddress());
98
System.out.printf("Setting https.proxyPort='%s'%n", String.valueOf(proxyPort));
99
System.setProperty("https.proxyPort", String.valueOf(proxyPort));
100
return pserver;
101
}
102
}
103
104