1 /* 2 * Copyright (c) 2003, 2012, 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 4804309 27 * @modules java.base/sun.net.www 28 * @library ../../../sun/net/www/httptest/ 29 * @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction 30 * @run main AuthHeaderTest 31 * @summary AuthHeaderTest bug 32 */ 33 34 import java.io.*; 35 import java.net.*; 36 37 public class AuthHeaderTest implements HttpCallback { 38 39 static int count = 0; 40 static String authstring; 41 42 void errorReply (HttpTransaction req, String reply) throws IOException { 43 req.addResponseHeader ("Connection", "close"); 44 req.addResponseHeader ("Www-authenticate", reply); 45 req.sendResponse (401, "Unauthorized"); 46 req.orderlyClose(); 47 } 48 49 void okReply (HttpTransaction req) throws IOException { 50 req.setResponseEntityBody ("Hello ."); 51 req.sendResponse (200, "Ok"); 52 req.orderlyClose(); 53 } 54 55 public void request (HttpTransaction req) { 56 try { 57 authstring = req.getRequestHeader ("Authorization"); 58 System.out.println (authstring); 59 switch (count) { 60 case 0: 61 errorReply (req, "Basic realm=\"wallyworld\""); 62 break; 63 case 1: 64 /* client stores a username/pw for wallyworld 65 */ 66 okReply (req); 67 break; 68 } 69 count ++; 70 } catch (IOException e) { 71 e.printStackTrace(); 72 } 73 } 74 75 static void read (InputStream is) throws IOException { 76 int c; 77 System.out.println ("reading"); 78 while ((c=is.read()) != -1) { 79 System.out.write (c); 80 } 81 System.out.println (""); 82 System.out.println ("finished reading"); 83 } 84 85 static void client (String u) throws Exception { 86 URL url = new URL (u); 87 System.out.println ("client opening connection to: " + u); 88 URLConnection urlc = url.openConnection (); 89 InputStream is = urlc.getInputStream (); 90 read (is); 91 is.close(); 92 } 93 94 static TestHttpServer server; 95 96 public static void main (String[] args) throws Exception { 97 MyAuthenticator auth = new MyAuthenticator (); 98 Authenticator.setDefault (auth); 99 try { 100 server = new TestHttpServer (new AuthHeaderTest(), 1, 10, 0); 101 System.out.println ("Server: listening on port: " + server.getLocalPort()); 102 client ("http://localhost:"+server.getLocalPort()+"/d1/foo.html"); 103 } catch (Exception e) { 104 if (server != null) { 105 server.terminate(); 106 } 107 throw e; 108 } 109 int f = auth.getCount(); 110 if (f != 1) { 111 except ("Authenticator was called "+f+" times. Should be 1"); 112 } 113 server.terminate(); 114 } 115 116 public static void except (String s) { 117 server.terminate(); 118 throw new RuntimeException (s); 119 } 120 121 static class MyAuthenticator extends Authenticator { 122 MyAuthenticator () { 123 super (); 124 } 125 126 int count = 0; 127 128 public PasswordAuthentication getPasswordAuthentication () { 129 PasswordAuthentication pw; 130 pw = new PasswordAuthentication ("user", "pass2".toCharArray()); 131 count ++; 132 return pw; 133 } 134 135 public int getCount () { 136 return (count); 137 } 138 } 139 }