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