Path: blob/master/test/jdk/java/net/Authenticator/B4962064.java
41149 views
/*1* Copyright (c) 2004, 2021, 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 496206426* @run main/othervm B496206427* @run main/othervm -Djava.net.preferIPv6Addresses=true B496206428* @summary Extend Authenticator to provide access to request URI and server/proxy29*/3031import java.io.IOException;32import java.io.InputStream;33import java.io.PrintWriter;34import java.net.Authenticator;35import java.net.InetAddress;36import java.net.InetSocketAddress;37import java.net.PasswordAuthentication;38import java.net.URL;39import java.net.URLConnection;40import java.util.concurrent.Executors;4142import com.sun.net.httpserver.HttpExchange;43import com.sun.net.httpserver.HttpHandler;44import com.sun.net.httpserver.HttpServer;4546public class B4962064 implements HttpHandler {4748static int count = 0;4950public void handle (HttpExchange req) {51try {52switch (count) {53case 0:54req.getResponseHeaders().set("Connection", "close");55req.getResponseHeaders().add("WWW-Authenticate", "Basic realm=\"foo\"");56req.sendResponseHeaders(401, -1);57break;58case 1:59case 3:60req.sendResponseHeaders(200, 0);61try(PrintWriter pw = new PrintWriter(req.getResponseBody())) {62pw.print("Hello .");63}64break;65case 2:66req.getResponseHeaders().set("Connection", "close");67req.getResponseHeaders().add("Proxy-Authenticate", "Basic realm=\"foo\"");68req.sendResponseHeaders(407, -1);69break;70}71count ++;72} catch (IOException e) {73e.printStackTrace();74}75}7677static void read (InputStream is) throws IOException {78int c;79System.out.println ("reading");80while ((c=is.read()) != -1) {81System.out.write (c);82}83System.out.println ("");84System.out.println ("finished reading");85}868788static void client (String u) throws Exception {89URL url = new URL (u);90System.out.println ("client opening connection to: " + u);91URLConnection urlc = url.openConnection ();92InputStream is = urlc.getInputStream ();93read (is);94is.close();95}9697static HttpServer server;98static URL urlsave;99100public static void main (String[] args) throws Exception {101B4962064 b4962064 = new B4962064();102try {103InetAddress address = InetAddress.getLoopbackAddress();104InetAddress resolved = InetAddress.getByName(address.getHostName());105System.out.println("Lookup: " + address + " -> \""106+ address.getHostName() + "\" -> "107+ resolved);108server = HttpServer.create(new InetSocketAddress(address, 0), 10);109server.createContext("/", b4962064);110server.setExecutor(Executors.newSingleThreadExecutor());111server.start();112int port = server.getAddress().getPort();113String proxyHost = address.equals(resolved)114? address.getHostName()115: address.getHostAddress();116System.setProperty ("http.proxyHost", proxyHost);117System.setProperty ("http.proxyPort", Integer.toString (port));118MyAuthenticator auth = new MyAuthenticator ();119Authenticator.setDefault (auth);120System.out.println ("Server started: listening on port: " + port);121String s = new String ("http://foo.com/d1/d2/d3/foo.html");122urlsave = new URL (s);123client (s);124s = new String ("http://bar.com/dr/d3/foo.html");125urlsave = new URL (s);126client (s);127} catch (Exception e) {128if (server != null) {129server.stop(1);130}131throw e;132}133server.stop(1);134}135136public static void except (String s) {137server.stop(1);138throw new RuntimeException (s);139}140141static class MyAuthenticator extends Authenticator {142int count = 0;143MyAuthenticator () {144super ();145}146147public PasswordAuthentication getPasswordAuthentication () {148URL url = getRequestingURL ();149if (!url.equals (urlsave)) {150except ("urls not equal");151}152Authenticator.RequestorType expected;153if (count == 0) {154expected = Authenticator.RequestorType.SERVER;155} else {156expected = Authenticator.RequestorType.PROXY;157}158if (getRequestorType() != expected) {159except ("wrong authtype");160}161count ++;162return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));163}164165}166167}168169170