author | coleenp |
Wed, 17 Jun 2015 21:44:48 +0000 | |
changeset 31362 | 8957ccbb5821 |
parent 30820 | 0d4717a011d3 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
13788
diff
changeset
|
2 |
* Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/** |
|
25 |
* @test |
|
26 |
* @bug 4921848 |
|
30820 | 27 |
* @modules java.base/sun.net.www |
2 | 28 |
* @library ../../../sun/net/www/httptest/ |
13788 | 29 |
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction |
2 | 30 |
* @run main/othervm -Dhttp.auth.preference=basic B4921848 |
31 |
* @summary Allow user control over authentication schemes |
|
32 |
*/ |
|
33 |
||
34 |
import java.io.*; |
|
35 |
import java.net.*; |
|
36 |
||
37 |
public class B4921848 implements HttpCallback { |
|
38 |
||
39 |
static int count = 0; |
|
40 |
||
41 |
public void request (HttpTransaction req) { |
|
42 |
try { |
|
43 |
if (count == 0 ) { |
|
44 |
req.addResponseHeader ("Connection", "close"); |
|
45 |
req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foo\""); |
|
46 |
req.addResponseHeader ("WWW-Authenticate", "Digest realm=\"bar\" domain=/biz nonce=\"hereisanonce\""); |
|
47 |
req.sendResponse (401, "Unauthorized"); |
|
48 |
req.orderlyClose(); |
|
49 |
} else { |
|
50 |
String authheader = req.getRequestHeader ("Authorization"); |
|
51 |
if (authheader.startsWith ("Basic")) { |
|
52 |
req.setResponseEntityBody ("Hello ."); |
|
53 |
req.sendResponse (200, "Ok"); |
|
54 |
req.orderlyClose(); |
|
55 |
} else { |
|
56 |
req.sendResponse (400, "Bad Request"); |
|
57 |
req.orderlyClose(); |
|
58 |
} |
|
59 |
} |
|
60 |
count ++; |
|
61 |
} catch (IOException e) { |
|
62 |
e.printStackTrace(); |
|
63 |
} |
|
64 |
} |
|
65 |
||
66 |
static void read (InputStream is) throws IOException { |
|
67 |
int c; |
|
68 |
System.out.println ("reading"); |
|
69 |
while ((c=is.read()) != -1) { |
|
70 |
System.out.write (c); |
|
71 |
} |
|
72 |
System.out.println (""); |
|
73 |
System.out.println ("finished reading"); |
|
74 |
} |
|
75 |
||
76 |
||
77 |
static void client (String u) throws Exception { |
|
78 |
URL url = new URL (u); |
|
79 |
System.out.println ("client opening connection to: " + u); |
|
80 |
URLConnection urlc = url.openConnection (); |
|
81 |
InputStream is = urlc.getInputStream (); |
|
82 |
read (is); |
|
83 |
is.close(); |
|
84 |
} |
|
85 |
||
13788 | 86 |
static TestHttpServer server; |
2 | 87 |
|
88 |
public static void main (String[] args) throws Exception { |
|
89 |
MyAuthenticator auth = new MyAuthenticator (); |
|
90 |
Authenticator.setDefault (auth); |
|
91 |
try { |
|
13788 | 92 |
server = new TestHttpServer (new B4921848(), 1, 10, 0); |
2 | 93 |
System.out.println ("Server started: listening on port: " + server.getLocalPort()); |
94 |
client ("http://localhost:"+server.getLocalPort()+"/d1/d2/d3/foo.html"); |
|
95 |
} catch (Exception e) { |
|
96 |
if (server != null) { |
|
97 |
server.terminate(); |
|
98 |
} |
|
99 |
throw e; |
|
100 |
} |
|
101 |
server.terminate(); |
|
102 |
} |
|
103 |
||
104 |
public static void except (String s) { |
|
105 |
server.terminate(); |
|
106 |
throw new RuntimeException (s); |
|
107 |
} |
|
108 |
||
109 |
static class MyAuthenticator extends Authenticator { |
|
110 |
MyAuthenticator () { |
|
111 |
super (); |
|
112 |
} |
|
113 |
||
114 |
public PasswordAuthentication getPasswordAuthentication () { |
|
115 |
return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray())); |
|
116 |
} |
|
117 |
||
118 |
} |
|
119 |
||
120 |
} |