Path: blob/master/test/jdk/sun/net/www/http/HttpClient/ProxyTest.java
41154 views
/*1* Copyright (c) 2002, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 472071526* @summary FTP with user and password doesn't work through proxy27*/2829import java.io.*;30import java.net.*;31import java.util.regex.*;323334/*35* The goal here is to simulate a simplified (a lot) HTTP proxy server to see36* what kind of URL is passed down the line by the URLConnection.37* In particular, we want to make sure no information is lost (like username38* and password).39*/4041public class ProxyTest {4243/*44* Proxy server as an innerclass. Has to run in a separate thread45*/46private class HttpProxyServer extends Thread {47private ServerSocket server;48private int port;49private volatile boolean done = false;50private String askedUrl;5152/**53* This Inner class will handle ONE client at a time.54* That's where 99% of the protocol handling is done.55*/5657private class HttpProxyHandler extends Thread {58BufferedReader in;59PrintWriter out;60Socket client;6162public HttpProxyHandler(Socket cl) {63client = cl;64}6566public void run() {67boolean done = false;6869try {70in = new BufferedReader(new InputStreamReader(client.getInputStream()));71out = new PrintWriter(client.getOutputStream(), true);72} catch (Exception ex) {73return;74}75/*76* Look for the actual GET request and extract the URL77* A regex should do the trick.78*/79Pattern p = Pattern.compile("^GET (.*) HTTP/1\\.1");80while (!done) {81try {82String str = in.readLine();83Matcher m = p.matcher(str);84if (m.find())85askedUrl = m.group(1);86if ("".equals(str))87done = true;88} catch (IOException ioe) {89ioe.printStackTrace();90try {91out.close();92} catch (Exception ex2) {93}94done = true;95}96}97/*98* sends back a 'dummy' document for completness sake.99*/100out.println("HTTP/1.0 200 OK");101out.println("Server: Squid/2.4.STABLE6");102out.println("Mime-Version: 1.0");103out.println("Date: Fri, 26 Jul 2002 17:56:00 GMT");104out.println("Content-Type: text/html");105out.println("Last-Modified: Fri, 26 Jul 2002 01:49:57 GMT");106out.println("Age: 168");107out.println("X-Cache: HIT from javinator");108out.println("Proxy-Connection: close");109out.println();110out.println("<!doctype html public \"-//w3c//dtd html 4.0 transitional//en\">");111out.println("<html>");112out.println("<head>");113out.println("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">");114out.println("<TITLE>Hoth Downloads</TITLE>");115out.println("</head>");116out.println("<body background=\"/images/background.gif\">");117out.println("<center>");118out.println("<h1>");119out.println("<b>Hoth Downloads</b></h1></center>");120out.println("</body>");121out.println("</html>");122out.flush();123out.close();124}125}126127public HttpProxyServer() throws IOException {128InetAddress loopback = InetAddress.getLoopbackAddress();129server = new ServerSocket();130server.bind(new InetSocketAddress(loopback, 0));131}132133public int getPort() {134if (server != null)135return server.getLocalPort();136return 0;137}138139public String getURL() {140return askedUrl;141}142143/**144* A way to tell the server that it can stop.145*/146synchronized public void terminate() {147done = true;148try { server.close(); } catch (IOException unused) {}149}150151public void run() {152try {153Socket client;154while (!done) {155client = server.accept();156(new HttpProxyHandler(client)).start();157}158} catch (Exception e) {159} finally {160try { server.close(); } catch (IOException unused) {}161}162}163}164165private static boolean hasFtp() {166try {167return new java.net.URL("ftp://") != null;168} catch (java.net.MalformedURLException x) {169System.out.println("FTP not supported by this runtime.");170return false;171}172}173174public static void main(String[] args) throws Exception {175if (hasFtp())176new ProxyTest();177}178179public ProxyTest() throws Exception {180BufferedReader in = null;181String testURL = "ftp://anonymous:[email protected]/index.html";182HttpProxyServer server = new HttpProxyServer();183try {184server.start();185int port = server.getPort();186187InetAddress loopback = InetAddress.getLoopbackAddress();188Proxy ftpProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(loopback, port));189URL url = new URL(testURL);190InputStream ins = (url.openConnection(ftpProxy)).getInputStream();191in = new BufferedReader(new InputStreamReader(ins));192String line;193do {194line = in.readLine();195} while (line != null);196in.close();197} catch (Exception e) {198e.printStackTrace();199} finally {200server.terminate();201try { in.close(); } catch (IOException unused) {}202}203/*204* If the URLs don't match, we've got a bug!205*/206if (!testURL.equals(server.getURL())) {207throw new RuntimeException(server.getURL() + " != " + testURL);208}209}210211}212213214