Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/www/http/HttpClient/ProxyTest.java
41154 views
1
/*
2
* Copyright (c) 2002, 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 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
InetAddress loopback = InetAddress.getLoopbackAddress();
130
server = new ServerSocket();
131
server.bind(new InetSocketAddress(loopback, 0));
132
}
133
134
public int getPort() {
135
if (server != null)
136
return server.getLocalPort();
137
return 0;
138
}
139
140
public String getURL() {
141
return askedUrl;
142
}
143
144
/**
145
* A way to tell the server that it can stop.
146
*/
147
synchronized public void terminate() {
148
done = true;
149
try { server.close(); } catch (IOException unused) {}
150
}
151
152
public void run() {
153
try {
154
Socket client;
155
while (!done) {
156
client = server.accept();
157
(new HttpProxyHandler(client)).start();
158
}
159
} catch (Exception e) {
160
} finally {
161
try { server.close(); } catch (IOException unused) {}
162
}
163
}
164
}
165
166
private static boolean hasFtp() {
167
try {
168
return new java.net.URL("ftp://") != null;
169
} catch (java.net.MalformedURLException x) {
170
System.out.println("FTP not supported by this runtime.");
171
return false;
172
}
173
}
174
175
public static void main(String[] args) throws Exception {
176
if (hasFtp())
177
new ProxyTest();
178
}
179
180
public ProxyTest() throws Exception {
181
BufferedReader in = null;
182
String testURL = "ftp://anonymous:[email protected]/index.html";
183
HttpProxyServer server = new HttpProxyServer();
184
try {
185
server.start();
186
int port = server.getPort();
187
188
InetAddress loopback = InetAddress.getLoopbackAddress();
189
Proxy ftpProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(loopback, port));
190
URL url = new URL(testURL);
191
InputStream ins = (url.openConnection(ftpProxy)).getInputStream();
192
in = new BufferedReader(new InputStreamReader(ins));
193
String line;
194
do {
195
line = in.readLine();
196
} while (line != null);
197
in.close();
198
} catch (Exception e) {
199
e.printStackTrace();
200
} finally {
201
server.terminate();
202
try { in.close(); } catch (IOException unused) {}
203
}
204
/*
205
* If the URLs don't match, we've got a bug!
206
*/
207
if (!testURL.equals(server.getURL())) {
208
throw new RuntimeException(server.getURL() + " != " + testURL);
209
}
210
}
211
212
}
213
214