Path: blob/master/test/jdk/sun/net/www/http/HttpClient/ProxyFromCache.java
41154 views
/*1* Copyright (c) 2006, 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 649856626* @summary URL.openConnection(Proxy.NO_PROXY) may connect through a proxy.27* @modules java.base/sun.net.www28* @library /test/lib29* @run main/othervm ProxyFromCache30*/3132import java.net.*;33import java.io.*;34import sun.net.www.MessageHeader;35import jdk.test.lib.net.URIBuilder;3637/* Creates a simple proxy and http server that just return 200 OK.38* Open a URL pointing to the http server and specify that the39* connection should use the proxy. Now make a second connection40* to the same URL, specifying that no proxy is to be used.41* We count the amount of requests being sent to each server. There42* should be only one request sent to each.43*/4445public class ProxyFromCache46{47public static void main(String[] args) throws Exception {48ServerSocket proxySSocket, httpSSocket;49int proxyPort, httpPort;50InetAddress loopback = InetAddress.getLoopbackAddress();5152try {53proxySSocket = new ServerSocket();54proxySSocket.bind(new InetSocketAddress(loopback, 0));55proxyPort = proxySSocket.getLocalPort();56httpSSocket = new ServerSocket();57httpSSocket.bind(new InetSocketAddress(loopback, 0));58httpPort = httpSSocket.getLocalPort();59} catch (Exception e) {60System.out.println ("Exception: " + e);61throw e;62}6364SimpleServer proxyServer = new SimpleServer(proxySSocket);65proxyServer.start();66SimpleServer httpServer = new SimpleServer(httpSSocket);67httpServer.start();6869InetSocketAddress addr = new InetSocketAddress(loopback, proxyPort);70Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);7172try {73URL url = URIBuilder.newBuilder()74.scheme("http")75.loopback()76.port(httpPort)77.path("/")78.toURL();7980String urlStr = url.toString();8182// 1st connection.83HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy);84InputStream is = uc.getInputStream();8586byte[] ba = new byte[1024];87while(is.read(ba) != -1);88is.close();8990// 2nd connection.91uc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);92is = uc.getInputStream();9394while(is.read(ba) != -1);95is.close();9697try {98proxySSocket.close();99httpSSocket.close();100} catch (IOException e) {}101102proxyServer.terminate();103httpServer.terminate();104105int httpCount = httpServer.getConnectionCount();106int proxyCount = proxyServer.getConnectionCount();107108if (proxyCount != 1 && httpCount != 1) {109System.out.println("Proxy = " + proxyCount + ", http = " + httpCount);110throw new RuntimeException("Failed: Proxy being sent " + proxyCount + " requests");111}112} catch (IOException e) {113throw e;114}115}116}117118class SimpleServer extends Thread119{120private ServerSocket ss;121private Socket sock;122private int connectionCount;123124String replyOK = "HTTP/1.1 200 OK\r\n" +125"Content-Length: 0\r\n\r\n";126127public SimpleServer(ServerSocket ss) {128this.ss = ss;129}130131public void run() {132try {133sock = ss.accept();134connectionCount++;135InputStream is = sock.getInputStream();136OutputStream os = sock.getOutputStream();137138MessageHeader headers = new MessageHeader (is);139os.write(replyOK.getBytes("UTF-8"));140141headers = new MessageHeader (is);142// If we get here then we received a second request.143connectionCount++;144os.write(replyOK.getBytes("UTF-8"));145146sock.close();147} catch (Exception e) {148//e.printStackTrace();149if (sock != null && !sock.isClosed()) {150try { sock.close();151} catch (IOException ioe) {}152}153}154}155156public int getConnectionCount() {157return connectionCount;158}159160public void terminate() {161if (sock != null && !sock.isClosed()) {162try { sock.close();163} catch (IOException ioe) {}164}165}166}167168169