Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/www/http/HttpClient/ProxyFromCache.java
38867 views
/*1* Copyright (c) 2006, 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 649856626* @run main/othervm ProxyFromCache27* @summary URL.openConnection(Proxy.NO_PROXY) may connect through a proxy.28*/2930import java.net.*;31import java.io.*;32import sun.net.www.MessageHeader;3334/* Creates a simple proxy and http server that just return 200 OK.35* Open a URL pointing to the http server and specify that the36* connection should use the proxy. Now make a second connection37* to the same URL, specifying that no proxy is to be used.38* We count the amount of requests being sent to each server. There39* should be only one request sent to each.40*/4142public class ProxyFromCache43{44public static void main(String[] args) {45ServerSocket proxySSocket, httpSSocket;46int proxyPort, httpPort;4748try {49proxySSocket = new ServerSocket(0);50proxyPort = proxySSocket.getLocalPort();51httpSSocket = new ServerSocket(0);52httpPort = httpSSocket.getLocalPort();53} catch (Exception e) {54System.out.println ("Exception: " + e);55return;56}5758SimpleServer proxyServer = new SimpleServer(proxySSocket);59proxyServer.start();60SimpleServer httpServer = new SimpleServer(httpSSocket);61httpServer.start();6263InetSocketAddress addr = new InetSocketAddress("localhost", proxyPort);64Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);6566try {67String urlStr = "http://localhost:" + httpPort + "/";68URL url = new URL(urlStr);6970// 1st connection.71HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);72InputStream is = uc.getInputStream();7374byte[] ba = new byte[1024];75while(is.read(ba) != -1);76is.close();7778// 2nd connection.79uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);80is = uc.getInputStream();8182while(is.read(ba) != -1);83is.close();8485try {86proxySSocket.close();87httpSSocket.close();88} catch (IOException e) {}8990proxyServer.terminate();91httpServer.terminate();9293int httpCount = httpServer.getConnectionCount();94int proxyCount = proxyServer.getConnectionCount();9596if (proxyCount != 1 && httpCount != 1) {97System.out.println("Proxy = " + proxyCount + ", http = " + httpCount);98throw new RuntimeException("Failed: Proxy being sent " + proxyCount + " requests");99}100} catch (IOException e) {101throw new RuntimeException(e);102}103}104}105106class SimpleServer extends Thread107{108private ServerSocket ss;109private Socket sock;110private int connectionCount;111112String replyOK = "HTTP/1.1 200 OK\r\n" +113"Content-Length: 0\r\n\r\n";114115public SimpleServer(ServerSocket ss) {116this.ss = ss;117}118119public void run() {120try {121sock = ss.accept();122connectionCount++;123InputStream is = sock.getInputStream();124OutputStream os = sock.getOutputStream();125126MessageHeader headers = new MessageHeader (is);127os.write(replyOK.getBytes("UTF-8"));128129headers = new MessageHeader (is);130// If we get here then we received a second request.131connectionCount++;132os.write(replyOK.getBytes("UTF-8"));133134sock.close();135} catch (Exception e) {136//e.printStackTrace();137if (sock != null && !sock.isClosed()) {138try { sock.close();139} catch (IOException ioe) {}140}141}142}143144public int getConnectionCount() {145return connectionCount;146}147148public void terminate() {149if (sock != null && !sock.isClosed()) {150try { sock.close();151} catch (IOException ioe) {}152}153}154}155156157