1 /* 2 * Copyright (c) 2016, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * @test 28 * @bug 8163561 29 * @modules java.base/sun.net.www 30 * jdk.incubator.httpclient 31 * @summary Verify that Proxy-Authenticate header is correctly handled 32 * 33 * @run main/othervm ProxyAuthTest 34 */ 35 36 import java.io.BufferedWriter; 37 import java.io.IOException; 38 import java.io.InputStream; 39 import java.io.OutputStream; 40 import java.io.OutputStreamWriter; 41 import java.io.PrintWriter; 42 import java.net.Authenticator; 43 import java.net.InetSocketAddress; 44 import java.net.PasswordAuthentication; 45 import java.net.ProxySelector; 46 import java.net.ServerSocket; 47 import java.net.Socket; 48 import java.net.URI; 49 import jdk.incubator.http.HttpClient; 50 import jdk.incubator.http.HttpRequest; 51 import jdk.incubator.http.HttpResponse; 52 import java.util.Base64; 53 import sun.net.www.MessageHeader; 54 import static jdk.incubator.http.HttpResponse.BodyHandler.discard; 55 56 public class ProxyAuthTest { 57 private static final String AUTH_USER = "user"; 58 private static final String AUTH_PASSWORD = "password"; 59 60 public static void main(String[] args) throws Exception { 61 try (ServerSocket ss = new ServerSocket(0)) { 62 int port = ss.getLocalPort(); 63 MyProxy proxy = new MyProxy(ss); 64 (new Thread(proxy)).start(); 65 System.out.println("Proxy listening port " + port); 66 67 Auth auth = new Auth(); 68 InetSocketAddress paddr = new InetSocketAddress("localhost", port); 69 70 URI uri = new URI("http://www.google.ie/"); 71 HttpClient client = HttpClient.newBuilder() 72 .proxy(ProxySelector.of(paddr)) 73 .authenticator(auth) 74 .build(); 75 HttpRequest req = HttpRequest.newBuilder(uri).GET().build(); 76 HttpResponse<?> resp = client.sendAsync(req, discard(null)).get(); 77 if (resp.statusCode() != 404) { 78 throw new RuntimeException("Unexpected status code: " + resp.statusCode()); 79 } 80 81 if (auth.isCalled) { 82 System.out.println("Authenticator is called"); 83 } else { 84 throw new RuntimeException("Authenticator is not called"); 85 } 86 87 if (!proxy.matched) { 88 throw new RuntimeException("Proxy authentication failed"); 89 } 90 } 91 } 92 93 static class Auth extends Authenticator { 94 private volatile boolean isCalled; 95 96 @Override 97 protected PasswordAuthentication getPasswordAuthentication() { 98 System.out.println("scheme: " + this.getRequestingScheme()); 99 isCalled = true; 100 return new PasswordAuthentication(AUTH_USER, 101 AUTH_PASSWORD.toCharArray()); 102 } 103 } 104 105 static class MyProxy implements Runnable { 106 final ServerSocket ss; 107 private volatile boolean matched; 108 109 MyProxy(ServerSocket ss) { 110 this.ss = ss; 111 } 112 113 public void run() { 114 for (int i = 0; i < 2; i++) { 115 try (Socket s = ss.accept(); 116 InputStream in = s.getInputStream(); 117 OutputStream os = s.getOutputStream(); 118 BufferedWriter writer = new BufferedWriter( 119 new OutputStreamWriter(os)); 120 PrintWriter out = new PrintWriter(writer);) { 121 MessageHeader headers = new MessageHeader(in); | 1 /* 2 * Copyright (c) 2016, 2017, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /* 27 * @test 28 * @bug 8163561 29 * @modules java.base/sun.net.www 30 * jdk.incubator.httpclient 31 * @summary Verify that Proxy-Authenticate header is correctly handled 32 * @run main/othervm ProxyAuthTest 33 */ 34 35 import java.io.BufferedWriter; 36 import java.io.IOException; 37 import java.io.InputStream; 38 import java.io.OutputStream; 39 import java.io.OutputStreamWriter; 40 import java.io.PrintWriter; 41 import java.net.Authenticator; 42 import java.net.InetSocketAddress; 43 import java.net.PasswordAuthentication; 44 import java.net.Proxy; 45 import java.net.ProxySelector; 46 import java.net.ServerSocket; 47 import java.net.Socket; 48 import java.net.SocketAddress; 49 import java.net.URI; 50 import jdk.incubator.http.HttpClient; 51 import jdk.incubator.http.HttpRequest; 52 import jdk.incubator.http.HttpResponse; 53 import java.util.Base64; 54 import java.util.List; 55 import sun.net.www.MessageHeader; 56 import static jdk.incubator.http.HttpResponse.BodyHandler.discard; 57 58 public class ProxyAuthTest { 59 private static final String AUTH_USER = "user"; 60 private static final String AUTH_PASSWORD = "password"; 61 62 public static void main(String[] args) throws Exception { 63 try (ServerSocket ss = new ServerSocket(0)) { 64 int port = ss.getLocalPort(); 65 MyProxy proxy = new MyProxy(ss); 66 (new Thread(proxy)).start(); 67 System.out.println("Proxy listening port " + port); 68 69 Auth auth = new Auth(); 70 InetSocketAddress paddr = new InetSocketAddress("localhost", port); 71 72 URI uri = new URI("http://www.google.ie/"); 73 CountingProxySelector ps = CountingProxySelector.of(paddr); 74 HttpClient client = HttpClient.newBuilder() 75 .proxy(ps) 76 .authenticator(auth) 77 .build(); 78 HttpRequest req = HttpRequest.newBuilder(uri).GET().build(); 79 HttpResponse<?> resp = client.sendAsync(req, discard(null)).get(); 80 if (resp.statusCode() != 404) { 81 throw new RuntimeException("Unexpected status code: " + resp.statusCode()); 82 } 83 84 if (auth.isCalled) { 85 System.out.println("Authenticator is called"); 86 } else { 87 throw new RuntimeException("Authenticator is not called"); 88 } 89 90 if (!proxy.matched) { 91 throw new RuntimeException("Proxy authentication failed"); 92 } 93 if (ps.count() > 1) { 94 throw new RuntimeException("CountingProxySelector. Expected 1, got " + ps.count()); 95 } 96 } 97 } 98 99 static class Auth extends Authenticator { 100 private volatile boolean isCalled; 101 102 @Override 103 protected PasswordAuthentication getPasswordAuthentication() { 104 System.out.println("scheme: " + this.getRequestingScheme()); 105 isCalled = true; 106 return new PasswordAuthentication(AUTH_USER, 107 AUTH_PASSWORD.toCharArray()); 108 } 109 } 110 111 /** 112 * A Proxy Selector that wraps a ProxySelector.of(), and counts the number 113 * of times its select method has been invoked. This can be used to ensure 114 * that the Proxy Selector is invoked only once per HttpClient.sendXXX 115 * invocation. 116 */ 117 static class CountingProxySelector extends ProxySelector { 118 private final ProxySelector proxySelector; 119 private volatile int count; // 0 120 private CountingProxySelector(InetSocketAddress proxyAddress) { 121 proxySelector = ProxySelector.of(proxyAddress); 122 } 123 124 public static CountingProxySelector of(InetSocketAddress proxyAddress) { 125 return new CountingProxySelector(proxyAddress); 126 } 127 128 int count() { return count; } 129 130 @Override 131 public List<Proxy> select(URI uri) { 132 count++; 133 return proxySelector.select(uri); 134 } 135 136 @Override 137 public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { 138 proxySelector.connectFailed(uri, sa, ioe); 139 } 140 } 141 142 static class MyProxy implements Runnable { 143 final ServerSocket ss; 144 private volatile boolean matched; 145 146 MyProxy(ServerSocket ss) { 147 this.ss = ss; 148 } 149 150 public void run() { 151 for (int i = 0; i < 2; i++) { 152 try (Socket s = ss.accept(); 153 InputStream in = s.getInputStream(); 154 OutputStream os = s.getOutputStream(); 155 BufferedWriter writer = new BufferedWriter( 156 new OutputStreamWriter(os)); 157 PrintWriter out = new PrintWriter(writer);) { 158 MessageHeader headers = new MessageHeader(in); |