blob: 2444d197ba755c53a6f015256c39a4b823484fc5 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2002-2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4720715
27 * @summary FTP with user and password doesn't work through proxy
28 */
29
30import java.io.*;
31import java.net.*;
32import 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
42public 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 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(int port) {
129 this.port = port;
130 }
131
132 public HttpProxyServer() {
133 this(0);
134 }
135
136 public int getPort() {
137 if (server != null)
138 return server.getLocalPort();
139 return 0;
140 }
141
142 public String getURL() {
143 return askedUrl;
144 }
145
146 /**
147 * A way to tell the server that it can stop.
148 */
149 synchronized public void terminate() {
150 done = true;
151 }
152
153 public void run() {
154 try {
155 server = new ServerSocket(port);
156 Socket client;
157 while (!done) {
158 client = server.accept();
159 (new HttpProxyHandler(client)).start();
160 }
161 server.close();
162 } catch (Exception e) {
163 }
164 }
165 }
166
167 public static void main(String[] args) {
168 ProxyTest test = new ProxyTest();
169 }
170
171 public ProxyTest() {
172 String testURL = "ftp://anonymous:password@myhost.mydomain/index.html";
173 HttpProxyServer server = new HttpProxyServer();
174 try {
175 server.start();
176 int port = 0;
177 while (port == 0) {
178 Thread.sleep(500);
179 port = server.getPort();
180 }
181
182 System.setProperty("ftp.proxyHost","localhost");
183 System.setProperty("ftp.proxyPort", String.valueOf(port));
184 URL url = new URL(testURL);
185 InputStream ins = url.openStream();
186 BufferedReader in = new BufferedReader(new InputStreamReader(ins));
187 String line;
188 do {
189 line = in.readLine();
190 } while (line != null);
191 in.close();
192 server.terminate();
193 server.interrupt();
194 } catch (Exception e) {
195 e.printStackTrace();
196 }
197 /*
198 * If the URLs don't match, we've got a bug!
199 */
200 if (!testURL.equals(server.getURL())) {
201 throw new RuntimeException(server.getURL() + " != " + testURL);
202 }
203 }
204
205}