0N/A/*
3261N/A * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4720715
0N/A * @summary FTP with user and password doesn't work through proxy
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.net.*;
0N/Aimport java.util.regex.*;
0N/A
0N/A
0N/A/*
0N/A * The goal here is to simulate a simplified (a lot) HTTP proxy server to see
0N/A * what kind of URL is passed down the line by the URLConnection.
0N/A * In particular, we want to make sure no information is lost (like username
0N/A * and password).
0N/A */
0N/A
0N/Apublic class ProxyTest {
0N/A
0N/A /*
0N/A * Proxy server as an innerclass. Has to run in a separate thread
0N/A */
0N/A private class HttpProxyServer extends Thread {
0N/A private ServerSocket server;
0N/A private int port;
2612N/A private volatile boolean done = false;
0N/A private String askedUrl;
0N/A
0N/A /**
0N/A * This Inner class will handle ONE client at a time.
0N/A * That's where 99% of the protocol handling is done.
0N/A */
0N/A
0N/A private class HttpProxyHandler extends Thread {
0N/A BufferedReader in;
0N/A PrintWriter out;
0N/A Socket client;
0N/A
0N/A public HttpProxyHandler(Socket cl) {
0N/A client = cl;
0N/A }
0N/A
0N/A public void run() {
0N/A boolean done = false;
0N/A
0N/A try {
0N/A in = new BufferedReader(new InputStreamReader(client.getInputStream()));
0N/A out = new PrintWriter(client.getOutputStream(), true);
0N/A } catch (Exception ex) {
0N/A return;
0N/A }
0N/A /*
0N/A * Look for the actual GET request and extract the URL
0N/A * A regex should do the trick.
0N/A */
0N/A Pattern p = Pattern.compile("^GET (.*) HTTP/1\\.1");
0N/A while (!done) {
0N/A try {
0N/A String str = in.readLine();
0N/A Matcher m = p.matcher(str);
0N/A if (m.find())
0N/A askedUrl = m.group(1);
0N/A if ("".equals(str))
0N/A done = true;
0N/A } catch (IOException ioe) {
0N/A ioe.printStackTrace();
0N/A try {
0N/A out.close();
0N/A } catch (Exception ex2) {
0N/A }
0N/A done = true;
0N/A }
0N/A }
0N/A /*
0N/A * sends back a 'dummy' document for completness sake.
0N/A */
0N/A out.println("HTTP/1.0 200 OK");
0N/A out.println("Server: Squid/2.4.STABLE6");
0N/A out.println("Mime-Version: 1.0");
0N/A out.println("Date: Fri, 26 Jul 2002 17:56:00 GMT");
0N/A out.println("Content-Type: text/html");
0N/A out.println("Last-Modified: Fri, 26 Jul 2002 01:49:57 GMT");
0N/A out.println("Age: 168");
0N/A out.println("X-Cache: HIT from javinator");
0N/A out.println("Proxy-Connection: close");
0N/A out.println();
0N/A out.println("<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">");
0N/A out.println("<html>");
0N/A out.println("<head>");
0N/A out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");
0N/A out.println("<TITLE>Hoth Downloads</TITLE>");
0N/A out.println("</head>");
0N/A out.println("<body background=\"/images/background.gif\">");
0N/A out.println("<center>");
0N/A out.println("<h1>");
0N/A out.println("<b>Hoth Downloads</b></h1></center>");
0N/A out.println("</body>");
0N/A out.println("</html>");
0N/A out.flush();
0N/A out.close();
0N/A }
0N/A }
0N/A
2612N/A public HttpProxyServer() throws IOException {
2612N/A server = new ServerSocket(0);
0N/A }
0N/A
0N/A public int getPort() {
0N/A if (server != null)
0N/A return server.getLocalPort();
0N/A return 0;
0N/A }
0N/A
0N/A public String getURL() {
0N/A return askedUrl;
0N/A }
0N/A
0N/A /**
0N/A * A way to tell the server that it can stop.
0N/A */
0N/A synchronized public void terminate() {
0N/A done = true;
2612N/A try { server.close(); } catch (IOException unused) {}
0N/A }
0N/A
0N/A public void run() {
0N/A try {
0N/A Socket client;
0N/A while (!done) {
0N/A client = server.accept();
0N/A (new HttpProxyHandler(client)).start();
0N/A }
0N/A } catch (Exception e) {
2612N/A } finally {
2612N/A try { server.close(); } catch (IOException unused) {}
0N/A }
0N/A }
0N/A }
0N/A
2612N/A public static void main(String[] args) throws Exception {
0N/A ProxyTest test = new ProxyTest();
0N/A }
0N/A
2612N/A public ProxyTest() throws Exception {
2612N/A BufferedReader in = null;
0N/A String testURL = "ftp://anonymous:password@myhost.mydomain/index.html";
0N/A HttpProxyServer server = new HttpProxyServer();
0N/A try {
2612N/A server.start();
2612N/A int port = server.getPort();
0N/A
2612N/A Proxy ftpProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", port));
2612N/A URL url = new URL(testURL);
2612N/A InputStream ins = (url.openConnection(ftpProxy)).getInputStream();
2612N/A in = new BufferedReader(new InputStreamReader(ins));
2612N/A String line;
2612N/A do {
2612N/A line = in.readLine();
2612N/A } while (line != null);
2612N/A in.close();
0N/A } catch (Exception e) {
0N/A e.printStackTrace();
2612N/A } finally {
2612N/A server.terminate();
2612N/A try { in.close(); } catch (IOException unused) {}
0N/A }
0N/A /*
0N/A * If the URLs don't match, we've got a bug!
0N/A */
0N/A if (!testURL.equals(server.getURL())) {
0N/A throw new RuntimeException(server.getURL() + " != " + testURL);
0N/A }
0N/A }
0N/A
0N/A}