1 /*
2  * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 4696512
27  * @summary HTTP client: Improve proxy server configuration and selection
28  * @modules java.base/sun.net.www
29  * @library ../../../sun/net/www/httptest/ /test/lib
30  * @build ClosedChannelList TestHttpServer HttpTransaction HttpCallback
31  * @compile ProxyTest.java
32  * @run main/othervm -Dhttp.proxyHost=inexistant -Dhttp.proxyPort=8080 ProxyTest
33  */
34 
35 import java.net.*;
36 import java.io.*;
37 import java.util.List;
38 import jdk.test.lib.net.URIBuilder;
39 
40 public class ProxyTest implements HttpCallback {
41     static TestHttpServer server;
42 
ProxyTest()43     public ProxyTest() {
44     }
45 
request(HttpTransaction req)46     public void request(HttpTransaction req) {
47         req.setResponseEntityBody("Hello .");
48         try {
49             req.sendResponse(200, "Ok");
50             req.orderlyClose();
51         } catch (IOException e) {
52         }
53     }
54 
55     static public class MyProxySelector extends ProxySelector {
56         private static volatile URI lastURI;
57         private final static List<Proxy> NO_PROXY = List.of(Proxy.NO_PROXY);
58 
select(URI uri)59         public java.util.List<Proxy> select(URI uri) {
60             System.out.println("Selecting no proxy for " + uri);
61             lastURI = uri;
62             return NO_PROXY;
63         }
64 
connectFailed(URI uri, SocketAddress sa, IOException ioe)65         public void connectFailed(URI uri, SocketAddress sa, IOException ioe) {
66         }
67 
lastURI()68         public static URI lastURI() { return lastURI; }
69     }
70 
main(String[] args)71     public static void main(String[] args) {
72         ProxySelector defSelector = ProxySelector.getDefault();
73         if (defSelector == null)
74             throw new RuntimeException("Default ProxySelector is null");
75         ProxySelector.setDefault(new MyProxySelector());
76         try {
77             InetAddress loopback = InetAddress.getLoopbackAddress();
78             server = new TestHttpServer(new ProxyTest(), 1, 10, loopback, 0);
79             URL url = URIBuilder.newBuilder()
80                       .scheme("http")
81                       .loopback()
82                       .port(server.getLocalPort())
83                       .toURL();
84             System.out.println("client opening connection to: " + url);
85             HttpURLConnection urlc = (HttpURLConnection)url.openConnection();
86             InputStream is = urlc.getInputStream();
87             is.close();
88             URI lastURI = MyProxySelector.lastURI();
89             if (!String.valueOf(lastURI).equals(url + "/")) {
90                 throw new AssertionError("Custom proxy was not used: last URI was " + lastURI);
91             }
92         } catch (Exception e) {
93                 throw new RuntimeException(e);
94         } finally {
95             if (server != null) {
96                 server.terminate();
97             }
98         }
99     }
100 }
101