author | mikael |
Fri, 04 Dec 2015 15:08:49 -0800 | |
changeset 35090 | 1f5b6aa795d0 |
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) 2004, 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 4962064 |
|
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 B4962064 |
31 |
* @summary Extend Authenticator to provide access to request URI and server/proxy |
|
32 |
*/ |
|
33 |
||
34 |
import java.io.*; |
|
35 |
import java.net.*; |
|
36 |
||
37 |
public class B4962064 implements HttpCallback { |
|
38 |
||
39 |
static int count = 0; |
|
40 |
||
41 |
public void request (HttpTransaction req) { |
|
42 |
try { |
|
43 |
switch (count) { |
|
44 |
case 0: |
|
45 |
req.addResponseHeader ("Connection", "close"); |
|
46 |
req.addResponseHeader ("WWW-Authenticate", "Basic realm=\"foo\""); |
|
47 |
req.sendResponse (401, "Unauthorized"); |
|
48 |
req.orderlyClose(); |
|
49 |
break; |
|
50 |
case 1: |
|
51 |
case 3: |
|
52 |
req.setResponseEntityBody ("Hello ."); |
|
53 |
req.sendResponse (200, "Ok"); |
|
54 |
req.orderlyClose(); |
|
55 |
break; |
|
56 |
case 2: |
|
57 |
req.addResponseHeader ("Connection", "close"); |
|
58 |
req.addResponseHeader ("Proxy-Authenticate", "Basic realm=\"foo\""); |
|
59 |
req.sendResponse (407, "Proxy Authentication Required"); |
|
60 |
req.orderlyClose(); |
|
61 |
break; |
|
62 |
} |
|
63 |
count ++; |
|
64 |
} catch (IOException e) { |
|
65 |
e.printStackTrace(); |
|
66 |
} |
|
67 |
} |
|
68 |
||
69 |
static void read (InputStream is) throws IOException { |
|
70 |
int c; |
|
71 |
System.out.println ("reading"); |
|
72 |
while ((c=is.read()) != -1) { |
|
73 |
System.out.write (c); |
|
74 |
} |
|
75 |
System.out.println (""); |
|
76 |
System.out.println ("finished reading"); |
|
77 |
} |
|
78 |
||
79 |
||
80 |
static void client (String u) throws Exception { |
|
81 |
URL url = new URL (u); |
|
82 |
System.out.println ("client opening connection to: " + u); |
|
83 |
URLConnection urlc = url.openConnection (); |
|
84 |
InputStream is = urlc.getInputStream (); |
|
85 |
read (is); |
|
86 |
is.close(); |
|
87 |
} |
|
88 |
||
13788 | 89 |
static TestHttpServer server; |
2 | 90 |
static URL urlsave; |
91 |
||
92 |
public static void main (String[] args) throws Exception { |
|
93 |
try { |
|
13788 | 94 |
server = new TestHttpServer (new B4962064(), 1, 10, 0); |
2 | 95 |
int port = server.getLocalPort(); |
96 |
System.setProperty ("http.proxyHost", "localhost"); |
|
97 |
System.setProperty ("http.proxyPort", Integer.toString (port)); |
|
98 |
MyAuthenticator auth = new MyAuthenticator (); |
|
99 |
Authenticator.setDefault (auth); |
|
100 |
System.out.println ("Server started: listening on port: " + port); |
|
101 |
//String s = new String ("http://localhost:"+port+"/d1/d2/d3/foo.html"); |
|
102 |
String s = new String ("http://foo.com/d1/d2/d3/foo.html"); |
|
103 |
urlsave = new URL (s); |
|
104 |
client (s); |
|
105 |
//s = new String ("http://localhost:"+port+"/dr/d3/foo.html"); |
|
106 |
s = new String ("http://bar.com/dr/d3/foo.html"); |
|
107 |
urlsave = new URL (s); |
|
108 |
client (s); |
|
109 |
} catch (Exception e) { |
|
110 |
if (server != null) { |
|
111 |
server.terminate(); |
|
112 |
} |
|
113 |
throw e; |
|
114 |
} |
|
115 |
server.terminate(); |
|
116 |
} |
|
117 |
||
118 |
public static void except (String s) { |
|
119 |
server.terminate(); |
|
120 |
throw new RuntimeException (s); |
|
121 |
} |
|
122 |
||
123 |
static class MyAuthenticator extends Authenticator { |
|
124 |
int count = 0; |
|
125 |
MyAuthenticator () { |
|
126 |
super (); |
|
127 |
} |
|
128 |
||
129 |
public PasswordAuthentication getPasswordAuthentication () { |
|
130 |
URL url = getRequestingURL (); |
|
131 |
if (!url.equals (urlsave)) { |
|
132 |
except ("urls not equal"); |
|
133 |
} |
|
134 |
Authenticator.RequestorType expected; |
|
135 |
if (count == 0) { |
|
136 |
expected = Authenticator.RequestorType.SERVER; |
|
137 |
} else { |
|
138 |
expected = Authenticator.RequestorType.PROXY; |
|
139 |
} |
|
140 |
if (getRequestorType() != expected) { |
|
141 |
except ("wrong authtype"); |
|
142 |
} |
|
143 |
count ++; |
|
144 |
return (new PasswordAuthentication ("user", "passwordNotCheckedAnyway".toCharArray())); |
|
145 |
} |
|
146 |
||
147 |
} |
|
148 |
||
149 |
} |