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/http/HttpClient/ProxyTest.java
38867 views
1
/*
2
* Copyright (c) 2002, 2013, 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 4720715
27
* @summary FTP with user and password doesn't work through proxy
28
*/
29
30
import java.io.*;
31
import java.net.*;
32
import java.util.regex.*;
33
34
35
/*
36
* The goal here is to simulate a simplified (a lot) HTTP proxy server to see
37
* what kind of URL is passed down the line by the URLConnection.
38
* In particular, we want to make sure no information is lost (like username
39
* and password).
40
*/
41
42
public class ProxyTest {
43
44
/*
45
* Proxy server as an innerclass. Has to run in a separate thread
46
*/
47
private class HttpProxyServer extends Thread {
48
private ServerSocket server;
49
private int port;
50
private volatile boolean done = false;
51
private String askedUrl;
52
53
/**
54
* This Inner class will handle ONE client at a time.
55
* That's where 99% of the protocol handling is done.
56
*/
57
58
private class HttpProxyHandler extends Thread {
59
BufferedReader in;
60
PrintWriter out;
61
Socket client;
62
63
public HttpProxyHandler(Socket cl) {
64
client = cl;
65
}
66
67
public void run() {
68
boolean done = false;
69
70
try {
71
in = new BufferedReader(new InputStreamReader(client.getInputStream()));
72
out = new PrintWriter(client.getOutputStream(), true);
73
} catch (Exception ex) {
74
return;
75
}
76
/*
77
* Look for the actual GET request and extract the URL
78
* A regex should do the trick.
79
*/
80
Pattern p = Pattern.compile("^GET (.*) HTTP/1\\.1");
81
while (!done) {
82
try {
83
String str = in.readLine();
84
Matcher m = p.matcher(str);
85
if (m.find())
86
askedUrl = m.group(1);
87
if ("".equals(str))
88
done = true;
89
} catch (IOException ioe) {
90
ioe.printStackTrace();
91
try {
92
out.close();
93
} catch (Exception ex2) {
94
}
95
done = true;
96
}
97
}
98
/*
99
* sends back a 'dummy' document for completness sake.
100
*/
101
out.println("HTTP/1.0 200 OK");
102
out.println("Server: Squid/2.4.STABLE6");
103
out.println("Mime-Version: 1.0");
104
out.println("Date: Fri, 26 Jul 2002 17:56:00 GMT");
105
out.println("Content-Type: text/html");
106
out.println("Last-Modified: Fri, 26 Jul 2002 01:49:57 GMT");
107
out.println("Age: 168");
108
out.println("X-Cache: HIT from javinator");
109
out.println("Proxy-Connection: close");
110
out.println();
111
out.println("<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">");
112
out.println("<html>");
113
out.println("<head>");
114
out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");
115
out.println("<TITLE>Hoth Downloads</TITLE>");
116
out.println("</head>");
117
out.println("<body background=\"/images/background.gif\">");
118
out.println("<center>");
119
out.println("<h1>");
120
out.println("<b>Hoth Downloads</b></h1></center>");
121
out.println("</body>");
122
out.println("</html>");
123
out.flush();
124
out.close();
125
}
126
}
127
128
public HttpProxyServer() throws IOException {
129
server = new ServerSocket(0);
130
}
131
132
public int getPort() {
133
if (server != null)
134
return server.getLocalPort();
135
return 0;
136
}
137
138
public String getURL() {
139
return askedUrl;
140
}
141
142
/**
143
* A way to tell the server that it can stop.
144
*/
145
synchronized public void terminate() {
146
done = true;
147
try { server.close(); } catch (IOException unused) {}
148
}
149
150
public void run() {
151
try {
152
Socket client;
153
while (!done) {
154
client = server.accept();
155
(new HttpProxyHandler(client)).start();
156
}
157
} catch (Exception e) {
158
} finally {
159
try { server.close(); } catch (IOException unused) {}
160
}
161
}
162
}
163
164
private static boolean hasFtp() {
165
try {
166
return new java.net.URL("ftp://") != null;
167
} catch (java.net.MalformedURLException x) {
168
System.out.println("FTP not supported by this runtime.");
169
return false;
170
}
171
}
172
173
public static void main(String[] args) throws Exception {
174
if (hasFtp())
175
new ProxyTest();
176
}
177
178
public ProxyTest() throws Exception {
179
BufferedReader in = null;
180
String testURL = "ftp://anonymous:[email protected]/index.html";
181
HttpProxyServer server = new HttpProxyServer();
182
try {
183
server.start();
184
int port = server.getPort();
185
186
Proxy ftpProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", port));
187
URL url = new URL(testURL);
188
InputStream ins = (url.openConnection(ftpProxy)).getInputStream();
189
in = new BufferedReader(new InputStreamReader(ins));
190
String line;
191
do {
192
line = in.readLine();
193
} while (line != null);
194
in.close();
195
} catch (Exception e) {
196
e.printStackTrace();
197
} finally {
198
server.terminate();
199
try { in.close(); } catch (IOException unused) {}
200
}
201
/*
202
* If the URLs don't match, we've got a bug!
203
*/
204
if (!testURL.equals(server.getURL())) {
205
throw new RuntimeException(server.getURL() + " != " + testURL);
206
}
207
}
208
209
}
210
211