Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/ProxySelector/LoopbackAddresses.java
41149 views
1
/*
2
* Copyright (c) 2003, 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
/* @test
25
* @bug 4924226
26
* @key intermittent
27
* @summary PIT: Can no launch jnlp application via 127.0.0.1 address on the web server.
28
* This test might fail intermittently as it needs a server that
29
* binds to the wildcard address.
30
* @modules java.base/sun.net.www
31
* @library ../../../sun/net/www/httptest/ /test/lib
32
* @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback
33
* @compile LoopbackAddresses.java
34
* @run main/othervm LoopbackAddresses
35
*/
36
37
import java.net.*;
38
import java.io.*;
39
import jdk.test.lib.net.URIBuilder;
40
41
/**
42
* Our default proxy selector should bypass localhost and loopback
43
* addresses when selecting proxies. This is the existing behaviour.
44
*/
45
46
public class LoopbackAddresses implements HttpCallback {
47
static TestHttpServer server;
48
49
public void request (HttpTransaction req) {
50
req.setResponseEntityBody ("Hello .");
51
try {
52
req.sendResponse (200, "Ok");
53
req.orderlyClose();
54
} catch (IOException e) {
55
}
56
}
57
58
public static void main(String[] args) {
59
try {
60
InetAddress loopback = InetAddress.getLoopbackAddress();
61
62
// This server needs to bind to the wildcard address as we want it
63
// to answer both for the loopback and "localhost".
64
// Though "localhost" usually point to the loopback there is no
65
// hard guarantee.
66
server = new TestHttpServer (new LoopbackAddresses(), 1, 10, 0);
67
ProxyServer pserver = new ProxyServer(InetAddress.getByName("localhost"), server.getLocalPort());
68
// start proxy server
69
new Thread(pserver).start();
70
71
System.setProperty("http.proxyHost", loopback.getHostAddress());
72
System.setProperty("http.proxyPort", pserver.getPort()+"");
73
74
URL url = new URL("http://localhost:"+server.getLocalPort());
75
76
try {
77
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
78
int respCode = urlc.getResponseCode();
79
urlc.disconnect();
80
} catch (IOException ioex) {
81
throw new RuntimeException("direct connection should succeed :"+ioex.getMessage());
82
}
83
84
try {
85
url = URIBuilder.newBuilder()
86
.scheme("http")
87
.host(loopback.getHostAddress())
88
.port(server.getLocalPort())
89
.toURL();
90
HttpURLConnection urlc = (HttpURLConnection)url.openConnection ();
91
int respCode = urlc.getResponseCode();
92
urlc.disconnect();
93
} catch (IOException ioex) {
94
throw new RuntimeException("direct connection should succeed :"+ioex.getMessage());
95
}
96
} catch (Exception e) {
97
throw new RuntimeException(e);
98
} finally {
99
if (server != null) {
100
server.terminate();
101
}
102
}
103
104
}
105
106
private static class ProxyServer extends Thread {
107
private static ServerSocket ss = null;
108
109
// client requesting for a tunnel
110
private Socket clientSocket = null;
111
112
/*
113
* Origin server's address and port that the client
114
* wants to establish the tunnel for communication. */
115
private InetAddress serverInetAddr;
116
private int serverPort;
117
118
public ProxyServer(InetAddress server, int port) throws IOException {
119
serverInetAddr = server;
120
serverPort = port;
121
ss = new ServerSocket();
122
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
123
}
124
125
public void run() {
126
try {
127
clientSocket = ss.accept();
128
throw new RuntimeException("loopback addresses shouldn't go through the proxy "+clientSocket);
129
130
} catch (IOException e) {
131
System.out.println("Proxy Failed: " + e);
132
e.printStackTrace();
133
} finally {
134
try {
135
ss.close();
136
}
137
catch (IOException excep) {
138
System.out.println("ProxyServer close error: " + excep);
139
excep.printStackTrace();
140
}
141
}
142
}
143
144
/**
145
***************************************************************
146
* helper methods follow
147
***************************************************************
148
*/
149
public int getPort() {
150
return ss.getLocalPort();
151
}
152
}
153
}
154
155