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/HttpsProxyStackOverflow.java
41161 views
1
/*
2
* Copyright (c) 2011, 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 6670868
27
* @summary StackOverFlow with bad authenticated Proxy tunnels
28
* @run main/othervm HttpsProxyStackOverflow
29
*
30
* No way to reserve default Authenticator, need to run in othervm mode.
31
*/
32
33
import java.io.IOException;
34
import java.io.InputStream;
35
import java.net.Authenticator;
36
import java.net.Proxy;
37
import java.net.InetAddress;
38
import java.net.InetSocketAddress;
39
import java.net.PasswordAuthentication;
40
import java.net.ServerSocket;
41
import java.net.Socket;
42
import java.net.URL;
43
import javax.net.ssl.HttpsURLConnection;
44
45
public class HttpsProxyStackOverflow {
46
47
public static void main(String[] args) throws IOException {
48
BadAuthProxyServer server = startServer();
49
doClient(server);
50
}
51
52
static void doClient(BadAuthProxyServer server) throws IOException {
53
// url doesn't matter since we will never make the connection
54
URL url = new URL("https://anythingwilldo/");
55
InetAddress loopback = InetAddress.getLoopbackAddress();
56
String loopbackAddress = loopback.getHostAddress();
57
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(
58
new Proxy(Proxy.Type.HTTP,
59
InetSocketAddress.createUnresolved(loopbackAddress, server.getPort())));
60
try (InputStream is = conn.getInputStream()) {
61
} catch(IOException unused) {
62
// no real server, IOException is expected.
63
// failure if StackOverflowError
64
} finally {
65
server.done();
66
}
67
}
68
69
static BadAuthProxyServer startServer() throws IOException {
70
Authenticator.setDefault(new Authenticator() {
71
@Override
72
protected PasswordAuthentication getPasswordAuthentication() {
73
return new PasswordAuthentication("xyz", "xyz".toCharArray());
74
}
75
});
76
InetAddress loopback = InetAddress.getLoopbackAddress();
77
InetSocketAddress address = new InetSocketAddress(loopback, 0);
78
ServerSocket ss = new ServerSocket();
79
ss.bind(address);
80
BadAuthProxyServer server = new BadAuthProxyServer(ss);
81
Thread serverThread = new Thread(server);
82
serverThread.start();
83
return server;
84
}
85
86
static class BadAuthProxyServer implements Runnable {
87
private ServerSocket ss;
88
private boolean done;
89
90
BadAuthProxyServer(ServerSocket ss) { this.ss = ss; }
91
92
public void run() {
93
try {
94
while (!done) {
95
Socket s = ss.accept();
96
s.getOutputStream().write(
97
("HTTP/1.1 407\nProxy-Authenticate:Basic " +
98
"realm=\"WallyWorld\"\n\n").getBytes("US-ASCII"));
99
100
s.close();
101
102
s = ss.accept();
103
s.close();
104
}
105
} catch (IOException e) {
106
// Ignore IOException when the main thread calls done
107
} finally {
108
try { ss.close(); } catch (IOException e) {}
109
}
110
}
111
112
int getPort() {
113
return ss.getLocalPort();
114
}
115
116
void done() {
117
try { ss.close(); } catch (IOException e) {}
118
done = true;
119
}
120
}
121
}
122
123