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