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 
errorReply(HttpTransaction req, String reply)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 
okReply(HttpTransaction req)49     void okReply (HttpTransaction req) throws IOException {
50         req.setResponseEntityBody ("Hello .");
51         req.sendResponse (200, "Ok");
52         req.orderlyClose();
53     }
54 
request(HttpTransaction req)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 
read(InputStream is)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 
client(String u)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 
main(String[] args)96     public static void main (String[] args) throws Exception {
97         MyAuthenticator auth = new MyAuthenticator ();
98         Authenticator.setDefault (auth);
99         InetAddress loopback = InetAddress.getLoopbackAddress();
100         try {
101             server = new TestHttpServer (new AuthHeaderTest(), 1, 10, loopback, 0);
102             System.out.println ("Server: listening on port: " + server.getAuthority());
103             client ("http://" + server.getAuthority() + "/d1/foo.html");
104         } catch (Exception e) {
105             if (server != null) {
106                 server.terminate();
107             }
108             throw e;
109         }
110         int f = auth.getCount();
111         if (f != 1) {
112             except ("Authenticator was called "+f+" times. Should be 1");
113         }
114         server.terminate();
115     }
116 
except(String s)117     public static void except (String s) {
118         server.terminate();
119         throw new RuntimeException (s);
120     }
121 
122     static class MyAuthenticator extends Authenticator {
MyAuthenticator()123         MyAuthenticator () {
124             super ();
125         }
126 
127         int count = 0;
128 
getPasswordAuthentication()129         public PasswordAuthentication getPasswordAuthentication () {
130             PasswordAuthentication pw;
131             pw = new PasswordAuthentication ("user", "pass2".toCharArray());
132             count ++;
133             return pw;
134         }
135 
getCount()136         public int getCount () {
137             return (count);
138         }
139     }
140 }
141