Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/HttpURLConnection/HttpURLConWithProxy.java
41149 views
1
/*
2
* Copyright (c) 2016, 2020, 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 8161016 8183369
27
* @library /test/lib
28
* @summary When proxy is set HttpURLConnection should not use DIRECT connection.
29
* @run main/othervm HttpURLConWithProxy
30
*/
31
import java.io.IOException;
32
import java.net.InetAddress;
33
import java.net.InetSocketAddress;
34
import java.net.Proxy;
35
import java.net.ProxySelector;
36
import java.net.ServerSocket;
37
import java.net.SocketAddress;
38
import java.net.URI;
39
import java.net.URL;
40
import java.net.HttpURLConnection;
41
import java.util.ArrayList;
42
import java.util.List;
43
import jdk.test.lib.net.URIBuilder;
44
import java.util.logging.Handler;
45
import java.util.logging.Level;
46
import java.util.logging.Logger;
47
import java.util.logging.LogRecord;
48
49
public class HttpURLConWithProxy {
50
51
private static Logger logger =
52
Logger.getLogger("sun.net.www.protocol.http.HttpURLConnection");
53
54
public static void main(String... arg) throws Exception {
55
// Remove the default nonProxyHosts to use localhost for testing
56
System.setProperty("http.nonProxyHosts", "");
57
58
System.setProperty("http.proxyHost", "1.1.1.1");
59
System.setProperty("http.proxyPort", "1111");
60
61
// Use the logger to help verify the Proxy was used
62
logger.setLevel(Level.ALL);
63
Handler h = new ProxyHandler();
64
h.setLevel(Level.ALL);
65
logger.addHandler(h);
66
67
ServerSocket ss;
68
URL url;
69
HttpURLConnection con;
70
InetAddress loopback = InetAddress.getLoopbackAddress();
71
InetSocketAddress address = new InetSocketAddress(loopback, 0);
72
73
// Test1: using Proxy set by System Property:
74
try {
75
ss = new ServerSocket();
76
ss.bind(address);
77
url = URIBuilder.newBuilder()
78
.scheme("http")
79
.loopback()
80
.port(ss.getLocalPort())
81
.toURL();
82
con = (HttpURLConnection) url.openConnection();
83
con.setConnectTimeout(10 * 1000);
84
con.connect();
85
if(con.usingProxy()){
86
System.out.println("Test1 Passed with: Connection succeeded with proxy");
87
} else {
88
throw new RuntimeException("Shouldn't use DIRECT connection "
89
+ "when proxy is invalid/down");
90
}
91
} catch (IOException ie) {
92
if(!ProxyHandler.proxyRetried) {
93
throw new RuntimeException("Connection not retried with proxy");
94
}
95
System.out.println("Test1 Passed with: " + ie.getMessage());
96
}
97
98
// Test2: using custom ProxySelector implementation
99
ProxyHandler.proxyRetried = false;
100
MyProxySelector myProxySel = new MyProxySelector();
101
ProxySelector.setDefault(myProxySel);
102
try {
103
ss = new ServerSocket();
104
ss.bind(address);
105
url = URIBuilder.newBuilder()
106
.scheme("http")
107
.loopback()
108
.port(ss.getLocalPort())
109
.toURL();
110
con = (HttpURLConnection) url.openConnection();
111
con.setConnectTimeout(10 * 1000);
112
con.connect();
113
if(con.usingProxy()){
114
System.out.println("Test2 Passed with: Connection succeeded with proxy");
115
} else {
116
throw new RuntimeException("Shouldn't use DIRECT connection "
117
+ "when proxy is invalid/down");
118
}
119
} catch (IOException ie) {
120
if(!ProxyHandler.proxyRetried) {
121
throw new RuntimeException("Connection not retried with proxy");
122
}
123
System.out.println("Test2 Passed with: " + ie.getMessage());
124
}
125
}
126
}
127
128
129
class MyProxySelector extends ProxySelector {
130
131
List<Proxy> proxies = new ArrayList<>();
132
133
MyProxySelector() {
134
Proxy p1 = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("2.2.2.2", 2222));
135
Proxy p2 = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("3.3.3.3", 3333));
136
proxies.add(p1);
137
proxies.add(p2);
138
}
139
140
@Override
141
public List<Proxy> select(URI uri) {
142
return proxies;
143
}
144
145
@Override
146
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
147
// System.out.println("MyProxySelector.connectFailed(): "+sa);
148
}
149
}
150
151
class ProxyHandler extends Handler {
152
public static boolean proxyRetried = false;
153
154
@Override
155
public void publish(LogRecord record) {
156
if (record.getMessage().contains("Retrying with proxy")) {
157
proxyRetried = true;
158
}
159
}
160
161
@Override
162
public void flush() {
163
}
164
165
@Override
166
public void close() throws SecurityException {
167
}
168
}
169
170