Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/HttpClient/ProxyFromCache.java
41154 views
1
/*
2
* Copyright (c) 2006, 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 6498566
27
* @summary URL.openConnection(Proxy.NO_PROXY) may connect through a proxy.
28
* @modules java.base/sun.net.www
29
* @library /test/lib
30
* @run main/othervm ProxyFromCache
31
*/
32
33
import java.net.*;
34
import java.io.*;
35
import sun.net.www.MessageHeader;
36
import jdk.test.lib.net.URIBuilder;
37
38
/* Creates a simple proxy and http server that just return 200 OK.
39
* Open a URL pointing to the http server and specify that the
40
* connection should use the proxy. Now make a second connection
41
* to the same URL, specifying that no proxy is to be used.
42
* We count the amount of requests being sent to each server. There
43
* should be only one request sent to each.
44
*/
45
46
public class ProxyFromCache
47
{
48
public static void main(String[] args) throws Exception {
49
ServerSocket proxySSocket, httpSSocket;
50
int proxyPort, httpPort;
51
InetAddress loopback = InetAddress.getLoopbackAddress();
52
53
try {
54
proxySSocket = new ServerSocket();
55
proxySSocket.bind(new InetSocketAddress(loopback, 0));
56
proxyPort = proxySSocket.getLocalPort();
57
httpSSocket = new ServerSocket();
58
httpSSocket.bind(new InetSocketAddress(loopback, 0));
59
httpPort = httpSSocket.getLocalPort();
60
} catch (Exception e) {
61
System.out.println ("Exception: " + e);
62
throw e;
63
}
64
65
SimpleServer proxyServer = new SimpleServer(proxySSocket);
66
proxyServer.start();
67
SimpleServer httpServer = new SimpleServer(httpSSocket);
68
httpServer.start();
69
70
InetSocketAddress addr = new InetSocketAddress(loopback, proxyPort);
71
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
72
73
try {
74
URL url = URIBuilder.newBuilder()
75
.scheme("http")
76
.loopback()
77
.port(httpPort)
78
.path("/")
79
.toURL();
80
81
String urlStr = url.toString();
82
83
// 1st connection.
84
HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);
85
InputStream is = uc.getInputStream();
86
87
byte[] ba = new byte[1024];
88
while(is.read(ba) != -1);
89
is.close();
90
91
// 2nd connection.
92
uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
93
is = uc.getInputStream();
94
95
while(is.read(ba) != -1);
96
is.close();
97
98
try {
99
proxySSocket.close();
100
httpSSocket.close();
101
} catch (IOException e) {}
102
103
proxyServer.terminate();
104
httpServer.terminate();
105
106
int httpCount = httpServer.getConnectionCount();
107
int proxyCount = proxyServer.getConnectionCount();
108
109
if (proxyCount != 1 && httpCount != 1) {
110
System.out.println("Proxy = " + proxyCount + ", http = " + httpCount);
111
throw new RuntimeException("Failed: Proxy being sent " + proxyCount + " requests");
112
}
113
} catch (IOException e) {
114
throw e;
115
}
116
}
117
}
118
119
class SimpleServer extends Thread
120
{
121
private ServerSocket ss;
122
private Socket sock;
123
private int connectionCount;
124
125
String replyOK = "HTTP/1.1 200 OK\r\n" +
126
"Content-Length: 0\r\n\r\n";
127
128
public SimpleServer(ServerSocket ss) {
129
this.ss = ss;
130
}
131
132
public void run() {
133
try {
134
sock = ss.accept();
135
connectionCount++;
136
InputStream is = sock.getInputStream();
137
OutputStream os = sock.getOutputStream();
138
139
MessageHeader headers = new MessageHeader (is);
140
os.write(replyOK.getBytes("UTF-8"));
141
142
headers = new MessageHeader (is);
143
// If we get here then we received a second request.
144
connectionCount++;
145
os.write(replyOK.getBytes("UTF-8"));
146
147
sock.close();
148
} catch (Exception e) {
149
//e.printStackTrace();
150
if (sock != null && !sock.isClosed()) {
151
try { sock.close();
152
} catch (IOException ioe) {}
153
}
154
}
155
}
156
157
public int getConnectionCount() {
158
return connectionCount;
159
}
160
161
public void terminate() {
162
if (sock != null && !sock.isClosed()) {
163
try { sock.close();
164
} catch (IOException ioe) {}
165
}
166
}
167
}
168
169