blob: bf33bb544c653735f08563300af5ab115cb56892 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2003 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/**
25 * @test
26 * @bug 4921848
27 * @library ../../../sun/net/www/httptest/
28 * @build HttpCallback HttpServer ClosedChannelList HttpTransaction
29 * @run main/othervm -Dhttp.auth.preference=basic B4921848
30 * @summary Allow user control over authentication schemes
31 */
32
33import java.io.*;
34import java.net.*;
35
36public class B4921848 implements HttpCallback {
37
38 static int count = 0;
39
40 public void request (HttpTransaction req) {
41 try {
42 if (count == 0 ) {
43 req.addResponseHeader ("Connection", "close");
44 req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foo\"");
45 req.addResponseHeader ("WWW-Authenticate", "Digest realm=\"bar\" domain=/biz nonce=\"hereisanonce\"");
46 req.sendResponse (401, "Unauthorized");
47 req.orderlyClose();
48 } else {
49 String authheader = req.getRequestHeader ("Authorization");
50 if (authheader.startsWith ("Basic")) {
51 req.setResponseEntityBody ("Hello .");
52 req.sendResponse (200, "Ok");
53 req.orderlyClose();
54 } else {
55 req.sendResponse (400, "Bad Request");
56 req.orderlyClose();
57 }
58 }
59 count ++;
60 } catch (IOException e) {
61 e.printStackTrace();
62 }
63 }
64
65 static void read (InputStream is) throws IOException {
66 int c;
67 System.out.println ("reading");
68 while ((c=is.read()) != -1) {
69 System.out.write (c);
70 }
71 System.out.println ("");
72 System.out.println ("finished reading");
73 }
74
75
76 static void client (String u) throws Exception {
77 URL url = new URL (u);
78 System.out.println ("client opening connection to: " + u);
79 URLConnection urlc = url.openConnection ();
80 InputStream is = urlc.getInputStream ();
81 read (is);
82 is.close();
83 }
84
85 static HttpServer server;
86
87 public static void main (String[] args) throws Exception {
88 MyAuthenticator auth = new MyAuthenticator ();
89 Authenticator.setDefault (auth);
90 try {
91 server = new HttpServer (new B4921848(), 1, 10, 0);
92 System.out.println ("Server started: listening on port: " + server.getLocalPort());
93 client ("http://localhost:"+server.getLocalPort()+"/d1/d2/d3/foo.html");
94 } catch (Exception e) {
95 if (server != null) {
96 server.terminate();
97 }
98 throw e;
99 }
100 server.terminate();
101 }
102
103 public static void except (String s) {
104 server.terminate();
105 throw new RuntimeException (s);
106 }
107
108 static class MyAuthenticator extends Authenticator {
109 MyAuthenticator () {
110 super ();
111 }
112
113 public PasswordAuthentication getPasswordAuthentication () {
114 return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray()));
115 }
116
117 }
118
119}