Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/HttpClient/ProxyTest.java
38867 views
/*1* Copyright (c) 2002, 2013, 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 {128server = new ServerSocket(0);129}130131public int getPort() {132if (server != null)133return server.getLocalPort();134return 0;135}136137public String getURL() {138return askedUrl;139}140141/**142* A way to tell the server that it can stop.143*/144synchronized public void terminate() {145done = true;146try { server.close(); } catch (IOException unused) {}147}148149public void run() {150try {151Socket client;152while (!done) {153client = server.accept();154(new HttpProxyHandler(client)).start();155}156} catch (Exception e) {157} finally {158try { server.close(); } catch (IOException unused) {}159}160}161}162163private static boolean hasFtp() {164try {165return new java.net.URL("ftp://") != null;166} catch (java.net.MalformedURLException x) {167System.out.println("FTP not supported by this runtime.");168return false;169}170}171172public static void main(String[] args) throws Exception {173if (hasFtp())174new ProxyTest();175}176177public ProxyTest() throws Exception {178BufferedReader in = null;179String testURL = "ftp://anonymous:[email protected]/index.html";180HttpProxyServer server = new HttpProxyServer();181try {182server.start();183int port = server.getPort();184185Proxy ftpProxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", port));186URL url = new URL(testURL);187InputStream ins = (url.openConnection(ftpProxy)).getInputStream();188in = new BufferedReader(new InputStreamReader(ins));189String line;190do {191line = in.readLine();192} while (line != null);193in.close();194} catch (Exception e) {195e.printStackTrace();196} finally {197server.terminate();198try { in.close(); } catch (IOException unused) {}199}200/*201* If the URLs don't match, we've got a bug!202*/203if (!testURL.equals(server.getURL())) {204throw new RuntimeException(server.getURL() + " != " + testURL);205}206}207208}209210211